blob: 4a2ec88c75181f12a168f8b2306e671d17aa15c9 [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;
Adrian Roosc45944b2017-03-07 20:20:35 +000020import android.os.Build;
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
28import 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";
Adrian Roosc45944b2017-03-07 20:20:35 +000035 public static final boolean ALWAYS_ON_AVAILABLE = Build.IS_DEBUGGABLE;
John Spurlockd06aa572014-09-10 10:40:49 -040036
37 private final Context mContext;
38
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;
43 }
44
45 public void dump(PrintWriter pw) {
46 pw.println(" DozeParameters:");
John Spurlock190d0262014-09-14 15:39:13 -040047 pw.print(" getDisplayStateSupported(): "); pw.println(getDisplayStateSupported());
John Spurlockf5d250d2014-12-02 10:41:25 -050048 pw.print(" getPulseDuration(pickup=false): "); pw.println(getPulseDuration(false));
49 pw.print(" getPulseDuration(pickup=true): "); pw.println(getPulseDuration(true));
50 pw.print(" getPulseInDuration(pickup=false): "); pw.println(getPulseInDuration(false));
51 pw.print(" getPulseInDuration(pickup=true): "); pw.println(getPulseInDuration(true));
John Spurlockd06aa572014-09-10 10:40:49 -040052 pw.print(" getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration());
53 pw.print(" getPulseOutDuration(): "); pw.println(getPulseOutDuration());
John Spurlock190d0262014-09-14 15:39:13 -040054 pw.print(" getPulseOnSigMotion(): "); pw.println(getPulseOnSigMotion());
55 pw.print(" getVibrateOnSigMotion(): "); pw.println(getVibrateOnSigMotion());
John Spurlock190d0262014-09-14 15:39:13 -040056 pw.print(" getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
John Spurlock686e4d52014-11-20 21:48:09 -050057 pw.print(" getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse());
John Spurlock50a8ea62014-09-16 09:12:03 -040058 pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
Adrian Roos5753f052016-07-13 10:30:37 -070059 pw.print(" getPickupSubtypePerformsProxCheck(): ");pw.println(
60 dumpPickupSubtypePerformsProxCheck());
Adrian Roosc45944b2017-03-07 20:20:35 +000061 if (ALWAYS_ON_AVAILABLE) {
62 pw.print(" getAlwaysOn(): "); pw.println(getAlwaysOn());
Adrian Roos7a1654e2017-02-14 14:06:29 +010063 pw.print(" getSensorsWakeUpFully(): "); pw.println(getSensorsWakeUpFully());
64 }
Adrian Roos5753f052016-07-13 10:30:37 -070065 }
66
67 private String dumpPickupSubtypePerformsProxCheck() {
68 // Refresh sPickupSubtypePerformsProxMatcher
69 getPickupSubtypePerformsProxCheck(0);
70
71 if (sPickupSubtypePerformsProxMatcher == null) {
72 return "fallback: " + mContext.getResources().getBoolean(
73 R.bool.doze_pickup_performs_proximity_check);
74 } else {
75 return "spec: " + sPickupSubtypePerformsProxMatcher.mSpec;
76 }
John Spurlock190d0262014-09-14 15:39:13 -040077 }
78
79 public boolean getDisplayStateSupported() {
80 return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
John Spurlockd06aa572014-09-10 10:40:49 -040081 }
82
John Spurlockf5d250d2014-12-02 10:41:25 -050083 public int getPulseDuration(boolean pickup) {
84 return getPulseInDuration(pickup) + getPulseVisibleDuration() + getPulseOutDuration();
John Spurlockd06aa572014-09-10 10:40:49 -040085 }
86
Adrian Roos7294c112016-09-20 14:03:21 -070087 public int getPulseInDuration(boolean pickupOrDoubleTap) {
88 return pickupOrDoubleTap
John Spurlockf5d250d2014-12-02 10:41:25 -050089 ? getInt("doze.pulse.duration.in.pickup", R.integer.doze_pulse_duration_in_pickup)
90 : getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
John Spurlockd06aa572014-09-10 10:40:49 -040091 }
92
93 public int getPulseVisibleDuration() {
94 return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible);
95 }
96
97 public int getPulseOutDuration() {
98 return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out);
99 }
100
John Spurlock190d0262014-09-14 15:39:13 -0400101 public boolean getPulseOnSigMotion() {
102 return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion);
John Spurlockd06aa572014-09-10 10:40:49 -0400103 }
104
John Spurlock190d0262014-09-14 15:39:13 -0400105 public boolean getVibrateOnSigMotion() {
106 return SystemProperties.getBoolean("doze.vibrate.sigmotion", false);
107 }
108
John Spurlock190d0262014-09-14 15:39:13 -0400109 public boolean getVibrateOnPickup() {
110 return SystemProperties.getBoolean("doze.vibrate.pickup", false);
111 }
112
John Spurlock686e4d52014-11-20 21:48:09 -0500113 public boolean getProxCheckBeforePulse() {
114 return getBoolean("doze.pulse.proxcheck", R.bool.doze_proximity_check_before_pulse);
115 }
116
John Spurlock50a8ea62014-09-16 09:12:03 -0400117 public int getPickupVibrationThreshold() {
118 return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
119 }
120
Adrian Roosebea7a72016-10-26 12:51:26 -0700121 public boolean getAlwaysOn() {
Adrian Roosc45944b2017-03-07 20:20:35 +0000122 return ALWAYS_ON_AVAILABLE
123 && Settings.Secure.getIntForUser(mContext.getContentResolver(),
124 Settings.Secure.DOZE_ALWAYS_ON, 0, UserHandle.USER_CURRENT) != 0;
Adrian Roosebea7a72016-10-26 12:51:26 -0700125 }
126
Adrian Roos7a1654e2017-02-14 14:06:29 +0100127 public boolean getSensorsWakeUpFully() {
Adrian Roosc45944b2017-03-07 20:20:35 +0000128 return ALWAYS_ON_AVAILABLE
Adrian Roos7a1654e2017-02-14 14:06:29 +0100129 && Settings.Secure.getIntForUser(mContext.getContentResolver(),
Adrian Roosd84a0802017-03-06 13:38:37 -0800130 DOZE_SENSORS_WAKE_UP_FULLY, 1, UserHandle.USER_CURRENT) != 0;
Adrian Roos7a1654e2017-02-14 14:06:29 +0100131 }
132
John Spurlock190d0262014-09-14 15:39:13 -0400133 private boolean getBoolean(String propName, int resId) {
134 return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId));
John Spurlockd06aa572014-09-10 10:40:49 -0400135 }
136
137 private int getInt(String propName, int resId) {
138 int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId));
139 return MathUtils.constrain(value, 0, MAX_DURATION);
140 }
141
142 private String getString(String propName, int resId) {
143 return SystemProperties.get(propName, mContext.getString(resId));
144 }
145
Adrian Roos5753f052016-07-13 10:30:37 -0700146 public boolean getPickupSubtypePerformsProxCheck(int subType) {
147 String spec = getString("doze.pickup.proxcheck",
148 R.string.doze_pickup_subtype_performs_proximity_check);
149
150 if (TextUtils.isEmpty(spec)) {
151 // Fall back to non-subtype based property.
152 return mContext.getResources().getBoolean(R.bool.doze_pickup_performs_proximity_check);
153 }
154
155 if (sPickupSubtypePerformsProxMatcher == null
156 || !TextUtils.equals(spec, sPickupSubtypePerformsProxMatcher.mSpec)) {
157 sPickupSubtypePerformsProxMatcher = new IntInOutMatcher(spec);
158 }
159
160 return sPickupSubtypePerformsProxMatcher.isIn(subType);
161 }
162
163
164 /**
165 * Parses a spec of the form `1,2,3,!5,*`. The resulting object will match numbers that are
166 * listed, will not match numbers that are listed with a ! prefix, and will match / not match
167 * unlisted numbers depending on whether * or !* is present.
168 *
169 * * -> match any numbers that are not explicitly listed
170 * !* -> don't match any numbers that are not explicitly listed
171 * 2 -> match 2
172 * !3 -> don't match 3
173 *
174 * It is illegal to specify:
175 * - an empty spec
176 * - a spec containing that are empty, or a lone !
177 * - a spec for anything other than numbers or *
178 * - multiple terms for the same number / multiple *s
179 */
180 public static class IntInOutMatcher {
181 private static final String WILDCARD = "*";
182 private static final char OUT_PREFIX = '!';
183
184 private final SparseBooleanArray mIsIn;
185 private final boolean mDefaultIsIn;
186 final String mSpec;
187
188 public IntInOutMatcher(String spec) {
189 if (TextUtils.isEmpty(spec)) {
190 throw new IllegalArgumentException("Spec must not be empty");
191 }
192
193 boolean defaultIsIn = false;
194 boolean foundWildcard = false;
195
196 mSpec = spec;
197 mIsIn = new SparseBooleanArray();
198
199 for (String itemPrefixed : spec.split(",", -1)) {
200 if (itemPrefixed.length() == 0) {
201 throw new IllegalArgumentException(
202 "Illegal spec, must not have zero-length items: `" + spec + "`");
203 }
204 boolean isIn = itemPrefixed.charAt(0) != OUT_PREFIX;
205 String item = isIn ? itemPrefixed : itemPrefixed.substring(1);
206
207 if (itemPrefixed.length() == 0) {
208 throw new IllegalArgumentException(
209 "Illegal spec, must not have zero-length items: `" + spec + "`");
210 }
211
212 if (WILDCARD.equals(item)) {
213 if (foundWildcard) {
214 throw new IllegalArgumentException("Illegal spec, `" + WILDCARD +
215 "` must not appear multiple times in `" + spec + "`");
216 }
217 defaultIsIn = isIn;
218 foundWildcard = true;
219 } else {
220 int key = Integer.parseInt(item);
221 if (mIsIn.indexOfKey(key) >= 0) {
222 throw new IllegalArgumentException("Illegal spec, `" + key +
223 "` must not appear multiple times in `" + spec + "`");
224 }
225 mIsIn.put(key, isIn);
226 }
227 }
228
229 if (!foundWildcard) {
230 throw new IllegalArgumentException("Illegal spec, must specify either * or !*");
231 }
232
233 mDefaultIsIn = defaultIsIn;
234 }
235
236 public boolean isIn(int value) {
237 return (mIsIn.get(value, mDefaultIsIn));
238 }
239 }
John Spurlockd06aa572014-09-10 10:40:49 -0400240}