blob: 364004b2bee02e6b4bbf7776bac9a1568486d058 [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
San Mehatb1043402010-02-05 08:26:50 -080019import android.os.storage.IMountService;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070020import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070021import android.os.storage.StorageVolume;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070022import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080023import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080024
Gilles Debunneee1d6302011-05-13 10:09:32 -070025import java.io.File;
26
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027/**
28 * Provides access to environment variables.
29 */
30public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080031 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Jeff Sharkeyb049e212012-09-07 23:16:01 -070033 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
34 private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET";
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070035 private static final String ENV_MEDIA_STORAGE = "MEDIA_STORAGE";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070036
Jeff Sharkeydfa45302012-09-12 16:25:22 -070037 /** {@hide} */
38 public static String DIRECTORY_ANDROID = "Android";
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 private static final File ROOT_DIRECTORY
41 = getDirectory("ANDROID_ROOT", "/system");
42
Oscar Montemayora8529f62009-11-18 10:14:20 -080043 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
44
Jeff Sharkeyb049e212012-09-07 23:16:01 -070045 private static UserEnvironment sCurrentUser;
Kenny Roote1ff2142010-10-12 11:20:01 -070046
Jeff Sharkeyb049e212012-09-07 23:16:01 -070047 private static final Object sLock = new Object();
48
49 // @GuardedBy("sLock")
50 private static volatile StorageVolume sPrimaryVolume;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070051
52 private static StorageVolume getPrimaryVolume() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -070053 if (sPrimaryVolume == null) {
54 synchronized (sLock) {
55 if (sPrimaryVolume == null) {
Mike Lockwood2f6a3882011-05-09 19:08:06 -070056 try {
57 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
58 .getService("mount"));
Jeff Sharkeyb049e212012-09-07 23:16:01 -070059 final StorageVolume[] volumes = mountService.getVolumeList();
60 sPrimaryVolume = StorageManager.getPrimaryVolume(volumes);
Mike Lockwood2f6a3882011-05-09 19:08:06 -070061 } catch (Exception e) {
62 Log.e(TAG, "couldn't talk to MountService", e);
63 }
64 }
65 }
66 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -070067 return sPrimaryVolume;
68 }
69
70 static {
71 initForCurrentUser();
72 }
73
74 /** {@hide} */
75 public static void initForCurrentUser() {
76 final int userId = UserHandle.myUserId();
77 sCurrentUser = new UserEnvironment(userId);
78
79 synchronized (sLock) {
80 sPrimaryVolume = null;
81 }
82 }
83
84 /** {@hide} */
85 public static class UserEnvironment {
86 // TODO: generalize further to create package-specific environment
87
88 private final File mExternalStorage;
89 private final File mExternalStorageAndroidData;
90 private final File mExternalStorageAndroidMedia;
91 private final File mExternalStorageAndroidObb;
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070092 private final File mMediaStorage;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070093
94 public UserEnvironment(int userId) {
95 // See storage config details at http://source.android.com/tech/storage/
96 String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
97 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070098 String rawMediaStorage = System.getenv(ENV_MEDIA_STORAGE);
99 if (TextUtils.isEmpty(rawMediaStorage)) {
100 rawMediaStorage = "/data/media";
101 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700102
103 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
104 // Device has emulated storage; external storage paths should have
105 // userId burned into them.
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700106 final String rawUserId = Integer.toString(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700107 final File emulatedBase = new File(rawEmulatedStorageTarget);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700108 final File mediaBase = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700109
110 // /storage/emulated/0
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700111 mExternalStorage = buildPath(emulatedBase, rawUserId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700112 // /storage/emulated/obb
113 mExternalStorageAndroidObb = buildPath(emulatedBase, "obb");
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700114 // /data/media/0
115 mMediaStorage = buildPath(mediaBase, rawUserId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700116
117 } else {
118 // Device has physical external storage; use plain paths.
119 if (TextUtils.isEmpty(rawExternalStorage)) {
120 Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default");
121 rawExternalStorage = "/storage/sdcard0";
122 }
123
124 // /storage/sdcard0
125 mExternalStorage = new File(rawExternalStorage);
126 // /storage/sdcard0/Android/obb
Jeff Sharkeydfa45302012-09-12 16:25:22 -0700127 mExternalStorageAndroidObb = buildPath(mExternalStorage, DIRECTORY_ANDROID, "obb");
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700128 // /data/media
129 mMediaStorage = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700130 }
131
Jeff Sharkeydfa45302012-09-12 16:25:22 -0700132 mExternalStorageAndroidData = buildPath(mExternalStorage, DIRECTORY_ANDROID, "data");
133 mExternalStorageAndroidMedia = buildPath(mExternalStorage, DIRECTORY_ANDROID, "media");
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700134 }
135
136 public File getExternalStorageDirectory() {
137 return mExternalStorage;
138 }
139
140 public File getExternalStoragePublicDirectory(String type) {
141 return new File(mExternalStorage, type);
142 }
143
144 public File getExternalStorageAndroidDataDir() {
145 return mExternalStorageAndroidData;
146 }
147
148 public File getExternalStorageAppDataDirectory(String packageName) {
149 return new File(mExternalStorageAndroidData, packageName);
150 }
151
152 public File getExternalStorageAppMediaDirectory(String packageName) {
153 return new File(mExternalStorageAndroidMedia, packageName);
154 }
155
156 public File getExternalStorageAppObbDirectory(String packageName) {
157 return new File(mExternalStorageAndroidObb, packageName);
158 }
159
160 public File getExternalStorageAppFilesDirectory(String packageName) {
161 return new File(new File(mExternalStorageAndroidData, packageName), "files");
162 }
163
164 public File getExternalStorageAppCacheDirectory(String packageName) {
165 return new File(new File(mExternalStorageAndroidData, packageName), "cache");
166 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700167
168 public File getMediaStorageDirectory() {
169 return mMediaStorage;
170 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700171 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /**
174 * Gets the Android root directory.
175 */
176 public static File getRootDirectory() {
177 return ROOT_DIRECTORY;
178 }
179
Jason parksa3cdaa52011-01-13 14:15:43 -0600180 /**
181 * Gets the system directory available for secure storage.
182 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
183 * Otherwise, it returns the unencrypted /data/system directory.
184 * @return File object representing the secure storage system directory.
185 * @hide
186 */
187 public static File getSystemSecureDirectory() {
188 if (isEncryptedFilesystemEnabled()) {
189 return new File(SECURE_DATA_DIRECTORY, "system");
190 } else {
191 return new File(DATA_DIRECTORY, "system");
192 }
193 }
194
195 /**
196 * Gets the data directory for secure storage.
197 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
198 * Otherwise, it returns the unencrypted /data directory.
199 * @return File object representing the data directory for secure storage.
200 * @hide
201 */
202 public static File getSecureDataDirectory() {
203 if (isEncryptedFilesystemEnabled()) {
204 return SECURE_DATA_DIRECTORY;
205 } else {
206 return DATA_DIRECTORY;
207 }
208 }
209
210 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700211 * Return directory used for internal media storage, which is protected by
212 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
213 *
214 * @hide
215 */
216 public static File getMediaStorageDirectory() {
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700217 throwIfSystem();
218 return sCurrentUser.getMediaStorageDirectory();
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700219 }
220
221 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700222 * Return the system directory for a user. This is for use by system services to store
223 * files relating to the user. This directory will be automatically deleted when the user
224 * is removed.
225 *
226 * @hide
227 */
228 public static File getUserSystemDirectory(int userId) {
229 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
230 }
231
232 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600233 * Returns whether the Encrypted File System feature is enabled on the device or not.
234 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
235 * if disabled.
236 * @hide
237 */
238 public static boolean isEncryptedFilesystemEnabled() {
239 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
240 }
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 private static final File DATA_DIRECTORY
243 = getDirectory("ANDROID_DATA", "/data");
244
Oscar Montemayora8529f62009-11-18 10:14:20 -0800245 /**
246 * @hide
247 */
248 private static final File SECURE_DATA_DIRECTORY
249 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
250
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700251 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 /**
254 * Gets the Android data directory.
255 */
256 public static File getDataDirectory() {
257 return DATA_DIRECTORY;
258 }
259
260 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800261 * Gets the Android external storage directory. This directory may not
262 * currently be accessible if it has been mounted by the user on their
263 * computer, has been removed from the device, or some other problem has
264 * happened. You can determine its current state with
265 * {@link #getExternalStorageState()}.
266 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700267 * <p><em>Note: don't be confused by the word "external" here. This
268 * directory can better be thought as media/shared storage. It is a
269 * filesystem that can hold a relatively large amount of data and that
270 * is shared across all applications (does not enforce permissions).
271 * Traditionally this is an SD card, but it may also be implemented as
272 * built-in storage in a device that is distinct from the protected
273 * internal storage and can be mounted as a filesystem on a computer.</em></p>
274 *
275 * <p>In devices with multiple "external" storage directories (such as
276 * both secure app storage and mountable shared storage), this directory
277 * represents the "primary" external storage that the user will interact
278 * with.</p>
279 *
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700280 * <p>Applications should not directly use this top-level directory, in
281 * order to avoid polluting the user's root namespace. Any files that are
282 * private to the application should be placed in a directory returned
283 * by {@link android.content.Context#getExternalFilesDir
284 * Context.getExternalFilesDir}, which the system will take care of deleting
285 * if the application is uninstalled. Other shared files should be placed
286 * in one of the directories returned by
287 * {@link #getExternalStoragePublicDirectory}.
288 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800289 * <p>Here is an example of typical code to monitor the state of
290 * external storage:</p>
291 *
292 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
293 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700294 *
295 * @see #getExternalStorageState()
296 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 */
298 public static File getExternalStorageDirectory() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700299 throwIfSystem();
300 return sCurrentUser.getExternalStorageDirectory();
301 }
302
303 /** {@hide} */
304 public static File getLegacyExternalStorageDirectory() {
305 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307
308 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800309 * Standard directory in which to place any audio files that should be
310 * in the regular list of music for the user.
311 * This may be combined with
312 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
313 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
314 * of directories to categories a particular audio file as more than one
315 * type.
316 */
317 public static String DIRECTORY_MUSIC = "Music";
318
319 /**
320 * Standard directory in which to place any audio files that should be
321 * in the list of podcasts that the user can select (not as regular
322 * music).
323 * This may be combined with {@link #DIRECTORY_MUSIC},
324 * {@link #DIRECTORY_NOTIFICATIONS},
325 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
326 * of directories to categories a particular audio file as more than one
327 * type.
328 */
329 public static String DIRECTORY_PODCASTS = "Podcasts";
330
331 /**
332 * Standard directory in which to place any audio files that should be
333 * in the list of ringtones that the user can select (not as regular
334 * music).
335 * This may be combined with {@link #DIRECTORY_MUSIC},
336 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
337 * {@link #DIRECTORY_ALARMS} as a series
338 * of directories to categories a particular audio file as more than one
339 * type.
340 */
341 public static String DIRECTORY_RINGTONES = "Ringtones";
342
343 /**
344 * Standard directory in which to place any audio files that should be
345 * in the list of alarms that the user can select (not as regular
346 * music).
347 * This may be combined with {@link #DIRECTORY_MUSIC},
348 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
349 * and {@link #DIRECTORY_RINGTONES} as a series
350 * of directories to categories a particular audio file as more than one
351 * type.
352 */
353 public static String DIRECTORY_ALARMS = "Alarms";
354
355 /**
356 * Standard directory in which to place any audio files that should be
357 * in the list of notifications that the user can select (not as regular
358 * music).
359 * This may be combined with {@link #DIRECTORY_MUSIC},
360 * {@link #DIRECTORY_PODCASTS},
361 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
362 * of directories to categories a particular audio file as more than one
363 * type.
364 */
365 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
366
367 /**
368 * Standard directory in which to place pictures that are available to
369 * the user. Note that this is primarily a convention for the top-level
370 * public directory, as the media scanner will find and collect pictures
371 * in any directory.
372 */
373 public static String DIRECTORY_PICTURES = "Pictures";
374
375 /**
376 * Standard directory in which to place movies that are available to
377 * the user. Note that this is primarily a convention for the top-level
378 * public directory, as the media scanner will find and collect movies
379 * in any directory.
380 */
381 public static String DIRECTORY_MOVIES = "Movies";
382
383 /**
384 * Standard directory in which to place files that have been downloaded by
385 * the user. Note that this is primarily a convention for the top-level
386 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700387 * private directories. Also note that though the constant here is
388 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
389 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800390 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700391 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800392
393 /**
394 * The traditional location for pictures and videos when mounting the
395 * device as a camera. Note that this is primarily a convention for the
396 * top-level public directory, as this convention makes no sense elsewhere.
397 */
398 public static String DIRECTORY_DCIM = "DCIM";
399
400 /**
401 * Get a top-level public external storage directory for placing files of
402 * a particular type. This is where the user will typically place and
403 * manage their own files, so you should be careful about what you put here
404 * to ensure you don't erase their files or get in the way of their own
405 * organization.
406 *
407 * <p>Here is an example of typical code to manipulate a picture on
408 * the public external storage:</p>
409 *
410 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
411 * public_picture}
412 *
413 * @param type The type of storage directory to return. Should be one of
414 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
415 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
416 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
417 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
418 * {@link #DIRECTORY_DCIM}. May not be null.
419 *
420 * @return Returns the File path for the directory. Note that this
421 * directory may not yet exist, so you must make sure it exists before
422 * using it such as with {@link File#mkdirs File.mkdirs()}.
423 */
424 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700425 throwIfSystem();
426 return sCurrentUser.getExternalStoragePublicDirectory(type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800427 }
428
429 /**
430 * Returns the path for android-specific data on the SD card.
431 * @hide
432 */
433 public static File getExternalStorageAndroidDataDir() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700434 throwIfSystem();
435 return sCurrentUser.getExternalStorageAndroidDataDir();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800436 }
437
438 /**
439 * Generates the raw path to an application's data
440 * @hide
441 */
442 public static File getExternalStorageAppDataDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700443 throwIfSystem();
444 return sCurrentUser.getExternalStorageAppDataDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800445 }
446
447 /**
448 * Generates the raw path to an application's media
449 * @hide
450 */
451 public static File getExternalStorageAppMediaDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700452 throwIfSystem();
453 return sCurrentUser.getExternalStorageAppMediaDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800454 }
455
456 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800457 * Generates the raw path to an application's OBB files
458 * @hide
459 */
460 public static File getExternalStorageAppObbDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700461 throwIfSystem();
462 return sCurrentUser.getExternalStorageAppObbDirectory(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800463 }
464
465 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800466 * Generates the path to an application's files.
467 * @hide
468 */
469 public static File getExternalStorageAppFilesDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700470 throwIfSystem();
471 return sCurrentUser.getExternalStorageAppFilesDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800472 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700473
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800474 /**
475 * Generates the path to an application's cache.
476 * @hide
477 */
478 public static File getExternalStorageAppCacheDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700479 throwIfSystem();
480 return sCurrentUser.getExternalStorageAppCacheDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800481 }
482
483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 * Gets the Android Download/Cache content directory.
485 */
486 public static File getDownloadCacheDirectory() {
487 return DOWNLOAD_CACHE_DIRECTORY;
488 }
489
490 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700491 * {@link #getExternalStorageState()} returns MEDIA_REMOVED if the media is not present.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
493 public static final String MEDIA_REMOVED = "removed";
494
495 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700496 * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 * but not mounted.
498 */
499 public static final String MEDIA_UNMOUNTED = "unmounted";
500
501 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700502 * {@link #getExternalStorageState()} returns MEDIA_CHECKING if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 * and being disk-checked
504 */
505 public static final String MEDIA_CHECKING = "checking";
506
507 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700508 * {@link #getExternalStorageState()} returns MEDIA_NOFS if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 * but is blank or is using an unsupported filesystem
510 */
511 public static final String MEDIA_NOFS = "nofs";
512
513 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700514 * {@link #getExternalStorageState()} returns MEDIA_MOUNTED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 * and mounted at its mount point with read/write access.
516 */
517 public static final String MEDIA_MOUNTED = "mounted";
518
519 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700520 * {@link #getExternalStorageState()} returns MEDIA_MOUNTED_READ_ONLY if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 * and mounted at its mount point with read only access.
522 */
523 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
524
525 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700526 * {@link #getExternalStorageState()} returns MEDIA_SHARED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * not mounted, and shared via USB mass storage.
528 */
529 public static final String MEDIA_SHARED = "shared";
530
531 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700532 * {@link #getExternalStorageState()} returns MEDIA_BAD_REMOVAL if the media was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * removed before it was unmounted.
534 */
535 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
536
537 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700538 * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTABLE if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 * but cannot be mounted. Typically this happens if the file system on the
540 * media is corrupted.
541 */
542 public static final String MEDIA_UNMOUNTABLE = "unmountable";
543
544 /**
Dianne Hackborn407f6252010-10-04 11:31:17 -0700545 * Gets the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800546 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700547 * <p>See {@link #getExternalStorageDirectory()} for more information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 */
549 public static String getExternalStorageState() {
San Mehat7fd0fee2009-12-17 07:12:23 -0800550 try {
Kenny Rootb2278dc2011-01-18 13:03:28 -0800551 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
552 .getService("mount"));
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700553 final StorageVolume primary = getPrimaryVolume();
554 return mountService.getVolumeState(primary.getPath());
555 } catch (RemoteException rex) {
556 Log.w(TAG, "Failed to read external storage state; assuming REMOVED: " + rex);
San Mehat7fd0fee2009-12-17 07:12:23 -0800557 return Environment.MEDIA_REMOVED;
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560
Dianne Hackborn407f6252010-10-04 11:31:17 -0700561 /**
562 * Returns whether the primary "external" storage device is removable.
563 * If true is returned, this device is for example an SD card that the
564 * user can remove. If false is returned, the storage is built into
565 * the device and can not be physically removed.
566 *
567 * <p>See {@link #getExternalStorageDirectory()} for more information.
568 */
569 public static boolean isExternalStorageRemovable() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700570 final StorageVolume primary = getPrimaryVolume();
571 return (primary != null && primary.isRemovable());
Dianne Hackborn407f6252010-10-04 11:31:17 -0700572 }
573
Kenny Roote1ff2142010-10-12 11:20:01 -0700574 /**
575 * Returns whether the device has an external storage device which is
Andy Stadler50c294f2011-03-07 19:13:42 -0800576 * emulated. If true, the device does not have real external storage, and the directory
577 * returned by {@link #getExternalStorageDirectory()} will be allocated using a portion of
578 * the internal storage system.
579 *
580 * <p>Certain system services, such as the package manager, use this
Kenny Roote1ff2142010-10-12 11:20:01 -0700581 * to determine where to install an application.
Andy Stadler50c294f2011-03-07 19:13:42 -0800582 *
583 * <p>Emulated external storage may also be encrypted - see
584 * {@link android.app.admin.DevicePolicyManager#setStorageEncryption(
585 * android.content.ComponentName, boolean)} for additional details.
Kenny Roote1ff2142010-10-12 11:20:01 -0700586 */
587 public static boolean isExternalStorageEmulated() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700588 final StorageVolume primary = getPrimaryVolume();
589 return (primary != null && primary.isEmulated());
Kenny Roote1ff2142010-10-12 11:20:01 -0700590 }
591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 static File getDirectory(String variableName, String defaultPath) {
593 String path = System.getenv(variableName);
594 return path == null ? new File(defaultPath) : new File(path);
595 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700596
597 private static void throwIfSystem() {
598 if (Process.myUid() == Process.SYSTEM_UID) {
599 Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable());
600 }
601 }
602
603 private static File buildPath(File base, String... segments) {
604 File cur = base;
605 for (String segment : segments) {
606 if (cur == null) {
607 cur = new File(segment);
608 } else {
609 cur = new File(cur, segment);
610 }
611 }
612 return cur;
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614}