One: Android handle power button Long press operation
In the framework layer, android4.x to the POWER key (keyevent.keycode_power), we analyze it from Phonewindowmanager, and I print the stack call information for the method before the analysis. You can refer to it for a moment.
public long interceptkeybeforedispatching (WindowState win, KeyEvent event, int policyflags) {
...
ANDROID.UTIL.LOG.D ("BILL", Android.util.Log.getStackTraceString (New Throwable ()));
...
}
1-13 19:35:32.458 D/bill (718): java.lang.Throwable
01-13 19:35:32.458 D/bill (718): at Com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeDispatching (phonewindowmanager.java:2224 )
01-13 19:35:32.458 D/bill (718): at Com.android.server.wm.InputMonitor.interceptKeyBeforeDispatching ( inputmonitor.java:501)
01-13 19:35:32.458 D/bill (718): at Com.android.server.input.InputManagerService.interceptKeyBeforeDispatching (inputmanagerservice.java:1383)
01-13 19:35:32.458 D/bill (718): at Dalvik.system.NativeStart.run (Native Method)
The calling process is as follows (only the critical code is posted):
Interceptkeybeforedispatching ()-Interceptpowerkeydown ()-mpowerlongpress.run ()
1>
Public long interceptkeybeforedispatching (WindowState win, keyevent event, int policyflags) {
......
Case Keyevent.keycode_power: {
Result &= ~action_pass_to_user;
if (down) {
Mimmersivemodeconfirmation.onpowerkeydown (Isscreenon, Event.getdowntime (),
Isimmersivemode (mlastsystemuiflags));
if (Isscreenon &&!mpowerkeytriggered
&& (Event.getflags () & keyevent.flag_fallback) = = 0) {
Mpowerkeytriggered = true;
Mpowerkeytime = Event.getdowntime ();
Interceptscreenshotchord ();
}
Itelephony Telephonyservice = Gettelephonyservice ();
Boolean hungup = false;
if (telephonyservice! = null) {
try {
if (telephonyservice.isringing ()) {
Pressing Power While there ' s a ringing incoming
Call should silence the ringer.
Telephonyservice.silenceringer ();
M: [ALPS00093981] @{
} else if ((Isscreenon
|| Mscreenoffreason = = off_because_of_prox_sensor)
/// @}
&& (Mincallpowerbehavior
& Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP)! = 0
&& Telephonyservice.isoffhook ()) {
Otherwise, if "Power button ends call" is enabled,
The Power button would hang and up any current active call.
Hungup = Telephonyservice.endcall ();
}
} catch (RemoteException ex) {
LOG.W (TAG, "Itelephony threw RemoteException", ex);
}
}
Interceptpowerkeydown (!isscreenon | | hungup
|| mvolumedownkeytriggered | | mvolumeupkeytriggered);
} else {
Mpowerkeytriggered = false;
Cancelpendingscreenshotchordaction ();
if (Interceptpowerkeyup(Canceled | | mpendingpowerkeyupcanceled)) {
result = (Result & ~action_wake_up) | Action_go_to_sleep;
}
mpendingpowerkeyupcanceled = false;
}
Break
}
......
}
Note! Red for judging long press (down), blue for judging Short press (UP).
2>
private void Interceptpowerkeydown (Boolean handled) {
mpowerkeyhandled = handled;
if (!handled) {
Mhandler.postdelayed (mpowerlongpress, Viewconfiguration.getglobalactionkeytimeout ());
}
}
3>
private final Runnable mpowerlongpress = new Runnable () {
......
Case Long_press_power_global_actions:
Mpowerkeyhandled = true;
if (!PERFORMHAPTICFEEDBACKLW (null, hapticfeedbackconstants.long_press, false)) {
Performauditoryfeedbackforaccessibilityifneed ();
}
Sendclosesystemwindows (system_dialog_reason_global_actions);
Showglobalactionsdialog ();
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 (Resolvedbehavior = = Long_press_power_shut_off);
Break
......
};
Note: The above Code underlines the dialog box that pops up (options such as shutdown, restart, airplane mode, and so on).
Android Framework layer Power key shutdown process (one, power long key operation processing)