@@ -56,8 +56,16 @@ import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
5656import com.fasterxml.jackson.databind.DeserializationFeature
5757import com.fasterxml.jackson.databind.ObjectMapper
5858import com.fasterxml.jackson.databind.module.SimpleModule
59+ import com.google.android.gms.common.moduleinstall.InstallStatusListener
60+ import com.google.android.gms.common.moduleinstall.ModuleInstall
61+ import com.google.android.gms.common.moduleinstall.ModuleInstallRequest
62+ import com.google.android.gms.common.moduleinstall.ModuleInstallStatusUpdate
63+ import com.google.android.gms.common.moduleinstall.ModuleInstallStatusUpdate.InstallState.STATE_CANCELED
64+ import com.google.android.gms.common.moduleinstall.ModuleInstallStatusUpdate.InstallState.STATE_COMPLETED
65+ import com.google.android.gms.common.moduleinstall.ModuleInstallStatusUpdate.InstallState.STATE_FAILED
5966import com.google.android.material.dialog.MaterialAlertDialogBuilder
6067import com.google.android.material.snackbar.Snackbar
68+ import com.google.mlkit.vision.barcode.BarcodeScanning
6169import eu.pretix.libpretixsync.api.PretixApi
6270import eu.pretix.libpretixsync.check.CheckException
6371import eu.pretix.libpretixsync.check.OnlineCheckProvider
@@ -528,6 +536,66 @@ class MainActivity : AppCompatActivity(), ReloadableActivity, ScannerView.Result
528536 pendingPinAction?.let { it(pin) }
529537 }
530538 }
539+
540+ if (conf.scanEngine == " mlkit" ) {
541+ val moduleInstallClient = ModuleInstall .getClient(this )
542+ val barcodeModule = BarcodeScanning .getClient()
543+ moduleInstallClient.areModulesAvailable(
544+ barcodeModule
545+ )
546+ class ModuleInstallProgressListener : InstallStatusListener {
547+ override fun onInstallStatusUpdated (update : ModuleInstallStatusUpdate ) {
548+ // Progress info is only set when modules are in the progress of downloading.
549+ update.progressInfo?.let {
550+ val progress = (it.bytesDownloaded * 100 / it.totalBytesToDownload).toInt()
551+ // Set the progress for the progress bar.
552+ // progressBar.setProgress(progress)
553+ }
554+
555+ if (isTerminateState(update.installState)) {
556+ moduleInstallClient.unregisterListener(this )
557+ }
558+ if (update.installState == STATE_COMPLETED ) {
559+ // -> switch engine
560+ switchScanEngine()
561+ }
562+ }
563+
564+ fun isTerminateState (@ModuleInstallStatusUpdate.InstallState state : Int ): Boolean {
565+ return state == STATE_CANCELED || state == STATE_COMPLETED || state == STATE_FAILED
566+ }
567+ }
568+
569+ val listener = ModuleInstallProgressListener ()
570+ val moduleInstallRequest = ModuleInstallRequest .newBuilder()
571+ .addApi(barcodeModule)
572+ .setListener(listener)
573+ .build()
574+
575+ moduleInstallClient
576+ .installModules(moduleInstallRequest)
577+ .addOnSuccessListener {
578+ if (it.areModulesAlreadyInstalled()) {
579+ // Modules are already installed when the request is sent.
580+ // -> switch engine
581+ switchScanEngine()
582+ }
583+ // The install request has been sent successfully. This does not mean
584+ // the installation is completed. To monitor the install status, set an
585+ // InstallStatusListener to the ModuleInstallRequest.
586+ }
587+ .addOnFailureListener {
588+ // Handle failure…
589+ }
590+ }
591+ }
592+
593+ private fun switchScanEngine () {
594+ when (conf.scanEngine) {
595+ " zxing" -> binding.scannerView.setAnalyzer(ScannerView .Companion .ANALYZER .ZXING )
596+ " mlkit" -> binding.scannerView.setAnalyzer(ScannerView .Companion .ANALYZER .MLKIT )
597+ else -> {}
598+ }
531599 }
532600
533601 private fun eventButtonText (): String {
0 commit comments