ESCPOS-ThermalPrinter-Android icon indicating copy to clipboard operation
ESCPOS-ThermalPrinter-Android copied to clipboard

Targeting U+ (version 34 and above) disallows creating or retrieving a PendingIntent with FLAG_MUTABLE, an implicit Intent within and without FLAG_NO_CREATE and FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT for security reasons. To retrieve an already existing PendingIntent, use FLAG_NO_CREATE, however, to create a new PendingIntent with an implicit Intent use FLAG_IMMUTABLE

Open Shiv-k-Tripathi opened this issue 1 year ago • 1 comments

Hi @DantSu ,

I’m using your library in my project for usb print, and it's working fine on Android devices running versions below 14. However, when I try to use it on Android 14 and above, I encounter the following issue.

Exception in native call java.lang.IllegalArgumentException: com.recahoposlite: Targeting U+ (version 34 and above) disallows creating or retrieving a PendingIntent with FLAG_MUTABLE, an implicit Intent within and without FLAG_NO_CREATE and FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT for security reasons. To retrieve an already existing PendingIntent, use FLAG_NO_CREATE, however, to create a new PendingIntent with an implicit Intent use FLAG_IMMUTABLE. at android.os.Parcel.createExceptionOrNull(Parcel.java:3073) at android.os.Parcel.createException(Parcel.java:3053) at android.os.Parcel.readException(Parcel.java:3036) at android.os.Parcel.readException(Parcel.java:2978) at android.app.IActivityManager$Stub$Proxy.getIntentSenderWithFeature(IActivityManager.java:7041) at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:752) at android.app.PendingIntent.getBroadcast(PendingIntent.java:735) at com.recahoposlite.MainActivity.printUsb(MainActivity.java:228) at com.recahoposlite.MyPrint.printUSB(MyPrint.java:637) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188) at com.facebook.jni.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loopOnce(Looper.java:230) at android.os.Looper.loop(Looper.java:319) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228) at java.lang.Thread.run(Thread.java:1012) Caused by: android.os.RemoteException: Remote stack trace: at com.android.server.am.ActivityManagerService.getIntentSenderWithFeatureAsApp(ActivityManagerService.java:6488) at com.android.server.am.ActivityManagerService.getIntentSenderWithFeature(ActivityManagerService.java:6431) at android.app.IActivityManager$Stub.onTransact$getIntentSenderWithFeature$(IActivityManager.java:11943) at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3400) at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3199)

Could you please provide guidance or updates on compatibility with Android 14 and above?

Shiv-k-Tripathi avatar Oct 10 '24 06:10 Shiv-k-Tripathi

@Shiv-k-Tripathi Try these changes in your code.

private val usbReceiver: BroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {

        val action = intent.action
        Log.d("onReceiveUSB:", "action: $action")
        if (ACTION_USB_PERMISSION == action) {
            Log.d("onReceiveUSB:", "")
            context.unregisterReceiver(this)
            synchronized(this) {
                val usbManager =
                    context.getSystemService(AppCompatActivity.USB_SERVICE) as UsbManager

                usbDevice = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    intent.getParcelableExtra(
                        UsbManager.EXTRA_DEVICE,
                        UsbDevice::class.java
                    ) as UsbDevice
                } else {
                    (intent.getParcelableExtra<Parcelable>(UsbManager.EXTRA_DEVICE) as UsbDevice?)!!
                }
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if (usbManager != null && usbDevice != null) {
                        Toast.makeText(context, "OnReceive Print", Toast.LENGTH_LONG).show()
                        printContent()
                    }
                }
            }
        }
    }
}



private fun printUsb() {
    val usbConnection = UsbPrintersConnections.selectFirstConnected(context)
    usbManager = context?.getSystemService(Context.USB_SERVICE) as UsbManager
    if (usbConnection != null && usbManager != null) {

        val explicitIntent = Intent(ACTION_USB_PERMISSION)
        explicitIntent.setPackage(requireContext().packageName) // Make Intent explicit
        val permissionIntent = PendingIntent.getBroadcast(
            requireContext(),
            0,
            explicitIntent,
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0
        )
        val filter = IntentFilter(ACTION_USB_PERMISSION)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {  // TIRAMISU is Android 13 / API 33
            requireContext().registerReceiver(
                this.usbReceiver,
                filter,
                Context.RECEIVER_NOT_EXPORTED
            )
        } else {
            requireContext().registerReceiver(this.usbReceiver, filter)
        }
        usbManager.requestPermission(usbConnection.device, permissionIntent)


    } else {
        Toast.makeText(context, "No USB Connection is found!!", Toast.LENGTH_LONG).show()
    }
}

harsh8088 avatar Oct 23 '24 05:10 harsh8088