Colota - Android GPS Location tracker
fix(tracking): restart tracking after app update (#486)
Max Dietrich46caca28027a
diff --git a/apps/mobile/android/app/src/main/AndroidManifest.xml b/apps/mobile/android/app/src/main/AndroidManifest.xmlindex b109a793..354a980c 100644--- a/apps/mobile/android/app/src/main/AndroidManifest.xml+++ b/apps/mobile/android/app/src/main/AndroidManifest.xml@@ -99,13 +99,14 @@android:foregroundServiceType="shortService"android:exported="false" />- <!-- Boot Receiver -->+ <!-- Tracking restart receiver (boot / app update) --><receiverandroid:name=".triggers.LocationBootReceiver"android:enabled="true"android:exported="false"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" />+ <action android:name="android.intent.action.MY_PACKAGE_REPLACED" /><category android:name="android.intent.CATEGORY_DEFAULT" /></intent-filter></receiver>diff --git a/apps/mobile/android/app/src/main/java/com/colota/triggers/BootReceiver.kt b/apps/mobile/android/app/src/main/java/com/colota/triggers/BootReceiver.ktindex fa604c6a..49be2dbb 100644--- a/apps/mobile/android/app/src/main/java/com/colota/triggers/BootReceiver.kt+++ b/apps/mobile/android/app/src/main/java/com/colota/triggers/BootReceiver.kt@@ -20,15 +20,15 @@ import com.Colota.util.DeviceInfoHelperimport kotlinx.coroutines.*/**- * Receiver responsible for restarting the location tracking service after device reboot.+ * Receiver responsible for restarting the location tracking service after a device+ * reboot or after the app is updated.*/class LocationBootReceiver : BroadcastReceiver() {-companion object {private const val TAG = "BootReceiver"-- private val BOOT_ACTIONS = setOf(- Intent.ACTION_BOOT_COMPLETED+ private val HANDLED_ACTIONS = setOf(+ Intent.ACTION_BOOT_COMPLETED,+ Intent.ACTION_MY_PACKAGE_REPLACED)private const val MAX_DB_RETRIES = 3@@ -38,8 +38,7 @@ class LocationBootReceiver : BroadcastReceiver() {override fun onReceive(context: Context, intent: Intent) {val action = intent.action ?: return- // Check if this is a boot-related action- if (action !in BOOT_ACTIONS) return+ if (action !in HANDLED_ACTIONS) return// Use goAsync() to prevent ANR during database accessval pendingResult = goAsync()@@ -97,7 +96,7 @@ class LocationBootReceiver : BroadcastReceiver() {val isEnabled = dbHelper.getSetting(SettingsKeys.TRACKING_ENABLED, "false") == "true"if (!isEnabled) {- AppLogger.d(TAG, "Boot detected but tracking disabled")+ AppLogger.d(TAG, "Trigger received but tracking disabled")// Charger auto-resume: only if the stop was battery-triggered (not a user stop).val stoppedByBattery = dbHelper.getSetting(SettingsKeys.STOPPED_BY_BATTERY, "false") == "true"@@ -121,9 +120,10 @@ class LocationBootReceiver : BroadcastReceiver() {return}- AppLogger.d(TAG, "Boot detected ($action). Restarting service...")+ AppLogger.d(TAG, "Trigger received ($action). Restarting service...")- LocationForegroundService.startTracking(context, dbHelper, "Device rebooted")+ val reason = if (action == Intent.ACTION_MY_PACKAGE_REPLACED) "App updated" else "Device rebooted"+ LocationForegroundService.startTracking(context, dbHelper, reason)AppLogger.d(TAG, "Service start requested successfully")diff --git a/apps/mobile/android/app/src/test/java/com/Colota/triggers/LocationBootReceiverTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/triggers/LocationBootReceiverTest.ktindex c2b1676f..c9151016 100644--- a/apps/mobile/android/app/src/test/java/com/Colota/triggers/LocationBootReceiverTest.kt+++ b/apps/mobile/android/app/src/test/java/com/Colota/triggers/LocationBootReceiverTest.kt@@ -10,6 +10,7 @@ import android.content.Contextimport android.content.Intentimport com.Colota.data.DatabaseHelperimport com.Colota.service.BatteryRecoveryScheduler+import com.Colota.service.LocationForegroundServiceimport com.Colota.service.NotificationHelperimport com.Colota.util.AppLoggerimport com.Colota.util.DeviceInfoHelper@@ -24,7 +25,7 @@ import kotlin.coroutines.Continuation/*** Tests for LocationBootReceiver:- * - onReceive action filtering (null, unknown, valid boot actions)+ * - onReceive action filtering (null, unknown, boot and app-update actions)* - handleBootCompleted logic (tracking disabled/enabled, DB not ready, SecurityException)* - waitForDatabaseReady retry logic*/@@ -100,6 +101,22 @@ class LocationBootReceiverTest {verify { pendingResult.finish() }}+ @Test+ fun `onReceive calls goAsync for ACTION_MY_PACKAGE_REPLACED`() {+ val spy = spyk(receiver)+ val pendingResult = mockk<BroadcastReceiver.PendingResult>(relaxed = true)+ every { spy.goAsync() } returns pendingResult+ every { mockContext.applicationContext } returns mockContext+ mockDbReadyAndDisabled()++ val intent = mockk<Intent> { every { action } returns Intent.ACTION_MY_PACKAGE_REPLACED }+ spy.onReceive(mockContext, intent)+ Thread.sleep(500)++ verify { spy.goAsync() }+ verify { pendingResult.finish() }+ }+@Testfun `onReceive always calls finish even on error`() {val spy = spyk(receiver)@@ -138,6 +155,28 @@ class LocationBootReceiverTest {verify { mockContext.startForegroundService(any()) }}+ @Test+ fun `handleBootCompleted passes 'App updated' reason on app update`() = runTest {+ mockkObject(LocationForegroundService.Companion)+ every { LocationForegroundService.startTracking(any(), any(), any()) } just Runs+ mockDbReadyAndEnabled()++ callHandleBootCompleted(mockContext, Intent.ACTION_MY_PACKAGE_REPLACED)++ verify { LocationForegroundService.startTracking(any(), any(), "App updated") }+ }++ @Test+ fun `handleBootCompleted passes 'Device rebooted' reason on boot`() = runTest {+ mockkObject(LocationForegroundService.Companion)+ every { LocationForegroundService.startTracking(any(), any(), any()) } just Runs+ mockDbReadyAndEnabled()++ callHandleBootCompleted(mockContext, Intent.ACTION_BOOT_COMPLETED)++ verify { LocationForegroundService.startTracking(any(), any(), "Device rebooted") }+ }+@Testfun `handleBootCompleted loads config from database`() = runTest {mockDbReadyAndEnabled()diff --git a/fastlane/metadata/android/en-US/changelogs/41.txt b/fastlane/metadata/android/en-US/changelogs/41.txtindex f1d9fa53..1095cc9b 100644--- a/fastlane/metadata/android/en-US/changelogs/41.txt+++ b/fastlane/metadata/android/en-US/changelogs/41.txt@@ -1 +1,2 @@- Share your setup as a link: pick which settings, geofences and profiles to include from the new Share Setup screen in Settings.+- Fixed tracking stopping after an app update.