Compare commits

..

1 commit

View file

@ -50,19 +50,28 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
private val gallerySelectorLauncher = private val gallerySelectorLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) { registerForActivityResult(ActivityResultContracts.GetContent()) {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) { onGalleryResult(it) }
val decoded = onGalleryResult(it ?: return@launch) ?: return@launch
withContext(Dispatchers.Main) { processLpaString(decoded) }
}
} }
private fun onGalleryResult(result: Uri) = try { private suspend fun onGalleryResult(result: Uri?) {
requireContext().contentResolver.openInputStream(result) fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
.use(BitmapFactory::decodeStream) block()
.use(::decodeQrFromBitmap) } finally {
} catch (e: Exception) { recycle()
Log.e(TAG, "Failed to decode QR code from gallery", e) }
null
val decoded = try {
requireContext().contentResolver.openInputStream(result ?: return)
.use(BitmapFactory::decodeStream)
.use(::decodeQrFromBitmap)
} catch (e: Exception) {
Log.e(TAG, "Failed to decode QR code from gallery", e)
return
}
withContext(Dispatchers.Main) {
processLpaString(decoded)
}
} }
val downloadMethods = arrayOf( val downloadMethods = arrayOf(
@ -190,10 +199,4 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
} }
} }
}
private fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
block()
} finally {
recycle()
} }