blob: 85fac16d1c7bf67869ce463e84c5a8a0554933e2 [file] [log] [blame]
John Spurlockd06aa572014-09-10 10:40:49 -04001/*
2 * Copyright (C) 2014 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
17package com.android.systemui.statusbar.phone;
18
19import android.content.Context;
Lucas Dupin16cfe452018-02-08 13:14:50 -080020import android.os.PowerManager;
John Spurlockd06aa572014-09-10 10:40:49 -040021import android.os.SystemProperties;
Adrian Roosb7e4e102016-10-14 13:03:45 -070022import android.os.UserHandle;
Adrian Roosebea7a72016-10-26 12:51:26 -070023import android.provider.Settings;
John Spurlockd06aa572014-09-10 10:40:49 -040024import android.text.TextUtils;
John Spurlockd06aa572014-09-10 10:40:49 -040025import android.util.MathUtils;
Adrian Roos5753f052016-07-13 10:30:37 -070026import android.util.SparseBooleanArray;
John Spurlockd06aa572014-09-10 10:40:49 -040027
Lucas Dupin16cfe452018-02-08 13:14:50 -080028import com.android.internal.annotations.VisibleForTesting;
Adrian Roos0261fb22017-03-07 20:20:35 +000029import com.android.internal.hardware.AmbientDisplayConfiguration;
Lucas Dupindff8cdf2018-01-09 13:01:01 -080030import com.android.systemui.Dependency;
John Spurlockd06aa572014-09-10 10:40:49 -040031import com.android.systemui.R;
Lucas Dupin82aa1632017-12-13 00:13:57 -080032import com.android.systemui.doze.AlwaysOnDisplayPolicy;
Lucas Dupincbe05962018-04-26 16:44:05 -070033import com.android.systemui.doze.DozeScreenState;
Lucas Dupindff8cdf2018-01-09 13:01:01 -080034import com.android.systemui.tuner.TunerService;
John Spurlockd06aa572014-09-10 10:40:49 -040035
36import java.io.PrintWriter;
John Spurlockd06aa572014-09-10 10:40:49 -040037
Lucas Dupindff8cdf2018-01-09 13:01:01 -080038public class DozeParameters implements TunerService.Tunable {
Selim Cinekbb998c92015-09-22 10:05:29 +020039 private static final int MAX_DURATION = 60 * 1000;
Adrian Roos7a1654e2017-02-14 14:06:29 +010040 public static final String DOZE_SENSORS_WAKE_UP_FULLY = "doze_sensors_wake_up_fully";
John Spurlockd06aa572014-09-10 10:40:49 -040041
Lucas Dupin16cfe452018-02-08 13:14:50 -080042 private static IntInOutMatcher sPickupSubtypePerformsProxMatcher;
43 private static DozeParameters sInstance;
44
John Spurlockd06aa572014-09-10 10:40:49 -040045 private final Context mContext;
Adrian Roos0261fb22017-03-07 20:20:35 +000046 private final AmbientDisplayConfiguration mAmbientDisplayConfiguration;
Lucas Dupin16cfe452018-02-08 13:14:50 -080047 private final PowerManager mPowerManager;
John Spurlockd06aa572014-09-10 10:40:49 -040048
Lucas Dupin82aa1632017-12-13 00:13:57 -080049 private final AlwaysOnDisplayPolicy mAlwaysOnPolicy;
Adrian Roos5753f052016-07-13 10:30:37 -070050
Lucas Dupindff8cdf2018-01-09 13:01:01 -080051 private boolean mDozeAlwaysOn;
Lucas Dupin16cfe452018-02-08 13:14:50 -080052 private boolean mControlScreenOffAnimation;
Lucas Dupindff8cdf2018-01-09 13:01:01 -080053
Lucas Dupin16cfe452018-02-08 13:14:50 -080054 public static DozeParameters getInstance(Context context) {
55 if (sInstance == null) {
56 sInstance = new DozeParameters(context);
57 }
58 return sInstance;
59 }
60
61 @VisibleForTesting
62 protected DozeParameters(Context context) {
Adrian Roos2f5a3852018-04-23 17:48:08 +020063 mContext = context.getApplicationContext();
Adrian Roos0261fb22017-03-07 20:20:35 +000064 mAmbientDisplayConfiguration = new AmbientDisplayConfiguration(mContext);
Adrian Roos2f5a3852018-04-23 17:48:08 +020065 mAlwaysOnPolicy = new AlwaysOnDisplayPolicy(mContext);
Lucas Dupindff8cdf2018-01-09 13:01:01 -080066
Lucas Dupin16cfe452018-02-08 13:14:50 -080067 mControlScreenOffAnimation = !getDisplayNeedsBlanking();
68 mPowerManager = mContext.getSystemService(PowerManager.class);
69 mPowerManager.setDozeAfterScreenOff(!mControlScreenOffAnimation);
70
Lucas Dupindff8cdf2018-01-09 13:01:01 -080071 Dependency.get(TunerService.class).addTunable(this, Settings.Secure.DOZE_ALWAYS_ON,
72 Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
John Spurlockd06aa572014-09-10 10:40:49 -040073 }
74
75 public void dump(PrintWriter pw) {
76 pw.println(" DozeParameters:");
John Spurlock190d0262014-09-14 15:39:13 -040077 pw.print(" getDisplayStateSupported(): "); pw.println(getDisplayStateSupported());
Lucas Dupin9e3fa102017-11-08 17:16:55 -080078 pw.print(" getPulseDuration(): "); pw.println(getPulseDuration());
79 pw.print(" getPulseInDuration(): "); pw.println(getPulseInDuration());
John Spurlockd06aa572014-09-10 10:40:49 -040080 pw.print(" getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration());
81 pw.print(" getPulseOutDuration(): "); pw.println(getPulseOutDuration());
John Spurlock190d0262014-09-14 15:39:13 -040082 pw.print(" getPulseOnSigMotion(): "); pw.println(getPulseOnSigMotion());
83 pw.print(" getVibrateOnSigMotion(): "); pw.println(getVibrateOnSigMotion());
John Spurlock190d0262014-09-14 15:39:13 -040084 pw.print(" getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
John Spurlock686e4d52014-11-20 21:48:09 -050085 pw.print(" getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse());
John Spurlock50a8ea62014-09-16 09:12:03 -040086 pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
Adrian Roos5753f052016-07-13 10:30:37 -070087 pw.print(" getPickupSubtypePerformsProxCheck(): ");pw.println(
88 dumpPickupSubtypePerformsProxCheck());
89 }
90
91 private String dumpPickupSubtypePerformsProxCheck() {
92 // Refresh sPickupSubtypePerformsProxMatcher
93 getPickupSubtypePerformsProxCheck(0);
94
95 if (sPickupSubtypePerformsProxMatcher == null) {
96 return "fallback: " + mContext.getResources().getBoolean(
97 R.bool.doze_pickup_performs_proximity_check);
98 } else {
99 return "spec: " + sPickupSubtypePerformsProxMatcher.mSpec;
100 }
John Spurlock190d0262014-09-14 15:39:13 -0400101 }
102
103 public boolean getDisplayStateSupported() {
104 return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
John Spurlockd06aa572014-09-10 10:40:49 -0400105 }
106
Adrian Roosa1e6b312017-03-28 16:20:34 -0700107 public boolean getDozeSuspendDisplayStateSupported() {
108 return mContext.getResources().getBoolean(R.bool.doze_suspend_display_state_supported);
109 }
110
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800111 public int getPulseDuration() {
112 return getPulseInDuration() + getPulseVisibleDuration() + getPulseOutDuration();
John Spurlockd06aa572014-09-10 10:40:49 -0400113 }
114
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700115 public float getScreenBrightnessDoze() {
116 return mContext.getResources().getInteger(
117 com.android.internal.R.integer.config_screenBrightnessDoze) / 255f;
118 }
119
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800120 public int getPulseInDuration() {
121 return getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
John Spurlockd06aa572014-09-10 10:40:49 -0400122 }
123
124 public int getPulseVisibleDuration() {
125 return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible);
126 }
127
128 public int getPulseOutDuration() {
129 return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out);
130 }
131
John Spurlock190d0262014-09-14 15:39:13 -0400132 public boolean getPulseOnSigMotion() {
133 return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion);
John Spurlockd06aa572014-09-10 10:40:49 -0400134 }
135
John Spurlock190d0262014-09-14 15:39:13 -0400136 public boolean getVibrateOnSigMotion() {
137 return SystemProperties.getBoolean("doze.vibrate.sigmotion", false);
138 }
139
John Spurlock190d0262014-09-14 15:39:13 -0400140 public boolean getVibrateOnPickup() {
141 return SystemProperties.getBoolean("doze.vibrate.pickup", false);
142 }
143
John Spurlock686e4d52014-11-20 21:48:09 -0500144 public boolean getProxCheckBeforePulse() {
145 return getBoolean("doze.pulse.proxcheck", R.bool.doze_proximity_check_before_pulse);
146 }
147
John Spurlock50a8ea62014-09-16 09:12:03 -0400148 public int getPickupVibrationThreshold() {
149 return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
150 }
151
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700152 /**
Lucas Dupin82aa1632017-12-13 00:13:57 -0800153 * For how long a wallpaper can be visible in AoD before it fades aways.
154 * @return duration in millis.
155 */
156 public long getWallpaperAodDuration() {
Lucas Dupincbe05962018-04-26 16:44:05 -0700157 return shouldControlScreenOff() ? DozeScreenState.ENTER_DOZE_HIDE_WALLPAPER_DELAY
158 : mAlwaysOnPolicy.wallpaperVisibilityDuration;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800159 }
160
161 /**
162 * How long it takes for the wallpaper fade away (Animation duration.)
163 * @return duration in millis.
164 */
165 public long getWallpaperFadeOutDuration() {
166 return mAlwaysOnPolicy.wallpaperFadeOutDuration;
167 }
168
169 /**
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700170 * Checks if always on is available and enabled for the current user.
171 * @return {@code true} if enabled and available.
172 */
Adrian Roosebea7a72016-10-26 12:51:26 -0700173 public boolean getAlwaysOn() {
Lucas Dupindff8cdf2018-01-09 13:01:01 -0800174 return mDozeAlwaysOn;
Adrian Roosebea7a72016-10-26 12:51:26 -0700175 }
176
Lucas Dupin43d0d732017-11-16 11:23:49 -0800177 /**
178 * Some screens need to be completely black before changing the display power mode,
179 * unexpected behavior might happen if this parameter isn't respected.
180 *
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700181 * @return {@code true} if screen needs to be completely black before a power transition.
Lucas Dupin43d0d732017-11-16 11:23:49 -0800182 */
183 public boolean getDisplayNeedsBlanking() {
184 return mContext.getResources().getBoolean(
185 com.android.internal.R.bool.config_displayBlanksAfterDoze);
186 }
187
Lucas Dupin16cfe452018-02-08 13:14:50 -0800188 public boolean shouldControlScreenOff() {
189 return mControlScreenOffAnimation;
190 }
191
192 public void setControlScreenOffAnimation(boolean controlScreenOffAnimation) {
193 if (mControlScreenOffAnimation == controlScreenOffAnimation) {
194 return;
195 }
196 mControlScreenOffAnimation = controlScreenOffAnimation;
197 getPowerManager().setDozeAfterScreenOff(!controlScreenOffAnimation);
198 }
199
200 @VisibleForTesting
201 protected PowerManager getPowerManager() {
202 return mPowerManager;
Lucas Dupin43d0d732017-11-16 11:23:49 -0800203 }
204
John Spurlock190d0262014-09-14 15:39:13 -0400205 private boolean getBoolean(String propName, int resId) {
206 return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId));
John Spurlockd06aa572014-09-10 10:40:49 -0400207 }
208
209 private int getInt(String propName, int resId) {
210 int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId));
211 return MathUtils.constrain(value, 0, MAX_DURATION);
212 }
213
214 private String getString(String propName, int resId) {
215 return SystemProperties.get(propName, mContext.getString(resId));
216 }
217
Adrian Roos5753f052016-07-13 10:30:37 -0700218 public boolean getPickupSubtypePerformsProxCheck(int subType) {
219 String spec = getString("doze.pickup.proxcheck",
220 R.string.doze_pickup_subtype_performs_proximity_check);
221
222 if (TextUtils.isEmpty(spec)) {
223 // Fall back to non-subtype based property.
224 return mContext.getResources().getBoolean(R.bool.doze_pickup_performs_proximity_check);
225 }
226
227 if (sPickupSubtypePerformsProxMatcher == null
228 || !TextUtils.equals(spec, sPickupSubtypePerformsProxMatcher.mSpec)) {
229 sPickupSubtypePerformsProxMatcher = new IntInOutMatcher(spec);
230 }
231
232 return sPickupSubtypePerformsProxMatcher.isIn(subType);
233 }
234
Adrian Roos67cca742017-04-13 16:52:51 -0700235 public int getPulseVisibleDurationExtended() {
236 return 2 * getPulseVisibleDuration();
237 }
238
Adrian Roos44198f52017-06-02 12:50:38 -0700239 public boolean doubleTapReportsTouchCoordinates() {
240 return mContext.getResources().getBoolean(R.bool.doze_double_tap_reports_touch_coordinates);
241 }
242
Lucas Dupindff8cdf2018-01-09 13:01:01 -0800243 @Override
244 public void onTuningChanged(String key, String newValue) {
245 mDozeAlwaysOn = mAmbientDisplayConfiguration.alwaysOnEnabled(UserHandle.USER_CURRENT);
246 }
Adrian Roos5753f052016-07-13 10:30:37 -0700247
Adrian Roos2f5a3852018-04-23 17:48:08 +0200248 public AlwaysOnDisplayPolicy getPolicy() {
249 return mAlwaysOnPolicy;
250 }
251
Adrian Roos5753f052016-07-13 10:30:37 -0700252 /**
253 * Parses a spec of the form `1,2,3,!5,*`. The resulting object will match numbers that are
254 * listed, will not match numbers that are listed with a ! prefix, and will match / not match
255 * unlisted numbers depending on whether * or !* is present.
256 *
257 * * -> match any numbers that are not explicitly listed
258 * !* -> don't match any numbers that are not explicitly listed
259 * 2 -> match 2
260 * !3 -> don't match 3
261 *
262 * It is illegal to specify:
263 * - an empty spec
264 * - a spec containing that are empty, or a lone !
265 * - a spec for anything other than numbers or *
266 * - multiple terms for the same number / multiple *s
267 */
268 public static class IntInOutMatcher {
269 private static final String WILDCARD = "*";
270 private static final char OUT_PREFIX = '!';
271
272 private final SparseBooleanArray mIsIn;
273 private final boolean mDefaultIsIn;
274 final String mSpec;
275
276 public IntInOutMatcher(String spec) {
277 if (TextUtils.isEmpty(spec)) {
278 throw new IllegalArgumentException("Spec must not be empty");
279 }
280
281 boolean defaultIsIn = false;
282 boolean foundWildcard = false;
283
284 mSpec = spec;
285 mIsIn = new SparseBooleanArray();
286
287 for (String itemPrefixed : spec.split(",", -1)) {
288 if (itemPrefixed.length() == 0) {
289 throw new IllegalArgumentException(
290 "Illegal spec, must not have zero-length items: `" + spec + "`");
291 }
292 boolean isIn = itemPrefixed.charAt(0) != OUT_PREFIX;
293 String item = isIn ? itemPrefixed : itemPrefixed.substring(1);
294
295 if (itemPrefixed.length() == 0) {
296 throw new IllegalArgumentException(
297 "Illegal spec, must not have zero-length items: `" + spec + "`");
298 }
299
300 if (WILDCARD.equals(item)) {
301 if (foundWildcard) {
302 throw new IllegalArgumentException("Illegal spec, `" + WILDCARD +
303 "` must not appear multiple times in `" + spec + "`");
304 }
305 defaultIsIn = isIn;
306 foundWildcard = true;
307 } else {
308 int key = Integer.parseInt(item);
309 if (mIsIn.indexOfKey(key) >= 0) {
310 throw new IllegalArgumentException("Illegal spec, `" + key +
311 "` must not appear multiple times in `" + spec + "`");
312 }
313 mIsIn.put(key, isIn);
314 }
315 }
316
317 if (!foundWildcard) {
318 throw new IllegalArgumentException("Illegal spec, must specify either * or !*");
319 }
320
321 mDefaultIsIn = defaultIsIn;
322 }
323
324 public boolean isIn(int value) {
325 return (mIsIn.get(value, mDefaultIsIn));
326 }
327 }
John Spurlockd06aa572014-09-10 10:40:49 -0400328}