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 static com.android.internal.accessibility.AccessibilityShortcutController.REDUCE_BRIGHT_COLORS_TILE_SERVICE_COMPONENT_NAME;
20 
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.database.ContentObserver;
24 import android.hardware.display.ColorDisplayManager;
25 import android.net.Uri;
26 import android.os.Handler;
27 import android.os.Looper;
28 import android.os.UserHandle;
29 import android.provider.Settings;
30 import android.text.TextUtils;
31 
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceScreen;
34 
35 import com.android.server.display.feature.flags.Flags;
36 import com.android.settings.R;
37 import com.android.settingslib.PrimarySwitchPreference;
38 import com.android.settingslib.core.lifecycle.LifecycleObserver;
39 import com.android.settingslib.core.lifecycle.events.OnStart;
40 import com.android.settingslib.core.lifecycle.events.OnStop;
41 
42 /** PreferenceController that shows the Reduce Bright Colors summary */
43 public class ReduceBrightColorsPreferenceController
44         extends AccessibilityQuickSettingsPrimarySwitchPreferenceController
45         implements LifecycleObserver, OnStart, OnStop {
46     private ContentObserver mSettingsContentObserver;
47     private PrimarySwitchPreference mPreference;
48     private final Context mContext;
49     private final ColorDisplayManager mColorDisplayManager;
50 
ReduceBrightColorsPreferenceController(Context context, String preferenceKey)51     public ReduceBrightColorsPreferenceController(Context context,
52             String preferenceKey) {
53         super(context, preferenceKey);
54         mContext = context;
55         mSettingsContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())){
56             @Override
57             public void onChange(boolean selfChange, Uri uri) {
58                 final String path = uri == null ? null : uri.getLastPathSegment();
59                 if (TextUtils.equals(path, Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED)) {
60                     updateState(mPreference);
61                 }
62             }
63         };
64         mColorDisplayManager = mContext.getSystemService(ColorDisplayManager.class);
65     }
66 
67     @Override
isChecked()68     public boolean isChecked() {
69         return mColorDisplayManager.isReduceBrightColorsActivated();
70     }
71 
72     @Override
setChecked(boolean isChecked)73     public boolean setChecked(boolean isChecked) {
74         super.setChecked(isChecked);
75         return mColorDisplayManager.setReduceBrightColorsActivated(isChecked);
76     }
77 
78     @Override
getSummary()79     public CharSequence getSummary() {
80         return mContext.getText(
81                 R.string.reduce_bright_colors_preference_summary);
82     }
83 
84     @Override
updateState(Preference preference)85     public void updateState(Preference preference) {
86         super.updateState(preference);
87         refreshSummary(preference);
88     }
89 
90     @Override
getAvailabilityStatus()91     public int getAvailabilityStatus() {
92         // Successor to this feature is Even Dimmer
93         // found in display/EvenDimmerPreferenceController
94         // Only allow RBC if even dimmer is not possible on this device
95         if (Flags.evenDimmer() && mContext.getResources().getBoolean(
96                 com.android.internal.R.bool.config_evenDimmerEnabled)) {
97             return UNSUPPORTED_ON_DEVICE;
98         }
99 
100 
101         return ColorDisplayManager.isReduceBrightColorsAvailable(mContext) ? AVAILABLE
102                 : UNSUPPORTED_ON_DEVICE;
103     }
104 
105     @Override
displayPreference(PreferenceScreen screen)106     public void displayPreference(PreferenceScreen screen) {
107         super.displayPreference(screen);
108         mPreference = screen.findPreference(getPreferenceKey());
109     }
110 
111     @Override
getSliceHighlightMenuRes()112     public int getSliceHighlightMenuRes() {
113         return R.string.menu_key_accessibility;
114     }
115 
116     @Override
onStart()117     public void onStart() {
118         mContext.getContentResolver().registerContentObserver(Settings.Secure.getUriFor(
119                 Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED),
120                 false, mSettingsContentObserver, UserHandle.USER_CURRENT);
121     }
122 
123     @Override
onStop()124     public void onStop() {
125         mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
126     }
127 
128     @Override
getTileComponentName()129     protected ComponentName getTileComponentName() {
130         return REDUCE_BRIGHT_COLORS_TILE_SERVICE_COMPONENT_NAME;
131     }
132 
133     @Override
getTileTooltipContent()134     CharSequence getTileTooltipContent() {
135         return mContext.getText(
136                 R.string.accessibility_reduce_bright_colors_auto_added_qs_tooltip_content);
137     }
138 }
139