功能需求短按实现弹出:重启或者关机对话框
长按直接关机:
1.短按:
2.长按:
------------------------------------------------------------------------------------------------------------------------------------------------
短按
mShortPressOnPowerBehavior = mContext.getResources().getInteger(
com.android.internal.R.integer.config_shortPressOnPowerBehavior);
长按
mLongPressOnPowerBehavior = mContext.getResources().getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior);
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;
case SHORT_PRESS_POWER_GO_HOME:
launchHomeFromHotKey(true /* awakenFromDreams */, false /*respectKeyguard*/);
break;
}
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() && !SystemProperties.getBoolean("ro.platform.has.tvuimode", false) ) ||(whichPowerKeyDefinition() == POWER_KEY_SHUTDOWN)) {
Message msg = mHandler.obtainMessage(MSG_POWER_LONG_PRESS);
msg.setAsynchronous(true);
// if(SystemProperties.getBoolean("ro.LongPowerkey.Defined", false))
// {
// Log.d(TAG,"11111 is powerkeyshutdowndefiniton");
// mHandler.sendMessageDelayed(msg, 3000);
// }
// else
// {
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;
}
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
private static final String POWER_KEY_DEFINITION = "power_key_definition";
static final int POWER_KEY_SUSPEND = 0;
static final int POWER_KEY_SHUTDOWN = 1;
static final int POWER_KEY_RESTART = 2;
private int whichPowerKeyDefinition() {
int default_value = 0;
// if (SystemProperties.getBoolean("ro.platform.has.tvuimode", false)) {
if (SystemProperties.getBoolean("ro.platform.has.tvuimode", false)|| SystemProperties.getBoolean("ro.platform.has.down_power", false)) {
default_value = 1;
}
return Settings.System.getInt(mContext.getContentResolver(), POWER_KEY_DEFINITION, default_value);
}
--------------------------------------------------------------------------------------------------------------------------------------
int definedPowerKey = whichPowerKeyDefinition();
if (definedPowerKey == POWER_KEY_SHUTDOWN) {
mPowerManager.shutdown(false,"userrequested",false); //我们要修改的地方
return;
}
private void powerLongPress() {
final int behavior = getResolvedLongPressOnPowerBehavior();
switch (behavior) {
case LONG_PRESS_POWER_NOTHING:
break;
case LONG_PRESS_POWER_GLOBAL_ACTIONS: //我们要修改的地方
mPowerKeyHandled = true;
if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {
performAuditoryFeedbackForAccessibilityIfNeed();
}
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;
}
}