1 /* 2 * Copyright (C) 2020 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 android.app.settings.SettingsEnums; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.hardware.display.ColorDisplayManager; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.os.Handler; 26 import android.provider.Settings; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 31 import androidx.preference.PreferenceCategory; 32 import androidx.preference.SwitchPreference; 33 34 import com.android.internal.accessibility.AccessibilityShortcutController; 35 import com.android.settings.R; 36 import com.android.settings.search.BaseSearchIndexProvider; 37 import com.android.settings.widget.SeekBarPreference; 38 import com.android.settings.widget.SettingsMainSwitchPreference; 39 import com.android.settingslib.search.SearchIndexable; 40 41 import java.util.ArrayList; 42 import java.util.List; 43 44 /** Settings for reducing brightness. */ 45 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) 46 public class ToggleReduceBrightColorsPreferenceFragment extends ToggleFeaturePreferenceFragment { 47 48 private static final String REDUCE_BRIGHT_COLORS_ACTIVATED_KEY = 49 Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED; 50 private static final String KEY_INTENSITY = "rbc_intensity"; 51 private static final String KEY_PERSIST = "rbc_persist"; 52 53 private final Handler mHandler = new Handler(); 54 private SettingsContentObserver mSettingsContentObserver; 55 private ReduceBrightColorsIntensityPreferenceController mRbcIntensityPreferenceController; 56 private ReduceBrightColorsPersistencePreferenceController mRbcPersistencePreferenceController; 57 private ColorDisplayManager mColorDisplayManager; 58 59 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)60 public View onCreateView(LayoutInflater inflater, ViewGroup container, 61 Bundle savedInstanceState) { 62 63 mImageUri = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) 64 .authority(getPrefContext().getPackageName()) 65 .appendPath(String.valueOf(R.raw.extra_dim_banner)) 66 .build(); 67 mComponentName = AccessibilityShortcutController.REDUCE_BRIGHT_COLORS_COMPONENT_NAME; 68 mPackageName = getText(R.string.reduce_bright_colors_preference_title); 69 mHtmlDescription = getText(R.string.reduce_bright_colors_preference_subtitle); 70 final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1); 71 enableServiceFeatureKeys.add(REDUCE_BRIGHT_COLORS_ACTIVATED_KEY); 72 mRbcIntensityPreferenceController = 73 new ReduceBrightColorsIntensityPreferenceController(getContext(), KEY_INTENSITY); 74 mRbcPersistencePreferenceController = 75 new ReduceBrightColorsPersistencePreferenceController(getContext(), KEY_PERSIST); 76 mRbcIntensityPreferenceController.displayPreference(getPreferenceScreen()); 77 mRbcPersistencePreferenceController.displayPreference(getPreferenceScreen()); 78 mSettingsContentObserver = new SettingsContentObserver(mHandler, enableServiceFeatureKeys) { 79 @Override 80 public void onChange(boolean selfChange, Uri uri) { 81 updateSwitchBarToggleSwitch(); 82 } 83 }; 84 mColorDisplayManager = getContext().getSystemService(ColorDisplayManager.class); 85 final View view = super.onCreateView(inflater, container, savedInstanceState); 86 // Parent sets the title when creating the view, so set it after calling super 87 mToggleServiceSwitchPreference.setTitle(R.string.reduce_bright_colors_switch_title); 88 updateGeneralCategoryOrder(); 89 return view; 90 } 91 updateGeneralCategoryOrder()92 private void updateGeneralCategoryOrder() { 93 final PreferenceCategory generalCategory = findPreference(KEY_GENERAL_CATEGORY); 94 final SeekBarPreference intensity = findPreference(KEY_INTENSITY); 95 getPreferenceScreen().removePreference(intensity); 96 intensity.setOrder(mShortcutPreference.getOrder() - 2); 97 generalCategory.addPreference(intensity); 98 final SwitchPreference persist = findPreference(KEY_PERSIST); 99 getPreferenceScreen().removePreference(persist); 100 persist.setOrder(mShortcutPreference.getOrder() - 1); 101 generalCategory.addPreference(persist); 102 } 103 104 @Override onViewCreated(View view, Bundle savedInstanceState)105 public void onViewCreated(View view, Bundle savedInstanceState) { 106 super.onViewCreated(view, savedInstanceState); 107 } 108 109 @Override onResume()110 public void onResume() { 111 super.onResume(); 112 updateSwitchBarToggleSwitch(); 113 mSettingsContentObserver.register(getContentResolver()); 114 } 115 116 @Override onPause()117 public void onPause() { 118 mSettingsContentObserver.unregister(getContentResolver()); 119 super.onPause(); 120 } 121 122 @Override getMetricsCategory()123 public int getMetricsCategory() { 124 return SettingsEnums.REDUCE_BRIGHT_COLORS_SETTINGS; 125 } 126 127 @Override getHelpResource()128 public int getHelpResource() { 129 // TODO(170973645): Link to help support page 130 return 0; 131 } 132 133 @Override getPreferenceScreenResId()134 protected int getPreferenceScreenResId() { 135 return R.xml.reduce_bright_colors_settings; 136 } 137 138 139 @Override onPreferenceToggled(String preferenceKey, boolean enabled)140 protected void onPreferenceToggled(String preferenceKey, boolean enabled) { 141 AccessibilityStatsLogUtils.logAccessibilityServiceEnabled(mComponentName, enabled); 142 mColorDisplayManager.setReduceBrightColorsActivated(enabled); 143 } 144 145 @Override onRemoveSwitchPreferenceToggleSwitch()146 protected void onRemoveSwitchPreferenceToggleSwitch() { 147 super.onRemoveSwitchPreferenceToggleSwitch(); 148 mToggleServiceSwitchPreference.setOnPreferenceClickListener( 149 /* onPreferenceClickListener= */ null); 150 } 151 152 @Override updateToggleServiceTitle(SettingsMainSwitchPreference switchPreference)153 protected void updateToggleServiceTitle(SettingsMainSwitchPreference switchPreference) { 154 switchPreference.setTitle(R.string.reduce_bright_colors_preference_title); 155 } 156 157 @Override getUserShortcutTypes()158 int getUserShortcutTypes() { 159 return AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(), 160 mComponentName); 161 } 162 163 @Override updateSwitchBarToggleSwitch()164 protected void updateSwitchBarToggleSwitch() { 165 final boolean checked = mColorDisplayManager.isReduceBrightColorsActivated(); 166 mRbcIntensityPreferenceController.updateState(getPreferenceScreen() 167 .findPreference(KEY_INTENSITY)); 168 mRbcPersistencePreferenceController.updateState(getPreferenceScreen() 169 .findPreference(KEY_PERSIST)); 170 if (mToggleServiceSwitchPreference.isChecked() != checked) { 171 mToggleServiceSwitchPreference.setChecked(checked); 172 } 173 } 174 175 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 176 new BaseSearchIndexProvider(R.xml.reduce_bright_colors_settings) { 177 @Override 178 protected boolean isPageSearchEnabled(Context context) { 179 return ColorDisplayManager.isReduceBrightColorsAvailable(context); 180 } 181 }; 182 } 183