Home | History | Annotate | Download | only in doze
      1 /*
      2  * Copyright (C) 2017 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.doze;
     18 
     19 import android.content.ContentResolver;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.database.ContentObserver;
     23 import android.net.Uri;
     24 import android.os.Handler;
     25 import android.os.UserHandle;
     26 import android.provider.Settings;
     27 import android.text.format.DateUtils;
     28 import android.util.KeyValueListParser;
     29 import android.util.Log;
     30 
     31 import com.android.systemui.R;
     32 
     33 /**
     34  * Class to store the policy for AOD, which comes from
     35  * {@link android.provider.Settings.Global}
     36  */
     37 public class AlwaysOnDisplayPolicy {
     38     public static final String TAG = "AlwaysOnDisplayPolicy";
     39 
     40     private static final long DEFAULT_PROX_SCREEN_OFF_DELAY_MS = 10 * DateUtils.SECOND_IN_MILLIS;
     41     private static final long DEFAULT_PROX_COOLDOWN_TRIGGER_MS = 2 * DateUtils.SECOND_IN_MILLIS;
     42     private static final long DEFAULT_PROX_COOLDOWN_PERIOD_MS = 5 * DateUtils.SECOND_IN_MILLIS;
     43     private static final long DEFAULT_WALLPAPER_VISIBILITY_MS = 60 * DateUtils.SECOND_IN_MILLIS;
     44     private static final long DEFAULT_WALLPAPER_FADE_OUT_MS = 400;
     45 
     46     static final String KEY_SCREEN_BRIGHTNESS_ARRAY = "screen_brightness_array";
     47     static final String KEY_DIMMING_SCRIM_ARRAY = "dimming_scrim_array";
     48     static final String KEY_PROX_SCREEN_OFF_DELAY_MS = "prox_screen_off_delay";
     49     static final String KEY_PROX_COOLDOWN_TRIGGER_MS = "prox_cooldown_trigger";
     50     static final String KEY_PROX_COOLDOWN_PERIOD_MS = "prox_cooldown_period";
     51     static final String KEY_WALLPAPER_VISIBILITY_MS = "wallpaper_visibility_timeout";
     52     static final String KEY_WALLPAPER_FADE_OUT_MS = "wallpaper_fade_out_duration";
     53 
     54     /**
     55      * Integer array to map ambient brightness type to real screen brightness.
     56      *
     57      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
     58      * @see #KEY_SCREEN_BRIGHTNESS_ARRAY
     59      */
     60     public int[] screenBrightnessArray;
     61 
     62     /**
     63      * Integer array to map ambient brightness type to dimming scrim.
     64      *
     65      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
     66      * @see #KEY_DIMMING_SCRIM_ARRAY
     67      */
     68     public int[] dimmingScrimArray;
     69 
     70     /**
     71      * Delay time(ms) from covering the prox to turning off the screen.
     72      *
     73      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
     74      * @see #KEY_PROX_SCREEN_OFF_DELAY_MS
     75      */
     76     public long proxScreenOffDelayMs;
     77 
     78     /**
     79      * The threshold time(ms) to trigger the cooldown timer, which will
     80      * turn off prox sensor for a period.
     81      *
     82      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
     83      * @see #KEY_PROX_COOLDOWN_TRIGGER_MS
     84      */
     85     public long proxCooldownTriggerMs;
     86 
     87     /**
     88      * The period(ms) to turning off the prox sensor if
     89      * {@link #KEY_PROX_COOLDOWN_TRIGGER_MS} is triggered.
     90      *
     91      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
     92      * @see #KEY_PROX_COOLDOWN_PERIOD_MS
     93      */
     94     public long proxCooldownPeriodMs;
     95 
     96     /**
     97      * For how long(ms) the wallpaper should still be visible
     98      * after entering AoD.
     99      *
    100      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
    101      * @see #KEY_WALLPAPER_VISIBILITY_MS
    102      */
    103     public long wallpaperVisibilityDuration;
    104 
    105     /**
    106      * Duration(ms) of the fade out animation after
    107      * {@link #KEY_WALLPAPER_VISIBILITY_MS} elapses.
    108      *
    109      * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
    110      * @see #KEY_WALLPAPER_FADE_OUT_MS
    111      */
    112     public long wallpaperFadeOutDuration;
    113 
    114     private final KeyValueListParser mParser;
    115     private final Context mContext;
    116     private SettingsObserver mSettingsObserver;
    117 
    118     public AlwaysOnDisplayPolicy(Context context) {
    119         context = context.getApplicationContext();
    120         mContext = context;
    121         mParser = new KeyValueListParser(',');
    122         mSettingsObserver = new SettingsObserver(context.getMainThreadHandler());
    123         mSettingsObserver.observe();
    124     }
    125 
    126     private final class SettingsObserver extends ContentObserver {
    127         private final Uri ALWAYS_ON_DISPLAY_CONSTANTS_URI
    128                 = Settings.Global.getUriFor(Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS);
    129 
    130         SettingsObserver(Handler handler) {
    131             super(handler);
    132         }
    133 
    134         void observe() {
    135             ContentResolver resolver = mContext.getContentResolver();
    136             resolver.registerContentObserver(ALWAYS_ON_DISPLAY_CONSTANTS_URI,
    137                     false, this, UserHandle.USER_ALL);
    138             update(null);
    139         }
    140 
    141         @Override
    142         public void onChange(boolean selfChange, Uri uri) {
    143             update(uri);
    144         }
    145 
    146         public void update(Uri uri) {
    147             if (uri == null || ALWAYS_ON_DISPLAY_CONSTANTS_URI.equals(uri)) {
    148                 final Resources resources = mContext.getResources();
    149                 final String value = Settings.Global.getString(mContext.getContentResolver(),
    150                         Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS);
    151 
    152                 try {
    153                     mParser.setString(value);
    154                 } catch (IllegalArgumentException e) {
    155                     Log.e(TAG, "Bad AOD constants");
    156                 }
    157 
    158                 proxScreenOffDelayMs = mParser.getLong(KEY_PROX_SCREEN_OFF_DELAY_MS,
    159                         DEFAULT_PROX_SCREEN_OFF_DELAY_MS);
    160                 proxCooldownTriggerMs = mParser.getLong(KEY_PROX_COOLDOWN_TRIGGER_MS,
    161                         DEFAULT_PROX_COOLDOWN_TRIGGER_MS);
    162                 proxCooldownPeriodMs = mParser.getLong(KEY_PROX_COOLDOWN_PERIOD_MS,
    163                         DEFAULT_PROX_COOLDOWN_PERIOD_MS);
    164                 wallpaperFadeOutDuration = mParser.getLong(KEY_WALLPAPER_FADE_OUT_MS,
    165                         DEFAULT_WALLPAPER_FADE_OUT_MS);
    166                 wallpaperVisibilityDuration = mParser.getLong(KEY_WALLPAPER_VISIBILITY_MS,
    167                         DEFAULT_WALLPAPER_VISIBILITY_MS);
    168                 screenBrightnessArray = mParser.getIntArray(KEY_SCREEN_BRIGHTNESS_ARRAY,
    169                         resources.getIntArray(
    170                                 R.array.config_doze_brightness_sensor_to_brightness));
    171                 dimmingScrimArray = mParser.getIntArray(KEY_DIMMING_SCRIM_ARRAY,
    172                         resources.getIntArray(
    173                                 R.array.config_doze_brightness_sensor_to_scrim_opacity));
    174             }
    175         }
    176     }
    177 }
    178