IDisplayManager 只是接口,需要找下哪里实现了它,搜索是在 BinderService,BinderService 是 DisplayManagerService 内部类。
final class BinderService extends IDisplayManager.Stub {
@Override // Binder call
public void setTemporaryBrightness(float 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);
}
}
}
mDisplayPowerController.setTemporaryBrightness(brightness)
后面经过一系列调用会到 LightsService#setLight_native,通过 JNI 调用到 native 层,调用底层进行背光调节,关于背光调节后面文章再细讲。
SystemServer
DisplayManagerService 是继承了 SystemService,DisplayManagerService 是怎么注册为系统服务的呢?在 SystemServer 里面:
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin(“StartDisplayManager”);
//开启DisplayManagerService
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
t.traceEnd();
}
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
//通知服务系统启动完成
t.traceBegin(“MakeDisplayManagerServiceReady”);
try {
// TODO: us
e boot phase and communicate these flags some other way
mDisplayManagerService.systemReady(safeMode, mOnlyCore);
} catch (Throwable e) {
reportWtf(“making Display Manager Service ready”, e);
}
t.traceEnd();
}
看完 DisplayManagerService 是怎么写的,不妨模仿写个。 所谓看着代码,感觉还是挺简单的,实际操作起来,各种编译报错……