上图是关于亮度调节的时序图
BrightnessController
private final DisplayManager mDisplayManager;
private void setBrightness(int brightness) {
mDisplayManager.setTemporaryBrightness(brightness);
}
DisplayManager
private final DisplayManagerGlobal mGlobal;
public void setTemporaryBrightness(int brightness) {
mGlobal.setTemporaryBrightness(brightness);
}
DispalyManagerGlobal
private final IDisplayManager mDm;
public void setTemporaryBrightness(int brightness) {
try {
mDm.setTemporaryBrightness(brightness);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
这里的IDisplayManager是一个aidl,具体的调用直接看DisplayManagerService
DisplayManagerService
它里面的BinderService继承了IDisplayManager,具体实现如下
@Override // Binder call
public void setTemporaryBrightness(int brightness) {
mContext.enforceCallingOrSelfPermission(
Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
"Permission required to set the display's brightness");
final long token = Binder.clearCallingIdentity();
try {
synchronized (mSyncRoot) {
mDisplayPowerController.setTemporaryBrightness(brightness);//在这里调用了设置亮度的方法
}
} finally {
Binder.restoreCallingIdentity(token);
}
}
DisplayPowerController
public void setTemporaryBrightness(int brightness) {
Message msg = mHandler.obtainMessage(MSG_SET_TEMPORARY_BRIGHTNESS,
brightness, 0 /*unused*/);
msg.sendToTarget();
}
看一下handMessage,把亮度值赋给了mTemporaryScreenBrightness 然后执行updatePowerState()方法
case MSG_SET_TEMPORARY_BRIGHTNESS:
// TODO: Should we have a a timeout for the temporary brightness?
mTemporaryScreenBrightness = msg.arg1;
updatePowerState();
break;
updatePowerState()中对于亮度的设置在这里
//在屏幕打开或打zing睡时对屏幕亮度进行动画处理。
//当屏幕关闭,暂停或切换到VR或从VR切换时,跳过动画。
if (!mPendingScreenOff) {
if (mSkipScreenOnBrightnessRamp) {
if (state == Display.STATE_ON) {
if (mSkipRampState == RAMP_STATE_SKIP_NONE && mDozing) {
mInitialAutoBrightness = brightness;
mSkipRampState = RAMP_STATE_SKIP_INITIAL;
} else if (mSkipRampState == RAMP_STATE_SKIP_INITIAL
&& mUseSoftwareAutoBrightnessConfig
&& brightness != mInitialAutoBrightness) {
mSkipRampState = RAMP_STATE_SKIP_AUTOBRIGHT;
} else if (mSkipRampState == RAMP_STATE_SKIP_AUTOBRIGHT) {
mSkipRampState = RAMP_STATE_SKIP_NONE;
}
} else {
mSkipRampState = RAMP_STATE_SKIP_NONE;
}
}
final boolean wasOrWillBeInVr =
(state == Display.STATE_VR || oldState == Display.STATE_VR);
final boolean initialRampSkip =
state == Display.STATE_ON && mSkipRampState != RAMP_STATE_SKIP_NONE;
// While dozing, sometimes the brightness is split into buckets. Rather than animating
// through the buckets, which is unlikely to be smooth in the first place, just jump
// right to the suggested brightness.
final boolean hasBrightnessBuckets =
Display.isDozeState(state) && mBrightnessBucketsInDozeConfig;
// If the color fade is totally covering the screen then we can change the backlight
// level without it being a noticeable jump since any actual content isn't yet visible.
final boolean isDisplayContentVisible =
mColorFadeEnabled && mPowerState.getColorFadeLevel() == 1.0f;
final boolean brightnessIsTemporary =
mAppliedTemporaryBrightness || mAppliedTemporaryAutoBrightnessAdjustment;
if (initialRampSkip || hasBrightnessBuckets
|| wasOrWillBeInVr || !isDisplayContentVisible || brightnessIsTemporary) {
animateScreenBrightness(brightness, 0);//动画处理
} else {
animateScreenBrightness(brightness,
slowChange ? mBrightnessRampRateSlow : mBrightnessRampRateFast);//动画处理
}
if (!brightnessIsTemporary) {
notifyBrightnessChanged(brightness, userInitiatedChange, hadUserBrightnessPoint);
}
}
然后是animateScreenBrightness(int target, int rate)方法
private void animateScreenBrightness(int target, int rate) {
if (DEBUG) {
Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate);
}
if (mScreenBrightnessRampAnimator.animateTo(target, rate)) {
//这里调用了动画调节
Trace.traceCounter(Trace.TRACE_TAG_POWER, "TargetScreenBrightness", target);
try {
mBatteryStats.noteScreenBrightness(target);
} catch (RemoteException ex) {
// same process
}
}
}
animateTo(target, rate)方法由mScreenBrightnessRampAnimator调用,mScreenBrightnessRampAnimator是RampAnimator类型的,这里对它的初始化在DisplayPowerController的initialize()里
mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>(
mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS);
mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener);
RampAnimator
看一下RampAnimator的animateTo方法
public RampAnimator(T object, IntProperty<T> property) {
this.mObject = object;
this.mProperty = property;
this.mChoreographer = Choreographer.getInstance();
}
public boolean animateTo(int target, int rate) {
if (!this.mFirstTime && rate > 0) {
if (!this.mAnimating || rate > this.mRate || target <= this.mCurrentValue && this.mCurrentValue <= this.mTargetValue || this.mTargetValue <= this.mCurrentValue && this.mCurrentValue <= target) {
this.mRate = rate;
}
boolean changed = this