Skip to content

Colota - Android GPS Location tracker

Fix thread safety and remove dead code in native layer

Max Dietrich12cb7b90a5e3
diff --git a/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt b/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
index 7c9f5027..3d304ee4 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/data/DatabaseHelper.kt
@@ -489,16 +489,6 @@ class DatabaseHelper private constructor(context: Context) :
}
}
- fun getTodayCount(): Int {
- val todayStart = getTodayStartTimestamp()
- return readableDatabase.rawQuery(
- "SELECT COUNT(*) FROM $TABLE_LOCATIONS WHERE timestamp >= ?",
- arrayOf(todayStart.toString())
- ).use {
- if (it.moveToFirst()) it.getInt(0) else 0
- }
- }
-
fun getDatabaseSizeMB(): Double {
val dbPath = readableDatabase.path ?: return 0.0
return java.io.File(dbPath).length() / (1024.0 * 1024.0)
@@ -514,22 +504,12 @@ class DatabaseHelper private constructor(context: Context) :
}
fun clearQueue(): Int {
- // Collect location IDs before deleting queue entries
- val locationIds = mutableListOf<Long>()
- readableDatabase.rawQuery("SELECT location_id FROM $TABLE_QUEUE", null).use { cursor ->
- while (cursor.moveToNext()) locationIds.add(cursor.getLong(0))
- }
-
- val deleted = writableDatabase.delete(TABLE_QUEUE, null, null)
-
- // Remove the unsent locations
- if (locationIds.isNotEmpty()) {
- val placeholders = locationIds.joinToString(",") { "?" }
- val args = locationIds.map { it.toString() }.toTypedArray()
- writableDatabase.delete(TABLE_LOCATIONS, "id IN ($placeholders)", args)
- }
-
- return deleted
+ // Deleting locations cascades to their queue entries via FK ON DELETE CASCADE
+ return writableDatabase.delete(
+ TABLE_LOCATIONS,
+ "id IN (SELECT location_id FROM $TABLE_QUEUE)",
+ null
+ )
}
fun clearAllLocations(): Int {
diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt b/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
index f5d00969..b78908fe 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
@@ -50,9 +50,9 @@ class LocationForegroundService : Service() {
@Volatile private var currentZoneName: String? = null
@Volatile private var lastKnownLocation: android.location.Location? = null
- private lateinit var config: ServiceConfig
+ @Volatile private lateinit var config: ServiceConfig
private var fieldMap: Map<String, String>? = null
- private var customFields: Map<String, String>? = null
+ @Volatile private var customFields: Map<String, String>? = null
companion object {
private const val TAG = "LocationService"
@@ -410,21 +410,14 @@ class LocationForegroundService : Service() {
insidePauseZone = true
currentZoneName = zoneName
- lastKnownLocation?.let { loc ->
- updateNotification(
- lat = loc.latitude,
- lon = loc.longitude,
- pausedInZone = true,
- zoneName = zoneName,
- forceUpdate = true
- )
- } ?: run {
- updateNotification(
- pausedInZone = true,
- zoneName = zoneName,
- forceUpdate = true
- )
- }
+ val loc = lastKnownLocation
+ updateNotification(
+ lat = loc?.latitude,
+ lon = loc?.longitude,
+ pausedInZone = true,
+ zoneName = zoneName,
+ forceUpdate = true
+ )
LocationServiceModule.sendPauseZoneEvent(true, zoneName)
@@ -439,11 +432,8 @@ class LocationForegroundService : Service() {
val exited = currentZoneName
currentZoneName = null
- lastKnownLocation?.let { loc ->
- updateNotification(loc.latitude, loc.longitude, forceUpdate = true)
- } ?: run {
- updateNotification(forceUpdate = true)
- }
+ val loc = lastKnownLocation
+ updateNotification(lat = loc?.latitude, lon = loc?.longitude, forceUpdate = true)
LocationServiceModule.sendPauseZoneEvent(false, exited)
if (BuildConfig.DEBUG) Log.d(TAG, "Exited pause zone: $exited")
@@ -533,9 +523,8 @@ class LocationForegroundService : Service() {
}
// Update notification to show/hide profile name in title
- lastKnownLocation?.let {
- updateNotification(lat = it.latitude, lon = it.longitude, forceUpdate = true)
- } ?: updateNotification(forceUpdate = true)
+ val loc = lastKnownLocation
+ updateNotification(lat = loc?.latitude, lon = loc?.longitude, forceUpdate = true)
if (BuildConfig.DEBUG) {
Log.i(TAG, "Profile config applied: ${profileManager.getActiveProfileName() ?: "default"} — interval=${interval}ms, distance=${distance}m, sync=${syncInterval}s")
diff --git a/apps/mobile/android/app/src/main/java/com/colota/util/DeviceInfoHelper.kt b/apps/mobile/android/app/src/main/java/com/colota/util/DeviceInfoHelper.kt
index 148fa944..ec6af003 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/util/DeviceInfoHelper.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/util/DeviceInfoHelper.kt
@@ -36,19 +36,12 @@ class DeviceInfoHelper(private val context: Context) {
putString("model", Build.MODEL)
putString("brand", Build.BRAND)
putString("manufacturer", Build.MANUFACTURER)
- putString("device", Build.DEVICE)
putString("deviceId", Build.DEVICE)
putString("systemVersion", Build.VERSION.RELEASE)
putInt("apiLevel", Build.VERSION.SDK_INT)
}
}
- fun getSystemVersion(): String = Build.VERSION.RELEASE
- fun getApiLevel(): Int = Build.VERSION.SDK_INT
- fun getModel(): String = Build.MODEL
- fun getBrand(): String = Build.BRAND
- fun getDeviceId(): String = Build.DEVICE
-
/**
* @return Pair of (battery level %, status code: 0=Unknown, 1=Discharging, 2=Charging, 3=Full)
*/