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.settings.accessibility.AccessibilityStatsLogUtils.logAccessibilityServiceEnabled; 21 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; 22 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 23 24 import android.app.settings.SettingsEnums; 25 import android.content.Context; 26 import android.content.res.Resources; 27 import android.net.Uri; 28 import android.os.Bundle; 29 import android.os.Handler; 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.Preference; 36 import androidx.preference.PreferenceScreen; 37 import androidx.preference.SwitchPreference; 38 39 import com.android.settings.R; 40 import com.android.settings.search.BaseSearchIndexProvider; 41 import com.android.settingslib.core.AbstractPreferenceController; 42 import com.android.settingslib.core.lifecycle.Lifecycle; 43 import com.android.settingslib.search.SearchIndexable; 44 45 import java.util.ArrayList; 46 import java.util.List; 47 48 /** Settings for daltonizer. */ 49 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) 50 public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceFragment 51 implements DaltonizerRadioButtonPreferenceController.OnChangeListener { 52 53 private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED; 54 private static final String KEY_PREVIEW = "daltonizer_preview"; 55 private static final String KEY_CATEGORY_MODE = "daltonizer_mode_category"; 56 private static final List<AbstractPreferenceController> sControllers = new ArrayList<>(); 57 private final Handler mHandler = new Handler(); 58 private SettingsContentObserver mSettingsContentObserver; 59 buildPreferenceControllers(Context context, Lifecycle lifecycle)60 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context, 61 Lifecycle lifecycle) { 62 if (sControllers.size() == 0) { 63 final Resources resources = context.getResources(); 64 final String[] daltonizerKeys = resources.getStringArray( 65 R.array.daltonizer_mode_keys); 66 67 for (int i = 0; i < daltonizerKeys.length; i++) { 68 sControllers.add(new DaltonizerRadioButtonPreferenceController( 69 context, lifecycle, daltonizerKeys[i])); 70 } 71 } 72 return sControllers; 73 } 74 75 @Override onCheckedChanged(Preference preference)76 public void onCheckedChanged(Preference preference) { 77 for (AbstractPreferenceController controller : sControllers) { 78 controller.updateState(preference); 79 } 80 } 81 82 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)83 public View onCreateView(LayoutInflater inflater, ViewGroup container, 84 Bundle savedInstanceState) { 85 mComponentName = DALTONIZER_COMPONENT_NAME; 86 mPackageName = getText(R.string.accessibility_display_daltonizer_preference_title); 87 mHtmlDescription = getText(R.string.accessibility_display_daltonizer_preference_subtitle); 88 final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1); 89 enableServiceFeatureKeys.add(ENABLED); 90 mSettingsContentObserver = new SettingsContentObserver(mHandler, enableServiceFeatureKeys) { 91 @Override 92 public void onChange(boolean selfChange, Uri uri) { 93 updateSwitchBarToggleSwitch(); 94 } 95 }; 96 return super.onCreateView(inflater, container, savedInstanceState); 97 } 98 99 @Override onViewCreated(View view, Bundle savedInstanceState)100 public void onViewCreated(View view, Bundle savedInstanceState) { 101 super.onViewCreated(view, savedInstanceState); 102 updatePreferenceOrder(); 103 } 104 105 /** Customizes the order by preference key. */ getPreferenceOrderList()106 private List<String> getPreferenceOrderList() { 107 List<String> lists = new ArrayList<>(); 108 lists.add(KEY_PREVIEW); 109 lists.add(KEY_USE_SERVICE_PREFERENCE); 110 lists.add(KEY_CATEGORY_MODE); 111 lists.add(KEY_GENERAL_CATEGORY); 112 lists.add(KEY_INTRODUCTION_CATEGORY); 113 return lists; 114 } 115 updatePreferenceOrder()116 private void updatePreferenceOrder() { 117 List<String> lists = getPreferenceOrderList(); 118 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 119 preferenceScreen.setOrderingAsAdded(false); 120 121 final int size = lists.size(); 122 for (int i = 0; i < size; i++) { 123 final Preference preference = preferenceScreen.findPreference(lists.get(i)); 124 if (preference != null) { 125 preference.setOrder(i); 126 } 127 } 128 } 129 130 @Override onResume()131 public void onResume() { 132 super.onResume(); 133 updateSwitchBarToggleSwitch(); 134 mSettingsContentObserver.register(getContentResolver()); 135 136 for (AbstractPreferenceController controller : 137 buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) { 138 ((DaltonizerRadioButtonPreferenceController) controller).setOnChangeListener(this); 139 ((DaltonizerRadioButtonPreferenceController) controller).displayPreference( 140 getPreferenceScreen()); 141 } 142 } 143 144 @Override onPause()145 public void onPause() { 146 mSettingsContentObserver.unregister(getContentResolver()); 147 for (AbstractPreferenceController controller : 148 buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) { 149 ((DaltonizerRadioButtonPreferenceController) controller).setOnChangeListener(null); 150 } 151 super.onPause(); 152 } 153 154 @Override getMetricsCategory()155 public int getMetricsCategory() { 156 return SettingsEnums.ACCESSIBILITY_TOGGLE_DALTONIZER; 157 } 158 159 @Override getHelpResource()160 public int getHelpResource() { 161 return R.string.help_url_color_correction; 162 } 163 164 @Override getPreferenceScreenResId()165 protected int getPreferenceScreenResId() { 166 return R.xml.accessibility_daltonizer_settings; 167 } 168 169 @Override onPreferenceToggled(String preferenceKey, boolean enabled)170 protected void onPreferenceToggled(String preferenceKey, boolean enabled) { 171 logAccessibilityServiceEnabled(mComponentName, enabled); 172 Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? ON : OFF); 173 } 174 175 @Override onRemoveSwitchPreferenceToggleSwitch()176 protected void onRemoveSwitchPreferenceToggleSwitch() { 177 super.onRemoveSwitchPreferenceToggleSwitch(); 178 mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener(null); 179 } 180 181 @Override updateToggleServiceTitle(SwitchPreference switchPreference)182 protected void updateToggleServiceTitle(SwitchPreference switchPreference) { 183 switchPreference.setTitle(R.string.accessibility_daltonizer_master_switch_title); 184 } 185 186 @Override onInstallSwitchPreferenceToggleSwitch()187 protected void onInstallSwitchPreferenceToggleSwitch() { 188 super.onInstallSwitchPreferenceToggleSwitch(); 189 updateSwitchBarToggleSwitch(); 190 mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener((preference) -> { 191 boolean checked = ((SwitchPreference) preference).isChecked(); 192 onPreferenceToggled(mPreferenceKey, checked); 193 return false; 194 }); 195 } 196 197 @Override onSettingsClicked(ShortcutPreference preference)198 public void onSettingsClicked(ShortcutPreference preference) { 199 super.onSettingsClicked(preference); 200 showDialog(DialogEnums.EDIT_SHORTCUT); 201 } 202 203 @Override getUserShortcutTypes()204 int getUserShortcutTypes() { 205 return AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(), 206 mComponentName); 207 } 208 updateSwitchBarToggleSwitch()209 private void updateSwitchBarToggleSwitch() { 210 final boolean checked = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON; 211 if (mToggleServiceDividerSwitchPreference.isChecked() == checked) { 212 return; 213 } 214 mToggleServiceDividerSwitchPreference.setChecked(checked); 215 } 216 217 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 218 new BaseSearchIndexProvider(R.xml.accessibility_daltonizer_settings); 219 } 220