2 * Copyright (C) 2012 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.cyanogenmod.filemanager.util;
18 import android.util.Log;
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;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.List;
33 * A helper class with useful methods for deal with mount points.
35 public final class MountPointHelper {
37 private static final String TAG = "MountPointHelper"; //$NON-NLS-1$
39 private static final List<String> ALLOWED_FS_TYPE = Arrays.asList(new String[]{
40 "rootfs", //$NON-NLS-1$
41 "tmpfs", //$NON-NLS-1$
48 private static final long MAX_CACHED_TIME = 60000L * 5;
50 private static List<MountPoint> sMountPoints;
51 private static long sLastCachedTime;
54 * Constructor of <code>MountPointHelper</code>.
56 private MountPointHelper() {
61 * Method that retrieve the mount point information for a directory.
63 * @param dir The directory of which recovers his mount point information
64 * @return MountPoint The mount point information
66 public static MountPoint getMountPointFromDirectory(FileSystemObject dir) {
67 return getMountPointFromDirectory(dir.getFullPath());
71 * Method that retrieve the mount point information for a directory.
73 * @param dir The directory of which recovers his mount point information
74 * @return MountPoint The mount point information
76 public static MountPoint getMountPointFromDirectory(String dir) {
78 return getMountPointFromDirectory(FileManagerApplication.getBackgroundConsole(), dir);
79 } catch (Exception e) {
80 Log.e(TAG, "Failed to retrieve mount point information.", e); //$NON-NLS-1$
86 * Method that retrieve the mount point information for a directory.
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
92 public synchronized static MountPoint getMountPointFromDirectory(Console console, String dir) {
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);
105 sLastCachedTime = now;
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>() {
112 public int compare(MountPoint lhs, MountPoint rhs) {
113 return lhs.compareTo(rhs) * -1;
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())) {
126 } catch (Exception e) {
127 Log.e(TAG, "Failed to retrieve the mount point information", e); //$NON-NLS-1$
130 //No mount point found
135 * Method that retrieve information about the disk usage of the mount point.
137 * @param mp The mount point
138 * @return DiskUsage The disk usage information
140 public static DiskUsage getMountPointDiskUsage(MountPoint mp) {
141 return getMountPointDiskUsage(FileManagerApplication.getBackgroundConsole(), mp);
145 * Method that retrieve information about the disk usage of the mount point.
147 * @param console The console in which realize the operation
148 * @param mp The mount point
149 * @return DiskUsage The disk usage information
151 public static DiskUsage getMountPointDiskUsage(Console console, MountPoint mp) {
153 //Retrieve the mount points
154 return CommandHelper.getDiskUsage(null, mp.getMountPoint(), console);
156 } catch (Exception e) {
158 String.format("Fail to load disk usage of mount point: %s", //$NON-NLS-1$
159 mp.getMountPoint()), e);
162 //No mount point found
167 * Method that returns if the filesystem is mounted as readonly.
169 * @param mp The mount point to check
170 * @return boolean If the mount point is mounted as readonly
172 public static boolean isReadOnly(MountPoint mp) {
174 return mp.getOptions().startsWith(MountExecutable.READONLY);
175 } catch (Exception e) {
176 Log.e(TAG, "Method \"isReadOnly\" failed.", e); //$NON-NLS-1$
179 //On fail is more secure consider it as read-only
184 * Method that returns if the filesystem is mounted as read-write.
186 * @param mp The mount point to check
187 * @return boolean If the mount point is mounted as read-write
189 public static boolean isReadWrite(MountPoint mp) {
191 return mp.getOptions().startsWith(MountExecutable.READWRITE);
192 } catch (Exception e) {
193 Log.e(TAG, "Method \"isReadWrite\" failed.", e); //$NON-NLS-1$
196 //On fail is more secure consider it as read-only
201 * Method that returns if the filesystem can be mounted.
203 * @param mp The mount point to check
204 * @return boolean If the mount point can be mounted
206 public static boolean isMountAllowed(MountPoint mp) {
207 return ALLOWED_FS_TYPE.contains(mp.getType());