Environment.java revision 3459e27e5d2f92d2d5487c189c9386c031e79fc1
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.content.res.Resources;
22import android.os.storage.IMountService;
23
24/**
25 * Provides access to environment variables.
26 */
27public class Environment {
28
29    private static final File ROOT_DIRECTORY
30            = getDirectory("ANDROID_ROOT", "/system");
31
32    private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
33
34    private static class MountServiceHolder {
35        static IMountService mSingleton = IMountService.Stub.asInterface(ServiceManager
36                .getService("mount"));
37    }
38
39    private static final Object mLock = new Object();
40
41    private volatile static Boolean mIsExternalStorageEmulated = null;
42
43    /**
44     * Gets the Android root directory.
45     */
46    public static File getRootDirectory() {
47        return ROOT_DIRECTORY;
48    }
49
50    /**
51     * Gets the system directory available for secure storage.
52     * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
53     * Otherwise, it returns the unencrypted /data/system directory.
54     * @return File object representing the secure storage system directory.
55     * @hide
56     */
57    public static File getSystemSecureDirectory() {
58        if (isEncryptedFilesystemEnabled()) {
59            return new File(SECURE_DATA_DIRECTORY, "system");
60        } else {
61            return new File(DATA_DIRECTORY, "system");
62        }
63    }
64
65    /**
66     * Gets the data directory for secure storage.
67     * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
68     * Otherwise, it returns the unencrypted /data directory.
69     * @return File object representing the data directory for secure storage.
70     * @hide
71     */
72    public static File getSecureDataDirectory() {
73        if (isEncryptedFilesystemEnabled()) {
74            return SECURE_DATA_DIRECTORY;
75        } else {
76            return DATA_DIRECTORY;
77        }
78    }
79
80    /**
81     * Returns whether the Encrypted File System feature is enabled on the device or not.
82     * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
83     * if disabled.
84     * @hide
85     */
86    public static boolean isEncryptedFilesystemEnabled() {
87        return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
88    }
89
90    private static final File DATA_DIRECTORY
91            = getDirectory("ANDROID_DATA", "/data");
92
93    /**
94     * @hide
95     */
96    private static final File SECURE_DATA_DIRECTORY
97            = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
98
99    private static final File EXTERNAL_STORAGE_DIRECTORY
100            = getDirectory("EXTERNAL_STORAGE", "/sdcard");
101
102    private static final File EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY
103            = new File (new File(getDirectory("EXTERNAL_STORAGE", "/sdcard"),
104                    "Android"), "data");
105
106    private static final File EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY
107            = new File (new File(getDirectory("EXTERNAL_STORAGE", "/sdcard"),
108                    "Android"), "media");
109
110    private static final File DOWNLOAD_CACHE_DIRECTORY
111            = getDirectory("DOWNLOAD_CACHE", "/cache");
112
113    /**
114     * Gets the Android data directory.
115     */
116    public static File getDataDirectory() {
117        return DATA_DIRECTORY;
118    }
119
120    /**
121     * Gets the Android external storage directory.  This directory may not
122     * currently be accessible if it has been mounted by the user on their
123     * computer, has been removed from the device, or some other problem has
124     * happened.  You can determine its current state with
125     * {@link #getExternalStorageState()}.
126     *
127     * <p><em>Note: don't be confused by the word "external" here.  This
128     * directory can better be thought as media/shared storage.  It is a
129     * filesystem that can hold a relatively large amount of data and that
130     * is shared across all applications (does not enforce permissions).
131     * Traditionally this is an SD card, but it may also be implemented as
132     * built-in storage in a device that is distinct from the protected
133     * internal storage and can be mounted as a filesystem on a computer.</em></p>
134     *
135     * <p>In devices with multiple "external" storage directories (such as
136     * both secure app storage and mountable shared storage), this directory
137     * represents the "primary" external storage that the user will interact
138     * with.</p>
139     *
140     * <p>Applications should not directly use this top-level directory, in
141     * order to avoid polluting the user's root namespace.  Any files that are
142     * private to the application should be placed in a directory returned
143     * by {@link android.content.Context#getExternalFilesDir
144     * Context.getExternalFilesDir}, which the system will take care of deleting
145     * if the application is uninstalled.  Other shared files should be placed
146     * in one of the directories returned by
147     * {@link #getExternalStoragePublicDirectory}.
148     *
149     * <p>Here is an example of typical code to monitor the state of
150     * external storage:</p>
151     *
152     * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
153     * monitor_storage}
154     *
155     * @see #getExternalStorageState()
156     * @see #isExternalStorageRemovable()
157     */
158    public static File getExternalStorageDirectory() {
159        return EXTERNAL_STORAGE_DIRECTORY;
160    }
161
162    /**
163     * Standard directory in which to place any audio files that should be
164     * in the regular list of music for the user.
165     * This may be combined with
166     * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
167     * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
168     * of directories to categories a particular audio file as more than one
169     * type.
170     */
171    public static String DIRECTORY_MUSIC = "Music";
172
173    /**
174     * Standard directory in which to place any audio files that should be
175     * in the list of podcasts that the user can select (not as regular
176     * music).
177     * This may be combined with {@link #DIRECTORY_MUSIC},
178     * {@link #DIRECTORY_NOTIFICATIONS},
179     * {@link #DIRECTORY_ALARMS}, 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_PODCASTS = "Podcasts";
184
185    /**
186     * Standard directory in which to place any audio files that should be
187     * in the list of ringtones that the user can select (not as regular
188     * music).
189     * This may be combined with {@link #DIRECTORY_MUSIC},
190     * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
191     * {@link #DIRECTORY_ALARMS} as a series
192     * of directories to categories a particular audio file as more than one
193     * type.
194     */
195    public static String DIRECTORY_RINGTONES = "Ringtones";
196
197    /**
198     * Standard directory in which to place any audio files that should be
199     * in the list of alarms that the user can select (not as regular
200     * music).
201     * This may be combined with {@link #DIRECTORY_MUSIC},
202     * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
203     * and {@link #DIRECTORY_RINGTONES} as a series
204     * of directories to categories a particular audio file as more than one
205     * type.
206     */
207    public static String DIRECTORY_ALARMS = "Alarms";
208
209    /**
210     * Standard directory in which to place any audio files that should be
211     * in the list of notifications that the user can select (not as regular
212     * music).
213     * This may be combined with {@link #DIRECTORY_MUSIC},
214     * {@link #DIRECTORY_PODCASTS},
215     * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
216     * of directories to categories a particular audio file as more than one
217     * type.
218     */
219    public static String DIRECTORY_NOTIFICATIONS = "Notifications";
220
221    /**
222     * Standard directory in which to place pictures that are available to
223     * the user.  Note that this is primarily a convention for the top-level
224     * public directory, as the media scanner will find and collect pictures
225     * in any directory.
226     */
227    public static String DIRECTORY_PICTURES = "Pictures";
228
229    /**
230     * Standard directory in which to place movies that are available to
231     * the user.  Note that this is primarily a convention for the top-level
232     * public directory, as the media scanner will find and collect movies
233     * in any directory.
234     */
235    public static String DIRECTORY_MOVIES = "Movies";
236
237    /**
238     * Standard directory in which to place files that have been downloaded by
239     * the user.  Note that this is primarily a convention for the top-level
240     * public directory, you are free to download files anywhere in your own
241     * private directories.  Also note that though the constant here is
242     * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
243     * backwards compatibility reasons.
244     */
245    public static String DIRECTORY_DOWNLOADS = "Download";
246
247    /**
248     * The traditional location for pictures and videos when mounting the
249     * device as a camera.  Note that this is primarily a convention for the
250     * top-level public directory, as this convention makes no sense elsewhere.
251     */
252    public static String DIRECTORY_DCIM = "DCIM";
253
254    /**
255     * Get a top-level public external storage directory for placing files of
256     * a particular type.  This is where the user will typically place and
257     * manage their own files, so you should be careful about what you put here
258     * to ensure you don't erase their files or get in the way of their own
259     * organization.
260     *
261     * <p>Here is an example of typical code to manipulate a picture on
262     * the public external storage:</p>
263     *
264     * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
265     * public_picture}
266     *
267     * @param type The type of storage directory to return.  Should be one of
268     * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
269     * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
270     * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
271     * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
272     * {@link #DIRECTORY_DCIM}.  May not be null.
273     *
274     * @return Returns the File path for the directory.  Note that this
275     * directory may not yet exist, so you must make sure it exists before
276     * using it such as with {@link File#mkdirs File.mkdirs()}.
277     */
278    public static File getExternalStoragePublicDirectory(String type) {
279        return new File(getExternalStorageDirectory(), type);
280    }
281
282    /**
283     * Returns the path for android-specific data on the SD card.
284     * @hide
285     */
286    public static File getExternalStorageAndroidDataDir() {
287        return EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY;
288    }
289
290    /**
291     * Generates the raw path to an application's data
292     * @hide
293     */
294    public static File getExternalStorageAppDataDirectory(String packageName) {
295        return new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName);
296    }
297
298    /**
299     * Generates the raw path to an application's media
300     * @hide
301     */
302    public static File getExternalStorageAppMediaDirectory(String packageName) {
303        return new File(EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY, packageName);
304    }
305
306    /**
307     * Generates the path to an application's files.
308     * @hide
309     */
310    public static File getExternalStorageAppFilesDirectory(String packageName) {
311        return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
312                packageName), "files");
313    }
314
315    /**
316     * Generates the path to an application's cache.
317     * @hide
318     */
319    public static File getExternalStorageAppCacheDirectory(String packageName) {
320        return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
321                packageName), "cache");
322    }
323
324    /**
325     * Gets the Android Download/Cache content directory.
326     */
327    public static File getDownloadCacheDirectory() {
328        return DOWNLOAD_CACHE_DIRECTORY;
329    }
330
331    /**
332     * getExternalStorageState() returns MEDIA_REMOVED if the media is not present.
333     */
334    public static final String MEDIA_REMOVED = "removed";
335
336    /**
337     * getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present
338     * but not mounted.
339     */
340    public static final String MEDIA_UNMOUNTED = "unmounted";
341
342    /**
343     * getExternalStorageState() returns MEDIA_CHECKING if the media is present
344     * and being disk-checked
345     */
346    public static final String MEDIA_CHECKING = "checking";
347
348    /**
349     * getExternalStorageState() returns MEDIA_NOFS if the media is present
350     * but is blank or is using an unsupported filesystem
351     */
352    public static final String MEDIA_NOFS = "nofs";
353
354    /**
355     * getExternalStorageState() returns MEDIA_MOUNTED if the media is present
356     * and mounted at its mount point with read/write access.
357     */
358    public static final String MEDIA_MOUNTED = "mounted";
359
360    /**
361     * getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present
362     * and mounted at its mount point with read only access.
363     */
364    public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
365
366    /**
367     * getExternalStorageState() returns MEDIA_SHARED if the media is present
368     * not mounted, and shared via USB mass storage.
369     */
370    public static final String MEDIA_SHARED = "shared";
371
372    /**
373     * getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was
374     * removed before it was unmounted.
375     */
376    public static final String MEDIA_BAD_REMOVAL = "bad_removal";
377
378    /**
379     * getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present
380     * but cannot be mounted.  Typically this happens if the file system on the
381     * media is corrupted.
382     */
383    public static final String MEDIA_UNMOUNTABLE = "unmountable";
384
385    /**
386     * Gets the current state of the primary "external" storage device.
387     *
388     * <p>See {@link #getExternalStorageDirectory()} for more information.
389     */
390    public static String getExternalStorageState() {
391        try {
392            return MountServiceHolder.mSingleton.getVolumeState(getExternalStorageDirectory()
393                    .toString());
394        } catch (Exception rex) {
395            return Environment.MEDIA_REMOVED;
396        }
397    }
398
399    /**
400     * Returns whether the primary "external" storage device is removable.
401     * If true is returned, this device is for example an SD card that the
402     * user can remove.  If false is returned, the storage is built into
403     * the device and can not be physically removed.
404     *
405     * <p>See {@link #getExternalStorageDirectory()} for more information.
406     */
407    public static boolean isExternalStorageRemovable() {
408        return Resources.getSystem().getBoolean(
409                com.android.internal.R.bool.config_externalStorageRemovable);
410    }
411
412    /**
413     * Returns whether the device has an external storage device which is
414     * emulated. If true, the device does not have real external storage
415     * and certain system services such as the package manager use this
416     * to determine where to install an application.
417     */
418    public static boolean isExternalStorageEmulated() {
419        if (mIsExternalStorageEmulated == null) {
420            synchronized (mLock) {
421                if (mIsExternalStorageEmulated == null) {
422                    boolean externalStorageEmulated;
423                    try {
424                        externalStorageEmulated =
425                                MountServiceHolder.mSingleton.isExternalStorageEmulated();
426                    } catch (Exception e) {
427                        externalStorageEmulated = false;
428                    }
429                    mIsExternalStorageEmulated = Boolean.valueOf(externalStorageEmulated);
430                }
431            }
432        }
433        return mIsExternalStorageEmulated;
434    }
435
436    static File getDirectory(String variableName, String defaultPath) {
437        String path = System.getenv(variableName);
438        return path == null ? new File(defaultPath) : new File(path);
439    }
440}
441