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