1 /*
2  * Copyright (C) 2021 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.hardware.display.ColorDisplayManager;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.provider.Settings;
24 import android.view.accessibility.Flags;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceCategory;
28 import androidx.preference.TwoStatePreference;
29 
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.settings.R;
32 import com.android.settings.dashboard.DashboardFragment;
33 import com.android.settings.search.BaseSearchIndexProvider;
34 import com.android.settingslib.core.AbstractPreferenceController;
35 import com.android.settingslib.search.SearchIndexable;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /** Accessibility settings for color and motion. */
41 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
42 public class ColorAndMotionFragment extends DashboardFragment {
43 
44     private static final String TAG = "ColorAndMotionFragment";
45 
46     private static final String CATEGORY_EXPERIMENTAL = "experimental_category";
47 
48     // Preferences
49     private static final String DISPLAY_DALTONIZER_PREFERENCE_SCREEN = "daltonizer_preference";
50     private static final String TOGGLE_DISABLE_ANIMATIONS = "toggle_disable_animations";
51     private static final String TOGGLE_LARGE_POINTER_ICON = "toggle_large_pointer_icon";
52     @VisibleForTesting
53     static final String TOGGLE_FORCE_INVERT = "toggle_force_invert";
54 
55     private Preference mDisplayDaltonizerPreferenceScreen;
56     private TwoStatePreference mToggleDisableAnimationsPreference;
57     private TwoStatePreference mToggleLargePointerIconPreference;
58     private AccessibilitySettingsContentObserver mSettingsContentObserver;
59 
60     private final List<String> mShortcutFeatureKeys = new ArrayList<>();
61 
62     @Override
getMetricsCategory()63     public int getMetricsCategory() {
64         return SettingsEnums.ACCESSIBILITY_COLOR_AND_MOTION;
65     }
66 
67     @Override
onCreate(Bundle icicle)68     public void onCreate(Bundle icicle) {
69         super.onCreate(icicle);
70         initializeAllPreferences();
71         updateSystemPreferences();
72 
73         mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
74         mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED);
75         mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE);
76         mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS);
77         if (android.view.accessibility.Flags.a11yQsShortcut()) {
78             mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_QS_TARGETS);
79         }
80         if (Flags.forceInvertColor()) {
81             mShortcutFeatureKeys.add(ToggleForceInvertPreferenceController.SETTINGS_KEY);
82         }
83 
84         mSettingsContentObserver = new AccessibilitySettingsContentObserver(new Handler());
85         mSettingsContentObserver.registerKeysToObserverCallback(mShortcutFeatureKeys,
86                 key -> updatePreferencesState());
87     }
88 
updatePreferencesState()89     private void updatePreferencesState() {
90         final List<AbstractPreferenceController> controllers = new ArrayList<>();
91         getPreferenceControllers().forEach(controllers::addAll);
92         controllers.forEach(controller -> controller.updateState(
93                 findPreference(controller.getPreferenceKey())));
94     }
95 
96     @Override
onStart()97     public void onStart() {
98         super.onStart();
99 
100         mSettingsContentObserver.register(getContentResolver());
101     }
102 
103     @Override
onStop()104     public void onStop() {
105         super.onStop();
106 
107         mSettingsContentObserver.unregister(getContentResolver());
108     }
109 
110     @Override
getPreferenceScreenResId()111     protected int getPreferenceScreenResId() {
112         return R.xml.accessibility_color_and_motion;
113     }
114 
115     @Override
getLogTag()116     protected String getLogTag() {
117         return TAG;
118     }
119 
initializeAllPreferences()120     private void initializeAllPreferences() {
121         // Display color adjustments.
122         mDisplayDaltonizerPreferenceScreen = findPreference(DISPLAY_DALTONIZER_PREFERENCE_SCREEN);
123 
124         // Disable animation.
125         mToggleDisableAnimationsPreference = findPreference(TOGGLE_DISABLE_ANIMATIONS);
126 
127         // Large pointer icon.
128         mToggleLargePointerIconPreference = findPreference(TOGGLE_LARGE_POINTER_ICON);
129     }
130 
131     /**
132      * Updates preferences related to system configurations.
133      */
updateSystemPreferences()134     private void updateSystemPreferences() {
135         final PreferenceCategory experimentalCategory = getPreferenceScreen().findPreference(
136                 CATEGORY_EXPERIMENTAL);
137         if (ColorDisplayManager.isColorTransformAccelerated(getContext())) {
138             getPreferenceScreen().removePreference(experimentalCategory);
139         } else {
140             // Move following preferences to experimental category if device don't supports HWC
141             // hardware-accelerated color transform.
142             getPreferenceScreen().removePreference(mDisplayDaltonizerPreferenceScreen);
143             getPreferenceScreen().removePreference(mToggleDisableAnimationsPreference);
144             getPreferenceScreen().removePreference(mToggleLargePointerIconPreference);
145             experimentalCategory.addPreference(mDisplayDaltonizerPreferenceScreen);
146             experimentalCategory.addPreference(mToggleDisableAnimationsPreference);
147             experimentalCategory.addPreference(mToggleLargePointerIconPreference);
148         }
149     }
150 
151     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
152             new BaseSearchIndexProvider(R.xml.accessibility_color_and_motion);
153 }
154