Compare commits

...

1 commit

View file

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