Skip to content

Colota - Android GPS Location tracker

refactor: extract shared TrackingControl from shortcut + broadcast triggers (#443)

Max Dietrich50ae662b406d
diff --git a/apps/docs/docs/development/architecture.md b/apps/docs/docs/development/architecture.md
index 2f913aac..bc560ee5 100644
--- a/apps/docs/docs/development/architecture.md
+++ b/apps/docs/docs/development/architecture.md
@@ -273,7 +273,9 @@ For backups, two `internal` methods support the export/import flow without expos
| `AutoExportScheduler` | Arms `AlarmManager.setAndAllowWhileIdle` for the next configured wall-clock time. Called on enable, after each worker run, after schedule edits and on boot |
| `AutoExportConfig` | Typed data class wrapping auto-export settings (interval, time-of-day, weekday, day-of-month, enabledAt) from the SQLite settings table with validation, `isExportDue()` and `nextExportTimestamp()` |
| `ExportConverters` | Native CSV/GeoJSON/GPX/KML serialization for both "Export all" (flat, streamed via `exportToFile`) and per-trip / multi-select export (trip-segmented via `convertTrips`, reached through the `exportTripsToFile` bridge). In-memory, streaming, and file-based interfaces |
-| `ShortcutHandlerActivity` | Handles app shortcut intents (start/stop tracking) without showing UI - reads config from DB via `ServiceConfig.fromDatabase()` and dispatches to `LocationForegroundService` |
+| `ShortcutHandlerActivity` | Transparent activity handling app shortcut intents (start/stop tracking) without UI. Delegates to the shared `TrackingControl` helper |
+| `TrackingControlReceiver` | Exported broadcast receiver for automation apps - `com.Colota.action.START_TRACKING` / `STOP_TRACKING` start or stop tracking from saved settings. Delegates to `TrackingControl` |
+| `TrackingControl` | Shared start/stop tracking actions used by both triggers above: reads config from DB via `ServiceConfig.fromDatabase()`, starts the foreground service and fires the started event; stop is routed through the service so `stopForegroundServiceWithReason` runs |
## React Native Layer
diff --git a/apps/mobile/android/app/src/main/java/com/colota/triggers/ShortcutHandlerActivity.kt b/apps/mobile/android/app/src/main/java/com/colota/triggers/ShortcutHandlerActivity.kt
index ab067932..d6b8ca27 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/triggers/ShortcutHandlerActivity.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/triggers/ShortcutHandlerActivity.kt
@@ -7,12 +7,7 @@ package com.Colota.triggers
import android.app.Activity
import android.content.Context
-import android.content.Intent
import android.os.Bundle
-import com.Colota.bridge.LocationServiceModule
-import com.Colota.data.DatabaseHelper
-import com.Colota.service.LocationForegroundService
-import com.Colota.service.ServiceConfig
import com.Colota.util.AppLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -52,20 +47,11 @@ class ShortcutHandlerActivity : Activity() {
private fun handleStart(context: Context) {
AppLogger.d(TAG, "Shortcut: start tracking")
- val config = ServiceConfig.fromDatabase(DatabaseHelper.getInstance(context))
- val serviceIntent = config.toIntent(
- Intent(context, LocationForegroundService::class.java)
- )
- context.startForegroundService(serviceIntent)
- LocationServiceModule.sendTrackingStartedEvent("Started via shortcut")
+ TrackingControl.start(context, "Started via shortcut")
}
- private fun handleStop(context: android.content.Context) {
+ private fun handleStop(context: Context) {
AppLogger.d(TAG, "Shortcut: stop tracking")
- val stopIntent = Intent(context, LocationForegroundService::class.java).apply {
- action = LocationForegroundService.ACTION_STOP_REQUEST
- putExtra(LocationForegroundService.EXTRA_STOP_REASON, "Stopped via shortcut")
- }
- context.startService(stopIntent)
+ TrackingControl.stop(context, "Stopped via shortcut")
}
}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/triggers/TrackingControl.kt b/apps/mobile/android/app/src/main/java/com/colota/triggers/TrackingControl.kt
new file mode 100644
index 00000000..b6f6be26
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/triggers/TrackingControl.kt
@@ -0,0 +1,36 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.triggers
+
+import android.content.Context
+import android.content.Intent
+import com.Colota.bridge.LocationServiceModule
+import com.Colota.data.DatabaseHelper
+import com.Colota.service.LocationForegroundService
+import com.Colota.service.ServiceConfig
+
+/**
+ * Shared start/stop tracking actions for external triggers (app shortcut,
+ * automation broadcast). Callers pass the source label; per-caller extras
+ * (logging, toasts) stay in the caller.
+ */
+object TrackingControl {
+ fun start(context: Context, startedReason: String) {
+ val config = ServiceConfig.fromDatabase(DatabaseHelper.getInstance(context))
+ context.startForegroundService(config.toIntent(Intent(context, LocationForegroundService::class.java)))
+ LocationServiceModule.sendTrackingStartedEvent(startedReason)
+ }
+
+ // Route stop through the service so stopForegroundServiceWithReason runs - direct
+ // stopService skips the pause-zone flag clearing and the stopped notification.
+ fun stop(context: Context, stopReason: String) {
+ val stopIntent = Intent(context, LocationForegroundService::class.java).apply {
+ action = LocationForegroundService.ACTION_STOP_REQUEST
+ putExtra(LocationForegroundService.EXTRA_STOP_REASON, stopReason)
+ }
+ context.startService(stopIntent)
+ }
+}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/triggers/TrackingControlReceiver.kt b/apps/mobile/android/app/src/main/java/com/colota/triggers/TrackingControlReceiver.kt
index 6328bfce..7125c74f 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/triggers/TrackingControlReceiver.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/triggers/TrackingControlReceiver.kt
@@ -11,10 +11,6 @@ import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.widget.Toast
-import com.Colota.bridge.LocationServiceModule
-import com.Colota.data.DatabaseHelper
-import com.Colota.service.LocationForegroundService
-import com.Colota.service.ServiceConfig
import com.Colota.util.AppLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -54,24 +50,13 @@ class TrackingControlReceiver : BroadcastReceiver() {
private fun handleStart(context: Context) {
AppLogger.d(TAG, "Broadcast: start tracking")
- val config = ServiceConfig.fromDatabase(DatabaseHelper.getInstance(context))
- val serviceIntent = config.toIntent(
- Intent(context, LocationForegroundService::class.java)
- )
- context.startForegroundService(serviceIntent)
- LocationServiceModule.sendTrackingStartedEvent("Started via automation intent")
+ TrackingControl.start(context, "Started via automation intent")
toastOnMain(context, "Colota tracking started")
}
- // Route stop through the service so stopForegroundServiceWithReason runs.
- // Direct stopService skips the pause-zone flag clearing and stopped notification.
private fun handleStop(context: Context) {
AppLogger.d(TAG, "Broadcast: stop tracking")
- val stopIntent = Intent(context, LocationForegroundService::class.java).apply {
- action = LocationForegroundService.ACTION_STOP_REQUEST
- putExtra(LocationForegroundService.EXTRA_STOP_REASON, "Stopped via automation intent")
- }
- context.startService(stopIntent)
+ TrackingControl.stop(context, "Stopped via automation intent")
toastOnMain(context, "Colota tracking stopped")
}