系统环境
瑞星微 px30 android 8.1系统
要实现的功能
原本长按power键的功能是弹出一个选择对话框,关机和重启功能,长按两个选择项会提示是否要进入安全模式,现将它改成长按恢复出厂功能
实现思路
- power按键和普通的按键一样都是在framework的PhoneWindowManager中的interceptKeyBeforeQueueing方法监听
@Override
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
if (!mSystemBooted) {
return 0;
}
....
....
switch (keyCode) {
case KeyEvent.KEYCODE_BACK: {
if (down) {
interceptBackKeyDown();
} else {
boolean handled = interceptBackKeyUp(event);
if (handled) {
result &= ~ACTION_PASS_TO_USER;
}
}
break;
}
case KeyEvent.KEYCODE_POWER: {
cancelPendingAccessibilityShortcutAction();
result &= ~ACTION_PASS_TO_USER;
isWakeKey = false;
if (down) {
interceptPowerKeyDown(event, interactive);
} else {
interceptPowerKeyUp(event, interactive, canceled);
}
break;
}
....
}
}
- 在interceptKeyBeforeQueueing找到KEYCODE_POWER的代码,这里走的是interceptPowerKeyDown方法
这里分发出去了一个消息MSG_POWER_LONG_PRESS
private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
...
...
mPowerKeyHandled = hungUp || mScreenshotChordVolumeDownKeyTriggered
|| mA11yShortcutChordVolumeUpKeyTriggered || gesturedServiceIntercepted;
if (!mPowerKeyHandled) {
if (interactive) {
if (hasLongPressOnPowerBehavior()) {
Message msg = mHandler.obtainMessage(MSG_POWER_LONG_PRESS);
msg.setAsynchronous(true);
mHandler.sendMessageDelayed(msg,
ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
}
} 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;
}
}
}
}
}
- 这里分发出去了一个消息MSG_POWER_LONG_PRESS这个消息里面就是一个方法powerLongPress
case MSG_POWER_LONG_PRESS:
powerLongPress();
break;
private void powerLongPress() {
Log.e("zhuw","-----------------powerLongPress----------------");
if (ActivityManager.isUserAMonkey()) {
return;
}
Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
intent.setPackage("android");
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, true);
intent.putExtra(Intent.EXTRA_WIPE_ESIMS, true);
mContext.sendBroadcast(intent);
}
- 这里原本的被注释的代码是根据config_longPressOnPowerBehavior这个系统属性来决定走哪一步操作 我这里原本的系统属性是1
- 因此对应的LONG_PRESS_POWER_GLOBAL_ACTIONS 原本的关机重启选择框就是在showGlobalActionsInternal的showDialog中实现
void showGlobalActionsInternal() {
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
if (mGlobalActions == null) {
mGlobalActions = new GlobalActions(mContext, mWindowManagerFuncs);
}
final boolean keyguardShowing = isKeyguardShowingAndNotOccluded();
mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
if (keyguardShowing) {
mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
}
}
public void showDialog(boolean keyguardShowing, boolean deviceProvisioned) {
if (DEBUG) Slog.d(TAG, "showDialog " + keyguardShowing + " " + deviceProvisioned);
mKeyguardShowing = keyguardShowing;
mDeviceProvisioned = deviceProvisioned;
mShowing = true;
boolean isTvProduct = SystemProperties.get("ro.target.product","unknown").equals("atv") ||
SystemProperties.get("ro.target.product","unknown").equals("box");
if (mStatusBarConnected && !isTvProduct) {
mStatusBarInternal.showGlobalActions();
mHandler.postDelayed(mShowTimeout, 5000);
} else {
ensureLegacyCreated();
mLegacyGlobalActions.showDialog(mKeyguardShowing, mDeviceProvisioned);
}
}
- showDialog方法这里有两种UI实现,一个是在SystemUI中,一个是当SystemUI不存在时framework中的旧菜单中实现 两者方法实现差不多,这里主要介绍下显示弹框的配置,它是获取config_globalActionsList数组配置来进行显示 我这里将原来的power改成reset
<string-array name="config_globalActionsList" translatable="false">
<!--<item>power</item>-->
<item>reset</item>
<item>restart</item>
<item>bugreport</item>
<item>users</item>
</string-array>
- 代码提供出来,可以参考
写在最后
- 本文主要介绍了在Android 8.1 系统中长按power键弹出一个关机菜单的流程并做对应修改
|