受Android4.0 组合键KEY_POWER+KEY_VOLUME_DOWN截屏功能启发,
实现组合键KEY_POWER+KEY_VOLUME_UP重启手机。
修改文件:
$(ANDROID_ROOT)\frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.java
方法public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn)中:
共修改两处;
第一处:
// Handle special keys.
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_MUTE: {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if (down) {
if (isScreenOn && !mVolumeDownKeyTriggered
&& (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
mVolumeDownKeyTriggered = true;
mVolumeDownKeyTime = event.getDownTime();
mVolumeDownKeyConsumedByScreenshotChord = false;
cancelPendingPowerKeyAction();
interceptScreenshotChord();
}
} else {
mVolumeDownKeyTriggered = false;
cancelPendingScreenshotChordAction();
}
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
if (down) {
//Modified here to reboot device start
if (!mVolumeUpKeyTriggered
&& (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
mVolumeUpKeyTriggered = true;
mVolumeUpKeyTime = event.getDownTime();
if(interceptRebootDevice()){
return 0;
}
if(isScreenOn){
cancelPendingPowerKeyAction();
cancelPendingScreenshotChordAction();
}
}
//Modified here to reboot device end
} else {
mVolumeUpKeyTriggered = false;
cancelPendingScreenshotChordAction();
cancelPendingRebootDeviceAction();
}
}
*********************************************************************************************
第二处:
case KeyEvent.KEYCODE_POWER: {
result &= ~ACTION_PASS_TO_USER;
if (down) {
//Modified here to reboot device start.
if (!mPowerKeyTriggered
&& (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
mPowerKeyTriggered = true;
mPowerKeyTime = event.getDownTime();
if(interceptRebootDevice()){
return 0;
}
if(isScreenOn){
interceptScreenshotChord();
}
}
//Modified here to reboot device end.
新增的变量及方法:
private long mVolumeUpKeyTime;
/**
* Added by shihaijun for reboot devices without reason.
*/
private boolean interceptRebootDevice(){
Log.d(TAG,"_______________enter inteceptRebootDevice_________");
if(mPowerKeyTriggered && mVolumeUpKeyTriggered && !mVolumeDownKeyTriggered){
final long now = SystemClock.uptimeMillis();
if(now <= mVolumeUpKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS
&& now <= mPowerKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS){
Log.d(TAG,"_______________Reboot Device after "+ViewConfiguration.getGlobalActionKeyTimeout()+"______");
mVolumeUpKeyConsumedByRebootDevice = true;
cancelPendingPowerKeyAction();
mHandler.postDelayed(mRebootDeviceLongPress,
ViewConfiguration.getGlobalActionKeyTimeout());
return true;
}
}
return false;
}
/** Added by shihaijun to reboot device*/
private Runnable mRebootDeviceLongPress = new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
Power.reboot(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private void cancelPendingRebootDeviceAction(){
mHandler.removeCallbacks(mRebootDeviceLongPress);
}