• 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 
17 package com.android.systemui.doze;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.database.ContentObserver;
23 import android.net.Uri;
24 import android.os.Handler;
25 import android.os.UserHandle;
26 import android.provider.Settings;
27 import android.text.format.DateUtils;
28 import android.util.KeyValueListParser;
29 import android.util.Log;
30 
31 import com.android.systemui.R;
32 import com.android.systemui.dagger.SysUISingleton;
33 
34 import javax.inject.Inject;
35 
36 /**
37  * Class to store the policy for AOD, which comes from
38  * {@link android.provider.Settings.Global}
39  */
40 @SysUISingleton
41 public class AlwaysOnDisplayPolicy {
42     public static final String TAG = "AlwaysOnDisplayPolicy";
43 
44     private static final long DEFAULT_PROX_SCREEN_OFF_DELAY_MS = 10 * DateUtils.SECOND_IN_MILLIS;
45     private static final long DEFAULT_PROX_COOLDOWN_TRIGGER_MS = 2 * DateUtils.SECOND_IN_MILLIS;
46     private static final long DEFAULT_PROX_COOLDOWN_PERIOD_MS = 5 * DateUtils.SECOND_IN_MILLIS;
47     private static final long DEFAULT_WALLPAPER_VISIBILITY_MS = 60 * DateUtils.SECOND_IN_MILLIS;
48     private static final long DEFAULT_WALLPAPER_FADE_OUT_MS = 400;
49 
50     static final String KEY_SCREEN_BRIGHTNESS_ARRAY = "screen_brightness_array";
51     static final String KEY_DIMMING_SCRIM_ARRAY = "dimming_scrim_array";
52     static final String KEY_PROX_SCREEN_OFF_DELAY_MS = "prox_screen_off_delay";
53     static final String KEY_PROX_COOLDOWN_TRIGGER_MS = "prox_cooldown_trigger";
54     static final String KEY_PROX_COOLDOWN_PERIOD_MS = "prox_cooldown_period";
55     static final String KEY_WALLPAPER_VISIBILITY_MS = "wallpaper_visibility_timeout";
56     static final String KEY_WALLPAPER_FADE_OUT_MS = "wallpaper_fade_out_duration";
57 
58 
59     /**
60      * Integer used to dim the screen while dozing.
61      *
62      * @see R.integer.config_screenBrightnessDoze
63      */
64     public int defaultDozeBrightness;
65 
66     /**
67      * Integer used to dim the screen just before the screen turns off.
68      *
69      * @see R.integer.config_screenBrightnessDim
70      */
71     public int dimBrightness;
72 
73     /**
74      * Integer array to map ambient brightness type to real screen brightness.
75      *
76      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
77      * @see #KEY_SCREEN_BRIGHTNESS_ARRAY
78      */
79     public int[] screenBrightnessArray;
80 
81     /**
82      * Integer array to map ambient brightness type to dimming scrim.
83      *
84      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
85      * @see #KEY_DIMMING_SCRIM_ARRAY
86      */
87     public int[] dimmingScrimArray;
88 
89     /**
90      * Delay time(ms) from covering the prox to turning off the screen.
91      *
92      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
93      * @see #KEY_PROX_SCREEN_OFF_DELAY_MS
94      */
95     public long proxScreenOffDelayMs;
96 
97     /**
98      * The threshold time(ms) to trigger the cooldown timer, which will
99      * turn off prox sensor for a period.
100      *
101      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
102      * @see #KEY_PROX_COOLDOWN_TRIGGER_MS
103      */
104     public long proxCooldownTriggerMs;
105 
106     /**
107      * The period(ms) to turning off the prox sensor if
108      * {@link #KEY_PROX_COOLDOWN_TRIGGER_MS} is triggered.
109      *
110      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
111      * @see #KEY_PROX_COOLDOWN_PERIOD_MS
112      */
113     public long proxCooldownPeriodMs;
114 
115     /**
116      * For how long(ms) the wallpaper should still be visible
117      * after entering AoD.
118      *
119      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
120      * @see #KEY_WALLPAPER_VISIBILITY_MS
121      */
122     public long wallpaperVisibilityDuration;
123 
124     /**
125      * Duration(ms) of the fade out animation after
126      * {@link #KEY_WALLPAPER_VISIBILITY_MS} elapses.
127      *
128      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
129      * @see #KEY_WALLPAPER_FADE_OUT_MS
130      */
131     public long wallpaperFadeOutDuration;
132 
133     private final KeyValueListParser mParser;
134     private final Context mContext;
135     private SettingsObserver mSettingsObserver;
136 
137     @Inject
AlwaysOnDisplayPolicy(Context context)138     public AlwaysOnDisplayPolicy(Context context) {
139         context = context.getApplicationContext();
140         mContext = context;
141         mParser = new KeyValueListParser(',');
142         mSettingsObserver = new SettingsObserver(context.getMainThreadHandler());
143         mSettingsObserver.observe();
144     }
145 
146     private final class SettingsObserver extends ContentObserver {
147         private final Uri ALWAYS_ON_DISPLAY_CONSTANTS_URI
148                 = Settings.Global.getUriFor(Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS);
149 
SettingsObserver(Handler handler)150         SettingsObserver(Handler handler) {
151             super(handler);
152         }
153 
observe()154         void observe() {
155             ContentResolver resolver = mContext.getContentResolver();
156             resolver.registerContentObserver(ALWAYS_ON_DISPLAY_CONSTANTS_URI,
157                     false, this, UserHandle.USER_ALL);
158             update(null);
159         }
160 
161         @Override
onChange(boolean selfChange, Uri uri)162         public void onChange(boolean selfChange, Uri uri) {
163             update(uri);
164         }
165 
update(Uri uri)166         public void update(Uri uri) {
167             if (uri == null || ALWAYS_ON_DISPLAY_CONSTANTS_URI.equals(uri)) {
168                 final Resources resources = mContext.getResources();
169                 final String value = Settings.Global.getString(mContext.getContentResolver(),
170                         Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS);
171 
172                 try {
173                     mParser.setString(value);
174                 } catch (IllegalArgumentException e) {
175                     Log.e(TAG, "Bad AOD constants");
176                 }
177 
178                 proxScreenOffDelayMs = mParser.getLong(KEY_PROX_SCREEN_OFF_DELAY_MS,
179                         DEFAULT_PROX_SCREEN_OFF_DELAY_MS);
180                 proxCooldownTriggerMs = mParser.getLong(KEY_PROX_COOLDOWN_TRIGGER_MS,
181                         DEFAULT_PROX_COOLDOWN_TRIGGER_MS);
182                 proxCooldownPeriodMs = mParser.getLong(KEY_PROX_COOLDOWN_PERIOD_MS,
183                         DEFAULT_PROX_COOLDOWN_PERIOD_MS);
184                 wallpaperFadeOutDuration = mParser.getLong(KEY_WALLPAPER_FADE_OUT_MS,
185                         DEFAULT_WALLPAPER_FADE_OUT_MS);
186                 wallpaperVisibilityDuration = mParser.getLong(KEY_WALLPAPER_VISIBILITY_MS,
187                         DEFAULT_WALLPAPER_VISIBILITY_MS);
188                 defaultDozeBrightness = resources.getInteger(
189                         com.android.internal.R.integer.config_screenBrightnessDoze);
190                 dimBrightness = resources.getInteger(
191                         com.android.internal.R.integer.config_screenBrightnessDim);
192                 screenBrightnessArray = mParser.getIntArray(KEY_SCREEN_BRIGHTNESS_ARRAY,
193                         resources.getIntArray(
194                                 R.array.config_doze_brightness_sensor_to_brightness));
195                 dimmingScrimArray = mParser.getIntArray(KEY_DIMMING_SCRIM_ARRAY,
196                         resources.getIntArray(
197                                 R.array.config_doze_brightness_sensor_to_scrim_opacity));
198             }
199         }
200     }
201 }
202