android: ShareActivity code cleanup

Simplified `loadFiles` by:
* Creating extension functions for the wordy getParcelable*Extra methods
* Shoring up some nullability checks
pull/690/head
Doug Melton 4 months ago
parent cc2f6386a6
commit b1a58b37d3

@ -69,31 +69,30 @@ class ShareActivity : ComponentActivity() {
val act = intent.action val act = intent.action
val uris: List<Uri?>? = val uris: List<Uri> =
when (act) { when (act) {
Intent.ACTION_SEND -> { Intent.ACTION_SEND -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (intent.extras?.containsKey(Intent.EXTRA_STREAM) == true) {
listOf(intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)) // If EXTRA_STREAM is present, get the single URI for that stream
} else { listOfNotNull(intent.versionSafeGetStreamUri())
@Suppress("DEPRECATION") }
listOf(intent.getParcelableExtra(Intent.EXTRA_STREAM) as? Uri) else {
TSLog.e(TAG, "No extras found in intent - nothing to share")
emptyList()
} }
} }
Intent.ACTION_SEND_MULTIPLE -> { Intent.ACTION_SEND_MULTIPLE -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // If ACTION_SEND_MULTIPLE, assume this is a list of files to share
intent.getParcelableArrayListExtra<Uri?>(Intent.EXTRA_STREAM, Uri::class.java) intent.versionSafeGetStreamUris()
} else {
@Suppress("DEPRECATION") intent.getParcelableArrayListExtra<Uri?>(Intent.EXTRA_STREAM)
}
} }
else -> { else -> {
TSLog.e(TAG, "No extras found in intent - nothing to share") TSLog.e(TAG, "Unexpected intent action: $act. Expected ACTION_SEND or ACTION_SEND_MULTIPLE")
null emptyList()
} }
} }
val pendingFiles: List<Ipn.OutgoingFile> = val pendingFiles: List<Ipn.OutgoingFile> =
uris?.filterNotNull()?.mapNotNull { uri -> uris.mapNotNull { uri ->
contentResolver?.query(uri, null, null, null, null)?.use { cursor -> contentResolver?.query(uri, null, null, null, null)?.use { cursor ->
val nameCol = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) val nameCol = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
val sizeCol = cursor.getColumnIndex(OpenableColumns.SIZE) val sizeCol = cursor.getColumnIndex(OpenableColumns.SIZE)
@ -107,7 +106,7 @@ class ShareActivity : ComponentActivity() {
null null
} }
} }
} ?: emptyList() }
if (pendingFiles.isEmpty()) { if (pendingFiles.isEmpty()) {
TSLog.e(TAG, "Share failure - no files extracted from intent") 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() 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<Uri> =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelableArrayListExtra<Uri?>(Intent.EXTRA_STREAM, Uri::class.java)
} else {
@Suppress("DEPRECATION")
getParcelableArrayListExtra<Uri?>(Intent.EXTRA_STREAM)
}
?.filterNotNull()
?: emptyList()
Loading…
Cancel
Save