diff --git a/app/src/main/java/org/beiwe/app/MainService.kt b/app/src/main/java/org/beiwe/app/MainService.kt index b49bbca3..260685f3 100644 --- a/app/src/main/java/org/beiwe/app/MainService.kt +++ b/app/src/main/java/org/beiwe/app/MainService.kt @@ -463,50 +463,44 @@ class MainService : Service() { * check. (we pad with an extra second to ensure that check hits an inflection point where * action is required.) */ fun do_an_on_off_session_check( - now: Long, - is_running: Boolean, - should_turn_off_at: Long, - should_turn_on_again_at: Long, - identifier_string: String, - intent_off_string: String, - on_action: () -> Unit, - off_action: () -> Unit, + now: Long, + is_running: Boolean, + should_turn_off_at: Long, + should_turn_on_at: Long, + intent_on_string: String, + intent_off_string: String, + on_action: () -> Unit, + off_action: () -> Unit, ) { // val t1 = System.currentTimeMillis() if (is_running && now <= should_turn_off_at) { - // printw("'$identifier_string' is running, not time to turn of") - // printv("'$identifier_string - is running - ${System.currentTimeMillis() - t1}") + print("Sensor listener service is running. Next $intent_off_string is scheduled to ${convertTimestamp(should_turn_off_at)}") return } // running, should be off, off is in the past if (is_running && should_turn_off_at < now) { - // printi("'$identifier_string' time to turn off") off_action() - val should_turn_on_again_at_safe = should_turn_on_again_at + 1000 // add a second to ensure we pass the timer - print("setting ON TIMER for $identifier_string to $should_turn_on_again_at_safe") - timer!!.setupSingleAlarmAt(should_turn_on_again_at_safe, Timer.intent_map[identifier_string]!!) - // printv("'$identifier_string - turn off - ${System.currentTimeMillis() - t1}") + val should_turn_on_at_safe = should_turn_on_at + 1000 // add a second to ensure we pass the timer + timer!!.setupSingleAlarmAt(should_turn_on_at_safe, Timer.intent_map[intent_on_string]!!) + print("Sensor listener service turned off. Next $intent_on_string is scheduled to ${convertTimestamp(should_turn_on_at_safe)}") } // not_running, should turn on is still in the future, do nothing - if (!is_running && should_turn_on_again_at >= now) { - // printw("'$identifier_string' correctly off") - // printv("'$identifier_string - correctly off - ${System.currentTimeMillis() - t1}") + if (!is_running && should_turn_on_at >= now) { + print("Sensor listener service is off. Next $intent_on_string is scheduled to ${convertTimestamp(should_turn_on_at)}") return } // not running, should be on, (on is in the past) - if (!is_running && should_turn_on_again_at < now) { + if (!is_running && should_turn_on_at < now) { // always get the current time, the now value could be stale - unlikely but possible we // care that we get data, not that data be rigidly accurate to a clock. - PersistentData.setMostRecentAlarmTime(identifier_string, System.currentTimeMillis()) - // printe("'$identifier_string' turn it on!") + PersistentData.setMostRecentAlarmTime(intent_on_string, System.currentTimeMillis()) on_action() val should_turn_off_at_safe = should_turn_off_at + 1000 // add a second to ensure we pass the timer - print("setting OFF TIMER for $identifier_string to $should_turn_off_at_safe") timer!!.setupSingleAlarmAt(should_turn_off_at_safe, Timer.intent_map[intent_off_string]!!) - // printv("'$identifier_string - on action - ${System.currentTimeMillis() - t1}") + print("Sensor listener service turned on. Next $intent_off_string is scheduled to ${convertTimestamp(should_turn_off_at_safe)}") } } @@ -776,7 +770,7 @@ class MainService : Service() { startForeground(1, notification) foregroundServiceLastStarted = now } - + PersistentData.serviceStartCommand = Date(System.currentTimeMillis()).toLocaleString() // onStartCommand is called every 30 seconds due to repeating high-priority-or-whatever diff --git a/app/src/main/java/org/beiwe/app/utils.kt b/app/src/main/java/org/beiwe/app/utils.kt index 15fde57f..43411e8f 100644 --- a/app/src/main/java/org/beiwe/app/utils.kt +++ b/app/src/main/java/org/beiwe/app/utils.kt @@ -1,8 +1,12 @@ package org.beiwe.app import android.app.PendingIntent +import android.icu.text.SimpleDateFormat +import android.icu.util.TimeZone import android.os.Build import android.util.Log +import java.util.Date +import java.util.Locale // This file is a location for new static functions, further factoring into files will occur when length of file becomes a problem. @@ -53,4 +57,14 @@ fun pending_intent_flag_fix(flag: Int): Int { return (PendingIntent.FLAG_IMMUTABLE or flag) else return flag +} + +/** + * Converts a UTC timestamp to a human-readable date and time string, based on current device's timezone. + */ +fun convertTimestamp(utcTimestamp: Long): String { + val date = Date(utcTimestamp) + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) + dateFormat.timeZone = TimeZone.getDefault() + return dateFormat.format(date) } \ No newline at end of file