FreemePhoneWindowManager.java
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
int result = super.interceptKeyBeforeQueueing(event, policyFlags);
......
}
PhoneWindowManager.java
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
......
// Handle special keys.
switch (keyCode) {
......
case KeyEvent.KEYCODE_POWER: {
// Any activity on the power button stops the accessibility shortcut
cancelPendingAccessibilityShortcutAction();
result &= ~ACTION_PASS_TO_USER;
isWakeKey = false; // wake-up will be handled separately
if (down) {
interceptPowerKeyDown(event, interactive);//如果按下会执行该方法
} else {
interceptPowerKeyUp(event, interactive, canceled);
}
break;
}
......
}
private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
......
// If the power key has still not yet been handled, then detect short
// press, long press, or multi press and decide what to do.
mPowerKeyHandled = hungUp || mScreenshotChordVolumeDownKeyTriggered
|| mA11yShortcutChordVolumeUpKeyTriggered || gesturedServiceIntercepted;
if (!mPowerKeyHandled) {
if (interactive) {
// When interactive, we're already awake.
// Wait for a long press or for the button to be released to decide what to do.
if (hasLongPressOnPowerBehavior()) {
Message msg = mHandler.obtainMessage(MSG_POWER_LONG_PRESS);
msg.setAsynchronous(true);
mHandler.sendMessageDelayed(msg,
ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
}//到Handler中去执行显示界面和关机的操作
} else {
wakeUpFromPowerKey(event.getDownTime());
if (mSupportLongPressPowerWhenNonInteractive && hasLongPressOnPowerBehavior()) {
Message msg = mHandler.obtainMessage(MSG_POWER_LONG_PRESS);
msg.setAsynchronous(true);
mHandler.sendMessageDelayed(msg,
ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
mBeganFromNonInteractive = true;
} else {
final int maxCount = getMaxMultiPressPowerCount();
if (maxCount <= 1) {
mPowerKeyHandled = true;
} else {
mBeganFromNonInteractive = true;
}
}
}
}
mHandler = new PolicyHandler();
private class PolicyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
......
case MSG_POWER_LONG_PRESS:
powerLongPress();
break;
......
}
}
}
private void powerLongPress() {
final int behavior = getResolvedLongPressOnPowerBehavior();
switch (behavior) {
case LONG_PRESS_POWER_NOTHING:
break;
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
showGlobalActionsInternal();//此处为显示关机界面的方法
break;
case LONG_PRESS_POWER_SHUT_OFF:
case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(behavior == LONG_PRESS_POWER_SHUT_OFF);
break;
}
}
FreemePhoneWindowManager.java重写了该方法
@Override
protected void showGlobalActionsInternal() {
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
com.freeme.server.policy.globalaction.FreemeGlobalActions
.getInstance(mContext, mWindowManagerFuncs)
.showDialog(isKeyguardShowingAndNotOccluded(), isDeviceProvisioned());
}
FreemeGlobalActions.java
public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
if (mFreemeGlobalActionsShowing) {
return;
}
mKeyguardShowing = keyguardShowing;
mIsDeviceProvisioned = isDeviceProvisioned;
mHandler.sendEmptyMessage(MESSAGE_SHOW);
}
private Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
......
case MESSAGE_SHOW: {
handleShow();
} break;
}
FreemeGlobalActions.java
private void createGlobalActionsView() {
if (mGlobalActionsView != null) {
mWM.removeView(mGlobalActionsView);
mGlobalActionsView = null;
}
int themeID = mContext.getResources().getIdentifier("Theme.Freeme.Panel", "style", "freeme");
ContextThemeWrapper themeContext = new ContextThemeWrapper(mContext, themeID);
LayoutInflater inflater = LayoutInflater.from(themeContext);
int flags = WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
| WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
int stretch = -1;
WindowManager.LayoutParams
lp = new WindowManager.LayoutParams(stretch, stretch, WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG, flags, stretch);
/*/
lp.privateFlags |= WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
//*/
lp.privateFlags |= WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS | WindowManager.LayoutParams.PRIVATE_FLAG_COLLAPSE_NAVI_BAR;
//*/
lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
//*/ FIXME: 17-7-13 replace anim later
lp.windowAnimations = android.R.style.Animation_Toast;
//*/
mGlobalActionsView = (FreemeGlobalActionsView) inflater.inflate(R.layout.globalaction_root_view_freeme, null);//mGlobalActionsView即为要显示的关机diglog,globalaction_root_view_freeme为xml视图
mGlobalActionsView.setContentDescription(mContext.getResources().getString(R.string.globalaction_poweroff_widget_name));
mGlobalActionsView.initUI(mHandler.getLooper());
mGlobalActionsView.registerActionPressedCallback(this);
mGlobalActionsView.setVisibility(View.INVISIBLE);
mGlobalActionsView.requestFocus();
mWM.addView(mGlobalActionsView, lp);
mGlobalActionsView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}