blob: 6f2b63d2e64dbf9bb9d2417733178791066ffae6 [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";
Selim Cinek0dd5a6b2018-05-15 13:15:42 -070041 public static final boolean FORCE_NO_BLANKING =
42 SystemProperties.getBoolean("debug.force_no_blanking", false);
Lucas Dupinee63c372018-08-03 11:58:24 -070043 public static final boolean FORCE_BLANKING =
44 SystemProperties.getBoolean("debug.force_blanking", false);
John Spurlockd06aa572014-09-10 10:40:49 -040045
Lucas Dupin16cfe452018-02-08 13:14:50 -080046 private static IntInOutMatcher sPickupSubtypePerformsProxMatcher;
47 private static DozeParameters sInstance;
48
John Spurlockd06aa572014-09-10 10:40:49 -040049 private final Context mContext;
Adrian Roos0261fb22017-03-07 20:20:35 +000050 private final AmbientDisplayConfiguration mAmbientDisplayConfiguration;
Lucas Dupin16cfe452018-02-08 13:14:50 -080051 private final PowerManager mPowerManager;
John Spurlockd06aa572014-09-10 10:40:49 -040052
Lucas Dupin82aa1632017-12-13 00:13:57 -080053 private final AlwaysOnDisplayPolicy mAlwaysOnPolicy;
Adrian Roos5753f052016-07-13 10:30:37 -070054
Lucas Dupindff8cdf2018-01-09 13:01:01 -080055 private boolean mDozeAlwaysOn;
Lucas Dupin16cfe452018-02-08 13:14:50 -080056 private boolean mControlScreenOffAnimation;
Lucas Dupindff8cdf2018-01-09 13:01:01 -080057
Lucas Dupin16cfe452018-02-08 13:14:50 -080058 public static DozeParameters getInstance(Context context) {
59 if (sInstance == null) {
60 sInstance = new DozeParameters(context);
61 }
62 return sInstance;
63 }
64
65 @VisibleForTesting
66 protected DozeParameters(Context context) {
Adrian Roos2f5a3852018-04-23 17:48:08 +020067 mContext = context.getApplicationContext();
Adrian Roos0261fb22017-03-07 20:20:35 +000068 mAmbientDisplayConfiguration = new AmbientDisplayConfiguration(mContext);
Adrian Roos2f5a3852018-04-23 17:48:08 +020069 mAlwaysOnPolicy = new AlwaysOnDisplayPolicy(mContext);
Lucas Dupindff8cdf2018-01-09 13:01:01 -080070
Lucas Dupin16cfe452018-02-08 13:14:50 -080071 mControlScreenOffAnimation = !getDisplayNeedsBlanking();
72 mPowerManager = mContext.getSystemService(PowerManager.class);
73 mPowerManager.setDozeAfterScreenOff(!mControlScreenOffAnimation);
74
Lucas Dupindff8cdf2018-01-09 13:01:01 -080075 Dependency.get(TunerService.class).addTunable(this, Settings.Secure.DOZE_ALWAYS_ON,
76 Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
John Spurlockd06aa572014-09-10 10:40:49 -040077 }
78
79 public void dump(PrintWriter pw) {
80 pw.println(" DozeParameters:");
John Spurlock190d0262014-09-14 15:39:13 -040081 pw.print(" getDisplayStateSupported(): "); pw.println(getDisplayStateSupported());
Lucas Dupin9e3fa102017-11-08 17:16:55 -080082 pw.print(" getPulseDuration(): "); pw.println(getPulseDuration());
83 pw.print(" getPulseInDuration(): "); pw.println(getPulseInDuration());
John Spurlockd06aa572014-09-10 10:40:49 -040084 pw.print(" getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration());
85 pw.print(" getPulseOutDuration(): "); pw.println(getPulseOutDuration());
John Spurlock190d0262014-09-14 15:39:13 -040086 pw.print(" getPulseOnSigMotion(): "); pw.println(getPulseOnSigMotion());
87 pw.print(" getVibrateOnSigMotion(): "); pw.println(getVibrateOnSigMotion());
John Spurlock190d0262014-09-14 15:39:13 -040088 pw.print(" getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
John Spurlock686e4d52014-11-20 21:48:09 -050089 pw.print(" getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse());
John Spurlock50a8ea62014-09-16 09:12:03 -040090 pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
Adrian Roos5753f052016-07-13 10:30:37 -070091 pw.print(" getPickupSubtypePerformsProxCheck(): ");pw.println(
92 dumpPickupSubtypePerformsProxCheck());
93 }
94
95 private String dumpPickupSubtypePerformsProxCheck() {
96 // Refresh sPickupSubtypePerformsProxMatcher
97 getPickupSubtypePerformsProxCheck(0);
98
99 if (sPickupSubtypePerformsProxMatcher == null) {
100 return "fallback: " + mContext.getResources().getBoolean(
101 R.bool.doze_pickup_performs_proximity_check);
102 } else {
103 return "spec: " + sPickupSubtypePerformsProxMatcher.mSpec;
104 }
John Spurlock190d0262014-09-14 15:39:13 -0400105 }
106
107 public boolean getDisplayStateSupported() {
108 return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
John Spurlockd06aa572014-09-10 10:40:49 -0400109 }
110
Adrian Roosa1e6b312017-03-28 16:20:34 -0700111 public boolean getDozeSuspendDisplayStateSupported() {
112 return mContext.getResources().getBoolean(R.bool.doze_suspend_display_state_supported);
113 }
114
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800115 public int getPulseDuration() {
116 return getPulseInDuration() + getPulseVisibleDuration() + getPulseOutDuration();
John Spurlockd06aa572014-09-10 10:40:49 -0400117 }
118
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700119 public float getScreenBrightnessDoze() {
120 return mContext.getResources().getInteger(
121 com.android.internal.R.integer.config_screenBrightnessDoze) / 255f;
122 }
123
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800124 public int getPulseInDuration() {
125 return getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
John Spurlockd06aa572014-09-10 10:40:49 -0400126 }
127
128 public int getPulseVisibleDuration() {
129 return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible);
130 }
131
132 public int getPulseOutDuration() {
133 return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out);
134 }
135
John Spurlock190d0262014-09-14 15:39:13 -0400136 public boolean getPulseOnSigMotion() {
137 return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion);
John Spurlockd06aa572014-09-10 10:40:49 -0400138 }
139
John Spurlock190d0262014-09-14 15:39:13 -0400140 public boolean getVibrateOnSigMotion() {
141 return SystemProperties.getBoolean("doze.vibrate.sigmotion", false);
142 }
143
John Spurlock190d0262014-09-14 15:39:13 -0400144 public boolean getVibrateOnPickup() {
145 return SystemProperties.getBoolean("doze.vibrate.pickup", false);
146 }
147
John Spurlock686e4d52014-11-20 21:48:09 -0500148 public boolean getProxCheckBeforePulse() {
149 return getBoolean("doze.pulse.proxcheck", R.bool.doze_proximity_check_before_pulse);
150 }
151
John Spurlock50a8ea62014-09-16 09:12:03 -0400152 public int getPickupVibrationThreshold() {
153 return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
154 }
155
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700156 /**
Lucas Dupin82aa1632017-12-13 00:13:57 -0800157 * For how long a wallpaper can be visible in AoD before it fades aways.
158 * @return duration in millis.
159 */
160 public long getWallpaperAodDuration() {
Lucas Dupinde64ee02018-12-21 14:45:12 -0800161 if (mAmbientDisplayConfiguration.wakeLockScreenGestureEnabled(UserHandle.USER_CURRENT)
162 || shouldControlScreenOff()) {
163 return DozeScreenState.ENTER_DOZE_HIDE_WALLPAPER_DELAY;
164 }
165 return mAlwaysOnPolicy.wallpaperVisibilityDuration;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800166 }
167
168 /**
169 * How long it takes for the wallpaper fade away (Animation duration.)
170 * @return duration in millis.
171 */
172 public long getWallpaperFadeOutDuration() {
173 return mAlwaysOnPolicy.wallpaperFadeOutDuration;
174 }
175
176 /**
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700177 * Checks if always on is available and enabled for the current user.
178 * @return {@code true} if enabled and available.
179 */
Adrian Roosebea7a72016-10-26 12:51:26 -0700180 public boolean getAlwaysOn() {
Lucas Dupindff8cdf2018-01-09 13:01:01 -0800181 return mDozeAlwaysOn;
Adrian Roosebea7a72016-10-26 12:51:26 -0700182 }
183
Lucas Dupin43d0d732017-11-16 11:23:49 -0800184 /**
185 * Some screens need to be completely black before changing the display power mode,
186 * unexpected behavior might happen if this parameter isn't respected.
187 *
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700188 * @return {@code true} if screen needs to be completely black before a power transition.
Lucas Dupin43d0d732017-11-16 11:23:49 -0800189 */
190 public boolean getDisplayNeedsBlanking() {
Lucas Dupinee63c372018-08-03 11:58:24 -0700191 return FORCE_BLANKING || !FORCE_NO_BLANKING && mContext.getResources().getBoolean(
Lucas Dupin43d0d732017-11-16 11:23:49 -0800192 com.android.internal.R.bool.config_displayBlanksAfterDoze);
193 }
194
Lucas Dupin16cfe452018-02-08 13:14:50 -0800195 public boolean shouldControlScreenOff() {
196 return mControlScreenOffAnimation;
197 }
198
199 public void setControlScreenOffAnimation(boolean controlScreenOffAnimation) {
200 if (mControlScreenOffAnimation == controlScreenOffAnimation) {
201 return;
202 }
203 mControlScreenOffAnimation = controlScreenOffAnimation;
204 getPowerManager().setDozeAfterScreenOff(!controlScreenOffAnimation);
205 }
206
207 @VisibleForTesting
208 protected PowerManager getPowerManager() {
209 return mPowerManager;
Lucas Dupin43d0d732017-11-16 11:23:49 -0800210 }
211
John Spurlock190d0262014-09-14 15:39:13 -0400212 private boolean getBoolean(String propName, int resId) {
213 return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId));
John Spurlockd06aa572014-09-10 10:40:49 -0400214 }
215
216 private int getInt(String propName, int resId) {
217 int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId));
218 return MathUtils.constrain(value, 0, MAX_DURATION);
219 }
220
221 private String getString(String propName, int resId) {
222 return SystemProperties.get(propName, mContext.getString(resId));
223 }
224
Adrian Roos5753f052016-07-13 10:30:37 -0700225 public boolean getPickupSubtypePerformsProxCheck(int subType) {
226 String spec = getString("doze.pickup.proxcheck",
227 R.string.doze_pickup_subtype_performs_proximity_check);
228
229 if (TextUtils.isEmpty(spec)) {
230 // Fall back to non-subtype based property.
231 return mContext.getResources().getBoolean(R.bool.doze_pickup_performs_proximity_check);
232 }
233
234 if (sPickupSubtypePerformsProxMatcher == null
235 || !TextUtils.equals(spec, sPickupSubtypePerformsProxMatcher.mSpec)) {
236 sPickupSubtypePerformsProxMatcher = new IntInOutMatcher(spec);
237 }
238
239 return sPickupSubtypePerformsProxMatcher.isIn(subType);
240 }
241
Adrian Roos67cca742017-04-13 16:52:51 -0700242 public int getPulseVisibleDurationExtended() {
243 return 2 * getPulseVisibleDuration();
244 }
245
Adrian Roos44198f52017-06-02 12:50:38 -0700246 public boolean doubleTapReportsTouchCoordinates() {
247 return mContext.getResources().getBoolean(R.bool.doze_double_tap_reports_touch_coordinates);
248 }
249
Lucas Dupindff8cdf2018-01-09 13:01:01 -0800250 @Override
251 public void onTuningChanged(String key, String newValue) {
252 mDozeAlwaysOn = mAmbientDisplayConfiguration.alwaysOnEnabled(UserHandle.USER_CURRENT);
253 }
Adrian Roos5753f052016-07-13 10:30:37 -0700254
Adrian Roos2f5a3852018-04-23 17:48:08 +0200255 public AlwaysOnDisplayPolicy getPolicy() {
256 return mAlwaysOnPolicy;
257 }
258
Adrian Roos5753f052016-07-13 10:30:37 -0700259 /**
260 * Parses a spec of the form `1,2,3,!5,*`. The resulting object will match numbers that are
261 * listed, will not match numbers that are listed with a ! prefix, and will match / not match
262 * unlisted numbers depending on whether * or !* is present.
263 *
264 * * -> match any numbers that are not explicitly listed
265 * !* -> don't match any numbers that are not explicitly listed
266 * 2 -> match 2
267 * !3 -> don't match 3
268 *
269 * It is illegal to specify:
270 * - an empty spec
271 * - a spec containing that are empty, or a lone !
272 * - a spec for anything other than numbers or *
273 * - multiple terms for the same number / multiple *s
274 */
275 public static class IntInOutMatcher {
276 private static final String WILDCARD = "*";
277 private static final char OUT_PREFIX = '!';
278
279 private final SparseBooleanArray mIsIn;
280 private final boolean mDefaultIsIn;
281 final String mSpec;
282
283 public IntInOutMatcher(String spec) {
284 if (TextUtils.isEmpty(spec)) {
285 throw new IllegalArgumentException("Spec must not be empty");
286 }
287
288 boolean defaultIsIn = false;
289 boolean foundWildcard = false;
290
291 mSpec = spec;
292 mIsIn = new SparseBooleanArray();
293
294 for (String itemPrefixed : spec.split(",", -1)) {
295 if (itemPrefixed.length() == 0) {
296 throw new IllegalArgumentException(
297 "Illegal spec, must not have zero-length items: `" + spec + "`");
298 }
299 boolean isIn = itemPrefixed.charAt(0) != OUT_PREFIX;
300 String item = isIn ? itemPrefixed : itemPrefixed.substring(1);
301
302 if (itemPrefixed.length() == 0) {
303 throw new IllegalArgumentException(
304 "Illegal spec, must not have zero-length items: `" + spec + "`");
305 }
306
307 if (WILDCARD.equals(item)) {
308 if (foundWildcard) {
309 throw new IllegalArgumentException("Illegal spec, `" + WILDCARD +
310 "` must not appear multiple times in `" + spec + "`");
311 }
312 defaultIsIn = isIn;
313 foundWildcard = true;
314 } else {
315 int key = Integer.parseInt(item);
316 if (mIsIn.indexOfKey(key) >= 0) {
317 throw new IllegalArgumentException("Illegal spec, `" + key +
318 "` must not appear multiple times in `" + spec + "`");
319 }
320 mIsIn.put(key, isIn);
321 }
322 }
323
324 if (!foundWildcard) {
325 throw new IllegalArgumentException("Illegal spec, must specify either * or !*");
326 }
327
328 mDefaultIsIn = defaultIsIn;
329 }
330
331 public boolean isIn(int value) {
332 return (mIsIn.get(value, mDefaultIsIn));
333 }
334 }
John Spurlockd06aa572014-09-10 10:40:49 -0400335}