• 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 
33 import java.util.Arrays;
34 
35 /**
36  * Class to store the policy for AOD, which comes from
37  * {@link android.provider.Settings.Global}
38  */
39 public class AlwaysOnDisplayPolicy {
40     public static final String TAG = "AlwaysOnDisplayPolicy";
41 
42     private static final long DEFAULT_PROX_SCREEN_OFF_DELAY_MS = 10 * DateUtils.SECOND_IN_MILLIS;
43     private static final long DEFAULT_PROX_COOLDOWN_TRIGGER_MS = 2 * DateUtils.SECOND_IN_MILLIS;
44     private static final long DEFAULT_PROX_COOLDOWN_PERIOD_MS = 5 * DateUtils.SECOND_IN_MILLIS;
45 
46     static final String KEY_SCREEN_BRIGHTNESS_ARRAY = "screen_brightness_array";
47     static final String KEY_DIMMING_SCRIM_ARRAY = "dimming_scrim_array";
48     static final String KEY_PROX_SCREEN_OFF_DELAY_MS = "prox_screen_off_delay";
49     static final String KEY_PROX_COOLDOWN_TRIGGER_MS = "prox_cooldown_trigger";
50     static final String KEY_PROX_COOLDOWN_PERIOD_MS = "prox_cooldown_period";
51 
52     /**
53      * Integer array to map ambient brightness type to real screen brightness.
54      *
55      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
56      * @see #KEY_SCREEN_BRIGHTNESS_ARRAY
57      */
58     public int[] screenBrightnessArray;
59 
60     /**
61      * Integer array to map ambient brightness type to dimming scrim.
62      *
63      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
64      * @see #KEY_DIMMING_SCRIM_ARRAY
65      */
66     public int[] dimmingScrimArray;
67 
68     /**
69      * Delay time(ms) from covering the prox to turning off the screen.
70      *
71      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
72      * @see #KEY_PROX_SCREEN_OFF_DELAY_MS
73      */
74     public long proxScreenOffDelayMs;
75 
76     /**
77      * The threshold time(ms) to trigger the cooldown timer, which will
78      * turn off prox sensor for a period.
79      *
80      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
81      * @see #KEY_PROX_COOLDOWN_TRIGGER_MS
82      */
83     public long proxCooldownTriggerMs;
84 
85     /**
86      * The period(ms) to turning off the prox sensor if
87      * {@link #KEY_PROX_COOLDOWN_TRIGGER_MS} is triggered.
88      *
89      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
90      * @see #KEY_PROX_COOLDOWN_PERIOD_MS
91      */
92     public long proxCooldownPeriodMs;
93 
94     private final KeyValueListParser mParser;
95     private final Context mContext;
96     private SettingsObserver mSettingsObserver;
97 
AlwaysOnDisplayPolicy(Context context)98     public AlwaysOnDisplayPolicy(Context context) {
99         mContext = context;
100         mParser = new KeyValueListParser(',');
101         mSettingsObserver = new SettingsObserver(context.getMainThreadHandler());
102         mSettingsObserver.observe();
103     }
104 
parseIntArray(final String key, final int[] defaultArray)105     private int[] parseIntArray(final String key, final int[] defaultArray) {
106         final String value = mParser.getString(key, null);
107         if (value != null) {
108             try {
109                 return Arrays.stream(value.split(":")).map(String::trim).mapToInt(
110                         Integer::parseInt).toArray();
111             } catch (NumberFormatException e) {
112                 return defaultArray;
113             }
114         } else {
115             return defaultArray;
116         }
117     }
118 
119     private final class SettingsObserver extends ContentObserver {
120         private final Uri ALWAYS_ON_DISPLAY_CONSTANTS_URI
121                 = Settings.Global.getUriFor(Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS);
122 
SettingsObserver(Handler handler)123         SettingsObserver(Handler handler) {
124             super(handler);
125         }
126 
observe()127         void observe() {
128             ContentResolver resolver = mContext.getContentResolver();
129             resolver.registerContentObserver(ALWAYS_ON_DISPLAY_CONSTANTS_URI,
130                     false, this, UserHandle.USER_ALL);
131             update(null);
132         }
133 
134         @Override
onChange(boolean selfChange, Uri uri)135         public void onChange(boolean selfChange, Uri uri) {
136             update(uri);
137         }
138 
update(Uri uri)139         public void update(Uri uri) {
140             if (uri == null || ALWAYS_ON_DISPLAY_CONSTANTS_URI.equals(uri)) {
141                 final Resources resources = mContext.getResources();
142                 final String value = Settings.Global.getString(mContext.getContentResolver(),
143                         Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS);
144 
145                 try {
146                     mParser.setString(value);
147                 } catch (IllegalArgumentException e) {
148                     Log.e(TAG, "Bad AOD constants");
149                 }
150 
151                 proxScreenOffDelayMs = mParser.getLong(KEY_PROX_SCREEN_OFF_DELAY_MS,
152                         DEFAULT_PROX_SCREEN_OFF_DELAY_MS);
153                 proxCooldownTriggerMs = mParser.getLong(KEY_PROX_COOLDOWN_TRIGGER_MS,
154                         DEFAULT_PROX_COOLDOWN_TRIGGER_MS);
155                 proxCooldownPeriodMs = mParser.getLong(KEY_PROX_COOLDOWN_PERIOD_MS,
156                         DEFAULT_PROX_COOLDOWN_PERIOD_MS);
157                 screenBrightnessArray = parseIntArray(KEY_SCREEN_BRIGHTNESS_ARRAY,
158                         resources.getIntArray(
159                                 R.array.config_doze_brightness_sensor_to_brightness));
160                 dimmingScrimArray = parseIntArray(KEY_DIMMING_SCRIM_ARRAY,
161                         resources.getIntArray(
162                                 R.array.config_doze_brightness_sensor_to_scrim_opacity));
163             }
164         }
165     }
166 }
167