blob: 1d890d0dde6cd63e3ca9ba76b50efca80e37f5ed [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
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
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 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());
57 pw.print(" getPulseOnPickup(): "); pw.println(getPulseOnPickup());
58 pw.print(" getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
John Spurlock686e4d52014-11-20 21:48:09 -050059 pw.print(" getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse());
John Spurlock190d0262014-09-14 15:39:13 -040060 pw.print(" getPulseOnNotifications(): "); pw.println(getPulseOnNotifications());
61 pw.print(" getPulseSchedule(): "); pw.println(getPulseSchedule());
62 pw.print(" getPulseScheduleResets(): "); pw.println(getPulseScheduleResets());
John Spurlock50a8ea62014-09-16 09:12:03 -040063 pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
John Spurlockf5d250d2014-12-02 10:41:25 -050064 pw.print(" getPickupPerformsProxCheck(): "); pw.println(getPickupPerformsProxCheck());
John Spurlock190d0262014-09-14 15:39:13 -040065 }
66
67 public boolean getDisplayStateSupported() {
68 return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
John Spurlockd06aa572014-09-10 10:40:49 -040069 }
70
John Spurlockf5d250d2014-12-02 10:41:25 -050071 public int getPulseDuration(boolean pickup) {
72 return getPulseInDuration(pickup) + getPulseVisibleDuration() + getPulseOutDuration();
John Spurlockd06aa572014-09-10 10:40:49 -040073 }
74
John Spurlockf5d250d2014-12-02 10:41:25 -050075 public int getPulseInDuration(boolean pickup) {
76 return pickup
77 ? getInt("doze.pulse.duration.in.pickup", R.integer.doze_pulse_duration_in_pickup)
78 : getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
John Spurlockd06aa572014-09-10 10:40:49 -040079 }
80
81 public int getPulseVisibleDuration() {
82 return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible);
83 }
84
85 public int getPulseOutDuration() {
86 return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out);
87 }
88
John Spurlock190d0262014-09-14 15:39:13 -040089 public boolean getPulseOnSigMotion() {
90 return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion);
John Spurlockd06aa572014-09-10 10:40:49 -040091 }
92
John Spurlock190d0262014-09-14 15:39:13 -040093 public boolean getVibrateOnSigMotion() {
94 return SystemProperties.getBoolean("doze.vibrate.sigmotion", false);
95 }
96
97 public boolean getPulseOnPickup() {
98 return getBoolean("doze.pulse.pickup", R.bool.doze_pulse_on_pick_up);
99 }
100
101 public boolean getVibrateOnPickup() {
102 return SystemProperties.getBoolean("doze.vibrate.pickup", false);
103 }
104
John Spurlock686e4d52014-11-20 21:48:09 -0500105 public boolean getProxCheckBeforePulse() {
106 return getBoolean("doze.pulse.proxcheck", R.bool.doze_proximity_check_before_pulse);
107 }
108
John Spurlockf5d250d2014-12-02 10:41:25 -0500109 public boolean getPickupPerformsProxCheck() {
110 return getBoolean("doze.pickup.proxcheck", R.bool.doze_pickup_performs_proximity_check);
111 }
112
John Spurlock190d0262014-09-14 15:39:13 -0400113 public boolean getPulseOnNotifications() {
114 return getBoolean("doze.pulse.notifications", R.bool.doze_pulse_on_notifications);
115 }
116
117 public PulseSchedule getPulseSchedule() {
118 final String spec = getString("doze.pulse.schedule", R.string.doze_pulse_schedule);
119 if (sPulseSchedule == null || !sPulseSchedule.mSpec.equals(spec)) {
120 sPulseSchedule = PulseSchedule.parse(spec);
John Spurlockd06aa572014-09-10 10:40:49 -0400121 }
John Spurlock190d0262014-09-14 15:39:13 -0400122 return sPulseSchedule;
John Spurlockd06aa572014-09-10 10:40:49 -0400123 }
124
John Spurlock190d0262014-09-14 15:39:13 -0400125 public int getPulseScheduleResets() {
126 return getInt("doze.pulse.schedule.resets", R.integer.doze_pulse_schedule_resets);
127 }
128
John Spurlock50a8ea62014-09-16 09:12:03 -0400129 public int getPickupVibrationThreshold() {
130 return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
131 }
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
John Spurlock190d0262014-09-14 15:39:13 -0400146 public static class PulseSchedule {
147 private static final Pattern PATTERN = Pattern.compile("(\\d+?)s", 0);
John Spurlockd06aa572014-09-10 10:40:49 -0400148
149 private String mSpec;
John Spurlock190d0262014-09-14 15:39:13 -0400150 private int[] mSchedule;
John Spurlockd06aa572014-09-10 10:40:49 -0400151
John Spurlock190d0262014-09-14 15:39:13 -0400152 public static PulseSchedule parse(String spec) {
John Spurlockd06aa572014-09-10 10:40:49 -0400153 if (TextUtils.isEmpty(spec)) return null;
154 try {
John Spurlock190d0262014-09-14 15:39:13 -0400155 final PulseSchedule rt = new PulseSchedule();
John Spurlockd06aa572014-09-10 10:40:49 -0400156 rt.mSpec = spec;
157 final String[] tokens = spec.split(",");
John Spurlock190d0262014-09-14 15:39:13 -0400158 rt.mSchedule = new int[tokens.length];
159 for (int i = 0; i < tokens.length; i++) {
John Spurlockd06aa572014-09-10 10:40:49 -0400160 final Matcher m = PATTERN.matcher(tokens[i]);
161 if (!m.matches()) throw new IllegalArgumentException("Bad token: " + tokens[i]);
John Spurlock190d0262014-09-14 15:39:13 -0400162 rt.mSchedule[i] = Integer.parseInt(m.group(1));
John Spurlockd06aa572014-09-10 10:40:49 -0400163 }
John Spurlock190d0262014-09-14 15:39:13 -0400164 if (DEBUG) Log.d(TAG, "Parsed spec [" + spec + "] as: " + rt);
John Spurlockd06aa572014-09-10 10:40:49 -0400165 return rt;
166 } catch (RuntimeException e) {
167 Log.w(TAG, "Error parsing spec: " + spec, e);
168 return null;
169 }
170 }
171
John Spurlock190d0262014-09-14 15:39:13 -0400172 @Override
173 public String toString() {
174 return Arrays.toString(mSchedule);
175 }
176
177 public long getNextTime(long now, long notificationTime) {
178 for (int i = 0; i < mSchedule.length; i++) {
179 final long time = notificationTime + mSchedule[i] * 1000;
180 if (time > now) return time;
John Spurlockd06aa572014-09-10 10:40:49 -0400181 }
John Spurlock190d0262014-09-14 15:39:13 -0400182 return 0;
John Spurlockd06aa572014-09-10 10:40:49 -0400183 }
184 }
185}