blob: 1bada67c4aad48cfe4db1750c0d5b3c67cb0cd44 [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
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080025import com.android.internal.annotations.GuardedBy;
26
Gilles Debunneee1d6302011-05-13 10:09:32 -070027import java.io.File;
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029/**
30 * Provides access to environment variables.
31 */
32public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080033 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Jeff Sharkeyb049e212012-09-07 23:16:01 -070035 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey4fbbda42012-09-24 18:34:07 -070036 private static final String ENV_EMULATED_STORAGE_SOURCE = "EMULATED_STORAGE_SOURCE";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070037 private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET";
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070038 private static final String ENV_MEDIA_STORAGE = "MEDIA_STORAGE";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070039
Jeff Sharkeydfa45302012-09-12 16:25:22 -070040 /** {@hide} */
41 public static String DIRECTORY_ANDROID = "Android";
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 private static final File ROOT_DIRECTORY
44 = getDirectory("ANDROID_ROOT", "/system");
45
Oscar Montemayora8529f62009-11-18 10:14:20 -080046 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
47
Jeff Sharkeyb049e212012-09-07 23:16:01 -070048 private static UserEnvironment sCurrentUser;
Kenny Roote1ff2142010-10-12 11:20:01 -070049
Jeff Sharkeyb049e212012-09-07 23:16:01 -070050 private static final Object sLock = new Object();
51
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080052 @GuardedBy("sLock")
Jeff Sharkeyb049e212012-09-07 23:16:01 -070053 private static volatile StorageVolume sPrimaryVolume;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070054
55 private static StorageVolume getPrimaryVolume() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -070056 if (sPrimaryVolume == null) {
57 synchronized (sLock) {
58 if (sPrimaryVolume == null) {
Mike Lockwood2f6a3882011-05-09 19:08:06 -070059 try {
60 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
61 .getService("mount"));
Jeff Sharkeyb049e212012-09-07 23:16:01 -070062 final StorageVolume[] volumes = mountService.getVolumeList();
63 sPrimaryVolume = StorageManager.getPrimaryVolume(volumes);
Mike Lockwood2f6a3882011-05-09 19:08:06 -070064 } catch (Exception e) {
65 Log.e(TAG, "couldn't talk to MountService", e);
66 }
67 }
68 }
69 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -070070 return sPrimaryVolume;
71 }
72
73 static {
74 initForCurrentUser();
75 }
76
77 /** {@hide} */
78 public static void initForCurrentUser() {
79 final int userId = UserHandle.myUserId();
80 sCurrentUser = new UserEnvironment(userId);
81
82 synchronized (sLock) {
83 sPrimaryVolume = null;
84 }
85 }
86
87 /** {@hide} */
88 public static class UserEnvironment {
89 // TODO: generalize further to create package-specific environment
90
91 private final File mExternalStorage;
92 private final File mExternalStorageAndroidData;
93 private final File mExternalStorageAndroidMedia;
94 private final File mExternalStorageAndroidObb;
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070095 private final File mMediaStorage;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070096
97 public UserEnvironment(int userId) {
98 // See storage config details at http://source.android.com/tech/storage/
99 String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
100 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700101 String rawMediaStorage = System.getenv(ENV_MEDIA_STORAGE);
102 if (TextUtils.isEmpty(rawMediaStorage)) {
103 rawMediaStorage = "/data/media";
104 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700105
106 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
107 // Device has emulated storage; external storage paths should have
108 // userId burned into them.
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700109 final String rawUserId = Integer.toString(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700110 final File emulatedBase = new File(rawEmulatedStorageTarget);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700111 final File mediaBase = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700112
113 // /storage/emulated/0
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700114 mExternalStorage = buildPath(emulatedBase, rawUserId);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700115 // /data/media/0
116 mMediaStorage = buildPath(mediaBase, rawUserId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700117
118 } else {
119 // Device has physical external storage; use plain paths.
120 if (TextUtils.isEmpty(rawExternalStorage)) {
121 Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default");
122 rawExternalStorage = "/storage/sdcard0";
123 }
124
125 // /storage/sdcard0
126 mExternalStorage = new File(rawExternalStorage);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700127 // /data/media
128 mMediaStorage = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700129 }
130
Jeff Sharkey7d8bcb42012-09-24 13:03:35 -0700131 mExternalStorageAndroidObb = buildPath(mExternalStorage, DIRECTORY_ANDROID, "obb");
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
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700140 public File getExternalStorageObbDirectory() {
141 return mExternalStorageAndroidObb;
142 }
143
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700144 public File getExternalStoragePublicDirectory(String type) {
145 return new File(mExternalStorage, type);
146 }
147
148 public File getExternalStorageAndroidDataDir() {
149 return mExternalStorageAndroidData;
150 }
151
152 public File getExternalStorageAppDataDirectory(String packageName) {
153 return new File(mExternalStorageAndroidData, packageName);
154 }
155
156 public File getExternalStorageAppMediaDirectory(String packageName) {
157 return new File(mExternalStorageAndroidMedia, packageName);
158 }
159
160 public File getExternalStorageAppObbDirectory(String packageName) {
161 return new File(mExternalStorageAndroidObb, packageName);
162 }
163
164 public File getExternalStorageAppFilesDirectory(String packageName) {
165 return new File(new File(mExternalStorageAndroidData, packageName), "files");
166 }
167
168 public File getExternalStorageAppCacheDirectory(String packageName) {
169 return new File(new File(mExternalStorageAndroidData, packageName), "cache");
170 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700171
172 public File getMediaStorageDirectory() {
173 return mMediaStorage;
174 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700175 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 /**
178 * Gets the Android root directory.
179 */
180 public static File getRootDirectory() {
181 return ROOT_DIRECTORY;
182 }
183
Jason parksa3cdaa52011-01-13 14:15:43 -0600184 /**
185 * Gets the system directory available for secure storage.
186 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
187 * Otherwise, it returns the unencrypted /data/system directory.
188 * @return File object representing the secure storage system directory.
189 * @hide
190 */
191 public static File getSystemSecureDirectory() {
192 if (isEncryptedFilesystemEnabled()) {
193 return new File(SECURE_DATA_DIRECTORY, "system");
194 } else {
195 return new File(DATA_DIRECTORY, "system");
196 }
197 }
198
199 /**
200 * Gets the data directory for secure storage.
201 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
202 * Otherwise, it returns the unencrypted /data directory.
203 * @return File object representing the data directory for secure storage.
204 * @hide
205 */
206 public static File getSecureDataDirectory() {
207 if (isEncryptedFilesystemEnabled()) {
208 return SECURE_DATA_DIRECTORY;
209 } else {
210 return DATA_DIRECTORY;
211 }
212 }
213
214 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700215 * Return directory used for internal media storage, which is protected by
216 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
217 *
218 * @hide
219 */
220 public static File getMediaStorageDirectory() {
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700221 throwIfSystem();
222 return sCurrentUser.getMediaStorageDirectory();
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700223 }
224
225 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700226 * Return the system directory for a user. This is for use by system services to store
227 * files relating to the user. This directory will be automatically deleted when the user
228 * is removed.
229 *
230 * @hide
231 */
232 public static File getUserSystemDirectory(int userId) {
233 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
234 }
235
236 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600237 * Returns whether the Encrypted File System feature is enabled on the device or not.
238 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
239 * if disabled.
240 * @hide
241 */
242 public static boolean isEncryptedFilesystemEnabled() {
243 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
244 }
245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 private static final File DATA_DIRECTORY
247 = getDirectory("ANDROID_DATA", "/data");
248
Oscar Montemayora8529f62009-11-18 10:14:20 -0800249 /**
250 * @hide
251 */
252 private static final File SECURE_DATA_DIRECTORY
253 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
254
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700255 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
257 /**
258 * Gets the Android data directory.
259 */
260 public static File getDataDirectory() {
261 return DATA_DIRECTORY;
262 }
263
264 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800265 * Gets the Android external storage directory. This directory may not
266 * currently be accessible if it has been mounted by the user on their
267 * computer, has been removed from the device, or some other problem has
268 * happened. You can determine its current state with
269 * {@link #getExternalStorageState()}.
270 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700271 * <p><em>Note: don't be confused by the word "external" here. This
272 * directory can better be thought as media/shared storage. It is a
273 * filesystem that can hold a relatively large amount of data and that
274 * is shared across all applications (does not enforce permissions).
275 * Traditionally this is an SD card, but it may also be implemented as
276 * built-in storage in a device that is distinct from the protected
277 * internal storage and can be mounted as a filesystem on a computer.</em></p>
278 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700279 * <p>On devices with multiple users (as described by {@link UserManager}),
280 * each user has their own isolated external storage. Applications only
281 * have access to the external storage for the user they're running as.</p>
282 *
Dianne Hackborn407f6252010-10-04 11:31:17 -0700283 * <p>In devices with multiple "external" storage directories (such as
284 * both secure app storage and mountable shared storage), this directory
285 * represents the "primary" external storage that the user will interact
286 * with.</p>
287 *
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700288 * <p>Applications should not directly use this top-level directory, in
289 * order to avoid polluting the user's root namespace. Any files that are
290 * private to the application should be placed in a directory returned
291 * by {@link android.content.Context#getExternalFilesDir
292 * Context.getExternalFilesDir}, which the system will take care of deleting
293 * if the application is uninstalled. Other shared files should be placed
294 * in one of the directories returned by
Jeff Sharkey8c165792012-10-22 14:08:29 -0700295 * {@link #getExternalStoragePublicDirectory}.</p>
296 *
297 * <p>Writing to this path requires the
298 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission. In
299 * a future platform release, access to this path will require the
300 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
301 * which is automatically granted if you hold the write permission.</p>
302 *
303 * <p>This path may change between platform versions, so applications
304 * should only persist relative paths.</p>
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700305 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800306 * <p>Here is an example of typical code to monitor the state of
307 * external storage:</p>
308 *
309 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
310 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700311 *
312 * @see #getExternalStorageState()
313 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 */
315 public static File getExternalStorageDirectory() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700316 throwIfSystem();
317 return sCurrentUser.getExternalStorageDirectory();
318 }
319
320 /** {@hide} */
321 public static File getLegacyExternalStorageDirectory() {
322 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
324
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700325 /** {@hide} */
326 public static File getLegacyExternalStorageObbDirectory() {
327 return buildPath(getLegacyExternalStorageDirectory(), DIRECTORY_ANDROID, "obb");
328 }
329
330 /** {@hide} */
331 public static File getEmulatedStorageSource(int userId) {
332 // /mnt/shell/emulated/0
333 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), String.valueOf(userId));
334 }
335
336 /** {@hide} */
337 public static File getEmulatedStorageObbSource() {
338 // /mnt/shell/emulated/obb
339 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), "obb");
340 }
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800343 * Standard directory in which to place any audio files that should be
344 * in the regular list of music for the user.
345 * This may be combined with
346 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
347 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
348 * of directories to categories a particular audio file as more than one
349 * type.
350 */
351 public static String DIRECTORY_MUSIC = "Music";
352
353 /**
354 * Standard directory in which to place any audio files that should be
355 * in the list of podcasts that the user can select (not as regular
356 * music).
357 * This may be combined with {@link #DIRECTORY_MUSIC},
358 * {@link #DIRECTORY_NOTIFICATIONS},
359 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
360 * of directories to categories a particular audio file as more than one
361 * type.
362 */
363 public static String DIRECTORY_PODCASTS = "Podcasts";
364
365 /**
366 * Standard directory in which to place any audio files that should be
367 * in the list of ringtones that the user can select (not as regular
368 * music).
369 * This may be combined with {@link #DIRECTORY_MUSIC},
370 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
371 * {@link #DIRECTORY_ALARMS} as a series
372 * of directories to categories a particular audio file as more than one
373 * type.
374 */
375 public static String DIRECTORY_RINGTONES = "Ringtones";
376
377 /**
378 * Standard directory in which to place any audio files that should be
379 * in the list of alarms that the user can select (not as regular
380 * music).
381 * This may be combined with {@link #DIRECTORY_MUSIC},
382 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
383 * and {@link #DIRECTORY_RINGTONES} as a series
384 * of directories to categories a particular audio file as more than one
385 * type.
386 */
387 public static String DIRECTORY_ALARMS = "Alarms";
388
389 /**
390 * Standard directory in which to place any audio files that should be
391 * in the list of notifications that the user can select (not as regular
392 * music).
393 * This may be combined with {@link #DIRECTORY_MUSIC},
394 * {@link #DIRECTORY_PODCASTS},
395 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
396 * of directories to categories a particular audio file as more than one
397 * type.
398 */
399 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
400
401 /**
402 * Standard directory in which to place pictures that are available to
403 * the user. Note that this is primarily a convention for the top-level
404 * public directory, as the media scanner will find and collect pictures
405 * in any directory.
406 */
407 public static String DIRECTORY_PICTURES = "Pictures";
408
409 /**
410 * Standard directory in which to place movies that are available to
411 * the user. Note that this is primarily a convention for the top-level
412 * public directory, as the media scanner will find and collect movies
413 * in any directory.
414 */
415 public static String DIRECTORY_MOVIES = "Movies";
416
417 /**
418 * Standard directory in which to place files that have been downloaded by
419 * the user. Note that this is primarily a convention for the top-level
420 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700421 * private directories. Also note that though the constant here is
422 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
423 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800424 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700425 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800426
427 /**
428 * The traditional location for pictures and videos when mounting the
429 * device as a camera. Note that this is primarily a convention for the
430 * top-level public directory, as this convention makes no sense elsewhere.
431 */
432 public static String DIRECTORY_DCIM = "DCIM";
433
434 /**
435 * Get a top-level public external storage directory for placing files of
436 * a particular type. This is where the user will typically place and
437 * manage their own files, so you should be careful about what you put here
438 * to ensure you don't erase their files or get in the way of their own
439 * organization.
440 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700441 * <p>On devices with multiple users (as described by {@link UserManager}),
442 * each user has their own isolated external storage. Applications only
443 * have access to the external storage for the user they're running as.</p>
444 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800445 * <p>Here is an example of typical code to manipulate a picture on
446 * the public external storage:</p>
447 *
448 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
449 * public_picture}
450 *
451 * @param type The type of storage directory to return. Should be one of
452 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
453 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
454 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
455 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
456 * {@link #DIRECTORY_DCIM}. May not be null.
457 *
458 * @return Returns the File path for the directory. Note that this
459 * directory may not yet exist, so you must make sure it exists before
460 * using it such as with {@link File#mkdirs File.mkdirs()}.
461 */
462 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700463 throwIfSystem();
464 return sCurrentUser.getExternalStoragePublicDirectory(type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800465 }
466
467 /**
468 * Returns the path for android-specific data on the SD card.
469 * @hide
470 */
471 public static File getExternalStorageAndroidDataDir() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700472 throwIfSystem();
473 return sCurrentUser.getExternalStorageAndroidDataDir();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800474 }
475
476 /**
477 * Generates the raw path to an application's data
478 * @hide
479 */
480 public static File getExternalStorageAppDataDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700481 throwIfSystem();
482 return sCurrentUser.getExternalStorageAppDataDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800483 }
484
485 /**
486 * Generates the raw path to an application's media
487 * @hide
488 */
489 public static File getExternalStorageAppMediaDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700490 throwIfSystem();
491 return sCurrentUser.getExternalStorageAppMediaDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800492 }
493
494 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800495 * Generates the raw path to an application's OBB files
496 * @hide
497 */
498 public static File getExternalStorageAppObbDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700499 throwIfSystem();
500 return sCurrentUser.getExternalStorageAppObbDirectory(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800501 }
502
503 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800504 * Generates the path to an application's files.
505 * @hide
506 */
507 public static File getExternalStorageAppFilesDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700508 throwIfSystem();
509 return sCurrentUser.getExternalStorageAppFilesDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800510 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700511
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800512 /**
513 * Generates the path to an application's cache.
514 * @hide
515 */
516 public static File getExternalStorageAppCacheDirectory(String packageName) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700517 throwIfSystem();
518 return sCurrentUser.getExternalStorageAppCacheDirectory(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800519 }
520
521 /**
Jeff Sharkey8c165792012-10-22 14:08:29 -0700522 * Gets the Android download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 */
524 public static File getDownloadCacheDirectory() {
525 return DOWNLOAD_CACHE_DIRECTORY;
526 }
527
528 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700529 * {@link #getExternalStorageState()} returns MEDIA_REMOVED if the media is not present.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 */
531 public static final String MEDIA_REMOVED = "removed";
532
533 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700534 * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 * but not mounted.
536 */
537 public static final String MEDIA_UNMOUNTED = "unmounted";
538
539 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700540 * {@link #getExternalStorageState()} returns MEDIA_CHECKING if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 * and being disk-checked
542 */
543 public static final String MEDIA_CHECKING = "checking";
544
545 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700546 * {@link #getExternalStorageState()} returns MEDIA_NOFS if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 * but is blank or is using an unsupported filesystem
548 */
549 public static final String MEDIA_NOFS = "nofs";
550
551 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700552 * {@link #getExternalStorageState()} returns MEDIA_MOUNTED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 * and mounted at its mount point with read/write access.
554 */
555 public static final String MEDIA_MOUNTED = "mounted";
556
557 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700558 * {@link #getExternalStorageState()} returns MEDIA_MOUNTED_READ_ONLY if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 * and mounted at its mount point with read only access.
560 */
561 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
562
563 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700564 * {@link #getExternalStorageState()} returns MEDIA_SHARED if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 * not mounted, and shared via USB mass storage.
566 */
567 public static final String MEDIA_SHARED = "shared";
568
569 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700570 * {@link #getExternalStorageState()} returns MEDIA_BAD_REMOVAL if the media was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 * removed before it was unmounted.
572 */
573 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
574
575 /**
Gilles Debunneee1d6302011-05-13 10:09:32 -0700576 * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTABLE if the media is present
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 * but cannot be mounted. Typically this happens if the file system on the
578 * media is corrupted.
579 */
580 public static final String MEDIA_UNMOUNTABLE = "unmountable";
581
582 /**
Dianne Hackborn407f6252010-10-04 11:31:17 -0700583 * Gets the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800584 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700585 * @see #getExternalStorageDirectory()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 */
587 public static String getExternalStorageState() {
San Mehat7fd0fee2009-12-17 07:12:23 -0800588 try {
Kenny Rootb2278dc2011-01-18 13:03:28 -0800589 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
590 .getService("mount"));
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700591 final StorageVolume primary = getPrimaryVolume();
592 return mountService.getVolumeState(primary.getPath());
593 } catch (RemoteException rex) {
594 Log.w(TAG, "Failed to read external storage state; assuming REMOVED: " + rex);
San Mehat7fd0fee2009-12-17 07:12:23 -0800595 return Environment.MEDIA_REMOVED;
596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 }
598
Dianne Hackborn407f6252010-10-04 11:31:17 -0700599 /**
600 * Returns whether the primary "external" storage device is removable.
601 * If true is returned, this device is for example an SD card that the
602 * user can remove. If false is returned, the storage is built into
603 * the device and can not be physically removed.
604 *
605 * <p>See {@link #getExternalStorageDirectory()} for more information.
606 */
607 public static boolean isExternalStorageRemovable() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700608 final StorageVolume primary = getPrimaryVolume();
609 return (primary != null && primary.isRemovable());
Dianne Hackborn407f6252010-10-04 11:31:17 -0700610 }
611
Kenny Roote1ff2142010-10-12 11:20:01 -0700612 /**
613 * Returns whether the device has an external storage device which is
Andy Stadler50c294f2011-03-07 19:13:42 -0800614 * emulated. If true, the device does not have real external storage, and the directory
615 * returned by {@link #getExternalStorageDirectory()} will be allocated using a portion of
616 * the internal storage system.
617 *
618 * <p>Certain system services, such as the package manager, use this
Kenny Roote1ff2142010-10-12 11:20:01 -0700619 * to determine where to install an application.
Andy Stadler50c294f2011-03-07 19:13:42 -0800620 *
621 * <p>Emulated external storage may also be encrypted - see
622 * {@link android.app.admin.DevicePolicyManager#setStorageEncryption(
623 * android.content.ComponentName, boolean)} for additional details.
Kenny Roote1ff2142010-10-12 11:20:01 -0700624 */
625 public static boolean isExternalStorageEmulated() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700626 final StorageVolume primary = getPrimaryVolume();
627 return (primary != null && primary.isEmulated());
Kenny Roote1ff2142010-10-12 11:20:01 -0700628 }
629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 static File getDirectory(String variableName, String defaultPath) {
631 String path = System.getenv(variableName);
632 return path == null ? new File(defaultPath) : new File(path);
633 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700634
635 private static void throwIfSystem() {
636 if (Process.myUid() == Process.SYSTEM_UID) {
637 Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable());
638 }
639 }
640
641 private static File buildPath(File base, String... segments) {
642 File cur = base;
643 for (String segment : segments) {
644 if (cur == null) {
645 cur = new File(segment);
646 } else {
647 cur = new File(cur, segment);
648 }
649 }
650 return cur;
651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652}