Compare commits
No commits in common. "d490d6f2396495ea4a6e2ae51b8cd14b13ffe1ed" and "c706b8ab36ba0c3a7faf512d783188ec01d6f3b5" have entirely different histories.
d490d6f239
...
c706b8ab36
2 changed files with 30 additions and 37 deletions
|
@ -2,9 +2,7 @@ package im.angry.openeuicc.ui.wizard
|
|||
|
||||
import android.app.AlertDialog
|
||||
import android.content.ClipboardManager
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
|
@ -22,9 +20,7 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder
|
|||
import com.journeyapps.barcodescanner.ScanContract
|
||||
import com.journeyapps.barcodescanner.ScanOptions
|
||||
import im.angry.openeuicc.common.R
|
||||
import im.angry.openeuicc.util.ActivationCode
|
||||
import im.angry.openeuicc.util.decodeQrFromBitmap
|
||||
import im.angry.openeuicc.util.preferenceRepository
|
||||
import im.angry.openeuicc.util.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -32,10 +28,6 @@ import kotlinx.coroutines.runBlocking
|
|||
import kotlinx.coroutines.withContext
|
||||
|
||||
class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizardStepFragment() {
|
||||
companion object {
|
||||
const val TAG = "DownloadWizardMethodSelectFragment"
|
||||
}
|
||||
|
||||
data class DownloadMethod(
|
||||
val iconRes: 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?)
|
||||
private val barcodeScannerLauncher =
|
||||
registerForActivityResult(ScanContract()) {
|
||||
processLpaString(it.contents ?: return@registerForActivityResult)
|
||||
private val barcodeScannerLauncher = registerForActivityResult(ScanContract()) { result ->
|
||||
result.contents?.let { content ->
|
||||
processLpaString(content)
|
||||
}
|
||||
}
|
||||
|
||||
private val gallerySelectorLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.GetContent()) {
|
||||
registerForActivityResult(ActivityResultContracts.GetContent()) { result ->
|
||||
if (result == null) return@registerForActivityResult
|
||||
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
val decoded = onGalleryResult(it ?: return@launch) ?: return@launch
|
||||
withContext(Dispatchers.Main) { processLpaString(decoded) }
|
||||
runCatching {
|
||||
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 {
|
||||
requireContext().contentResolver.openInputStream(result)
|
||||
.use(BitmapFactory::decodeStream)
|
||||
.use(::decodeQrFromBitmap)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to decode QR code from gallery", e)
|
||||
null
|
||||
bmp.recycle()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val downloadMethods = arrayOf(
|
||||
|
@ -191,9 +189,3 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
|
||||
block()
|
||||
} finally {
|
||||
recycle()
|
||||
}
|
|
@ -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)
|
||||
bmp.getPixels(pixels, 0, bmp.width, 0, 0, bmp.width, bmp.height)
|
||||
|
||||
val luminanceSource = RGBLuminanceSource(bmp.width, bmp.height, pixels)
|
||||
val binaryBmp = BinaryBitmap(HybridBinarizer(luminanceSource))
|
||||
|
||||
return QRCodeReader().decode(binaryBmp).text
|
||||
}
|
||||
QRCodeReader().decode(binaryBmp).text
|
||||
}.getOrNull()
|
||||
|
|
Loading…
Add table
Reference in a new issue