blob: c6e8c3e5b7be797319fad7130f1c598d1583d0b2 [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
Jeff Sharkey1abdb712013-08-11 16:28:14 -070019import android.content.Context;
San Mehatb1043402010-02-05 08:26:50 -080020import android.os.storage.IMountService;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070021import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070022import android.os.storage.StorageVolume;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070023import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080024import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080025
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080026import com.android.internal.annotations.GuardedBy;
27
Gilles Debunneee1d6302011-05-13 10:09:32 -070028import java.io.File;
Jeff Sharkey63d0a062013-03-01 16:12:55 -080029import java.io.IOException;
Gilles Debunneee1d6302011-05-13 10:09:32 -070030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031/**
32 * Provides access to environment variables.
33 */
34public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080035 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Jeff Sharkeyb049e212012-09-07 23:16:01 -070037 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey4fbbda42012-09-24 18:34:07 -070038 private static final String ENV_EMULATED_STORAGE_SOURCE = "EMULATED_STORAGE_SOURCE";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070039 private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET";
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070040 private static final String ENV_MEDIA_STORAGE = "MEDIA_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080041 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070042
Jeff Sharkeydfa45302012-09-12 16:25:22 -070043 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070044 public static final String DIR_ANDROID = "Android";
45 private static final String DIR_DATA = "data";
46 private static final String DIR_MEDIA = "media";
47 private static final String DIR_OBB = "obb";
48 private static final String DIR_FILES = "files";
49 private static final String DIR_CACHE = "cache";
50
51 /** {@hide} */
52 @Deprecated
53 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070054
Jeff Sharkey63d0a062013-03-01 16:12:55 -080055 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
56 private static final File DIR_MEDIA_STORAGE = getDirectory(ENV_MEDIA_STORAGE, "/data/media");
57
58 private static final String CANONCIAL_EMULATED_STORAGE_TARGET = getCanonicalPathOrNull(
59 ENV_EMULATED_STORAGE_TARGET);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
Oscar Montemayora8529f62009-11-18 10:14:20 -080061 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
62
Jeff Sharkeyb049e212012-09-07 23:16:01 -070063 private static UserEnvironment sCurrentUser;
Jeff Sharkey48749fc2013-04-19 13:25:04 -070064 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070065
Jeff Sharkeyb049e212012-09-07 23:16:01 -070066 private static final Object sLock = new Object();
67
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080068 @GuardedBy("sLock")
Jeff Sharkeyb049e212012-09-07 23:16:01 -070069 private static volatile StorageVolume sPrimaryVolume;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070070
71 private static StorageVolume getPrimaryVolume() {
Dan Morrille4d9a012013-03-28 18:10:43 -070072 if (SystemProperties.getBoolean("config.disable_storage", false)) {
73 return null;
74 }
75
Jeff Sharkeyb049e212012-09-07 23:16:01 -070076 if (sPrimaryVolume == null) {
77 synchronized (sLock) {
78 if (sPrimaryVolume == null) {
Mike Lockwood2f6a3882011-05-09 19:08:06 -070079 try {
80 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
81 .getService("mount"));
Jeff Sharkeyb049e212012-09-07 23:16:01 -070082 final StorageVolume[] volumes = mountService.getVolumeList();
83 sPrimaryVolume = StorageManager.getPrimaryVolume(volumes);
Mike Lockwood2f6a3882011-05-09 19:08:06 -070084 } catch (Exception e) {
85 Log.e(TAG, "couldn't talk to MountService", e);
86 }
87 }
88 }
89 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -070090 return sPrimaryVolume;
91 }
92
93 static {
94 initForCurrentUser();
95 }
96
97 /** {@hide} */
98 public static void initForCurrentUser() {
99 final int userId = UserHandle.myUserId();
100 sCurrentUser = new UserEnvironment(userId);
101
102 synchronized (sLock) {
103 sPrimaryVolume = null;
104 }
105 }
106
107 /** {@hide} */
108 public static class UserEnvironment {
109 // TODO: generalize further to create package-specific environment
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700110 // TODO: add support for secondary external storage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700111
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700112 private final File[] mExternalDirs;
113 private final File mMediaDir;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114
115 public UserEnvironment(int userId) {
116 // See storage config details at http://source.android.com/tech/storage/
117 String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
118 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700119 String rawMediaStorage = System.getenv(ENV_MEDIA_STORAGE);
120 if (TextUtils.isEmpty(rawMediaStorage)) {
121 rawMediaStorage = "/data/media";
122 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700123
124 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
125 // Device has emulated storage; external storage paths should have
126 // userId burned into them.
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700127 final String rawUserId = Integer.toString(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700128 final File emulatedBase = new File(rawEmulatedStorageTarget);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700129 final File mediaBase = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700130
131 // /storage/emulated/0
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700132 mExternalDirs = new File[] { buildPath(emulatedBase, rawUserId) };
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700133 // /data/media/0
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700134 mMediaDir = buildPath(mediaBase, rawUserId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700135
136 } else {
137 // Device has physical external storage; use plain paths.
138 if (TextUtils.isEmpty(rawExternalStorage)) {
139 Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default");
140 rawExternalStorage = "/storage/sdcard0";
141 }
142
143 // /storage/sdcard0
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700144 mExternalDirs = new File[] { new File(rawExternalStorage) };
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700145 // /data/media
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700146 mMediaDir = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700147 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700148 }
149
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700150 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700151 public File getExternalStorageDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700152 return mExternalDirs[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700153 }
154
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700155 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700156 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700157 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700158 }
159
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700160 public File[] getExternalDirs() {
161 return mExternalDirs;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700162 }
163
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700164 public File getMediaDir() {
165 return mMediaDir;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700166 }
167
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700168 public File[] buildExternalStoragePublicDirs(String type) {
169 return buildPaths(mExternalDirs, type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700170 }
171
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700172 public File[] buildExternalStorageAndroidDataDirs() {
173 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700174 }
175
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700176 public File[] buildExternalStorageAndroidObbDirs() {
177 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700178 }
179
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700180 public File[] buildExternalStorageAppDataDirs(String packageName) {
181 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700182 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700183
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700184 public File[] buildExternalStorageAppMediaDirs(String packageName) {
185 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_MEDIA, packageName);
186 }
187
188 public File[] buildExternalStorageAppObbDirs(String packageName) {
189 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_OBB, packageName);
190 }
191
192 public File[] buildExternalStorageAppFilesDirs(String packageName) {
193 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
194 }
195
196 public File[] buildExternalStorageAppCacheDirs(String packageName) {
197 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700198 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700199 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
202 * Gets the Android root directory.
203 */
204 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800205 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 }
207
Jason parksa3cdaa52011-01-13 14:15:43 -0600208 /**
209 * Gets the system directory available for secure storage.
210 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
211 * Otherwise, it returns the unencrypted /data/system directory.
212 * @return File object representing the secure storage system directory.
213 * @hide
214 */
215 public static File getSystemSecureDirectory() {
216 if (isEncryptedFilesystemEnabled()) {
217 return new File(SECURE_DATA_DIRECTORY, "system");
218 } else {
219 return new File(DATA_DIRECTORY, "system");
220 }
221 }
222
223 /**
224 * Gets the data directory for secure storage.
225 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
226 * Otherwise, it returns the unencrypted /data directory.
227 * @return File object representing the data directory for secure storage.
228 * @hide
229 */
230 public static File getSecureDataDirectory() {
231 if (isEncryptedFilesystemEnabled()) {
232 return SECURE_DATA_DIRECTORY;
233 } else {
234 return DATA_DIRECTORY;
235 }
236 }
237
238 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700239 * Return directory used for internal media storage, which is protected by
240 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
241 *
242 * @hide
243 */
244 public static File getMediaStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700245 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700246 return sCurrentUser.getMediaDir();
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700247 }
248
249 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700250 * Return the system directory for a user. This is for use by system services to store
251 * files relating to the user. This directory will be automatically deleted when the user
252 * is removed.
253 *
254 * @hide
255 */
256 public static File getUserSystemDirectory(int userId) {
257 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
258 }
259
260 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600261 * Returns whether the Encrypted File System feature is enabled on the device or not.
262 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
263 * if disabled.
264 * @hide
265 */
266 public static boolean isEncryptedFilesystemEnabled() {
267 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
268 }
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 private static final File DATA_DIRECTORY
271 = getDirectory("ANDROID_DATA", "/data");
272
Oscar Montemayora8529f62009-11-18 10:14:20 -0800273 /**
274 * @hide
275 */
276 private static final File SECURE_DATA_DIRECTORY
277 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
278
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700279 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
281 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700282 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 */
284 public static File getDataDirectory() {
285 return DATA_DIRECTORY;
286 }
287
288 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700289 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800290 * currently be accessible if it has been mounted by the user on their
291 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700292 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800293 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700294 * <p>
295 * <em>Note: don't be confused by the word "external" here. This directory
296 * can better be thought as media/shared storage. It is a filesystem that
297 * can hold a relatively large amount of data and that is shared across all
298 * applications (does not enforce permissions). Traditionally this is an SD
299 * card, but it may also be implemented as built-in storage in a device that
300 * is distinct from the protected internal storage and can be mounted as a
301 * filesystem on a computer.</em>
302 * <p>
303 * On devices with multiple users (as described by {@link UserManager}),
304 * each user has their own isolated external storage. Applications only have
305 * access to the external storage for the user they're running as.
306 * <p>
307 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700308 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700309 * with. Access to secondary storage is available through
310 * <p>
311 * Applications should not directly use this top-level directory, in order
312 * to avoid polluting the user's root namespace. Any files that are private
313 * to the application should be placed in a directory returned by
314 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700315 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700316 * if the application is uninstalled. Other shared files should be placed in
317 * one of the directories returned by
318 * {@link #getExternalStoragePublicDirectory}.
319 * <p>
320 * Writing to this path requires the
321 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
322 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700323 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700324 * which is automatically granted if you hold the write permission.
325 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700326 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700327 * application only needs to store internal data, consider using
328 * {@link Context#getExternalFilesDir(String)} or
329 * {@link Context#getExternalCacheDir()}, which require no permissions to
330 * read or write.
331 * <p>
332 * This path may change between platform versions, so applications should
333 * only persist relative paths.
334 * <p>
335 * Here is an example of typical code to monitor the state of external
336 * storage:
337 * <p>
338 * {@sample
339 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800340 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700341 *
342 * @see #getExternalStorageState()
343 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 */
345 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700346 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700347 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700348 }
349
350 /** {@hide} */
351 public static File getLegacyExternalStorageDirectory() {
352 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
354
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700355 /** {@hide} */
356 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700357 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700358 }
359
360 /** {@hide} */
361 public static File getEmulatedStorageSource(int userId) {
362 // /mnt/shell/emulated/0
363 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), String.valueOf(userId));
364 }
365
366 /** {@hide} */
367 public static File getEmulatedStorageObbSource() {
368 // /mnt/shell/emulated/obb
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700369 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700370 }
371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800373 * Standard directory in which to place any audio files that should be
374 * in the regular list of music for the user.
375 * This may be combined with
376 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
377 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
378 * of directories to categories a particular audio file as more than one
379 * type.
380 */
381 public static String DIRECTORY_MUSIC = "Music";
382
383 /**
384 * Standard directory in which to place any audio files that should be
385 * in the list of podcasts that the user can select (not as regular
386 * music).
387 * This may be combined with {@link #DIRECTORY_MUSIC},
388 * {@link #DIRECTORY_NOTIFICATIONS},
389 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
390 * of directories to categories a particular audio file as more than one
391 * type.
392 */
393 public static String DIRECTORY_PODCASTS = "Podcasts";
394
395 /**
396 * Standard directory in which to place any audio files that should be
397 * in the list of ringtones that the user can select (not as regular
398 * music).
399 * This may be combined with {@link #DIRECTORY_MUSIC},
400 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
401 * {@link #DIRECTORY_ALARMS} as a series
402 * of directories to categories a particular audio file as more than one
403 * type.
404 */
405 public static String DIRECTORY_RINGTONES = "Ringtones";
406
407 /**
408 * Standard directory in which to place any audio files that should be
409 * in the list of alarms that the user can select (not as regular
410 * music).
411 * This may be combined with {@link #DIRECTORY_MUSIC},
412 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
413 * and {@link #DIRECTORY_RINGTONES} as a series
414 * of directories to categories a particular audio file as more than one
415 * type.
416 */
417 public static String DIRECTORY_ALARMS = "Alarms";
418
419 /**
420 * Standard directory in which to place any audio files that should be
421 * in the list of notifications that the user can select (not as regular
422 * music).
423 * This may be combined with {@link #DIRECTORY_MUSIC},
424 * {@link #DIRECTORY_PODCASTS},
425 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
426 * of directories to categories a particular audio file as more than one
427 * type.
428 */
429 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
430
431 /**
432 * Standard directory in which to place pictures that are available to
433 * the user. Note that this is primarily a convention for the top-level
434 * public directory, as the media scanner will find and collect pictures
435 * in any directory.
436 */
437 public static String DIRECTORY_PICTURES = "Pictures";
438
439 /**
440 * Standard directory in which to place movies that are available to
441 * the user. Note that this is primarily a convention for the top-level
442 * public directory, as the media scanner will find and collect movies
443 * in any directory.
444 */
445 public static String DIRECTORY_MOVIES = "Movies";
446
447 /**
448 * Standard directory in which to place files that have been downloaded by
449 * the user. Note that this is primarily a convention for the top-level
450 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700451 * private directories. Also note that though the constant here is
452 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
453 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800454 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700455 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800456
457 /**
458 * The traditional location for pictures and videos when mounting the
459 * device as a camera. Note that this is primarily a convention for the
460 * top-level public directory, as this convention makes no sense elsewhere.
461 */
462 public static String DIRECTORY_DCIM = "DCIM";
463
464 /**
465 * Get a top-level public external storage directory for placing files of
466 * a particular type. This is where the user will typically place and
467 * manage their own files, so you should be careful about what you put here
468 * to ensure you don't erase their files or get in the way of their own
469 * organization.
470 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700471 * <p>On devices with multiple users (as described by {@link UserManager}),
472 * each user has their own isolated external storage. Applications only
473 * have access to the external storage for the user they're running as.</p>
474 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800475 * <p>Here is an example of typical code to manipulate a picture on
476 * the public external storage:</p>
477 *
478 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
479 * public_picture}
480 *
481 * @param type The type of storage directory to return. Should be one of
482 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
483 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
484 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
485 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
486 * {@link #DIRECTORY_DCIM}. May not be null.
487 *
488 * @return Returns the File path for the directory. Note that this
489 * directory may not yet exist, so you must make sure it exists before
490 * using it such as with {@link File#mkdirs File.mkdirs()}.
491 */
492 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700493 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700494 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800495 }
496
497 /**
498 * Returns the path for android-specific data on the SD card.
499 * @hide
500 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700501 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700502 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700503 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800504 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700505
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800506 /**
507 * Generates the raw path to an application's data
508 * @hide
509 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700510 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700511 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700512 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800513 }
514
515 /**
516 * Generates the raw path to an application's media
517 * @hide
518 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700519 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700520 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700521 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800522 }
523
524 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800525 * Generates the raw path to an application's OBB files
526 * @hide
527 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700528 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700529 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700530 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800531 }
532
533 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800534 * Generates the path to an application's files.
535 * @hide
536 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700537 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700538 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700539 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800540 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700541
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800542 /**
543 * Generates the path to an application's cache.
544 * @hide
545 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700546 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700547 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700548 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800549 }
550
551 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700552 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 */
554 public static File getDownloadCacheDirectory() {
555 return DOWNLOAD_CACHE_DIRECTORY;
556 }
557
558 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700559 * Unknown storage state, such as when a path isn't backed by known storage
560 * media.
561 *
562 * @see #getStorageState(File)
563 */
564 public static final String MEDIA_UNKNOWN = "unknown";
565
566 /**
567 * Storage state if the media is not present.
568 *
569 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 */
571 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700574 * Storage state if the media is present but not mounted.
575 *
576 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 */
578 public static final String MEDIA_UNMOUNTED = "unmounted";
579
580 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700581 * Storage state if the media is present and being disk-checked.
582 *
583 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 */
585 public static final String MEDIA_CHECKING = "checking";
586
587 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700588 * Storage state if the media is present but is blank or is using an
589 * unsupported filesystem.
590 *
591 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 */
593 public static final String MEDIA_NOFS = "nofs";
594
595 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700596 * Storage state if the media is present and mounted at its mount point with
597 * read/write access.
598 *
599 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 */
601 public static final String MEDIA_MOUNTED = "mounted";
602
603 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700604 * Storage state if the media is present and mounted at its mount point with
605 * read-only access.
606 *
607 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 */
609 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
610
611 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700612 * Storage state if the media is present not mounted, and shared via USB
613 * mass storage.
614 *
615 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 */
617 public static final String MEDIA_SHARED = "shared";
618
619 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700620 * Storage state if the media was removed before it was unmounted.
621 *
622 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 */
624 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
625
626 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700627 * Storage state if the media is present but cannot be mounted. Typically
628 * this happens if the file system on the media is corrupted.
629 *
630 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 */
632 public static final String MEDIA_UNMOUNTABLE = "unmountable";
633
634 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700635 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800636 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700637 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700638 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
639 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
640 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
641 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
642 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 */
644 public static String getExternalStorageState() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700645 return getStorageState(getExternalStorageDirectory());
646 }
647
648 /**
649 * Returns the current state of the storage device that provides the given
650 * path.
651 *
652 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
653 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
654 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
655 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
656 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
657 */
658 public static String getStorageState(File path) {
659 final String rawPath;
San Mehat7fd0fee2009-12-17 07:12:23 -0800660 try {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700661 rawPath = path.getCanonicalPath();
662 } catch (IOException e) {
663 Log.w(TAG, "Failed to resolve target path: " + e);
664 return Environment.MEDIA_UNKNOWN;
San Mehat7fd0fee2009-12-17 07:12:23 -0800665 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700666
667 try {
668 final IMountService mountService = IMountService.Stub.asInterface(
669 ServiceManager.getService("mount"));
670 final StorageVolume[] volumes = mountService.getVolumeList();
671 for (StorageVolume volume : volumes) {
672 if (rawPath.startsWith(volume.getPath())) {
673 return mountService.getVolumeState(volume.getPath());
674 }
675 }
676 } catch (RemoteException e) {
677 Log.w(TAG, "Failed to find external storage state: " + e);
678 }
679 return Environment.MEDIA_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 }
681
Dianne Hackborn407f6252010-10-04 11:31:17 -0700682 /**
683 * Returns whether the primary "external" storage device is removable.
684 * If true is returned, this device is for example an SD card that the
685 * user can remove. If false is returned, the storage is built into
686 * the device and can not be physically removed.
687 *
688 * <p>See {@link #getExternalStorageDirectory()} for more information.
689 */
690 public static boolean isExternalStorageRemovable() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700691 final StorageVolume primary = getPrimaryVolume();
692 return (primary != null && primary.isRemovable());
Dianne Hackborn407f6252010-10-04 11:31:17 -0700693 }
694
Kenny Roote1ff2142010-10-12 11:20:01 -0700695 /**
696 * Returns whether the device has an external storage device which is
Andy Stadler50c294f2011-03-07 19:13:42 -0800697 * emulated. If true, the device does not have real external storage, and the directory
698 * returned by {@link #getExternalStorageDirectory()} will be allocated using a portion of
699 * the internal storage system.
700 *
701 * <p>Certain system services, such as the package manager, use this
Kenny Roote1ff2142010-10-12 11:20:01 -0700702 * to determine where to install an application.
Andy Stadler50c294f2011-03-07 19:13:42 -0800703 *
704 * <p>Emulated external storage may also be encrypted - see
705 * {@link android.app.admin.DevicePolicyManager#setStorageEncryption(
706 * android.content.ComponentName, boolean)} for additional details.
Kenny Roote1ff2142010-10-12 11:20:01 -0700707 */
708 public static boolean isExternalStorageEmulated() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700709 final StorageVolume primary = getPrimaryVolume();
710 return (primary != null && primary.isEmulated());
Kenny Roote1ff2142010-10-12 11:20:01 -0700711 }
712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 static File getDirectory(String variableName, String defaultPath) {
714 String path = System.getenv(variableName);
715 return path == null ? new File(defaultPath) : new File(path);
716 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700717
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800718 private static String getCanonicalPathOrNull(String variableName) {
719 String path = System.getenv(variableName);
720 if (path == null) {
721 return null;
722 }
723 try {
724 return new File(path).getCanonicalPath();
725 } catch (IOException e) {
726 Log.w(TAG, "Unable to resolve canonical path for " + path);
727 return null;
728 }
729 }
730
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700731 /** {@hide} */
732 public static void setUserRequired(boolean userRequired) {
733 sUserRequired = userRequired;
734 }
735
736 private static void throwIfUserRequired() {
737 if (sUserRequired) {
738 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
739 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700740 }
741 }
742
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700743 /**
744 * Append path segments to each given base path, returning result.
745 *
746 * @hide
747 */
748 public static File[] buildPaths(File[] base, String... segments) {
749 File[] result = new File[base.length];
750 for (int i = 0; i < base.length; i++) {
751 result[i] = buildPath(base[i], segments);
752 }
753 return result;
754 }
755
756 /**
757 * Append path segments to given base path, returning result.
758 *
759 * @hide
760 */
761 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700762 File cur = base;
763 for (String segment : segments) {
764 if (cur == null) {
765 cur = new File(segment);
766 } else {
767 cur = new File(cur, segment);
768 }
769 }
770 return cur;
771 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800772
773 /**
774 * If the given path exists on emulated external storage, return the
775 * translated backing path hosted on internal storage. This bypasses any
776 * emulation later, improving performance. This is <em>only</em> suitable
777 * for read-only access.
778 * <p>
779 * Returns original path if given path doesn't meet these criteria. Callers
780 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
781 * permission.
782 *
783 * @hide
784 */
785 public static File maybeTranslateEmulatedPathToInternal(File path) {
786 // Fast return if not emulated, or missing variables
787 if (!Environment.isExternalStorageEmulated()
788 || CANONCIAL_EMULATED_STORAGE_TARGET == null) {
789 return path;
790 }
791
792 try {
793 final String rawPath = path.getCanonicalPath();
794 if (rawPath.startsWith(CANONCIAL_EMULATED_STORAGE_TARGET)) {
795 final File internalPath = new File(DIR_MEDIA_STORAGE,
796 rawPath.substring(CANONCIAL_EMULATED_STORAGE_TARGET.length()));
797 if (internalPath.exists()) {
798 return internalPath;
799 }
800 }
801 } catch (IOException e) {
802 Log.w(TAG, "Failed to resolve canonical path for " + path);
803 }
804
805 // Unable to translate to internal path; use original
806 return path;
807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808}