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; |
| 28 | import java.util.regex.Matcher; |
| 29 | import java.util.regex.Pattern; |
| 30 | |
| 31 | public class DozeParameters { |
| 32 | private static final String TAG = "DozeParameters"; |
| 33 | |
| 34 | private static final int MAX_DURATION = 10 * 1000; |
| 35 | |
| 36 | private final Context mContext; |
| 37 | |
| 38 | private StepFunction mPulsePeriodFunction; |
| 39 | |
| 40 | public DozeParameters(Context context) { |
| 41 | mContext = context; |
| 42 | } |
| 43 | |
| 44 | public void dump(PrintWriter pw) { |
| 45 | pw.println(" DozeParameters:"); |
| 46 | pw.print(" getPulseDuration(): "); pw.println(getPulseDuration()); |
| 47 | pw.print(" getPulseInDuration(): "); pw.println(getPulseInDuration()); |
| 48 | pw.print(" getPulseInVisibleDuration(): "); pw.println(getPulseVisibleDuration()); |
| 49 | pw.print(" getPulseOutDuration(): "); pw.println(getPulseOutDuration()); |
| 50 | pw.print(" getPulseStartDelay(): "); pw.println(getPulseStartDelay()); |
| 51 | pw.print(" getPulsePeriodFunction(): "); pw.println(getPulsePeriodFunction()); |
| 52 | } |
| 53 | |
| 54 | public int getPulseDuration() { |
| 55 | return getPulseInDuration() + getPulseVisibleDuration() + getPulseOutDuration(); |
| 56 | } |
| 57 | |
| 58 | public int getPulseInDuration() { |
| 59 | return getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in); |
| 60 | } |
| 61 | |
| 62 | public int getPulseVisibleDuration() { |
| 63 | return getInt("doze.pulse.duration.visible", R.integer.doze_pulse_duration_visible); |
| 64 | } |
| 65 | |
| 66 | public int getPulseOutDuration() { |
| 67 | return getInt("doze.pulse.duration.out", R.integer.doze_pulse_duration_out); |
| 68 | } |
| 69 | |
| 70 | public int getPulseStartDelay() { |
| 71 | return getInt("doze.pulse.delay", R.integer.doze_pulse_delay); |
| 72 | } |
| 73 | |
| 74 | public long getPulsePeriod(long age) { |
| 75 | final String spec = getPulsePeriodFunction(); |
| 76 | if (mPulsePeriodFunction == null || !mPulsePeriodFunction.mSpec.equals(spec)) { |
| 77 | mPulsePeriodFunction = StepFunction.parse(spec); |
| 78 | } |
| 79 | return mPulsePeriodFunction != null ? mPulsePeriodFunction.evaluate(age) : 0; |
| 80 | } |
| 81 | |
| 82 | private String getPulsePeriodFunction() { |
| 83 | return getString("doze.pulse.period.function", R.string.doze_pulse_period_function); |
| 84 | } |
| 85 | |
| 86 | private int getInt(String propName, int resId) { |
| 87 | int value = SystemProperties.getInt(propName, mContext.getResources().getInteger(resId)); |
| 88 | return MathUtils.constrain(value, 0, MAX_DURATION); |
| 89 | } |
| 90 | |
| 91 | private String getString(String propName, int resId) { |
| 92 | return SystemProperties.get(propName, mContext.getString(resId)); |
| 93 | } |
| 94 | |
| 95 | private static class StepFunction { |
| 96 | private static final Pattern PATTERN = Pattern.compile("(\\d+?)(:(\\d+?))?", 0); |
| 97 | |
| 98 | private String mSpec; |
| 99 | private long[] mSteps; |
| 100 | private long[] mValues; |
| 101 | private long mDefault; |
| 102 | |
| 103 | public static StepFunction parse(String spec) { |
| 104 | if (TextUtils.isEmpty(spec)) return null; |
| 105 | try { |
| 106 | final StepFunction rt = new StepFunction(); |
| 107 | rt.mSpec = spec; |
| 108 | final String[] tokens = spec.split(","); |
| 109 | rt.mSteps = new long[tokens.length - 1]; |
| 110 | rt.mValues = new long[tokens.length - 1]; |
| 111 | for (int i = 0; i < tokens.length - 1; i++) { |
| 112 | final Matcher m = PATTERN.matcher(tokens[i]); |
| 113 | if (!m.matches()) throw new IllegalArgumentException("Bad token: " + tokens[i]); |
| 114 | rt.mSteps[i] = Long.parseLong(m.group(1)); |
| 115 | rt.mValues[i] = Long.parseLong(m.group(3)); |
| 116 | } |
| 117 | rt.mDefault = Long.parseLong(tokens[tokens.length - 1]); |
| 118 | return rt; |
| 119 | } catch (RuntimeException e) { |
| 120 | Log.w(TAG, "Error parsing spec: " + spec, e); |
| 121 | return null; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public long evaluate(long x) { |
| 126 | for (int i = 0; i < mSteps.length; i++) { |
| 127 | if (x < mSteps[i]) return mValues[i]; |
| 128 | } |
| 129 | return mDefault; |
| 130 | } |
| 131 | } |
| 132 | } |