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