Skip to content

Commit

Permalink
Fix for Opaque USB support on Android 14.
Browse files Browse the repository at this point in the history
  • Loading branch information
iiordanov committed Dec 29, 2023
1 parent c3044da commit 3681e2d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.graphics.Bitmap;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
Expand Down Expand Up @@ -146,12 +147,7 @@ public SpiceCommunicator(Context context, Handler handler, Viewable canvas, bool
}

usbDeviceManager = new UsbDeviceManager(context, usbEnabled);
if (usbEnabled) {
context.registerReceiver(usbPermissionRequestedReceiver, new IntentFilter(ACTION_USB_PERMISSION));
IntentFilter filter = new IntentFilter();
filter.addAction("android.hardware.usb.action.USB_STATE");
context.registerReceiver(usbStateChangedReceiver, filter);
}
registerReceiversForUsbDevices(context);
modifierMap.put(RemoteKeyboard.CTRL_MASK, LCONTROL);
modifierMap.put(RemoteKeyboard.RCTRL_MASK, RCONTROL);
modifierMap.put(RemoteKeyboard.ALT_MASK, LALT);
Expand All @@ -163,6 +159,23 @@ public SpiceCommunicator(Context context, Handler handler, Viewable canvas, bool

}

private void registerReceiversForUsbDevices(Context context) {
if (usbEnabled) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.registerReceiver(usbPermissionRequestedReceiver, new IntentFilter(ACTION_USB_PERMISSION), Context.RECEIVER_NOT_EXPORTED);
} else {
context.registerReceiver(usbPermissionRequestedReceiver, new IntentFilter(ACTION_USB_PERMISSION));
}
IntentFilter filter = new IntentFilter();
filter.addAction("android.hardware.usb.action.USB_STATE");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.registerReceiver(usbStateChangedReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
} else {
context.registerReceiver(usbStateChangedReceiver, filter);
}
}
}

public static void sendMessage(int message) {
android.util.Log.d(TAG, "sendMessage called with message: " + message);
myself.handler.sendEmptyMessage(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class UsbDeviceManager(val context: Context, val usbEnabled: Boolean) {
val d = this.getUnrequested()
if (d != null) {
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.FLAG_MUTABLE
PendingIntent.FLAG_IMMUTABLE
} else {
0
};
Expand Down

0 comments on commit 3681e2d

Please sign in to comment.