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