Created
July 8, 2014 21:46
-
-
Save bmc08gt/958f9230f6504ed9434b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StorageInfo { | |
public final String path; | |
public final boolean readonly; | |
public final boolean removable; | |
public final int number; | |
StorageInfo(String path, boolean readonly, boolean removable, int number) { | |
this.path = path; | |
this.readonly = readonly; | |
this.removable = removable; | |
this.number = number; | |
} | |
public static List<StorageInfo> getMountedVolumes() { | |
List<StorageInfo> list = new ArrayList<StorageInfo>(); | |
String primary = Environment.getExternalStorageDirectory().getPath(); | |
boolean isPrimaryRemovable = Environment.isExternalStorageRemovable(); | |
String primaryState = Environment.getExternalStorageState(); | |
boolean isPrimaryAvailable = primaryState.equals(Environment.MEDIA_MOUNTED) | |
|| primaryState.equals(Environment.MEDIA_MOUNTED_READ_ONLY); | |
boolean isPrimaryReadOnly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY); | |
HashSet<String> paths = new HashSet<String>(); | |
int num_removable = 1; | |
if (isPrimaryAvailable) { | |
paths.add(primary); | |
list.add(0, new StorageInfo( | |
primary, isPrimaryReadOnly, isPrimaryRemovable, isPrimaryRemovable ? num_removable++ : -1)); | |
} | |
BufferedReader bufferedReader = null; | |
try { | |
bufferedReader = new BufferedReader(new FileReader("/proc/mounts")); | |
String mount; | |
while ((mount = bufferedReader.readLine()) != null) { | |
if (mount.contains("vfat") || mount.contains("/mnt")) { | |
StringTokenizer tokens = new StringTokenizer(mount, " "); | |
String unused = tokens.nextToken(); | |
String mountPoint = tokens.nextToken(); | |
if (paths.contains(mountPoint)) { | |
continue; | |
} | |
unused = tokens.nextToken(); | |
List<String> flags = Arrays.asList(tokens.nextToken().split(",")); | |
boolean readOnly = flags.contains("ro"); | |
if (mount.contains("/dev/block/vold")) { | |
if (!mount.contains("/mnt/secure") | |
&& !mount.contains("/mnt/asec") | |
&& !mount.contains("/mnt/obb") | |
&& !mount.contains("/dev/mapper") | |
&& !mount.contains("tmpfs")) { | |
paths.add(mountPoint); | |
list.add(new StorageInfo(mountPoint, readOnly, true, num_removable++)); | |
} | |
} | |
} | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException io) { | |
io.printStackTrace(); | |
} finally { | |
if (bufferedReader != null) { | |
try { | |
bufferedReader.close(); | |
} catch (IOException ignored) {} | |
} | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment