blob: cc956428fb5af4c35e627c720c2617b6fe876d5f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Jason parksa3cdaa52011-01-13 14:15:43 -060019import java.io.File;
20
Dianne Hackborn407f6252010-10-04 11:31:17 -070021import android.content.res.Resources;
San Mehatb1043402010-02-05 08:26:50 -080022import android.os.storage.IMountService;
San Mehat7fd0fee2009-12-17 07:12:23 -080023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024/**
25 * Provides access to environment variables.
26 */
27public class Environment {
28
29 private static final File ROOT_DIRECTORY
30 = getDirectory("ANDROID_ROOT", "/system");
31
Oscar Montemayora8529f62009-11-18 10:14:20 -080032 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
33
Kenny Roote1ff2142010-10-12 11:20:01 -070034 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;
San Mehat7fd0fee2009-12-17 07:12:23 -080042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 /**
44 * Gets the Android root directory.
45 */
46 public static File getRootDirectory() {
47 return ROOT_DIRECTORY;
48 }
49
Jason parksa3cdaa52011-01-13 14:15:43 -060050 /**
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 private static final File DATA_DIRECTORY
91 = getDirectory("ANDROID_DATA", "/data");
92
Oscar Montemayora8529f62009-11-18 10:14:20 -080093 /**
94 * @hide
95 */
96 private static final File SECURE_DATA_DIRECTORY
97 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 private static final File EXTERNAL_STORAGE_DIRECTORY
100 = getDirectory("EXTERNAL_STORAGE", "/sdcard");
101
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800102 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
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800110 private static final File EXTERNAL_STORAGE_ANDROID_OBB_DIRECTORY
111 = new File (new File(getDirectory("EXTERNAL_STORAGE", "/sdcard"),
112 "Android"), "obb");
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 private static final File DOWNLOAD_CACHE_DIRECTORY
115 = getDirectory("DOWNLOAD_CACHE", "/cache");
116
117 /**
118 * Gets the Android data directory.
119 */
120 public static File getDataDirectory() {
121 return DATA_DIRECTORY;
122 }
123
124 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800125 * Gets the Android external storage directory. This directory may not
126 * currently be accessible if it has been mounted by the user on their
127 * computer, has been removed from the device, or some other problem has
128 * happened. You can determine its current state with
129 * {@link #getExternalStorageState()}.
130 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700131 * <p><em>Note: don't be confused by the word "external" here. This
132 * directory can better be thought as media/shared storage. It is a
133 * filesystem that can hold a relatively large amount of data and that
134 * is shared across all applications (does not enforce permissions).
135 * Traditionally this is an SD card, but it may also be implemented as
136 * built-in storage in a device that is distinct from the protected
137 * internal storage and can be mounted as a filesystem on a computer.</em></p>
138 *
139 * <p>In devices with multiple "external" storage directories (such as
140 * both secure app storage and mountable shared storage), this directory
141 * represents the "primary" external storage that the user will interact
142 * with.</p>
143 *
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700144 * <p>Applications should not directly use this top-level directory, in
145 * order to avoid polluting the user's root namespace. Any files that are
146 * private to the application should be placed in a directory returned
147 * by {@link android.content.Context#getExternalFilesDir
148 * Context.getExternalFilesDir}, which the system will take care of deleting
149 * if the application is uninstalled. Other shared files should be placed
150 * in one of the directories returned by
151 * {@link #getExternalStoragePublicDirectory}.
152 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800153 * <p>Here is an example of typical code to monitor the state of
154 * external storage:</p>
155 *
156 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
157 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700158 *
159 * @see #getExternalStorageState()
160 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 */
162 public static File getExternalStorageDirectory() {
163 return EXTERNAL_STORAGE_DIRECTORY;
164 }
165
166 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800167 * Standard directory in which to place any audio files that should be
168 * in the regular list of music for the user.
169 * This may be combined with
170 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
171 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
172 * of directories to categories a particular audio file as more than one
173 * type.
174 */
175 public static String DIRECTORY_MUSIC = "Music";
176
177 /**
178 * Standard directory in which to place any audio files that should be
179 * in the list of podcasts that the user can select (not as regular
180 * music).
181 * This may be combined with {@link #DIRECTORY_MUSIC},
182 * {@link #DIRECTORY_NOTIFICATIONS},
183 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
184 * of directories to categories a particular audio file as more than one
185 * type.
186 */
187 public static String DIRECTORY_PODCASTS = "Podcasts";
188
189 /**
190 * Standard directory in which to place any audio files that should be
191 * in the list of ringtones that the user can select (not as regular
192 * music).
193 * This may be combined with {@link #DIRECTORY_MUSIC},
194 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
195 * {@link #DIRECTORY_ALARMS} as a series
196 * of directories to categories a particular audio file as more than one
197 * type.
198 */
199 public static String DIRECTORY_RINGTONES = "Ringtones";
200
201 /**
202 * Standard directory in which to place any audio files that should be
203 * in the list of alarms that the user can select (not as regular
204 * music).
205 * This may be combined with {@link #DIRECTORY_MUSIC},
206 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
207 * and {@link #DIRECTORY_RINGTONES} as a series
208 * of directories to categories a particular audio file as more than one
209 * type.
210 */
211 public static String DIRECTORY_ALARMS = "Alarms";
212
213 /**
214 * Standard directory in which to place any audio files that should be
215 * in the list of notifications that the user can select (not as regular
216 * music).
217 * This may be combined with {@link #DIRECTORY_MUSIC},
218 * {@link #DIRECTORY_PODCASTS},
219 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
220 * of directories to categories a particular audio file as more than one
221 * type.
222 */
223 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
224
225 /**
226 * Standard directory in which to place pictures that are available to
227 * the user. Note that this is primarily a convention for the top-level
228 * public directory, as the media scanner will find and collect pictures
229 * in any directory.
230 */
231 public static String DIRECTORY_PICTURES = "Pictures";
232
233 /**
234 * Standard directory in which to place movies that are available to
235 * the user. Note that this is primarily a convention for the top-level
236 * public directory, as the media scanner will find and collect movies
237 * in any directory.
238 */
239 public static String DIRECTORY_MOVIES = "Movies";
240
241 /**
242 * Standard directory in which to place files that have been downloaded by
243 * the user. Note that this is primarily a convention for the top-level
244 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700245 * private directories. Also note that though the constant here is
246 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
247 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800248 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700249 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800250
251 /**
252 * The traditional location for pictures and videos when mounting the
253 * device as a camera. Note that this is primarily a convention for the
254 * top-level public directory, as this convention makes no sense elsewhere.
255 */
256 public static String DIRECTORY_DCIM = "DCIM";
257
258 /**
259 * Get a top-level public external storage directory for placing files of
260 * a particular type. This is where the user will typically place and
261 * manage their own files, so you should be careful about what you put here
262 * to ensure you don't erase their files or get in the way of their own
263 * organization.
264 *
265 * <p>Here is an example of typical code to manipulate a picture on
266 * the public external storage:</p>
267 *
268 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
269 * public_picture}
270 *
271 * @param type The type of storage directory to return. Should be one of
272 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
273 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
274 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
275 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
276 * {@link #DIRECTORY_DCIM}. May not be null.
277 *
278 * @return Returns the File path for the directory. Note that this
279 * directory may not yet exist, so you must make sure it exists before
280 * using it such as with {@link File#mkdirs File.mkdirs()}.
281 */
282 public static File getExternalStoragePublicDirectory(String type) {
283 return new File(getExternalStorageDirectory(), type);
284 }
285
286 /**
287 * Returns the path for android-specific data on the SD card.
288 * @hide
289 */
290 public static File getExternalStorageAndroidDataDir() {
291 return EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY;
292 }
293
294 /**
295 * Generates the raw path to an application's data
296 * @hide
297 */
298 public static File getExternalStorageAppDataDirectory(String packageName) {
299 return new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName);
300 }
301
302 /**
303 * Generates the raw path to an application's media
304 * @hide
305 */
306 public static File getExternalStorageAppMediaDirectory(String packageName) {
307 return new File(EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY, packageName);
308 }
309
310 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800311 * Generates the raw path to an application's OBB files
312 * @hide
313 */
314 public static File getExternalStorageAppObbDirectory(String packageName) {
315 return new File(EXTERNAL_STORAGE_ANDROID_OBB_DIRECTORY, packageName);
316 }
317
318 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800319 * Generates the path to an application's files.
320 * @hide
321 */
322 public static File getExternalStorageAppFilesDirectory(String packageName) {
323 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
324 packageName), "files");
325 }
326
327 /**
328 * Generates the path to an application's cache.
329 * @hide
330 */
331 public static File getExternalStorageAppCacheDirectory(String packageName) {
332 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
333 packageName), "cache");
334 }
335
336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * Gets the Android Download/Cache content directory.
338 */
339 public static File getDownloadCacheDirectory() {
340 return DOWNLOAD_CACHE_DIRECTORY;
341 }
342
343 /**
344 * getExternalStorageState() returns MEDIA_REMOVED if the media is not present.
345 */
346 public static final String MEDIA_REMOVED = "removed";
347
348 /**
349 * getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present
350 * but not mounted.
351 */
352 public static final String MEDIA_UNMOUNTED = "unmounted";
353
354 /**
355 * getExternalStorageState() returns MEDIA_CHECKING if the media is present
356 * and being disk-checked
357 */
358 public static final String MEDIA_CHECKING = "checking";
359
360 /**
361 * getExternalStorageState() returns MEDIA_NOFS if the media is present
362 * but is blank or is using an unsupported filesystem
363 */
364 public static final String MEDIA_NOFS = "nofs";
365
366 /**
367 * getExternalStorageState() returns MEDIA_MOUNTED if the media is present
368 * and mounted at its mount point with read/write access.
369 */
370 public static final String MEDIA_MOUNTED = "mounted";
371
372 /**
373 * getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present
374 * and mounted at its mount point with read only access.
375 */
376 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
377
378 /**
379 * getExternalStorageState() returns MEDIA_SHARED if the media is present
380 * not mounted, and shared via USB mass storage.
381 */
382 public static final String MEDIA_SHARED = "shared";
383
384 /**
385 * getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was
386 * removed before it was unmounted.
387 */
388 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
389
390 /**
391 * getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present
392 * but cannot be mounted. Typically this happens if the file system on the
393 * media is corrupted.
394 */
395 public static final String MEDIA_UNMOUNTABLE = "unmountable";
396
397 /**
Dianne Hackborn407f6252010-10-04 11:31:17 -0700398 * Gets the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800399 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700400 * <p>See {@link #getExternalStorageDirectory()} for more information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 */
402 public static String getExternalStorageState() {
San Mehat7fd0fee2009-12-17 07:12:23 -0800403 try {
Kenny Roote1ff2142010-10-12 11:20:01 -0700404 return MountServiceHolder.mSingleton.getVolumeState(getExternalStorageDirectory()
405 .toString());
San Mehata6a72812010-01-07 22:39:53 -0800406 } catch (Exception rex) {
San Mehat7fd0fee2009-12-17 07:12:23 -0800407 return Environment.MEDIA_REMOVED;
408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410
Dianne Hackborn407f6252010-10-04 11:31:17 -0700411 /**
412 * Returns whether the primary "external" storage device is removable.
413 * If true is returned, this device is for example an SD card that the
414 * user can remove. If false is returned, the storage is built into
415 * the device and can not be physically removed.
416 *
417 * <p>See {@link #getExternalStorageDirectory()} for more information.
418 */
419 public static boolean isExternalStorageRemovable() {
420 return Resources.getSystem().getBoolean(
421 com.android.internal.R.bool.config_externalStorageRemovable);
422 }
423
Kenny Roote1ff2142010-10-12 11:20:01 -0700424 /**
425 * Returns whether the device has an external storage device which is
426 * emulated. If true, the device does not have real external storage
427 * and certain system services such as the package manager use this
428 * to determine where to install an application.
Kenny Roote1ff2142010-10-12 11:20:01 -0700429 */
430 public static boolean isExternalStorageEmulated() {
431 if (mIsExternalStorageEmulated == null) {
432 synchronized (mLock) {
433 if (mIsExternalStorageEmulated == null) {
434 boolean externalStorageEmulated;
435 try {
436 externalStorageEmulated =
437 MountServiceHolder.mSingleton.isExternalStorageEmulated();
438 } catch (Exception e) {
439 externalStorageEmulated = false;
440 }
441 mIsExternalStorageEmulated = Boolean.valueOf(externalStorageEmulated);
442 }
443 }
444 }
445 return mIsExternalStorageEmulated;
446 }
447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 static File getDirectory(String variableName, String defaultPath) {
449 String path = System.getenv(variableName);
450 return path == null ? new File(defaultPath) : new File(path);
451 }
452}