DozeParameters.java revision cfaec4862e5f07879ec5e83f2f0445ca02d38e00
1/*
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;
28import java.util.Arrays;
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32public class DozeParameters {
33    private static final String TAG = "DozeParameters";
34    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
35
36    private static final int MAX_DURATION = 10 * 1000;
37
38    private final Context mContext;
39
40    private static PulseSchedule sPulseSchedule;
41
42    public DozeParameters(Context context) {
43        mContext = context;
44    }
45
46    public void dump(PrintWriter pw) {
47        pw.println("  DozeParameters:");
48        pw.print("    getDisplayStateSupported(): "); pw.println(getDisplayStateSupported());
49        pw.print("    getPulseDuration(): "); pw.println(getPulseDuration());
50        pw.print("    getPulseInDuration(): "); pw.println(getPulseInDuration());
51        pw.print("    getPulseInDelay(): "); pw.println(getPulseInDelay());
52        pw.print("    getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration());
53        pw.print("    getPulseOutDuration(): "); pw.println(getPulseOutDuration());
54        pw.print("    getPulseOnSigMotion(): "); pw.println(getPulseOnSigMotion());
55        pw.print("    getVibrateOnSigMotion(): "); pw.println(getVibrateOnSigMotion());
56        pw.print("    getPulseOnPickup(): "); pw.println(getPulseOnPickup());
57        pw.print("    getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
58        pw.print("    getPulseOnNotifications(): "); pw.println(getPulseOnNotifications());
59        pw.print("    getPulseSchedule(): "); pw.println(getPulseSchedule());
60        pw.print("    getPulseScheduleResets(): "); pw.println(getPulseScheduleResets());
61        pw.print("    getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
62    }
63
64    public boolean getDisplayStateSupported() {
65        return getBoolean("doze.display.supported", R.bool.doze_display_state_supported);
66    }
67
68    public int getPulseDuration() {
69        return getPulseInDuration() + getPulseVisibleDuration() + getPulseOutDuration();
70    }
71
72    public int getPulseInDuration() {
73        return getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
74    }
75
76    public int getPulseInDelay() {
77        return getInt("doze.pulse.delay.in", R.integer.doze_pulse_delay_in);
78    }
79
80    public int getPulseVisibleDuration() {
81        return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible);
82    }
83
84    public int getPulseOutDuration() {
85        return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out);
86    }
87
88    public boolean getPulseOnSigMotion() {
89        return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion);
90    }
91
92    public boolean getVibrateOnSigMotion() {
93        return SystemProperties.getBoolean("doze.vibrate.sigmotion", false);
94    }
95
96    public boolean getPulseOnPickup() {
97        return getBoolean("doze.pulse.pickup", R.bool.doze_pulse_on_pick_up);
98    }
99
100    public boolean getVibrateOnPickup() {
101        return SystemProperties.getBoolean("doze.vibrate.pickup", false);
102    }
103
104    public boolean getPulseOnNotifications() {
105        return getBoolean("doze.pulse.notifications", R.bool.doze_pulse_on_notifications);
106    }
107
108    public PulseSchedule getPulseSchedule() {
109        final String spec = getString("doze.pulse.schedule", R.string.doze_pulse_schedule);
110        if (sPulseSchedule == null || !sPulseSchedule.mSpec.equals(spec)) {
111            sPulseSchedule = PulseSchedule.parse(spec);
112        }
113        return sPulseSchedule;
114    }
115
116    public int getPulseScheduleResets() {
117        return getInt("doze.pulse.schedule.resets", R.integer.doze_pulse_schedule_resets);
118    }
119
120    public int getPickupVibrationThreshold() {
121        return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
122    }
123
124    private boolean getBoolean(String propName, int resId) {
125        return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId));
126    }
127
128    private int getInt(String propName, int resId) {
129        int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId));
130        return MathUtils.constrain(value, 0, MAX_DURATION);
131    }
132
133    private String getString(String propName, int resId) {
134        return SystemProperties.get(propName, mContext.getString(resId));
135    }
136
137    public static class PulseSchedule {
138        private static final Pattern PATTERN = Pattern.compile("(\\d+?)s", 0);
139
140        private String mSpec;
141        private int[] mSchedule;
142
143        public static PulseSchedule parse(String spec) {
144            if (TextUtils.isEmpty(spec)) return null;
145            try {
146                final PulseSchedule rt = new PulseSchedule();
147                rt.mSpec = spec;
148                final String[] tokens = spec.split(",");
149                rt.mSchedule = new int[tokens.length];
150                for (int i = 0; i < tokens.length; i++) {
151                    final Matcher m = PATTERN.matcher(tokens[i]);
152                    if (!m.matches()) throw new IllegalArgumentException("Bad token: " + tokens[i]);
153                    rt.mSchedule[i] = Integer.parseInt(m.group(1));
154                }
155                if (DEBUG) Log.d(TAG, "Parsed spec [" + spec + "] as: " + rt);
156                return rt;
157            } catch (RuntimeException e) {
158                Log.w(TAG, "Error parsing spec: " + spec, e);
159                return null;
160            }
161        }
162
163        @Override
164        public String toString() {
165            return Arrays.toString(mSchedule);
166        }
167
168        public long getNextTime(long now, long notificationTime) {
169            for (int i = 0; i < mSchedule.length; i++) {
170                final long time = notificationTime + mSchedule[i] * 1000;
171                if (time > now) return time;
172            }
173            return 0;
174        }
175    }
176}
177