Skip to content

Commit 53d003f

Browse files
Craig MautnerAndroid (Google) Code Review
authored andcommitted
Merge "Modify auto brightness to return to dim level." into jb-dev
2 parents 81de5a0 + 196943f commit 53d003f

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

services/java/com/android/server/PowerManagerService.java

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ public class PowerManagerService extends IPowerManager.Stub
178178
static final int ANIM_STEPS = 60; // nominal # of frames at 60Hz
179179
// Slower animation for autobrightness changes
180180
static final int AUTOBRIGHTNESS_ANIM_STEPS = 2 * ANIM_STEPS;
181+
// Even slower animation for autodimness changes
182+
static final int AUTODIMNESS_ANIM_STEPS = 15 * ANIM_STEPS;
181183
// Number of steps when performing a more immediate brightness change.
182184
static final int IMMEDIATE_ANIM_STEPS = 4;
183185

@@ -1745,7 +1747,6 @@ private int setScreenStateLocked(boolean on) {
17451747
+ Integer.toHexString(mPowerState)
17461748
+ " mSkippedScreenOn=" + mSkippedScreenOn);
17471749
}
1748-
mScreenBrightnessHandler.removeMessages(ScreenBrightnessAnimator.ANIMATE_LIGHTS);
17491750
mScreenBrightnessAnimator.animateTo(PowerManager.BRIGHTNESS_OFF, SCREEN_BRIGHT_BIT, 0);
17501751
}
17511752
}
@@ -2159,6 +2160,8 @@ class ScreenBrightnessAnimator extends HandlerThread {
21592160
static final int ANIMATE_POWER_OFF = 11;
21602161
volatile int startValue;
21612162
volatile int endValue;
2163+
volatile int startSensorValue;
2164+
volatile int endSensorValue;
21622165
volatile int currentValue;
21632166
private int currentMask;
21642167
private int duration;
@@ -2182,7 +2185,7 @@ public void handleMessage(Message msg) {
21822185
int value = msg.arg2;
21832186
long tStart = SystemClock.uptimeMillis();
21842187
if ((mask & SCREEN_BRIGHT_BIT) != 0) {
2185-
if (mDebugLightAnimation) Log.v(TAG, "Set brightness: " + value);
2188+
if (mDebugLightAnimation) Slog.v(TAG, "Set brightness: " + value);
21862189
mLcdLight.setBrightness(value, brightnessMode);
21872190
}
21882191
long elapsed = SystemClock.uptimeMillis() - tStart;
@@ -2194,12 +2197,12 @@ public void handleMessage(Message msg) {
21942197
}
21952198

21962199
if (elapsed > 100) {
2197-
Log.e(TAG, "Excessive delay setting brightness: " + elapsed
2200+
Slog.e(TAG, "Excessive delay setting brightness: " + elapsed
21982201
+ "ms, mask=" + mask);
21992202
}
22002203

22012204
// Throttle brightness updates to frame refresh rate
2202-
int delay = elapsed < NOMINAL_FRAME_TIME_MS ? NOMINAL_FRAME_TIME_MS : 0;
2205+
int delay = elapsed < NOMINAL_FRAME_TIME_MS ? NOMINAL_FRAME_TIME_MS : 1;
22032206
synchronized(this) {
22042207
currentValue = value;
22052208
}
@@ -2227,25 +2230,41 @@ private void animateInternal(int mask, boolean turningOff, int delay) {
22272230
newValue = startValue + delta * elapsed / duration;
22282231
newValue = Math.max(PowerManager.BRIGHTNESS_OFF, newValue);
22292232
newValue = Math.min(PowerManager.BRIGHTNESS_ON, newValue);
2233+
// Optimization to delay next step until a change will occur.
2234+
if (delay > 0 && newValue == currentValue) {
2235+
final int timePerStep = duration / Math.abs(delta);
2236+
delay = Math.min(duration - elapsed, timePerStep);
2237+
newValue += delta < 0 ? -1 : 1;
2238+
}
2239+
// adjust the peak sensor value until we get to the target sensor value
2240+
delta = endSensorValue - startSensorValue;
2241+
mHighestLightSensorValue = startSensorValue + delta * elapsed / duration;
22302242
} else {
22312243
newValue = endValue;
2244+
mHighestLightSensorValue = endSensorValue;
22322245
mInitialAnimation = false;
22332246
}
22342247

22352248
if (mDebugLightAnimation) {
2236-
Log.v(TAG, "Animating light: " + "start:" + startValue
2249+
Slog.v(TAG, "Animating light: " + "start:" + startValue
22372250
+ ", end:" + endValue + ", elapsed:" + elapsed
22382251
+ ", duration:" + duration + ", current:" + currentValue
2239-
+ ", delay:" + delay);
2252+
+ ", newValue:" + newValue
2253+
+ ", delay:" + delay
2254+
+ ", highestSensor:" + mHighestLightSensorValue);
22402255
}
22412256

22422257
if (turningOff && !mHeadless && !mAnimateScreenLights) {
22432258
int mode = mScreenOffReason == OFF_BECAUSE_OF_PROX_SENSOR
22442259
? 0 : mAnimationSetting;
2245-
if (mDebugLightAnimation) Log.v(TAG, "Doing power-off anim, mode=" + mode);
2260+
if (mDebugLightAnimation) {
2261+
Slog.v(TAG, "Doing power-off anim, mode=" + mode);
2262+
}
22462263
mScreenBrightnessHandler.obtainMessage(ANIMATE_POWER_OFF, mode, 0)
22472264
.sendToTarget();
22482265
}
2266+
mScreenBrightnessHandler.removeMessages(
2267+
ScreenBrightnessAnimator.ANIMATE_LIGHTS);
22492268
Message msg = mScreenBrightnessHandler
22502269
.obtainMessage(ANIMATE_LIGHTS, mask, newValue);
22512270
mScreenBrightnessHandler.sendMessageDelayed(msg, delay);
@@ -2259,16 +2278,24 @@ public void dump(PrintWriter pw, String string) {
22592278
}
22602279

22612280
public void animateTo(int target, int mask, int animationDuration) {
2281+
animateTo(target, mHighestLightSensorValue, mask, animationDuration);
2282+
}
2283+
2284+
public void animateTo(int target, int sensorTarget, int mask, int animationDuration) {
22622285
synchronized(this) {
22632286
startValue = currentValue;
22642287
endValue = target;
2288+
startSensorValue = mHighestLightSensorValue;
2289+
endSensorValue = sensorTarget;
22652290
currentMask = mask;
22662291
duration = (int) (mWindowScaleAnimation * animationDuration);
22672292
startTimeMillis = SystemClock.elapsedRealtime();
22682293
mInitialAnimation = currentValue == 0 && target > 0;
22692294

22702295
if (mDebugLightAnimation) {
2271-
Log.v(TAG, "animateTo(target=" + target + ", mask=" + mask
2296+
Slog.v(TAG, "animateTo(target=" + target
2297+
+ ", sensor=" + sensorTarget
2298+
+ ", mask=" + mask
22722299
+ ", duration=" + animationDuration +")"
22732300
+ ", currentValue=" + currentValue
22742301
+ ", startTime=" + startTimeMillis);
@@ -2612,9 +2639,11 @@ private void lightSensorChangedLocked(int value, boolean immediate) {
26122639
return;
26132640
}
26142641

2615-
// do not allow light sensor value to decrease
2616-
if (mHighestLightSensorValue < value) {
2617-
mHighestLightSensorValue = value;
2642+
final int stepsToTargetLevel;
2643+
if (mHighestLightSensorValue <= value) {
2644+
stepsToTargetLevel = AUTOBRIGHTNESS_ANIM_STEPS;
2645+
} else {
2646+
stepsToTargetLevel = AUTODIMNESS_ANIM_STEPS;
26182647
}
26192648

26202649
if (mLightSensorValue != value) {
@@ -2623,9 +2652,7 @@ private void lightSensorChangedLocked(int value, boolean immediate) {
26232652
// use maximum light sensor value seen since screen went on for LCD to avoid flicker
26242653
// we only do this if we are undocked, since lighting should be stable when
26252654
// stationary in a dock.
2626-
int lcdValue = getAutoBrightnessValue(
2627-
(mIsDocked ? value : mHighestLightSensorValue),
2628-
mLcdBacklightValues);
2655+
int lcdValue = getAutoBrightnessValue(value, mLcdBacklightValues);
26292656
int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
26302657
int keyboardValue;
26312658
if (mKeyboardVisible) {
@@ -2645,9 +2672,8 @@ private void lightSensorChangedLocked(int value, boolean immediate) {
26452672

26462673
if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {
26472674
if (!mSkippedScreenOn && !mInitialAnimation) {
2648-
int steps = immediate ? IMMEDIATE_ANIM_STEPS : AUTOBRIGHTNESS_ANIM_STEPS;
2649-
mScreenBrightnessAnimator.cancelAnimation();
2650-
mScreenBrightnessAnimator.animateTo(lcdValue,
2675+
int steps = immediate ? IMMEDIATE_ANIM_STEPS : stepsToTargetLevel;
2676+
mScreenBrightnessAnimator.animateTo(lcdValue, value,
26512677
SCREEN_BRIGHT_BIT, steps * NOMINAL_FRAME_TIME_MS);
26522678
}
26532679
}

0 commit comments

Comments
 (0)