blob: a956151f7b50a14f492cbd0c4739a92679524a55 [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;
21import android.text.TextUtils;
22import android.util.Log;
23import android.util.MathUtils;
24
25import com.android.systemui.R;
26
27import java.io.PrintWriter;
John Spurlock190d0262014-09-14 15:39:13 -040028import java.util.Arrays;
John Spurlockd06aa572014-09-10 10:40:49 -040029import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32public class DozeParameters {
33 private static final String TAG = "DozeParameters";
John Spurlock190d0262014-09-14 15:39:13 -040034 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlockd06aa572014-09-10 10:40:49 -040035
36 private static final int MAX_DURATION = 10 * 1000;
37
38 private final Context mContext;
39
John Spurlock190d0262014-09-14 15:39:13 -040040 private static PulseSchedule sPulseSchedule;
John Spurlockd06aa572014-09-10 10:40:49 -040041
42 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 Spurlockd06aa572014-09-10 10:40:49 -040049 pw.print(" getPulseDuration(): "); pw.println(getPulseDuration());
50 pw.print(" getPulseInDuration(): "); pw.println(getPulseInDuration());
51 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());
55 pw.print(" getPulseOnPickup(): "); pw.println(getPulseOnPickup());
56 pw.print(" getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
57 pw.print(" getPulseOnNotifications(): "); pw.println(getPulseOnNotifications());
58 pw.print(" getPulseSchedule(): "); pw.println(getPulseSchedule());
59 pw.print(" getPulseScheduleResets(): "); pw.println(getPulseScheduleResets());
John Spurlock50a8ea62014-09-16 09:12:03 -040060 pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
John Spurlock190d0262014-09-14 15:39:13 -040061 }
62
63 public boolean getDisplayStateSupported() {
64 return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
John Spurlockd06aa572014-09-10 10:40:49 -040065 }
66
67 public int getPulseDuration() {
68 return getPulseInDuration() + getPulseVisibleDuration() + getPulseOutDuration();
69 }
70
71 public int getPulseInDuration() {
72 return getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
73 }
74
Jorim Jaggi16423aa2014-10-28 11:42:58 +010075 public int getPulseInDelay() {
76 return getInt("doze.pulse.delay.in", R.integer.doze_pulse_delay_in);
77 }
78
John Spurlockd06aa572014-09-10 10:40:49 -040079 public int getPulseVisibleDuration() {
80 return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible);
81 }
82
83 public int getPulseOutDuration() {
84 return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out);
85 }
86
John Spurlock190d0262014-09-14 15:39:13 -040087 public boolean getPulseOnSigMotion() {
88 return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion);
John Spurlockd06aa572014-09-10 10:40:49 -040089 }
90
John Spurlock190d0262014-09-14 15:39:13 -040091 public boolean getVibrateOnSigMotion() {
92 return SystemProperties.getBoolean("doze.vibrate.sigmotion", false);
93 }
94
95 public boolean getPulseOnPickup() {
96 return getBoolean("doze.pulse.pickup", R.bool.doze_pulse_on_pick_up);
97 }
98
99 public boolean getVibrateOnPickup() {
100 return SystemProperties.getBoolean("doze.vibrate.pickup", false);
101 }
102
103 public boolean getPulseOnNotifications() {
104 return getBoolean("doze.pulse.notifications", R.bool.doze_pulse_on_notifications);
105 }
106
107 public PulseSchedule getPulseSchedule() {
108 final String spec = getString("doze.pulse.schedule", R.string.doze_pulse_schedule);
109 if (sPulseSchedule == null || !sPulseSchedule.mSpec.equals(spec)) {
110 sPulseSchedule = PulseSchedule.parse(spec);
John Spurlockd06aa572014-09-10 10:40:49 -0400111 }
John Spurlock190d0262014-09-14 15:39:13 -0400112 return sPulseSchedule;
John Spurlockd06aa572014-09-10 10:40:49 -0400113 }
114
John Spurlock190d0262014-09-14 15:39:13 -0400115 public int getPulseScheduleResets() {
116 return getInt("doze.pulse.schedule.resets", R.integer.doze_pulse_schedule_resets);
117 }
118
John Spurlock50a8ea62014-09-16 09:12:03 -0400119 public int getPickupVibrationThreshold() {
120 return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
121 }
122
John Spurlock190d0262014-09-14 15:39:13 -0400123 private boolean getBoolean(String propName, int resId) {
124 return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId));
John Spurlockd06aa572014-09-10 10:40:49 -0400125 }
126
127 private int getInt(String propName, int resId) {
128 int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId));
129 return MathUtils.constrain(value, 0, MAX_DURATION);
130 }
131
132 private String getString(String propName, int resId) {
133 return SystemProperties.get(propName, mContext.getString(resId));
134 }
135
John Spurlock190d0262014-09-14 15:39:13 -0400136 public static class PulseSchedule {
137 private static final Pattern PATTERN = Pattern.compile("(\\d+?)s", 0);
John Spurlockd06aa572014-09-10 10:40:49 -0400138
139 private String mSpec;
John Spurlock190d0262014-09-14 15:39:13 -0400140 private int[] mSchedule;
John Spurlockd06aa572014-09-10 10:40:49 -0400141
John Spurlock190d0262014-09-14 15:39:13 -0400142 public static PulseSchedule parse(String spec) {
John Spurlockd06aa572014-09-10 10:40:49 -0400143 if (TextUtils.isEmpty(spec)) return null;
144 try {
John Spurlock190d0262014-09-14 15:39:13 -0400145 final PulseSchedule rt = new PulseSchedule();
John Spurlockd06aa572014-09-10 10:40:49 -0400146 rt.mSpec = spec;
147 final String[] tokens = spec.split(",");
John Spurlock190d0262014-09-14 15:39:13 -0400148 rt.mSchedule = new int[tokens.length];
149 for (int i = 0; i < tokens.length; i++) {
John Spurlockd06aa572014-09-10 10:40:49 -0400150 final Matcher m = PATTERN.matcher(tokens[i]);
151 if (!m.matches()) throw new IllegalArgumentException("Bad token: " + tokens[i]);
John Spurlock190d0262014-09-14 15:39:13 -0400152 rt.mSchedule[i] = Integer.parseInt(m.group(1));
John Spurlockd06aa572014-09-10 10:40:49 -0400153 }
John Spurlock190d0262014-09-14 15:39:13 -0400154 if (DEBUG) Log.d(TAG, "Parsed spec [" + spec + "] as: " + rt);
John Spurlockd06aa572014-09-10 10:40:49 -0400155 return rt;
156 } catch (RuntimeException e) {
157 Log.w(TAG, "Error parsing spec: " + spec, e);
158 return null;
159 }
160 }
161
John Spurlock190d0262014-09-14 15:39:13 -0400162 @Override
163 public String toString() {
164 return Arrays.toString(mSchedule);
165 }
166
167 public long getNextTime(long now, long notificationTime) {
168 for (int i = 0; i < mSchedule.length; i++) {
169 final long time = notificationTime + mSchedule[i] * 1000;
170 if (time > now) return time;
John Spurlockd06aa572014-09-10 10:40:49 -0400171 }
John Spurlock190d0262014-09-14 15:39:13 -0400172 return 0;
John Spurlockd06aa572014-09-10 10:40:49 -0400173 }
174 }
175}