Merge pull request #461 from meshtastic/scan

fix code scanner choice by install source
master
Andre Kirchhoff 2022-08-14 20:25:27 -03:00 zatwierdzone przez GitHub
commit d93916368c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 22 dodań i 31 usunięć

Wyświetl plik

@ -220,29 +220,6 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
barcodeLauncher.launch(zxingScan)
}
fun mlkitScan() {
debug("Starting ML Kit QR code scanner")
val options = GmsBarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_QR_CODE
)
.build()
val scanner = GmsBarcodeScanning.getClient(requireContext(), options)
scanner.startScan()
.addOnSuccessListener { barcode ->
if (barcode.rawValue != null)
model.setRequestChannelUrl(Uri.parse(barcode.rawValue))
}
.addOnFailureListener { ex ->
errormsg("ML Kit scanner error: ${ex.message}")
Snackbar.make(
requireView(),
"${ex.message}",
Snackbar.LENGTH_SHORT
).show()
}
}
val requestPermissionAndScanLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
if (permissions.entries.all { it.value }) zxingScan()
@ -261,6 +238,24 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
.show()
}
fun mlkitScan() {
debug("Starting ML Kit code scanner")
val options = GmsBarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
.build()
val scanner = GmsBarcodeScanning.getClient(requireContext(), options)
scanner.startScan()
.addOnSuccessListener { barcode ->
if (barcode.rawValue != null)
model.setRequestChannelUrl(Uri.parse(barcode.rawValue))
}
.addOnFailureListener { ex ->
errormsg("code scanner failed: ${ex.message}")
if (requireContext().hasCameraPermission()) zxingScan()
else requestPermissionAndScan()
}
}
binding.channelNameEdit.on(EditorInfo.IME_ACTION_DONE) {
requireActivity().hideKeyboard()
}
@ -284,16 +279,12 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
}
binding.scanButton.setOnClickListener {
val installSource = requireContext().installSource()
debug("Using scanner for installSource: $installSource")
if (installSource != null) { // only use ML Kit when install from PlayStore
// only use ML Kit for play store installs
if (requireContext().installSource() == "com.android.vending") {
mlkitScan()
} else {
if (requireContext().hasCameraPermission()) {
zxingScan()
} else {
requestPermissionAndScan()
}
if (requireContext().hasCameraPermission()) zxingScan()
else requestPermissionAndScan()
}
}