Skip to content

Commit

Permalink
Merge pull request #333 from tangem/bugfix/AND-4628_move_nfc_enabled_…
Browse files Browse the repository at this point in the history
…check

AND-4628 Moved NFC enable check
  • Loading branch information
kozarezvlad authored Jan 11, 2024
2 parents d86f1e0 + 6c5ceac commit a89cfbe
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class DemoActivity : AppCompatActivity() {
val nfcManager = TangemSdk.initNfcManager(this)
val authenticationManager = TangemSdk.initBiometricManager(this)

val viewDelegate = DefaultSessionViewDelegate(nfcManager, nfcManager.reader, this)
val viewDelegate = DefaultSessionViewDelegate(nfcManager, this)
viewDelegate.sdkConfig = config
this.viewDelegate = viewDelegate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import com.tangem.common.core.CompletionCallback
import com.tangem.common.core.Config
import com.tangem.common.core.TangemError
import com.tangem.common.extensions.VoidCallback
import com.tangem.common.nfc.CardReader
import com.tangem.operations.resetcode.ResetCodesViewDelegate
import com.tangem.sdk.extensions.sdkThemeContext
import com.tangem.sdk.nfc.NfcAntennaLocationProvider
import com.tangem.sdk.nfc.NfcManager
import com.tangem.sdk.ui.AttestationFailedDialog
import com.tangem.sdk.ui.NfcEnableDialog
import com.tangem.sdk.ui.NfcSessionDialog

/**
Expand All @@ -27,7 +27,6 @@ import com.tangem.sdk.ui.NfcSessionDialog
*/
class DefaultSessionViewDelegate(
private val nfcManager: NfcManager,
private val reader: CardReader,
private val activity: Activity,
) : SessionViewDelegate {

Expand All @@ -36,6 +35,7 @@ class DefaultSessionViewDelegate(
override val resetCodesViewDelegate: ResetCodesViewDelegate = AndroidResetCodesViewDelegate(activity)

private var readingDialog: NfcSessionDialog? = null
private var nfcEnableDialog: NfcEnableDialog? = null
private var stoppedBySession: Boolean = false

override fun onSessionStarted(
Expand All @@ -46,6 +46,17 @@ class DefaultSessionViewDelegate(
) {
Log.view { "session started" }
createAndShowState(SessionViewDelegateState.Ready(formatCardId(cardId)), enableHowTo, message, iconScanRes)
checkNfcEnabled()
}

private fun checkNfcEnabled() {
postUI(msTime = 500) {
if (!nfcManager.isNfcEnabled) {
nfcEnableDialog?.cancel()
nfcEnableDialog = NfcEnableDialog()
activity.let { nfcEnableDialog?.show(it) }
}
}
}

override fun onSessionStopped(message: Message?) {
Expand Down Expand Up @@ -179,7 +190,7 @@ class DefaultSessionViewDelegate(
stoppedBySession = false
create()
setOnCancelListener {
if (!stoppedBySession) reader.stopSession(true)
if (!stoppedBySession) nfcManager.reader.stopSession(true)
readingDialog = null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fun TangemSdk.Companion.init(activity: ComponentActivity, config: Config = Confi
val secureStorage = SecureStorage.create(activity)
val nfcManager = TangemSdk.initNfcManager(activity)

val viewDelegate = DefaultSessionViewDelegate(nfcManager, nfcManager.reader, activity)
val viewDelegate = DefaultSessionViewDelegate(nfcManager, activity)
viewDelegate.sdkConfig = config

return TangemSdk(
Expand All @@ -49,7 +49,7 @@ fun TangemSdk.Companion.initWithBiometrics(activity: FragmentActivity, config: C
val nfcManager = TangemSdk.initNfcManager(activity)
val authenticationManager = initBiometricManager(activity)

val viewDelegate = DefaultSessionViewDelegate(nfcManager, nfcManager.reader, activity)
val viewDelegate = DefaultSessionViewDelegate(nfcManager, activity)
viewDelegate.sdkConfig = config

return TangemSdk(
Expand All @@ -71,7 +71,7 @@ fun TangemSdk.Companion.customDelegate(
val secureStorage = SecureStorage.create(activity)
val nfcManager = TangemSdk.initNfcManager(activity)

val safeViewDelegate = viewDelegate ?: DefaultSessionViewDelegate(nfcManager, nfcManager.reader, activity).apply {
val safeViewDelegate = viewDelegate ?: DefaultSessionViewDelegate(nfcManager, activity).apply {
this.sdkConfig = config
}

Expand Down
37 changes: 18 additions & 19 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 @@ -13,7 +13,6 @@ import android.os.Bundle
import com.tangem.Log
import com.tangem.common.extensions.VoidCallback
import com.tangem.common.nfc.ReadingActiveListener
import com.tangem.sdk.ui.NfcEnableDialog

/**
* Helps use NFC, leveraging Android NFC functionality.
Expand All @@ -33,10 +32,12 @@ class NfcManager : NfcAdapter.ReaderCallback, ReadingActiveListener {

val reader = NfcReader()

val isNfcEnabled: Boolean
get() = nfcAdapter?.isEnabled == true

private val onTagDiscoveredListeners: MutableList<VoidCallback> = mutableListOf()
private var activity: Activity? = null
private var nfcAdapter: NfcAdapter? = null
private var nfcEnableDialog: NfcEnableDialog? = null

private val mBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Expand All @@ -49,6 +50,12 @@ class NfcManager : NfcAdapter.ReaderCallback, ReadingActiveListener {
}
}

override fun onTagDiscovered(tag: Tag?) {
Log.debug { "NFC tag is discovered" }
onTagDiscoveredListeners.forEach { it.invoke() }
if (readingIsActive) reader.onTagDiscovered(tag) else ignoreTag(tag)
}

fun addTagDiscoveredListener(listener: VoidCallback) {
onTagDiscoveredListeners.add(listener)
}
Expand All @@ -62,29 +69,13 @@ class NfcManager : NfcAdapter.ReaderCallback, ReadingActiveListener {
nfcAdapter = NfcAdapter.getDefaultAdapter(activity)
}

override fun onTagDiscovered(tag: Tag?) {
Log.debug { "NFC tag is discovered" }
onTagDiscoveredListeners.forEach { it.invoke() }
if (readingIsActive) reader.onTagDiscovered(tag) else ignoreTag(tag)
}

fun onStart() {
val filter = IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)
activity?.registerReceiver(mBroadcastReceiver, filter)
handleNfcEnabled(nfcAdapter?.isEnabled == true)
enableReaderModeIfEnabled()
reader.listener = this
}

private fun handleNfcEnabled(nfcEnabled: Boolean) {
if (nfcEnabled) {
enableReaderMode()
nfcEnableDialog?.cancel()
} else {
nfcEnableDialog = NfcEnableDialog()
activity?.let { nfcEnableDialog?.show(it) }
}
}

fun onStop() {
activity?.unregisterReceiver(mBroadcastReceiver)
reader.stopSession(true)
Expand All @@ -97,6 +88,14 @@ class NfcManager : NfcAdapter.ReaderCallback, ReadingActiveListener {
nfcAdapter = null
}

private fun enableReaderModeIfEnabled() {
val nfcEnabled = nfcAdapter?.isEnabled == true

if (nfcEnabled) {
enableReaderMode()
}
}

private fun enableReaderMode() {
nfcAdapter?.enableReaderMode(activity, this, READER_FLAGS, Bundle())
}
Expand Down

0 comments on commit a89cfbe

Please sign in to comment.