1.背光服务框架
如下图是背光框架层图
2.UML时序图
这里主要标出的是各个服务或者框架层之间连接的api,中间会省略一些调用流程。
如下图,PowerManagerService会监听SettingProvider的亮度背光值screen_brightness变化,然后进行背光调节。
该图主要是一些服务之前的通信。
Jni层访问过程如下图:
jni通过hidl直通式访问light库,对hardware层light库会访问对应的sys节点从而进行背光驱动的访问,最终达到调节
背光的目的。
注意点:
(1)背光的调节并不是一步到位的,而是通过一个线程按照一定的速度来减少或者增加一个背光亮度值来调节的,例如一般背光的调控范围是0-255,假如目前背光150,用户想要调节到180的话,那么系统会开启一个线程按照一定的速度来每次增加背光亮度值1,连续增加30次来达到调节目的。这样调节的目的可能是为了让用户感受到舒适吧。
核心代码部分如下:
代码路径:base\services\core\java\com\android\server\display\RampAnimator.java
//创建了一个线程
private final Runnable mAnimationCallback = new Runnable() {
@Override // Choreographer callback
public void run() {
final long frameTimeNanos = mChoreographer.getFrameTimeNanos();
final float timeDelta = (frameTimeNanos - mLastFrameTimeNanos)
* 0.000000001f;
mLastFrameTimeNanos = frameTimeNanos;
// Advance the animated value towards the target at the specified rate
// and clamp to the target. This gives us the new current value but
// we keep the animated value around to allow for fractional increments
// towards the target.
final float scale = ValueAnimator.getDurationScale();
if (scale == 0) {
// Animation off.
mAnimatedValue = mTargetValue;
} else {
final float amount = timeDelta * mRate / scale;
if (mTargetValue > mCurrentValue) {
mAnimatedValue = Math.min(mAnimatedValue + amount, mTargetValue);
} else {
mAnimatedValue = Math.max(mAnimatedValue - amount, mTargetValue);
}
}
final int oldCurrentValue = mCurrentValue;
mCurrentValue = Math.round(mAnimatedValue);
if (oldCurrentValue != mCurrentValue) {
//在线程run中调节背光,mProperty是DisplayPowerState对像,
//base\services\core\java\com\android\server\display\DisplayPowerState.java
mProperty.setValue(mObject, mCurrentValue);
}
if (mTargetValue != mCurrentValue) {
postAnimationCallback();
} else {
mAnimating = false;
if (mListener != null) {
mListener.onAnimationEnd();
}
}
}
};
(2)如何获取从灯光服务中确定背光调用对象
LightsService是一个有关光调节的服务,其中包括了背光,按键灯,无线通信指示灯等的控制,DisplayService与LightsService通信主要是背光调节,那么DisplayService是如何通知或者获取lightservice中背光的调节对象呢?其实如下代码在LocalDisplayDevice对象构建的时候用LightsManager.LIGHT_ID_BACKLIGHT参数明确了获取的LightsService服务中的lights对象为背光调节类型。所以mBacklight.setBrightness(brightness);调用的是背光的灯光对象。
public abstract class LightsManager {
//背光
public static final int LIGHT_ID_BACKLIGHT = Type.BACKLIGHT;
//键盘灯
public static final int LIGHT_ID_KEYBOARD = Type.KEYBOARD;
//按键灯
public static final int LIGHT_ID_BUTTONS = Type.BUTTONS;
//电池相关灯,可能是充电吧
public static final int LIGHT_ID_BATTERY = Type.BATTERY;
//通知类型的灯光
public static final int LIGHT_ID_NOTIFICATIONS = Type.NOTIFICATIONS;
public static final int LIGHT_ID_ATTENTION = Type.ATTENTION;
//蓝牙模块的灯光
public static final int LIGHT_ID_BLUETOOTH = Type.BLUETOOTH;
//wifi模块的灯光
public static final int LIGHT_ID_WIFI = Type.WIFI;
public static final