OSDN Git Service

CMFileManager: Fixes for non-rooted devices
[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.Arrays;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.List;
31
32 /**
33  * A helper class with useful methods for deal with mount points.
34  */
35 public final class MountPointHelper {
36
37     private static final String TAG = "MountPointHelper"; //$NON-NLS-1$
38
39     private static final List<String> ALLOWED_FS_TYPE = Arrays.asList(new String[]{
40                                                 "rootfs", //$NON-NLS-1$
41                                                 "tmpfs",  //$NON-NLS-1$
42                                                 "vfat",   //$NON-NLS-1$
43                                                 "ext2",   //$NON-NLS-1$
44                                                 "ext3",   //$NON-NLS-1$
45                                                 "ext4"    //$NON-NLS-1$
46                                                     });
47
48     private static final long MAX_CACHED_TIME = 60000L * 5;
49
50     private static List<MountPoint> sMountPoints;
51     private static long sLastCachedTime;
52
53     /**
54      * Constructor of <code>MountPointHelper</code>.
55      */
56     private MountPointHelper() {
57         super();
58     }
59
60     /**
61      * Method that retrieve the mount point information for a directory.
62      *
63      * @param dir The directory of which recovers his mount point information
64      * @return MountPoint The mount point information
65      */
66     public static MountPoint getMountPointFromDirectory(FileSystemObject dir) {
67         return getMountPointFromDirectory(dir.getFullPath());
68     }
69
70     /**
71      * Method that retrieve the mount point information for a directory.
72      *
73      * @param dir The directory of which recovers his mount point information
74      * @return MountPoint The mount point information
75      */
76     public static MountPoint getMountPointFromDirectory(String dir) {
77         try {
78             return getMountPointFromDirectory(FileManagerApplication.getBackgroundConsole(), dir);
79         } catch (Exception e) {
80             Log.e(TAG, "Failed to retrieve mount point information.", e); //$NON-NLS-1$
81         }
82         return null;
83     }
84
85     /**
86      * Method that retrieve the mount point information for a directory.
87      *
88      * @param console The console in which realize the operation
89      * @param dir The directory of which recovers his mount point information
90      * @return MountPoint The mount point information
91      */
92     public synchronized static MountPoint getMountPointFromDirectory(Console console, String dir) {
93         try {
94             // For non-rooted devices, which console is java and runs under a chrooted
95             // device, mount point info mustn't be a main objective. Caching the status
96             // should be enough and operation runs smoothly.
97             // Refresh mount points after some time (5 minutes should be enough)
98             long now = System.currentTimeMillis();
99             if (sMountPoints == null || (now - sLastCachedTime) > MAX_CACHED_TIME ||
100                 FileManagerApplication.isDeviceRooted()) {
101                 //Retrieve the mount points
102                 List<MountPoint> mps =
103                         CommandHelper.getMountPoints(null, console);
104                 sMountPoints = mps;
105                 sLastCachedTime = now;
106             }
107
108             //Sort mount points in reverse order, needed for avoid
109             //found an incorrect that matches the name
110             Collections.sort(sMountPoints, new Comparator<MountPoint>() {
111                 @Override
112                 public int compare(MountPoint lhs, MountPoint rhs) {
113                     return lhs.compareTo(rhs) * -1;
114                 }
115             });
116
117             //Search for the mount point information
118             int cc = sMountPoints.size();
119             for (int i = 0; i < cc; i++) {
120                 MountPoint mp = sMountPoints.get(i);
121                 if (dir.startsWith(mp.getMountPoint())) {
122                     return mp;
123                 }
124             }
125
126         } catch (Exception e) {
127             Log.e(TAG, "Failed to retrieve the mount point information", e); //$NON-NLS-1$
128         }
129
130         //No mount point found
131         return null;
132     }
133
134     /**
135      * Method that retrieve information about the disk usage of the mount point.
136      *
137      * @param mp The mount point
138      * @return DiskUsage The disk usage information
139      */
140     public static DiskUsage getMountPointDiskUsage(MountPoint mp) {
141         return getMountPointDiskUsage(FileManagerApplication.getBackgroundConsole(), mp);
142     }
143
144     /**
145      * Method that retrieve information about the disk usage of the mount point.
146      *
147      * @param console The console in which realize the operation
148      * @param mp The mount point
149      * @return DiskUsage The disk usage information
150      */
151     public static DiskUsage getMountPointDiskUsage(Console console, MountPoint mp) {
152         try {
153             //Retrieve the mount points
154             return CommandHelper.getDiskUsage(null, mp.getMountPoint(), console);
155
156         } catch (Exception e) {
157             Log.e(TAG,
158                     String.format("Fail to load disk usage of mount point: %s",  //$NON-NLS-1$
159                             mp.getMountPoint()), e);
160         }
161
162         //No mount point found
163         return null;
164     }
165
166     /**
167      * Method that returns if the filesystem is mounted as readonly.
168      *
169      * @param mp The mount point to check
170      * @return boolean If the mount point is mounted as readonly
171      */
172     public static boolean isReadOnly(MountPoint mp) {
173         try {
174             return mp.getOptions().startsWith(MountExecutable.READONLY);
175         } catch (Exception e) {
176             Log.e(TAG, "Method \"isReadOnly\" failed.", e); //$NON-NLS-1$
177         }
178
179         //On fail is more secure consider it as read-only
180         return true;
181     }
182
183     /**
184      * Method that returns if the filesystem is mounted as read-write.
185      *
186      * @param mp The mount point to check
187      * @return boolean If the mount point is mounted as read-write
188      */
189     public static boolean isReadWrite(MountPoint mp) {
190         try {
191             return mp.getOptions().startsWith(MountExecutable.READWRITE);
192         } catch (Exception e) {
193             Log.e(TAG, "Method \"isReadWrite\" failed.", e); //$NON-NLS-1$
194         }
195
196         //On fail is more secure consider it as read-only
197         return false;
198     }
199
200     /**
201      * Method that returns if the filesystem can be mounted.
202      *
203      * @param mp The mount point to check
204      * @return boolean If the mount point can be mounted
205      */
206     public static boolean isMountAllowed(MountPoint mp) {
207         return ALLOWED_FS_TYPE.contains(mp.getType());
208     }
209 }