workaround - restart nfc discovery for add account

This commit is contained in:
Adam Velebil 2022-07-28 15:12:11 +02:00
parent 4fc7db106c
commit d064fe357f
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
4 changed files with 55 additions and 19 deletions

View File

@ -40,32 +40,45 @@ class MainActivity : FlutterFragmentActivity() {
viewModel.handleYubiKey.observe(this) {
if (it) {
Log.d(TAG, "Starting usb discovery")
yubikit.startUsbDiscovery(UsbConfiguration()) { device ->
viewModel.yubiKeyDevice.postValue(device)
device.setOnClosed { viewModel.yubiKeyDevice.postValue(null) }
}
hasNfc = try {
yubikit.startNfcDiscovery(nfcConfiguration, this) { device ->
viewModel.yubiKeyDevice.apply {
lifecycleScope.launch(Dispatchers.Main) {
value = device
postValue(null)
}
}
}
true
} catch (e: NfcNotAvailable) {
false
}
hasNfc = startNfcDiscovery()
} else {
yubikit.stopNfcDiscovery(this)
stopNfcDiscovery()
yubikit.stopUsbDiscovery()
Log.d(TAG, "Stopped usb discovery")
}
}
setupYubiKitLogger()
}
fun startNfcDiscovery(): Boolean =
try {
Log.d(TAG, "Starting nfc discovery")
yubikit.startNfcDiscovery(nfcConfiguration, this) { device ->
viewModel.yubiKeyDevice.apply {
lifecycleScope.launch(Dispatchers.Main) {
value = device
postValue(null)
}
}
}
true
} catch (e: NfcNotAvailable) {
false
}
fun stopNfcDiscovery() {
if (hasNfc) {
yubikit.stopNfcDiscovery(this)
Log.d(TAG, "Stopped nfc discovery")
}
}
private fun setupYubiKitLogger() {
Logger.setLogger(object : Logger() {
private val TAG = "yubikit"
@ -85,6 +98,7 @@ class MainActivity : FlutterFragmentActivity() {
private lateinit var oathManager: OathManager
private lateinit var dialogManager: DialogManager
private lateinit var flutterLog: FlutterLog
private lateinit var nfcDiscoveryHelper: NfcDiscoveryHelper
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
@ -94,8 +108,12 @@ class MainActivity : FlutterFragmentActivity() {
flutterLog = FlutterLog(messenger)
appContext = AppContext(messenger)
dialogManager = DialogManager(messenger, this.lifecycleScope)
nfcDiscoveryHelper = NfcDiscoveryHelper(this)
oathManager = OathManager(this, messenger, appContext, viewModel, dialogManager)
oathManager = OathManager(this, messenger, appContext, viewModel, dialogManager, nfcDiscoveryHelper)
}
companion object {
const val TAG = "MainActivity"
}
}

View File

@ -0,0 +1,16 @@
package com.yubico.authenticator
class NfcDiscoveryHelper(private val mainActivity: MainActivity) {
private fun startDiscovery() {
mainActivity.startNfcDiscovery()
}
private fun stopDiscovery() {
mainActivity.stopNfcDiscovery()
}
fun restartDiscovery() {
stopDiscovery()
startDiscovery()
}
}

View File

@ -30,7 +30,8 @@ class OathManager(
messenger: BinaryMessenger,
appContext: AppContext,
private val appViewModel: MainViewModel,
private val dialogManager: DialogManager
private val dialogManager: DialogManager,
private val nfcDiscoveryHelper: NfcDiscoveryHelper
) : OathApi {
private val _dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
@ -229,6 +230,7 @@ class OathManager(
requireTouch: Boolean,
result: Result<String>
) {
nfcDiscoveryHelper.restartDiscovery()
coroutineScope.launch {
try {
useOathSession("Add account", true) { session ->

View File

@ -1,8 +1,8 @@
group 'com.yubico.authenticator.flutter_plugins.qrscanner_zxing'
version '1.0-SNAPSHOT'
version '1.0'
buildscript {
ext.kotlin_version = '1.6.21'
ext.kotlin_version = '1.7.10'
repositories {
google()
mavenCentral()
@ -46,7 +46,7 @@ android {
}
dependencies {
def camerax_version = "1.1.0-beta03"
def camerax_version = "1.1.0"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"