8
8
import android .view .MotionEvent ;
9
9
10
10
import java .io .IOException ;
11
+ import java .util .Timer ;
12
+ import java .util .TimerTask ;
11
13
12
14
public class Controller {
13
15
@@ -24,6 +26,9 @@ public class Controller {
24
26
private final MotionEvent .PointerProperties [] pointerProperties = new MotionEvent .PointerProperties [PointersState .MAX_POINTERS ];
25
27
private final MotionEvent .PointerCoords [] pointerCoords = new MotionEvent .PointerCoords [PointersState .MAX_POINTERS ];
26
28
29
+ private boolean screenStayOff ;
30
+ private static final Timer keepScreenOffTimer = new Timer ("KeepScreenOff" , true );
31
+
27
32
public Controller (Device device , DesktopConnection connection ) {
28
33
this .device = device ;
29
34
this .connection = connection ;
@@ -117,6 +122,7 @@ private void handleEvent() throws IOException {
117
122
int mode = msg .getAction ();
118
123
boolean setPowerModeOk = Device .setScreenPowerMode (mode );
119
124
if (setPowerModeOk ) {
125
+ screenStayOff = mode == Device .POWER_MODE_OFF ;
120
126
Ln .i ("Device screen turned " + (mode == Device .POWER_MODE_OFF ? "off" : "on" ));
121
127
}
122
128
}
@@ -130,6 +136,9 @@ private void handleEvent() throws IOException {
130
136
}
131
137
132
138
private boolean injectKeycode (int action , int keycode , int repeat , int metaState ) {
139
+ if (screenStayOff && action == KeyEvent .ACTION_UP && (keycode == KeyEvent .KEYCODE_POWER || keycode == KeyEvent .KEYCODE_WAKEUP )) {
140
+ scheduleScreenOff ();
141
+ }
133
142
return device .injectKeyEvent (action , keycode , repeat , metaState );
134
143
}
135
144
@@ -223,8 +232,24 @@ private boolean injectScroll(Position position, int hScroll, int vScroll) {
223
232
return device .injectEvent (event );
224
233
}
225
234
235
+ /**
236
+ * Schedule a call to turn the screen off after a small delay.
237
+ */
238
+ private static void scheduleScreenOff () {
239
+ keepScreenOffTimer .schedule (new TimerTask () {
240
+ @ Override
241
+ public void run () {
242
+ Ln .i ("Forcing screen off" );
243
+ Device .setScreenPowerMode (Device .POWER_MODE_OFF );
244
+ }
245
+ }, 200 );
246
+ }
247
+
226
248
private boolean pressBackOrTurnScreenOn () {
227
249
int keycode = device .isScreenOn () ? KeyEvent .KEYCODE_BACK : KeyEvent .KEYCODE_WAKEUP ;
250
+ if (screenStayOff && keycode == KeyEvent .KEYCODE_WAKEUP ) {
251
+ scheduleScreenOff ();
252
+ }
228
253
return device .injectKeycode (keycode );
229
254
}
230
255
0 commit comments