Modern Apps git

← Modern-Apps.git

share: add half-height share popup and fix tile long-press

Commit
9c6763fe01b0f171cc19e3934e5c52291f446060
Author
vayun-mathur <[email protected]>
When
2026-08-20 20:54 UTC (3 months ago)
Parents
f022fab

Diff

share/src/main/AndroidManifest.xml
diff --git a/share/src/main/AndroidManifest.xml b/share/src/main/AndroidManifest.xmlindex 0ac7f88516..d4af9c12d7 100644--- a/share/src/main/AndroidManifest.xml+++ b/share/src/main/AndroidManifest.xml@@ -65,7 +65,23 @@                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>-            <!-- Surface Share in the system share sheet (files, images, text). -->+            <!-- Long-press on the Quick Settings tile. The tile's+                 android.service.quicksettings.ACTIVITY meta-data names this activity, but the+                 system only honours it if the target also declares this action. -->+            <intent-filter>+                <action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />+                <category android:name="android.intent.category.DEFAULT" />+            </intent-filter>+        </activity>++        <!-- The system share sheet's target. A bottom sheet over the sharing app rather than the+             whole app: the files are already chosen, so only the device is left to pick.+             configChanges matches MainActivity's so a rotation does not restart discovery. -->+        <activity+            android:name=".SharePopupActivity"+            android:configChanges="mcc|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|keyboardHidden|layoutDirection|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"+            android:exported="true"+            android:theme="@style/Theme.SharePopup">             <intent-filter>                 <action android:name="android.intent.action.SEND" />                 <category android:name="android.intent.category.DEFAULT" />
share/src/main/java/com/vayunmathur/share/MainActivity.kt
diff --git a/share/src/main/java/com/vayunmathur/share/MainActivity.kt b/share/src/main/java/com/vayunmathur/share/MainActivity.ktindex 9f97a6a7c4..5010b8bafe 100644--- a/share/src/main/java/com/vayunmathur/share/MainActivity.kt+++ b/share/src/main/java/com/vayunmathur/share/MainActivity.kt@@ -1,6 +1,5 @@ package com.vayunmathur.share -import android.content.Intent import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent@@ -13,12 +12,17 @@ import com.vayunmathur.share.platform.SharePermissions import com.vayunmathur.share.platform.ShareViewModel import com.vayunmathur.share.platform.ShareViewModelFactory +/**+ * The app's own screen, reached from the launcher or by long-pressing the Quick Settings tile.+ *+ * Share-sheet intents do NOT land here: [SharePopupActivity] owns ACTION_SEND / SEND_MULTIPLE so+ * that picking Share from another app opens a bottom sheet instead of the whole app.+ */ class MainActivity : ComponentActivity() {     private val shareViewModel: ShareViewModel by viewModels { ShareViewModelFactory(application) }     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         enableEdgeToEdge()-        handleIncomingShareIntent(intent)         setContent {             DynamicTheme {                 PermissionsChecker(@@ -28,15 +32,4 @@ class MainActivity : ComponentActivity() {             }         }     }-    override fun onNewIntent(intent: Intent) {-        super.onNewIntent(intent)-        setIntent(intent)-        handleIncomingShareIntent(intent)-    }-    private fun handleIncomingShareIntent(intent: Intent?) {-        if (intent == null) return-        when (intent.action) {-            Intent.ACTION_SEND, Intent.ACTION_SEND_MULTIPLE -> shareViewModel.handleShareIntent(intent)-        }-    }-}\ No newline at end of file+}
share/src/main/java/com/vayunmathur/share/SharePopupActivity.kt
diff --git a/share/src/main/java/com/vayunmathur/share/SharePopupActivity.kt b/share/src/main/java/com/vayunmathur/share/SharePopupActivity.ktnew file mode 100644index 0000000000..de2bb4844f--- /dev/null+++ b/share/src/main/java/com/vayunmathur/share/SharePopupActivity.kt@@ -0,0 +1,38 @@+package com.vayunmathur.share++import android.os.Bundle+import androidx.activity.ComponentActivity+import androidx.activity.compose.setContent+import androidx.activity.enableEdgeToEdge+import androidx.activity.viewModels+import com.vayunmathur.library.ui.DynamicTheme+import com.vayunmathur.share.platform.ShareViewModel+import com.vayunmathur.share.platform.ShareViewModelFactory+import com.vayunmathur.share.ui.SharePopupSheet++/**+ * The system share sheet's target: a half-height bottom sheet over whatever app shared the files.+ *+ * Opening the whole app for a share is a detour — the files are already chosen, so the only thing+ * left is picking a device, and that is all this shows. The window is transparent+ * (`Theme.SharePopup`) so the Compose sheet's own scrim is the only thing drawn over the app below.+ *+ * Nothing here stops the scan: [ShareViewModel.connectToDevice] stops it when a device is chosen+ * (the radios are needed for the transfer) and `onCleared` stops it when this activity goes away.+ */+class SharePopupActivity : ComponentActivity() {+    private val shareViewModel: ShareViewModel by viewModels { ShareViewModelFactory(application) }++    override fun onCreate(savedInstanceState: Bundle?) {+        super.onCreate(savedInstanceState)+        enableEdgeToEdge()+        // Only on a fresh launch: the URIs live in the ViewModel, which survives recreation, and+        // re-staging shared text would rewrite the file a transfer may already be reading.+        if (savedInstanceState == null) shareViewModel.handleShareIntent(intent)+        setContent {+            DynamicTheme {+                SharePopupSheet(viewModel = shareViewModel, onDismiss = ::finish)+            }+        }+    }+}
share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.kt
diff --git a/share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.kt b/share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.ktindex 4fec04deba..29348a8a50 100644--- a/share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.kt+++ b/share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.kt@@ -202,7 +202,7 @@ class ShareViewModel(         }     } -    /** Entry from MainActivity's share intent — handles ACTION_SEND / SEND_MULTIPLE. */+    /** Entry from the share-sheet popup's intent — handles ACTION_SEND / SEND_MULTIPLE. */     fun handleShareIntent(intent: Intent) {         when (intent.action) {             Intent.ACTION_SEND -> {
share/src/main/java/com/vayunmathur/share/ui/SharePopupSheet.kt
diff --git a/share/src/main/java/com/vayunmathur/share/ui/SharePopupSheet.kt b/share/src/main/java/com/vayunmathur/share/ui/SharePopupSheet.ktnew file mode 100644index 0000000000..e6dc74c5c8--- /dev/null+++ b/share/src/main/java/com/vayunmathur/share/ui/SharePopupSheet.kt@@ -0,0 +1,157 @@+package com.vayunmathur.share.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.navigationBarsPadding+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.items+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalConfiguration+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.Card+import com.vayunmathur.library.ui.CircularProgressIndicator+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.ModalBottomSheet+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.rememberModalBottomSheetState+import com.vayunmathur.library.ui.rememberMultiplePermissionRequest+import com.vayunmathur.share.R+import com.vayunmathur.share.platform.SendUiState+import com.vayunmathur.share.platform.ShareActions+import com.vayunmathur.share.platform.SharePermissions+import com.vayunmathur.share.platform.ShareViewModel++/**+ * The share-sheet target's UI: pick a device, watch the transfer, that is all.+ *+ * Deliberately not [ShareSendScreen] in a smaller window. The user already chose the files in the+ * app they shared from, so there is no picker, no Clear and no Scan button here — the only decision+ * left is which device, and discovery starts on its own so that decision is immediately available.+ */+@Composable+fun SharePopupSheet(viewModel: ShareViewModel, onDismiss: () -> Unit) {+    val uiState by viewModel.sendUiState.collectAsState()+    var permissionsDenied by remember { mutableStateOf(false) }+    val requestPermissions = rememberMultiplePermissionRequest(+        permissions = SharePermissions.sendFlowPermissions(),+    ) { granted ->+        permissionsDenied = !granted+        if (granted) viewModel.startScan()+    }+    // Scanning without BLUETOOTH_SCAN / NEARBY_WIFI_DEVICES throws, so the request gates the scan+    // rather than the sheet: the file summary is still worth showing while the dialog is up.+    LaunchedEffect(Unit) { requestPermissions() }+    SharePopupContent(+        uiState = uiState,+        actions = viewModel,+        permissionsDenied = permissionsDenied,+        onDismiss = onDismiss,+    )+}++@Composable+fun SharePopupContent(+    uiState: SendUiState,+    actions: ShareActions,+    permissionsDenied: Boolean,+    onDismiss: () -> Unit,+) {+    // skipPartiallyExpanded: the sheet is already sized to half the screen, so a partial state+    // would only offer a second, smaller resting place nobody asked for.+    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)+    ModalBottomSheet(onDismissRequest = onDismiss, sheetState = sheetState) {+        Column(+            modifier = Modifier+                .height(LocalConfiguration.current.screenHeightDp.dp / 2)+                .navigationBarsPadding()+                .padding(horizontal = 16.dp)+                .padding(bottom = 16.dp),+            verticalArrangement = Arrangement.spacedBy(12.dp),+        ) {+            Text(+                stringResource(R.string.share_popup_title),+                style = MaterialTheme.typography.titleMedium,+            )+            OutgoingFilesSummary(uiState.outgoingDisplayNames, uiState.outgoingUris.size)+            val connection = uiState.activeConnection+            when {+                // A live transfer replaces the device list: the choice has been made, and the+                // sheet stays up until dismissed so the result is readable.+                connection != null -> TransferCardHost(+                    conn = connection,+                    onDisconnect = { actions.disconnect(connection) },+                )+                permissionsDenied -> Text(+                    stringResource(R.string.share_popup_permission_needed),+                    style = MaterialTheme.typography.bodyMedium,+                    color = MaterialTheme.colorScheme.error,+                )+                else -> {+                    if (uiState.isScanning) ScanningRow()+                    LazyColumn(+                        modifier = Modifier.fillMaxWidth(),+                        verticalArrangement = Arrangement.spacedBy(8.dp),+                    ) {+                        items(uiState.discoveredDevices, key = { it.endpointId }) { device ->+                            DeviceRow(device = device, onTap = { actions.connectToDevice(device) })+                        }+                        if (uiState.discoveredDevices.isEmpty()) {+                            item {+                                Text(+                                    stringResource(R.string.share_popup_no_devices),+                                    style = MaterialTheme.typography.bodySmall,+                                    color = MaterialTheme.colorScheme.outline,+                                )+                            }+                        }+                    }+                }+            }+        }+    }+}++/** Read-only: what is being sent was decided in the app the user shared from. */+@Composable+private fun OutgoingFilesSummary(displayNames: List<String>, count: Int) {+    Card(modifier = Modifier.fillMaxWidth()) {+        Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {+            Text(+                if (count == 0) stringResource(R.string.share_no_files_selected)+                else stringResource(R.string.share_files_selected, count),+                style = MaterialTheme.typography.titleSmall,+            )+            displayNames.forEach { name ->+                Text(+                    "• $name",+                    style = MaterialTheme.typography.bodySmall,+                    color = MaterialTheme.colorScheme.outline,+                )+            }+        }+    }+}++@Composable+private fun ScanningRow() {+    Row(verticalAlignment = Alignment.CenterVertically) {+        CircularProgressIndicator(modifier = Modifier.padding(end = 12.dp))+        Text(+            stringResource(R.string.share_scanning),+            style = MaterialTheme.typography.bodyMedium,+        )+    }+}
share/src/main/java/com/vayunmathur/share/ui/ShareSendScreen.kt
diff --git a/share/src/main/java/com/vayunmathur/share/ui/ShareSendScreen.kt b/share/src/main/java/com/vayunmathur/share/ui/ShareSendScreen.ktindex 5ee09b24a6..5b1fefe4d0 100644--- a/share/src/main/java/com/vayunmathur/share/ui/ShareSendScreen.kt+++ b/share/src/main/java/com/vayunmathur/share/ui/ShareSendScreen.kt@@ -237,7 +237,7 @@ fun DeviceRow(device: NearbyDevice, onTap: () -> Unit) {  * and a native session handle, and Layoutlib can load neither.  */ @Composable-private fun TransferCardHost(conn: Connection, onDisconnect: () -> Unit) {+internal fun TransferCardHost(conn: Connection, onDisconnect: () -> Unit) {     val state by conn.state.collectAsState()     val error by conn.error.collectAsState()     val sent by conn.bytesSent.collectAsState()
share/src/main/res/values/strings.xml
diff --git a/share/src/main/res/values/strings.xml b/share/src/main/res/values/strings.xmlindex 0c4427b216..343daf9ff0 100644--- a/share/src/main/res/values/strings.xml+++ b/share/src/main/res/values/strings.xml@@ -63,4 +63,9 @@     <string name="share_dismiss">Dismiss</string>     <string name="share_unknown_state">Unknown state</string>     <string name="share_nothing_selected">Pick files before choosing a device.</string>++    <!-- Share-sheet popup -->+    <string name="share_popup_title">Send to a nearby device</string>+    <string name="share_popup_no_devices">No devices found yet. Keep the other device visible.</string>+    <string name="share_popup_permission_needed">Nearby Share needs Bluetooth and Wi-Fi permissions to find nearby devices.</string> </resources>
share/src/main/res/values/themes.xml
diff --git a/share/src/main/res/values/themes.xml b/share/src/main/res/values/themes.xmlnew file mode 100644index 0000000000..37aeed577d--- /dev/null+++ b/share/src/main/res/values/themes.xml@@ -0,0 +1,18 @@+<?xml version="1.0" encoding="utf-8"?>+<resources>+    <!--+        The share-sheet target is a bottom sheet, not a screen: the activity window itself must+        be invisible so the Compose sheet's own scrim is the only thing drawn over whatever the+        user was already looking at. Inheriting Theme.General keeps the rest of its behaviour+        (notably windowSoftInputMode=stateUnchanged); the two overrides are the same pair+        Theme.Launcher needs to expose the wallpaper.++        windowIsTranslucent is what stops the system from freezing the activity behind this one,+        so the app under the sheet keeps drawing. No backgroundDimEnabled: that only applies to+        floating windows, and the sheet dims for itself.+    -->+    <style name="Theme.SharePopup" parent="Theme.General">+        <item name="android:windowBackground">@android:color/transparent</item>+        <item name="android:windowIsTranslucent">true</item>+    </style>+</resources>