From b1a58b37d3f9f9a0175ea86eef4a6ed86e4ba223 Mon Sep 17 00:00:00 2001 From: Doug Melton Date: Tue, 12 Aug 2025 15:03:54 -0700 Subject: [PATCH] android: ShareActivity code cleanup Simplified `loadFiles` by: * Creating extension functions for the wordy getParcelable*Extra methods * Shoring up some nullability checks --- .../java/com/tailscale/ipn/ShareActivity.kt | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/android/src/main/java/com/tailscale/ipn/ShareActivity.kt b/android/src/main/java/com/tailscale/ipn/ShareActivity.kt index 09d9665..46b772a 100644 --- a/android/src/main/java/com/tailscale/ipn/ShareActivity.kt +++ b/android/src/main/java/com/tailscale/ipn/ShareActivity.kt @@ -69,31 +69,30 @@ class ShareActivity : ComponentActivity() { val act = intent.action - val uris: List? = + val uris: List = when (act) { Intent.ACTION_SEND -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - listOf(intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)) - } else { - @Suppress("DEPRECATION") - listOf(intent.getParcelableExtra(Intent.EXTRA_STREAM) as? Uri) + if (intent.extras?.containsKey(Intent.EXTRA_STREAM) == true) { + // If EXTRA_STREAM is present, get the single URI for that stream + listOfNotNull(intent.versionSafeGetStreamUri()) + } + else { + TSLog.e(TAG, "No extras found in intent - nothing to share") + emptyList() } } Intent.ACTION_SEND_MULTIPLE -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri::class.java) - } else { - @Suppress("DEPRECATION") intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM) - } + // If ACTION_SEND_MULTIPLE, assume this is a list of files to share + intent.versionSafeGetStreamUris() } else -> { - TSLog.e(TAG, "No extras found in intent - nothing to share") - null + TSLog.e(TAG, "Unexpected intent action: $act. Expected ACTION_SEND or ACTION_SEND_MULTIPLE") + emptyList() } } val pendingFiles: List = - uris?.filterNotNull()?.mapNotNull { uri -> + uris.mapNotNull { uri -> contentResolver?.query(uri, null, null, null, null)?.use { cursor -> val nameCol = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) val sizeCol = cursor.getColumnIndex(OpenableColumns.SIZE) @@ -107,7 +106,7 @@ class ShareActivity : ComponentActivity() { null } } - } ?: emptyList() + } if (pendingFiles.isEmpty()) { TSLog.e(TAG, "Share failure - no files extracted from intent") @@ -123,3 +122,22 @@ class ShareActivity : ComponentActivity() { return if (extension != null) "$randomId.$extension" else randomId.toString() } } + +private fun Intent.versionSafeGetStreamUri(): Uri? = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java) + } + else { + @Suppress("DEPRECATION") + getParcelableExtra(Intent.EXTRA_STREAM) as? Uri + } + +private fun Intent.versionSafeGetStreamUris(): List = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + getParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri::class.java) + } else { + @Suppress("DEPRECATION") + getParcelableArrayListExtra(Intent.EXTRA_STREAM) + } + ?.filterNotNull() + ?: emptyList() \ No newline at end of file