Example usage for android.os.storage StorageManager getStorageVolume

List of usage examples for android.os.storage StorageManager getStorageVolume

Introduction

In this page you can find the example usage for android.os.storage StorageManager getStorageVolume.

Prototype

public @Nullable StorageVolume getStorageVolume(File file) 

Source Link

Document

Return the StorageVolume that contains the given file, or null if none.

Usage

From source file:net.sf.fakenames.fddemo.BaseDirLayout.java

private void parseMounts(HashMap<File, String> pathNameMap, List<StorageVolume> volumes) throws IOException {
    mounts: for (LongObjectCursor<MountInfo.Mount> mount : mountInfo.mountMap) {
        final Iterator<Map.Entry<File, String>> i = pathNameMap.entrySet().iterator();
        while (i.hasNext()) {
            final Map.Entry<File, String> e = i.next();
            final String p = e.getKey().toString();
            if (p.equals(mount.value.rootPath) || p.startsWith(mount.value.rootPath + '/')) {
                i.remove();//from w ww .  j a  v  a 2s .co m
                pathNameMap.remove(e.getKey());
                mount.value.rootPath = p;
                mount.value.description = e.getValue();
                usableFilesystems.add(mount.value.fstype);
                break;
            }
        }

        if (Build.VERSION.SDK_INT >= 24) {
            final File mountRoot = new File(mount.value.rootPath);

            final StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE);
            Iterator<StorageVolume> j = volumes.iterator();
            while (j.hasNext()) {
                StorageVolume knownVolume = j.next();

                if (knownVolume.equals(sm.getStorageVolume(mountRoot))) {
                    // seems like the mount belongs to the storage device, appropriately labeled
                    // by the manufacturer  use system-provided description
                    j.remove();
                    mount.value.description = knownVolume.getDescription(this);
                    usableFilesystems.add(mount.value.fstype);
                    continue mounts;
                }
            }
        }
    }

    final Set<File> current = new HashSet<>();
    Collections.addAll(current, home.listFiles());

    // display all mounts with sensible descriptions
    for (LongObjectCursor<MountInfo.Mount> mount : mountInfo.mountMap) {
        if (TextUtils.isEmpty(mount.value.description)) {
            continue;
        }

        File descFile = new File(home, mount.value.description);

        if (!descFile.exists()) {
            if (current.contains(descFile)) {
                // a broken link...

                //noinspection ResultOfMethodCallIgnored
                descFile.delete();
            }

            @Fd
            int dir = os.opendir(home.getPath());

            os.symlinkat(mount.value.rootPath, dir, descFile.getName());
        }
    }
}