From 22cdab6bfb0c6ac7f28c5c62ea82a7d607ad9b27 Mon Sep 17 00:00:00 2001 From: brunoais Date: Thu, 9 Jul 2020 19:00:24 +0100 Subject: [PATCH] More efficient version recycling the scheduling thread --- .../com/genymobile/scrcpy/Controller.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Controller.java b/server/src/main/java/com/genymobile/scrcpy/Controller.java index eaa43e4b..e4ea101e 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/Controller.java @@ -28,6 +28,8 @@ public class Controller { private final MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[PointersState.MAX_POINTERS]; private final MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[PointersState.MAX_POINTERS]; + private final Timer keepScreenOffTimer = new Timer("KeepScreenOff", true); + public Controller(Device device, DesktopConnection connection) { this.device = device; this.connection = connection; @@ -238,18 +240,16 @@ public class Controller { * Hopefully something better will come up in the future * Tested on a low end Android 6 device */ - public void scheduleScreenOff(){ - if(screenStayOff) { - final Timer timing = new Timer("KeepScreenOn", true); - timing.schedule(new TimerTask() { - @Override - public void run() { - Ln.i("Forcing screen back off"); - if (Device.setScreenPowerMode(Device.POWER_MODE_OFF)) { - timing.cancel(); - } - } - }, + public void scheduleScreenOff() { + if (screenStayOff) { + keepScreenOffTimer.schedule( + new TimerTask() { + @Override + public void run() { + Ln.i("Forcing screen back off"); + Device.setScreenPowerMode(Device.POWER_MODE_OFF); + } + }, 100); } }