blob: 9e9bdd7a172e81594ce2e5221a73654f59eecc03 [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;
John Spurlockd06aa572014-09-10 10:40:49 -040022import android.text.TextUtils;
23import android.util.Log;
24import android.util.MathUtils;
Adrian Roos5753f052016-07-13 10:30:37 -070025import android.util.SparseBooleanArray;
John Spurlockd06aa572014-09-10 10:40:49 -040026
Adrian Roosb7e4e102016-10-14 13:03:45 -070027import com.android.internal.hardware.AmbientDisplayConfiguration;
John Spurlockd06aa572014-09-10 10:40:49 -040028import com.android.systemui.R;
29
30import java.io.PrintWriter;
John Spurlock190d0262014-09-14 15:39:13 -040031import java.util.Arrays;
John Spurlockd06aa572014-09-10 10:40:49 -040032import java.util.regex.Matcher;
33import java.util.regex.Pattern;
34
35public class DozeParameters {
Selim Cinekbb998c92015-09-22 10:05:29 +020036 private static final int MAX_DURATION = 60 * 1000;
John Spurlockd06aa572014-09-10 10:40:49 -040037
38 private final Context mContext;
39
Adrian Roos5753f052016-07-13 10:30:37 -070040 private static IntInOutMatcher sPickupSubtypePerformsProxMatcher;
41
John Spurlockd06aa572014-09-10 10:40:49 -040042 public DozeParameters(Context context) {
43 mContext = context;
44 }
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());
John Spurlockf5d250d2014-12-02 10:41:25 -050049 pw.print(" getPulseDuration(pickup=false): "); pw.println(getPulseDuration(false));
50 pw.print(" getPulseDuration(pickup=true): "); pw.println(getPulseDuration(true));
51 pw.print(" getPulseInDuration(pickup=false): "); pw.println(getPulseInDuration(false));
52 pw.print(" getPulseInDuration(pickup=true): "); pw.println(getPulseInDuration(true));
John Spurlockd06aa572014-09-10 10:40:49 -040053 pw.print(" getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration());
54 pw.print(" getPulseOutDuration(): "); pw.println(getPulseOutDuration());
John Spurlock190d0262014-09-14 15:39:13 -040055 pw.print(" getPulseOnSigMotion(): "); pw.println(getPulseOnSigMotion());
56 pw.print(" getVibrateOnSigMotion(): "); pw.println(getVibrateOnSigMotion());
John Spurlock190d0262014-09-14 15:39:13 -040057 pw.print(" getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
John Spurlock686e4d52014-11-20 21:48:09 -050058 pw.print(" getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse());
John Spurlock50a8ea62014-09-16 09:12:03 -040059 pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
Adrian Roos5753f052016-07-13 10:30:37 -070060 pw.print(" getPickupSubtypePerformsProxCheck(): ");pw.println(
61 dumpPickupSubtypePerformsProxCheck());
62 }
63
64 private String dumpPickupSubtypePerformsProxCheck() {
65 // Refresh sPickupSubtypePerformsProxMatcher
66 getPickupSubtypePerformsProxCheck(0);
67
68 if (sPickupSubtypePerformsProxMatcher == null) {
69 return "fallback: " + mContext.getResources().getBoolean(
70 R.bool.doze_pickup_performs_proximity_check);
71 } else {
72 return "spec: " + sPickupSubtypePerformsProxMatcher.mSpec;
73 }
John Spurlock190d0262014-09-14 15:39:13 -040074 }
75
76 public boolean getDisplayStateSupported() {
77 return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
John Spurlockd06aa572014-09-10 10:40:49 -040078 }
79
John Spurlockf5d250d2014-12-02 10:41:25 -050080 public int getPulseDuration(boolean pickup) {
81 return getPulseInDuration(pickup) + getPulseVisibleDuration() + getPulseOutDuration();
John Spurlockd06aa572014-09-10 10:40:49 -040082 }
83
Adrian Roos7294c112016-09-20 14:03:21 -070084 public int getPulseInDuration(boolean pickupOrDoubleTap) {
85 return pickupOrDoubleTap
John Spurlockf5d250d2014-12-02 10:41:25 -050086 ? getInt("doze.pulse.duration.in.pickup", R.integer.doze_pulse_duration_in_pickup)
87 : 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
John Spurlock190d0262014-09-14 15:39:13 -0400118 private boolean getBoolean(String propName, int resId) {
119 return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId));
John Spurlockd06aa572014-09-10 10:40:49 -0400120 }
121
122 private int getInt(String propName, int resId) {
123 int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId));
124 return MathUtils.constrain(value, 0, MAX_DURATION);
125 }
126
127 private String getString(String propName, int resId) {
128 return SystemProperties.get(propName, mContext.getString(resId));
129 }
130
Adrian Roos5753f052016-07-13 10:30:37 -0700131 public boolean getPickupSubtypePerformsProxCheck(int subType) {
132 String spec = getString("doze.pickup.proxcheck",
133 R.string.doze_pickup_subtype_performs_proximity_check);
134
135 if (TextUtils.isEmpty(spec)) {
136 // Fall back to non-subtype based property.
137 return mContext.getResources().getBoolean(R.bool.doze_pickup_performs_proximity_check);
138 }
139
140 if (sPickupSubtypePerformsProxMatcher == null
141 || !TextUtils.equals(spec, sPickupSubtypePerformsProxMatcher.mSpec)) {
142 sPickupSubtypePerformsProxMatcher = new IntInOutMatcher(spec);
143 }
144
145 return sPickupSubtypePerformsProxMatcher.isIn(subType);
146 }
147
148
149 /**
150 * Parses a spec of the form `1,2,3,!5,*`. The resulting object will match numbers that are
151 * listed, will not match numbers that are listed with a ! prefix, and will match / not match
152 * unlisted numbers depending on whether * or !* is present.
153 *
154 * * -> match any numbers that are not explicitly listed
155 * !* -> don't match any numbers that are not explicitly listed
156 * 2 -> match 2
157 * !3 -> don't match 3
158 *
159 * It is illegal to specify:
160 * - an empty spec
161 * - a spec containing that are empty, or a lone !
162 * - a spec for anything other than numbers or *
163 * - multiple terms for the same number / multiple *s
164 */
165 public static class IntInOutMatcher {
166 private static final String WILDCARD = "*";
167 private static final char OUT_PREFIX = '!';
168
169 private final SparseBooleanArray mIsIn;
170 private final boolean mDefaultIsIn;
171 final String mSpec;
172
173 public IntInOutMatcher(String spec) {
174 if (TextUtils.isEmpty(spec)) {
175 throw new IllegalArgumentException("Spec must not be empty");
176 }
177
178 boolean defaultIsIn = false;
179 boolean foundWildcard = false;
180
181 mSpec = spec;
182 mIsIn = new SparseBooleanArray();
183
184 for (String itemPrefixed : spec.split(",", -1)) {
185 if (itemPrefixed.length() == 0) {
186 throw new IllegalArgumentException(
187 "Illegal spec, must not have zero-length items: `" + spec + "`");
188 }
189 boolean isIn = itemPrefixed.charAt(0) != OUT_PREFIX;
190 String item = isIn ? itemPrefixed : itemPrefixed.substring(1);
191
192 if (itemPrefixed.length() == 0) {
193 throw new IllegalArgumentException(
194 "Illegal spec, must not have zero-length items: `" + spec + "`");
195 }
196
197 if (WILDCARD.equals(item)) {
198 if (foundWildcard) {
199 throw new IllegalArgumentException("Illegal spec, `" + WILDCARD +
200 "` must not appear multiple times in `" + spec + "`");
201 }
202 defaultIsIn = isIn;
203 foundWildcard = true;
204 } else {
205 int key = Integer.parseInt(item);
206 if (mIsIn.indexOfKey(key) >= 0) {
207 throw new IllegalArgumentException("Illegal spec, `" + key +
208 "` must not appear multiple times in `" + spec + "`");
209 }
210 mIsIn.put(key, isIn);
211 }
212 }
213
214 if (!foundWildcard) {
215 throw new IllegalArgumentException("Illegal spec, must specify either * or !*");
216 }
217
218 mDefaultIsIn = defaultIsIn;
219 }
220
221 public boolean isIn(int value) {
222 return (mIsIn.get(value, mDefaultIsIn));
223 }
224 }
John Spurlockd06aa572014-09-10 10:40:49 -0400225}