Compare commits

..

1 commit

View file

@ -50,28 +50,19 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
private val gallerySelectorLauncher =
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?) {
fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
block()
} finally {
recycle()
}
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)
}
private fun onGalleryResult(result: Uri) = try {
requireContext().contentResolver.openInputStream(result)
.use(BitmapFactory::decodeStream)
.use(::decodeQrFromBitmap)
} catch (e: Exception) {
Log.e(TAG, "Failed to decode QR code from gallery", e)
null
}
val downloadMethods = arrayOf(
@ -199,4 +190,10 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
}
}
}
private fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
block()
} finally {
recycle()
}