Add wakelock to prevent doze when not at home.

Recently I took a 90 minute drive, and trkpt missed about 15
minutes of it. It wasn't until now that I realized it probably
went into doze mode due to the low accelerometer movements.
If the device is plugged in it does not doze, but to achieve the
no-friction goal we should not be required to take it out of our
pocket and plug in all the time for fear of missing points.
master
voussoir 2023-04-03 19:03:05 -07:00
parent f55d683fe7
commit 548dcf2c58
4 changed files with 41 additions and 14 deletions

View File

@ -14,6 +14,7 @@
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- DANGEROUS PERMISSIONS, must request --> <!-- DANGEROUS PERMISSIONS, must request -->
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" /> <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

View File

@ -678,6 +678,7 @@ class MapFragment : Fragment()
watchdog: ${iso8601_local_noms(tracker.last_watchdog)} watchdog: ${iso8601_local_noms(tracker.last_watchdog)}
died: ${iso8601_local_noms(tracker.gave_up_at)} died: ${iso8601_local_noms(tracker.gave_up_at)}
power: ${tracker.device_is_charging} power: ${tracker.device_is_charging}
wakelock: ${tracker.wakelock.isHeld}
""".trimIndent() """.trimIndent()
} }
else else

View File

@ -90,8 +90,11 @@ class TrackerService: Service()
var device_is_charging: Boolean = false var device_is_charging: Boolean = false
private var charging_broadcast_receiver: BroadcastReceiver? = null private var charging_broadcast_receiver: BroadcastReceiver? = null
lateinit var wakelock: PowerManager.WakeLock
private fun addGpsLocationListener(interval: Long): Boolean private fun addGpsLocationListener(interval: Long): Boolean
{ {
gpsLocationListenerRegistered = false
gpsProviderActive = isGpsEnabled(locationManager) gpsProviderActive = isGpsEnabled(locationManager)
if (! gpsProviderActive) if (! gpsProviderActive)
{ {
@ -119,6 +122,7 @@ class TrackerService: Service()
private fun addNetworkLocationListener(interval: Long): Boolean private fun addNetworkLocationListener(interval: Long): Boolean
{ {
networkLocationListenerRegistered = false
networkProviderActive = isNetworkEnabled(locationManager) networkProviderActive = isNetworkEnabled(locationManager)
if (!networkProviderActive) if (!networkProviderActive)
{ {
@ -176,11 +180,9 @@ class TrackerService: Service()
{ {
Log.i("VOUSSOIR", "TrackerService.reset_location_listeners") Log.i("VOUSSOIR", "TrackerService.reset_location_listeners")
location_interval = interval location_interval = interval
var gps_added = false
var network_added = false
if (use_gps_location && interval != Keys.LOCATION_INTERVAL_DEAD && interval != Keys.LOCATION_INTERVAL_STOP) if (use_gps_location && interval != Keys.LOCATION_INTERVAL_DEAD && interval != Keys.LOCATION_INTERVAL_STOP)
{ {
gps_added = addGpsLocationListener(interval) addGpsLocationListener(interval)
} }
else if (gpsLocationListenerRegistered) else if (gpsLocationListenerRegistered)
{ {
@ -188,14 +190,19 @@ class TrackerService: Service()
} }
if (use_network_location && interval != Keys.LOCATION_INTERVAL_DEAD && interval != Keys.LOCATION_INTERVAL_STOP) if (use_network_location && interval != Keys.LOCATION_INTERVAL_DEAD && interval != Keys.LOCATION_INTERVAL_STOP)
{ {
network_added = addNetworkLocationListener(interval) addNetworkLocationListener(interval)
} }
else if (networkLocationListenerRegistered) else if (networkLocationListenerRegistered)
{ {
removeNetworkLocationListener() removeNetworkLocationListener()
} }
if (gps_added || network_added) if (interval != Keys.LOCATION_INTERVAL_DEAD)
{
gave_up_at = 0
}
if (gpsLocationListenerRegistered || networkLocationListenerRegistered)
{ {
listeners_enabled_at = System.currentTimeMillis() listeners_enabled_at = System.currentTimeMillis()
if (interval != Keys.LOCATION_INTERVAL_SLEEP) if (interval != Keys.LOCATION_INTERVAL_SLEEP)
@ -212,6 +219,21 @@ class TrackerService: Service()
listeners_enabled_at = 0 listeners_enabled_at = 0
location_interval = Keys.LOCATION_INTERVAL_DEAD location_interval = Keys.LOCATION_INTERVAL_DEAD
} }
if (
(gpsLocationListenerRegistered || networkLocationListenerRegistered) &&
trackingState == Keys.STATE_TRACKING_ACTIVE &&
interval == Keys.LOCATION_INTERVAL_FULL_POWER &&
!wakelock.isHeld
)
{
wakelock.acquire()
}
else if (wakelock.isHeld)
{
wakelock.release()
}
displayNotification() displayNotification()
} }
@ -598,6 +620,9 @@ class TrackerService: Service()
charging_intent_filter.addAction(Intent.ACTION_POWER_DISCONNECTED) charging_intent_filter.addAction(Intent.ACTION_POWER_DISCONNECTED)
registerReceiver(charging_broadcast_receiver, charging_intent_filter) registerReceiver(charging_broadcast_receiver, charging_intent_filter)
val powermanager = getSystemService(Context.POWER_SERVICE) as PowerManager
wakelock = powermanager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "trkpt::wakelock")
handler.post(background_watchdog) handler.post(background_watchdog)
} }
@ -647,8 +672,8 @@ class TrackerService: Service()
fun startTracking() fun startTracking()
{ {
Log.i("VOUSSOIR", "TrackerService.startTracking") Log.i("VOUSSOIR", "TrackerService.startTracking")
reset_location_listeners(interval=Keys.LOCATION_INTERVAL_FULL_POWER)
trackingState = Keys.STATE_TRACKING_ACTIVE trackingState = Keys.STATE_TRACKING_ACTIVE
reset_location_listeners(interval=Keys.LOCATION_INTERVAL_FULL_POWER)
PreferencesHelper.saveTrackingState(trackingState) PreferencesHelper.saveTrackingState(trackingState)
recent_displacement_locations.clear() recent_displacement_locations.clear()
startForeground(Keys.TRACKER_SERVICE_NOTIFICATION_ID, displayNotification()) startForeground(Keys.TRACKER_SERVICE_NOTIFICATION_ID, displayNotification())
@ -658,8 +683,8 @@ class TrackerService: Service()
{ {
Log.i("VOUSSOIR", "TrackerService.stopTracking") Log.i("VOUSSOIR", "TrackerService.stopTracking")
trackbook.database.commit() trackbook.database.commit()
reset_location_listeners(interval=Keys.LOCATION_INTERVAL_FULL_POWER)
trackingState = Keys.STATE_TRACKING_STOPPED trackingState = Keys.STATE_TRACKING_STOPPED
reset_location_listeners(interval=Keys.LOCATION_INTERVAL_FULL_POWER)
PreferencesHelper.saveTrackingState(trackingState) PreferencesHelper.saveTrackingState(trackingState)
recent_displacement_locations.clear() recent_displacement_locations.clear()
displayNotification() displayNotification()

View File

@ -92,7 +92,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:gravity="right" android:gravity="left"
app:fontFamily="monospace" app:fontFamily="monospace"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"