From 75a23c1863301fcf3b647b6630d4993270f63d0e Mon Sep 17 00:00:00 2001 From: Jens Date: Sat, 30 Dec 2023 00:02:33 +0100 Subject: [PATCH] Calendar trigger --- .../receivers/CalendarReceiver.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java index 28244ca..879747f 100644 --- a/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java +++ b/app/src/main/java/com/jens/automation2/receivers/CalendarReceiver.java @@ -6,6 +6,8 @@ import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.net.Uri; +import android.os.Handler; +import android.os.Message; import androidx.annotation.NonNull; @@ -34,9 +36,10 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis static List calendarsCache = null; static List calendarEventsCache = null; - static Timer timer = null; - static TimerTask timerTask = null; + static Handler handler; static Calendar nextWakeup = null; + static Message recheckMessage; + static int recheckMessageId = 2130; public static CalendarReceiver getInstance() { @@ -264,11 +267,11 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis private static void armOrRearmTimer() { - timerTask = new TimerTask() - { + handler = new Handler(){ @Override - public void run() + public void handleMessage(@NonNull Message msg) { + super.handleMessage(msg); checkForRules(Miscellaneous.getAnyContext()); // Set next timer @@ -278,12 +281,11 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis } }; - if(timer != null) + if(handler != null) { - timer.cancel(); - timer.purge(); + handler.removeMessages(recheckMessageId); } - timer = new Timer(); +// timer = new Timer(); if(nextWakeup == null) { @@ -291,9 +293,14 @@ public class CalendarReceiver extends BroadcastReceiver implements AutomationLis calculateNextWakeup(); } + if(recheckMessage == null) + { + recheckMessage = new Message(); + recheckMessage.what = recheckMessageId; + } // If it's now filled, go on if(nextWakeup != null) - timer.schedule(timerTask, nextWakeup.getTimeInMillis()); + handler.sendMessageAtTime(recheckMessage, nextWakeup.getTimeInMillis()); } private static void calculateNextWakeup()