blob: 8f552e31f5a5faa5aa76590ca840bcf8e3fabef2 [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);
John Spurlockd06aa572014-09-10 10:40:49 -040043
Lucas Dupin16cfe452018-02-08 13:14:50 -080044 private static IntInOutMatcher sPickupSubtypePerformsProxMatcher;
45 private static DozeParameters sInstance;
46
John Spurlockd06aa572014-09-10 10:40:49 -040047 private final Context mContext;
Adrian Roos0261fb22017-03-07 20:20:35 +000048 private final AmbientDisplayConfiguration mAmbientDisplayConfiguration;
Lucas Dupin16cfe452018-02-08 13:14:50 -080049 private final PowerManager mPowerManager;
John Spurlockd06aa572014-09-10 10:40:49 -040050
Lucas Dupin82aa1632017-12-13 00:13:57 -080051 private final AlwaysOnDisplayPolicy mAlwaysOnPolicy;
Adrian Roos5753f052016-07-13 10:30:37 -070052
Lucas Dupindff8cdf2018-01-09 13:01:01 -080053 private boolean mDozeAlwaysOn;
Lucas Dupin16cfe452018-02-08 13:14:50 -080054 private boolean mControlScreenOffAnimation;
Lucas Dupindff8cdf2018-01-09 13:01:01 -080055
Lucas Dupin16cfe452018-02-08 13:14:50 -080056 public static DozeParameters getInstance(Context context) {
57 if (sInstance == null) {
58 sInstance = new DozeParameters(context);
59 }
60 return sInstance;
61 }
62
63 @VisibleForTesting
64 protected DozeParameters(Context context) {
Adrian Roos2f5a3852018-04-23 17:48:08 +020065 mContext = context.getApplicationContext();
Adrian Roos0261fb22017-03-07 20:20:35 +000066 mAmbientDisplayConfiguration = new AmbientDisplayConfiguration(mContext);
Adrian Roos2f5a3852018-04-23 17:48:08 +020067 mAlwaysOnPolicy = new AlwaysOnDisplayPolicy(mContext);
Lucas Dupindff8cdf2018-01-09 13:01:01 -080068
Lucas Dupin16cfe452018-02-08 13:14:50 -080069 mControlScreenOffAnimation = !getDisplayNeedsBlanking();
70 mPowerManager = mContext.getSystemService(PowerManager.class);
71 mPowerManager.setDozeAfterScreenOff(!mControlScreenOffAnimation);
72
Lucas Dupindff8cdf2018-01-09 13:01:01 -080073 Dependency.get(TunerService.class).addTunable(this, Settings.Secure.DOZE_ALWAYS_ON,
74 Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
John Spurlockd06aa572014-09-10 10:40:49 -040075 }
76
77 public void dump(PrintWriter pw) {
78 pw.println(" DozeParameters:");
John Spurlock190d0262014-09-14 15:39:13 -040079 pw.print(" getDisplayStateSupported(): "); pw.println(getDisplayStateSupported());
Lucas Dupin9e3fa102017-11-08 17:16:55 -080080 pw.print(" getPulseDuration(): "); pw.println(getPulseDuration());
81 pw.print(" getPulseInDuration(): "); pw.println(getPulseInDuration());
John Spurlockd06aa572014-09-10 10:40:49 -040082 pw.print(" getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration());
83 pw.print(" getPulseOutDuration(): "); pw.println(getPulseOutDuration());
John Spurlock190d0262014-09-14 15:39:13 -040084 pw.print(" getPulseOnSigMotion(): "); pw.println(getPulseOnSigMotion());
85 pw.print(" getVibrateOnSigMotion(): "); pw.println(getVibrateOnSigMotion());
John Spurlock190d0262014-09-14 15:39:13 -040086 pw.print(" getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
John Spurlock686e4d52014-11-20 21:48:09 -050087 pw.print(" getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse());
John Spurlock50a8ea62014-09-16 09:12:03 -040088 pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
Adrian Roos5753f052016-07-13 10:30:37 -070089 pw.print(" getPickupSubtypePerformsProxCheck(): ");pw.println(
90 dumpPickupSubtypePerformsProxCheck());
91 }
92
93 private String dumpPickupSubtypePerformsProxCheck() {
94 // Refresh sPickupSubtypePerformsProxMatcher
95 getPickupSubtypePerformsProxCheck(0);
96
97 if (sPickupSubtypePerformsProxMatcher == null) {
98 return "fallback: " + mContext.getResources().getBoolean(
99 R.bool.doze_pickup_performs_proximity_check);
100 } else {
101 return "spec: " + sPickupSubtypePerformsProxMatcher.mSpec;
102 }
John Spurlock190d0262014-09-14 15:39:13 -0400103 }
104
105 public boolean getDisplayStateSupported() {
106 return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
John Spurlockd06aa572014-09-10 10:40:49 -0400107 }
108
Adrian Roosa1e6b312017-03-28 16:20:34 -0700109 public boolean getDozeSuspendDisplayStateSupported() {
110 return mContext.getResources().getBoolean(R.bool.doze_suspend_display_state_supported);
111 }
112
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800113 public int getPulseDuration() {
114 return getPulseInDuration() + getPulseVisibleDuration() + getPulseOutDuration();
John Spurlockd06aa572014-09-10 10:40:49 -0400115 }
116
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700117 public float getScreenBrightnessDoze() {
118 return mContext.getResources().getInteger(
119 com.android.internal.R.integer.config_screenBrightnessDoze) / 255f;
120 }
121
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800122 public int getPulseInDuration() {
123 return getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
John Spurlockd06aa572014-09-10 10:40:49 -0400124 }
125
126 public int getPulseVisibleDuration() {
127 return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible);
128 }
129
130 public int getPulseOutDuration() {
131 return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out);
132 }
133
John Spurlock190d0262014-09-14 15:39:13 -0400134 public boolean getPulseOnSigMotion() {
135 return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion);
John Spurlockd06aa572014-09-10 10:40:49 -0400136 }
137
John Spurlock190d0262014-09-14 15:39:13 -0400138 public boolean getVibrateOnSigMotion() {
139 return SystemProperties.getBoolean("doze.vibrate.sigmotion", false);
140 }
141
John Spurlock190d0262014-09-14 15:39:13 -0400142 public boolean getVibrateOnPickup() {
143 return SystemProperties.getBoolean("doze.vibrate.pickup", false);
144 }
145
John Spurlock686e4d52014-11-20 21:48:09 -0500146 public boolean getProxCheckBeforePulse() {
147 return getBoolean("doze.pulse.proxcheck", R.bool.doze_proximity_check_before_pulse);
148 }
149
John Spurlock50a8ea62014-09-16 09:12:03 -0400150 public int getPickupVibrationThreshold() {
151 return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
152 }
153
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700154 /**
Lucas Dupin82aa1632017-12-13 00:13:57 -0800155 * For how long a wallpaper can be visible in AoD before it fades aways.
156 * @return duration in millis.
157 */
158 public long getWallpaperAodDuration() {
Lucas Dupincbe05962018-04-26 16:44:05 -0700159 return shouldControlScreenOff() ? DozeScreenState.ENTER_DOZE_HIDE_WALLPAPER_DELAY
160 : mAlwaysOnPolicy.wallpaperVisibilityDuration;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800161 }
162
163 /**
164 * How long it takes for the wallpaper fade away (Animation duration.)
165 * @return duration in millis.
166 */
167 public long getWallpaperFadeOutDuration() {
168 return mAlwaysOnPolicy.wallpaperFadeOutDuration;
169 }
170
171 /**
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700172 * Checks if always on is available and enabled for the current user.
173 * @return {@code true} if enabled and available.
174 */
Adrian Roosebea7a72016-10-26 12:51:26 -0700175 public boolean getAlwaysOn() {
Lucas Dupindff8cdf2018-01-09 13:01:01 -0800176 return mDozeAlwaysOn;
Adrian Roosebea7a72016-10-26 12:51:26 -0700177 }
178
Lucas Dupin43d0d732017-11-16 11:23:49 -0800179 /**
180 * Some screens need to be completely black before changing the display power mode,
181 * unexpected behavior might happen if this parameter isn't respected.
182 *
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700183 * @return {@code true} if screen needs to be completely black before a power transition.
Lucas Dupin43d0d732017-11-16 11:23:49 -0800184 */
185 public boolean getDisplayNeedsBlanking() {
Selim Cinek0dd5a6b2018-05-15 13:15:42 -0700186 return !FORCE_NO_BLANKING && mContext.getResources().getBoolean(
Lucas Dupin43d0d732017-11-16 11:23:49 -0800187 com.android.internal.R.bool.config_displayBlanksAfterDoze);
188 }
189
Lucas Dupin16cfe452018-02-08 13:14:50 -0800190 public boolean shouldControlScreenOff() {
191 return mControlScreenOffAnimation;
192 }
193
194 public void setControlScreenOffAnimation(boolean controlScreenOffAnimation) {
195 if (mControlScreenOffAnimation == controlScreenOffAnimation) {
196 return;
197 }
198 mControlScreenOffAnimation = controlScreenOffAnimation;
199 getPowerManager().setDozeAfterScreenOff(!controlScreenOffAnimation);
200 }
201
202 @VisibleForTesting
203 protected PowerManager getPowerManager() {
204 return mPowerManager;
Lucas Dupin43d0d732017-11-16 11:23:49 -0800205 }
206
John Spurlock190d0262014-09-14 15:39:13 -0400207 private boolean getBoolean(String propName, int resId) {
208 return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId));
John Spurlockd06aa572014-09-10 10:40:49 -0400209 }
210
211 private int getInt(String propName, int resId) {
212 int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId));
213 return MathUtils.constrain(value, 0, MAX_DURATION);
214 }
215
216 private String getString(String propName, int resId) {
217 return SystemProperties.get(propName, mContext.getString(resId));
218 }
219
Adrian Roos5753f052016-07-13 10:30:37 -0700220 public boolean getPickupSubtypePerformsProxCheck(int subType) {
221 String spec = getString("doze.pickup.proxcheck",
222 R.string.doze_pickup_subtype_performs_proximity_check);
223
224 if (TextUtils.isEmpty(spec)) {
225 // Fall back to non-subtype based property.
226 return mContext.getResources().getBoolean(R.bool.doze_pickup_performs_proximity_check);
227 }
228
229 if (sPickupSubtypePerformsProxMatcher == null
230 || !TextUtils.equals(spec, sPickupSubtypePerformsProxMatcher.mSpec)) {
231 sPickupSubtypePerformsProxMatcher = new IntInOutMatcher(spec);
232 }
233
234 return sPickupSubtypePerformsProxMatcher.isIn(subType);
235 }
236
Adrian Roos67cca742017-04-13 16:52:51 -0700237 public int getPulseVisibleDurationExtended() {
238 return 2 * getPulseVisibleDuration();
239 }
240
Adrian Roos44198f52017-06-02 12:50:38 -0700241 public boolean doubleTapReportsTouchCoordinates() {
242 return mContext.getResources().getBoolean(R.bool.doze_double_tap_reports_touch_coordinates);
243 }
244
Lucas Dupindff8cdf2018-01-09 13:01:01 -0800245 @Override
246 public void onTuningChanged(String key, String newValue) {
247 mDozeAlwaysOn = mAmbientDisplayConfiguration.alwaysOnEnabled(UserHandle.USER_CURRENT);
248 }
Adrian Roos5753f052016-07-13 10:30:37 -0700249
Adrian Roos2f5a3852018-04-23 17:48:08 +0200250 public AlwaysOnDisplayPolicy getPolicy() {
251 return mAlwaysOnPolicy;
252 }
253
Adrian Roos5753f052016-07-13 10:30:37 -0700254 /**
255 * Parses a spec of the form `1,2,3,!5,*`. The resulting object will match numbers that are
256 * listed, will not match numbers that are listed with a ! prefix, and will match / not match
257 * unlisted numbers depending on whether * or !* is present.
258 *
259 * * -> match any numbers that are not explicitly listed
260 * !* -> don't match any numbers that are not explicitly listed
261 * 2 -> match 2
262 * !3 -> don't match 3
263 *
264 * It is illegal to specify:
265 * - an empty spec
266 * - a spec containing that are empty, or a lone !
267 * - a spec for anything other than numbers or *
268 * - multiple terms for the same number / multiple *s
269 */
270 public static class IntInOutMatcher {
271 private static final String WILDCARD = "*";
272 private static final char OUT_PREFIX = '!';
273
274 private final SparseBooleanArray mIsIn;
275 private final boolean mDefaultIsIn;
276 final String mSpec;
277
278 public IntInOutMatcher(String spec) {
279 if (TextUtils.isEmpty(spec)) {
280 throw new IllegalArgumentException("Spec must not be empty");
281 }
282
283 boolean defaultIsIn = false;
284 boolean foundWildcard = false;
285
286 mSpec = spec;
287 mIsIn = new SparseBooleanArray();
288
289 for (String itemPrefixed : spec.split(",", -1)) {
290 if (itemPrefixed.length() == 0) {
291 throw new IllegalArgumentException(
292 "Illegal spec, must not have zero-length items: `" + spec + "`");
293 }
294 boolean isIn = itemPrefixed.charAt(0) != OUT_PREFIX;
295 String item = isIn ? itemPrefixed : itemPrefixed.substring(1);
296
297 if (itemPrefixed.length() == 0) {
298 throw new IllegalArgumentException(
299 "Illegal spec, must not have zero-length items: `" + spec + "`");
300 }
301
302 if (WILDCARD.equals(item)) {
303 if (foundWildcard) {
304 throw new IllegalArgumentException("Illegal spec, `" + WILDCARD +
305 "` must not appear multiple times in `" + spec + "`");
306 }
307 defaultIsIn = isIn;
308 foundWildcard = true;
309 } else {
310 int key = Integer.parseInt(item);
311 if (mIsIn.indexOfKey(key) >= 0) {
312 throw new IllegalArgumentException("Illegal spec, `" + key +
313 "` must not appear multiple times in `" + spec + "`");
314 }
315 mIsIn.put(key, isIn);
316 }
317 }
318
319 if (!foundWildcard) {
320 throw new IllegalArgumentException("Illegal spec, must specify either * or !*");
321 }
322
323 mDefaultIsIn = defaultIsIn;
324 }
325
326 public boolean isIn(int value) {
327 return (mIsIn.get(value, mDefaultIsIn));
328 }
329 }
John Spurlockd06aa572014-09-10 10:40:49 -0400330}