blob: 50cbd69e31c8b4b3b80ac48cb7c16a1ad4ec570a [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;
20import android.os.SystemProperties;
Adrian Roosb7e4e102016-10-14 13:03:45 -070021import android.os.UserHandle;
Adrian Roosebea7a72016-10-26 12:51:26 -070022import android.provider.Settings;
John Spurlockd06aa572014-09-10 10:40:49 -040023import android.text.TextUtils;
John Spurlockd06aa572014-09-10 10:40:49 -040024import android.util.MathUtils;
Adrian Roos5753f052016-07-13 10:30:37 -070025import android.util.SparseBooleanArray;
John Spurlockd06aa572014-09-10 10:40:49 -040026
Adrian Roos0261fb22017-03-07 20:20:35 +000027import com.android.internal.hardware.AmbientDisplayConfiguration;
John Spurlockd06aa572014-09-10 10:40:49 -040028import com.android.systemui.R;
29
30import java.io.PrintWriter;
John Spurlockd06aa572014-09-10 10:40:49 -040031
32public class DozeParameters {
Selim Cinekbb998c92015-09-22 10:05:29 +020033 private static final int MAX_DURATION = 60 * 1000;
Adrian Roos7a1654e2017-02-14 14:06:29 +010034 public static final String DOZE_SENSORS_WAKE_UP_FULLY = "doze_sensors_wake_up_fully";
John Spurlockd06aa572014-09-10 10:40:49 -040035
36 private final Context mContext;
Adrian Roos0261fb22017-03-07 20:20:35 +000037 private final AmbientDisplayConfiguration mAmbientDisplayConfiguration;
John Spurlockd06aa572014-09-10 10:40:49 -040038
Adrian Roos5753f052016-07-13 10:30:37 -070039 private static IntInOutMatcher sPickupSubtypePerformsProxMatcher;
40
John Spurlockd06aa572014-09-10 10:40:49 -040041 public DozeParameters(Context context) {
42 mContext = context;
Adrian Roos0261fb22017-03-07 20:20:35 +000043 mAmbientDisplayConfiguration = new AmbientDisplayConfiguration(mContext);
John Spurlockd06aa572014-09-10 10:40:49 -040044 }
45
46 public void dump(PrintWriter pw) {
47 pw.println(" DozeParameters:");
John Spurlock190d0262014-09-14 15:39:13 -040048 pw.print(" getDisplayStateSupported(): "); pw.println(getDisplayStateSupported());
Lucas Dupin9e3fa102017-11-08 17:16:55 -080049 pw.print(" getPulseDuration(): "); pw.println(getPulseDuration());
50 pw.print(" getPulseInDuration(): "); pw.println(getPulseInDuration());
John Spurlockd06aa572014-09-10 10:40:49 -040051 pw.print(" getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration());
52 pw.print(" getPulseOutDuration(): "); pw.println(getPulseOutDuration());
John Spurlock190d0262014-09-14 15:39:13 -040053 pw.print(" getPulseOnSigMotion(): "); pw.println(getPulseOnSigMotion());
54 pw.print(" getVibrateOnSigMotion(): "); pw.println(getVibrateOnSigMotion());
John Spurlock190d0262014-09-14 15:39:13 -040055 pw.print(" getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
John Spurlock686e4d52014-11-20 21:48:09 -050056 pw.print(" getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse());
John Spurlock50a8ea62014-09-16 09:12:03 -040057 pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
Adrian Roos5753f052016-07-13 10:30:37 -070058 pw.print(" getPickupSubtypePerformsProxCheck(): ");pw.println(
59 dumpPickupSubtypePerformsProxCheck());
60 }
61
62 private String dumpPickupSubtypePerformsProxCheck() {
63 // Refresh sPickupSubtypePerformsProxMatcher
64 getPickupSubtypePerformsProxCheck(0);
65
66 if (sPickupSubtypePerformsProxMatcher == null) {
67 return "fallback: " + mContext.getResources().getBoolean(
68 R.bool.doze_pickup_performs_proximity_check);
69 } else {
70 return "spec: " + sPickupSubtypePerformsProxMatcher.mSpec;
71 }
John Spurlock190d0262014-09-14 15:39:13 -040072 }
73
74 public boolean getDisplayStateSupported() {
75 return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
John Spurlockd06aa572014-09-10 10:40:49 -040076 }
77
Adrian Roosa1e6b312017-03-28 16:20:34 -070078 public boolean getDozeSuspendDisplayStateSupported() {
79 return mContext.getResources().getBoolean(R.bool.doze_suspend_display_state_supported);
80 }
81
Lucas Dupin9e3fa102017-11-08 17:16:55 -080082 public int getPulseDuration() {
83 return getPulseInDuration() + getPulseVisibleDuration() + getPulseOutDuration();
John Spurlockd06aa572014-09-10 10:40:49 -040084 }
85
Lucas Dupin9e3fa102017-11-08 17:16:55 -080086 public int getPulseInDuration() {
87 return getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
John Spurlockd06aa572014-09-10 10:40:49 -040088 }
89
90 public int getPulseVisibleDuration() {
91 return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible);
92 }
93
94 public int getPulseOutDuration() {
95 return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out);
96 }
97
John Spurlock190d0262014-09-14 15:39:13 -040098 public boolean getPulseOnSigMotion() {
99 return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion);
John Spurlockd06aa572014-09-10 10:40:49 -0400100 }
101
John Spurlock190d0262014-09-14 15:39:13 -0400102 public boolean getVibrateOnSigMotion() {
103 return SystemProperties.getBoolean("doze.vibrate.sigmotion", false);
104 }
105
John Spurlock190d0262014-09-14 15:39:13 -0400106 public boolean getVibrateOnPickup() {
107 return SystemProperties.getBoolean("doze.vibrate.pickup", false);
108 }
109
John Spurlock686e4d52014-11-20 21:48:09 -0500110 public boolean getProxCheckBeforePulse() {
111 return getBoolean("doze.pulse.proxcheck", R.bool.doze_proximity_check_before_pulse);
112 }
113
John Spurlock50a8ea62014-09-16 09:12:03 -0400114 public int getPickupVibrationThreshold() {
115 return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
116 }
117
Adrian Roosebea7a72016-10-26 12:51:26 -0700118 public boolean getAlwaysOn() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000119 return mAmbientDisplayConfiguration.alwaysOnEnabled(UserHandle.USER_CURRENT);
Adrian Roosebea7a72016-10-26 12:51:26 -0700120 }
121
Lucas Dupin43d0d732017-11-16 11:23:49 -0800122 /**
123 * Some screens need to be completely black before changing the display power mode,
124 * unexpected behavior might happen if this parameter isn't respected.
125 *
126 * @return true if screen needs to be completely black before a power transition.
127 */
128 public boolean getDisplayNeedsBlanking() {
129 return mContext.getResources().getBoolean(
130 com.android.internal.R.bool.config_displayBlanksAfterDoze);
131 }
132
133 /**
134 * Whether we can implement our own screen off animation or if we need
135 * to rely on DisplayPowerManager to dim the display.
136 *
137 * @return true if SystemUI can control the screen off animation.
138 */
139 public boolean getCanControlScreenOffAnimation() {
140 return !mContext.getResources().getBoolean(
141 com.android.internal.R.bool.config_dozeAfterScreenOff);
142 }
143
John Spurlock190d0262014-09-14 15:39:13 -0400144 private boolean getBoolean(String propName, int resId) {
145 return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId));
John Spurlockd06aa572014-09-10 10:40:49 -0400146 }
147
148 private int getInt(String propName, int resId) {
149 int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId));
150 return MathUtils.constrain(value, 0, MAX_DURATION);
151 }
152
153 private String getString(String propName, int resId) {
154 return SystemProperties.get(propName, mContext.getString(resId));
155 }
156
Adrian Roos5753f052016-07-13 10:30:37 -0700157 public boolean getPickupSubtypePerformsProxCheck(int subType) {
158 String spec = getString("doze.pickup.proxcheck",
159 R.string.doze_pickup_subtype_performs_proximity_check);
160
161 if (TextUtils.isEmpty(spec)) {
162 // Fall back to non-subtype based property.
163 return mContext.getResources().getBoolean(R.bool.doze_pickup_performs_proximity_check);
164 }
165
166 if (sPickupSubtypePerformsProxMatcher == null
167 || !TextUtils.equals(spec, sPickupSubtypePerformsProxMatcher.mSpec)) {
168 sPickupSubtypePerformsProxMatcher = new IntInOutMatcher(spec);
169 }
170
171 return sPickupSubtypePerformsProxMatcher.isIn(subType);
172 }
173
Adrian Roos67cca742017-04-13 16:52:51 -0700174 public int getPulseVisibleDurationExtended() {
175 return 2 * getPulseVisibleDuration();
176 }
177
Adrian Roos44198f52017-06-02 12:50:38 -0700178 public boolean doubleTapReportsTouchCoordinates() {
179 return mContext.getResources().getBoolean(R.bool.doze_double_tap_reports_touch_coordinates);
180 }
181
Adrian Roos5753f052016-07-13 10:30:37 -0700182
183 /**
184 * Parses a spec of the form `1,2,3,!5,*`. The resulting object will match numbers that are
185 * listed, will not match numbers that are listed with a ! prefix, and will match / not match
186 * unlisted numbers depending on whether * or !* is present.
187 *
188 * * -> match any numbers that are not explicitly listed
189 * !* -> don't match any numbers that are not explicitly listed
190 * 2 -> match 2
191 * !3 -> don't match 3
192 *
193 * It is illegal to specify:
194 * - an empty spec
195 * - a spec containing that are empty, or a lone !
196 * - a spec for anything other than numbers or *
197 * - multiple terms for the same number / multiple *s
198 */
199 public static class IntInOutMatcher {
200 private static final String WILDCARD = "*";
201 private static final char OUT_PREFIX = '!';
202
203 private final SparseBooleanArray mIsIn;
204 private final boolean mDefaultIsIn;
205 final String mSpec;
206
207 public IntInOutMatcher(String spec) {
208 if (TextUtils.isEmpty(spec)) {
209 throw new IllegalArgumentException("Spec must not be empty");
210 }
211
212 boolean defaultIsIn = false;
213 boolean foundWildcard = false;
214
215 mSpec = spec;
216 mIsIn = new SparseBooleanArray();
217
218 for (String itemPrefixed : spec.split(",", -1)) {
219 if (itemPrefixed.length() == 0) {
220 throw new IllegalArgumentException(
221 "Illegal spec, must not have zero-length items: `" + spec + "`");
222 }
223 boolean isIn = itemPrefixed.charAt(0) != OUT_PREFIX;
224 String item = isIn ? itemPrefixed : itemPrefixed.substring(1);
225
226 if (itemPrefixed.length() == 0) {
227 throw new IllegalArgumentException(
228 "Illegal spec, must not have zero-length items: `" + spec + "`");
229 }
230
231 if (WILDCARD.equals(item)) {
232 if (foundWildcard) {
233 throw new IllegalArgumentException("Illegal spec, `" + WILDCARD +
234 "` must not appear multiple times in `" + spec + "`");
235 }
236 defaultIsIn = isIn;
237 foundWildcard = true;
238 } else {
239 int key = Integer.parseInt(item);
240 if (mIsIn.indexOfKey(key) >= 0) {
241 throw new IllegalArgumentException("Illegal spec, `" + key +
242 "` must not appear multiple times in `" + spec + "`");
243 }
244 mIsIn.put(key, isIn);
245 }
246 }
247
248 if (!foundWildcard) {
249 throw new IllegalArgumentException("Illegal spec, must specify either * or !*");
250 }
251
252 mDefaultIsIn = defaultIsIn;
253 }
254
255 public boolean isIn(int value) {
256 return (mIsIn.get(value, mDefaultIsIn));
257 }
258 }
John Spurlockd06aa572014-09-10 10:40:49 -0400259}