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
1 /*
2  * Copyright (C) 2012 The CyanogenMod Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.cyanogenmod.filemanager.util;
17
18 import android.util.Log;
19
20 import com.cyanogenmod.filemanager.FileManagerApplication;
21 import com.cyanogenmod.filemanager.commands.MountExecutable;
22 import com.cyanogenmod.filemanager.console.Console;
23 import com.cyanogenmod.filemanager.model.DiskUsage;
24 import com.cyanogenmod.filemanager.model.FileSystemObject;
25 import com.cyanogenmod.filemanager.model.MountPoint;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
32
33 /**
34  * A helper class with useful methods for deal with mount points.
35  */
36 public final class MountPointHelper {
37
38     private static final String TAG = "MountPointHelper"; //$NON-NLS-1$
39
40     private static final List<String> RESTRICTED_FS_TYPE = Arrays.asList(new String[]{
41                                                 "devpts", //$NON-NLS-1$
42                                                 "proc", //$NON-NLS-1$
43                                                 "sysfs", //$NON-NLS-1$
44                                                 "debugfs", //$NON-NLS-1$
45                                                 "cgroup", //$NON-NLS-1$
46                                                 "tmpfs" //$NON-NLS-1$
47                                                     });
48
49     private static final long MAX_CACHED_TIME = 60000L * 5;
50
51     private static List<MountPoint> sMountPoints = new ArrayList<>();
52     private static long sLastCachedTime;
53
54     /**
55      * Constructor of <code>MountPointHelper</code>.
56      */
57     private MountPointHelper() {
58         super();
59     }
60
61     /**
62      * Method that retrieve the mount point information for a directory.
63      *
64      * @param dir The directory of which recovers his mount point information
65      * @return MountPoint The mount point information
66      */
67     public static MountPoint getMountPointFromDirectory(FileSystemObject dir) {
68         return getMountPointFromDirectory(dir.getFullPath());
69     }
70
71     /**
72      * Method that retrieve the mount point information for a directory.
73      *
74      * @param dir The directory of which recovers his mount point information
75      * @return MountPoint The mount point information
76      */
77     public static MountPoint getMountPointFromDirectory(String dir) {
78         try {
79             return getMountPointFromDirectory(FileManagerApplication.getBackgroundConsole(), dir);
80         } catch (Exception e) {
81             Log.e(TAG, "Failed to retrieve mount point information.", e); //$NON-NLS-1$
82         }
83         return null;
84     }
85
86     /**
87      * Method that refresh the mount point information.
88      *
89      * @return boolean refresh success or not.
90      */
91     public static boolean refreshMountPoints(Console console) {
92         synchronized(sMountPoints) {
93             try {
94                 sMountPoints.clear();
95                 sMountPoints.addAll(CommandHelper.getMountPoints(null, null));
96                 sLastCachedTime = System.currentTimeMillis();
97                 return true;
98             } catch (Exception e) {
99                 Log.e(TAG, "Failed to update the mount point information", e); //$NON-NLS-1$
100             }
101         }
102         return false;
103     }
104
105     /**
106      * Method that retrieve the mount point information for a directory.
107      *
108      * @param console The console in which realize the operation
109      * @param dir The directory of which recovers his mount point information
110      * @return MountPoint The mount point information
111      */
112     public static MountPoint getMountPointFromDirectory(Console console, String dir) {
113         try {
114             // For non-rooted devices, which console is java and runs under a chrooted
115             // device, mount point info mustn't be a main objective. Caching the status
116             // should be enough and operation runs smoothly.
117             // Refresh mount points after some time (5 minutes should be enough)
118             long now = System.currentTimeMillis();
119             synchronized(sMountPoints) {
120                 if (sMountPoints == null || (now - sLastCachedTime) > MAX_CACHED_TIME || FileManagerApplication.hasShellCommands()) {
121                     //Retrieve the mount points
122                     refreshMountPoints(console);
123                 }
124                 if (sMountPoints == null) {
125                     return null;
126                 }
127
128                 //Sort mount points in reverse order, needed for avoid
129                 //found an incorrect mount point that matches the name
130                 Collections.sort(sMountPoints, new Comparator<MountPoint>() {
131                     @Override
132                     public int compare(MountPoint lhs, MountPoint rhs) {
133                         return lhs.compareTo(rhs) * -1;
134                     }
135                 });
136
137                 //Search for the mount point information
138                 int cc = sMountPoints.size();
139                 for (int i = 0; i < cc; i++) {
140                     MountPoint mp = sMountPoints.get(i);
141                     if (dir.startsWith(mp.getMountPoint())) {
142                         return mp;
143                     }
144                 }
145             }
146
147         } catch (Exception e) {
148             Log.e(TAG, "Failed to retrieve the mount point information", e); //$NON-NLS-1$
149         }
150
151         //No mount point found
152         return null;
153     }
154
155     /**
156      * Method that retrieve information about the disk usage of the mount point.
157      *
158      * @param mp The mount point
159      * @return DiskUsage The disk usage information
160      */
161     public static DiskUsage getMountPointDiskUsage(MountPoint mp) {
162         return getMountPointDiskUsage(FileManagerApplication.getBackgroundConsole(), mp);
163     }
164
165     /**
166      * Method that retrieve information about the disk usage of the mount point.
167      *
168      * @param console The console in which realize the operation
169      * @param mp The mount point
170      * @return DiskUsage The disk usage information
171      */
172     public static DiskUsage getMountPointDiskUsage(Console console, MountPoint mp) {
173         try {
174             //Retrieve the mount points
175             return CommandHelper.getDiskUsage(null, mp.getMountPoint(), console);
176
177         } catch (Exception e) {
178             Log.e(TAG,
179                     String.format("Fail to load disk usage of mount point: %s",  //$NON-NLS-1$
180                             mp.getMountPoint()), e);
181         }
182
183         //No mount point found
184         return null;
185     }
186
187     /**
188      * Method that returns if the filesystem is mounted as readonly.
189      *
190      * @param mp The mount point to check
191      * @return boolean If the mount point is mounted as readonly
192      */
193     public static boolean isReadOnly(MountPoint mp) {
194         try {
195             return mp.getOptions().startsWith(MountExecutable.READONLY);
196         } catch (Exception e) {
197             Log.e(TAG, "Method \"isReadOnly\" failed.", e); //$NON-NLS-1$
198         }
199
200         //On fail is more secure consider it as read-only
201         return true;
202     }
203
204     /**
205      * Method that returns if the filesystem is mounted as read-write.
206      *
207      * @param mp The mount point to check
208      * @return boolean If the mount point is mounted as read-write
209      */
210     public static boolean isReadWrite(MountPoint mp) {
211         try {
212             return mp.getOptions().startsWith(MountExecutable.READWRITE);
213         } catch (Exception e) {
214             Log.e(TAG, "Method \"isReadWrite\" failed.", e); //$NON-NLS-1$
215         }
216
217         //On fail is more secure consider it as read-only
218         return false;
219     }
220
221     /**
222      * Method that returns if a filesystem is allowed to be mounted/unmounted (rw/ro).
223      *
224      * @param mp The mount point to check
225      * @return boolean If the mount point can be mounted/unmount (rw/ro)
226      */
227     public static boolean isMountAllowed(MountPoint mp) {
228         return !RESTRICTED_FS_TYPE.contains(mp.getType());
229     }
230 }