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