AlwaysOnDisplayPolicy.java revision c21891284b31b1372ee48b2f588850a5c482389e
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 17package com.android.systemui.doze; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.provider.Settings; 22import android.text.format.DateUtils; 23import android.util.KeyValueListParser; 24import android.util.Log; 25 26import com.android.systemui.R; 27 28import java.util.Arrays; 29 30/** 31 * Class to store the policy for AOD, which comes from 32 * {@link android.provider.Settings.Global} 33 */ 34public class AlwaysOnDisplayPolicy { 35 public static final String TAG = "AlwaysOnDisplayPolicy"; 36 37 static final String KEY_SCREEN_BRIGHTNESS_ARRAY = "screen_brightness_array"; 38 static final String KEY_DIMMING_SCRIM_ARRAY = "dimming_scrim_array"; 39 static final String KEY_PROX_SCREEN_OFF_DELAY_MS = "prox_screen_off_delay"; 40 static final String KEY_PROX_COOLDOWN_TRIGGER_MS = "prox_cooldown_trigger"; 41 static final String KEY_PROX_COOLDOWN_PERIOD_MS = "prox_cooldown_period"; 42 43 /** 44 * Integer array to map ambient brightness type to real screen brightness. 45 * 46 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 47 * @see #KEY_SCREEN_BRIGHTNESS_ARRAY 48 */ 49 public final int[] screenBrightnessArray; 50 51 /** 52 * Integer array to map ambient brightness type to dimming scrim. 53 * 54 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 55 * @see #KEY_DIMMING_SCRIM_ARRAY 56 */ 57 public final int[] dimmingScrimArray; 58 59 /** 60 * Delay time(ms) from covering the prox to turning off the screen. 61 * 62 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 63 * @see #KEY_PROX_SCREEN_OFF_DELAY_MS 64 */ 65 public final long proxScreenOffDelayMs; 66 67 /** 68 * The threshold time(ms) to trigger the cooldown timer, which will 69 * turn off prox sensor for a period. 70 * 71 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 72 * @see #KEY_PROX_COOLDOWN_TRIGGER_MS 73 */ 74 public final long proxCooldownTriggerMs; 75 76 /** 77 * The period(ms) to turning off the prox sensor if 78 * {@link #KEY_PROX_COOLDOWN_TRIGGER_MS} is triggered. 79 * 80 * @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS 81 * @see #KEY_PROX_COOLDOWN_PERIOD_MS 82 */ 83 public final long proxCooldownPeriodMs; 84 85 private final KeyValueListParser mParser; 86 87 public AlwaysOnDisplayPolicy(Context context) { 88 final Resources resources = context.getResources(); 89 mParser = new KeyValueListParser(','); 90 91 final String value = Settings.Global.getString(context.getContentResolver(), 92 Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS); 93 94 try { 95 mParser.setString(value); 96 } catch (IllegalArgumentException e) { 97 Log.e(TAG, "Bad AOD constants"); 98 } 99 100 proxScreenOffDelayMs = mParser.getLong(KEY_PROX_SCREEN_OFF_DELAY_MS, 101 10 * DateUtils.SECOND_IN_MILLIS); 102 proxCooldownTriggerMs = mParser.getLong(KEY_PROX_COOLDOWN_TRIGGER_MS, 103 2 * DateUtils.SECOND_IN_MILLIS); 104 proxCooldownPeriodMs = mParser.getLong(KEY_PROX_COOLDOWN_PERIOD_MS, 105 5 * DateUtils.SECOND_IN_MILLIS); 106 screenBrightnessArray = parseIntArray(KEY_SCREEN_BRIGHTNESS_ARRAY, 107 resources.getIntArray(R.array.config_doze_brightness_sensor_to_brightness)); 108 dimmingScrimArray = parseIntArray(KEY_DIMMING_SCRIM_ARRAY, 109 resources.getIntArray(R.array.config_doze_brightness_sensor_to_scrim_opacity)); 110 } 111 112 private int[] parseIntArray(final String key, final int[] defaultArray) { 113 final String value = mParser.getString(key, null); 114 if (value != null) { 115 return Arrays.stream(value.split(":")).map(String::trim).mapToInt( 116 Integer::parseInt).toArray(); 117 } else { 118 return defaultArray; 119 } 120 } 121 122} 123