该系列文章总纲链接:专题分纲目录 Android SystemUI组件
本章关键点总结 & 说明:
说明:本章节持续迭代之前章节的思维导图,主要关注左侧上方锁屏分析部分 睡眠灭屏 即可。
Power按键的处理逻辑最终是由PhoneWindowManager来完成。想了解更多可参考输入子系统的相关文章。整理如下:
Android Framework 输入子系统(01)核心机制 inotify和epoll
Android Framework 输入子系统(02)核心机制 双向通信(socketpair+binder)
Android Framework 输入子系统(03)输入系统框架
Android Framework 输入子系统(04)InputReader解读
Android Framework 输入子系统(05)InputDispatcher解读
Android Framework 输入子系统(06)Global Key 一键启动 应用程序案例
Android Framework 输入子系统(07)APP建立联系
Android Framework 输入子系统(08)View基础(activity window decor view)
Android Framework 输入子系统(09)InputStage解读
Android Framework 输入子系统(10)Input命令解读
Android Framework 输入子系统(11)sendevent与getevent命令解读
本章我们只关注与Power按键相关的内容,InputManagerService处理的按键事件,最终会传递到PhoneWindowManager的interceptKeyBeforeQueueing方法中处理。我们就从这里入手逐步分析。
1 从PhoneWindowManager到PowerManagerService的处理
public class PhoneWindowManager implements WindowManagerPolicy {
static final String TAG = "WindowManager";
static final boolean DEBUG = false;
//...
//关键流程step1
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
//...
//表示屏幕是否点亮
final boolean interactive = (policyFlags & FLAG_INTERACTIVE) != 0;
final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
final boolean canceled = event.isCanceled();
//获取按键编码
final int keyCode = event.getKeyCode();
final boolean keyguardActive = (mKeyguardDelegate == null ? false :
(interactive ?
isKeyguardShowingAndNotOccluded() :
mKeyguardDelegate.isShowing()));
boolean isWakeKey = (policyFlags & WindowManagerPolicy.FLAG_WAKE) != 0
|| event.isWakeKey();
//...
// Handle special keys.
switch (keyCode) {
//...
case KeyEvent.KEYCODE_POWER: {
result &= ~ACTION_PASS_TO_USER;
isWakeKey = false; // wake-up will be handled separately
if (down) {
//亮屏 step1 按下power按键,亮屏流程
interceptPowerKeyDown(event, interactive);
} else {
//抬起power按键
interceptPowerKeyUp(event, interactive, canceled);
}
break;
}
case KeyEvent.KEYCODE_SLEEP: {
result &= ~ACTION_PASS_TO_USER;
if (!mPowerManager.isInteractive()) {
useHapticFeedback = false; // suppress feedback if already non-interactive
}
mPowerManager.goToSleep(event.getEventTime(),
PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON, 0);
isWakeKey = false;
break;
}
case KeyEvent.KEYCODE_WAKEUP: {
result &= ~ACTION_PASS_TO_USER;
isWakeKey = true;
break;
}
//...
}
//...
if (isWakeKey) {
wakeUp(event.getEventTime(), mAllowTheaterModeWakeFromKey);
}
return result;
}
//...
//关键流程step2
private void interceptPowerKeyUp(KeyEvent event, boolean interactive, boolean canceled) {
final boolean handled = canceled || mPowerKeyHandled;
mScreenshotChordPowerKeyTriggered = false;
cancelPendingScreenshotChordAction();
cancelPendingPowerKeyAction();
if (!handled) {
// Figure out how to handle the key now that it has been released.
mPowerKeyPressCounter += 1;
final int maxCount = getMaxMultiPressPowerCount();
final long eventTime = event.getDownTime();
if (mPowerKeyPressCounter < maxCount) {
// This could be a multi-press. Wait a little bit longer to confirm.
// Continue holding the wake lock.
Message msg = mHandler.obtainMessage(MSG_POWER_DELAYED_PRESS,
interactive ? 1 : 0, mPowerKeyPressCounter, eventTime);
msg.setAsynchronous(true);
mHandler.sendMessageDelayed(msg, ViewConfiguration.getDoubleTapTimeout());
return;
}
// No other actions. Handle it immediately.
powerPress(eventTime, interactive, mPowerKeyPressCounter);
}
// Done. Reset our state.
finishPowerKeyPress();
}
//...
//关键流程step3
private void powerPress(long eventTime, boolean interactive, int count) {
if (mScreenOnEarly && !mScreenOnFully) {
return;
}
if (count == 2) {
powerMultiPressAction(eventTime, interactive, mDoublePressOnPowerBehavior);
} else if (count == 3) {
powerMultiPressAction(eventTime, interactive, mTriplePressOnPowerBehavior);
} else if (interactive && !mBeganFromNonInteractive) {
//关键流程step4 除了无效处理意外,都会调用到PowerManagerService中的goToSleep方法
switch (mShortPressOnPowerBehavior) {
case SHORT_PRESS_POWER_NOTHING:
break;
case SHORT_PRESS_POWER_GO_TO_SLEEP:
mPowerManager.goToSleep(eventTime,
PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON, 0);
break;
case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP:
mPowerManager.goToSleep(eventTime,
PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON,
PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE);
break;
case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP_AND_GO_HOME:
mPowerManager.goToSleep(eventTime,
PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON,
PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE);
launchHomeFromHotKey();
break;
}
}
}
//...
}
2 从PowerManagerService到Notifier的处理
从PhoneWindowManager的interceptKeyBeforeQueueing方法入口开始分析,最终会到达PowerManagerService的gotoSleep方法(从PowerManager到PowerManagerService的调用就不在分析了,这个属于binder通信的范畴),对应的代码实现如下:
public final class PowerManagerService extends SystemService
implements Watchdog.Monitor {
private static final String TAG = "PowerManagerService";
private static final boolean DEBUG = false;
//...
//关键流程step1
private final class BinderService extends IPowerManager.Stub {
@Override // Binder call
public void goToSleep(long eventTime, int reason, int flags) {
if (eventTime > SystemClock.uptimeMillis()) {
throw new IllegalArgumentException("event time must not be in the future");
}
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.DEVICE_POWER, null);
final int uid = Binder.getCallingUid();
final long ident = Binder.clearCallingIdentity();
try {
goToSleepInternal(eventTime, reason, flags, uid);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
//...
}
//...
//关键流程step2
private void goToSleepInternal(long eventTime, int reason, int flags, int uid) {
synchronized (mLock) {
if (goToSleepNoUpdateLocked(eventTime, reason, flags, uid)) {
updatePowerStateLocked();
}
}
}
//...
//关键流程step3
private boolean goToSleepNoUpdateLocked(long eventTime, int reason, int flags, int uid) {
if (eventTime < mLastWakeTime
|| mWakefulness == WAKEFULNESS_ASLEEP
|| mWakefulness == WAKEFULNESS_DOZING
|| !mBootCompleted || !mSystemReady) {
return false;
}
try {
//...
mLastSleepTime = eventTime;
mSandmanSummoned = true;
setWakefulnessLocked(WAKEFULNESS_DOZING, reason);
// Report the number of wake locks that will be cleared by going to sleep.
int numWakeLocksCleared = 0;
final int numWakeLocks = mWakeLocks.size();
for (int i = 0; i < numWakeLocks; i++) {
final WakeLock wakeLock = mWakeLocks.get(i);
switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
case PowerManager.FULL_WAKE_LOCK:
case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
case PowerManager.SCREEN_DIM_WAKE_LOCK:
numWakeLocksCleared += 1;
break;
}
}
// Skip dozing if requested.
if ((flags & PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE) != 0) {
reallyGoToSleepNoUpdateLocked(eventTime, uid);
}
}
return true;
}
//...
//关键流程step4
private boolean reallyGoToSleepNoUpdateLocked(long eventTime, int uid) {
if (eventTime < mLastWakeTime || mWakefulness == WAKEFULNESS_ASLEEP
|| !mBootCompleted || !mSystemReady) {
return false;
}
try {
mDirty |= DIRTY_WAKEFULNESS;
mWakefulness = WAKEFULNESS_ASLEEP;
setInteractiveStateLocked(false, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);
}
return true;
}
//关键流程step5
private void setInteractiveStateLocked(boolean interactive, int reason) {
if (mInteractive != interactive) {
finishInteractiveStateChangeLocked();
mInteractive = interactive;
mInteractiveChanging = true;
mNotifier.onInteractiveStateChangeStarted(interactive, reason);
}
}
//...
//关键流程step6
private void finishInteractiveStateChangeLocked() {
if (mInteractiveChanging) {
mNotifier.onInteractiveStateChangeFinished(mInteractive);
mInteractiveChanging = false;
}
}
//...
}
3 Notifier的处理
Notifier的处理包含2个层面,一个是发送SCREEN_OFF的广播,通知其他子系统亮屏的消息。一个是通过PhoneWindowManager的处理来逐层执行对应回调onScreenTurnedOff方法。
3.1 从Notifier最终发送SCREEN_OFF广播
针对发送广播的逻辑处理流程,代码逻辑流程如下:
//Notifier
final class Notifier {
private static final String TAG = "PowerManagerNotifier";
private static final boolean DEBUG = false;
//...
//关键流程step1
public void onInteractiveStateChangeFinished(boolean interactive) {
synchronized (mLock) {
if (!interactive) {
if (mActualPowerState != POWER_STATE_ASLEEP) {
mActualPowerState = POWER_STATE_ASLEEP;
mPendingGoToSleepBroadcast = true;
if (mUserActivityPending) {
mUserActivityPending = false;
mHandler.removeMessages(MSG_USER_ACTIVITY);
}
mHandler.post(new Runnable() {
@Override
public void run() {
int why = WindowManagerPolicy.OFF_BECAUSE_OF_USER;
switch (mLastGoToSleepReason) {
case PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN:
why = WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN;
break;
case PowerManager.GO_TO_SLEEP_REASON_TIMEOUT:
why = WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT;
break;
}
EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, why, 0, 0);
mPolicy.goingToSleep(why);
mActivityManagerInternal.goingToSleep();
}
});
updatePendingBroadcastLocked();
}
}
}
}
//...
//关键流程step2
private void updatePendingBroadcastLocked() {
if (!mBroadcastInProgress
&& mActualInteractiveState != INTERACTIVE_STATE_UNKNOWN
&& (mPendingWakeUpBroadcast || mPendingGoToSleepBroadcast
|| mActualInteractiveState != mBroadcastedInteractiveState)) {
mBroadcastInProgress = true;
mSuspendBlocker.acquire();
Message msg = mHandler.obtainMessage(MSG_BROADCAST);
msg.setAsynchronous(true);
mHandler.sendMessage(msg);
}
}
//...
//关键流程step3
private final class NotifierHandler extends Handler {
public NotifierHandler(Looper looper) {
super(looper, null, true /*async*/);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_BROADCAST:
sendNextBroadcast();
break;
//...
}
}
}
//消息处理:当设备从睡眠状态唤醒时,会发送 ACTION_SCREEN_ON 广播;当设备准备进入睡眠状态时,会发送 ACTION_SCREEN_OFF 广播。
private void sendNextBroadcast() {
final int powerState;
synchronized (mLock) {
//mBroadcastedInteractiveState相关处理
//...
mBroadcastStartTime = SystemClock.uptimeMillis();
powerState = mBroadcastedInteractiveState;
}
EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, 1);
// 根据电源状态发送相应的广播
if (powerState == INTERACTIVE_STATE_AWAKE) {
sendWakeUpBroadcast(); //发送唤醒广播
} else {
sendGoToSleepBroadcast(); //发送睡眠广播
}
}
//...
//消息处理:发送有序广播
private void sendGoToSleepBroadcast() {
if (ActivityManagerNative.isSystemReady()) {
// 发送广播关键API,参数解读如下:
// mScreenOnIntent 是一个 Intent,包含了唤醒屏幕的信息
// UserHandle.ALL 表示这个广播会发送给所有用户
// mWakeUpBroadcastDone 是一个 BroadcastReceiver,用于在广播完成后接收回调
// mHandler 是一个 Handler,用于处理广播完成后的回调
// 0 是一个 flags,表示广播的权限
// 最后的 null, null 是额外的参数,这里没有使用
mContext.sendOrderedBroadcastAsUser(mScreenOffIntent, UserHandle.ALL, null,
mGoToSleepBroadcastDone, mHandler, 0, null, null);
} else {
EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3, 1);
sendNextBroadcast();
}
}
//...
}
这条逻辑的处理主要是发送睡眠广播为主。接下来分析从mPolicy.goingToSleep(why);出发的灭屏回调处理流程。
3.2 PhoneWindowManager的goingToSleep方法处理灭屏回调方法
这里主要是从到PhoneWindowManager的wakingUp(policy.goingToSleep)方法,再到各层的onScreenTurnedOff方法的处理逻辑流程,从onInteractiveStateChangeFinished处开始,代码实现如下:
//Notifier
final class Notifier {
private static final String TAG = "PowerManagerNotifier";
private static final boolean DEBUG = false;
//...
public void onInteractiveStateChangeFinished(boolean interactive) {
synchronized (mLock) {
if (!interactive) {
if (mActualPowerState != POWER_STATE_ASLEEP) {
mActualPowerState = POWER_STATE_ASLEEP;
mPendingGoToSleepBroadcast = true;
if (mUserActivityPending) {
mUserActivityPending = false;
mHandler.removeMessages(MSG_USER_ACTIVITY);
}
mHandler.post(new Runnable() {
@Override
public void run() {
int why = WindowManagerPolicy.OFF_BECAUSE_OF_USER;
switch (mLastGoToSleepReason) {
case PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN:
why = WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN;
break;
case PowerManager.GO_TO_SLEEP_REASON_TIMEOUT:
why = WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT;
break;
}
EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, why, 0, 0);
mPolicy.goingToSleep(why);
mActivityManagerInternal.goingToSleep();
}
});
updatePendingBroadcastLocked();
}
}
}
}
//...
}
这里会调用到PhoneWindowManager的goingToSleep方法。代码实现如下:
//phonewindowmanager
@Override
public void goingToSleep(int why) {
EventLog.writeEvent(70000, 0);
synchronized (mLock) {
mAwake = false;
mKeyguardDrawComplete = false;
updateWakeGestureListenerLp();
updateOrientationListenerLp();
updateLockScreenTimeout();
}
if (mKeyguardDelegate != null) {
mKeyguardDelegate.onScreenTurnedOff(why);
}
}
这里会调用到KeyguardDelegate的onScreenTurnedOff方法。代码实现如下:
//KeyguardDelegate
public void onScreenTurnedOff(int why) {
if (mKeyguardService != null) {
mKeyguardService.onScreenTurnedOff(why);
}
mKeyguardState.offReason = why;
mKeyguardState.screenIsOn = false;
}
这里会调用到KeyguardServiceWrapper的onScreenTurnedOff方法。代码实现如下:
//wrapper
@Override // Binder interface
public void onScreenTurnedOff(int reason) {
try {
mService.onScreenTurnedOff(reason);
} catch (RemoteException e) {
Slog.w(TAG , "Remote Exception", e);
}
}
这里会调用到KeyguardService的onScreenTurnedOff方法。代码实现如下:
//KeyguardService
//binder
@Override // Binder interface
public void onScreenTurnedOff(int reason) {
checkPermission();
mKeyguardViewMediator.onScreenTurnedOff(reason);
}
这里会调用到KeyguardViewMediator的onScreenTurnedOff方法。这里才是真正的逻辑实现,代码实现如下:
//KeyguardViewMediator
public void onScreenTurnedOff(int why) {
synchronized (this) {
// 将屏幕状态设置为关闭
mScreenOn = false;
// 重置关键的锁屏完成等待状态
resetKeyguardDonePendingLocked();
// 标记未运行隐藏动画
mHideAnimationRun = false;
// 判断是否需要立即锁屏
// 如果设置为按下电源键立即锁屏,或者设备没有设置安全措施,则立即锁屏
final boolean lockImmediately =
mLockPatternUtils.getPowerButtonInstantlyLocks() || !mLockPatternUtils.isSecure();
// 通知屏幕已关闭
notifyScreenOffLocked();
// 如果存在退出安全模式的回调,执行回调并设置为null
if (mExitSecureCallback != null) {
// 执行回调以通知锁屏退出的结果
try {
mExitSecureCallback.onKeyguardExitResult(false);
} catch (RemoteException e) {
// 如果远程调用失败,记录错误
Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
}
mExitSecureCallback = null;
// 如果锁屏没有被外部启用,隐藏锁屏
if (!mExternallyEnabled) {
hideLocked();
}
} else if (mShowing) {
// 如果锁屏当前正在显示,重置锁屏状态
resetStateLocked();
} else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT ||
(why == WindowManagerPolicy.OFF_BECAUSE_OF_USER && !lockImmediately)){
// 如果屏幕是因为超时关闭,或者用户主动关闭但不需要立即锁屏,
// 则计划延迟显示锁屏
doKeyguardLaterLocked();
} else {
// 否则,立即显示锁屏
doKeyguardLocked(null);
}
}
// 通知更新监控器屏幕已关闭,并传递关闭的原因
KeyguardUpdateMonitor.getInstance(mContext).dispatchScreenTurndOff(why);
}
这里总结下,onScreenTurnedOff 关键逻辑解读如下:
- 当屏幕关闭时,这个方法会被调用。
- 如果设备设置为按下电源键立即锁定,或者设备没有设置安全措施(如PIN、密码、图案),则变量 lockImmediately 会被设置为 true。
- 根据 why 参数的值(表示屏幕关闭的原因),决定是否立即调用 doKeyguardLocked 或者延迟调用 doKeyguardLaterLocked(也会调用doKeyguardLocked )。
接下来关于doKeyguardLocked的处理,可以参考如下文章:
Android SystemUI组件(07)锁屏KeyguardViewMediator分析
参考其第二部分2.2 即可。