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

Update UsbDocumentProvider.java to support Android 14 / sdk 34 #425

Open
wants to merge 1 commit into
base: develop
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 @@ -45,6 +45,7 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -104,6 +105,10 @@ private static String[] resolveDocumentProjection(String[] projection) {
return projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION;
}

private ArrayList<BroadcastReceiver> broadcastReceivers = new ArrayList<BroadcastReceiver>();

private final static int receiverFlags = Context.RECEIVER_NOT_EXPORTED;

@Override
public Cursor queryRoots(String[] projection) throws FileNotFoundException {
Log.d(TAG, "queryRoots()");
Expand Down Expand Up @@ -273,31 +278,53 @@ public String getDocumentType(String documentId) {
return "application/octet-stream";
}

private void registerBroadcaster(Context ctx, BroadcastReceiver receiverToRegister, IntentFilter receiverFilter) {

broadcastReceivers.add(receiverToRegister);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
ctx.registerReceiver(receiverToRegister, receiverFilter, receiverFlags);
}
else {
ctx.registerReceiver(receiverToRegister, receiverFilter);
}
}

private void unregisterBroadcasters() {
Context cts = getContext();
for (BroadcastReceiver receiver : broadcastReceivers) {
try {
cts.unregisterReceiver(receiver);
} catch (IllegalArgumentException ilex) {
Log.d(TAG, "failed to unregister broadcast receiver");
}
}
}

@Override
public boolean onCreate() {
Log.d(TAG, "onCreate()");

Context context = getContext();
assert context != null;

context.registerReceiver(new BroadcastReceiver() {
registerBroadcaster(context, new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
discoverDevice(device);
}
}
}, new IntentFilter(ACTION_USB_PERMISSION));

context.registerReceiver(new BroadcastReceiver() {
registerBroadcaster(context, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
discoverDevice(device);
}
}, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));

context.registerReceiver(new BroadcastReceiver() {
registerBroadcaster(context, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Expand All @@ -313,6 +340,10 @@ public void onReceive(Context context, Intent intent) {
return true;
}

public void onDestroy() {
unregisterBroadcasters();
}

private static String getMimeType(UsbFile file) {

if (file.isDirectory()) {
Expand Down