Merge "Hook up AOD knobs to Settings.Global" into oc-mr1-dev

This commit is contained in:
Lei Yu
2017-09-01 00:54:01 +00:00
committed by Android (Google) Code Review
11 changed files with 255 additions and 22 deletions

View File

@@ -9331,6 +9331,25 @@ public final class Settings {
*/
public static final String ANOMALY_DETECTION_CONSTANTS = "anomaly_detection_constants";
/**
* Always on display(AOD) specific settings
* This is encoded as a key=value list, separated by commas. Ex:
*
* "prox_screen_off_delay=10000,screen_brightness_array=0:1:2:3:4"
*
* The following keys are supported:
*
* <pre>
* screen_brightness_array (string)
* dimming_scrim_array (string)
* prox_screen_off_delay (long)
* prox_cooldown_trigger (long)
* prox_cooldown_period (long)
* </pre>
* @hide
*/
public static final String ALWAYS_ON_DISPLAY_CONSTANTS = "always_on_display_constants";
/**
* App standby (app idle) specific settings.
* This is encoded as a key=value list, separated by commas. Ex:

View File

@@ -99,6 +99,7 @@ public class SettingsBackupTest {
Settings.Global.ALARM_MANAGER_CONSTANTS,
Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED,
Settings.Global.ALWAYS_FINISH_ACTIVITIES,
Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS,
Settings.Global.ANIMATOR_DURATION_SCALE,
Settings.Global.ANOMALY_DETECTION_CONSTANTS,
Settings.Global.APN_DB_UPDATE_CONTENT_URL,

View File

@@ -0,0 +1,122 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.systemui.doze;
import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;
import android.text.format.DateUtils;
import android.util.KeyValueListParser;
import android.util.Log;
import com.android.systemui.R;
import java.util.Arrays;
/**
* Class to store the policy for AOD, which comes from
* {@link android.provider.Settings.Global}
*/
public class AlwaysOnDisplayPolicy {
public static final String TAG = "AlwaysOnDisplayPolicy";
static final String KEY_SCREEN_BRIGHTNESS_ARRAY = "screen_brightness_array";
static final String KEY_DIMMING_SCRIM_ARRAY = "dimming_scrim_array";
static final String KEY_PROX_SCREEN_OFF_DELAY_MS = "prox_screen_off_delay";
static final String KEY_PROX_COOLDOWN_TRIGGER_MS = "prox_cooldown_trigger";
static final String KEY_PROX_COOLDOWN_PERIOD_MS = "prox_cooldown_period";
/**
* Integer array to map ambient brightness type to real screen brightness.
*
* @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
* @see #KEY_SCREEN_BRIGHTNESS_ARRAY
*/
public final int[] screenBrightnessArray;
/**
* Integer array to map ambient brightness type to dimming scrim.
*
* @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
* @see #KEY_DIMMING_SCRIM_ARRAY
*/
public final int[] dimmingScrimArray;
/**
* Delay time(ms) from covering the prox to turning off the screen.
*
* @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
* @see #KEY_PROX_SCREEN_OFF_DELAY_MS
*/
public final long proxScreenOffDelayMs;
/**
* The threshold time(ms) to trigger the cooldown timer, which will
* turn off prox sensor for a period.
*
* @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
* @see #KEY_PROX_COOLDOWN_TRIGGER_MS
*/
public final long proxCooldownTriggerMs;
/**
* The period(ms) to turning off the prox sensor if
* {@link #KEY_PROX_COOLDOWN_TRIGGER_MS} is triggered.
*
* @see Settings.Global#ALWAYS_ON_DISPLAY_CONSTANTS
* @see #KEY_PROX_COOLDOWN_PERIOD_MS
*/
public final long proxCooldownPeriodMs;
private final KeyValueListParser mParser;
public AlwaysOnDisplayPolicy(Context context) {
final Resources resources = context.getResources();
mParser = new KeyValueListParser(',');
final String value = Settings.Global.getString(context.getContentResolver(),
Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS);
try {
mParser.setString(value);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Bad AOD constants");
}
proxScreenOffDelayMs = mParser.getLong(KEY_PROX_SCREEN_OFF_DELAY_MS,
10 * DateUtils.MINUTE_IN_MILLIS);
proxCooldownTriggerMs = mParser.getLong(KEY_PROX_COOLDOWN_TRIGGER_MS,
2 * DateUtils.MINUTE_IN_MILLIS);
proxCooldownPeriodMs = mParser.getLong(KEY_PROX_COOLDOWN_PERIOD_MS,
5 * DateUtils.MINUTE_IN_MILLIS);
screenBrightnessArray = parseIntArray(KEY_SCREEN_BRIGHTNESS_ARRAY,
resources.getIntArray(R.array.config_doze_brightness_sensor_to_brightness));
dimmingScrimArray = parseIntArray(KEY_DIMMING_SCRIM_ARRAY,
resources.getIntArray(R.array.config_doze_brightness_sensor_to_scrim_opacity));
}
private int[] parseIntArray(final String key, final int[] defaultArray) {
final String value = mParser.getString(key, null);
if (value != null) {
return Arrays.stream(value.split(":")).map(String::trim).mapToInt(
Integer::parseInt).toArray();
} else {
return defaultArray;
}
}
}

View File

@@ -59,7 +59,7 @@ public class DozeFactory {
DozeMachine machine = new DozeMachine(wrappedService, config, wakeLock);
machine.setParts(new DozeMachine.Part[]{
new DozePauser(handler, machine, alarmManager),
new DozePauser(handler, machine, alarmManager, new AlwaysOnDisplayPolicy(context)),
new DozeFalsingManagerAdapter(FalsingManager.getInstance(context)),
createDozeTriggers(context, sensorManager, host, alarmManager, config, params,
handler, wakeLock, machine),
@@ -76,7 +76,8 @@ public class DozeFactory {
Handler handler) {
Sensor sensor = DozeSensors.findSensorWithType(sensorManager,
context.getString(R.string.doze_brightness_sensor_type));
return new DozeScreenBrightness(context, service, sensorManager, sensor, host, handler);
return new DozeScreenBrightness(context, service, sensorManager, sensor, host, handler,
new AlwaysOnDisplayPolicy(context));
}
private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager,

View File

@@ -26,20 +26,22 @@ import com.android.systemui.util.AlarmTimeout;
*/
public class DozePauser implements DozeMachine.Part {
public static final String TAG = DozePauser.class.getSimpleName();
private static final long TIMEOUT = 10 * 1000;
private final AlarmTimeout mPauseTimeout;
private final DozeMachine mMachine;
private final long mTimeoutMs;
public DozePauser(Handler handler, DozeMachine machine, AlarmManager alarmManager) {
public DozePauser(Handler handler, DozeMachine machine, AlarmManager alarmManager,
AlwaysOnDisplayPolicy policy) {
mMachine = machine;
mPauseTimeout = new AlarmTimeout(alarmManager, this::onTimeout, TAG, handler);
mTimeoutMs = policy.proxScreenOffDelayMs;
}
@Override
public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
switch (newState) {
case DOZE_AOD_PAUSING:
mPauseTimeout.schedule(TIMEOUT, AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
mPauseTimeout.schedule(mTimeoutMs, AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
break;
default:
mPauseTimeout.cancel();

View File

@@ -42,7 +42,7 @@ public class DozeScreenBrightness implements DozeMachine.Part, SensorEventListen
public DozeScreenBrightness(Context context, DozeMachine.Service service,
SensorManager sensorManager, Sensor lightSensor, DozeHost host,
Handler handler) {
Handler handler, AlwaysOnDisplayPolicy policy) {
mContext = context;
mDozeService = service;
mSensorManager = sensorManager;
@@ -50,10 +50,8 @@ public class DozeScreenBrightness implements DozeMachine.Part, SensorEventListen
mDozeHost = host;
mHandler = handler;
mSensorToBrightness = context.getResources().getIntArray(
R.array.config_doze_brightness_sensor_to_brightness);
mSensorToScrimOpacity = context.getResources().getIntArray(
R.array.config_doze_brightness_sensor_to_scrim_opacity);
mSensorToBrightness = policy.screenBrightnessArray;
mSensorToScrimOpacity = policy.dimmingScrimArray;
}
@Override

View File

@@ -72,7 +72,7 @@ public class DozeSensors {
public DozeSensors(Context context, AlarmManager alarmManager, SensorManager sensorManager,
DozeParameters dozeParameters,
AmbientDisplayConfiguration config, WakeLock wakeLock, Callback callback,
Consumer<Boolean> proxCallback) {
Consumer<Boolean> proxCallback, AlwaysOnDisplayPolicy policy) {
mContext = context;
mAlarmManager = alarmManager;
mSensorManager = sensorManager;
@@ -112,7 +112,7 @@ public class DozeSensors {
true /* touchscreen */),
};
mProxSensor = new ProxSensor();
mProxSensor = new ProxSensor(policy);
mCallback = callback;
}
@@ -206,17 +206,16 @@ public class DozeSensors {
private class ProxSensor implements SensorEventListener {
static final long COOLDOWN_TRIGGER = 2 * 1000;
static final long COOLDOWN_PERIOD = 5 * 1000;
boolean mRequested;
boolean mRegistered;
Boolean mCurrentlyFar;
long mLastNear;
final AlarmTimeout mCooldownTimer;
final AlwaysOnDisplayPolicy mPolicy;
public ProxSensor() {
public ProxSensor(AlwaysOnDisplayPolicy policy) {
mPolicy = policy;
mCooldownTimer = new AlarmTimeout(mAlarmManager, this::updateRegistered,
"prox_cooldown", mHandler);
}
@@ -264,11 +263,12 @@ public class DozeSensors {
// Sensor has been unregistered by the proxCallback. Do nothing.
} else if (!mCurrentlyFar) {
mLastNear = now;
} else if (mCurrentlyFar && now - mLastNear < COOLDOWN_TRIGGER) {
} else if (mCurrentlyFar && now - mLastNear < mPolicy.proxCooldownTriggerMs) {
// If the last near was very recent, we might be using more power for prox
// wakeups than we're saving from turning of the screen. Instead, turn it off
// for a while.
mCooldownTimer.schedule(COOLDOWN_PERIOD, AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
mCooldownTimer.schedule(mPolicy.proxCooldownPeriodMs,
AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
updateRegistered();
}
}

View File

@@ -84,7 +84,8 @@ public class DozeTriggers implements DozeMachine.Part {
mWakeLock = wakeLock;
mAllowPulseTriggers = allowPulseTriggers;
mDozeSensors = new DozeSensors(context, alarmManager, mSensorManager, dozeParameters,
config, wakeLock, this::onSensor, this::onProximityFar);
config, wakeLock, this::onSensor, this::onProximityFar,
new AlwaysOnDisplayPolicy(context));
mUiModeManager = mContext.getSystemService(UiModeManager.class);
}

View File

@@ -54,7 +54,8 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
SystemUI-proto \
SystemUI-tags \
legacy-android-test \
testables
testables \
truth-prebuilt \
LOCAL_JAVA_LIBRARIES := android.test.runner telephony-common android.car

View File

@@ -0,0 +1,86 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.doze;
import static com.google.common.truth.Truth.assertThat;
import android.provider.Settings;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.text.format.DateUtils;
import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class AlwaysOnDisplayPolicyTest extends SysuiTestCase {
private static final String ALWAYS_ON_DISPLAY_CONSTANTS_VALUE = "prox_screen_off_delay=1000"
+ ",prox_cooldown_trigger=2000"
+ ",prox_cooldown_period=3000"
+ ",screen_brightness_array=1:2:3:4:5"
+ ",dimming_scrim_array=5:4:3:2:1";
private String mPreviousConfig;
@Before
public void setUp() {
mPreviousConfig = Settings.Global.getString(mContext.getContentResolver(),
Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS);
}
@After
public void tearDown() {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS, mPreviousConfig);
}
@Test
public void testPolicy_valueNull_containsDefaultValue() throws Exception {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS, null);
AlwaysOnDisplayPolicy policy = new AlwaysOnDisplayPolicy(mContext);
assertThat(policy.proxScreenOffDelayMs).isEqualTo(10 * DateUtils.MINUTE_IN_MILLIS);
assertThat(policy.proxCooldownTriggerMs).isEqualTo(2 * DateUtils.MINUTE_IN_MILLIS);
assertThat(policy.proxCooldownPeriodMs).isEqualTo(5 * DateUtils.MINUTE_IN_MILLIS);
assertThat(policy.screenBrightnessArray).isEqualTo(mContext.getResources().getIntArray(
R.array.config_doze_brightness_sensor_to_brightness));
assertThat(policy.dimmingScrimArray).isEqualTo(mContext.getResources().getIntArray(
R.array.config_doze_brightness_sensor_to_scrim_opacity));
}
@Test
public void testPolicy_valueNotNull_containsValue() throws Exception {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS, ALWAYS_ON_DISPLAY_CONSTANTS_VALUE);
AlwaysOnDisplayPolicy policy = new AlwaysOnDisplayPolicy(mContext);
assertThat(policy.proxScreenOffDelayMs).isEqualTo(1000);
assertThat(policy.proxCooldownTriggerMs).isEqualTo(2000);
assertThat(policy.proxCooldownPeriodMs).isEqualTo(3000);
assertThat(policy.screenBrightnessArray).isEqualTo(new int[]{1, 2, 3, 4, 5});
assertThat(policy.dimmingScrimArray).isEqualTo(new int[]{5, 4, 3, 2, 1});
}
}

View File

@@ -60,7 +60,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
mSensorManager = new FakeSensorManager(mContext);
mSensor = mSensorManager.getFakeLightSensor();
mScreen = new DozeScreenBrightness(mContext, mServiceFake, mSensorManager,
mSensor.getSensor(), mHostFake, null /* handler */);
mSensor.getSensor(), mHostFake, null /* handler */,
new AlwaysOnDisplayPolicy(mContext));
}
@Test
@@ -135,7 +136,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
@Test
public void testNullSensor() throws Exception {
mScreen = new DozeScreenBrightness(mContext, mServiceFake, mSensorManager,
null /* sensor */, mHostFake, null /* handler */);
null /* sensor */, mHostFake, null /* handler */,
new AlwaysOnDisplayPolicy(mContext));
mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
mScreen.transitionTo(INITIALIZED, DOZE_AOD);