From 4eb67120a50e8f5e4f09273afd7ca1f49eacb8ef Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 3 Apr 2023 19:06:27 -0700 Subject: [PATCH] Don't go to dead from sleep, doze produces extremely long delays. --- README.md | 6 ++-- .../java/net/voussoir/trkpt/TrackerService.kt | 34 +++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3b59171..fce0e2f 100644 --- a/README.md +++ b/README.md @@ -33,15 +33,13 @@ trkpt has three states of power management. The states transition like this: Location leaves homepoint → Full power (presumably motion sensors will trigger, but just in case) - Unplugged from charger → Full power (maybe you are getting ready to depart) - - Unable to receive fix for several minutes and not charging → Dead (time is doubled to accommodate slower sleeping pace) + Plugged/unplugged charger → Full power 3. **DEAD**: disables location updates. Motion sensors → Full power - Plugged in to charger → Full power + Plugged/unplugged charger → Full power Although saving battery power is important, capturing trackpoints is the #1 priority. I'd rather have too many wakeups than too few. diff --git a/app/src/main/java/net/voussoir/trkpt/TrackerService.kt b/app/src/main/java/net/voussoir/trkpt/TrackerService.kt index c538946..cdff4b5 100644 --- a/app/src/main/java/net/voussoir/trkpt/TrackerService.kt +++ b/app/src/main/java/net/voussoir/trkpt/TrackerService.kt @@ -562,23 +562,15 @@ class TrackerService: Service() { if (intent.action == Intent.ACTION_POWER_CONNECTED) { - Log.i("VOUSSOIR", "Power connected") device_is_charging = true - if (location_interval == Keys.LOCATION_INTERVAL_DEAD) - { - reset_location_listeners(interval=Keys.LOCATION_INTERVAL_FULL_POWER) - } } else if (intent.action == Intent.ACTION_POWER_DISCONNECTED) { - Log.i("VOUSSOIR", "Power disconnected") device_is_charging = false - // If the user has just unplugged their device, maybe they're getting ready - // to go out. - if (location_interval == Keys.LOCATION_INTERVAL_SLEEP) - { - reset_location_listeners(interval=Keys.LOCATION_INTERVAL_FULL_POWER) - } + } + if (trackingState == Keys.STATE_TRACKING_ACTIVE) + { + reset_location_listeners(interval=Keys.LOCATION_INTERVAL_FULL_POWER) } } } @@ -682,7 +674,7 @@ class TrackerService: Service() Keys.PREF_ALLOW_SLEEP -> { allow_sleep = PreferencesHelper.loadAllowSleep() - if (! allow_sleep && location_interval != Keys.LOCATION_INTERVAL_FULL_POWER) + if (! allow_sleep && trackingState == Keys.STATE_TRACKING_ACTIVE && location_interval != Keys.LOCATION_INTERVAL_FULL_POWER) { reset_location_listeners(interval=Keys.LOCATION_INTERVAL_FULL_POWER) } @@ -716,16 +708,22 @@ class TrackerService: Service() val now = System.currentTimeMillis() last_watchdog = now - val struggletime = if (location_interval == Keys.LOCATION_INTERVAL_SLEEP) (TIME_UNTIL_DEAD * 2) else TIME_UNTIL_DEAD if ( allow_sleep && has_motion_sensor && !device_is_charging && + // We only go to dead during active tracking because if you are looking at the + // device in non-tracking mode you are probably waiting for your signal to recover. trackingState == Keys.STATE_TRACKING_ACTIVE && - location_interval != Keys.LOCATION_INTERVAL_DEAD && - (now - listeners_enabled_at) > struggletime && - (now - currentBestLocation.time) > struggletime && - (now - last_significant_motion) > struggletime + // We only go to dead from full power because in the sleep state, the wakelock is + // turned off and the device may go into doze. During doze, the device stops + // updating the location listeners anyway, so there is no benefit in going to dead. + // When the user interacts with the device and it leaves doze, it's better to come + // from sleep state than dead state. + location_interval == Keys.LOCATION_INTERVAL_FULL_POWER && + (now - listeners_enabled_at) > TIME_UNTIL_DEAD && + (now - currentBestLocation.time) > TIME_UNTIL_DEAD && + (now - last_significant_motion) > TIME_UNTIL_DEAD ) { reset_location_listeners(interval=Keys.LOCATION_INTERVAL_DEAD)