import com.cyanogenmod.filemanager.model.FileSystemObject;
import com.cyanogenmod.filemanager.model.MountPoint;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
private static final String TAG = "MountPointHelper"; //$NON-NLS-1$
- private static final List<String> ALLOWED_FS_TYPE = Arrays.asList(new String[]{
- "rootfs", //$NON-NLS-1$
- "tmpfs", //$NON-NLS-1$
- "vfat", //$NON-NLS-1$
- "ext2", //$NON-NLS-1$
- "ext3", //$NON-NLS-1$
- "ext4" //$NON-NLS-1$
+ private static final List<String> RESTRICTED_FS_TYPE = Arrays.asList(new String[]{
+ "devpts", //$NON-NLS-1$
+ "proc", //$NON-NLS-1$
+ "sysfs", //$NON-NLS-1$
+ "debugfs", //$NON-NLS-1$
+ "cgroup", //$NON-NLS-1$
+ "tmpfs" //$NON-NLS-1$
});
+ private static final long MAX_CACHED_TIME = 60000L * 5;
+
+ private static List<MountPoint> sMountPoints = new ArrayList<>();
+ private static long sLastCachedTime;
+
/**
* Constructor of <code>MountPointHelper</code>.
*/
}
/**
+ * Method that refresh the mount point information.
+ *
+ * @return boolean refresh success or not.
+ */
+ public static boolean refreshMountPoints(Console console) {
+ synchronized(sMountPoints) {
+ try {
+ sMountPoints.clear();
+ sMountPoints.addAll(CommandHelper.getMountPoints(null, null));
+ sLastCachedTime = System.currentTimeMillis();
+ return true;
+ } catch (Exception e) {
+ Log.e(TAG, "Failed to update the mount point information", e); //$NON-NLS-1$
+ }
+ }
+ return false;
+ }
+
+ /**
* Method that retrieve the mount point information for a directory.
*
* @param console The console in which realize the operation
*/
public static MountPoint getMountPointFromDirectory(Console console, String dir) {
try {
- //Retrieve the mount points
- List<MountPoint> mps =
- CommandHelper.getMountPoints(null, console);
-
- //Sort mount points in reverse order, needed for avoid
- //found an incorrect that matches the name
- Collections.sort(mps, new Comparator<MountPoint>() {
- @Override
- public int compare(MountPoint lhs, MountPoint rhs) {
- return lhs.compareTo(rhs) * -1;
+ // For non-rooted devices, which console is java and runs under a chrooted
+ // device, mount point info mustn't be a main objective. Caching the status
+ // should be enough and operation runs smoothly.
+ // Refresh mount points after some time (5 minutes should be enough)
+ long now = System.currentTimeMillis();
+ synchronized(sMountPoints) {
+ if (sMountPoints == null || (now - sLastCachedTime) > MAX_CACHED_TIME || FileManagerApplication.hasShellCommands()) {
+ //Retrieve the mount points
+ refreshMountPoints(console);
+ }
+ if (sMountPoints == null) {
+ return null;
}
- });
-
- //Search for the mount point information
- int cc = mps.size();
- for (int i = 0; i < cc; i++) {
- MountPoint mp = mps.get(i);
- if (dir.startsWith(mp.getMountPoint())) {
- return mp;
+
+ //Sort mount points in reverse order, needed for avoid
+ //found an incorrect mount point that matches the name
+ Collections.sort(sMountPoints, new Comparator<MountPoint>() {
+ @Override
+ public int compare(MountPoint lhs, MountPoint rhs) {
+ return lhs.compareTo(rhs) * -1;
+ }
+ });
+
+ //Search for the mount point information
+ int cc = sMountPoints.size();
+ for (int i = 0; i < cc; i++) {
+ MountPoint mp = sMountPoints.get(i);
+ if (dir.startsWith(mp.getMountPoint())) {
+ return mp;
+ }
}
}
}
/**
- * Method that returns if the filesystem can be mounted.
+ * Method that returns if a filesystem is allowed to be mounted/unmounted (rw/ro).
*
* @param mp The mount point to check
- * @return boolean If the mount point can be mounted
+ * @return boolean If the mount point can be mounted/unmount (rw/ro)
*/
public static boolean isMountAllowed(MountPoint mp) {
- return ALLOWED_FS_TYPE.contains(mp.getType());
+ return !RESTRICTED_FS_TYPE.contains(mp.getType());
}
}