Skip to content

Commit 76a8b01

Browse files
committed
fix Android 14 related issues
1 parent b473f57 commit 76a8b01

File tree

5 files changed

+38
-36
lines changed

5 files changed

+38
-36
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ android {
3535
}
3636

3737
defaultConfig {
38-
minSdkVersion 29
38+
minSdkVersion 26
3939
}
4040

4141
dependencies {

android/src/main/kotlin/im/nfc/ccid/CcidPlugin.kt

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.content.Context
66
import android.content.Intent
77
import android.content.IntentFilter
88
import android.hardware.usb.*
9+
import android.os.Build
910
import io.flutter.Log
1011
import io.flutter.embedding.engine.plugins.FlutterPlugin
1112
import io.flutter.embedding.engine.plugins.activity.ActivityAware
@@ -15,6 +16,7 @@ import io.flutter.plugin.common.MethodChannel
1516
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
1617
import io.flutter.plugin.common.MethodChannel.Result
1718

19+
1820
/** CcidPlugin */
1921
class 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(

example/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ android {
4242

4343
defaultConfig {
4444
applicationId "im.nfc.ccid.example"
45-
minSdkVersion 29
45+
minSdkVersion 26
4646
targetSdkVersion 34
4747
compileSdkVersion 34
4848
versionCode flutterVersionCode.toInteger()

example/android/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
org.gradle.jvmargs=-Xmx1536M
22
android.useAndroidX=true
33
android.enableJetifier=true
4-
AGPVersion=7.4.2
4+
AGPVersion=8.7.3
55
KotlinVersion=1.9.23

example/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-all.zip

0 commit comments

Comments
 (0)