From 536a5e22f96b6bcf1262645cf67903ec0088252c Mon Sep 17 00:00:00 2001 From: jens Date: Sat, 2 Jul 2022 12:46:34 +0200 Subject: [PATCH] wakelock action --- .../java/com/jens/automation2/Action.java | 6 ++ .../java/com/jens/automation2/Actions.java | 66 ++++++++++--------- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index 32f8aa3f..528924c8 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -576,6 +576,12 @@ public class Action else if(execParts.length == 2) Actions.runExecutable(context, this.getParameter1(), execParts[0], execParts[1]); break; + case wakelock: + if(this.getParameter1()) + Actions.wakeLockStart(context, Long.parseLong(this.getParameter2())); + else + Actions.wakeLockStop(); + break; default: Miscellaneous.logEvent("w", "Action", context.getResources().getString(R.string.unknownActionSpecified), 3); break; diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index 57144f10..d9cd7fd0 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -1142,7 +1142,6 @@ public class Actions { Miscellaneous.logEvent("i", "waitBeforeNextAction", "waitBeforeNextAction for " + String.valueOf(waitTime) + " milliseconds.", 4); - wakeLockStart(60000); try { Thread.sleep(waitTime); @@ -2097,48 +2096,53 @@ public class Actions public final static int wakeLockTimeoutDisabled = -1; static boolean wakeLockStopRequested = false; - public static void wakeLockStart(long duration) + public static void wakeLockStart(Context context, long duration) { - long waited = 0; - int step = 1000; - - if(duration < 0) - step = 99999; - - try + Thread lockThread = new Thread(new Runnable() { - PowerManager powerManager = (PowerManager) Miscellaneous.getAnyContext().getSystemService(Context.POWER_SERVICE); - PowerManager.WakeLock fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK"); - fullWakeLock.acquire(); // turn on - - do + @Override + public void run() { + wakeLockStopRequested = false; + + long waited = 0; + int step = 2000; try { - Thread.sleep(step); // turn on duration - } - catch (InterruptedException e) - { - e.printStackTrace(); - } + PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + PowerManager.WakeLock fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK"); + fullWakeLock.acquire(); // turn on - waited += step; + do + { - if(false) //stop requested + try + { + Thread.sleep(step); // turn on duration + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + + if(duration > 0) + waited += step; + + if(wakeLockStopRequested) //stop requested + Miscellaneous.logEvent("i", "WakeLockStart", "Stop requested.", 4); + } + while(!wakeLockStopRequested && (duration < 0 || waited <= duration)); + + fullWakeLock.release(); + } + catch (Exception e) { - Miscellaneous.logEvent("i", "WakeLockStart", "Stop requested.", 4); - wakeLockStopRequested = false; - break; } } - while(waited <= duration); + }); - fullWakeLock.release(); - } - catch (Exception e) - { - } + lockThread.start(); } public static void wakeLockStop()