Environment.java revision d49c455cc97d5fcdd8af497dc2ba85efdc719959
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 EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY
95            = new File (new File(getDirectory("EXTERNAL_STORAGE", "/sdcard"),
96                    "Android"), "data");
97
98    private static final File EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY
99            = new File (new File(getDirectory("EXTERNAL_STORAGE", "/sdcard"),
100                    "Android"), "media");
101
102    private static final File DOWNLOAD_CACHE_DIRECTORY
103            = getDirectory("DOWNLOAD_CACHE", "/cache");
104
105    /**
106     * Gets the Android data directory.
107     */
108    public static File getDataDirectory() {
109        return DATA_DIRECTORY;
110    }
111
112    /**
113     * Gets the Android external storage directory.  This directory may not
114     * currently be accessible if it has been mounted by the user on their
115     * computer, has been removed from the device, or some other problem has
116     * happened.  You can determine its current state with
117     * {@link #getExternalStorageState()}.
118     *
119     * <p>Applications should not directly use this top-level directory, in
120     * order to avoid polluting the user's root namespace.  Any files that are
121     * private to the application should be placed in a directory returned
122     * by {@link android.content.Context#getExternalFilesDir
123     * Context.getExternalFilesDir}, which the system will take care of deleting
124     * if the application is uninstalled.  Other shared files should be placed
125     * in one of the directories returned by
126     * {@link #getExternalStoragePublicDirectory}.
127     *
128     * <p>Here is an example of typical code to monitor the state of
129     * external storage:</p>
130     *
131     * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
132     * monitor_storage}
133     */
134    public static File getExternalStorageDirectory() {
135        return EXTERNAL_STORAGE_DIRECTORY;
136    }
137
138    /**
139     * Standard directory in which to place any audio files that should be
140     * in the regular list of music for the user.
141     * This may be combined with
142     * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
143     * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
144     * of directories to categories a particular audio file as more than one
145     * type.
146     */
147    public static String DIRECTORY_MUSIC = "Music";
148
149    /**
150     * Standard directory in which to place any audio files that should be
151     * in the list of podcasts that the user can select (not as regular
152     * music).
153     * This may be combined with {@link #DIRECTORY_MUSIC},
154     * {@link #DIRECTORY_NOTIFICATIONS},
155     * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
156     * of directories to categories a particular audio file as more than one
157     * type.
158     */
159    public static String DIRECTORY_PODCASTS = "Podcasts";
160
161    /**
162     * Standard directory in which to place any audio files that should be
163     * in the list of ringtones that the user can select (not as regular
164     * music).
165     * This may be combined with {@link #DIRECTORY_MUSIC},
166     * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
167     * {@link #DIRECTORY_ALARMS} as a series
168     * of directories to categories a particular audio file as more than one
169     * type.
170     */
171    public static String DIRECTORY_RINGTONES = "Ringtones";
172
173    /**
174     * Standard directory in which to place any audio files that should be
175     * in the list of alarms that the user can select (not as regular
176     * music).
177     * This may be combined with {@link #DIRECTORY_MUSIC},
178     * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
179     * and {@link #DIRECTORY_RINGTONES} as a series
180     * of directories to categories a particular audio file as more than one
181     * type.
182     */
183    public static String DIRECTORY_ALARMS = "Alarms";
184
185    /**
186     * Standard directory in which to place any audio files that should be
187     * in the list of notifications that the user can select (not as regular
188     * music).
189     * This may be combined with {@link #DIRECTORY_MUSIC},
190     * {@link #DIRECTORY_PODCASTS},
191     * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
192     * of directories to categories a particular audio file as more than one
193     * type.
194     */
195    public static String DIRECTORY_NOTIFICATIONS = "Notifications";
196
197    /**
198     * Standard directory in which to place pictures that are available to
199     * the user.  Note that this is primarily a convention for the top-level
200     * public directory, as the media scanner will find and collect pictures
201     * in any directory.
202     */
203    public static String DIRECTORY_PICTURES = "Pictures";
204
205    /**
206     * Standard directory in which to place movies that are available to
207     * the user.  Note that this is primarily a convention for the top-level
208     * public directory, as the media scanner will find and collect movies
209     * in any directory.
210     */
211    public static String DIRECTORY_MOVIES = "Movies";
212
213    /**
214     * Standard directory in which to place files that have been downloaded by
215     * the user.  Note that this is primarily a convention for the top-level
216     * public directory, you are free to download files anywhere in your own
217     * private directories.  Also note that though the constant here is
218     * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
219     * backwards compatibility reasons.
220     */
221    public static String DIRECTORY_DOWNLOADS = "Download";
222
223    /**
224     * The traditional location for pictures and videos when mounting the
225     * device as a camera.  Note that this is primarily a convention for the
226     * top-level public directory, as this convention makes no sense elsewhere.
227     */
228    public static String DIRECTORY_DCIM = "DCIM";
229
230    /**
231     * Get a top-level public external storage directory for placing files of
232     * a particular type.  This is where the user will typically place and
233     * manage their own files, so you should be careful about what you put here
234     * to ensure you don't erase their files or get in the way of their own
235     * organization.
236     *
237     * <p>Here is an example of typical code to manipulate a picture on
238     * the public external storage:</p>
239     *
240     * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
241     * public_picture}
242     *
243     * @param type The type of storage directory to return.  Should be one of
244     * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
245     * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
246     * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
247     * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
248     * {@link #DIRECTORY_DCIM}.  May not be null.
249     *
250     * @return Returns the File path for the directory.  Note that this
251     * directory may not yet exist, so you must make sure it exists before
252     * using it such as with {@link File#mkdirs File.mkdirs()}.
253     */
254    public static File getExternalStoragePublicDirectory(String type) {
255        return new File(getExternalStorageDirectory(), type);
256    }
257
258    /**
259     * Returns the path for android-specific data on the SD card.
260     * @hide
261     */
262    public static File getExternalStorageAndroidDataDir() {
263        return EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY;
264    }
265
266    /**
267     * Generates the raw path to an application's data
268     * @hide
269     */
270    public static File getExternalStorageAppDataDirectory(String packageName) {
271        return new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName);
272    }
273
274    /**
275     * Generates the raw path to an application's media
276     * @hide
277     */
278    public static File getExternalStorageAppMediaDirectory(String packageName) {
279        return new File(EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY, packageName);
280    }
281
282    /**
283     * Generates the path to an application's files.
284     * @hide
285     */
286    public static File getExternalStorageAppFilesDirectory(String packageName) {
287        return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
288                packageName), "files");
289    }
290
291    /**
292     * Generates the path to an application's cache.
293     * @hide
294     */
295    public static File getExternalStorageAppCacheDirectory(String packageName) {
296        return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
297                packageName), "cache");
298    }
299
300    /**
301     * Gets the Android Download/Cache content directory.
302     */
303    public static File getDownloadCacheDirectory() {
304        return DOWNLOAD_CACHE_DIRECTORY;
305    }
306
307    /**
308     * getExternalStorageState() returns MEDIA_REMOVED if the media is not present.
309     */
310    public static final String MEDIA_REMOVED = "removed";
311
312    /**
313     * getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present
314     * but not mounted.
315     */
316    public static final String MEDIA_UNMOUNTED = "unmounted";
317
318    /**
319     * getExternalStorageState() returns MEDIA_CHECKING if the media is present
320     * and being disk-checked
321     */
322    public static final String MEDIA_CHECKING = "checking";
323
324    /**
325     * getExternalStorageState() returns MEDIA_NOFS if the media is present
326     * but is blank or is using an unsupported filesystem
327     */
328    public static final String MEDIA_NOFS = "nofs";
329
330    /**
331     * getExternalStorageState() returns MEDIA_MOUNTED if the media is present
332     * and mounted at its mount point with read/write access.
333     */
334    public static final String MEDIA_MOUNTED = "mounted";
335
336    /**
337     * getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present
338     * and mounted at its mount point with read only access.
339     */
340    public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
341
342    /**
343     * getExternalStorageState() returns MEDIA_SHARED if the media is present
344     * not mounted, and shared via USB mass storage.
345     */
346    public static final String MEDIA_SHARED = "shared";
347
348    /**
349     * getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was
350     * removed before it was unmounted.
351     */
352    public static final String MEDIA_BAD_REMOVAL = "bad_removal";
353
354    /**
355     * getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present
356     * but cannot be mounted.  Typically this happens if the file system on the
357     * media is corrupted.
358     */
359    public static final String MEDIA_UNMOUNTABLE = "unmountable";
360
361    /**
362     * Gets the current state of the external storage device.
363     * Note: This call should be deprecated as it doesn't support
364     * multiple volumes.
365     *
366     * <p>See {@link #getExternalStorageDirectory()} for an example of its use.
367     */
368    public static String getExternalStorageState() {
369        try {
370            if (mMntSvc == null) {
371                mMntSvc = IMountService.Stub.asInterface(ServiceManager
372                                                         .getService("mount"));
373            }
374            return mMntSvc.getVolumeState(getExternalStorageDirectory().toString());
375        } catch (Exception rex) {
376            return Environment.MEDIA_REMOVED;
377        }
378    }
379
380    static File getDirectory(String variableName, String defaultPath) {
381        String path = System.getenv(variableName);
382        return path == null ? new File(defaultPath) : new File(path);
383    }
384}
385