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.graphics.Color; 25 import android.provider.Settings; 26 27 import androidx.fragment.app.Fragment; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settings.R; 32 import com.android.settings.core.TogglePreferenceController; 33 import com.android.settings.overlay.FeatureFactory; 34 35 import java.util.function.Consumer; 36 37 /** 38 * Controller for Screen flash notification. 39 */ 40 public class ScreenFlashNotificationPreferenceController extends TogglePreferenceController { 41 42 private Fragment mParentFragment; 43 private Preference mPreference; 44 ScreenFlashNotificationPreferenceController(Context context, String preferenceKey)45 public ScreenFlashNotificationPreferenceController(Context context, String preferenceKey) { 46 super(context, preferenceKey); 47 } 48 setParentFragment(Fragment parentFragment)49 public void setParentFragment(Fragment parentFragment) { 50 this.mParentFragment = parentFragment; 51 } 52 53 @Override getAvailabilityStatus()54 public int getAvailabilityStatus() { 55 return AVAILABLE; 56 } 57 58 @Override isChecked()59 public boolean isChecked() { 60 return Settings.System.getInt(mContext.getContentResolver(), 61 Settings.System.SCREEN_FLASH_NOTIFICATION, OFF) != OFF; 62 } 63 64 @Override setChecked(boolean isChecked)65 public boolean setChecked(boolean isChecked) { 66 FeatureFactory.getFeatureFactory().getMetricsFeatureProvider().changed( 67 getMetricsCategory(), getPreferenceKey(), isChecked ? 1 : 0); 68 if (isChecked) { 69 checkAndSetInitialColor(); 70 } 71 return Settings.System.putInt(mContext.getContentResolver(), 72 Settings.System.SCREEN_FLASH_NOTIFICATION, (isChecked ? ON : OFF)); 73 } 74 75 @Override getSliceHighlightMenuRes()76 public int getSliceHighlightMenuRes() { 77 return R.string.menu_key_accessibility; 78 } 79 80 @Override getSummary()81 public CharSequence getSummary() { 82 return FlashNotificationsUtil.getColorDescriptionText(mContext, 83 Settings.System.getInt(mContext.getContentResolver(), 84 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, 85 DEFAULT_SCREEN_FLASH_COLOR)); 86 } 87 88 @Override displayPreference(PreferenceScreen screen)89 public void displayPreference(PreferenceScreen screen) { 90 super.displayPreference(screen); 91 mPreference = screen.findPreference(getPreferenceKey()); 92 refreshColorSummary(); 93 } 94 95 @Override handlePreferenceTreeClick(Preference preference)96 public boolean handlePreferenceTreeClick(Preference preference) { 97 if (getPreferenceKey().equals(preference.getKey()) && mParentFragment != null) { 98 99 final int initialColor = Settings.System.getInt(mContext.getContentResolver(), 100 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, 101 DEFAULT_SCREEN_FLASH_COLOR); 102 103 final Consumer<Integer> consumer = color -> { 104 Settings.System.putInt(mContext.getContentResolver(), 105 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, color); 106 refreshColorSummary(); 107 }; 108 109 ScreenFlashNotificationColorDialogFragment 110 .getInstance(initialColor, consumer) 111 .show(mParentFragment.getParentFragmentManager(), 112 ScreenFlashNotificationColorDialogFragment.class.getSimpleName()); 113 return true; 114 } 115 116 return super.handlePreferenceTreeClick(preference); 117 } 118 checkAndSetInitialColor()119 private void checkAndSetInitialColor() { 120 if (Settings.System.getInt(mContext.getContentResolver(), 121 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, Color.TRANSPARENT) 122 == Color.TRANSPARENT) { 123 Settings.System.putInt(mContext.getContentResolver(), 124 Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, DEFAULT_SCREEN_FLASH_COLOR); 125 } 126 } 127 refreshColorSummary()128 private void refreshColorSummary() { 129 if (mPreference != null) mPreference.setSummary(getSummary()); 130 } 131 } 132