8
8
import android .view .MotionEvent ;
9
9
10
10
import java .io .IOException ;
11
+ import java .util .concurrent .Executors ;
12
+ import java .util .concurrent .ScheduledExecutorService ;
13
+ import java .util .concurrent .TimeUnit ;
11
14
12
15
public class Controller {
13
16
14
17
private static final int DEVICE_ID_VIRTUAL = -1 ;
15
18
19
+ private static final ScheduledExecutorService EXECUTOR = Executors .newSingleThreadScheduledExecutor ();
20
+
16
21
private final Device device ;
17
22
private final DesktopConnection connection ;
18
23
private final DeviceMessageSender sender ;
@@ -24,6 +29,8 @@ public class Controller {
24
29
private final MotionEvent .PointerProperties [] pointerProperties = new MotionEvent .PointerProperties [PointersState .MAX_POINTERS ];
25
30
private final MotionEvent .PointerCoords [] pointerCoords = new MotionEvent .PointerCoords [PointersState .MAX_POINTERS ];
26
31
32
+ private boolean keepPowerModeOff ;
33
+
27
34
public Controller (Device device , DesktopConnection connection ) {
28
35
this .device = device ;
29
36
this .connection = connection ;
@@ -117,6 +124,7 @@ private void handleEvent() throws IOException {
117
124
int mode = msg .getAction ();
118
125
boolean setPowerModeOk = Device .setScreenPowerMode (mode );
119
126
if (setPowerModeOk ) {
127
+ keepPowerModeOff = mode == Device .POWER_MODE_OFF ;
120
128
Ln .i ("Device screen turned " + (mode == Device .POWER_MODE_OFF ? "off" : "on" ));
121
129
}
122
130
}
@@ -130,6 +138,9 @@ private void handleEvent() throws IOException {
130
138
}
131
139
132
140
private boolean injectKeycode (int action , int keycode , int repeat , int metaState ) {
141
+ if (keepPowerModeOff && action == KeyEvent .ACTION_UP && (keycode == KeyEvent .KEYCODE_POWER || keycode == KeyEvent .KEYCODE_WAKEUP )) {
142
+ schedulePowerModeOff ();
143
+ }
133
144
return device .injectKeyEvent (action , keycode , repeat , metaState );
134
145
}
135
146
@@ -223,8 +234,24 @@ private boolean injectScroll(Position position, int hScroll, int vScroll) {
223
234
return device .injectEvent (event );
224
235
}
225
236
237
+ /**
238
+ * Schedule a call to set power mode to off after a small delay.
239
+ */
240
+ private static void schedulePowerModeOff () {
241
+ EXECUTOR .schedule (new Runnable () {
242
+ @ Override
243
+ public void run () {
244
+ Ln .i ("Forcing screen off" );
245
+ Device .setScreenPowerMode (Device .POWER_MODE_OFF );
246
+ }
247
+ }, 200 , TimeUnit .MILLISECONDS );
248
+ }
249
+
226
250
private boolean pressBackOrTurnScreenOn () {
227
251
int keycode = device .isScreenOn () ? KeyEvent .KEYCODE_BACK : KeyEvent .KEYCODE_WAKEUP ;
252
+ if (keepPowerModeOff && keycode == KeyEvent .KEYCODE_WAKEUP ) {
253
+ schedulePowerModeOff ();
254
+ }
228
255
return device .injectKeycode (keycode );
229
256
}
230
257
0 commit comments