Modern Apps git

← Modern-Apps.git

findfamily: add Quick Settings tile to pause sharing (#648)

Commit
5e5f3ca0675cf655a8a9a7890dc69bc6ef25fe59
Author
vayun-mathur <[email protected]>
When
2026-09-07 00:59 UTC (3 months ago)
Parents
174c7a8

Diff

findfamily/src/main/AndroidManifest.xml
diff --git a/findfamily/src/main/AndroidManifest.xml b/findfamily/src/main/AndroidManifest.xmlindex 1b3750e685..eb37ace290 100644--- a/findfamily/src/main/AndroidManifest.xml+++ b/findfamily/src/main/AndroidManifest.xml@@ -87,6 +87,20 @@                 android:value=".MainActivity" />         </service> +        <service+            android:name=".service.SharingTileService"+            android:exported="true"+            android:icon="@drawable/ic_tile_sharing"+            android:label="@string/sharing_tile_label"+            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">+            <intent-filter>+                <action android:name="android.service.quicksettings.action.QS_TILE" />+            </intent-filter>+            <meta-data+                android:name="android.service.quicksettings.ACTIVITY"+                android:value=".MainActivity" />+        </service>+          <receiver android:name=".util.BootReceiver" android:exported="false">             <intent-filter>
findfamily/src/main/java/com/vayunmathur/findfamily/service/SharingTileService.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/service/SharingTileService.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/service/SharingTileService.ktnew file mode 100644index 0000000000..49e0017f7e--- /dev/null+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/service/SharingTileService.kt@@ -0,0 +1,75 @@+package com.vayunmathur.findfamily.service++import android.content.ComponentName+import android.content.Context+import android.service.quicksettings.Tile+import android.service.quicksettings.TileService+import com.vayunmathur.findfamily.R+import com.vayunmathur.findfamily.util.LocationServiceController+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob+import kotlinx.coroutines.cancel+import kotlinx.coroutines.launch++/**+ * Quick Settings tile that pauses and resumes publishing this device's location+ * (GitHub #648), as opposed to the tracking tile from #487 which stops the whole+ * service.+ *+ * It flips [LocationServiceController.GLOBAL_SHARING_ENABLED_KEY] rather than the+ * per-person switches, so resuming shares with exactly the people the user had+ * chosen, and it deliberately leaves the foreground service running: peers'+ * locations, waypoint entry/exit and UWB tracker reporting continue while paused.+ */+class SharingTileService : 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()+        scope.launch {+            val enabled = LocationServiceController.isGlobalSharingEnabled(this@SharingTileService)+            LocationServiceController.setGlobalSharingEnabled(this@SharingTileService, !enabled)+            refreshTile()+        }+    }++    private fun refreshTile() {+        scope.launch {+            val tile = qsTile ?: return@launch+            val enabled = LocationServiceController.isGlobalSharingEnabled(this@SharingTileService)+            tile.state = if (enabled) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE+            tile.subtitle = getString(+                if (enabled) R.string.tile_subtitle_on else R.string.tile_subtitle_off+            )+            tile.updateTile()+        }+    }++    override fun onDestroy() {+        super.onDestroy()+        scope.cancel()+    }++    companion object {+        /**+         * Ask the system to bind the tile so it re-reads the flag. Without this a+         * change made inside the app leaves a stale tile until the shade is reopened.+         */+        fun requestRefresh(context: Context) {+            val app = context.applicationContext+            TileService.requestListeningState(app, ComponentName(app, SharingTileService::class.java))+        }+    }+}
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 44ba0087bb..64163ebe84 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt@@ -189,6 +189,7 @@ fun MainPage(      val temporaryLinks by ffViewModel.temporaryLinks.collectAsState()     val waypoints by ffViewModel.waypoints.collectAsState()+    val globalSharingEnabled by ffViewModel.globalSharingEnabled.collectAsState()      val connectedUsers by ffViewModel.connectedUsers.collectAsState()     val awaitingRequestUsers by ffViewModel.awaitingRequestUsers.collectAsState()@@ -278,7 +279,9 @@ fun MainPage(             locationByUser = userPositions,             userNamesByLocationName = usersByLocationName         ),-        person = selectedUser?.let { PersonUiState(it, userPositions[it.id], waypoints) }+        person = selectedUser?.let {+            PersonUiState(it, userPositions[it.id], waypoints, globalSharingEnabled)+        }     )      // Owned here, not by MapView: the two animateTo effects below drive it, and MapView is@@ -717,7 +720,16 @@ fun PersonDetailSheet(state: PersonUiState, actions: PersonActions) {                 )                 Switch(                     user.sendingEnabled,-                    { send -> actions.setUserSharing(user, send) }+                    { send -> actions.setUserSharing(user, send) },+                    enabled = state.sharingGloballyEnabled+                )+            }+            if (!state.sharingGloballyEnabled) {+                Text(+                    stringResource(R.string.sharing_paused_note),+                    Modifier.padding(horizontal = 4.dp),+                    style = MaterialTheme.typography.bodySmall,+                    color = MaterialTheme.colorScheme.onSurfaceVariant                 )             }             // Auto-toggle: "Turn on/off after" + duration/arrival dropdown (Never default)
findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyUiContract.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyUiContract.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyUiContract.ktindex df88d61128..79982f05de 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyUiContract.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyUiContract.kt@@ -57,6 +57,8 @@ data class PersonUiState(     val location: LocationValue? = null,     /** Saved places, offered in the auto-toggle dropdown as "Arrival at <name>" triggers. */     val waypoints: List<Waypoint> = emptyList(),+    /** False while the Quick Settings sharing tile has sharing paused (GitHub #648). */+    val sharingGloballyEnabled: Boolean = true, )  /** Person-sheet callbacks. Same no-op-default arrangement as [FamilyListActions]. */
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 f745d8b5c5..922a6963c4 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyViewModel.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/FindFamilyViewModel.kt@@ -360,6 +360,12 @@ class FindFamilyViewModel(         }     } +    /** False while the Quick Settings sharing tile has publishing paused (GitHub #648);+     * the per-person switches are shown but disabled so their state is still visible. */+    val globalSharingEnabled: StateFlow<Boolean> =+        LocationServiceController.globalSharingEnabledFlow(ctx)+            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), true)+     /** Toggle per-person location sharing and reconcile the service so it stops      * when nobody is being shared with and starts when sharing is (re)enabled.      * Manual toggle always resets auto-toggle to Never.
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 4917164e90..f069566887 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt@@ -51,11 +51,13 @@ import com.vayunmathur.findfamily.tracker.TrackerReporting import com.vayunmathur.findfamily.tracker.TrackerStore import com.vayunmathur.findfamily.MainActivity import com.vayunmathur.findfamily.R+import com.vayunmathur.findfamily.service.SharingTileService import com.vayunmathur.library.util.DataStoreUtils import com.vayunmathur.library.work.startRepeatedTask import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay+import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch@@ -228,13 +230,18 @@ class LocationTrackingService : Service(), SensorEventListener {                 Log.w("FF-Heartbeat", "arrival auto-toggle apply failed", e)             } -            val publishTargets = publishBaseUsers.filter { it.id != Networking.userid && it.sendingEnabled }-            Log.d("FF-Heartbeat", "publish targets count=${publishTargets.size} ids=${publishTargets.map{ it.id.toULong() }} names=${publishTargets.map{ it.name }}")+            // The sharing tile (GitHub #648) suppresses outbound publishing without touching the+            // per-person switches, so turning it back on resumes exactly the set of people the+            // user was sharing with. Receiving, waypoints and tracker reporting are unaffected.+            val sharingOut = LocationServiceController.isGlobalSharingEnabled(this)+            val publishTargets = if (!sharingOut) emptyList<User>()+            else publishBaseUsers.filter { it.id != Networking.userid && it.sendingEnabled }+            Log.d("FF-Heartbeat", "publish targets count=${publishTargets.size} ids=${publishTargets.map{ it.id.toULong() }} names=${publishTargets.map{ it.name }} globalSharing=$sharingOut")             publishTargets.forEach {                 val result = runCatching { Networking.publishLocation(locationValue, it) }                 if (result.isFailure) Log.w("FF-Heartbeat", "publish to ${it.id.toULong()} threw", result.exceptionOrNull())             }-            currentLinks.filter { now < it.deleteAt }.forEach {+            if (sharingOut) currentLinks.filter { now < it.deleteAt }.forEach {                 val result = runCatching { Networking.publishLocation(locationValue, it) }                 if (result.isFailure) Log.w("FF-Heartbeat", "publish to link ${it.id} threw", result.exceptionOrNull())             }@@ -918,6 +925,17 @@ object LocationServiceController {     /** Persisted on/off switch for the whole tracking service (default on). */     const val TRACKING_ENABLED_KEY = "tracking_enabled" +    /**+     * Persisted on/off switch for publishing this device's location to anyone, toggled+     * from the Quick Settings sharing tile (default on — see SharingTileService).+     *+     * Deliberately separate from the per-person `sendingEnabled` flags so pausing and+     * resuming restores whoever was being shared with, and from [TRACKING_ENABLED_KEY]+     * so the service keeps running: peers' locations, waypoint entry/exit and UWB+     * tracker reporting all continue while sharing is paused.+     */+    const val GLOBAL_SHARING_ENABLED_KEY = "global_sharing_enabled"+     fun hasFineLocationPermission(context: Context): Boolean =         ContextCompat.checkSelfPermission(             context,@@ -934,6 +952,23 @@ object LocationServiceController {         syncServiceState(context)     } +    /** Whether outbound sharing is on. Defaults to true (opt-out, not opt-in). */+    suspend fun isGlobalSharingEnabled(context: Context): Boolean =+        DataStoreUtils.getInstance(context).getBooleanAwait(GLOBAL_SHARING_ENABLED_KEY, true)++    /** [isGlobalSharingEnabled] as a stream, for the UI to grey out the per-person switches. */+    fun globalSharingEnabledFlow(context: Context): Flow<Boolean> =+        DataStoreUtils.getInstance(context).booleanFlow(GLOBAL_SHARING_ENABLED_KEY, true)++    /**+     * Persist the outbound-sharing choice. The next heartbeat picks it up; the service+     * itself is left alone, so this never stops receiving or tracker reporting.+     */+    suspend fun setGlobalSharingEnabled(context: Context, enabled: Boolean) {+        DataStoreUtils.getInstance(context).setBoolean(GLOBAL_SHARING_ENABLED_KEY, enabled)+        SharingTileService.requestRefresh(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
findfamily/src/main/res/drawable/ic_tile_sharing.xml
diff --git a/findfamily/src/main/res/drawable/ic_tile_sharing.xml b/findfamily/src/main/res/drawable/ic_tile_sharing.xmlnew file mode 100644index 0000000000..3e7d41b7c0--- /dev/null+++ b/findfamily/src/main/res/drawable/ic_tile_sharing.xml@@ -0,0 +1,10 @@+<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">+    <path+        android:fillColor="#FFFFFFFF"+        android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92s2.92,-1.31 2.92,-2.92 -1.31,-2.92 -2.92,-2.92z" />+</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 72f2bb9d2a..934bf1292b 100644--- a/findfamily/src/main/res/values/strings.xml+++ b/findfamily/src/main/res/values/strings.xml@@ -298,11 +298,15 @@      <!-- TrackingTileService: Quick Settings tile label. Short noun phrase -->     <string name="tile_label">FindFamily tracking</string>-    <!-- TrackingTileService: tile subtitle when tracking is on. Short status -->+    <!-- Quick Settings tile subtitle when the tile is on. Short status -->     <string name="tile_subtitle_on">On</string>-    <!-- TrackingTileService: tile subtitle when tracking is off. Short status -->+    <!-- Quick Settings tile subtitle when the tile 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>+    <!-- SharingTileService: Quick Settings tile label for the pause-sharing tile. Short noun phrase -->+    <string name="sharing_tile_label">FindFamily sharing</string>+    <!-- UserPage: Note under the disabled sharing switch when the Quick Settings sharing tile is off. Sentence -->+    <string name="sharing_paused_note">Sharing is paused for everyone from the Quick Settings tile</string>  </resources>