Skip to content

Commit

Permalink
AND-9232 add logs, move eneble nfc onResume lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
kozarezvlad committed Nov 26, 2024
1 parent f6fc9d5 commit 64ce2f5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ package com.tangem.sdk.nfc

import android.nfc.tech.IsoDep
import com.tangem.Log
import java.io.IOException

internal fun IsoDep.connectInternal() {
internal fun IsoDep.connectInternal(onError: () -> Unit) {
Log.nfc { "connectInternal" }
this.connect()
try {
this.connect()
} catch (e: IOException) {
Log.nfc { "connectInternal error $e" }
onError()
}
}

internal fun IsoDep.closeInternal() {
internal fun IsoDep.closeInternal(onError: () -> Unit) {
Log.nfc { "closeInternal" }
this.close()
try {
this.close()
} catch (e: IOException) {
Log.nfc { "closeInternal error $e" }
onError()
}
}
23 changes: 19 additions & 4 deletions tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.lifecycle.LifecycleOwner
import com.tangem.Log
import com.tangem.common.extensions.VoidCallback
import com.tangem.common.nfc.ReadingActiveListener
import kotlinx.coroutines.plus

/**
* Helps use NFC, leveraging Android NFC functionality.
Expand Down Expand Up @@ -75,16 +74,28 @@ class NfcManager : NfcAdapter.ReaderCallback, ReadingActiveListener, DefaultLife
}

override fun onStart(owner: LifecycleOwner) {
enableReaderModeIfNfcEnabled()
reader.listener = this
}

override fun onStop(owner: LifecycleOwner) {
override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
enableReaderModeIfNfcEnabled()
}

override fun onPause(owner: LifecycleOwner) {
super.onPause(owner)
disableReaderMode()
}

override fun onStop(owner: LifecycleOwner) {
reader.stopSession(true)
reader.listener = null
}

override fun onStartSession() {
enableReaderModeIfNfcEnabled()
}

override fun onDestroy(owner: LifecycleOwner) {
activity?.unregisterReceiver(mBroadcastReceiver)
activity = null
Expand Down Expand Up @@ -116,7 +127,11 @@ class NfcManager : NfcAdapter.ReaderCallback, ReadingActiveListener, DefaultLife
private fun ignoreTag(tag: Tag?) {
Log.nfc { "NFC tag is ignored" }
nfcAdapter?.ignore(tag, IGNORE_DEBOUNCE_MS, null, null)
IsoDep.get(tag)?.closeInternal()
IsoDep.get(tag)?.closeInternal(
onError = {
Log.nfc { "ignoreTag close failure" }
},
)
}

private companion object {
Expand Down
22 changes: 16 additions & 6 deletions tangem-sdk-android/src/main/java/com/tangem/sdk/nfc/NfcReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class NfcReader : CardReader {
scope?.launchWithLock(readerMutex) {
Log.nfc { "start NFC session, thread ${Thread.currentThread().id}" }
nfcTag = null
listener?.onStartSession()
listener?.readingIsActive = true
}
}
Expand All @@ -62,6 +63,7 @@ class NfcReader : CardReader {
override fun resumeSession() {
scope?.launchWithLock(readerMutex) {
Log.nfc { "resume NFC session, thread ${Thread.currentThread().id}" }
listener?.onStartSession()
listener?.readingIsActive = true
}
}
Expand All @@ -73,24 +75,32 @@ class NfcReader : CardReader {
return@launchWithLock
}
IsoDep.get(tag)?.let { isoDep ->
connect(isoDep)
nfcTag = NfcTag(TagType.Nfc, isoDep)
connect(
isoDep,
onSuccess = {
nfcTag = NfcTag(TagType.Nfc, isoDep)
},
onError = {
nfcTag = null
},
)
}
}
}

private suspend fun connect(isoDep: IsoDep) {
private suspend fun connect(isoDep: IsoDep, onSuccess: (IsoDep) -> Unit, onError: () -> Unit) {
Log.nfc { "connect" }
if (isoDep.isConnected) {
Log.nfc { "already connected close and reconnect" }
isoDep.closeInternal()
isoDep.closeInternal(onError)
delay(CONNECTION_DELAY)
isoDep.connectInternal()
isoDep.connectInternal(onError)
} else {
isoDep.connectInternal()
isoDep.connectInternal(onError)
Log.nfc { "connected" }
}
isoDep.timeout = ISO_DEP_TIMEOUT_MS
onSuccess(isoDep)
}

override fun stopSession(cancelled: Boolean) {
Expand Down
1 change: 1 addition & 0 deletions tangem-sdk-core/src/main/java/com/tangem/TangemSdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,7 @@ class TangemSdk(
accessCode = accessCode,
preflightReadFilter = CardIdPreflightReadFilter.initOrNull(cardId),
)

cardSession?.start(onSessionStarted = callback)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ interface CardReader {

interface ReadingActiveListener {
var readingIsActive: Boolean

fun onStartSession()
}

0 comments on commit 64ce2f5

Please sign in to comment.