Search in sources :

Example 1 with VolumeInfo

use of dev.dworks.apps.anexplorer.misc.VolumeInfo in project AnExplorer by 1hakr.

the class ExternalStorageProvider method updateVolumesLocked2.

private void updateVolumesLocked2() {
    mRoots.clear();
    VolumeInfo primaryVolume = null;
    // UserHandle.myUserId();
    final int userId = 0;
    StorageUtils storageUtils = new StorageUtils(getContext());
    for (VolumeInfo volume : storageUtils.getVolumes()) {
        if (!volume.isMountedReadable())
            continue;
        final String rootId;
        final String title;
        if (volume.getType() == VolumeInfo.TYPE_EMULATED) {
            // We currently only support a single emulated volume mounted at
            // a time, and it's always considered the primary
            rootId = ROOT_ID_PRIMARY_EMULATED;
            if (VolumeInfo.ID_EMULATED_INTERNAL.equals(volume.getId())) {
                title = getContext().getString(R.string.root_internal_storage);
            } else {
                // This should cover all other storage devices, like an SD card
                // or USB OTG drive plugged in. Using getBestVolumeDescription()
                // will give us a nice string like "Samsung SD card" or "SanDisk USB drive"
                final VolumeInfo privateVol = storageUtils.findPrivateForEmulated(volume);
                title = StorageUtils.getBestVolumeDescription(getContext(), privateVol);
            }
        } else if (volume.getType() == VolumeInfo.TYPE_PUBLIC) {
            rootId = ROOT_ID_SECONDARY + volume.getFsUuid();
            title = StorageUtils.getBestVolumeDescription(getContext(), volume);
        } else {
            // Unsupported volume; ignore
            continue;
        }
        if (TextUtils.isEmpty(rootId)) {
            Log.d(TAG, "Missing UUID for " + volume.getId() + "; skipping");
            continue;
        }
        if (mRoots.containsKey(rootId)) {
            Log.w(TAG, "Duplicate UUID " + rootId + " for " + volume.getId() + "; skipping");
            continue;
        }
        final RootInfo root = new RootInfo();
        mRoots.put(rootId, root);
        root.rootId = rootId;
        root.flags = Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
        final DiskInfo disk = volume.getDisk();
        Log.d(TAG, "Disk for root " + rootId + " is " + disk);
        if (disk != null && disk.isSd()) {
            root.flags |= Root.FLAG_REMOVABLE_SD;
        } else if (disk != null && disk.isUsb()) {
            root.flags |= Root.FLAG_REMOVABLE_USB;
        }
        if (volume.isPrimary()) {
            // save off the primary volume for subsequent "Home" dir initialization.
            primaryVolume = volume;
            root.flags |= Root.FLAG_ADVANCED;
        }
        // Dunno when this would NOT be the case, but never hurts to be correct.
        if (volume.isMountedWritable()) {
            root.flags |= Root.FLAG_SUPPORTS_CREATE;
        }
        root.title = title;
        if (volume.getType() == VolumeInfo.TYPE_PUBLIC) {
            root.flags |= Root.FLAG_HAS_SETTINGS;
        }
        if (volume.isVisibleForRead(userId)) {
            root.visiblePath = volume.getPathForUser(userId);
        } else {
            root.visiblePath = null;
        }
        root.path = volume.getInternalPathForUser(userId);
        try {
            root.docId = getDocIdForFile(root.path);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
    // by calling either getPathForUser, or getInternalPathForUser.
    if (primaryVolume != null && primaryVolume.isVisible()) {
        final RootInfo root = new RootInfo();
        mRoots.put(root.rootId, root);
        root.rootId = ROOT_ID_HOME;
        root.title = getContext().getString(R.string.root_documents);
        // Only report bytes on *volumes*...as a matter of policy.
        root.reportAvailableBytes = false;
        root.flags = Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
        // Dunno when this would NOT be the case, but never hurts to be correct.
        if (primaryVolume.isMountedWritable()) {
            root.flags |= Root.FLAG_SUPPORTS_CREATE;
        }
        // Create the "Documents" directory on disk (don't use the localized title).
        root.visiblePath = new File(primaryVolume.getPathForUser(userId), Environment.DIRECTORY_DOCUMENTS);
        root.path = new File(primaryVolume.getInternalPathForUser(userId), Environment.DIRECTORY_DOCUMENTS);
        root.path = new File(primaryVolume.getInternalPathForUser(userId), Environment.DIRECTORY_DOCUMENTS);
        try {
            root.docId = getDocIdForFile(root.path);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
    Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots");
    // Note this affects content://com.android.externalstorage.documents/root/39BD-07C5
    // as well as content://com.android.externalstorage.documents/document/*/children,
    // so just notify on content://com.android.externalstorage.documents/.
    notifyRootsChanged(getContext());
}
Also used : FileNotFoundException(java.io.FileNotFoundException) DiskInfo(dev.dworks.apps.anexplorer.misc.DiskInfo) VolumeInfo(dev.dworks.apps.anexplorer.misc.VolumeInfo) StorageUtils(dev.dworks.apps.anexplorer.misc.StorageUtils) DocumentInfo.getCursorString(dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString) FileUtils.getTypeForFile(dev.dworks.apps.anexplorer.misc.FileUtils.getTypeForFile) DocumentFile(android.support.provider.DocumentFile) File(java.io.File) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Point (android.graphics.Point)1 DocumentFile (android.support.provider.DocumentFile)1 DiskInfo (dev.dworks.apps.anexplorer.misc.DiskInfo)1 FileUtils.getTypeForFile (dev.dworks.apps.anexplorer.misc.FileUtils.getTypeForFile)1 StorageUtils (dev.dworks.apps.anexplorer.misc.StorageUtils)1 VolumeInfo (dev.dworks.apps.anexplorer.misc.VolumeInfo)1 DocumentInfo.getCursorString (dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1