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 {
block()
} finally {
recycle()
}
val decoded = try {
requireContext().contentResolver.openInputStream(result ?: return)
.use(BitmapFactory::decodeStream) .use(BitmapFactory::decodeStream)
.use(::decodeQrFromBitmap) .use(::decodeQrFromBitmap)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to decode QR code from gallery", e) Log.e(TAG, "Failed to decode QR code from gallery", e)
null return
}
withContext(Dispatchers.Main) {
processLpaString(decoded)
}
} }
val downloadMethods = arrayOf( val downloadMethods = arrayOf(
@ -191,9 +200,3 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
} }
} }
private fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
block()
} finally {
recycle()
}