• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.settings.display;
17 
18 import android.content.Context;
19 import android.content.pm.PackageManager;
20 import android.hardware.display.AmbientDisplayConfiguration;
21 import android.os.PowerManager;
22 import android.os.SystemProperties;
23 import android.os.UserHandle;
24 import android.provider.Settings;
25 import android.text.TextUtils;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.TogglePreferenceController;
31 
32 // LINT.IfChange
33 public class AmbientDisplayAlwaysOnPreferenceController extends TogglePreferenceController {
34 
35     private final int ON = 1;
36     private final int OFF = 0;
37 
38     private static final int MY_USER = UserHandle.myUserId();
39     private static final String PROP_AWARE_AVAILABLE = "ro.vendor.aware_available";
40     private static final String AOD_SUPPRESSED_TOKEN = "winddown";
41 
42     private AmbientDisplayConfiguration mConfig;
43 
AmbientDisplayAlwaysOnPreferenceController(Context context, String key)44     public AmbientDisplayAlwaysOnPreferenceController(Context context, String key) {
45         super(context, key);
46     }
47 
48     @Override
getAvailabilityStatus()49     public int getAvailabilityStatus() {
50         return isAvailable(getConfig())
51                 && !SystemProperties.getBoolean(PROP_AWARE_AVAILABLE, false) ?
52                 AVAILABLE : UNSUPPORTED_ON_DEVICE;
53     }
54 
55     @Override
updateState(Preference preference)56     public void updateState(Preference preference) {
57         super.updateState(preference);
58         refreshSummary(preference);
59     }
60 
61     @Override
isSliceable()62     public boolean isSliceable() {
63         return true;
64     }
65 
66     @Override
isPublicSlice()67     public boolean isPublicSlice() {
68         return TextUtils.equals(getPreferenceKey(), "ambient_display_always_on");
69     }
70 
71     @Override
getSliceHighlightMenuRes()72     public int getSliceHighlightMenuRes() {
73         return R.string.menu_key_display;
74     }
75 
76     @Override
isChecked()77     public boolean isChecked() {
78         return getConfig().alwaysOnEnabled(MY_USER);
79     }
80 
81     @Override
setChecked(boolean isChecked)82     public boolean setChecked(boolean isChecked) {
83         int enabled = isChecked ? ON : OFF;
84         Settings.Secure.putInt(
85                 mContext.getContentResolver(), Settings.Secure.DOZE_ALWAYS_ON, enabled);
86         return true;
87     }
88 
89     @Override
getSummary()90     public CharSequence getSummary() {
91         return mContext.getText(
92                 isAodSuppressedByBedtime(mContext) ? R.string.aware_summary_when_bedtime_on
93                         : R.string.doze_always_on_summary);
94     }
95 
setConfig( AmbientDisplayConfiguration config)96     public AmbientDisplayAlwaysOnPreferenceController setConfig(
97             AmbientDisplayConfiguration config) {
98         mConfig = config;
99         return this;
100     }
101 
isAvailable(AmbientDisplayConfiguration config)102     public static boolean isAvailable(AmbientDisplayConfiguration config) {
103         return config.alwaysOnAvailableForUser(MY_USER);
104     }
105 
getConfig()106     private AmbientDisplayConfiguration getConfig() {
107         if (mConfig == null) {
108             mConfig = new AmbientDisplayConfiguration(mContext);
109         }
110         return mConfig;
111     }
112 
113     /**
114      * Returns whether AOD is suppressed by Bedtime mode, a feature of Digital Wellbeing.
115      *
116      * We know that Bedtime mode suppresses AOD using {@link AOD_SUPPRESSED_TOKEN}. If the Digital
117      * Wellbeing app is suppressing AOD with {@link AOD_SUPPRESSED_TOKEN}, then we can infer that
118      * AOD is being suppressed by Bedtime mode.
119      */
isAodSuppressedByBedtime(Context context)120     public static boolean isAodSuppressedByBedtime(Context context) {
121         int uid;
122         final PowerManager powerManager = context.getSystemService(PowerManager.class);
123         final PackageManager packageManager = context.getPackageManager();
124         final String packageName = context.getString(
125                 com.android.internal.R.string.config_systemWellbeing);
126         try {
127             uid = packageManager.getApplicationInfo(packageName, /* flags= */ 0).uid;
128         } catch (PackageManager.NameNotFoundException e) {
129             return false;
130         }
131         return powerManager.isAmbientDisplaySuppressedForTokenByApp(AOD_SUPPRESSED_TOKEN, uid);
132     }
133 }
134 // LINT.ThenChange(AmbientDisplayAlwaysOnPreference.kt)
135