blob: 1607b96c0ad98353eb2284af5ae9c0e263f8c2ca [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";
35
Jeff Sharkeydfa45302012-09-12 16:25:22 -070036 /** {@hide} */
37 public static String DIRECTORY_ANDROID = "Android";
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 private static final File ROOT_DIRECTORY
40 = getDirectory("ANDROID_ROOT", "/system");
41
Oscar Montemayora8529f62009-11-18 10:14:20 -080042 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
43
Jeff Sharkeyb049e212012-09-07 23:16:01 -070044 private static UserEnvironment sCurrentUser;
Kenny Roote1ff2142010-10-12 11:20:01 -070045
Jeff Sharkeyb049e212012-09-07 23:16:01 -070046 private static final Object sLock = new Object();
47
48 // @GuardedBy("sLock")
49 private static volatile StorageVolume sPrimaryVolume;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070050
51 private static StorageVolume getPrimaryVolume() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -070052 if (sPrimaryVolume == null) {
53 synchronized (sLock) {
54 if (sPrimaryVolume == null) {
Mike Lockwood2f6a3882011-05-09 19:08:06 -070055 try {
56 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
57 .getService("mount"));
Jeff Sharkeyb049e212012-09-07 23:16:01 -070058 final StorageVolume[] volumes = mountService.getVolumeList();
59 sPrimaryVolume = StorageManager.getPrimaryVolume(volumes);
Mike Lockwood2f6a3882011-05-09 19:08:06 -070060 } catch (Exception e) {
61 Log.e(TAG, "couldn't talk to MountService", e);
62 }
63 }
64 }
65 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -070066 return sPrimaryVolume;
67 }
68
69 static {
70 initForCurrentUser();
71 }
72
73 /** {@hide} */
74 public static void initForCurrentUser() {
75 final int userId = UserHandle.myUserId();
76 sCurrentUser = new UserEnvironment(userId);
77
78 synchronized (sLock) {
79 sPrimaryVolume = null;
80 }
81 }
82
83 /** {@hide} */
84 public static class UserEnvironment {
85 // TODO: generalize further to create package-specific environment
86
87 private final File mExternalStorage;
88 private final File mExternalStorageAndroidData;
89 private final File mExternalStorageAndroidMedia;
90 private final File mExternalStorageAndroidObb;
91
92 public UserEnvironment(int userId) {
93 // See storage config details at http://source.android.com/tech/storage/
94 String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
95 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
96
97 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
98 // Device has emulated storage; external storage paths should have
99 // userId burned into them.
100 final File emulatedBase = new File(rawEmulatedStorageTarget);
101
102 // /storage/emulated/0
103 mExternalStorage = buildPath(emulatedBase, Integer.toString(userId));
104 // /storage/emulated/obb
105 mExternalStorageAndroidObb = buildPath(emulatedBase, "obb");
106
107 } else {
108 // Device has physical external storage; use plain paths.
109 if (TextUtils.isEmpty(rawExternalStorage)) {
110 Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default");
111 rawExternalStorage = "/storage/sdcard0";
112 }
113
114 // /storage/sdcard0
115 mExternalStorage = new File(rawExternalStorage);
116 // /storage/sdcard0/Android/obb
Jeff Sharkeydfa45302012-09-12 16:25:22 -0700117 mExternalStorageAndroidObb = buildPath(mExternalStorage, DIRECTORY_ANDROID, "obb");
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700118 }
119
Jeff Sharkeydfa45302012-09-12 16:25:22 -0700120 mExternalStorageAndroidData = buildPath(mExternalStorage, DIRECTORY_ANDROID, "data");
121 mExternalStorageAndroidMedia = buildPath(mExternalStorage, DIRECTORY_ANDROID, "media");
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700122 }
123
124 public File getExternalStorageDirectory() {
125 return mExternalStorage;
126 }
127
128 public File getExternalStoragePublicDirectory(String type) {
129 return new File(mExternalStorage, type);
130 }
131
132 public File getExternalStorageAndroidDataDir() {
133 return mExternalStorageAndroidData;
134 }
135
136 public File getExternalStorageAppDataDirectory(String packageName) {
137 return new File(mExternalStorageAndroidData, packageName);
138 }
139
140 public File getExternalStorageAppMediaDirectory(String packageName) {
141 return new File(mExternalStorageAndroidMedia, packageName);
142 }
143
144 public File getExternalStorageAppObbDirectory(String packageName) {
145 return new File(mExternalStorageAndroidObb, packageName);
146 }
147
148 public File getExternalStorageAppFilesDirectory(String packageName) {
149 return new File(new File(mExternalStorageAndroidData, packageName), "files");
150 }
151
152 public File getExternalStorageAppCacheDirectory(String packageName) {
153 return new File(new File(mExternalStorageAndroidData, packageName), "cache");
154 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700155 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /**
158 * Gets the Android root directory.
159 */
160 public static File getRootDirectory() {
161 return ROOT_DIRECTORY;
162 }
163
Jason parksa3cdaa52011-01-13 14:15:43 -0600164 /**
165 * Gets the system directory available for secure storage.
166 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
167 * Otherwise, it returns the unencrypted /data/system directory.
168 * @return File object representing the secure storage system directory.
169 * @hide
170 */
171 public static File getSystemSecureDirectory() {
172 if (isEncryptedFilesystemEnabled()) {
173 return new File(SECURE_DATA_DIRECTORY, "system");
174 } else {
175 return new File(DATA_DIRECTORY, "system");
176 }
177 }
178
179 /**
180 * Gets the data directory for secure storage.
181 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
182 * Otherwise, it returns the unencrypted /data directory.
183 * @return File object representing the data directory for secure storage.
184 * @hide
185 */
186 public static File getSecureDataDirectory() {
187 if (isEncryptedFilesystemEnabled()) {
188 return SECURE_DATA_DIRECTORY;
189 } else {
190 return DATA_DIRECTORY;
191 }
192 }
193
194 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700195 * Return directory used for internal media storage, which is protected by
196 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
197 *
198 * @hide
199 */
200 public static File getMediaStorageDirectory() {
201 return MEDIA_STORAGE_DIRECTORY;
202 }
203
204 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700205 * Return the system directory for a user. This is for use by system services to store
206 * files relating to the user. This directory will be automatically deleted when the user
207 * is removed.
208 *
209 * @hide
210 */
211 public static File getUserSystemDirectory(int userId) {
212 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
213 }
214
215 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600216 * Returns whether the Encrypted File System feature is enabled on the device or not.
217 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
218 * if disabled.
219 * @hide
220 */
221 public static boolean isEncryptedFilesystemEnabled() {
222 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
223 }
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 private static final File DATA_DIRECTORY
226 = getDirectory("ANDROID_DATA", "/data");
227
Oscar Montemayora8529f62009-11-18 10:14:20 -0800228 /**
229 * @hide
230 */
231 private static final File SECURE_DATA_DIRECTORY
232 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
233
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700234 /** @hide */
235 private static final File MEDIA_STORAGE_DIRECTORY
236 = getDirectory("MEDIA_STORAGE", "/data/media");
237
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700238 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
240 /**
241 * Gets the Android data directory.
242 */
243 public static File getDataDirectory() {
244 return DATA_DIRECTORY;
245 }
246
247 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800248 * Gets the Android external storage directory. This directory may not
249 * currently be accessible if it has been mounted by the user on their
250 * computer, has been removed from the device, or some other problem has
251 * happened. You can determine its current state with
252 * {@link #getExternalStorageState()}.
253 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700254 * <p><em>Note: don't be confused by the word "external" here. This
255 * directory can better be thought as media/shared storage. It is a
256 * filesystem that can hold a relatively large amount of data and that
257 * is shared across all applications (does not enforce permissions).
258 * Traditionally this is an SD card, but it may also be implemented as
259 * built-in storage in a device that is distinct from the protected
260 * internal storage and can be mounted as a filesystem on a computer.</em></p>
261 *
262 * <p>In devices with multiple "external" storage directories (such as
263 * both secure app storage and mountable shared storage), this directory
264 * represents the "primary" external storage that the user will interact
265 * with.</p>
266 *
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700267 * <p>Applications should not directly use this top-level directory, in
268 * order to avoid polluting the user's root namespace. Any files that are
269 * private to the application should be placed in a directory returned
270 * by {@link android.content.Context#getExternalFilesDir
271 * Context.getExternalFilesDir}, which the system will take care of deleting
272 * if the application is uninstalled. Other shared files should be placed
273 * in one of the directories returned by
274 * {@link #getExternalStoragePublicDirectory}.
275 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800276 * <p>Here is an example of typical code to monitor the state of
277 * external storage:</p>
278 *
279 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
280 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700281 *
282 * @see #getExternalStorageState()
283 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 */
285 public static File getExternalStorageDirectory() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700286 throwIfSystem();
287 return sCurrentUser.getExternalStorageDirectory();
288 }
289
290 /** {@hide} */
291 public static File getLegacyExternalStorageDirectory() {
292 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 }
294
295 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800296 * Standard directory in which to place any audio files that should be
297 * in the regular list of music for the user.
298 * This may be combined with
299 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
300 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
301 * of directories to categories a particular audio file as more than one
302 * type.
303 */
304 public static String DIRECTORY_MUSIC = "Music";
305
306 /**
307 * Standard directory in which to place any audio files that should be
308 * in the list of podcasts that the user can select (not as regular
309 * music).
310 * This may be combined with {@link #DIRECTORY_MUSIC},
311 * {@link #DIRECTORY_NOTIFICATIONS},
312 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
313 * of directories to categories a particular audio file as more than one
314 * type.
315 */
316 public static String DIRECTORY_PODCASTS = "Podcasts";
317
318 /**
319 * Standard directory in which to place any audio files that should be
320 * in the list of ringtones that the user can select (not as regular
321 * music).
322 * This may be combined with {@link #DIRECTORY_MUSIC},
323 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
324 * {@link #DIRECTORY_ALARMS} as a series
325 * of directories to categories a particular audio file as more than one
326 * type.
327 */
328 public static String DIRECTORY_RINGTONES = "Ringtones";
329
330 /**
331 * Standard directory in which to place any audio files that should be
332 * in the list of alarms that the user can select (not as regular
333 * music).
334 * This may be combined with {@link #DIRECTORY_MUSIC},
335 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
336 * and {@link #DIRECTORY_RINGTONES} as a series
337 * of directories to categories a particular audio file as more than one
338 * type.
339 */
340 public static String DIRECTORY_ALARMS = "Alarms";
341
342 /**
343 * Standard directory in which to place any audio files that should be
344 * in the list of notifications that the user can select (not as regular
345 * music).
346 * This may be combined with {@link #DIRECTORY_MUSIC},
347 * {@link #DIRECTORY_PODCASTS},
348 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
349 * of directories to categories a particular audio file as more than one
350 * type.
351 */
352 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
353
354 /**
355 * Standard directory in which to place pictures that are available to
356 * the user. Note that this is primarily a convention for the top-level
357 * public directory, as the media scanner will find and collect pictures
358 * in any directory.
359 */
360 public static String DIRECTORY_PICTURES = "Pictures";
361
362 /**
363 * Standard directory in which to place movies that are available to
364 * the user. Note that this is primarily a convention for the top-level
365 * public directory, as the media scanner will find and collect movies
366 * in any directory.
367 */
368 public static String DIRECTORY_MOVIES = "Movies";
369
370 /**
371 * Standard directory in which to place files that have been downloaded by
372 * the user. Note that this is primarily a convention for the top-level
373 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700374 * private directories. Also note that though the constant here is
375 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
376 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800377 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700378 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800379
380 /**
381 * The traditional location for pictures and videos when mounting the
382 * device as a camera. Note that this is primarily a convention for the
383 * top-level public directory, as this convention makes no sense elsewhere.
384 */
385 public static String DIRECTORY_DCIM = "DCIM";
386
387 /**
388 * Get a top-level public external storage directory for placing files of
389 * a particular type. This is where the user will typically place and
390 * manage their own files, so you should be careful about what you put here
391 * to ensure you don't erase their files or get in the way of their own
392 * organization.
393 *
394 * <p>Here is an example of typical code to manipulate a picture on
395 * the public external storage:</p>
396 *
397 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
398 * public_picture}
399 *
400 * @param type The type of storage directory to return. Should be one of
401 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
402 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
403 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
404 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
405 * {@link #DIRECTORY_DCIM}. May not be null.
406 *
407 * @return Returns the File path for the directory. Note that this
408 * directory may not yet exist, so you must make sure it exists before
409 * using it such as with {@link File#mkdirs File.mkdirs()}.
410 */
411 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700412 throwIfSystem();
413 return sCurrentUser.getExternalStoragePublicDirectory(type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800414 }
415
416 /**
417 * Returns the path for android-specific data on the SD card.
418 * @hide
419 */
420 public static File getExternalStorageAndroidDataDir() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700421 throwIfSystem();
422 return sCurrentUser.getExternalStorageAndroidDataDir();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800423 }
424
425 /**
426 * Generates the raw path to an application's data
427 * @hide
428 */
429 public static File getExternalStorageAppDataDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700430 throwIfSystem();
431 return sCurrentUser.getExternalStorageAppDataDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800432 }
433
434 /**
435 * Generates the raw path to an application's media
436 * @hide
437 */
438 public static File getExternalStorageAppMediaDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700439 throwIfSystem();
440 return sCurrentUser.getExternalStorageAppMediaDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800441 }
442
443 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800444 * Generates the raw path to an application's OBB files
445 * @hide
446 */
447 public static File getExternalStorageAppObbDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700448 throwIfSystem();
449 return sCurrentUser.getExternalStorageAppObbDirectory(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800450 }
451
452 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800453 * Generates the path to an application's files.
454 * @hide
455 */
456 public static File getExternalStorageAppFilesDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700457 throwIfSystem();
458 return sCurrentUser.getExternalStorageAppFilesDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800459 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700460
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800461 /**
462 * Generates the path to an application's cache.
463 * @hide
464 */
465 public static File getExternalStorageAppCacheDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700466 throwIfSystem();
467 return sCurrentUser.getExternalStorageAppCacheDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800468 }
469
470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 * Gets the Android Download/Cache content directory.
472 */
473 public static File getDownloadCacheDirectory() {
474 return DOWNLOAD_CACHE_DIRECTORY;
475 }
476
477 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700478 * {@link #getExternalStorageState()} returns MEDIA_REMOVED if the media is not present.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 */
480 public static final String MEDIA_REMOVED = "removed";
481
482 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700483 * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 * but not mounted.
485 */
486 public static final String MEDIA_UNMOUNTED = "unmounted";
487
488 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700489 * {@link #getExternalStorageState()} returns MEDIA_CHECKING if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 * and being disk-checked
491 */
492 public static final String MEDIA_CHECKING = "checking";
493
494 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700495 * {@link #getExternalStorageState()} returns MEDIA_NOFS if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 * but is blank or is using an unsupported filesystem
497 */
498 public static final String MEDIA_NOFS = "nofs";
499
500 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700501 * {@link #getExternalStorageState()} returns MEDIA_MOUNTED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 * and mounted at its mount point with read/write access.
503 */
504 public static final String MEDIA_MOUNTED = "mounted";
505
506 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700507 * {@link #getExternalStorageState()} returns MEDIA_MOUNTED_READ_ONLY if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 * and mounted at its mount point with read only access.
509 */
510 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
511
512 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700513 * {@link #getExternalStorageState()} returns MEDIA_SHARED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 * not mounted, and shared via USB mass storage.
515 */
516 public static final String MEDIA_SHARED = "shared";
517
518 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700519 * {@link #getExternalStorageState()} returns MEDIA_BAD_REMOVAL if the media was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 * removed before it was unmounted.
521 */
522 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
523
524 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700525 * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTABLE if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * but cannot be mounted. Typically this happens if the file system on the
527 * media is corrupted.
528 */
529 public static final String MEDIA_UNMOUNTABLE = "unmountable";
530
531 /**
Dianne Hackborn407f6252010-10-04 11:31:17 -0700532 * Gets the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800533 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700534 * <p>See {@link #getExternalStorageDirectory()} for more information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
536 public static String getExternalStorageState() {
San Mehat7fd0fee2009-12-17 07:12:23 -0800537 try {
Kenny Rootb2278dc2011-01-18 13:03:28 -0800538 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
539 .getService("mount"));
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700540 final StorageVolume primary = getPrimaryVolume();
541 return mountService.getVolumeState(primary.getPath());
542 } catch (RemoteException rex) {
543 Log.w(TAG, "Failed to read external storage state; assuming REMOVED: " + rex);
San Mehat7fd0fee2009-12-17 07:12:23 -0800544 return Environment.MEDIA_REMOVED;
545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 }
547
Dianne Hackborn407f6252010-10-04 11:31:17 -0700548 /**
549 * Returns whether the primary "external" storage device is removable.
550 * If true is returned, this device is for example an SD card that the
551 * user can remove. If false is returned, the storage is built into
552 * the device and can not be physically removed.
553 *
554 * <p>See {@link #getExternalStorageDirectory()} for more information.
555 */
556 public static boolean isExternalStorageRemovable() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700557 final StorageVolume primary = getPrimaryVolume();
558 return (primary != null && primary.isRemovable());
Dianne Hackborn407f6252010-10-04 11:31:17 -0700559 }
560
Kenny Roote1ff2142010-10-12 11:20:01 -0700561 /**
562 * Returns whether the device has an external storage device which is
Andy Stadler50c294f2011-03-07 19:13:42 -0800563 * emulated. If true, the device does not have real external storage, and the directory
564 * returned by {@link #getExternalStorageDirectory()} will be allocated using a portion of
565 * the internal storage system.
566 *
567 * <p>Certain system services, such as the package manager, use this
Kenny Roote1ff2142010-10-12 11:20:01 -0700568 * to determine where to install an application.
Andy Stadler50c294f2011-03-07 19:13:42 -0800569 *
570 * <p>Emulated external storage may also be encrypted - see
571 * {@link android.app.admin.DevicePolicyManager#setStorageEncryption(
572 * android.content.ComponentName, boolean)} for additional details.
Kenny Roote1ff2142010-10-12 11:20:01 -0700573 */
574 public static boolean isExternalStorageEmulated() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700575 final StorageVolume primary = getPrimaryVolume();
576 return (primary != null && primary.isEmulated());
Kenny Roote1ff2142010-10-12 11:20:01 -0700577 }
578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 static File getDirectory(String variableName, String defaultPath) {
580 String path = System.getenv(variableName);
581 return path == null ? new File(defaultPath) : new File(path);
582 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700583
584 private static void throwIfSystem() {
585 if (Process.myUid() == Process.SYSTEM_UID) {
586 Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable());
587 }
588 }
589
590 private static File buildPath(File base, String... segments) {
591 File cur = base;
592 for (String segment : segments) {
593 if (cur == null) {
594 cur = new File(segment);
595 } else {
596 cur = new File(cur, segment);
597 }
598 }
599 return cur;
600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601}