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 uris: List<Uri?>? =
val uris: List<Uri> =
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<Uri?>(Intent.EXTRA_STREAM, Uri::class.java)
} else {
@Suppress("DEPRECATION") intent.getParcelableArrayListExtra<Uri?>(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<Ipn.OutgoingFile> =
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<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