commit ddc3edd468f7947c723a4d0aeb7ba5f3b7125fa7
parent bb1cb25c9f3f60550eff94c388ce2b83e5953852
Author: Iván Ávalos <avalos@disroot.org>
Date: Tue, 17 Feb 2026 23:50:29 +0100
[wallet] improved NFC service code
Diffstat:
4 files changed, 39 insertions(+), 22 deletions(-)
diff --git a/cashier/src/main/java/net/taler/cashier/withdraw/TransactionFragment.kt b/cashier/src/main/java/net/taler/cashier/withdraw/TransactionFragment.kt
@@ -77,14 +77,14 @@ class TransactionFragment : Fragment() {
override fun onDestroy() {
super.onDestroy()
- TalerNfcService.clearUri(requireActivity())
+ TalerNfcService.clearNdefPayload(requireActivity())
if (!requireActivity().isChangingConfigurations) {
withdrawManager.abort()
}
}
private fun onWithdrawResultReceived(result: WithdrawResult?) {
- TalerNfcService.clearUri(requireActivity())
+ TalerNfcService.clearNdefPayload(requireActivity())
if (result != null) {
ui.progressBar.animate()
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/MainActivity.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/MainActivity.kt
@@ -64,7 +64,7 @@ class MainActivity : AppCompatActivity(), OnNavigationItemSelectedListener {
payment?.talerPayUri?.let {
TalerNfcService.setUri(this, it)
} ?: run {
- TalerNfcService.clearUri(this)
+ TalerNfcService.clearNdefPayload(this)
}
}
diff --git a/taler-kotlin-android/src/main/java/net/taler/lib/android/TalerNfcService.kt b/taler-kotlin-android/src/main/java/net/taler/lib/android/TalerNfcService.kt
@@ -17,7 +17,6 @@
package net.taler.lib.android
import android.app.Activity
-import android.app.Service
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
@@ -30,7 +29,8 @@ import android.nfc.cardemulation.CardEmulation
import android.nfc.cardemulation.HostApduService
import android.os.Bundle
import android.util.Log
-import androidx.localbroadcastmanager.content.LocalBroadcastManager
+import androidx.core.content.ContextCompat
+import androidx.core.content.IntentCompat
import java.math.BigInteger
class TalerNfcService : HostApduService() {
@@ -53,12 +53,22 @@ class TalerNfcService : HostApduService() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
SET_URI_INTENT -> intent.getStringExtra("uri")?.let { uri ->
+ Log.d(TAG, "onReceive(SET_URI_INTENT) | URI: $uri")
ndefMessage = NdefMessage(createUriRecord(uri))
- Log.d(TAG, "onReceive() | URI: $uri")
}
- SET_NDEF_INTENT -> intent.getParcelableExtra<NdefMessage>("ndef")?.let { ndef ->
+
+ SET_NDEF_INTENT -> IntentCompat.getParcelableExtra(
+ intent,
+ "ndef",
+ NdefMessage::class.java,
+ )?.let { ndef ->
+ Log.d(TAG, "onReceive(SET_NDEF_INTENT) | NDEF: $ndef")
ndefMessage = ndef
- Log.d(TAG, "onReceive() | NDEF: $ndef")
+ }
+
+ CLEAR_NDEF_INTENT -> {
+ Log.d(TAG, "onReceive(CLEAR_NDEF_INTENT)")
+ ndefMessage = null
}
}
}
@@ -211,16 +221,23 @@ class TalerNfcService : HostApduService() {
override fun onCreate() {
super.onCreate()
- LocalBroadcastManager.getInstance(this).registerReceiver(
+ Log.d(TAG, "onCreate() service running")
+ val intentFilter = IntentFilter()
+ intentFilter.addAction(SET_URI_INTENT)
+ intentFilter.addAction(SET_NDEF_INTENT)
+ intentFilter.addAction(CLEAR_NDEF_INTENT)
+ ContextCompat.registerReceiver(
+ this@TalerNfcService,
broadcastReceiver,
- IntentFilter(SET_URI_INTENT),
+ intentFilter,
+ ContextCompat.RECEIVER_NOT_EXPORTED,
)
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "onDestroy() NFC service")
- LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver)
+ unregisterReceiver(broadcastReceiver)
ndefMessage = null
}
@@ -228,6 +245,7 @@ class TalerNfcService : HostApduService() {
private const val TAG = "taler-wallet-hce"
const val SET_URI_INTENT = "taler-wallet-set-url"
const val SET_NDEF_INTENT = "taler-wallet-set-ndef"
+ const val CLEAR_NDEF_INTENT = "taler-wallet-clear-ndef"
private val APDU_SELECT = byteArrayOf(
0x00.toByte(), // CLA - Class - Class of instruction
@@ -349,26 +367,25 @@ class TalerNfcService : HostApduService() {
fun setUri(activity: Activity, uri: String) {
if (!hasNfc(activity)) return
- val broadcastManager = LocalBroadcastManager.getInstance(activity)
val intent = Intent(SET_URI_INTENT)
+ intent.setPackage(activity.packageName)
intent.putExtra("uri", uri)
- broadcastManager.sendBroadcast(intent)
+ activity.sendBroadcast(intent)
}
fun setNdefPayload(activity: Activity, ndef: NdefMessage) {
if (!hasNfc(activity)) return
- val broadcastManager = LocalBroadcastManager.getInstance(activity)
val intent = Intent(SET_NDEF_INTENT)
+ intent.setPackage(activity.packageName)
intent.putExtra("ndef", ndef)
- broadcastManager.sendBroadcast(intent)
+ activity.sendBroadcast(intent)
}
- fun clearUri(activity: Activity) {
+ fun clearNdefPayload(activity: Activity) {
if (!hasNfc(activity)) return
- val broadcastManager = LocalBroadcastManager.getInstance(activity)
- val intent = Intent(SET_URI_INTENT)
- intent.putExtra("uri", null as String?)
- broadcastManager.sendBroadcast(intent)
+ val intent = Intent(CLEAR_NDEF_INTENT)
+ intent.setPackage(activity.packageName)
+ activity.sendBroadcast(intent)
}
}
}
\ No newline at end of file
diff --git a/wallet/src/main/java/net/taler/wallet/main/MainActivity.kt b/wallet/src/main/java/net/taler/wallet/main/MainActivity.kt
@@ -136,7 +136,7 @@ class MainActivity : AppCompatActivity(), OnPreferenceStartFragmentCallback {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
model.transactionManager.selectedTransaction.collect { tx ->
- TalerNfcService.clearUri(this@MainActivity)
+ TalerNfcService.clearNdefPayload(this@MainActivity)
when (tx) {
is TransactionPeerPushDebit -> tx.talerUri
@@ -372,8 +372,8 @@ class MainActivity : AppCompatActivity(), OnPreferenceStartFragmentCallback {
override fun onDestroy() {
super.onDestroy()
+ TalerNfcService.clearNdefPayload(this)
TalerNfcService.stopService(this)
- TalerNfcService.clearUri(this)
model.stopWallet()
}
}