1 /* 2 * Copyright (C) 2019 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.car.developeroptions.accessibility; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.hardware.display.ColorDisplayManager; 22 import android.os.Bundle; 23 import android.provider.SearchIndexableResource; 24 import android.provider.Settings; 25 import android.view.accessibility.AccessibilityManager; 26 import android.widget.Switch; 27 28 import androidx.preference.ListPreference; 29 import androidx.preference.Preference; 30 31 import com.android.car.developeroptions.R; 32 import com.android.car.developeroptions.search.BaseSearchIndexProvider; 33 import com.android.car.developeroptions.widget.SwitchBar; 34 import com.android.settingslib.search.Indexable; 35 import com.android.settingslib.search.SearchIndexable; 36 37 import java.util.ArrayList; 38 import java.util.List; 39 40 @SearchIndexable 41 public class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceFragment 42 implements Preference.OnPreferenceChangeListener, SwitchBar.OnSwitchChangeListener { 43 private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED; 44 private static final String TYPE = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER; 45 private static final int DEFAULT_TYPE = AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY; 46 private static final String KEY_DALTONIZER_FOOTER = "daltonizer_footer"; 47 48 private ListPreference mType; 49 50 @Override getMetricsCategory()51 public int getMetricsCategory() { 52 return SettingsEnums.ACCESSIBILITY_TOGGLE_DALTONIZER; 53 } 54 55 @Override getHelpResource()56 public int getHelpResource() { 57 return R.string.help_url_color_correction; 58 } 59 60 @Override onCreate(Bundle savedInstanceState)61 public void onCreate(Bundle savedInstanceState) { 62 super.onCreate(savedInstanceState); 63 64 mType = (ListPreference) findPreference("type"); 65 66 final Preference footer = findPreference(KEY_DALTONIZER_FOOTER); 67 footer.setVisible(!ColorDisplayManager.isColorTransformAccelerated(getActivity())); 68 initPreferences(); 69 } 70 71 @Override getPreferenceScreenResId()72 protected int getPreferenceScreenResId() { 73 return R.xml.accessibility_daltonizer_settings; 74 } 75 76 @Override onPreferenceToggled(String preferenceKey, boolean enabled)77 protected void onPreferenceToggled(String preferenceKey, boolean enabled) { 78 Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? 1 : 0); 79 } 80 81 @Override onPreferenceChange(Preference preference, Object newValue)82 public boolean onPreferenceChange(Preference preference, Object newValue) { 83 if (preference == mType) { 84 Settings.Secure.putInt(getContentResolver(), TYPE, Integer.parseInt((String) newValue)); 85 preference.setSummary("%s"); 86 } 87 88 return true; 89 } 90 91 @Override onInstallSwitchBarToggleSwitch()92 protected void onInstallSwitchBarToggleSwitch() { 93 super.onInstallSwitchBarToggleSwitch(); 94 95 mSwitchBar.setCheckedInternal( 96 Settings.Secure.getInt(getContentResolver(), ENABLED, 0) == 1); 97 mSwitchBar.addOnSwitchChangeListener(this); 98 } 99 100 @Override onRemoveSwitchBarToggleSwitch()101 protected void onRemoveSwitchBarToggleSwitch() { 102 super.onRemoveSwitchBarToggleSwitch(); 103 mSwitchBar.removeOnSwitchChangeListener(this); 104 } 105 106 @Override updateSwitchBarText(SwitchBar switchBar)107 protected void updateSwitchBarText(SwitchBar switchBar) { 108 switchBar.setSwitchBarText(R.string.accessibility_daltonizer_master_switch_title, 109 R.string.accessibility_daltonizer_master_switch_title); 110 } 111 initPreferences()112 private void initPreferences() { 113 final String value = Integer.toString( 114 Settings.Secure.getInt(getContentResolver(), TYPE, DEFAULT_TYPE)); 115 mType.setValue(value); 116 mType.setOnPreferenceChangeListener(this); 117 final int index = mType.findIndexOfValue(value); 118 if (index < 0) { 119 // We're using a mode controlled by developer preferences. 120 mType.setSummary(getString(R.string.daltonizer_type_overridden, 121 getString(R.string.simulate_color_space))); 122 } 123 } 124 125 @Override onSwitchChanged(Switch switchView, boolean isChecked)126 public void onSwitchChanged(Switch switchView, boolean isChecked) { 127 onPreferenceToggled(mPreferenceKey, isChecked); 128 } 129 130 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 131 new BaseSearchIndexProvider() { 132 @Override 133 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, 134 boolean enabled) { 135 final ArrayList<SearchIndexableResource> result = new ArrayList<>(); 136 137 final SearchIndexableResource sir = new SearchIndexableResource(context); 138 sir.xmlResId = R.xml.accessibility_daltonizer_settings; 139 result.add(sir); 140 return result; 141 } 142 }; 143 144 } 145