Skip to content

Commit

Permalink
Make intent explicit to fix crash when using compileSdkVersion 34
Browse files Browse the repository at this point in the history
  • Loading branch information
rikner authored Feb 15, 2024
2 parents 63c421b + b521f4f commit e31fac4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion MIDIDriver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
}

android {
compileSdkVersion 31
compileSdk 34

defaultConfig {
minSdkVersion 12
Expand Down Expand Up @@ -53,6 +53,7 @@ android {
withSourcesJar()
}
}

}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jp.kshoji.driver.midi.device;

import static android.content.Context.RECEIVER_EXPORTED;
import static android.content.Context.RECEIVER_NOT_EXPORTED;
import static jp.kshoji.driver.midi.util.Constants.TAG;

import android.app.PendingIntent;
Expand All @@ -12,6 +14,7 @@
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
Expand Down Expand Up @@ -242,6 +245,7 @@ private final class MidiDeviceConnectionWatchThread extends Thread {
private Set<UsbDevice> connectedDevices;
boolean stopFlag;
private List<DeviceFilter> deviceFilters;
private String packageName;

/**
* Constructor
Expand All @@ -254,6 +258,7 @@ private final class MidiDeviceConnectionWatchThread extends Thread {
this.usbManager = usbManager;
this.deviceAttachedListener = deviceAttachedListener;
this.deviceDetachedHandler = deviceDetachedHandler;
this.packageName = context.getPackageName();
connectedDevices = new HashSet<>();
stopFlag = false;
deviceFilters = DeviceFilter.getDeviceFilters(context);
Expand All @@ -270,13 +275,24 @@ public void run() {
if (!deviceGrantQueue.isEmpty() && !isGranting) {
isGranting = true;
grantingDevice = deviceGrantQueue.remove();


Intent intent = new Intent(UsbMidiGrantedReceiver.USB_PERMISSION_GRANTED_ACTION);
intent.setPackage(packageName); // make intent explicit

PendingIntent permissionIntent = PendingIntent.getBroadcast(
context, 0, new Intent(UsbMidiGrantedReceiver.USB_PERMISSION_GRANTED_ACTION),
android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0
context, 0, intent,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0
);

context.registerReceiver(new UsbMidiGrantedReceiver(grantingDevice, deviceAttachedListener), new IntentFilter(UsbMidiGrantedReceiver.USB_PERMISSION_GRANTED_ACTION));

UsbMidiGrantedReceiver receiver = new UsbMidiGrantedReceiver(grantingDevice, deviceAttachedListener);
IntentFilter filter = new IntentFilter(UsbMidiGrantedReceiver.USB_PERMISSION_GRANTED_ACTION);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.registerReceiver(receiver, filter, RECEIVER_NOT_EXPORTED);
} else {
context.registerReceiver(receiver, filter);
}

usbManager.requestPermission(grantingDevice, permissionIntent);
}
}
Expand Down
2 changes: 1 addition & 1 deletion MIDIDriverSample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies {
}

android {
compileSdkVersion 31
compileSdk 34

defaultConfig {
minSdkVersion 12
Expand Down

0 comments on commit e31fac4

Please sign in to comment.