@@ -27,7 +27,6 @@ import android.content.pm.ActivityInfo
2727import androidx.lifecycle.LiveData
2828import androidx.lifecycle.MutableLiveData
2929import androidx.lifecycle.ViewModel
30- import androidx.lifecycle.viewModelScope
3130import com.google.common.collect.ImmutableMap
3231import dagger.hilt.android.lifecycle.HiltViewModel
3332import ee.ria.DigiDoc.R
@@ -59,7 +58,6 @@ import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.debugLog
5958import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.errorLog
6059import ee.ria.libdigidocpp.ExternalSigner
6160import kotlinx.coroutines.Dispatchers.Main
62- import kotlinx.coroutines.launch
6361import kotlinx.coroutines.runBlocking
6462import kotlinx.coroutines.withContext
6563import org.bouncycastle.util.encoders.Hex
@@ -279,7 +277,7 @@ class NFCViewModel
279277 _signStatus .postValue(true )
280278 _signedContainer .postValue(container)
281279 } catch (ex: SmartCardReaderException ) {
282- handlePin2SmartCardReaderException (ex, pinType)
280+ handleSmartCardReaderException (ex, CodeType . PIN2 , pinType)
283281 } catch (ex: Exception ) {
284282 _signStatus .postValue(false )
285283 _shouldResetPIN .postValue(true )
@@ -367,7 +365,7 @@ class NFCViewModel
367365 _cryptoContainer .postValue(decryptedContainer)
368366 } catch (ex: SmartCardReaderException ) {
369367 _decryptStatus .postValue(false )
370- handlePin1SmartCardReaderException (ex, pinType)
368+ handleSmartCardReaderException (ex, CodeType . PIN1 , pinType)
371369 } catch (ex: Exception ) {
372370 _decryptStatus .postValue(false )
373371 _shouldResetPIN .postValue(true )
@@ -419,9 +417,7 @@ class NFCViewModel
419417 checkNFCStatus(
420418 nfcSmartCardReaderManager.startDiscovery(activity) { nfcReader, exc ->
421419 if ((nfcReader != null ) && (exc == null )) {
422- viewModelScope.launch {
423- _message .postValue(R .string.signature_update_nfc_detected)
424- }
420+ _message .postValue(R .string.signature_update_nfc_detected)
425421 try {
426422 val card = TokenWithPace .create(nfcReader)
427423 card.tunnel(canNumber)
@@ -510,7 +506,7 @@ class NFCViewModel
510506 _shouldResetPIN .postValue(true )
511507 _webEidAuthResult .postValue(Triple (authCert, signingCert, signatureArray))
512508 } catch (ex: SmartCardReaderException ) {
513- handlePin1SmartCardReaderException (ex, pinType)
509+ handleSmartCardReaderException (ex, CodeType . PIN1 , pinType)
514510 } catch (ex: Exception ) {
515511 _shouldResetPIN .postValue(true )
516512
@@ -637,7 +633,7 @@ class NFCViewModel
637633 Triple (signerCertB64, signatureArray, responseUri),
638634 )
639635 } catch (ex: SmartCardReaderException ) {
640- handlePin2SmartCardReaderException (ex, pinType)
636+ handleSmartCardReaderException (ex, CodeType . PIN2 , pinType)
641637 } catch (ex: Exception ) {
642638 _signStatus .postValue(false )
643639 _shouldResetPIN .postValue(true )
@@ -735,118 +731,59 @@ class NFCViewModel
735731 errorLog(logTag, " Unable to perform with NFC: ${e.message} " , e)
736732 }
737733
738- private fun handlePin1SmartCardReaderException (
734+ private fun handleSmartCardReaderException (
739735 ex : SmartCardReaderException ,
736+ codeType : CodeType ,
740737 pinType : String ,
741738 ) {
742- if (ex.message?.contains(" TagLostException" ) == true ) {
743- _errorState .postValue(Triple (R .string.signature_update_nfc_tag_lost, null , null ))
744- } else if (ex.message?.contains(" PIN1 verification failed" ) == true &&
745- ex.message?.contains(" Retries left: 2" ) == true
746- ) {
747- _shouldResetPIN .postValue(true )
748- _errorState .postValue(
749- Triple (
750- R .string.id_card_sign_pin_invalid,
751- pinType,
752- 2 ,
753- ),
754- )
755- } else if (ex.message?.contains(" PIN1 verification failed" ) == true &&
756- ex.message?.contains(" Retries left: 1" ) == true
757- ) {
758- _shouldResetPIN .postValue(true )
759- _errorState .postValue(
760- Triple (
761- R .string.id_card_sign_pin_invalid_final,
762- pinType,
763- null ,
764- ),
765- )
766- } else if (ex.message?.contains(" PIN1 verification failed" ) == true &&
767- ex.message?.contains(" Retries left: 0" ) == true
768- ) {
769- _shouldResetPIN .postValue(true )
770- _errorState .postValue(
771- Triple (
772- R .string.id_card_sign_pin_locked,
773- pinType,
774- null ,
775- ),
776- )
777- } else if (ex is ApduResponseException ) {
778- _errorState .postValue(
779- Triple (R .string.signature_update_nfc_technical_error, null , null ),
780- )
781- } else if (ex is PaceTunnelException ) {
782- _errorState .postValue(
783- Triple (R .string.signature_update_nfc_wrong_can, null , null ),
784- )
785- } else {
786- showTechnicalError(ex)
739+ val pinName = codeType.name
740+ val isSigning = codeType == CodeType .PIN2
741+
742+ if (isSigning) {
743+ _signStatus .postValue(false )
787744 }
788745
789- errorLog(logTag, " Exception: " + ex.message, ex)
790- }
746+ when {
747+ ex.message?.contains(" TagLostException" ) == true -> {
748+ _errorState .postValue(Triple (R .string.signature_update_nfc_tag_lost, null , null ))
749+ }
791750
792- private fun handlePin2SmartCardReaderException (
793- ex : SmartCardReaderException ,
794- pinType : String ,
795- ) {
796- _signStatus .postValue(false )
797-
798- if (ex.message?.contains(" TagLostException" ) == true ) {
799- _errorState .postValue(
800- Triple (
801- R .string.signature_update_nfc_tag_lost,
802- null ,
803- null ,
804- ),
805- )
806- } else if (ex.message?.contains(" PIN2 has not been changed" ) == true ) {
807- _dialogError .postValue(R .string.sign_blocked_pin2_unchanged_message)
808- } else if (ex.message?.contains(" PIN2 verification failed" ) == true &&
809- ex.message?.contains(" Retries left: 2" ) == true
810- ) {
811- _shouldResetPIN .postValue(true )
812- _errorState .postValue(
813- Triple (
814- R .string.id_card_sign_pin_invalid,
815- pinType,
816- 2 ,
817- ),
818- )
819- } else if (ex.message?.contains(" PIN2 verification failed" ) == true &&
820- ex.message?.contains(" Retries left: 1" ) == true
821- ) {
822- _shouldResetPIN .postValue(true )
823- _errorState .postValue(
824- Triple (
825- R .string.id_card_sign_pin_invalid_final,
826- pinType,
827- null ,
828- ),
829- )
830- } else if (ex.message?.contains(" PIN2 verification failed" ) == true &&
831- ex.message?.contains(" Retries left: 0" ) == true
832- ) {
833- _shouldResetPIN .postValue(true )
834- _errorState .postValue(
835- Triple (R .string.id_card_sign_pin_locked, pinType, null ),
836- )
837- } else if (ex is ApduResponseException ) {
838- _errorState .postValue(
839- Triple (R .string.signature_update_nfc_technical_error, null , null ),
840- )
841- } else if (ex is PaceTunnelException ) {
842- _errorState .postValue(
843- Triple (R .string.signature_update_nfc_wrong_can, null , null ),
844- )
845- } else {
846- showTechnicalError(ex)
751+ isSigning && ex.message?.contains(" PIN2 has not been changed" ) == true -> {
752+ _dialogError .postValue(R .string.sign_blocked_pin2_unchanged_message)
753+ }
754+
755+ ex.message?.contains(" $pinName verification failed" ) == true &&
756+ ex.message?.contains(" Retries left: 2" ) == true -> {
757+ _shouldResetPIN .postValue(true )
758+ _errorState .postValue(Triple (R .string.id_card_sign_pin_invalid, pinType, 2 ))
759+ }
760+
761+ ex.message?.contains(" $pinName verification failed" ) == true &&
762+ ex.message?.contains(" Retries left: 1" ) == true -> {
763+ _shouldResetPIN .postValue(true )
764+ _errorState .postValue(Triple (R .string.id_card_sign_pin_invalid_final, pinType, null ))
765+ }
766+
767+ ex.message?.contains(" $pinName verification failed" ) == true &&
768+ ex.message?.contains(" Retries left: 0" ) == true -> {
769+ _shouldResetPIN .postValue(true )
770+ _errorState .postValue(Triple (R .string.id_card_sign_pin_locked, pinType, null ))
771+ }
772+
773+ ex is ApduResponseException -> {
774+ _errorState .postValue(Triple (R .string.signature_update_nfc_technical_error, null , null ))
775+ }
776+
777+ ex is PaceTunnelException -> {
778+ _errorState .postValue(Triple (R .string.signature_update_nfc_wrong_can, null , null ))
779+ }
780+
781+ else -> {
782+ showTechnicalError(ex)
783+ }
847784 }
848785
849- errorLog(logTag, " Exception: " + ex.message, ex)
786+ errorLog(logTag, " Exception: ${ ex.message} " , ex)
850787 }
851788
852789 private fun handleGeneralException (ex : Exception ): Boolean {
0 commit comments