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 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 21 import static com.android.settings.accessibility.FlashNotificationsUtil.DEFAULT_SCREEN_FLASH_COLOR; 22 23 import android.content.Context; 24 import android.database.ContentObserver; 25 import android.graphics.Color; 26 import android.net.Uri; 27 import android.os.Handler; 28 import android.provider.Settings; 29 30 import androidx.annotation.NonNull; 31 import androidx.annotation.Nullable; 32 import androidx.fragment.app.Fragment; 33 import androidx.lifecycle.DefaultLifecycleObserver; 34 import androidx.lifecycle.LifecycleOwner; 35 import androidx.preference.Preference; 36 import androidx.preference.PreferenceScreen; 37 38 import com.android.settings.R; 39 import com.android.settings.core.TogglePreferenceController; 40 import com.android.settings.overlay.FeatureFactory; 41 42 /** 43 * Controller for Screen flash notification. 44 */ 45 public class ScreenFlashNotificationPreferenceController extends 46 TogglePreferenceController implements DefaultLifecycleObserver { 47 48 private final FlashNotificationColorContentObserver mFlashNotificationColorContentObserver; 49 50 private Fragment mParentFragment; 51 private Preference mPreference; 52 ScreenFlashNotificationPreferenceController(Context context, String preferenceKey)53 public ScreenFlashNotificationPreferenceController(Context context, String preferenceKey) { 54 super(context, preferenceKey); 55 mFlashNotificationColorContentObserver = new FlashNotificationColorContentObserver( 56 new Handler(mContext.getMainLooper())); 57 } 58 setParentFragment(Fragment parentFragment)59 public void setParentFragment(Fragment parentFragment) { 60 this.mParentFragment = parentFragment; 61 } 62 63 @Override onStart(@onNull LifecycleOwner owner)64 public void onStart(@NonNull LifecycleOwner owner) { 65 mFlashNotificationColorContentObserver.register(mContext); 66 } 67 68 @Override onStop(@onNull LifecycleOwner owner)69 public void onStop(@NonNull LifecycleOwner owner) { 70 mFlashNotificationColorContentObserver.unregister(mContext); 71 } 72 73 @Override getAvailabilityStatus()74 public int getAvailabilityStatus() { 75 return AVAILABLE; 76 } 77 78 @Override isChecked()79 public boolean isChecked() { 80 return Settings.System.getInt(mContext.getContentResolver(), 81 Settings.System.SCREEN_FLASH_NOTIFICATION, OFF) != OFF; 82 } 83 84 @Override setChecked(boolean isChecked)85 public boolean setChecked(boolean isChecked) { 86 FeatureFactory.getFeatureFactory().getMetricsFeatureProvider().changed( 87 getMetricsCategory(), getPreferenceKey(), isChecked ? 1 : 0); 88 if (isChecked) { 89 checkAndSetInitialColor(); 90 } 91 return Settings.System.putInt(mContext.getContentResolver(), 92 Settings.System.SCREEN_FLASH_NOTIFICATION, (isChecked ? ON : OFF)); 93 } 94 95 @Override getSliceHighlightMenuRes()96 public int getSliceHighlightMenuRes() { 97 return R.string.menu_key_accessibility; 98 } 99 100 @Override getSummary()101 public CharSequence getSummary() { 102 return FlashNotificationsUtil.getColorDescriptionText(mContext, 103 Settings.System.getInt(mContext.getContentResolver(), 104 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, 105 DEFAULT_SCREEN_FLASH_COLOR)); 106 } 107 108 @Override displayPreference(PreferenceScreen screen)109 public void displayPreference(PreferenceScreen screen) { 110 super.displayPreference(screen); 111 mPreference = screen.findPreference(getPreferenceKey()); 112 refreshColorSummary(); 113 } 114 115 @Override handlePreferenceTreeClick(Preference preference)116 public boolean handlePreferenceTreeClick(Preference preference) { 117 if (getPreferenceKey().equals(preference.getKey()) && mParentFragment != null) { 118 119 final int initialColor = Settings.System.getInt(mContext.getContentResolver(), 120 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, 121 DEFAULT_SCREEN_FLASH_COLOR); 122 123 ScreenFlashNotificationColorDialogFragment 124 .getInstance(initialColor) 125 .show(mParentFragment.getParentFragmentManager(), 126 ScreenFlashNotificationColorDialogFragment.class.getSimpleName()); 127 return true; 128 } 129 130 return super.handlePreferenceTreeClick(preference); 131 } 132 checkAndSetInitialColor()133 private void checkAndSetInitialColor() { 134 if (Settings.System.getInt(mContext.getContentResolver(), 135 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, Color.TRANSPARENT) 136 == Color.TRANSPARENT) { 137 Settings.System.putInt(mContext.getContentResolver(), 138 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, DEFAULT_SCREEN_FLASH_COLOR); 139 } 140 } 141 refreshColorSummary()142 private void refreshColorSummary() { 143 if (mPreference != null) mPreference.setSummary(getSummary()); 144 } 145 146 private final class FlashNotificationColorContentObserver extends ContentObserver { 147 private final Uri mColorUri = Settings.System.getUriFor( 148 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR); 149 FlashNotificationColorContentObserver(Handler handler)150 FlashNotificationColorContentObserver(Handler handler) { 151 super(handler); 152 } 153 154 /** 155 * Register this observer to given {@link Context}, to be called from lifecycle 156 * {@code onStart} method. 157 */ register(@onNull Context context)158 public void register(@NonNull Context context) { 159 context.getContentResolver().registerContentObserver( 160 mColorUri, /* notifyForDescendants= */ false, this); 161 } 162 163 /** 164 * Unregister this observer from given {@link Context}, to be called from lifecycle 165 * {@code onStop} method. 166 */ unregister(@onNull Context context)167 public void unregister(@NonNull Context context) { 168 context.getContentResolver().unregisterContentObserver(this); 169 } 170 171 @Override onChange(boolean selfChange, @Nullable Uri uri)172 public void onChange(boolean selfChange, @Nullable Uri uri) { 173 if (mColorUri.equals(uri)) { 174 refreshColorSummary(); 175 } 176 } 177 } 178 } 179