资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
android6.0 PowerManagerService Notifier 分析这个类在 PowerManagerService 中是非常重要的,当 PowerManagerService 的状态改变时,都是靠这个类通知。比如常用的灭屏和亮屏广播,也是在这个类中发送的。一、Notifier 初始化首先我们来看它在 PowerManagerService 构造函数中的初始化:cpp view plain copy 在 CODE 上查看代码片派生到我的代码片mNotifier = new Notifier(Looper.getMainLooper(), mContext, mBatteryStats, mAppOps, createSuspendBlockerLocked(PowerManagerService.Broadcasts), mPolicy); PowerManagerService 把自己的 main looper 传进去了,说明在 Notifier 的消息处理是在PowerManagerService 的主线程中,还有一点和之前的 WakeLocks,和 Display 锁一样,这里也为 Notifier 创建了一个 Broadcasts 锁。二、 持锁我们现在开始看各 PowerManagerService 个状态是如何在 Notifier 里处理的。先来看 acquireWakeLockInternal 函数cpp view plain copy 在 CODE 上查看代码片派生到我的代码片private void acquireWakeLockInternal(IBinder lock, int flags, String tag, String packageName, WorkSource ws, String historyTag, int uid, int pid) synchronized (mLock) WakeLock wakeLock; int index = findWakeLockIndexLocked(lock); boolean notifyAcquire; if (index = 0) wakeLock = mWakeLocks.get(index); if (!wakeLock.hasSameProperties(flags, tag, ws, uid, pid) / Update existing wake lock. This shouldnt happen but is harmless. notifyWakeLockChangingLocked(wakeLock, flags, tag, packageName, uid, pid, ws, historyTag); wakeLock.updateProperties(flags, tag, packageName, ws, historyTag, uid, pid); notifyAcquire = false; else wakeLock = new WakeLock(lock, flags, tag, packageName, ws, historyTag, uid, pid); try lock.linkToDeath(wakeLock, 0); catch (RemoteException ex) throw new IllegalArgumentException(Wake lock is already dead.); mWakeLocks.add(wakeLock); setWakeLockDisabledStateLocked(wakeLock); notifyAcquire = true;/只有在新建的时候通知 Notifier applyWakeLockFlagsOnAcquireLocked(wakeLock, uid); mDirty |= DIRTY_WAKE_LOCKS; updatePowerStateLocked(); if (notifyAcquire) / This needs to be done last so we are sure we have acquired the / kernel wake lock. Otherwise we have a race where the system may / go to sleep between the time we start the accounting in battery / stats and when we actually get around to telling the kernel to / stay awake. notifyWakeLockAcquiredLocked(wakeLock); 我们看上面函数只有在 WakeLock 是新建的时候才会调用 notifyWakeLockAcquiredLocked函数:cpp view plain copy 在 CODE 上查看代码片派生到我的代码片private void notifyWakeLockAcquiredLocked(WakeLock wakeLock) if (mSystemReady & !wakeLock.mDisabled) wakeLock.mNotifiedAcquired = true; mNotifier.onWakeLockAcquired(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource, wakeLock.mHistoryTag); Notifier 也是统计电量和 appops 处理下。cpp view plain copy 在 CODE 上查看代码片派生到我的代码片public void onWakeLockAcquired(int flags, String tag, String packageName, int ownerUid, int ownerPid, WorkSource workSource, String historyTag) final int monitorType = getBatteryStatsWakeLockMonitorType(flags); if (monitorType = 0) try final boolean unimportantForLogging = ownerUid = Process.SYSTEM_UID if (workSource != null) mBatteryStats.noteStartWakelockFromSource(workSource, ownerPid, tag, historyTag, monitorType, unimportantForLogging); else mBatteryStats.noteStartWakelock(ownerUid, ownerPid, tag, historyTag, monitorType, unimportantForLogging); / XXX need to deal with disabled operations. mAppOps.startOperation(AppOpsManager.getToken(mAppOps), AppOpsManager.OP_WAKE_LOCK, ownerUid, packageName); catch (RemoteException ex) / Ignore PowerManagerService 在删除锁的时候会调用 mNotifier.onWakeLockReleased 函数,更新锁的时候最后会调用 mNotifier.onWakeLockChanging 函数。这里面都是通知电池以及 appops相关。三、唤醒在 PowerManagerService 的 wakeUpNoUpdateLocked 函数会调用如下函数:cpp view plain copy 在 CODE 上查看代码片派生到我的代码片mNotifier.onWakeUp(reason, reasonUid, opPackageName, opUid); 再看下 Notifier 处理:cpp view plain copy 在 CODE 上查看代码片派生到我的代码片public void onWakeUp(String reason, int reasonUid, String opPackageName, int opUid) try mBatteryStats.noteWakeUp(reason, reasonUid); if (opPackageName != null) mAppOps.noteOperation(AppOpsManager.OP_TURN_SCREEN_ON, opUid, opPackageName); catch (RemoteException ex) / Ignore 四、userActivity在 userActivityNoUpdateLocked 函数中也会调用如下函数cpp view plain copy 在 CODE 上查看代码片派生到我的代码片mNotifier.onUserActivity(event, uid); 我们看下 Notifier 的处理cpp view plain copy 在 CODE 上查看代码片派生到我的代码片public void onUserActivity(int event, int uid) try mBatteryStats.noteUserActivity(uid, event); catch (RemoteException ex) / Ignore synchronized (mLock) if (!mUserActivityPending) mUserActivityPending = true; Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY); msg.setAsynchronous(true); mHandler.sendMessage(msg); 消息处理函数为:sendUserActivitycpp view plain copy 在 CODE 上查看代码片派生到我的代码片private void sendUserActivity() synchronized (mLock) if (!mUserActivityPending) return; mUserActivityPending = false; mPolicy.userActivity(); 最后是在 PhoneWindowManager 中处理好像是在 keyguard 中处理。五、更新屏幕最亮化我们再来看 updateScreenBrightnessBoostL
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号