Compare commits

..

No commits in common. "d490d6f2396495ea4a6e2ae51b8cd14b13ffe1ed" and "c706b8ab36ba0c3a7faf512d783188ec01d6f3b5" have entirely different histories.

2 changed files with 30 additions and 37 deletions

View file

@ -2,9 +2,7 @@ package im.angry.openeuicc.ui.wizard
import android.app.AlertDialog import android.app.AlertDialog
import android.content.ClipboardManager import android.content.ClipboardManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
@ -22,9 +20,7 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.journeyapps.barcodescanner.ScanContract import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions import com.journeyapps.barcodescanner.ScanOptions
import im.angry.openeuicc.common.R import im.angry.openeuicc.common.R
import im.angry.openeuicc.util.ActivationCode import im.angry.openeuicc.util.*
import im.angry.openeuicc.util.decodeQrFromBitmap
import im.angry.openeuicc.util.preferenceRepository
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -32,10 +28,6 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizardStepFragment() { class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizardStepFragment() {
companion object {
const val TAG = "DownloadWizardMethodSelectFragment"
}
data class DownloadMethod( data class DownloadMethod(
val iconRes: Int, val iconRes: Int,
val titleRes: Int, val titleRes: Int,
@ -43,26 +35,32 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
) )
// TODO: Maybe we should find a better barcode scanner (or an external one?) // TODO: Maybe we should find a better barcode scanner (or an external one?)
private val barcodeScannerLauncher = private val barcodeScannerLauncher = registerForActivityResult(ScanContract()) { result ->
registerForActivityResult(ScanContract()) { result.contents?.let { content ->
processLpaString(it.contents ?: return@registerForActivityResult) processLpaString(content)
}
} }
private val gallerySelectorLauncher = private val gallerySelectorLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) { registerForActivityResult(ActivityResultContracts.GetContent()) { result ->
if (result == null) return@registerForActivityResult
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
val decoded = onGalleryResult(it ?: return@launch) ?: return@launch runCatching {
withContext(Dispatchers.Main) { processLpaString(decoded) } requireContext().contentResolver.openInputStream(result)?.let { input ->
val bmp = BitmapFactory.decodeStream(input)
input.close()
decodeQrFromBitmap(bmp)?.let {
withContext(Dispatchers.Main) {
processLpaString(it)
} }
} }
private fun onGalleryResult(result: Uri) = try { bmp.recycle()
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( val downloadMethods = arrayOf(
@ -191,9 +189,3 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
} }
} }
private fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
block()
} finally {
recycle()
}

View file

@ -86,12 +86,13 @@ suspend fun connectSEService(context: Context): SEService = suspendCoroutine { c
} }
} }
fun decodeQrFromBitmap(bmp: Bitmap): String { fun decodeQrFromBitmap(bmp: Bitmap): String? =
runCatching {
val pixels = IntArray(bmp.width * bmp.height) val pixels = IntArray(bmp.width * bmp.height)
bmp.getPixels(pixels, 0, bmp.width, 0, 0, bmp.width, bmp.height) bmp.getPixels(pixels, 0, bmp.width, 0, 0, bmp.width, bmp.height)
val luminanceSource = RGBLuminanceSource(bmp.width, bmp.height, pixels) val luminanceSource = RGBLuminanceSource(bmp.width, bmp.height, pixels)
val binaryBmp = BinaryBitmap(HybridBinarizer(luminanceSource)) val binaryBmp = BinaryBitmap(HybridBinarizer(luminanceSource))
return QRCodeReader().decode(binaryBmp).text QRCodeReader().decode(binaryBmp).text
} }.getOrNull()