package com.stay.gamecenter.utilities;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import android.os.Environment;
public class Dev_Mounts {
private static final String HEAD = "dev_mount";
private static Dev_Mounts instance;
private String mDevMountsLocation = null;
private ArrayList<String> mDevMounts = new ArrayList<String>();
private String[] mMountedPaths = null;
private HashMap<String, File> map;
private ArrayList<String> mVold;
private Dev_Mounts() {
mDevMountsLocation = Environment.getRootDirectory().getAbsolutePath()
+ File.separator + "etc" + File.separator + "vold.fstab";
initialize();
}
public static Dev_Mounts getInstance() {
if (instance == null) {
instance = new Dev_Mounts();
}
return instance;
}
// private void initialize() {
// try {
// mDevMounts.clear();
// BufferedReader br = new BufferedReader(new FileReader(
// mDevMountsLocation));
// String tmp = null;
// while ((tmp = br.readLine()) != null) {
// // the words startsWith "dev_mount" are the SD info
// if (tmp.startsWith(HEAD)) {
// mDevMounts.add(tmp);
// }
// }
// br.close();
// if (mDevMounts.size() > 0) {
// parseDevMounts();
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
private void parseDevMounts() {
mMountedPaths = new String[mDevMounts.size()];
String[] format = null;
String mount = null;
String path = null;
for (int i = 0; i < mDevMounts.size(); i++) {
mount = mDevMounts.get(i);
format = mount.split(" ");
path = format[2];
if (path.indexOf(":") >= 0) {
path.substring(0, path.indexOf(":"));
}
mMountedPaths[i] = path;
}
}
public String[] getMountedPaths() {
if (mMountedPaths == null || mMountedPaths.length == 0) {
return new String[] { Environment.getExternalStorageDirectory()
.getAbsolutePath() };
}
return mMountedPaths;
}
public ArrayList<String> getScanDirs() {
ArrayList<String> dirs = new ArrayList<String>();
File root = Environment.getRootDirectory().getParentFile();
File[] list = root.listFiles();
File dir = null;
for (int i = 0; i < list.length; i++) {
dir = list[i];
if (dir.isDirectory()) {
if ("mnt".equalsIgnoreCase(dir.getName())
|| "storage".equalsIgnoreCase(dir.getName())
|| "sdcard".equalsIgnoreCase(dir.getName())
|| dir.getName().startsWith("sdcard")) {
dirs.add(dir.getAbsolutePath());
}
}
}
return dirs;
}
public static final String SD_CARD = "sdCard";
public static final String EXTERNAL_SD_CARD = "externalSdCard";
/**
* @return True if the external storage is available. False otherwise.
*/
public static boolean isAvailable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
public static String getSdCardPath() {
return Environment.getExternalStorageDirectory().getPath() + "/";
}
/**
* @return True if the external storage is writable. False otherwise.
*/
public static boolean isWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/**
* @return A map of all storage locations available
*/
public List<String> getAllStorageLocations() {
return mVold;
}
private void initialize() {
map = new HashMap<String, File>(10);
List<String> mMounts = new ArrayList<String>(10);
mVold = new ArrayList<String>(10);
try {
File mountFile = new File("/proc/mounts");
if (mountFile.exists()) {
Scanner scanner = new Scanner(mountFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.startsWith("/dev/block/vold/")
|| line.startsWith("/dev/fuse")) {
String[] lineElements = line.split(" ");
String element = lineElements[1];
// don't add the default mount path
// it's already in the list.
mMounts.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
File voldFile = new File("/system/etc/vold.fstab");
if (voldFile.exists()) {
Scanner scanner = new Scanner(voldFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.startsWith("dev_mount")) {
String[] lineElements = line.split(" ");
String element = lineElements[2];
if (element.contains(":")) {
element = element
.substring(0, element.indexOf(":"));
}
if (!mMounts.contains(element)) {
mMounts.add(element);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
// for (int i = 0; i < mMounts.size(); i++) {
// String mount = mMounts.get(i);
// if (!mVold.contains(mount))
// mMounts.remove(i--);
// }
// mVold.clear();
List<String> mountHash = new ArrayList<String>(10);
for (String mount : mMounts) {
File root = new File(mount);
if (root.exists() && root.isDirectory() && root.canWrite()) {
File[] list = root.listFiles();
String hash = "[";
if (list != null) {
for (File f : list) {
hash += f.getName().hashCode() + ":" + f.length()
+ ", ";
}
}
hash += "]";
if (!mountHash.contains(hash)) {
// String key = SD_CARD + "_" + map.size();
// if (map.size() == 0) {
// key = SD_CARD;
// } else if (map.size() == 1) {
// key = EXTERNAL_SD_CARD;
// }
mountHash.add(hash);
// map.put(key, root);
mVold.add(mount);
}
}
}
mMounts.clear();
// if (map.isEmpty()) {
// map.put(SD_CARD, Environment.getExternalStorageDirectory());
// }
if (mVold.isEmpty()) {
mVold.add(Environment.getExternalStorageDirectory().getAbsolutePath());
}
}
}
SD卡列表
最新推荐文章于 2021-11-27 16:39:21 发布