@@ -6,6 +6,7 @@ import android.content.Context
66import android.content.Intent
77import android.content.IntentFilter
88import android.hardware.usb.*
9+ import android.os.Build
910import io.flutter.Log
1011import io.flutter.embedding.engine.plugins.FlutterPlugin
1112import io.flutter.embedding.engine.plugins.activity.ActivityAware
@@ -15,6 +16,7 @@ import io.flutter.plugin.common.MethodChannel
1516import io.flutter.plugin.common.MethodChannel.MethodCallHandler
1617import io.flutter.plugin.common.MethodChannel.Result
1718
19+
1820/* * CcidPlugin */
1921class CcidPlugin : FlutterPlugin , MethodCallHandler , ActivityAware {
2022 private lateinit var channel: MethodChannel
@@ -25,30 +27,30 @@ class CcidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
2527 private val usbReceiver = object : BroadcastReceiver () {
2628 override fun onReceive (context : Context , intent : Intent ) {
2729 if (intent.action == ACTION_USB_PERMISSION ) {
28- println ( " Intent! " )
29- val device: UsbDevice ? = intent.getParcelableExtra(UsbManager .EXTRA_DEVICE )
30- if (intent.getBooleanExtra(UsbManager .EXTRA_PERMISSION_GRANTED , false )) {
31- device?.apply {
32- val reader = readers[intent.identifier ]
33- if (reader == null ) {
34- Log .e(TAG , " Reader not found" )
35- return
36- }
37- println ( " reader: $ reader" )
38- val ccid = connectToInterface(device, reader.interfaceIdx)
39- if (ccid != null ) {
40- readers[reader.name] = reader.copy(ccid = ccid, result = null )
41- reader.result !! .success( null )
42- } else {
43- reader.result !! .error(
44- " CCID_READER_CONNECT_ERROR " ,
45- " Failed to connect " ,
46- null
47- )
30+ synchronized( this ) {
31+ val device: UsbDevice ? = intent.getParcelableExtra(UsbManager .EXTRA_DEVICE )
32+ if (intent.getBooleanExtra(UsbManager .EXTRA_PERMISSION_GRANTED , false )) {
33+ device?.apply {
34+ val reader = readers[intent.getStringExtra( " name " ) ]
35+ if (reader == null ) {
36+ Log .e(TAG , " Reader not found" )
37+ return
38+ }
39+ val ccid = connectToInterface(device, reader.interfaceIdx )
40+ if (ccid != null ) {
41+ readers[reader.name] = reader.copy (ccid = ccid, result = null )
42+ reader. result!! .success( null )
43+ } else {
44+ reader.result !! .error(
45+ " CCID_READER_CONNECT_ERROR " ,
46+ " Failed to connect " ,
47+ null
48+ )
49+ }
4850 }
51+ } else {
52+ Log .d(TAG , " permission denied for device $device " )
4953 }
50- } else {
51- Log .d(TAG , " permission denied for device $device " )
5254 }
5355 }
5456 }
@@ -111,6 +113,13 @@ class CcidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
111113 override fun onAttachedToActivity (binding : ActivityPluginBinding ) {
112114 context = binding.activity.applicationContext
113115 usbManager = context.getSystemService(Context .USB_SERVICE ) as UsbManager
116+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
117+ context.registerReceiver(
118+ usbReceiver, IntentFilter (ACTION_USB_PERMISSION ), Context .RECEIVER_EXPORTED
119+ )
120+ } else {
121+ context.registerReceiver(usbReceiver, IntentFilter (ACTION_USB_PERMISSION ))
122+ }
114123 }
115124
116125 override fun onDetachedFromActivityForConfigChanges () {}
@@ -167,24 +176,18 @@ class CcidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
167176 }
168177
169178 if (! usbManager.hasPermission(device)) {
170- context.registerReceiver(
171- usbReceiver,
172- IntentFilter (ACTION_USB_PERMISSION ),
173- Context .RECEIVER_EXPORTED
174- )
175-
176179 // Request permission
177180 readers[name] = reader.copy(result = result)
178181 val intent = Intent (ACTION_USB_PERMISSION )
179- intent.identifier = name
182+ intent.putExtra(" name" , name)
183+ intent.setPackage(context.packageName)
180184 val pendingIntent = PendingIntent .getBroadcast(
181185 context,
182186 0 ,
183187 intent,
184- PendingIntent .FLAG_IMMUTABLE
188+ if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . S ) PendingIntent .FLAG_MUTABLE else 0
185189 )
186190 usbManager.requestPermission(device, pendingIntent)
187-
188191 return
189192 } else {
190193 val ccid = connectToInterface(device, reader.interfaceIdx)
@@ -260,7 +263,6 @@ class CcidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
260263 companion object {
261264 private val TAG = FlutterPlugin ::class .java.name
262265 private const val ACTION_USB_PERMISSION = " im.nfc.ccid.USB_PERMISSION"
263- private const val TIMEOUT = 1000
264266 }
265267
266268 private data class Reader (
0 commit comments