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