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 /** 56 * Integer used to dim the screen while dozing. 57 * 58 * @see R.integer.config_screenBrightnessDoze 59 */ 60 public int defaultDozeBrightness; 61 62 /** 63 * Integer used to dim the screen just before the screen turns off. 64 * 65 * @see R.integer.config_screenBrightnessDim 66 */ 67 public int dimBrightness; 68 69 /** 70 * Integer array to map ambient brightness type to real screen brightness. 71 * 72 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 73 * @see #KEY_SCREEN_BRIGHTNESS_ARRAY 74 */ 75 public int[] screenBrightnessArray; 76 77 /** 78 * Integer array to map ambient brightness type to dimming scrim. 79 * 80 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 81 * @see #KEY_DIMMING_SCRIM_ARRAY 82 */ 83 public int[] dimmingScrimArray; 84 85 /** 86 * Delay time(ms) from covering the prox to turning off the screen. 87 * 88 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 89 * @see #KEY_PROX_SCREEN_OFF_DELAY_MS 90 */ 91 public long proxScreenOffDelayMs; 92 93 /** 94 * The threshold time(ms) to trigger the cooldown timer, which will 95 * turn off prox sensor for a period. 96 * 97 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 98 * @see #KEY_PROX_COOLDOWN_TRIGGER_MS 99 */ 100 public long proxCooldownTriggerMs; 101 102 /** 103 * The period(ms) to turning off the prox sensor if 104 * {@link #KEY_PROX_COOLDOWN_TRIGGER_MS} is triggered. 105 * 106 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 107 * @see #KEY_PROX_COOLDOWN_PERIOD_MS 108 */ 109 public long proxCooldownPeriodMs; 110 111 /** 112 * For how long(ms) the wallpaper should still be visible 113 * after entering AoD. 114 * 115 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 116 * @see #KEY_WALLPAPER_VISIBILITY_MS 117 */ 118 public long wallpaperVisibilityDuration; 119 120 /** 121 * Duration(ms) of the fade out animation after 122 * {@link #KEY_WALLPAPER_VISIBILITY_MS} elapses. 123 * 124 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 125 * @see #KEY_WALLPAPER_FADE_OUT_MS 126 */ 127 public long wallpaperFadeOutDuration; 128 129 private final KeyValueListParser mParser; 130 private final Context mContext; 131 private SettingsObserver mSettingsObserver; 132 AlwaysOnDisplayPolicy(Context context)133 public AlwaysOnDisplayPolicy(Context context) { 134 context = context.getApplicationContext(); 135 mContext = context; 136 mParser = new KeyValueListParser(','); 137 mSettingsObserver = new SettingsObserver(context.getMainThreadHandler()); 138 mSettingsObserver.observe(); 139 } 140 141 private final class SettingsObserver extends ContentObserver { 142 private final Uri ALWAYS_ON_DISPLAY_CONSTANTS_URI 143 = Settings.Global.getUriFor(Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS); 144 SettingsObserver(Handler handler)145 SettingsObserver(Handler handler) { 146 super(handler); 147 } 148 observe()149 void observe() { 150 ContentResolver resolver = mContext.getContentResolver(); 151 resolver.registerContentObserver(ALWAYS_ON_DISPLAY_CONSTANTS_URI, 152 false, this, UserHandle.USER_ALL); 153 update(null); 154 } 155 156 @Override onChange(boolean selfChange, Uri uri)157 public void onChange(boolean selfChange, Uri uri) { 158 update(uri); 159 } 160 update(Uri uri)161 public void update(Uri uri) { 162 if (uri == null || ALWAYS_ON_DISPLAY_CONSTANTS_URI.equals(uri)) { 163 final Resources resources = mContext.getResources(); 164 final String value = Settings.Global.getString(mContext.getContentResolver(), 165 Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS); 166 167 try { 168 mParser.setString(value); 169 } catch (IllegalArgumentException e) { 170 Log.e(TAG, "Bad AOD constants"); 171 } 172 173 proxScreenOffDelayMs = mParser.getLong(KEY_PROX_SCREEN_OFF_DELAY_MS, 174 DEFAULT_PROX_SCREEN_OFF_DELAY_MS); 175 proxCooldownTriggerMs = mParser.getLong(KEY_PROX_COOLDOWN_TRIGGER_MS, 176 DEFAULT_PROX_COOLDOWN_TRIGGER_MS); 177 proxCooldownPeriodMs = mParser.getLong(KEY_PROX_COOLDOWN_PERIOD_MS, 178 DEFAULT_PROX_COOLDOWN_PERIOD_MS); 179 wallpaperFadeOutDuration = mParser.getLong(KEY_WALLPAPER_FADE_OUT_MS, 180 DEFAULT_WALLPAPER_FADE_OUT_MS); 181 wallpaperVisibilityDuration = mParser.getLong(KEY_WALLPAPER_VISIBILITY_MS, 182 DEFAULT_WALLPAPER_VISIBILITY_MS); 183 defaultDozeBrightness = resources.getInteger( 184 com.android.internal.R.integer.config_screenBrightnessDoze); 185 dimBrightness = resources.getInteger( 186 com.android.internal.R.integer.config_screenBrightnessDim); 187 screenBrightnessArray = mParser.getIntArray(KEY_SCREEN_BRIGHTNESS_ARRAY, 188 resources.getIntArray( 189 R.array.config_doze_brightness_sensor_to_brightness)); 190 dimmingScrimArray = mParser.getIntArray(KEY_DIMMING_SCRIM_ARRAY, 191 resources.getIntArray( 192 R.array.config_doze_brightness_sensor_to_scrim_opacity)); 193 } 194 } 195 } 196 } 197