1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.accessibility; 18 19 20 import static com.android.settings.accessibility.FlashNotificationsUtil.ACTION_FLASH_NOTIFICATION_START_PREVIEW; 21 import static com.android.settings.accessibility.FlashNotificationsUtil.EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE; 22 import static com.android.settings.accessibility.FlashNotificationsUtil.TYPE_SHORT_PREVIEW; 23 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.database.ContentObserver; 28 import android.net.Uri; 29 import android.os.Handler; 30 import android.os.Looper; 31 import android.os.UserHandle; 32 import android.provider.Settings; 33 34 import androidx.annotation.NonNull; 35 import androidx.annotation.Nullable; 36 import androidx.annotation.VisibleForTesting; 37 import androidx.lifecycle.Lifecycle; 38 import androidx.lifecycle.LifecycleEventObserver; 39 import androidx.lifecycle.LifecycleOwner; 40 import androidx.preference.Preference; 41 import androidx.preference.PreferenceScreen; 42 43 import com.android.settings.core.BasePreferenceController; 44 45 /** 46 * Controller for flash notifications preview. 47 */ 48 public class FlashNotificationsPreviewPreferenceController extends 49 BasePreferenceController implements LifecycleEventObserver { 50 51 private Preference mPreference; 52 private final ContentResolver mContentResolver; 53 54 @VisibleForTesting 55 final ContentObserver mContentObserver = new ContentObserver( 56 new Handler(Looper.getMainLooper())) { 57 @Override 58 public void onChange(boolean selfChange, @Nullable Uri uri) { 59 updateState(mPreference); 60 } 61 }; 62 FlashNotificationsPreviewPreferenceController(Context context, String preferenceKey)63 public FlashNotificationsPreviewPreferenceController(Context context, String preferenceKey) { 64 super(context, preferenceKey); 65 mContentResolver = context.getContentResolver(); 66 } 67 68 @Override getAvailabilityStatus()69 public int getAvailabilityStatus() { 70 return AVAILABLE; 71 } 72 73 @Override displayPreference(PreferenceScreen screen)74 public void displayPreference(PreferenceScreen screen) { 75 super.displayPreference(screen); 76 mPreference = screen.findPreference(getPreferenceKey()); 77 updateState(mPreference); 78 } 79 80 @Override handlePreferenceTreeClick(Preference preference)81 public boolean handlePreferenceTreeClick(Preference preference) { 82 if (getPreferenceKey().equals(preference.getKey())) { 83 Intent intent = new Intent(ACTION_FLASH_NOTIFICATION_START_PREVIEW); 84 intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_SHORT_PREVIEW); 85 mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM); 86 return true; 87 } 88 89 return super.handlePreferenceTreeClick(preference); 90 } 91 92 @Override onStateChanged(@onNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event)93 public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, 94 @NonNull Lifecycle.Event event) { 95 if (event == Lifecycle.Event.ON_RESUME) { 96 mContentResolver.registerContentObserver( 97 Settings.System.getUriFor(Settings.System.CAMERA_FLASH_NOTIFICATION), 98 /* notifyForDescendants= */ false, mContentObserver); 99 mContentResolver.registerContentObserver( 100 Settings.System.getUriFor(Settings.System.SCREEN_FLASH_NOTIFICATION), 101 /* notifyForDescendants= */ false, mContentObserver); 102 } else if (event == Lifecycle.Event.ON_PAUSE) { 103 mContentResolver.unregisterContentObserver(mContentObserver); 104 } 105 } 106 107 @Override updateState(Preference preference)108 public void updateState(Preference preference) { 109 super.updateState(preference); 110 if (preference == null) { 111 return; 112 } 113 preference.setEnabled(FlashNotificationsUtil.getFlashNotificationsState(mContext) 114 != FlashNotificationsUtil.State.OFF); 115 } 116 } 117