OSDN Git Service

Fix FileSystemInfo's ImageView shows incorrect picture in BreadcrumbView .
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / util / MountPointHelper.java
old mode 100644 (file)
new mode 100755 (executable)
index 9703618..65c9181
@@ -24,6 +24,7 @@ import com.cyanogenmod.filemanager.model.DiskUsage;
 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;
@@ -36,15 +37,20 @@ public final class MountPointHelper {
 
     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>.
      */
@@ -78,6 +84,25 @@ public final class MountPointHelper {
     }
 
     /**
+     * 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
@@ -86,25 +111,36 @@ public final class MountPointHelper {
      */
     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;
+                    }
                 }
             }
 
@@ -183,12 +219,12 @@ public final class MountPointHelper {
     }
 
     /**
-     * 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());
     }
 }