|
@@ -1,16 +1,25 @@
|
|
|
package im.angry.openeuicc.ui.wizard
|
|
package im.angry.openeuicc.ui.wizard
|
|
|
|
|
|
|
|
|
|
+import android.graphics.BitmapFactory
|
|
|
import android.os.Bundle
|
|
import android.os.Bundle
|
|
|
import android.view.LayoutInflater
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
import android.view.ViewGroup
|
|
|
import android.widget.ImageView
|
|
import android.widget.ImageView
|
|
|
import android.widget.TextView
|
|
import android.widget.TextView
|
|
|
|
|
+import androidx.activity.result.contract.ActivityResultContracts
|
|
|
|
|
+import androidx.lifecycle.lifecycleScope
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration
|
|
import androidx.recyclerview.widget.DividerItemDecoration
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
import androidx.recyclerview.widget.RecyclerView.ViewHolder
|
|
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.common.R
|
|
|
|
|
+import im.angry.openeuicc.util.*
|
|
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
|
|
|
|
|
class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizardStepFragment() {
|
|
class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizardStepFragment() {
|
|
|
data class DownloadMethod(
|
|
data class DownloadMethod(
|
|
@@ -19,12 +28,44 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
|
|
|
val onClick: () -> Unit
|
|
val onClick: () -> Unit
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ // TODO: Maybe we should find a better barcode scanner (or an external one?)
|
|
|
|
|
+ private val barcodeScannerLauncher = registerForActivityResult(ScanContract()) { result ->
|
|
|
|
|
+ result.contents?.let { content ->
|
|
|
|
|
+ processLpaString(content)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private val gallerySelectorLauncher =
|
|
|
|
|
+ registerForActivityResult(ActivityResultContracts.GetContent()) { result ->
|
|
|
|
|
+ if (result == null) return@registerForActivityResult
|
|
|
|
|
+
|
|
|
|
|
+ lifecycleScope.launch(Dispatchers.IO) {
|
|
|
|
|
+ runCatching {
|
|
|
|
|
+ requireContext().contentResolver.openInputStream(result)?.let { input ->
|
|
|
|
|
+ val bmp = BitmapFactory.decodeStream(input)
|
|
|
|
|
+ input.close()
|
|
|
|
|
+
|
|
|
|
|
+ decodeQrFromBitmap(bmp)?.let {
|
|
|
|
|
+ withContext(Dispatchers.Main) {
|
|
|
|
|
+ processLpaString(it)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bmp.recycle()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
val downloadMethods = arrayOf(
|
|
val downloadMethods = arrayOf(
|
|
|
DownloadMethod(R.drawable.ic_scan_black, R.string.download_wizard_method_qr_code) {
|
|
DownloadMethod(R.drawable.ic_scan_black, R.string.download_wizard_method_qr_code) {
|
|
|
-
|
|
|
|
|
|
|
+ barcodeScannerLauncher.launch(ScanOptions().apply {
|
|
|
|
|
+ setDesiredBarcodeFormats(ScanOptions.QR_CODE)
|
|
|
|
|
+ setOrientationLocked(false)
|
|
|
|
|
+ })
|
|
|
},
|
|
},
|
|
|
DownloadMethod(R.drawable.ic_gallery_black, R.string.download_wizard_method_gallery) {
|
|
DownloadMethod(R.drawable.ic_gallery_black, R.string.download_wizard_method_gallery) {
|
|
|
-
|
|
|
|
|
|
|
+ gallerySelectorLauncher.launch("image/*")
|
|
|
},
|
|
},
|
|
|
DownloadMethod(R.drawable.ic_edit, R.string.download_wizard_method_manual) {
|
|
DownloadMethod(R.drawable.ic_edit, R.string.download_wizard_method_manual) {
|
|
|
gotoNextFragment(DownloadWizardDetailsFragment())
|
|
gotoNextFragment(DownloadWizardDetailsFragment())
|
|
@@ -36,9 +77,8 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
|
|
|
override val hasPrev: Boolean
|
|
override val hasPrev: Boolean
|
|
|
get() = true
|
|
get() = true
|
|
|
|
|
|
|
|
- override fun createNextFragment(): DownloadWizardActivity.DownloadWizardStepFragment? {
|
|
|
|
|
- TODO("Not yet implemented")
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ override fun createNextFragment(): DownloadWizardActivity.DownloadWizardStepFragment? =
|
|
|
|
|
+ null
|
|
|
|
|
|
|
|
override fun createPrevFragment(): DownloadWizardActivity.DownloadWizardStepFragment =
|
|
override fun createPrevFragment(): DownloadWizardActivity.DownloadWizardStepFragment =
|
|
|
DownloadWizardSlotSelectFragment()
|
|
DownloadWizardSlotSelectFragment()
|
|
@@ -62,6 +102,14 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
|
|
|
return view
|
|
return view
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private fun processLpaString(s: String) {
|
|
|
|
|
+ val components = s.split("$")
|
|
|
|
|
+ if (components.size < 3 || components[0] != "LPA:1") return
|
|
|
|
|
+ state.smdp = components[1]
|
|
|
|
|
+ state.matchingId = components[2]
|
|
|
|
|
+ gotoNextFragment(DownloadWizardDetailsFragment())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private class DownloadMethodViewHolder(private val root: View) : ViewHolder(root) {
|
|
private class DownloadMethodViewHolder(private val root: View) : ViewHolder(root) {
|
|
|
private val icon = root.requireViewById<ImageView>(R.id.download_method_icon)
|
|
private val icon = root.requireViewById<ImageView>(R.id.download_method_icon)
|
|
|
private val title = root.requireViewById<TextView>(R.id.download_method_title)
|
|
private val title = root.requireViewById<TextView>(R.id.download_method_title)
|