PowerManagerService是android系统电源管理的核心服务。
PowerManagerService在Framework层本质为策略控制方案,其作用为:
frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
负责管理、协调设备电源管理的系统服务之一,设备中常见功能如亮灭屏、亮度调节、低电量模式、保持CPU唤醒都是通过PMS进行协调和处理。
PowerManagerService 继承SystemSerive即具备生命周期, SystemService系的服务都是在SystemServer中启动、注册到系统服务中,通过Binder和其他组件进行交互
故我们先看下SystemServer的启动流程。
SystemServer.main
->SystemServer.run
->->SystemServer.startBootstrapServices() 启动引导服务
->->->mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class); // 通过Java的newInstance()反射启动电源管理服务,好处是代码很解耦
PowerManagerService(Context context, Injector injector) {
...
mHandlerThread = new ServiceThread(TAG,Process.THREAD_PRIORITY_DISPLAY, false /*allowIo*/);
mHandlerThread.start();
mHandler = injector.createHandler(mHandlerThread.getLooper(),new PowerManagerHandlerCallback());
...
// 控制CPU的唤醒
mWakeLockSuspendBlocker =mInjector.createSuspendBlocker(this, "PowerManagerService.WakeLocks");
// 控制屏幕的点亮和息屏
mDisplaySuspendBlocker =mInjector.createSuspendBlocker(this, "PowerManagerService.Display");
...
}
看到上述的2个锁,让我联系到经常使用的如下Flag
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On Off Off 0x00000001 1
SCREEN_DIM_WAKE_LOCK On Dim Off 0x00000006 6
SCREEN_BRIGHT_WAKE_LOCK On Bright Off 0x0000000a 10
FULL_WAKE_LOCK On Bright Bright 0x0000001a 26
Onstart的作用:
@Override
public void onStart() {
// 其他模块就可以通过Binder获取实例
publishBinderService(Context.POWER_SERVICE, mBinderService, /* allowIsolated= */ false,DUMP_FLAG_PRIORITY_DEFAULT | DUMP_FLAG_PRIORITY_CRITICAL);
// 当进行本地注册后,只有在Sysem进程才能获得到其实例
publishLocalService(PowerManagerInternal.class, mLocalService);
Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);
}
systemReady()函数作用:初始化工作,即我们可以看到电源管理策略的需要用的功能初始化。
例如我们在做冻结策略、场景识别策略的ROM定制也大体都是在 systemReady 进行init
public void systemReady(IAppOpsService appOps) {
...
mDreamManager = getLocalService(DreamManagerInternal.class); // 1. 初始化互动屏保
mDisplayManagerInternal = getLocalService(DisplayManagerInternal.class);// 2. 初始化屏幕显示管理服务
mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class);// 3. 初始化电池管理服务
SensorManager sensorManager = new SystemSensorManager(mContext, mHandler.getLooper());//5. 获取传感器管理服务
mBatteryStats = BatteryStatsService.getService();//6. 初始化电量统计服务
mLightsManager = getLocalService(LightsManager.class);//8. LED指示灯管理服务
mAttentionLight = mLightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION);//8. LED指示灯管理服务
mDisplayManagerInternal.initPowerManagement(mDisplayPowerCallbacks, mHandler, sensorManager);// 9. 初始化屏幕显示服务
// 整个 PMS 中最重要的方法
updatePowerStateLocked();// 11. 更新电源相关信息
...
}
总结一下:SystemReady完成的工作主要如下:
PowerMananger 是PowerManangerService的代理类,PowerMananger向上层应用提供交互的接口。上层调用对应的接口后,具体的工作内容交予PowerManangerService完成。可以重点关注如下接口:
package com.sufadi.commlib.utils
import android.annotation.SuppressLint
import android.content.Context
import android.os.PowerManager
/*
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On Off Off 0x00000001 1
SCREEN_DIM_WAKE_LOCK On Dim Off 0x00000006 6
SCREEN_BRIGHT_WAKE_LOCK On Bright Off 0x0000000a 10
FULL_WAKE_LOCK On Bright Bright 0x0000001a 26
*/
object AlertWakeLock {
private val TAG = "AlertWakeLock"
private var sCpuWakeLock: PowerManager.WakeLock? = null
@SuppressLint("InvalidWakeLockTag")
internal fun createPartialWakeLock(context: Context): PowerManager.WakeLock? {
// 第一步:获取PowerManager的实例
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager ?: return null
// 第二步:调用PowerManager中的newWakeLock方法创建一个WakeLock对象
return pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG)
//return pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, TAG);
}
fun acquireCpuWakeLock(context: Context) {
if (sCpuWakeLock != null) {
return
}
sCpuWakeLock = createPartialWakeLock(context)
// 第三步:acquire()获取相应的锁
sCpuWakeLock!!.acquire()
}
fun releaseCpuLock() {
if (sCpuWakeLock != null) {
// 最后:release释放
sCpuWakeLock!!.release()
sCpuWakeLock = null
}
}
}