• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.accessibility;
18 
19 import static com.android.internal.accessibility.AccessibilityShortcutController.COLOR_INVERSION_COMPONENT_NAME;
20 import static com.android.internal.accessibility.AccessibilityShortcutController.COLOR_INVERSION_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 
25 import android.app.settings.SettingsEnums;
26 import android.content.ComponentName;
27 import android.content.ContentResolver;
28 import android.content.Context;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.provider.Settings;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 
36 import androidx.annotation.VisibleForTesting;
37 
38 import com.android.settings.R;
39 import com.android.settings.search.BaseSearchIndexProvider;
40 import com.android.settings.widget.SettingsMainSwitchPreference;
41 import com.android.settingslib.search.SearchIndexable;
42 import com.android.settingslib.search.SearchIndexableRaw;
43 
44 import java.util.ArrayList;
45 import java.util.List;
46 
47 /**
48  * Settings page for color inversion.
49  */
50 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
51 public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePreferenceFragment {
52 
53     private static final String TAG = "ToggleColorInversionPreferenceFragment";
54     private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED;
55 
56     @VisibleForTesting
57     static final String KEY_SHORTCUT_PREFERENCE = "color_inversion_shortcut_key";
58     @VisibleForTesting
59     static final String KEY_SWITCH_PREFERENCE = "color_inversion_switch_preference_key";
60 
61     @Override
registerKeysToObserverCallback( AccessibilitySettingsContentObserver contentObserver)62     protected void registerKeysToObserverCallback(
63             AccessibilitySettingsContentObserver contentObserver) {
64         super.registerKeysToObserverCallback(contentObserver);
65 
66         final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1);
67         enableServiceFeatureKeys.add(ENABLED);
68         contentObserver.registerKeysToObserverCallback(enableServiceFeatureKeys,
69                 key -> updateSwitchBarToggleSwitch());
70     }
71 
72     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)73     public View onCreateView(LayoutInflater inflater, ViewGroup container,
74             Bundle savedInstanceState) {
75         mComponentName = COLOR_INVERSION_COMPONENT_NAME;
76         mFeatureName = getText(R.string.accessibility_display_inversion_preference_title);
77         mHtmlDescription = getText(R.string.accessibility_display_inversion_preference_subtitle);
78         mTopIntroTitle = getText(R.string.accessibility_display_inversion_preference_intro_text);
79         mImageUri = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
80                 .authority(getPrefContext().getPackageName())
81                 .appendPath(String.valueOf(R.raw.a11y_color_inversion_banner))
82                 .build();
83         final View view = super.onCreateView(inflater, container, savedInstanceState);
84         updateFooterPreference();
85         return view;
86     }
87 
88     @Override
onViewCreated(View view, Bundle savedInstanceState)89     public void onViewCreated(View view, Bundle savedInstanceState) {
90         super.onViewCreated(view, savedInstanceState);
91         final View rootView = getActivity().getWindow().peekDecorView();
92         if (rootView != null) {
93             rootView.setAccessibilityPaneTitle(getString(
94                     R.string.accessibility_display_inversion_preference_title));
95         }
96     }
97 
98     @Override
getMetricsCategory()99     public int getMetricsCategory() {
100         return SettingsEnums.ACCESSIBILITY_COLOR_INVERSION_SETTINGS;
101     }
102 
103     @Override
onPreferenceToggled(String preferenceKey, boolean enabled)104     protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
105         final boolean isEnabled = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON;
106         if (enabled == isEnabled) {
107             return;
108         }
109         logAccessibilityServiceEnabled(mComponentName, enabled);
110         Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? ON : OFF);
111     }
112 
113     @Override
getPreferenceScreenResId()114     protected int getPreferenceScreenResId() {
115         return R.xml.accessibility_color_inversion_settings;
116     }
117 
118     @Override
getLogTag()119     protected String getLogTag() {
120         return TAG;
121     }
122 
123     @Override
onRemoveSwitchPreferenceToggleSwitch()124     protected void onRemoveSwitchPreferenceToggleSwitch() {
125         super.onRemoveSwitchPreferenceToggleSwitch();
126         mToggleServiceSwitchPreference.setOnPreferenceClickListener(null);
127     }
128 
129     @Override
updateToggleServiceTitle(SettingsMainSwitchPreference switchPreference)130     protected void updateToggleServiceTitle(SettingsMainSwitchPreference switchPreference) {
131         switchPreference.setTitle(R.string.accessibility_display_inversion_switch_title);
132     }
133 
134     @Override
getUseServicePreferenceKey()135     protected String getUseServicePreferenceKey() {
136         return KEY_SWITCH_PREFERENCE;
137     }
138 
139     @Override
getShortcutTitle()140     protected CharSequence getShortcutTitle() {
141         return getText(R.string.accessibility_display_inversion_shortcut_title);
142     }
143 
updateFooterPreference()144     private void updateFooterPreference() {
145         final String title = getPrefContext().getString(
146                 R.string.accessibility_color_inversion_about_title);
147         final String learnMoreText = getPrefContext().getString(
148                 R.string.accessibility_color_inversion_footer_learn_more_content_description);
149         mFooterPreferenceController.setIntroductionTitle(title);
150         mFooterPreferenceController.setupHelpLink(getHelpResource(), learnMoreText);
151         mFooterPreferenceController.displayPreference(getPreferenceScreen());
152     }
153 
154     @Override
onResume()155     public void onResume() {
156         super.onResume();
157         updateSwitchBarToggleSwitch();
158     }
159 
160     @Override
onPause()161     public void onPause() {
162         super.onPause();
163     }
164 
165     @Override
getHelpResource()166     public int getHelpResource() {
167         return R.string.help_url_color_inversion;
168     }
169 
170     @Override
getUserShortcutTypes()171     int getUserShortcutTypes() {
172         return AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(),
173                 mComponentName);
174     }
175 
176     @Override
getTileComponentName()177     ComponentName getTileComponentName() {
178         return COLOR_INVERSION_TILE_COMPONENT_NAME;
179     }
180 
181     @Override
updateSwitchBarToggleSwitch()182     protected void updateSwitchBarToggleSwitch() {
183         final boolean checked = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON;
184         if (mToggleServiceSwitchPreference.isChecked() == checked) {
185             return;
186         }
187         mToggleServiceSwitchPreference.setChecked(checked);
188     }
189 
190     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
191             new BaseSearchIndexProvider(R.xml.accessibility_color_inversion_settings) {
192                 @Override
193                 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
194                         boolean enabled) {
195                     final List<SearchIndexableRaw> rawData =
196                             super.getRawDataToIndex(context, enabled);
197 
198                     SearchIndexableRaw raw = new SearchIndexableRaw(context);
199                     raw.key = KEY_SHORTCUT_PREFERENCE;
200                     raw.title = context.getString(
201                             R.string.accessibility_display_inversion_shortcut_title);
202                     rawData.add(raw);
203 
204                     if (Flags.fixA11ySettingsSearch()) {
205                         SearchIndexableRaw mainPreferenceRaw = new SearchIndexableRaw(context);
206                         mainPreferenceRaw.key = KEY_SWITCH_PREFERENCE;
207                         mainPreferenceRaw.title = context.getString(
208                                 R.string.accessibility_display_inversion_switch_title);
209                         rawData.add(mainPreferenceRaw);
210                     }
211                     return rawData;
212                 }
213             };
214 }
215