John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.SystemProperties; |
| 21 | import android.text.TextUtils; |
| 22 | import android.util.Log; |
| 23 | import android.util.MathUtils; |
| 24 | |
| 25 | import com.android.systemui.R; |
| 26 | |
| 27 | import java.io.PrintWriter; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 28 | import java.util.Arrays; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 29 | import java.util.regex.Matcher; |
| 30 | import java.util.regex.Pattern; |
| 31 | |
| 32 | public class DozeParameters { |
| 33 | private static final String TAG = "DozeParameters"; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 34 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 35 | |
| 36 | private static final int MAX_DURATION = 10 * 1000; |
| 37 | |
| 38 | private final Context mContext; |
| 39 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 40 | private static PulseSchedule sPulseSchedule; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 41 | |
| 42 | public DozeParameters(Context context) { |
| 43 | mContext = context; |
| 44 | } |
| 45 | |
| 46 | public void dump(PrintWriter pw) { |
| 47 | pw.println(" DozeParameters:"); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 48 | pw.print(" getDisplayStateSupported(): "); pw.println(getDisplayStateSupported()); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 49 | pw.print(" getPulseDuration(): "); pw.println(getPulseDuration()); |
| 50 | pw.print(" getPulseInDuration(): "); pw.println(getPulseInDuration()); |
Jorim Jaggi | cfaec48 | 2014-10-29 14:30:16 +0100 | [diff] [blame] | 51 | pw.print(" getPulseInDelay(): "); pw.println(getPulseInDelay()); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 52 | pw.print(" getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration()); |
| 53 | pw.print(" getPulseOutDuration(): "); pw.println(getPulseOutDuration()); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 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()); |
John Spurlock | 686e4d5 | 2014-11-20 21:48:09 -0500 | [diff] [blame^] | 58 | pw.print(" getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse()); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 59 | pw.print(" getPulseOnNotifications(): "); pw.println(getPulseOnNotifications()); |
| 60 | pw.print(" getPulseSchedule(): "); pw.println(getPulseSchedule()); |
| 61 | pw.print(" getPulseScheduleResets(): "); pw.println(getPulseScheduleResets()); |
John Spurlock | 50a8ea6 | 2014-09-16 09:12:03 -0400 | [diff] [blame] | 62 | pw.print(" getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold()); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public boolean getDisplayStateSupported() { |
| 66 | return getBoolean("doze.display.supported", R.bool.doze_display_state_supported); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public int getPulseDuration() { |
| 70 | return getPulseInDuration() + getPulseVisibleDuration() + getPulseOutDuration(); |
| 71 | } |
| 72 | |
| 73 | public int getPulseInDuration() { |
| 74 | return getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in); |
| 75 | } |
| 76 | |
Jorim Jaggi | 16423aa | 2014-10-28 11:42:58 +0100 | [diff] [blame] | 77 | public int getPulseInDelay() { |
| 78 | return getInt("doze.pulse.delay.in", R.integer.doze_pulse_delay_in); |
| 79 | } |
| 80 | |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 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 Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 89 | public boolean getPulseOnSigMotion() { |
| 90 | return getBoolean("doze.pulse.sigmotion", R.bool.doze_pulse_on_significant_motion); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 91 | } |
| 92 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 93 | 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 Spurlock | 686e4d5 | 2014-11-20 21:48:09 -0500 | [diff] [blame^] | 105 | public boolean getProxCheckBeforePulse() { |
| 106 | return getBoolean("doze.pulse.proxcheck", R.bool.doze_proximity_check_before_pulse); |
| 107 | } |
| 108 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 109 | public boolean getPulseOnNotifications() { |
| 110 | return getBoolean("doze.pulse.notifications", R.bool.doze_pulse_on_notifications); |
| 111 | } |
| 112 | |
| 113 | public PulseSchedule getPulseSchedule() { |
| 114 | final String spec = getString("doze.pulse.schedule", R.string.doze_pulse_schedule); |
| 115 | if (sPulseSchedule == null || !sPulseSchedule.mSpec.equals(spec)) { |
| 116 | sPulseSchedule = PulseSchedule.parse(spec); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 117 | } |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 118 | return sPulseSchedule; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 119 | } |
| 120 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 121 | public int getPulseScheduleResets() { |
| 122 | return getInt("doze.pulse.schedule.resets", R.integer.doze_pulse_schedule_resets); |
| 123 | } |
| 124 | |
John Spurlock | 50a8ea6 | 2014-09-16 09:12:03 -0400 | [diff] [blame] | 125 | public int getPickupVibrationThreshold() { |
| 126 | return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold); |
| 127 | } |
| 128 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 129 | private boolean getBoolean(String propName, int resId) { |
| 130 | return SystemProperties.getBoolean(propName, mContext.getResources().getBoolean(resId)); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private int getInt(String propName, int resId) { |
| 134 | int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId)); |
| 135 | return MathUtils.constrain(value, 0, MAX_DURATION); |
| 136 | } |
| 137 | |
| 138 | private String getString(String propName, int resId) { |
| 139 | return SystemProperties.get(propName, mContext.getString(resId)); |
| 140 | } |
| 141 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 142 | public static class PulseSchedule { |
| 143 | private static final Pattern PATTERN = Pattern.compile("(\\d+?)s", 0); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 144 | |
| 145 | private String mSpec; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 146 | private int[] mSchedule; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 147 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 148 | public static PulseSchedule parse(String spec) { |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 149 | if (TextUtils.isEmpty(spec)) return null; |
| 150 | try { |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 151 | final PulseSchedule rt = new PulseSchedule(); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 152 | rt.mSpec = spec; |
| 153 | final String[] tokens = spec.split(","); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 154 | rt.mSchedule = new int[tokens.length]; |
| 155 | for (int i = 0; i < tokens.length; i++) { |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 156 | final Matcher m = PATTERN.matcher(tokens[i]); |
| 157 | if (!m.matches()) throw new IllegalArgumentException("Bad token: " + tokens[i]); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 158 | rt.mSchedule[i] = Integer.parseInt(m.group(1)); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 159 | } |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 160 | if (DEBUG) Log.d(TAG, "Parsed spec [" + spec + "] as: " + rt); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 161 | return rt; |
| 162 | } catch (RuntimeException e) { |
| 163 | Log.w(TAG, "Error parsing spec: " + spec, e); |
| 164 | return null; |
| 165 | } |
| 166 | } |
| 167 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 168 | @Override |
| 169 | public String toString() { |
| 170 | return Arrays.toString(mSchedule); |
| 171 | } |
| 172 | |
| 173 | public long getNextTime(long now, long notificationTime) { |
| 174 | for (int i = 0; i < mSchedule.length; i++) { |
| 175 | final long time = notificationTime + mSchedule[i] * 1000; |
| 176 | if (time > now) return time; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 177 | } |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 178 | return 0; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |