support opening USB-OTG on android-29+

Here's another gem: they added this nice new API in android-24 and told
everyone this was THE WAY. Then they made it a no-op in android-29
https://developer.android.com/reference/android/os/storage/StorageVolume#createAccessIntent(java.lang.String)
This commit is contained in:
Hans-Christoph Steiner 2020-11-11 16:24:55 +01:00
parent 6887e09d88
commit d00108ba68
3 changed files with 26 additions and 11 deletions

View File

@ -74,6 +74,11 @@ public class TreeUriScannerIntentService extends IntentService {
public static final String TAG = "TreeUriScannerIntentSer"; public static final String TAG = "TreeUriScannerIntentSer";
private static final String ACTION_SCAN_TREE_URI = "org.fdroid.fdroid.nearby.action.SCAN_TREE_URI"; private static final String ACTION_SCAN_TREE_URI = "org.fdroid.fdroid.nearby.action.SCAN_TREE_URI";
/**
* @see <a href="https://android.googlesource.com/platform/frameworks/base/+/android-10.0.0_r38/core/java/android/provider/DocumentsContract.java#238">DocumentsContract.EXTERNAL_STORAGE_PROVIDER_AUTHORITY</a>
* @see <a href="https://android.googlesource.com/platform/frameworks/base/+/android-10.0.0_r38/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java#70">ExternalStorageProvider.AUTHORITY</a>
*/
public static final String EXTERNAL_STORAGE_PROVIDER_AUTHORITY = "com.android.externalstorage.documents";
public TreeUriScannerIntentService() { public TreeUriScannerIntentService() {
super("TreeUriScannerIntentService"); super("TreeUriScannerIntentService");

View File

@ -19,25 +19,19 @@
package org.fdroid.fdroid.nearby; package org.fdroid.fdroid.nearby;
import android.app.Activity;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.UriPermission; import android.content.UriPermission;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.hardware.usb.UsbManager; import android.hardware.usb.UsbManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import org.fdroid.fdroid.views.main.MainActivity; import androidx.annotation.RequiresApi;
import org.fdroid.fdroid.views.main.NearbyViewBinder; import org.fdroid.fdroid.views.main.NearbyViewBinder;

View File

@ -13,6 +13,7 @@ import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.os.storage.StorageManager; import android.os.storage.StorageManager;
import android.os.storage.StorageVolume; import android.os.storage.StorageVolume;
import android.provider.DocumentsContract;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@ -21,11 +22,9 @@ import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import org.fdroid.fdroid.R; import org.fdroid.fdroid.R;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.nearby.SDCardScannerService; import org.fdroid.fdroid.nearby.SDCardScannerService;
@ -61,6 +60,8 @@ import java.util.List;
* *
* @see TreeUriScannerIntentService * @see TreeUriScannerIntentService
* @see org.fdroid.fdroid.nearby.SDCardScannerService * @see org.fdroid.fdroid.nearby.SDCardScannerService
* <p>
* TODO use {@link StorageManager#registerStorageVolumeCallback(Executor, StorageManager.StorageVolumeCallback)}
*/ */
public class NearbyViewBinder { public class NearbyViewBinder {
public static final String TAG = "NearbyViewBinder"; public static final String TAG = "NearbyViewBinder";
@ -165,11 +166,26 @@ public class NearbyViewBinder {
for (final StorageVolume storageVolume : storageManager.getStorageVolumes()) { for (final StorageVolume storageVolume : storageManager.getStorageVolumes()) {
if (storageVolume.isRemovable() && !storageVolume.isPrimary()) { if (storageVolume.isRemovable() && !storageVolume.isPrimary()) {
Log.i(TAG, "StorageVolume: " + storageVolume); Log.i(TAG, "StorageVolume: " + storageVolume);
final Intent intent = storageVolume.createAccessIntent(null); Intent tmpIntent = null;
if (intent == null) { if (Build.VERSION.SDK_INT < 29) {
tmpIntent = storageVolume.createAccessIntent(null);
} else {
tmpIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
tmpIntent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,
Uri.parse("content://"
+ TreeUriScannerIntentService.EXTERNAL_STORAGE_PROVIDER_AUTHORITY
+ "/tree/"
+ storageVolume.getUuid()
+ "%3A/document/"
+ storageVolume.getUuid()
+ "%3A"));
}
if (tmpIntent == null) {
Utils.debugLog(TAG, "Got null Storage Volume access Intent"); Utils.debugLog(TAG, "Got null Storage Volume access Intent");
return; return;
} }
final Intent intent = tmpIntent;
storageVolumeText.setVisibility(View.VISIBLE); storageVolumeText.setVisibility(View.VISIBLE);
String text = storageVolume.getDescription(context); String text = storageVolume.getDescription(context);