blob: bc34e01ee8db4fc209b576916ac5ae004b16f39c [file] [log] [blame]
Alan Viverettefc0ab212013-10-02 16:36:33 -07001/*
2 * Copyright (C) 2013 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
17package com.android.settings.accessibility;
18
jasonwshsu8ef01d22019-12-26 18:39:27 +080019import static com.android.internal.accessibility.AccessibilityShortcutController.DALTONIZER_COMPONENT_NAME;
menghanli9e4a9d62022-01-25 03:55:42 +080020import static com.android.internal.accessibility.AccessibilityShortcutController.DALTONIZER_TILE_COMPONENT_NAME;
jasonwshsud2572c32020-05-09 05:34:59 +080021import static com.android.settings.accessibility.AccessibilityStatsLogUtils.logAccessibilityServiceEnabled;
menghanli1869e012020-02-04 17:32:10 +080022import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
23import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
jasonwshsu8ef01d22019-12-26 18:39:27 +080024
Fan Zhang31b21002019-01-16 13:49:47 -080025import android.app.settings.SettingsEnums;
menghanliba9f0972022-01-24 13:06:54 +080026import android.content.ComponentName;
Fan Zhangaf155222018-07-11 09:41:39 -070027import android.content.Context;
menghanli0eae5822019-12-10 19:55:13 +080028import android.content.res.Resources;
Kevin Change65ed3e2019-12-13 09:55:04 +080029import android.os.Bundle;
Alan Viverettefc0ab212013-10-02 16:36:33 -070030import android.provider.Settings;
Kevin Change65ed3e2019-12-13 09:55:04 +080031import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
Jason Monk39b46742015-09-10 15:52:51 -040034
menghanli9c9a5e42022-04-21 11:57:09 +080035import androidx.annotation.VisibleForTesting;
Fan Zhang23f8d592018-08-28 15:11:40 -070036import androidx.preference.Preference;
37
Alan Viverettefc0ab212013-10-02 16:36:33 -070038import com.android.settings.R;
menghanli01d02d32022-02-11 19:38:51 +080039import com.android.settings.accessibility.AccessibilityUtil.QuickSettingsTooltipType;
Fan Zhangaf155222018-07-11 09:41:39 -070040import com.android.settings.search.BaseSearchIndexProvider;
Stanley Wang86cc0572021-02-03 23:40:46 +080041import com.android.settings.widget.SettingsMainSwitchPreference;
menghanli0eae5822019-12-10 19:55:13 +080042import com.android.settingslib.core.AbstractPreferenceController;
43import com.android.settingslib.core.lifecycle.Lifecycle;
Fan Zhangaf155222018-07-11 09:41:39 -070044import com.android.settingslib.search.SearchIndexable;
45
46import java.util.ArrayList;
47import java.util.List;
Alan Viverettefc0ab212013-10-02 16:36:33 -070048
Hiroki Sato0e75d472020-04-10 13:19:32 +090049/** Settings for daltonizer. */
50@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
menghanli9c9a5e42022-04-21 11:57:09 +080051public class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceFragment
menghanli1869e012020-02-04 17:32:10 +080052 implements DaltonizerRadioButtonPreferenceController.OnChangeListener {
Alan Viverettefc0ab212013-10-02 16:36:33 -070053
jasonwshsu81456312022-07-10 11:59:04 +080054 private static final String TAG = "ToggleDaltonizerPreferenceFragment";
menghanli0eae5822019-12-10 19:55:13 +080055 private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED;
menghanli5638acb2020-02-10 13:58:24 +080056 private static final String KEY_PREVIEW = "daltonizer_preview";
menghanli9c9a5e42022-04-21 11:57:09 +080057 @VisibleForTesting
58 static final String KEY_DEUTERANOMALY = "daltonizer_mode_deuteranomaly";
59 @VisibleForTesting
60 static final String KEY_PROTANOMALY = "daltonizer_mode_protanomaly";
61 @VisibleForTesting
62 static final String KEY_TRITANOMEALY = "daltonizer_mode_tritanomaly";
63 @VisibleForTesting
64 static final String KEY_GRAYSCALE = "daltonizer_mode_grayscale";
menghanli0eae5822019-12-10 19:55:13 +080065 private static final List<AbstractPreferenceController> sControllers = new ArrayList<>();
66
menghanli65718902019-12-16 21:49:53 +080067 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
68 Lifecycle lifecycle) {
69 if (sControllers.size() == 0) {
70 final Resources resources = context.getResources();
71 final String[] daltonizerKeys = resources.getStringArray(
72 R.array.daltonizer_mode_keys);
73
jasonwshsu81456312022-07-10 11:59:04 +080074 for (String daltonizerKey : daltonizerKeys) {
menghanli65718902019-12-16 21:49:53 +080075 sControllers.add(new DaltonizerRadioButtonPreferenceController(
jasonwshsu81456312022-07-10 11:59:04 +080076 context, lifecycle, daltonizerKey));
menghanli65718902019-12-16 21:49:53 +080077 }
78 }
79 return sControllers;
80 }
81
Sally301d6212022-05-17 23:39:21 +000082
83 @Override
jasonwshsu81456312022-07-10 11:59:04 +080084 protected void registerKeysToObserverCallback(
85 AccessibilitySettingsContentObserver contentObserver) {
86 super.registerKeysToObserverCallback(contentObserver);
87
88 final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1);
89 enableServiceFeatureKeys.add(ENABLED);
90 contentObserver.registerKeysToObserverCallback(enableServiceFeatureKeys,
91 key -> updateSwitchBarToggleSwitch());
menghanli0eae5822019-12-10 19:55:13 +080092 }
93
94 @Override
Kevin Change65ed3e2019-12-13 09:55:04 +080095 public View onCreateView(LayoutInflater inflater, ViewGroup container,
96 Bundle savedInstanceState) {
menghanlib8293152020-01-03 14:10:50 +080097 mComponentName = DALTONIZER_COMPONENT_NAME;
Chaohui Wang25413812023-07-31 15:56:42 +080098 mPackageName = getText(com.android.settingslib.R
99 .string.accessibility_display_daltonizer_preference_title);
100 mHtmlDescription = getText(com.android.settingslib.R
101 .string.accessibility_display_daltonizer_preference_subtitle);
menghanli27e442d2022-02-13 14:12:06 +0800102 mTopIntroTitle = getText(R.string.accessibility_daltonizer_about_intro_text);
menghanlibae720f2021-07-09 13:29:26 +0800103 final View view = super.onCreateView(inflater, container, savedInstanceState);
104 updateFooterPreference();
105 return view;
Kevin Change65ed3e2019-12-13 09:55:04 +0800106 }
107
Daniel Hsieh55bc4982022-01-17 05:03:41 +0000108 @Override
jasonwshsu81456312022-07-10 11:59:04 +0800109 public void onViewCreated(View view, Bundle savedInstanceState) {
110 super.onViewCreated(view, savedInstanceState);
111 final View rootView = getActivity().getWindow().peekDecorView();
112 if (rootView != null) {
Chaohui Wang25413812023-07-31 15:56:42 +0800113 rootView.setAccessibilityPaneTitle(getString(com.android.settingslib.R
114 .string.accessibility_display_daltonizer_preference_title));
jasonwshsu81456312022-07-10 11:59:04 +0800115 }
116 }
Daniel Hsieh55bc4982022-01-17 05:03:41 +0000117
jasonwshsu81456312022-07-10 11:59:04 +0800118 @Override
119 public void onCheckedChanged(Preference preference) {
120 for (AbstractPreferenceController controller : sControllers) {
121 controller.updateState(preference);
122 }
Daniel Hsieh55bc4982022-01-17 05:03:41 +0000123 }
124
menghanlibae720f2021-07-09 13:29:26 +0800125 private void updateFooterPreference() {
126 final String title = getPrefContext()
127 .getString(R.string.accessibility_daltonizer_about_title);
Edgar Wang68d12952022-05-13 13:30:29 +0800128 final String learnMoreText = getPrefContext()
menghanlibae720f2021-07-09 13:29:26 +0800129 .getString(R.string.accessibility_daltonizer_footer_learn_more_content_description);
130 mFooterPreferenceController.setIntroductionTitle(title);
Edgar Wang68d12952022-05-13 13:30:29 +0800131 mFooterPreferenceController.setupHelpLink(getHelpResource(), learnMoreText);
menghanlibae720f2021-07-09 13:29:26 +0800132 mFooterPreferenceController.displayPreference(getPreferenceScreen());
menghanli5638acb2020-02-10 13:58:24 +0800133 }
134
135 /** Customizes the order by preference key. */
Stanley Wang86cc0572021-02-03 23:40:46 +0800136 protected List<String> getPreferenceOrderList() {
137 final List<String> lists = new ArrayList<>();
menghanli27e442d2022-02-13 14:12:06 +0800138 lists.add(KEY_TOP_INTRO_PREFERENCE);
Stanley Wang86cc0572021-02-03 23:40:46 +0800139 lists.add(KEY_PREVIEW);
menghanli1949a5d2021-05-10 19:39:58 +0800140 lists.add(KEY_USE_SERVICE_PREFERENCE);
menghanli27e442d2022-02-13 14:12:06 +0800141 lists.add(KEY_DEUTERANOMALY);
142 lists.add(KEY_PROTANOMALY);
143 lists.add(KEY_TRITANOMEALY);
144 lists.add(KEY_GRAYSCALE);
menghanli5638acb2020-02-10 13:58:24 +0800145 lists.add(KEY_GENERAL_CATEGORY);
menghanlicdcc3ae2021-04-22 22:38:00 +0800146 lists.add(KEY_HTML_DESCRIPTION_PREFERENCE);
menghanli5638acb2020-02-10 13:58:24 +0800147 return lists;
148 }
149
menghanli87147f52020-01-14 11:19:56 +0800150 @Override
menghanli0eae5822019-12-10 19:55:13 +0800151 public void onResume() {
152 super.onResume();
menghanli1869e012020-02-04 17:32:10 +0800153 updateSwitchBarToggleSwitch();
menghanli0eae5822019-12-10 19:55:13 +0800154 for (AbstractPreferenceController controller :
155 buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) {
menghanli65718902019-12-16 21:49:53 +0800156 ((DaltonizerRadioButtonPreferenceController) controller).setOnChangeListener(this);
157 ((DaltonizerRadioButtonPreferenceController) controller).displayPreference(
158 getPreferenceScreen());
menghanli0eae5822019-12-10 19:55:13 +0800159 }
160 }
161
162 @Override
163 public void onPause() {
menghanli0eae5822019-12-10 19:55:13 +0800164 for (AbstractPreferenceController controller :
165 buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) {
menghanli65718902019-12-16 21:49:53 +0800166 ((DaltonizerRadioButtonPreferenceController) controller).setOnChangeListener(null);
menghanli0eae5822019-12-10 19:55:13 +0800167 }
menghanlibf460322020-01-20 17:34:31 +0800168 super.onPause();
menghanli0eae5822019-12-10 19:55:13 +0800169 }
Alan Viverettefc0ab212013-10-02 16:36:33 -0700170
171 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700172 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800173 return SettingsEnums.ACCESSIBILITY_TOGGLE_DALTONIZER;
Chris Wren8a963ba2015-03-20 10:29:14 -0400174 }
175
176 @Override
Fan Zhange0b0e9f2017-11-29 14:55:59 -0800177 public int getHelpResource() {
Fan Zhang179645e2017-06-05 13:13:42 -0700178 return R.string.help_url_color_correction;
179 }
180
Alan Viverettefc0ab212013-10-02 16:36:33 -0700181 @Override
Doris Ling03a3b512017-10-18 14:25:01 -0700182 protected int getPreferenceScreenResId() {
183 return R.xml.accessibility_daltonizer_settings;
184 }
185
186 @Override
jasonwshsu81456312022-07-10 11:59:04 +0800187 protected String getLogTag() {
188 return TAG;
189 }
190
191 @Override
Alan Viverettefc0ab212013-10-02 16:36:33 -0700192 protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
menghanlia9e6e8c2022-04-18 21:50:36 +0800193 final boolean isEnabled = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON;
194 if (enabled == isEnabled) {
195 return;
196 }
197
menghanli01d02d32022-02-11 19:38:51 +0800198 if (enabled) {
199 showQuickSettingsTooltipIfNeeded(QuickSettingsTooltipType.GUIDE_TO_DIRECT_USE);
200 }
jasonwshsud2572c32020-05-09 05:34:59 +0800201 logAccessibilityServiceEnabled(mComponentName, enabled);
menghanli1869e012020-02-04 17:32:10 +0800202 Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? ON : OFF);
Fabrice Di Meglio62202752014-05-15 17:34:14 -0700203 }
204
205 @Override
menghanli6eb39122020-02-03 22:12:45 +0800206 protected void onRemoveSwitchPreferenceToggleSwitch() {
207 super.onRemoveSwitchPreferenceToggleSwitch();
Stanley Wang86cc0572021-02-03 23:40:46 +0800208 mToggleServiceSwitchPreference.setOnPreferenceClickListener(null);
Alan Viverettefc0ab212013-10-02 16:36:33 -0700209 }
210
Fan Zhang87d120a2018-01-11 13:31:43 -0800211 @Override
Stanley Wang86cc0572021-02-03 23:40:46 +0800212 protected void updateToggleServiceTitle(SettingsMainSwitchPreference switchPreference) {
Edgar Wangdd032d32020-08-06 00:34:35 +0800213 switchPreference.setTitle(R.string.accessibility_daltonizer_primary_switch_title);
Fan Zhang87d120a2018-01-11 13:31:43 -0800214 }
215
menghanli0eae5822019-12-10 19:55:13 +0800216 @Override
menghanli50520c22022-04-15 08:00:36 +0800217 protected CharSequence getShortcutTitle() {
218 return getText(R.string.accessibility_daltonizer_shortcut_title);
menghanlia606c432021-07-02 16:07:30 +0800219 }
220
221 @Override
Peter_Liangcfe3e452020-04-15 15:26:56 +0800222 int getUserShortcutTypes() {
223 return AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(),
224 mComponentName);
225 }
226
Stanley Wang86cc0572021-02-03 23:40:46 +0800227 @Override
menghanliba9f0972022-01-24 13:06:54 +0800228 ComponentName getTileComponentName() {
menghanli9e4a9d62022-01-25 03:55:42 +0800229 return DALTONIZER_TILE_COMPONENT_NAME;
menghanliba9f0972022-01-24 13:06:54 +0800230 }
231
232 @Override
menghanli99a91712022-03-08 10:50:03 +0800233 CharSequence getTileTooltipContent(@QuickSettingsTooltipType int type) {
234 return getText(type == QuickSettingsTooltipType.GUIDE_TO_EDIT
235 ? R.string.accessibility_color_correction_qs_tooltip_content
236 : R.string.accessibility_color_correction_auto_added_qs_tooltip_content);
menghanliba9f0972022-01-24 13:06:54 +0800237 }
238
239 @Override
Stanley Wang86cc0572021-02-03 23:40:46 +0800240 protected void updateSwitchBarToggleSwitch() {
menghanli1869e012020-02-04 17:32:10 +0800241 final boolean checked = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON;
Stanley Wang86cc0572021-02-03 23:40:46 +0800242 if (mToggleServiceSwitchPreference.isChecked() == checked) {
menghanli1869e012020-02-04 17:32:10 +0800243 return;
244 }
Stanley Wang86cc0572021-02-03 23:40:46 +0800245 mToggleServiceSwitchPreference.setChecked(checked);
menghanli1869e012020-02-04 17:32:10 +0800246 }
247
menghanlib7306042019-12-23 17:08:49 +0800248 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
249 new BaseSearchIndexProvider(R.xml.accessibility_daltonizer_settings);
Alan Viverettefc0ab212013-10-02 16:36:33 -0700250}