Skip to content

Commit

Permalink
Merge pull request #95 from dpkristensen/master
Browse files Browse the repository at this point in the history
Supress deprecation warning about getParcelableExtra()
  • Loading branch information
altera2015 authored Jan 21, 2024
2 parents 8514cb0 + 6fc7c1e commit 29b1a80
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions android/src/main/java/dev/bessems/usbserial/UsbSerialPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public class UsbSerialPlugin implements FlutterPlugin, MethodCallHandler, EventC

private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {

private UsbDevice getUsbDeviceFromIntent(Intent intent) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
return intent.getParcelableExtra(UsbManager.EXTRA_DEVICE, UsbDevice.class);
} else {
// Create local variable to keep scope of deprecation suppression smallest
@SuppressWarnings("deprecation")
UsbDevice ret = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
return ret;
}
}

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Expand All @@ -56,12 +67,7 @@ public void onReceive(Context context, Intent intent) {
if (action.equals(ACTION_USB_ATTACHED)) {
Log.d(TAG, "ACTION_USB_ATTACHED");
if (m_EventSink != null) {
UsbDevice device;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE, UsbDevice.class);
} else {
device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
}
UsbDevice device = getUsbDeviceFromIntent(intent);
if (device != null) {
HashMap<String, Object> msg = serializeDevice(device);
msg.put("event", ACTION_USB_ATTACHED);
Expand All @@ -73,12 +79,7 @@ public void onReceive(Context context, Intent intent) {
} else if (action.equals(ACTION_USB_DETACHED)) {
Log.d(TAG, "ACTION_USB_DETACHED");
if (m_EventSink != null) {
UsbDevice device;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE, UsbDevice.class);
} else {
device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
}
UsbDevice device = getUsbDeviceFromIntent(intent);
if (device != null) {
HashMap<String, Object> msg = serializeDevice(device);
msg.put("event", ACTION_USB_DETACHED);
Expand Down

0 comments on commit 29b1a80

Please sign in to comment.