添加模拟power key 动作

Java 发表评论

diff –git a/android/frameworks/base/services/java/com/android/server/power/PowerManagerService.java b/android/frameworks/base/services/java/com/android/server/power/PowerManagerService.java
index 090a9aa..de78261 100644
— a/android/frameworks/base/services/java/com/android/server/power/PowerManagerService.java
+++ b/android/frameworks/base/services/java/com/android/server/power/PowerManagerService.java
@@ -72,6 +72,15 @@ import libcore.util.Objects;
 import com.mitac.android.common.*;
 import android.net.wifi.WifiManager;
 import java.io.FileWriter;
+//piano start
+import android.view.KeyEvent;
+import android.hardware.input.InputManager;
+
+import android.os.SystemProperties;
+import android.view.InputDevice;
+import android.view.InputEvent;
+import android.view.KeyCharacterMap;
+//piano end

 
 /**
  * The power manager service is responsible for coordinating power management
@@ -2435,11 +2444,34 @@ public final class PowerManagerService extends IPowerManager.Stub
             }
 
             if (mBatteryForceSleep != BatteryManager.BATTERY_WORK_FINE) {
+                sendKey(KeyEvent.KEYCODE_POWER, 1);//添加模拟power key 动作
+                SystemClock.sleep(1000);
+                Slog.i(USERTAG, “sleep 1s”);

                 goToSleep(SystemClock.uptimeMillis(), PowerManager.GO_TO_SLEEP_REASON_USER);
             }
         }
     }

+//piano start
+    private boolean injectEventSync(KeyEvent event) {
+        final InputManager mInputManager = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
+        return mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
+    }   
+    private boolean sendKey(int keyCode, int metaState) {
+        final long eventTime = SystemClock.uptimeMillis();
+        KeyEvent downEvent = KeyEvent.obtain(eventTime, eventTime, KeyEvent.ACTION_DOWN,
+                keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
+                InputDevice.SOURCE_KEYBOARD, null);
+        if (injectEventSync(downEvent)) {
+            KeyEvent upEvent = KeyEvent.obtain(eventTime, eventTime, KeyEvent.ACTION_UP,
+                    keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
+                    InputDevice.SOURCE_KEYBOARD, null);
+            if(injectEventSync(upEvent)) {
+                return true;
+            }
+        }
+        return false;
+    }
+//piano end  
 
     private final class BootCompletedReceiver extends BroadcastReceiver {
         @Override
         public void onReceive(Context context, Intent intent) {

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *