Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix android 14 crash #5

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading