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 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.settings.R;
32 import com.android.settings.search.BaseSearchIndexProvider;
33 import com.android.settings.search.Indexable;
34 import com.android.settings.widget.SwitchBar;
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 
47     private ListPreference mType;
48 
49     @Override
getMetricsCategory()50     public int getMetricsCategory() {
51         return SettingsEnums.ACCESSIBILITY_TOGGLE_DALTONIZER;
52     }
53 
54     @Override
getHelpResource()55     public int getHelpResource() {
56         return R.string.help_url_color_correction;
57     }
58 
59     @Override
onCreate(Bundle savedInstanceState)60     public void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62 
63         mType = (ListPreference) findPreference("type");
64 
65         if (!ColorDisplayManager.isColorTransformAccelerated(getActivity())) {
66             mFooterPreferenceMixin.createFooterPreference().setTitle(
67                     R.string.accessibility_display_daltonizer_preference_subtitle);
68         }
69         initPreferences();
70     }
71 
72     @Override
getPreferenceScreenResId()73     protected int getPreferenceScreenResId() {
74         return R.xml.accessibility_daltonizer_settings;
75     }
76 
77     @Override
onPreferenceToggled(String preferenceKey, boolean enabled)78     protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
79         Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? 1 : 0);
80     }
81 
82     @Override
onPreferenceChange(Preference preference, Object newValue)83     public boolean onPreferenceChange(Preference preference, Object newValue) {
84         if (preference == mType) {
85             Settings.Secure.putInt(getContentResolver(), TYPE, Integer.parseInt((String) newValue));
86             preference.setSummary("%s");
87         }
88 
89         return true;
90     }
91 
92     @Override
onInstallSwitchBarToggleSwitch()93     protected void onInstallSwitchBarToggleSwitch() {
94         super.onInstallSwitchBarToggleSwitch();
95 
96         mSwitchBar.setCheckedInternal(
97                 Settings.Secure.getInt(getContentResolver(), ENABLED, 0) == 1);
98         mSwitchBar.addOnSwitchChangeListener(this);
99     }
100 
101     @Override
onRemoveSwitchBarToggleSwitch()102     protected void onRemoveSwitchBarToggleSwitch() {
103         super.onRemoveSwitchBarToggleSwitch();
104         mSwitchBar.removeOnSwitchChangeListener(this);
105     }
106 
107     @Override
updateSwitchBarText(SwitchBar switchBar)108     protected void updateSwitchBarText(SwitchBar switchBar) {
109         switchBar.setSwitchBarText(R.string.accessibility_daltonizer_master_switch_title,
110                 R.string.accessibility_daltonizer_master_switch_title);
111     }
112 
initPreferences()113     private void initPreferences() {
114         final String value = Integer.toString(
115                 Settings.Secure.getInt(getContentResolver(), TYPE, DEFAULT_TYPE));
116         mType.setValue(value);
117         mType.setOnPreferenceChangeListener(this);
118         final int index = mType.findIndexOfValue(value);
119         if (index < 0) {
120             // We're using a mode controlled by developer preferences.
121             mType.setSummary(getString(R.string.daltonizer_type_overridden,
122                     getString(R.string.simulate_color_space)));
123         }
124     }
125 
126     @Override
onSwitchChanged(Switch switchView, boolean isChecked)127     public void onSwitchChanged(Switch switchView, boolean isChecked) {
128         onPreferenceToggled(mPreferenceKey, isChecked);
129     }
130 
131     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
132             new BaseSearchIndexProvider() {
133                 @Override
134                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
135                         boolean enabled) {
136                     final ArrayList<SearchIndexableResource> result = new ArrayList<>();
137 
138                     final SearchIndexableResource sir = new SearchIndexableResource(context);
139                     sir.xmlResId = R.xml.accessibility_daltonizer_settings;
140                     result.add(sir);
141                     return result;
142                 }
143             };
144 
145 }
146