Skip to content

Commit b3b868b

Browse files
NFC-83 Remove CoroutineScope(Main) from NFCViewModel. Disable NFC reader in ViewModel onCleared
1 parent a559c39 commit b3b868b

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

app/src/main/kotlin/ee/ria/DigiDoc/viewmodel/NFCViewModel.kt

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import ee.ria.DigiDoc.utilsLib.extensions.clearSensitive
5858
import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.debugLog
5959
import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.errorLog
6060
import ee.ria.libdigidocpp.ExternalSigner
61-
import kotlinx.coroutines.CoroutineScope
6261
import kotlinx.coroutines.Dispatchers.Main
6362
import kotlinx.coroutines.launch
6463
import kotlinx.coroutines.runBlocking
@@ -256,9 +255,8 @@ class NFCViewModel
256255
nfcSmartCardReaderManager.startDiscovery(activity) { nfcReader, exc ->
257256
if ((nfcReader != null) && (exc == null)) {
258257
try {
259-
CoroutineScope(Main).launch {
260-
_message.postValue(R.string.signature_update_nfc_detected)
261-
}
258+
_message.postValue(R.string.signature_update_nfc_detected)
259+
262260
val card = TokenWithPace.create(nfcReader)
263261
card.tunnel(canNumber)
264262
val signerCert = card.certificate(CertificateType.SIGNING)
@@ -285,11 +283,9 @@ class NFCViewModel
285283
signatureArray,
286284
)
287285

288-
CoroutineScope(Main).launch {
289-
_shouldResetPIN.postValue(true)
290-
_signStatus.postValue(true)
291-
_signedContainer.postValue(container)
292-
}
286+
_shouldResetPIN.postValue(true)
287+
_signStatus.postValue(true)
288+
_signedContainer.postValue(container)
293289
} catch (ex: SmartCardReaderException) {
294290
handlePin2SmartCardReaderException(ex, pinType)
295291
} catch (ex: Exception) {
@@ -350,9 +346,8 @@ class NFCViewModel
350346
nfcSmartCardReaderManager.startDiscovery(activity) { nfcReader, exc ->
351347
if ((nfcReader != null) && (exc == null)) {
352348
try {
353-
CoroutineScope(Main).launch {
354-
_message.postValue(R.string.signature_update_nfc_detected)
355-
}
349+
_message.postValue(R.string.signature_update_nfc_detected)
350+
356351
val card = TokenWithPace.create(nfcReader)
357352
card.tunnel(canNumber)
358353

@@ -374,11 +369,10 @@ class NFCViewModel
374369
configurationRepository,
375370
)
376371
pin1Code.clearSensitive()
377-
CoroutineScope(Main).launch {
378-
_shouldResetPIN.postValue(true)
379-
_decryptStatus.postValue(true)
380-
_cryptoContainer.postValue(decryptedContainer)
381-
}
372+
373+
_shouldResetPIN.postValue(true)
374+
_decryptStatus.postValue(true)
375+
_cryptoContainer.postValue(decryptedContainer)
382376
} catch (ex: SmartCardReaderException) {
383377
_decryptStatus.postValue(false)
384378
handlePin1SmartCardReaderException(ex, pinType)
@@ -506,9 +500,7 @@ class NFCViewModel
506500
nfcSmartCardReaderManager.startDiscovery(activity) { nfcReader, exc ->
507501
if ((nfcReader != null) && (exc == null)) {
508502
try {
509-
CoroutineScope(Main).launch {
510-
_message.postValue(R.string.signature_update_nfc_detected)
511-
}
503+
_message.postValue(R.string.signature_update_nfc_detected)
512504

513505
val card = TokenWithPace.create(nfcReader)
514506
card.tunnel(canNumber)
@@ -522,13 +514,9 @@ class NFCViewModel
522514
)
523515

524516
pin1Code.clearSensitive()
525-
debugLog(logTag, "Auth certificate: " + Base64.getEncoder().encodeToString(authCert))
526-
debugLog(logTag, "Auth signature: " + Hex.toHexString(signatureArray))
527517

528-
CoroutineScope(Main).launch {
529-
_shouldResetPIN.postValue(true)
530-
_webEidAuthResult.postValue(Triple(authCert, signingCert, signatureArray))
531-
}
518+
_shouldResetPIN.postValue(true)
519+
_webEidAuthResult.postValue(Triple(authCert, signingCert, signatureArray))
532520
} catch (ex: SmartCardReaderException) {
533521
handlePin1SmartCardReaderException(ex, pinType)
534522
} catch (ex: Exception) {
@@ -574,19 +562,15 @@ class NFCViewModel
574562
nfcSmartCardReaderManager.startDiscovery(activity) { nfcReader, exc ->
575563
if ((nfcReader != null) && (exc == null)) {
576564
try {
577-
CoroutineScope(Main).launch {
578-
_message.postValue(R.string.signature_update_nfc_detected)
579-
}
565+
_message.postValue(R.string.signature_update_nfc_detected)
580566

581567
val card = TokenWithPace.create(nfcReader)
582568
card.tunnel(canNumber)
583569

584570
val signingCert = card.certificate(CertificateType.SIGNING)
585571
val signingCertB64 = Base64.getEncoder().encodeToString(signingCert)
586572

587-
CoroutineScope(Main).launch {
588-
_webEidCertificateResult.postValue(signingCertB64)
589-
}
573+
_webEidCertificateResult.postValue(signingCertB64)
590574
} catch (ex: SmartCardReaderException) {
591575
if (ex.message?.contains("TagLostException") == true) {
592576
_errorState.postValue(Triple(R.string.signature_update_nfc_tag_lost, null, null))
@@ -646,23 +630,20 @@ class NFCViewModel
646630
nfcSmartCardReaderManager.startDiscovery(activity) { nfcReader, exc ->
647631
if ((nfcReader != null) && (exc == null)) {
648632
try {
649-
CoroutineScope(Main).launch {
650-
_message.postValue(R.string.signature_update_nfc_detected)
651-
}
633+
_message.postValue(R.string.signature_update_nfc_detected)
634+
652635
val card = TokenWithPace.create(nfcReader)
653636
card.tunnel(canNumber)
654637
val signerCert = card.certificate(CertificateType.SIGNING)
655638
val signerCertB64 = Base64.getEncoder().encodeToString(signerCert)
656639
val hashBytes = Base64.getDecoder().decode(hash)
657640
val (_, signatureArray) = idCardService.sign(card, pin2Code, hashBytes)
658641

659-
CoroutineScope(Main).launch {
660-
_shouldResetPIN.postValue(true)
661-
_signStatus.postValue(true)
662-
_webEidSignResult.postValue(
663-
Triple(signerCertB64, signatureArray, responseUri),
664-
)
665-
}
642+
_shouldResetPIN.postValue(true)
643+
_signStatus.postValue(true)
644+
_webEidSignResult.postValue(
645+
Triple(signerCertB64, signatureArray, responseUri),
646+
)
666647
} catch (ex: SmartCardReaderException) {
667648
handlePin2SmartCardReaderException(ex, pinType)
668649
} catch (ex: Exception) {
@@ -906,4 +887,9 @@ class NFCViewModel
906887
errorLog(logTag, "Exception: ${ex.message}", ex)
907888
}
908889
}
890+
891+
override fun onCleared() {
892+
super.onCleared()
893+
nfcSmartCardReaderManager.disableNfcReaderMode()
894+
}
909895
}

0 commit comments

Comments
 (0)