Java Code Examples for android.os.storage.StorageVolume#isRemovable()

The following examples show how to use android.os.storage.StorageVolume#isRemovable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NearbyViewBinder.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void updateUsbOtg(final Context context) {
    if (Build.VERSION.SDK_INT < 24) {
        return;
    }
    if (swapView == null) {
        Utils.debugLog(TAG, "swapView == null");
        return;
    }
    TextView storageVolumeText = swapView.findViewById(R.id.storage_volume_text);
    Button requestStorageVolume = swapView.findViewById(R.id.request_storage_volume_button);
    storageVolumeText.setVisibility(View.GONE);
    requestStorageVolume.setVisibility(View.GONE);

    final StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    for (final StorageVolume storageVolume : storageManager.getStorageVolumes()) {
        if (storageVolume.isRemovable() && !storageVolume.isPrimary()) {
            Log.i(TAG, "StorageVolume: " + storageVolume);
            final Intent intent = storageVolume.createAccessIntent(null);
            if (intent == null) {
                Utils.debugLog(TAG, "Got null Storage Volume access Intent");
                return;
            }
            storageVolumeText.setVisibility(View.VISIBLE);

            String text = storageVolume.getDescription(context);
            if (!TextUtils.isEmpty(text)) {
                requestStorageVolume.setText(text);
                UsbDevice usb = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (usb != null) {
                    text = String.format("%s (%s %s)", text, usb.getManufacturerName(), usb.getProductName());
                    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
                }
            }

            requestStorageVolume.setVisibility(View.VISIBLE);
            requestStorageVolume.setOnClickListener(new View.OnClickListener() {
                @Override
                @RequiresApi(api = 24)
                public void onClick(View v) {
                    List<UriPermission> list = context.getContentResolver().getPersistedUriPermissions();
                    if (list != null) for (UriPermission uriPermission : list) {
                        Uri uri = uriPermission.getUri();
                        if (uri.getPath().equals(String.format("/tree/%s:", storageVolume.getUuid()))) {
                            intent.setData(uri);
                            TreeUriScannerIntentService.onActivityResult(context, intent);
                            return;
                        }
                    }
                    ((Activity) context).startActivityForResult(intent,
                        MainActivity.REQUEST_STORAGE_ACCESS);
                }
            });
        }
    }
}
 
Example 2
Source File: Environment.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Returns whether the shared/external storage media at the given path is
 * physically removable.
 *
 * @return true if the storage device can be removed (such as an SD card),
 *         or false if the storage device is built in and cannot be
 *         physically removed.
 * @throws IllegalArgumentException if the path is not a valid storage
 *             device.
 */
public static boolean isExternalStorageRemovable(File path) {
    final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
    if (volume != null) {
        return volume.isRemovable();
    } else {
        throw new IllegalArgumentException("Failed to find storage device at " + path);
    }
}