1 /* 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 17 package com.android.settings.accessibility; 18 19 import static com.android.internal.accessibility.AccessibilityShortcutController.DALTONIZER_COMPONENT_NAME; 20 import static com.android.internal.accessibility.AccessibilityShortcutController.DALTONIZER_TILE_COMPONENT_NAME; 21 import static com.android.settings.accessibility.AccessibilityStatsLogUtils.logAccessibilityServiceEnabled; 22 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; 23 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 24 import static com.android.settings.accessibility.DaltonizerPreferenceUtil.isSecureAccessibilityDaltonizerEnabled; 25 26 import android.app.settings.SettingsEnums; 27 import android.content.ComponentName; 28 import android.content.Context; 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.annotation.VisibleForTesting; 36 37 import com.android.settings.R; 38 import com.android.settings.search.BaseSearchIndexProvider; 39 import com.android.settings.widget.SettingsMainSwitchPreference; 40 import com.android.settingslib.search.SearchIndexable; 41 import com.android.settingslib.search.SearchIndexableRaw; 42 43 import java.util.ArrayList; 44 import java.util.List; 45 46 /** Settings for daltonizer. */ 47 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) 48 public class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceFragment { 49 50 private static final String TAG = "ToggleDaltonizerPreferenceFragment"; 51 private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED; 52 private static final String KEY_PREVIEW = "daltonizer_preview"; 53 private static final String KEY_DEUTERANOMALY = "daltonizer_mode_deuteranomaly"; 54 private static final String KEY_PROTANOMALY = "daltonizer_mode_protanomaly"; 55 private static final String KEY_TRITANOMEALY = "daltonizer_mode_tritanomaly"; 56 private static final String KEY_GRAYSCALE = "daltonizer_mode_grayscale"; 57 58 @VisibleForTesting 59 static final String KEY_SHORTCUT_PREFERENCE = "daltonizer_shortcut_key"; 60 @VisibleForTesting 61 static final String KEY_SWITCH_PREFERENCE = "daltonizer_switch_preference_key"; 62 @VisibleForTesting 63 static final String KEY_SATURATION = "daltonizer_saturation"; 64 65 @Override registerKeysToObserverCallback( AccessibilitySettingsContentObserver contentObserver)66 protected void registerKeysToObserverCallback( 67 AccessibilitySettingsContentObserver contentObserver) { 68 super.registerKeysToObserverCallback(contentObserver); 69 70 final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1); 71 enableServiceFeatureKeys.add(ENABLED); 72 contentObserver.registerKeysToObserverCallback(enableServiceFeatureKeys, 73 key -> updateSwitchBarToggleSwitch()); 74 } 75 76 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)77 public View onCreateView(LayoutInflater inflater, ViewGroup container, 78 Bundle savedInstanceState) { 79 mComponentName = DALTONIZER_COMPONENT_NAME; 80 mFeatureName = getText(com.android.settingslib.R 81 .string.accessibility_display_daltonizer_preference_title); 82 mHtmlDescription = getText(com.android.settingslib.R 83 .string.accessibility_display_daltonizer_preference_subtitle); 84 mTopIntroTitle = getText(R.string.accessibility_daltonizer_about_intro_text); 85 final View view = super.onCreateView(inflater, container, savedInstanceState); 86 updateFooterPreference(); 87 return view; 88 } 89 90 @Override onViewCreated(View view, Bundle savedInstanceState)91 public void onViewCreated(View view, Bundle savedInstanceState) { 92 super.onViewCreated(view, savedInstanceState); 93 final View rootView = getActivity().getWindow().peekDecorView(); 94 if (rootView != null) { 95 rootView.setAccessibilityPaneTitle(getString(com.android.settingslib.R 96 .string.accessibility_display_daltonizer_preference_title)); 97 } 98 } 99 updateFooterPreference()100 private void updateFooterPreference() { 101 final String title = getPrefContext() 102 .getString(R.string.accessibility_daltonizer_about_title); 103 final String learnMoreText = getPrefContext() 104 .getString(R.string.accessibility_daltonizer_footer_learn_more_content_description); 105 mFooterPreferenceController.setIntroductionTitle(title); 106 mFooterPreferenceController.setupHelpLink(getHelpResource(), learnMoreText); 107 mFooterPreferenceController.displayPreference(getPreferenceScreen()); 108 } 109 110 /** Customizes the order by preference key. */ getPreferenceOrderList()111 protected List<String> getPreferenceOrderList() { 112 final List<String> lists = new ArrayList<>(); 113 lists.add(KEY_TOP_INTRO_PREFERENCE); 114 lists.add(KEY_PREVIEW); 115 lists.add(getUseServicePreferenceKey()); 116 // Putting saturation level close to the preview so users can see what is changing. 117 lists.add(KEY_SATURATION); 118 lists.add(KEY_DEUTERANOMALY); 119 lists.add(KEY_PROTANOMALY); 120 lists.add(KEY_TRITANOMEALY); 121 lists.add(KEY_GRAYSCALE); 122 lists.add(KEY_GENERAL_CATEGORY); 123 lists.add(KEY_HTML_DESCRIPTION_PREFERENCE); 124 return lists; 125 } 126 127 @Override onResume()128 public void onResume() { 129 super.onResume(); 130 updateSwitchBarToggleSwitch(); 131 } 132 133 @Override getMetricsCategory()134 public int getMetricsCategory() { 135 return SettingsEnums.ACCESSIBILITY_TOGGLE_DALTONIZER; 136 } 137 138 @Override getHelpResource()139 public int getHelpResource() { 140 return R.string.help_url_color_correction; 141 } 142 143 @Override getPreferenceScreenResId()144 protected int getPreferenceScreenResId() { 145 return R.xml.accessibility_daltonizer_settings; 146 } 147 148 @Override getLogTag()149 protected String getLogTag() { 150 return TAG; 151 } 152 153 @Override onPreferenceToggled(String preferenceKey, boolean enabled)154 protected void onPreferenceToggled(String preferenceKey, boolean enabled) { 155 final boolean isEnabled = 156 isSecureAccessibilityDaltonizerEnabled(getContentResolver()); 157 if (enabled == isEnabled) { 158 return; 159 } 160 logAccessibilityServiceEnabled(mComponentName, enabled); 161 Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? ON : OFF); 162 } 163 164 @Override onRemoveSwitchPreferenceToggleSwitch()165 protected void onRemoveSwitchPreferenceToggleSwitch() { 166 super.onRemoveSwitchPreferenceToggleSwitch(); 167 mToggleServiceSwitchPreference.setOnPreferenceClickListener(null); 168 } 169 170 @Override updateToggleServiceTitle(SettingsMainSwitchPreference switchPreference)171 protected void updateToggleServiceTitle(SettingsMainSwitchPreference switchPreference) { 172 switchPreference.setTitle(R.string.accessibility_daltonizer_primary_switch_title); 173 } 174 175 @Override getUseServicePreferenceKey()176 protected String getUseServicePreferenceKey() { 177 return KEY_SWITCH_PREFERENCE; 178 } 179 180 @Override getShortcutTitle()181 protected CharSequence getShortcutTitle() { 182 return getText(R.string.accessibility_daltonizer_shortcut_title); 183 } 184 185 @Override getUserShortcutTypes()186 int getUserShortcutTypes() { 187 return AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(), 188 mComponentName); 189 } 190 191 @Override getTileComponentName()192 ComponentName getTileComponentName() { 193 return DALTONIZER_TILE_COMPONENT_NAME; 194 } 195 196 @Override updateSwitchBarToggleSwitch()197 protected void updateSwitchBarToggleSwitch() { 198 final boolean checked = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON; 199 if (mToggleServiceSwitchPreference.isChecked() == checked) { 200 return; 201 } 202 mToggleServiceSwitchPreference.setChecked(checked); 203 } 204 205 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 206 new BaseSearchIndexProvider(R.xml.accessibility_daltonizer_settings) { 207 @Override 208 public List<SearchIndexableRaw> getRawDataToIndex(Context context, 209 boolean enabled) { 210 final List<SearchIndexableRaw> rawData = 211 super.getRawDataToIndex(context, enabled); 212 213 if (Flags.fixA11ySettingsSearch()) { 214 SearchIndexableRaw shortcutRaw = new SearchIndexableRaw(context); 215 shortcutRaw.key = KEY_SHORTCUT_PREFERENCE; 216 shortcutRaw.title = context.getString( 217 R.string.accessibility_daltonizer_shortcut_title); 218 rawData.add(shortcutRaw); 219 220 SearchIndexableRaw mainSwitchRaw = new SearchIndexableRaw(context); 221 mainSwitchRaw.key = KEY_SWITCH_PREFERENCE; 222 mainSwitchRaw.title = context.getString( 223 R.string.accessibility_daltonizer_primary_switch_title); 224 rawData.add(mainSwitchRaw); 225 } 226 return rawData; 227 } 228 }; 229 } 230