Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AND-9232 add logs, move eneble nfc onResume lifecycle #425

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
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
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()
}