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.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- DANGEROUS PERMISSIONS, must request -->
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

View File

@ -671,13 +671,14 @@ class MapFragment : Fragment()
if (show_debug)
{
map_current_time.text = """
now: ${iso8601_local_noms(System.currentTimeMillis())}
location: ${iso8601_local_noms(tracker.currentBestLocation.time)}
now: ${iso8601_local_noms(System.currentTimeMillis())}
location: ${iso8601_local_noms(tracker.currentBestLocation.time)}
listeners: ${iso8601_local_noms(tracker.listeners_enabled_at)}
motion: ${iso8601_local_noms(tracker.last_significant_motion)}
watchdog: ${iso8601_local_noms(tracker.last_watchdog)}
died: ${iso8601_local_noms(tracker.gave_up_at)}
power: ${tracker.device_is_charging}
motion: ${iso8601_local_noms(tracker.last_significant_motion)}
watchdog: ${iso8601_local_noms(tracker.last_watchdog)}
died: ${iso8601_local_noms(tracker.gave_up_at)}
power: ${tracker.device_is_charging}
wakelock: ${tracker.wakelock.isHeld}
""".trimIndent()
}
else

View File

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

View File

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