android6.0 power显示(亮度等)深入分
析(二)DisplayManagerService
一、DisplayManagerService注册localDisplay的适配层 我们先来看构造函数:
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 public DisplayManagerService(Context context) { super(context);
mContext = context;
mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());//消息处理 mUiHandler = UiThread.getHandler();
mDisplayAdapterListener = new DisplayAdapterListener();//display适配层监视器
mSingleDisplayDemoMode = SystemProperties.getBoolean(\false);
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();//成员变量屏幕亮度 }
我们再来看onStart函数,publish了一个BinderService和LocalService,还有发送了一个消息。
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 @Override
public void onStart() {
mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);
publishBinderService(Context.DISPLAY_SERVICE, new BinderService(), true /*allowIsolated*/);
publishLocalService(DisplayManagerInternal.class, new LocalService()); }
我们看消息处理,就是调用了registerDefaultDisplayAdapter函数: [cpp] view plain copy 在CODE上查看代码片派生到我的代码片 @Override
public void handleMessage(Message msg) { switch (msg.what) {
case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER: registerDefaultDisplayAdapter(); break;
registerDefaultDisplayAdapter函数
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 private void registerDefaultDisplayAdapter() { // Register default display adapter. synchronized (mSyncRoot) {
registerDisplayAdapterLocked(new LocalDisplayAdapter(
mSyncRoot, mContext, mHandler, mDisplayAdapterListener)); } }
再来看看registerDisplayAdapterLocked
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 private void registerDisplayAdapterLocked(DisplayAdapter adapter) { mDisplayAdapters.add(adapter); adapter.registerLocked(); }
这里就是register了DefaultDisplay的适配层,就是和背光相关的。在新建LocalDisplayAdapter的时候我们把mDisplayAdapterListener传过去了。
二、LocalDisplayAdapter & LocalDisplayDevice
LocalDisplayAdapter构造函数调用了父类的,而父类也就是保存了变量 [cpp] view plain copy 在CODE上查看代码片派生到我的代码片
public LocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, Context context, Handler handler, Listener listener) { super(syncRoot, context, handler, listener, TAG); }
上面又紧跟着调用了registerLocked函数
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 public void registerLocked() { super.registerLocked();
mHotplugReceiver = new HotplugDisplayEventReceiver(getHandler().getLooper());
for (int builtInDisplayId : BUILT_IN_DISPLAY_IDS_TO_SCAN) { tryConnectDisplayLocked(builtInDisplayId); } }
tryConnectDisplayLocked函数,先是看传入的builtInDisplayId是否支持,一个是main,一个是hdmi的。
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 private void tryConnectDisplayLocked(int builtInDisplayId) {
IBinder displayToken = SurfaceControl.getBuiltInDisplay(builtInDisplayId); if (displayToken != null) {
SurfaceControl.PhysicalDisplayInfo[] configs =
SurfaceControl.getDisplayConfigs(displayToken); if (configs == null) {
// There are no valid configs for this device, so we can't use it Slog.w(TAG, \ builtInDisplayId); return;
}
int activeConfig = SurfaceControl.getActiveConfig(displayToken); if (activeConfig < 0) {
// There is no active config, and for now we don't have the // policy to set one.
Slog.w(TAG, \ builtInDisplayId); return; }
LocalDisplayDevice device = mDevices.get(builtInDisplayId); if (device == null) {
// Display was added.
device = new LocalDisplayDevice(displayToken, builtInDisplayId, configs, activeConfig);
mDevices.put(builtInDisplayId, device);
sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED); } else if (device.updatePhysicalDisplayInfoLocked(configs, activeConfig)) { // Display properties changed.
sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED); } } else {
// The display is no longer available. Ignore the attempt to add it. // If it was connected but has already been disconnected, we'll get a // disconnect event that will remove it from mDevices. } }
然后再去查找这个LocalDisplayDevice,如果是找到了需要更新下configs,没找到需要新建一个LocalDisplayDevice。最后都调用了sendDisplayDeviceEventLocked函数。 我们再来看LocalDisplayDevice,如果传入的是BUILT_IN_DISPLAY_ID_MAIN就是背光的,我们获取背光的Light,保存在mBackLight变量。
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
public LocalDisplayDevice(IBinder displayToken, int builtInDisplayId,
SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo) { super(LocalDisplayAdapter.this, displayToken, UNIQUE_ID_PREFIX + builtInDisplayId); mBuiltInDisplayId = builtInDisplayId;
updatePhysicalDisplayInfoLocked(physicalDisplayInfos, activeDisplayInfo); if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) { LightsManager lights = LocalServices.getService(LightsManager.class); mBacklight = lights.getLight(LightsManager.LIGHT_ID_BACKLIGHT); } else {
mBacklight = null; } }
然后上面函数调用了sendDisplayDeviceEventLocked函数,就是调用了传入的参数DisplayAdapterListener
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 protected final void sendDisplayDeviceEventLocked( final DisplayDevice device, final int event) { mHandler.post(new Runnable() { @Override
public void run() {
mListener.onDisplayDeviceEvent(device, event); } }); }
如果是新建就调用了handleDisplayDeviceAdded函数,
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
private final class DisplayAdapterListener implements DisplayAdapter.Listener { @Override
public void onDisplayDeviceEvent(DisplayDevice device, int event) { switch (event) {
case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED: handleDisplayDeviceAdded(device); break;
case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED: handleDisplayDeviceChanged(device); break;
case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED: handleDisplayDeviceRemoved(device); break; } }
@Override
public void onTraversalRequested() { synchronized (mSyncRoot) {
scheduleTraversalLocked(false); } } }
我们先来看看handleDisplayDeviceAdded,最后将device保存在了mDisplayDevices中。 [cpp] view plain copy 在CODE上查看代码片派生到我的代码片 private void handleDisplayDeviceAdded(DisplayDevice device) { synchronized (mSyncRoot) {
handleDisplayDeviceAddedLocked(device);
} }
private void handleDisplayDeviceAddedLocked(DisplayDevice device) { DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked(); if (mDisplayDevices.contains(device)) {
Slog.w(TAG, \ return; }
Slog.i(TAG, \ device.mDebugLastLoggedDeviceInfo = info;
mDisplayDevices.add(device); addLogicalDisplayLocked(device);
Runnable work = updateDisplayStateLocked(device); if (work != null) { work.run(); }
scheduleTraversalLocked(false); }
三、设置背光
现在我们在上篇博客不是说背光的调制最后是在DisplayManagerService中,是在下面函数的requestGlobalDisplayStateInternal中调用的
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler, SensorManager sensorManager) { synchronized (mSyncRoot) {
DisplayBlanker blanker = new DisplayBlanker() { @Override
public void requestDisplayState(int state, int brightness) { // The order of operations is important for legacy reasons. if (state == Display.STATE_OFF) {
requestGlobalDisplayStateInternal(state, brightness); }
callbacks.onDisplayStateChange(state);
if (state != Display.STATE_OFF) {
requestGlobalDisplayStateInternal(state, brightness); } } };
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库android6.0 power显示(亮度等)深入分析(二)DisplayManagerServic在线全文阅读。
相关推荐: