1、涉及主要代码:
frameworks\base\core\java\android\os\PowerManager.java
frameworks\base\core\java\android\os\PowerManagerInternal.java
frameworks\base\services\core\java\com\android\server\power\PowerManagerService.java
frameworks\base\services\core\jni\com_android_server_power_PowerManagerService.cpp
frameworks\base\services\java\com\android\server\SystemServer.java
frameworks\base\services\core\java\com\android\server\SystemServiceManager.java
PowerManagerService的启动:
SystemServer.java
power服务是在systemserver启动后较早启动的一个服务,因其他服务对其有依赖
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
......
try {
// TODO: use boot phase
mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());
} catch (Throwable e) {
reportWtf("making Power Manager Service ready", e);
}
PowerManager分析
PowerManager是power服务对外提供的一个开放接口,从其中定义的方法、变量、常量,可知,对外提供的功能主要分三类:
wacklock、屏幕亮度亮灭屏、useractivity、关机重启、睡眠
wakelock相关常量:
cpu保持运行,屏幕和键盘允许退出,若用户按power键,则cpu保持运行,屏幕熄灭
public static final int PARTIAL_WAKE_LOCK = 0x00000001;
保持屏幕不熄灭,或者变暗,若按power,则cpu和屏幕一同退出运行,推荐应用使用的锁
public static final int SCREEN_DIM_WAKE_LOCK = 0x00000006;
保持屏幕高亮,按power则同cpu一起退出
public static final int SCREEN_BRIGHT_WAKE_LOCK = 0x0000000a;
屏幕和键盘保持高亮,按power同cpu一起退出
public static final int FULL_WAKE_LOCK = 0x0000001a;
距离传感器激活时候灭屏,离开时候亮屏,猜测应用于通话时候靠近息屏
public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 0x00000020;
若当前没有其他锁,则让屏幕处于低电状态,并且阻塞cpu,被dream manager用来进入doze模式。
public static final int DOZE_WAKE_LOCK = 0x00000040;
wakelock相关方法:
acquire 与 release需成对,有获取必有释放。
public WakeLock newWakeLock(int levelAndFlags, String tag) {
validateWakeLockParameters(levelAndFlags, tag);
return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
}
public final class WakeLock {
private int mFlags;
private String mTag;
private final String mPackageName;
private final IBinder mToken;
private int mInternalCount;
private int mExternalCount;
private boolean mRefCounted = true;
private boolean mHeld;
private WorkSource mWorkSource;
private String mHistoryTag;
private final String mTraceName;
private final Runnable mReleaser = new Runnable() {
public void run() {
release(RELEASE_FLAG_TIMEOUT);
}
};
WakeLock(int flags, String tag, String packageName) {
mFlags = flags;
mTag = tag;
mPackageName = packageName;
mToken = new Binder();
mTraceName = "WakeLock (" + mTag + ")";
}
......
public void acquire() {
synchronized (mToken) {
acquireLocked();
}
}
......
public void acquire(long timeout) {
synchronized (mToken) {
acquireLocked();
mHandler.postDelayed(mReleaser, timeout);
}
}
......
private void acquireLocked() {
mInternalCount++;
mExternalCount++;
if (!mRefCounted || mInternalCount == 1) {
mHandler.removeCallbacks(mReleaser);
Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
try {
mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
mHistoryTag);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
mHeld = true;
}
}
......
public void release() {
release(0);
}
public boolean isHeld() {
synchronized (mToken) {
return mHeld;
}
}
......
}
屏幕相关:
public static final int BRIGHTNESS_ON = 255;
public static final int BRIGHTNESS_OFF = 0;
public static final int BRIGHTNESS_DEFAULT = -1;
public int getMinimumScreenBrightnessSetting() {
return mContext.getResources().getInteger(
com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
}
/**
* Gets the maximum supported screen brightness setting.
* The screen may be allowed to become dimmer than this value but
* this is the maximum value that can be set by the user.
* @hide
*/
public int getMaximumScreenBrightnessSetting() {
return mContext.getResources().getInteger(
com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
}
/**
* Gets the default screen brightness setting.
* @hide
*/
public int getDefaultScreenBrightnessSetting() {
return mContext.getResources().getInteger(
com.android.internal.R.integer.config_screenBrightnessSettingDefault);
}
/**
* Gets the minimum supported screen brightness setting for VR Mode.
* @hide
*/
public int getMinimumScreenBrightnessForVrSetting() {
return mContext.getResources().getInteger(
com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum);
}
/**
* Gets the maximum supported screen brightness setting for VR Mode.
* The screen may be allowed to become dimmer than this value but
* this is the maximum value that can be set by the user.
* @hide
*/
public int getMaximumScreenBrightnessForVrSetting() {
return mContext.getResources().getInteger(
com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum);
}
/**
* Gets the default screen brightness for VR setting.
* @hide
*/
public int getDefaultScreenBrightnessForVrSetting() {
return mContext.getResources().getInteger(
com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault);
}
用户行为相关:
@SystemApi
public static final int USER_ACTIVITY_EVENT_OTHER = 0;
@SystemApi
public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
@SystemApi
public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
@SystemApi
public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3;
通知powermanager所发生额用户行为
public void userActivity(long when, boolean noChangeLights) {
userActivity(when, USER_ACTIVITY_EVENT_OTHER,
noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
}
关机重启相关:
public static final String REBOOT_RECOVERY = "recovery";
public static final String REBOOT_RECOVERY_UPDATE = "recovery-update";
public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner";
public static final String REBOOT_SAFE_MODE = "safemode";
public static final String REBOOT_QUIESCENT = "quiescent";
public static final String SHUTDOWN_USER_REQUESTED = "userrequested";
public void shutdown(boolean confirm, String reason, boolean wait) {
try {
mService.shutdown(confirm, reason, wait);
} catch (RemoteException e) {
throw e.re