1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.display;
15 
16 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
17 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
18 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
19 
20 import android.content.Context;
21 import android.os.Process;
22 import android.os.UserManager;
23 import android.provider.Settings;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.settings.R;
28 import com.android.settings.accessibility.Flags;
29 import com.android.settings.core.TogglePreferenceController;
30 import com.android.settingslib.PrimarySwitchPreference;
31 
32 public class AutoBrightnessPreferenceController extends TogglePreferenceController {
33 
34     private final String SYSTEM_KEY = SCREEN_BRIGHTNESS_MODE;
35     private final int DEFAULT_VALUE = SCREEN_BRIGHTNESS_MODE_MANUAL;
36 
37     private boolean mInSetupWizard;
38 
AutoBrightnessPreferenceController(Context context, String key)39     public AutoBrightnessPreferenceController(Context context, String key) {
40         super(context, key);
41     }
42 
setInSetupWizard(boolean inSetupWizard)43     public void setInSetupWizard(boolean inSetupWizard) {
44         mInSetupWizard = inSetupWizard;
45     }
46 
47     @Override
isChecked()48     public boolean isChecked() {
49         return Settings.System.getInt(mContext.getContentResolver(),
50                 SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
51     }
52 
53     @Override
setChecked(boolean isChecked)54     public boolean setChecked(boolean isChecked) {
55         Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY,
56                 isChecked ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : DEFAULT_VALUE);
57         return true;
58     }
59 
60     @Override
61     @AvailabilityStatus
getAvailabilityStatus()62     public int getAvailabilityStatus() {
63         if (!mContext.getResources().getBoolean(
64                 com.android.internal.R.bool.config_automatic_brightness_available)) {
65             return UNSUPPORTED_ON_DEVICE;
66         }
67         if (mInSetupWizard && !Flags.addBrightnessSettingsInSuw()) {
68             return CONDITIONALLY_UNAVAILABLE;
69         }
70         return AVAILABLE_UNSEARCHABLE;
71     }
72 
73     @Override
updateState(Preference preference)74     public void updateState(Preference preference) {
75         super.updateState(preference);
76         if (!(preference instanceof PrimarySwitchPreference)) {
77             return;
78         }
79 
80         PrimarySwitchPreference pref = (PrimarySwitchPreference) preference;
81         if (pref.isEnabled() && UserManager.get(mContext).hasBaseUserRestriction(
82                 UserManager.DISALLOW_CONFIG_BRIGHTNESS, Process.myUserHandle())) {
83             pref.setEnabled(false);
84             pref.setSwitchEnabled(false);
85         }
86     }
87 
88     @Override
getSummary()89     public CharSequence getSummary() {
90         return mContext.getText(isChecked()
91                 ? R.string.auto_brightness_summary_on
92                 : R.string.auto_brightness_summary_off);
93     }
94 
95     @Override
getSliceHighlightMenuRes()96     public int getSliceHighlightMenuRes() {
97         return R.string.menu_key_display;
98     }
99 }
100