Modern Apps git

← Modern-Apps.git

findfamily: fall back to GPS with battery-drain warning when network provider is unavailable

Commit
dd03402906a193353bb843df8e9e104f34813018
Author
Vayun Mathur <[email protected]>
When
2026-08-09 16:01 UTC (3 months ago)
Parents
f0af53d

Diff

findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.ktindex 704f3c9d34..073045f514 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt@@ -5,6 +5,7 @@ import kotlinx.datetime.format.DateTimeFormat import com.vayunmathur.library.util.DateNameStyle import android.content.Context import androidx.activity.compose.BackHandler+import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable@@ -84,6 +85,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.lerp import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource+import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.style.TextOverflow@@ -100,6 +102,7 @@ import com.vayunmathur.findfamily.data.User import com.vayunmathur.findfamily.data.Waypoint import com.vayunmathur.findfamily.data.toGeoPoint import com.vayunmathur.findfamily.ui.dialogs.encodeBase26+import com.vayunmathur.findfamily.ui.dialogs.GpsFallbackWarningDialog import com.vayunmathur.findfamily.ui.dialogs.SecurityCodeDialog import com.vayunmathur.findfamily.util.FamilyListActions import com.vayunmathur.findfamily.util.FamilyListUiState@@ -167,6 +170,8 @@ fun MainPage(     val isShowingPresent by ffViewModel.isShowingPresent.collectAsState()     val historicalPosition by ffViewModel.historicalPosition.collectAsState()     var showSecurityCode by remember { mutableStateOf(false) }+    var showGpsWarning by remember { mutableStateOf(false) }+    val usingGpsFallback by ffViewModel.usingGpsFallback.collectAsState()      val waypointName by ffViewModel.waypointName.collectAsState()     val waypointRange by ffViewModel.waypointRange.collectAsState()@@ -284,6 +289,18 @@ fun MainPage(                 },                 actions = {                     if (selectedUserId == null && (selectedWaypointId == null || selectedWaypointId == 0L)) {+                        if (usingGpsFallback) {+                            IconButton({ showGpsWarning = true }) {+                                // Use Image (not the tinting IconWarning) so the+                                // custom yellow-and-red drawable keeps both colors.+                                Image(+                                    painter = painterResource(R.drawable.ic_warning_gps),+                                    contentDescription = stringResource(+                                        R.string.gps_fallback_warning_content_description+                                    )+                                )+                            }+                        }                         BackupButtons(                             dbConfigs = listOf("passwords-db" to ffViewModel.backupPassphrase),                             dbCodec = SqlCipherDbCodec,@@ -521,6 +538,10 @@ fun MainPage(         val user by ffViewModel.userByIdState(selectedUserId!!)         user?.let { SecurityCodeDialog(it, ffViewModel) { showSecurityCode = false } }     }++    if (showGpsWarning) {+        GpsFallbackWarningDialog { showGpsWarning = false }+    } }  /**
findfamily/src/main/java/com/vayunmathur/findfamily/ui/dialogs/GpsFallbackWarningDialog.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/ui/dialogs/GpsFallbackWarningDialog.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/ui/dialogs/GpsFallbackWarningDialog.ktnew file mode 100644index 0000000000..e8900bd828--- /dev/null+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/ui/dialogs/GpsFallbackWarningDialog.kt@@ -0,0 +1,24 @@+package com.vayunmathur.findfamily.ui.dialogs++import androidx.compose.runtime.Composable+import androidx.compose.ui.res.stringResource+import com.vayunmathur.library.ui.AlertDialog+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.TextButton+import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.findfamily.R++/**+ * Explains that the device has no network location provider so the app has+ * fallen back to GPS-only tracking, which can cause significant battery drain.+ * Shown when the user taps the GPS-fallback warning icon in the top bar.+ */+@Composable+fun GpsFallbackWarningDialog(onDismiss: () -> Unit) {+    AlertDialog(+        onDismissRequest = onDismiss,+        title = { Text(stringResource(R.string.gps_fallback_warning_title)) },+        text = { Text(stringResource(R.string.gps_fallback_warning_body)) },+        confirmButton = { TextButton(onClick = onDismiss) { Text(stringResource(UiR.string.done)) } }+    )+}
findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyViewModel.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyViewModel.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyViewModel.ktindex b80f109a18..bfd0089a6d 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyViewModel.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyViewModel.kt@@ -437,6 +437,13 @@ class FindFamilyViewModel(     /** The peer this session is currently with (initiator OR responder), if any. */     val uwbPeerUserId: StateFlow<Long?> = UwbSessionManager.peerUserId +    /**+     * True iff the tracking service has fallen back to GPS-only location because+     * the device has no network location provider. Backed by+     * [LocationProviderStatus]; the UI uses it to show a battery-drain warning.+     */+    val usingGpsFallback: StateFlow<Boolean> = LocationProviderStatus.usingGpsFallback+     /** User tapped "Find with Precision" on the given peer. */     fun startRanging(peerUserId: Long) {         UwbSessionManager.startAsInitiator(peerUserId)
findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationProviderStatus.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationProviderStatus.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationProviderStatus.ktnew file mode 100644index 0000000000..1afb86794a--- /dev/null+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationProviderStatus.kt@@ -0,0 +1,29 @@+package com.vayunmathur.findfamily.util++import kotlinx.coroutines.flow.MutableStateFlow+import kotlinx.coroutines.flow.StateFlow+import kotlinx.coroutines.flow.asStateFlow++/**+ * Process-global signal describing which location provider the+ * [LocationTrackingService] is currently relying on.+ *+ * Some devices (no Play Services / MicroG, GrapheneOS without network+ * location) have no `NETWORK_PROVIDER`. When that happens the service falls+ * back to GPS-only tracking, which is functional but noticeably heavier on+ * battery. This flag lets the UI surface a warning so the user understands why+ * their battery is draining faster.+ *+ * Mirrors the singleton-StateFlow pattern of [UwbSessionManager].+ */+object LocationProviderStatus {++    private val _usingGpsFallback = MutableStateFlow(false)++    /** True iff the network provider is unavailable and GPS-only fallback is active. */+    val usingGpsFallback: StateFlow<Boolean> = _usingGpsFallback.asStateFlow()++    fun setUsingGpsFallback(value: Boolean) {+        _usingGpsFallback.value = value+    }+}
findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.ktindex 05477664b0..f0965db7be 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt@@ -469,15 +469,32 @@ class LocationTrackingService : Service(), SensorEventListener {         val isLowPower = powerManager.isPowerSaveMode         val networkInterval = if (isLowPower) 30_000L else 10_000L -        try {-            locationManager.removeUpdates(networkListener)-            locationManager.requestLocationUpdates(-                LocationManager.NETWORK_PROVIDER,-                networkInterval,-                0f,-                networkListener-            )-        } catch (_: SecurityException) {+        // Devices without Play Services / MicroG (e.g. GrapheneOS) may have no+        // NETWORK_PROVIDER at all. Requesting updates from a missing provider+        // throws IllegalArgumentException, which used to crash the app on every+        // launch. Guard the request the same way startGps() guards GPS, and when+        // network location is unavailable fall back to GPS-only tracking (the+        // networkListener that would normally start GPS never fires without a+        // network provider).+        val hasNetworkProvider =+            locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)++        if (hasNetworkProvider) {+            LocationProviderStatus.setUsingGpsFallback(false)+            try {+                locationManager.removeUpdates(networkListener)+                locationManager.requestLocationUpdates(+                    LocationManager.NETWORK_PROVIDER,+                    networkInterval,+                    0f,+                    networkListener+                )+            } catch (_: SecurityException) {+            } catch (_: IllegalArgumentException) {+            }+        } else {+            LocationProviderStatus.setUsingGpsFallback(true)+            startGps()         }     } 
findfamily/src/main/res/drawable/ic_warning_gps.xml
diff --git a/findfamily/src/main/res/drawable/ic_warning_gps.xml b/findfamily/src/main/res/drawable/ic_warning_gps.xmlnew file mode 100644index 0000000000..bef4019bf1--- /dev/null+++ b/findfamily/src/main/res/drawable/ic_warning_gps.xml@@ -0,0 +1,19 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android"+    android:width="24dp"+    android:height="24dp"+    android:viewportWidth="24"+    android:viewportHeight="24">+    <!-- Yellow triangle body -->+    <path+        android:fillColor="#FFEB3B"+        android:strokeColor="#F44336"+        android:strokeWidth="1.2"+        android:pathData="M12,3 L22,20 L2,20 Z" />+    <!-- Red exclamation mark -->+    <path+        android:fillColor="#F44336"+        android:pathData="M11,9h2v6h-2z" />+    <path+        android:fillColor="#F44336"+        android:pathData="M11,16.5h2v2h-2z" />+</vector>
findfamily/src/main/res/values/strings.xml
diff --git a/findfamily/src/main/res/values/strings.xml b/findfamily/src/main/res/values/strings.xmlindex 00eae1d433..ffa2b0ec78 100644--- a/findfamily/src/main/res/values/strings.xml+++ b/findfamily/src/main/res/values/strings.xml@@ -249,4 +249,11 @@     <!-- UwbRangingScreen: Hint when azimuth null. Instruction sentence -->     <string name="point_the_back_of_your_phone_toward_your">Point the back of your phone toward your contact</string> +    <!-- MainPage: Content description for the GPS-only fallback warning icon in the top bar. Noun phrase -->+    <string name="gps_fallback_warning_content_description">Network location unavailable</string>+    <!-- GpsFallbackWarningDialog: Title of the GPS-only fallback warning dialog. Noun phrase -->+    <string name="gps_fallback_warning_title">Using GPS only</string>+    <!-- GpsFallbackWarningDialog: Body explaining network location is off and GPS drains battery. Sentence -->+    <string name="gps_fallback_warning_body">Network location isn\'t turned on for this device, so FindFamily is using GPS only to keep tracking your location. GPS is more accurate but may cause significant battery drain. To reduce battery use, turn on network (Wi-Fi &amp; mobile) location in your device settings.</string>+ </resources>