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

(Android) Add RECEIVER_EXPORTED flags upon registering receiver for Android 12+ requirements #13438

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
Expand Down Expand Up @@ -64,9 +65,26 @@ public Enumerator(Context context, DeviceListener listener){
mListener = listener;
mContext = context;

context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbUtilities.ACTION_USB_PERMISSION));
context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
// Extra flag to counteract the extra requirements to pass an exported or not-exported value
// when broadcasting for Android 12 and up.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
context.registerReceiver(mBroadcastReceiver,
new IntentFilter(UsbUtilities.ACTION_USB_PERMISSION),
Context.RECEIVER_EXPORTED);

context.registerReceiver(mBroadcastReceiver,
new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED),
Context.RECEIVER_EXPORTED);

context.registerReceiver(mBroadcastReceiver,
new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED),
Context.RECEIVER_EXPORTED);
} else {
// For pre-Android 12 versions, use the standard registerReceiver without the export flag
context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbUtilities.ACTION_USB_PERMISSION));
context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
}

onDeviceAttach(context);
}
Expand Down