Modern Apps git

← Modern-Apps.git

findfamily: add Quick Settings tile to toggle tracking and hide self sharing (#487)

Commit
500f7ab5dad5e9d209856bd865003ef46e0dfe21
Author
Vayun Mathur <[email protected]>
When
2026-08-12 16:45 UTC (3 months ago)
Parents
e083ed2

Diff

findfamily/src/main/AndroidManifest.xml
diff --git a/findfamily/src/main/AndroidManifest.xml b/findfamily/src/main/AndroidManifest.xmlindex eb67447385..b9d9e3b521 100644--- a/findfamily/src/main/AndroidManifest.xml+++ b/findfamily/src/main/AndroidManifest.xml@@ -63,6 +63,18 @@             android:foregroundServiceType="location"             android:exported="false" /> +        <!-- Quick Settings tile to turn location tracking on/off (GitHub #487). -->+        <service+            android:name=".util.TrackingTileService"+            android:exported="true"+            android:icon="@drawable/ic_tile_tracking"+            android:label="@string/tile_label"+            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">+            <intent-filter>+                <action android:name="android.service.quicksettings.action.QS_TILE" />+            </intent-filter>+        </service>+          <receiver android:name=".util.BootReceiver" android:exported="false">             <intent-filter>
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 fea9ed0ff9..df59447e10 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt@@ -592,24 +592,29 @@ fun PersonDetailSheet(state: PersonUiState, actions: PersonActions) {     Column(Modifier.fillMaxWidth().navigationBarsPadding().padding(horizontal = 12.dp, vertical = 4.dp)) {         UserCard(user, state.location, true) {}         Spacer(Modifier.height(8.dp))-        Row(-            Modifier.fillMaxWidth().padding(horizontal = 4.dp),-            verticalAlignment = Alignment.CenterVertically-        ) {-            Text(-                stringResource(R.string.share_your_location),-                Modifier.weight(1f),-                style = MaterialTheme.typography.bodyMedium-            )-            Switch(-                user.sendingEnabled,-                { send -> actions.setUserSharing(user, send) }-            )+        // Sharing controls are hidden for your own entry: you can't "stop sharing with+        // yourself", and doing so per-person was a confusing way to try to turn the+        // service off. Use the Quick Settings tile to disable tracking (GitHub #487).+        if (user.id != Networking.userid) {+            Row(+                Modifier.fillMaxWidth().padding(horizontal = 4.dp),+                verticalAlignment = Alignment.CenterVertically+            ) {+                Text(+                    stringResource(R.string.share_your_location),+                    Modifier.weight(1f),+                    style = MaterialTheme.typography.bodyMedium+                )+                Switch(+                    user.sendingEnabled,+                    { send -> actions.setUserSharing(user, send) }+                )+            }+            // Auto-toggle: "Turn on/off after" + duration dropdown (Never default)+            Spacer(Modifier.height(4.dp))+            AutoToggleRow(user, actions)+            Spacer(Modifier.height(4.dp))         }-        // Auto-toggle: "Turn on/off after" + duration dropdown (Never default)-        Spacer(Modifier.height(4.dp))-        AutoToggleRow(user, actions)-        Spacer(Modifier.height(4.dp))         OutlinedButton(             { actions.changeConnectedContact() },             Modifier.fillMaxWidth()
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 13c43f54e5..3c7774a190 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt@@ -700,22 +700,35 @@ class ServiceRestartWorker(  * Single source of truth for whether the [LocationTrackingService] should be  * running and for (re)starting / stopping it accordingly.  *- * The service must run whenever fine (precise) location permission is granted,- * regardless of whether location sharing is enabled. Sharing toggles are- * enforced inside the heartbeat (we only publish to users with- * sendingEnabled=true) rather than by stopping the service, so that UWB- * inbox draining, waypoint entry/exit, low-battery alerts, and receiving peers'- * locations continue to work even when the user pauses sharing or on fresh- * install before any contact is added.+ * The service runs whenever fine (precise) location permission is granted **and**+ * the user hasn't turned tracking off (the [TRACKING_ENABLED_KEY] flag, toggled+ * from the Quick Settings tile — see TrackingTileService). Sharing toggles are+ * enforced inside the heartbeat (we only publish to users with sendingEnabled=true)+ * rather than by stopping the service, so that UWB inbox draining, waypoint+ * entry/exit, low-battery alerts, and receiving peers' locations continue to work+ * even when the user pauses sharing or on fresh install before any contact is added.  */ object LocationServiceController { +    /** Persisted on/off switch for the whole tracking service (default on). */+    const val TRACKING_ENABLED_KEY = "tracking_enabled"+     fun hasFineLocationPermission(context: Context): Boolean =         ContextCompat.checkSelfPermission(             context,             Manifest.permission.ACCESS_FINE_LOCATION         ) == PackageManager.PERMISSION_GRANTED +    /** Whether the user has left tracking enabled. Defaults to true (opt-out, not opt-in). */+    suspend fun isTrackingEnabled(context: Context): Boolean =+        DataStoreUtils.getInstance(context).getBooleanAwait(TRACKING_ENABLED_KEY, true)++    /** Persist the tracking on/off choice and immediately start/stop the service to match. */+    suspend fun setTrackingEnabled(context: Context, enabled: Boolean) {+        DataStoreUtils.getInstance(context).setBoolean(TRACKING_ENABLED_KEY, enabled)+        syncServiceState(context)+    }+     /**      * True iff the user is sharing their location with at least one *other*      * person (the self user is excluded). Reads directly from the DB so the@@ -730,11 +743,12 @@ object LocationServiceController {      /**      * Start the service if eligible, otherwise make sure it is stopped. Safe to-     * call from any context (worker, boot, ViewModel, permission refresh).+     * call from any context (worker, boot, ViewModel, permission refresh, tile).+     * Eligible = fine-location permission granted AND tracking not turned off.      */     suspend fun syncServiceState(context: Context) {         val appContext = context.applicationContext-        val eligible = hasFineLocationPermission(appContext)+        val eligible = hasFineLocationPermission(appContext) && isTrackingEnabled(appContext)         val intent = Intent(appContext, LocationTrackingService::class.java)         withContext(Dispatchers.Main) {             if (eligible) {
findfamily/src/main/java/com/vayunmathur/findfamily/util/TrackingTileService.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/TrackingTileService.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/TrackingTileService.ktnew file mode 100644index 0000000000..5c13ae80e4--- /dev/null+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/TrackingTileService.kt@@ -0,0 +1,84 @@+package com.vayunmathur.findfamily.util++import android.app.PendingIntent+import android.content.Intent+import android.os.Build+import android.service.quicksettings.Tile+import android.service.quicksettings.TileService+import com.vayunmathur.findfamily.MainActivity+import com.vayunmathur.findfamily.R+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob+import kotlinx.coroutines.cancel+import kotlinx.coroutines.launch++/**+ * Quick Settings tile that turns the whole FindFamily location-tracking service on+ * and off (GitHub #487). Tapping it flips the persisted [LocationServiceController.TRACKING_ENABLED_KEY]+ * flag and immediately starts/stops the foreground service; boot and the periodic+ * restart worker both honor the same flag via [LocationServiceController.syncServiceState].+ *+ * If fine-location permission isn't granted the tile can't run tracking, so tapping it+ * opens the app to grant permission instead of toggling.+ */+class TrackingTileService : TileService() {++    private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)++    override fun onStartListening() {+        super.onStartListening()+        refreshTile()+    }++    override fun onTileAdded() {+        super.onTileAdded()+        refreshTile()+    }++    override fun onClick() {+        super.onClick()+        if (!LocationServiceController.hasFineLocationPermission(this)) {+            // Tracking can't run without location permission — send the user to the app to grant it.+            openApp()+            return+        }+        scope.launch {+            val enabled = LocationServiceController.isTrackingEnabled(this@TrackingTileService)+            LocationServiceController.setTrackingEnabled(this@TrackingTileService, !enabled)+            refreshTile()+        }+    }++    private fun refreshTile() {+        scope.launch {+            val tile = qsTile ?: return@launch+            val hasPermission = LocationServiceController.hasFineLocationPermission(this@TrackingTileService)+            val enabled = LocationServiceController.isTrackingEnabled(this@TrackingTileService)+            tile.state = if (enabled && hasPermission) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE+            tile.subtitle = when {+                !hasPermission -> getString(R.string.tile_subtitle_no_permission)+                enabled -> getString(R.string.tile_subtitle_on)+                else -> getString(R.string.tile_subtitle_off)+            }+            tile.updateTile()+        }+    }++    private fun openApp() {+        val intent = Intent(this, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+        if (Build.VERSION.SDK_INT >= 34) {+            startActivityAndCollapse(+                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)+            )+        } else {+            @Suppress("DEPRECATION")+            startActivityAndCollapse(intent)+        }+    }++    override fun onDestroy() {+        super.onDestroy()+        scope.cancel()+    }+}
findfamily/src/main/res/drawable/ic_tile_tracking.xml
diff --git a/findfamily/src/main/res/drawable/ic_tile_tracking.xml b/findfamily/src/main/res/drawable/ic_tile_tracking.xmlnew file mode 100644index 0000000000..dbf030818a--- /dev/null+++ b/findfamily/src/main/res/drawable/ic_tile_tracking.xml@@ -0,0 +1,11 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android"+    android:width="24dp"+    android:height="24dp"+    android:viewportWidth="24"+    android:viewportHeight="24"+    android:tint="?android:attr/colorControlNormal">+    <!-- Location pin; monochrome so the Quick Settings tile can tint it by state. -->+    <path+        android:fillColor="#FFFFFFFF"+        android:pathData="M12,2C8.13,2 5,5.13 5,9c0,5.25 7,13 7,13s7,-7.75 7,-13c0,-3.87 -3.13,-7 -7,-7zM12,11.5c-1.38,0 -2.5,-1.12 -2.5,-2.5s1.12,-2.5 2.5,-2.5 2.5,1.12 2.5,2.5 -1.12,2.5 -2.5,2.5z" />+</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 7589dc80e5..1f22d620c6 100644--- a/findfamily/src/main/res/values/strings.xml+++ b/findfamily/src/main/res/values/strings.xml@@ -261,4 +261,13 @@     <!-- OutdatedPeerDialog: Body telling the user the other person must update their app before sharing can work. Sentence -->     <string name="outdated_peer_body">This person is using an older version of FindFamily that doesn\'t support quantum-safe encryption. Ask them to update their app, then try connecting again.</string> +    <!-- TrackingTileService: Quick Settings tile label. Short noun phrase -->+    <string name="tile_label">FindFamily tracking</string>+    <!-- TrackingTileService: tile subtitle when tracking is on. Short status -->+    <string name="tile_subtitle_on">On</string>+    <!-- TrackingTileService: tile subtitle when tracking is off. Short status -->+    <string name="tile_subtitle_off">Off</string>+    <!-- TrackingTileService: tile subtitle when location permission is missing. Short status -->+    <string name="tile_subtitle_no_permission">Tap to set up</string>+ </resources>