From 6a3c935f4e27a8f135bbb2aebf659a481fda48aa Mon Sep 17 00:00:00 2001 From: Matthew Clough Date: Thu, 16 Jun 2022 17:50:53 -0600 Subject: [PATCH] Updates to account for Android 12 changes to Intent creation. Includes spelling correction --- .../FlutterUsbPrinterPlugin.kt | 2 +- .../adapter/USBPrinterAdapter.kt | 20 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/android/src/main/kotlin/app/mylekha/client/flutter_usb_printer/FlutterUsbPrinterPlugin.kt b/android/src/main/kotlin/app/mylekha/client/flutter_usb_printer/FlutterUsbPrinterPlugin.kt index fa27d33..095d965 100644 --- a/android/src/main/kotlin/app/mylekha/client/flutter_usb_printer/FlutterUsbPrinterPlugin.kt +++ b/android/src/main/kotlin/app/mylekha/client/flutter_usb_printer/FlutterUsbPrinterPlugin.kt @@ -29,7 +29,7 @@ class FlutterUsbPrinterPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_usb_printer") channel.setMethodCallHandler(this) - context = flutterPluginBinding.applicationContext + context = flutterPluginBinding.getApplicationContext() adapter = USBPrinterAdapter().getInstance() } diff --git a/android/src/main/kotlin/app/mylekha/client/flutter_usb_printer/adapter/USBPrinterAdapter.kt b/android/src/main/kotlin/app/mylekha/client/flutter_usb_printer/adapter/USBPrinterAdapter.kt index 0e79307..84a268e 100644 --- a/android/src/main/kotlin/app/mylekha/client/flutter_usb_printer/adapter/USBPrinterAdapter.kt +++ b/android/src/main/kotlin/app/mylekha/client/flutter_usb_printer/adapter/USBPrinterAdapter.kt @@ -6,6 +6,7 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.hardware.usb.* +import android.os.Build import android.util.Base64 import android.util.Log import android.widget.Toast @@ -21,7 +22,7 @@ class USBPrinterAdapter { private val LOG_TAG = "Flutter USB Printer" private var mContext: Context? = null private var mUSBManager: UsbManager? = null - private var mPermissionIndent: PendingIntent? = null + private var mPermissionIntent: PendingIntent? = null private var mUsbDevice: UsbDevice? = null private var mUsbDeviceConnection: UsbDeviceConnection? = null private var mUsbInterface: UsbInterface? = null @@ -41,8 +42,8 @@ class USBPrinterAdapter { private val mUsbDeviceReceiver: BroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { - val action = intent.action - if (ACTION_USB_PERMISSION == action) { + var action = intent.getAction(); + if (ACTION_USB_PERMISSION.equals(action)) { synchronized(this) { val usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE) @@ -73,8 +74,13 @@ class USBPrinterAdapter { fun init(reactContext: Context?) { mContext = reactContext mUSBManager = mContext!!.getSystemService(Context.USB_SERVICE) as UsbManager - mPermissionIndent = - PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + mPermissionIntent = + PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE) + } else { + mPermissionIntent = + PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_UPDATE_CURRENT) + } val filter = IntentFilter(ACTION_USB_PERMISSION) filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED) mContext!!.registerReceiver(mUsbDeviceReceiver, filter) @@ -115,7 +121,7 @@ class USBPrinterAdapter { "Request for device: vendor_id: " + usbDevice.vendorId + ", product_id: " + usbDevice.productId ) closeConnectionIfExists() - mUSBManager!!.requestPermission(usbDevice, mPermissionIndent) + mUSBManager!!.requestPermission(usbDevice, mPermissionIntent) return true } } @@ -126,7 +132,7 @@ class USBPrinterAdapter { private fun openConnection(): Boolean { if (mUsbDevice == null) { - Log.e(LOG_TAG, "USB Deivce is not initialized") + Log.e(LOG_TAG, "USB Device is not initialized") return false } if (mUSBManager == null) {