blob: 2fbcf3fe296905ecd7bc37e479c37ab0d159abdb [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
Dianne Hackborn407f6252010-10-04 11:31:17 -070019import android.content.res.Resources;
San Mehatb1043402010-02-05 08:26:50 -080020import android.os.storage.IMountService;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070021import android.os.storage.StorageVolume;
Kenny Rootb2278dc2011-01-18 13:03:28 -080022import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080023
Gilles Debunneee1d6302011-05-13 10:09:32 -070024import java.io.File;
25
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026/**
27 * Provides access to environment variables.
28 */
29public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080030 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
32 private static final File ROOT_DIRECTORY
33 = getDirectory("ANDROID_ROOT", "/system");
34
Oscar Montemayora8529f62009-11-18 10:14:20 -080035 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
36
Kenny Roote1ff2142010-10-12 11:20:01 -070037 private static final Object mLock = new Object();
38
Mike Lockwood2f6a3882011-05-09 19:08:06 -070039 private volatile static StorageVolume mPrimaryVolume = null;
40
41 private static StorageVolume getPrimaryVolume() {
42 if (mPrimaryVolume == null) {
43 synchronized (mLock) {
44 if (mPrimaryVolume == null) {
45 try {
46 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
47 .getService("mount"));
48 Parcelable[] volumes = mountService.getVolumeList();
49 mPrimaryVolume = (StorageVolume)volumes[0];
50 } catch (Exception e) {
51 Log.e(TAG, "couldn't talk to MountService", e);
52 }
53 }
54 }
55 }
56 return mPrimaryVolume;
57 }
San Mehat7fd0fee2009-12-17 07:12:23 -080058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /**
60 * Gets the Android root directory.
61 */
62 public static File getRootDirectory() {
63 return ROOT_DIRECTORY;
64 }
65
Jason parksa3cdaa52011-01-13 14:15:43 -060066 /**
67 * Gets the system directory available for secure storage.
68 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
69 * Otherwise, it returns the unencrypted /data/system directory.
70 * @return File object representing the secure storage system directory.
71 * @hide
72 */
73 public static File getSystemSecureDirectory() {
74 if (isEncryptedFilesystemEnabled()) {
75 return new File(SECURE_DATA_DIRECTORY, "system");
76 } else {
77 return new File(DATA_DIRECTORY, "system");
78 }
79 }
80
81 /**
82 * Gets the data directory for secure storage.
83 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
84 * Otherwise, it returns the unencrypted /data directory.
85 * @return File object representing the data directory for secure storage.
86 * @hide
87 */
88 public static File getSecureDataDirectory() {
89 if (isEncryptedFilesystemEnabled()) {
90 return SECURE_DATA_DIRECTORY;
91 } else {
92 return DATA_DIRECTORY;
93 }
94 }
95
96 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -070097 * Return directory used for internal media storage, which is protected by
98 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
99 *
100 * @hide
101 */
102 public static File getMediaStorageDirectory() {
103 return MEDIA_STORAGE_DIRECTORY;
104 }
105
106 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700107 * Return the system directory for a user. This is for use by system services to store
108 * files relating to the user. This directory will be automatically deleted when the user
109 * is removed.
110 *
111 * @hide
112 */
113 public static File getUserSystemDirectory(int userId) {
114 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
115 }
116
117 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600118 * Returns whether the Encrypted File System feature is enabled on the device or not.
119 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
120 * if disabled.
121 * @hide
122 */
123 public static boolean isEncryptedFilesystemEnabled() {
124 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
125 }
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 private static final File DATA_DIRECTORY
128 = getDirectory("ANDROID_DATA", "/data");
129
Oscar Montemayora8529f62009-11-18 10:14:20 -0800130 /**
131 * @hide
132 */
133 private static final File SECURE_DATA_DIRECTORY
134 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
135
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700136 /** @hide */
137 private static final File MEDIA_STORAGE_DIRECTORY
138 = getDirectory("MEDIA_STORAGE", "/data/media");
139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 private static final File EXTERNAL_STORAGE_DIRECTORY
Jeff Sharkeye704a152012-04-17 16:28:44 -0700141 = getDirectory("EXTERNAL_STORAGE", "/storage/sdcard0");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Jeff Sharkeye704a152012-04-17 16:28:44 -0700143 private static final File EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY = new File(new File(
144 getDirectory("EXTERNAL_STORAGE", "/storage/sdcard0"), "Android"), "data");
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800145
Jeff Sharkeye704a152012-04-17 16:28:44 -0700146 private static final File EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY = new File(new File(
147 getDirectory("EXTERNAL_STORAGE", "/storage/sdcard0"), "Android"), "media");
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800148
Jeff Sharkeye704a152012-04-17 16:28:44 -0700149 private static final File EXTERNAL_STORAGE_ANDROID_OBB_DIRECTORY = new File(new File(
150 getDirectory("EXTERNAL_STORAGE", "/storage/sdcard0"), "Android"), "obb");
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 private static final File DOWNLOAD_CACHE_DIRECTORY
153 = getDirectory("DOWNLOAD_CACHE", "/cache");
154
155 /**
156 * Gets the Android data directory.
157 */
158 public static File getDataDirectory() {
159 return DATA_DIRECTORY;
160 }
161
162 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800163 * Gets the Android external storage directory. This directory may not
164 * currently be accessible if it has been mounted by the user on their
165 * computer, has been removed from the device, or some other problem has
166 * happened. You can determine its current state with
167 * {@link #getExternalStorageState()}.
168 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700169 * <p><em>Note: don't be confused by the word "external" here. This
170 * directory can better be thought as media/shared storage. It is a
171 * filesystem that can hold a relatively large amount of data and that
172 * is shared across all applications (does not enforce permissions).
173 * Traditionally this is an SD card, but it may also be implemented as
174 * built-in storage in a device that is distinct from the protected
175 * internal storage and can be mounted as a filesystem on a computer.</em></p>
176 *
177 * <p>In devices with multiple "external" storage directories (such as
178 * both secure app storage and mountable shared storage), this directory
179 * represents the "primary" external storage that the user will interact
180 * with.</p>
181 *
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700182 * <p>Applications should not directly use this top-level directory, in
183 * order to avoid polluting the user's root namespace. Any files that are
184 * private to the application should be placed in a directory returned
185 * by {@link android.content.Context#getExternalFilesDir
186 * Context.getExternalFilesDir}, which the system will take care of deleting
187 * if the application is uninstalled. Other shared files should be placed
188 * in one of the directories returned by
189 * {@link #getExternalStoragePublicDirectory}.
190 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800191 * <p>Here is an example of typical code to monitor the state of
192 * external storage:</p>
193 *
194 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
195 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700196 *
197 * @see #getExternalStorageState()
198 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 */
200 public static File getExternalStorageDirectory() {
201 return EXTERNAL_STORAGE_DIRECTORY;
202 }
203
204 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800205 * Standard directory in which to place any audio files that should be
206 * in the regular list of music for the user.
207 * This may be combined with
208 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
209 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
210 * of directories to categories a particular audio file as more than one
211 * type.
212 */
213 public static String DIRECTORY_MUSIC = "Music";
214
215 /**
216 * Standard directory in which to place any audio files that should be
217 * in the list of podcasts that the user can select (not as regular
218 * music).
219 * This may be combined with {@link #DIRECTORY_MUSIC},
220 * {@link #DIRECTORY_NOTIFICATIONS},
221 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
222 * of directories to categories a particular audio file as more than one
223 * type.
224 */
225 public static String DIRECTORY_PODCASTS = "Podcasts";
226
227 /**
228 * Standard directory in which to place any audio files that should be
229 * in the list of ringtones that the user can select (not as regular
230 * music).
231 * This may be combined with {@link #DIRECTORY_MUSIC},
232 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
233 * {@link #DIRECTORY_ALARMS} as a series
234 * of directories to categories a particular audio file as more than one
235 * type.
236 */
237 public static String DIRECTORY_RINGTONES = "Ringtones";
238
239 /**
240 * Standard directory in which to place any audio files that should be
241 * in the list of alarms that the user can select (not as regular
242 * music).
243 * This may be combined with {@link #DIRECTORY_MUSIC},
244 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
245 * and {@link #DIRECTORY_RINGTONES} as a series
246 * of directories to categories a particular audio file as more than one
247 * type.
248 */
249 public static String DIRECTORY_ALARMS = "Alarms";
250
251 /**
252 * Standard directory in which to place any audio files that should be
253 * in the list of notifications that the user can select (not as regular
254 * music).
255 * This may be combined with {@link #DIRECTORY_MUSIC},
256 * {@link #DIRECTORY_PODCASTS},
257 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
258 * of directories to categories a particular audio file as more than one
259 * type.
260 */
261 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
262
263 /**
264 * Standard directory in which to place pictures that are available to
265 * the user. Note that this is primarily a convention for the top-level
266 * public directory, as the media scanner will find and collect pictures
267 * in any directory.
268 */
269 public static String DIRECTORY_PICTURES = "Pictures";
270
271 /**
272 * Standard directory in which to place movies that are available to
273 * the user. Note that this is primarily a convention for the top-level
274 * public directory, as the media scanner will find and collect movies
275 * in any directory.
276 */
277 public static String DIRECTORY_MOVIES = "Movies";
278
279 /**
280 * Standard directory in which to place files that have been downloaded by
281 * the user. Note that this is primarily a convention for the top-level
282 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700283 * private directories. Also note that though the constant here is
284 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
285 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800286 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700287 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800288
289 /**
290 * The traditional location for pictures and videos when mounting the
291 * device as a camera. Note that this is primarily a convention for the
292 * top-level public directory, as this convention makes no sense elsewhere.
293 */
294 public static String DIRECTORY_DCIM = "DCIM";
295
296 /**
297 * Get a top-level public external storage directory for placing files of
298 * a particular type. This is where the user will typically place and
299 * manage their own files, so you should be careful about what you put here
300 * to ensure you don't erase their files or get in the way of their own
301 * organization.
302 *
303 * <p>Here is an example of typical code to manipulate a picture on
304 * the public external storage:</p>
305 *
306 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
307 * public_picture}
308 *
309 * @param type The type of storage directory to return. Should be one of
310 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
311 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
312 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
313 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
314 * {@link #DIRECTORY_DCIM}. May not be null.
315 *
316 * @return Returns the File path for the directory. Note that this
317 * directory may not yet exist, so you must make sure it exists before
318 * using it such as with {@link File#mkdirs File.mkdirs()}.
319 */
320 public static File getExternalStoragePublicDirectory(String type) {
321 return new File(getExternalStorageDirectory(), type);
322 }
323
324 /**
325 * Returns the path for android-specific data on the SD card.
326 * @hide
327 */
328 public static File getExternalStorageAndroidDataDir() {
329 return EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY;
330 }
331
332 /**
333 * Generates the raw path to an application's data
334 * @hide
335 */
336 public static File getExternalStorageAppDataDirectory(String packageName) {
337 return new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName);
338 }
339
340 /**
341 * Generates the raw path to an application's media
342 * @hide
343 */
344 public static File getExternalStorageAppMediaDirectory(String packageName) {
345 return new File(EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY, packageName);
346 }
347
348 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800349 * Generates the raw path to an application's OBB files
350 * @hide
351 */
352 public static File getExternalStorageAppObbDirectory(String packageName) {
353 return new File(EXTERNAL_STORAGE_ANDROID_OBB_DIRECTORY, packageName);
354 }
355
356 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800357 * Generates the path to an application's files.
358 * @hide
359 */
360 public static File getExternalStorageAppFilesDirectory(String packageName) {
361 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
362 packageName), "files");
363 }
364
365 /**
366 * Generates the path to an application's cache.
367 * @hide
368 */
369 public static File getExternalStorageAppCacheDirectory(String packageName) {
370 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
371 packageName), "cache");
372 }
373
374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 * Gets the Android Download/Cache content directory.
376 */
377 public static File getDownloadCacheDirectory() {
378 return DOWNLOAD_CACHE_DIRECTORY;
379 }
380
381 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700382 * {@link #getExternalStorageState()} returns MEDIA_REMOVED if the media is not present.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 */
384 public static final String MEDIA_REMOVED = "removed";
385
386 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700387 * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * but not mounted.
389 */
390 public static final String MEDIA_UNMOUNTED = "unmounted";
391
392 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700393 * {@link #getExternalStorageState()} returns MEDIA_CHECKING if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 * and being disk-checked
395 */
396 public static final String MEDIA_CHECKING = "checking";
397
398 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700399 * {@link #getExternalStorageState()} returns MEDIA_NOFS if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * but is blank or is using an unsupported filesystem
401 */
402 public static final String MEDIA_NOFS = "nofs";
403
404 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700405 * {@link #getExternalStorageState()} returns MEDIA_MOUNTED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 * and mounted at its mount point with read/write access.
407 */
408 public static final String MEDIA_MOUNTED = "mounted";
409
410 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700411 * {@link #getExternalStorageState()} returns MEDIA_MOUNTED_READ_ONLY if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 * and mounted at its mount point with read only access.
413 */
414 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
415
416 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700417 * {@link #getExternalStorageState()} returns MEDIA_SHARED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 * not mounted, and shared via USB mass storage.
419 */
420 public static final String MEDIA_SHARED = "shared";
421
422 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700423 * {@link #getExternalStorageState()} returns MEDIA_BAD_REMOVAL if the media was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * removed before it was unmounted.
425 */
426 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
427
428 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700429 * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTABLE if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * but cannot be mounted. Typically this happens if the file system on the
431 * media is corrupted.
432 */
433 public static final String MEDIA_UNMOUNTABLE = "unmountable";
434
435 /**
Dianne Hackborn407f6252010-10-04 11:31:17 -0700436 * Gets the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800437 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700438 * <p>See {@link #getExternalStorageDirectory()} for more information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 */
440 public static String getExternalStorageState() {
San Mehat7fd0fee2009-12-17 07:12:23 -0800441 try {
Kenny Rootb2278dc2011-01-18 13:03:28 -0800442 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
443 .getService("mount"));
444 return mountService.getVolumeState(getExternalStorageDirectory()
Kenny Roote1ff2142010-10-12 11:20:01 -0700445 .toString());
San Mehata6a72812010-01-07 22:39:53 -0800446 } catch (Exception rex) {
San Mehat7fd0fee2009-12-17 07:12:23 -0800447 return Environment.MEDIA_REMOVED;
448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450
Dianne Hackborn407f6252010-10-04 11:31:17 -0700451 /**
452 * Returns whether the primary "external" storage device is removable.
453 * If true is returned, this device is for example an SD card that the
454 * user can remove. If false is returned, the storage is built into
455 * the device and can not be physically removed.
456 *
457 * <p>See {@link #getExternalStorageDirectory()} for more information.
458 */
459 public static boolean isExternalStorageRemovable() {
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700460 StorageVolume volume = getPrimaryVolume();
461 return (volume != null && volume.isRemovable());
Dianne Hackborn407f6252010-10-04 11:31:17 -0700462 }
463
Kenny Roote1ff2142010-10-12 11:20:01 -0700464 /**
465 * Returns whether the device has an external storage device which is
Andy Stadler50c294f2011-03-07 19:13:42 -0800466 * emulated. If true, the device does not have real external storage, and the directory
467 * returned by {@link #getExternalStorageDirectory()} will be allocated using a portion of
468 * the internal storage system.
469 *
470 * <p>Certain system services, such as the package manager, use this
Kenny Roote1ff2142010-10-12 11:20:01 -0700471 * to determine where to install an application.
Andy Stadler50c294f2011-03-07 19:13:42 -0800472 *
473 * <p>Emulated external storage may also be encrypted - see
474 * {@link android.app.admin.DevicePolicyManager#setStorageEncryption(
475 * android.content.ComponentName, boolean)} for additional details.
Kenny Roote1ff2142010-10-12 11:20:01 -0700476 */
477 public static boolean isExternalStorageEmulated() {
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700478 StorageVolume volume = getPrimaryVolume();
479 return (volume != null && volume.isEmulated());
Kenny Roote1ff2142010-10-12 11:20:01 -0700480 }
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 static File getDirectory(String variableName, String defaultPath) {
483 String path = System.getenv(variableName);
484 return path == null ? new File(defaultPath) : new File(path);
485 }
486}