Environment.java revision b104340496e3a531e26c8f428c808eca0e039f50
1/*
2 * Copyright (C) 2007 The Android Open Source 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
17package android.os;
18
19import java.io.File;
20
21import android.os.storage.IMountService;
22
23/**
24 * Provides access to environment variables.
25 */
26public class Environment {
27
28    private static final File ROOT_DIRECTORY
29            = getDirectory("ANDROID_ROOT", "/system");
30
31    private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
32
33    private static IMountService mMntSvc = null;
34
35    /**
36     * Gets the Android root directory.
37     */
38    public static File getRootDirectory() {
39        return ROOT_DIRECTORY;
40    }
41
42    /**
43     * Gets the system directory available for secure storage.
44     * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
45     * Otherwise, it returns the unencrypted /data/system directory.
46     * @return File object representing the secure storage system directory.
47     * @hide
48     */
49    public static File getSystemSecureDirectory() {
50        if (isEncryptedFilesystemEnabled()) {
51            return new File(SECURE_DATA_DIRECTORY, "system");
52        } else {
53            return new File(DATA_DIRECTORY, "system");
54        }
55    }
56
57    /**
58     * Gets the data directory for secure storage.
59     * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
60     * Otherwise, it returns the unencrypted /data directory.
61     * @return File object representing the data directory for secure storage.
62     * @hide
63     */
64    public static File getSecureDataDirectory() {
65        if (isEncryptedFilesystemEnabled()) {
66            return SECURE_DATA_DIRECTORY;
67        } else {
68            return DATA_DIRECTORY;
69        }
70    }
71
72    /**
73     * Returns whether the Encrypted File System feature is enabled on the device or not.
74     * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
75     * if disabled.
76     * @hide
77     */
78    public static boolean isEncryptedFilesystemEnabled() {
79        return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
80    }
81
82    private static final File DATA_DIRECTORY
83            = getDirectory("ANDROID_DATA", "/data");
84
85    /**
86     * @hide
87     */
88    private static final File SECURE_DATA_DIRECTORY
89            = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
90
91    private static final File EXTERNAL_STORAGE_DIRECTORY
92            = getDirectory("EXTERNAL_STORAGE", "/sdcard");
93
94    private static final File DOWNLOAD_CACHE_DIRECTORY
95            = getDirectory("DOWNLOAD_CACHE", "/cache");
96
97    /**
98     * Gets the Android data directory.
99     */
100    public static File getDataDirectory() {
101        return DATA_DIRECTORY;
102    }
103
104    /**
105     * Gets the Android external storage directory.
106     */
107    public static File getExternalStorageDirectory() {
108        return EXTERNAL_STORAGE_DIRECTORY;
109    }
110
111    /**
112     * Gets the Android Download/Cache content directory.
113     */
114    public static File getDownloadCacheDirectory() {
115        return DOWNLOAD_CACHE_DIRECTORY;
116    }
117
118    /**
119     * getExternalStorageState() returns MEDIA_REMOVED if the media is not present.
120     */
121    public static final String MEDIA_REMOVED = "removed";
122
123    /**
124     * getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present
125     * but not mounted.
126     */
127    public static final String MEDIA_UNMOUNTED = "unmounted";
128
129    /**
130     * getExternalStorageState() returns MEDIA_CHECKING if the media is present
131     * and being disk-checked
132     */
133    public static final String MEDIA_CHECKING = "checking";
134
135    /**
136     * getExternalStorageState() returns MEDIA_NOFS if the media is present
137     * but is blank or is using an unsupported filesystem
138     */
139    public static final String MEDIA_NOFS = "nofs";
140
141    /**
142     * getExternalStorageState() returns MEDIA_MOUNTED if the media is present
143     * and mounted at its mount point with read/write access.
144     */
145    public static final String MEDIA_MOUNTED = "mounted";
146
147    /**
148     * getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present
149     * and mounted at its mount point with read only access.
150     */
151    public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
152
153    /**
154     * getExternalStorageState() returns MEDIA_SHARED if the media is present
155     * not mounted, and shared via USB mass storage.
156     */
157    public static final String MEDIA_SHARED = "shared";
158
159    /**
160     * getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was
161     * removed before it was unmounted.
162     */
163    public static final String MEDIA_BAD_REMOVAL = "bad_removal";
164
165    /**
166     * getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present
167     * but cannot be mounted.  Typically this happens if the file system on the
168     * media is corrupted.
169     */
170    public static final String MEDIA_UNMOUNTABLE = "unmountable";
171
172    /**
173     * Gets the current state of the external storage device.
174     * Note: This call should be deprecated as it doesn't support
175     * multiple volumes.
176     */
177    public static String getExternalStorageState() {
178        try {
179            if (mMntSvc == null) {
180                mMntSvc = IMountService.Stub.asInterface(ServiceManager
181                                                         .getService("mount"));
182            }
183            return mMntSvc.getVolumeState(getExternalStorageDirectory().toString());
184        } catch (Exception rex) {
185            return Environment.MEDIA_REMOVED;
186        }
187    }
188
189    static File getDirectory(String variableName, String defaultPath) {
190        String path = System.getenv(variableName);
191        return path == null ? new File(defaultPath) : new File(path);
192    }
193}
194