blob: 2eb97f14fc818e4696ac70f4b043de8bffbfc0e0 [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 Sharkey4ca728c2014-01-10 16:27:19 -080019import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070020import android.content.Context;
Jeff Sharkey48877892015-03-18 11:27:19 -070021import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070022import android.os.storage.StorageVolume;
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";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080034 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070035 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
36 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080037 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070038 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070039
Jeff Sharkeydfa45302012-09-12 16:25:22 -070040 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070041 public static final String DIR_ANDROID = "Android";
42 private static final String DIR_DATA = "data";
43 private static final String DIR_MEDIA = "media";
44 private static final String DIR_OBB = "obb";
45 private static final String DIR_FILES = "files";
46 private static final String DIR_CACHE = "cache";
47
48 /** {@hide} */
49 @Deprecated
50 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070051
Jeff Sharkey63d0a062013-03-01 16:12:55 -080052 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070053 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
54 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080055 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Christopher Tate740888f2014-04-18 12:24:57 -070056 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Oscar Montemayora8529f62009-11-18 10:14:20 -080058 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
59
Jeff Sharkeyb049e212012-09-07 23:16:01 -070060 private static UserEnvironment sCurrentUser;
Jeff Sharkey48749fc2013-04-19 13:25:04 -070061 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070062
Jeff Sharkeyb049e212012-09-07 23:16:01 -070063 static {
64 initForCurrentUser();
65 }
66
67 /** {@hide} */
68 public static void initForCurrentUser() {
69 final int userId = UserHandle.myUserId();
70 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070071 }
72
73 /** {@hide} */
74 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070075 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070076
77 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070078 mUserId = userId;
79 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070080
Jeff Sharkey48877892015-03-18 11:27:19 -070081 public File[] getExternalDirs() {
82 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070083 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -070084 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070085 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070086 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070087 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070088 }
89
Jeff Sharkey1abdb712013-08-11 16:28:14 -070090 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -070091 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -070092 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -070093 }
94
Jeff Sharkey1abdb712013-08-11 16:28:14 -070095 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -070096 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -070097 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -070098 }
99
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700100 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700101 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700102 }
103
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700104 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700105 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700106 }
107
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700108 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700109 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700110 }
111
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700112 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700113 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700115
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700116 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700117 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700118 }
119
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700120 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700121 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700122 }
123
124 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700125 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700126 }
127
128 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700129 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700130 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700131 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800134 * Return root of the "system" partition holding the core Android OS.
135 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 */
137 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800138 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 }
140
Jeff Sharkey48877892015-03-18 11:27:19 -0700141 /** {@hide} */
142 public static File getStorageDirectory() {
143 return DIR_ANDROID_STORAGE;
144 }
145
Jason parksa3cdaa52011-01-13 14:15:43 -0600146 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800147 * Return root directory of the "oem" partition holding OEM customizations,
148 * if any. If present, the partition is mounted read-only.
149 *
150 * @hide
151 */
152 public static File getOemDirectory() {
153 return DIR_OEM_ROOT;
154 }
155
156 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700157 * Return root directory of the "vendor" partition that holds vendor-provided
158 * software that should persist across simple reflashing of the "system" partition.
159 * @hide
160 */
161 public static File getVendorDirectory() {
162 return DIR_VENDOR_ROOT;
163 }
164
165 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600166 * Gets the system directory available for secure storage.
167 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
168 * Otherwise, it returns the unencrypted /data/system directory.
169 * @return File object representing the secure storage system directory.
170 * @hide
171 */
172 public static File getSystemSecureDirectory() {
173 if (isEncryptedFilesystemEnabled()) {
174 return new File(SECURE_DATA_DIRECTORY, "system");
175 } else {
176 return new File(DATA_DIRECTORY, "system");
177 }
178 }
179
180 /**
181 * Gets the data directory for secure storage.
182 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
183 * Otherwise, it returns the unencrypted /data directory.
184 * @return File object representing the data directory for secure storage.
185 * @hide
186 */
187 public static File getSecureDataDirectory() {
188 if (isEncryptedFilesystemEnabled()) {
189 return SECURE_DATA_DIRECTORY;
190 } else {
191 return DATA_DIRECTORY;
192 }
193 }
194
195 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700196 * Return the system directory for a user. This is for use by system services to store
197 * files relating to the user. This directory will be automatically deleted when the user
198 * is removed.
199 *
200 * @hide
201 */
202 public static File getUserSystemDirectory(int userId) {
203 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
204 }
205
206 /**
Robin Lee69591332014-04-28 16:03:22 +0100207 * Returns the config directory for a user. This is for use by system services to store files
208 * relating to the user which should be readable by any app running as that user.
209 *
210 * @hide
211 */
212 public static File getUserConfigDirectory(int userId) {
213 return new File(new File(new File(
214 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
215 }
216
217 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600218 * Returns whether the Encrypted File System feature is enabled on the device or not.
219 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
220 * if disabled.
221 * @hide
222 */
223 public static boolean isEncryptedFilesystemEnabled() {
224 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
225 }
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 private static final File DATA_DIRECTORY
228 = getDirectory("ANDROID_DATA", "/data");
229
Oscar Montemayora8529f62009-11-18 10:14:20 -0800230 /**
231 * @hide
232 */
233 private static final File SECURE_DATA_DIRECTORY
234 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
235
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700236 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237
238 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700239 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 */
241 public static File getDataDirectory() {
242 return DATA_DIRECTORY;
243 }
244
245 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700246 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800247 * currently be accessible if it has been mounted by the user on their
248 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700249 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800250 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700251 * <p>
252 * <em>Note: don't be confused by the word "external" here. This directory
253 * can better be thought as media/shared storage. It is a filesystem that
254 * can hold a relatively large amount of data and that is shared across all
255 * applications (does not enforce permissions). Traditionally this is an SD
256 * card, but it may also be implemented as built-in storage in a device that
257 * is distinct from the protected internal storage and can be mounted as a
258 * filesystem on a computer.</em>
259 * <p>
260 * On devices with multiple users (as described by {@link UserManager}),
261 * each user has their own isolated external storage. Applications only have
262 * access to the external storage for the user they're running as.
263 * <p>
264 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700265 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700266 * with. Access to secondary storage is available through
267 * <p>
268 * Applications should not directly use this top-level directory, in order
269 * to avoid polluting the user's root namespace. Any files that are private
270 * to the application should be placed in a directory returned by
271 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700272 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700273 * if the application is uninstalled. Other shared files should be placed in
274 * one of the directories returned by
275 * {@link #getExternalStoragePublicDirectory}.
276 * <p>
277 * Writing to this path requires the
278 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
279 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700280 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700281 * which is automatically granted if you hold the write permission.
282 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700283 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700284 * application only needs to store internal data, consider using
285 * {@link Context#getExternalFilesDir(String)} or
286 * {@link Context#getExternalCacheDir()}, which require no permissions to
287 * read or write.
288 * <p>
289 * This path may change between platform versions, so applications should
290 * only persist relative paths.
291 * <p>
292 * Here is an example of typical code to monitor the state of external
293 * storage:
294 * <p>
295 * {@sample
296 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800297 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700298 *
299 * @see #getExternalStorageState()
300 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 */
302 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700303 throwIfUserRequired();
Jeff Sharkey48877892015-03-18 11:27:19 -0700304 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700305 }
306
307 /** {@hide} */
308 public static File getLegacyExternalStorageDirectory() {
309 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
311
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700312 /** {@hide} */
313 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700314 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700315 }
316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800318 * Standard directory in which to place any audio files that should be
319 * in the regular list of music for the user.
320 * This may be combined with
321 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
322 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
323 * of directories to categories a particular audio file as more than one
324 * type.
325 */
326 public static String DIRECTORY_MUSIC = "Music";
327
328 /**
329 * Standard directory in which to place any audio files that should be
330 * in the list of podcasts that the user can select (not as regular
331 * music).
332 * This may be combined with {@link #DIRECTORY_MUSIC},
333 * {@link #DIRECTORY_NOTIFICATIONS},
334 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
335 * of directories to categories a particular audio file as more than one
336 * type.
337 */
338 public static String DIRECTORY_PODCASTS = "Podcasts";
339
340 /**
341 * Standard directory in which to place any audio files that should be
342 * in the list of ringtones that the user can select (not as regular
343 * music).
344 * This may be combined with {@link #DIRECTORY_MUSIC},
345 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
346 * {@link #DIRECTORY_ALARMS} as a series
347 * of directories to categories a particular audio file as more than one
348 * type.
349 */
350 public static String DIRECTORY_RINGTONES = "Ringtones";
351
352 /**
353 * Standard directory in which to place any audio files that should be
354 * in the list of alarms that the user can select (not as regular
355 * music).
356 * This may be combined with {@link #DIRECTORY_MUSIC},
357 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
358 * and {@link #DIRECTORY_RINGTONES} as a series
359 * of directories to categories a particular audio file as more than one
360 * type.
361 */
362 public static String DIRECTORY_ALARMS = "Alarms";
363
364 /**
365 * Standard directory in which to place any audio files that should be
366 * in the list of notifications that the user can select (not as regular
367 * music).
368 * This may be combined with {@link #DIRECTORY_MUSIC},
369 * {@link #DIRECTORY_PODCASTS},
370 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
371 * of directories to categories a particular audio file as more than one
372 * type.
373 */
374 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
375
376 /**
377 * Standard directory in which to place pictures that are available to
378 * the user. Note that this is primarily a convention for the top-level
379 * public directory, as the media scanner will find and collect pictures
380 * in any directory.
381 */
382 public static String DIRECTORY_PICTURES = "Pictures";
383
384 /**
385 * Standard directory in which to place movies that are available to
386 * the user. Note that this is primarily a convention for the top-level
387 * public directory, as the media scanner will find and collect movies
388 * in any directory.
389 */
390 public static String DIRECTORY_MOVIES = "Movies";
391
392 /**
393 * Standard directory in which to place files that have been downloaded by
394 * the user. Note that this is primarily a convention for the top-level
395 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700396 * private directories. Also note that though the constant here is
397 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
398 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800399 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700400 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800401
402 /**
403 * The traditional location for pictures and videos when mounting the
404 * device as a camera. Note that this is primarily a convention for the
405 * top-level public directory, as this convention makes no sense elsewhere.
406 */
407 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700408
409 /**
410 * Standard directory in which to place documents that have been created by
411 * the user.
412 */
413 public static String DIRECTORY_DOCUMENTS = "Documents";
414
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800415 /**
416 * Get a top-level public external storage directory for placing files of
417 * a particular type. This is where the user will typically place and
418 * manage their own files, so you should be careful about what you put here
419 * to ensure you don't erase their files or get in the way of their own
420 * organization.
421 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700422 * <p>On devices with multiple users (as described by {@link UserManager}),
423 * each user has their own isolated external storage. Applications only
424 * have access to the external storage for the user they're running as.</p>
425 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800426 * <p>Here is an example of typical code to manipulate a picture on
427 * the public external storage:</p>
428 *
429 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
430 * public_picture}
431 *
432 * @param type The type of storage directory to return. Should be one of
433 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
434 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
435 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
436 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
437 * {@link #DIRECTORY_DCIM}. May not be null.
438 *
439 * @return Returns the File path for the directory. Note that this
440 * directory may not yet exist, so you must make sure it exists before
441 * using it such as with {@link File#mkdirs File.mkdirs()}.
442 */
443 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700444 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700445 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800446 }
447
448 /**
449 * Returns the path for android-specific data on the SD card.
450 * @hide
451 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700452 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700453 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700454 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800455 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700456
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800457 /**
458 * Generates the raw path to an application's data
459 * @hide
460 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700461 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700462 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700463 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800464 }
465
466 /**
467 * Generates the raw path to an application's media
468 * @hide
469 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700470 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700471 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700472 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800473 }
474
475 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800476 * Generates the raw path to an application's OBB files
477 * @hide
478 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700479 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700480 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700481 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800482 }
483
484 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800485 * Generates the path to an application's files.
486 * @hide
487 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700488 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700489 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700490 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800491 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700492
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800493 /**
494 * Generates the path to an application's cache.
495 * @hide
496 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700497 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700498 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700499 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800500 }
501
502 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700503 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 */
505 public static File getDownloadCacheDirectory() {
506 return DOWNLOAD_CACHE_DIRECTORY;
507 }
508
509 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700510 * Unknown storage state, such as when a path isn't backed by known storage
511 * media.
512 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800513 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700514 */
515 public static final String MEDIA_UNKNOWN = "unknown";
516
517 /**
518 * Storage state if the media is not present.
519 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800520 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 */
522 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700525 * Storage state if the media is present but not mounted.
526 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800527 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 */
529 public static final String MEDIA_UNMOUNTED = "unmounted";
530
531 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700532 * Storage state if the media is present and being disk-checked.
533 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800534 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
536 public static final String MEDIA_CHECKING = "checking";
537
538 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700539 * Storage state if the media is present but is blank or is using an
540 * unsupported filesystem.
541 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800542 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 */
544 public static final String MEDIA_NOFS = "nofs";
545
546 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700547 * Storage state if the media is present and mounted at its mount point with
548 * read/write access.
549 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800550 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 */
552 public static final String MEDIA_MOUNTED = "mounted";
553
554 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700555 * Storage state if the media is present and mounted at its mount point with
556 * read-only access.
557 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800558 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 */
560 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
561
562 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700563 * Storage state if the media is present not mounted, and shared via USB
564 * mass storage.
565 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800566 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 */
568 public static final String MEDIA_SHARED = "shared";
569
570 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700571 * Storage state if the media was removed before it was unmounted.
572 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800573 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 */
575 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
576
577 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700578 * Storage state if the media is present but cannot be mounted. Typically
579 * this happens if the file system on the media is corrupted.
580 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800581 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 */
583 public static final String MEDIA_UNMOUNTABLE = "unmountable";
584
585 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700586 * Storage state if the media is in the process of being ejected.
587 *
588 * @see #getExternalStorageState(File)
589 */
590 public static final String MEDIA_EJECTING = "ejecting";
591
592 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700593 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800594 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700595 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700596 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
597 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
598 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
599 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
600 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 */
602 public static String getExternalStorageState() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700603 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800604 return getExternalStorageState(externalDir);
605 }
606
607 /**
608 * @deprecated use {@link #getExternalStorageState(File)}
609 */
610 @Deprecated
611 public static String getStorageState(File path) {
612 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700613 }
614
615 /**
616 * Returns the current state of the storage device that provides the given
617 * path.
618 *
619 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
620 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
621 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
622 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
623 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
624 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800625 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700626 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800627 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700628 return volume.getState();
629 } else {
630 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633
Dianne Hackborn407f6252010-10-04 11:31:17 -0700634 /**
635 * Returns whether the primary "external" storage device is removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700636 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800637 * @return true if the storage device can be removed (such as an SD card),
638 * or false if the storage device is built in and cannot be
639 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700640 */
641 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800642 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700643 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800644 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700645 }
646
Kenny Roote1ff2142010-10-12 11:20:01 -0700647 /**
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800648 * Returns whether the storage device that provides the given path is
649 * removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800650 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800651 * @return true if the storage device can be removed (such as an SD card),
652 * or false if the storage device is built in and cannot be
653 * physically removed.
654 * @throws IllegalArgumentException if the path is not a valid storage
655 * device.
656 */
657 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700658 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800659 if (volume != null) {
660 return volume.isRemovable();
661 } else {
662 throw new IllegalArgumentException("Failed to find storage device at " + path);
663 }
664 }
665
666 /**
667 * Returns whether the primary "external" storage device is emulated. If
668 * true, data stored on this device will be stored on a portion of the
669 * internal storage system.
Andy Stadler50c294f2011-03-07 19:13:42 -0800670 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800671 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
672 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700673 */
674 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800675 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700676 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800677 return isExternalStorageEmulated(externalDir);
678 }
679
680 /**
681 * Returns whether the storage device that provides the given path is
682 * emulated. If true, data stored on this device will be stored on a portion
683 * of the internal storage system.
684 *
685 * @throws IllegalArgumentException if the path is not a valid storage
686 * device.
687 */
688 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700689 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800690 if (volume != null) {
691 return volume.isEmulated();
692 } else {
693 throw new IllegalArgumentException("Failed to find storage device at " + path);
694 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700695 }
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 static File getDirectory(String variableName, String defaultPath) {
698 String path = System.getenv(variableName);
699 return path == null ? new File(defaultPath) : new File(path);
700 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700701
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700702 /** {@hide} */
703 public static void setUserRequired(boolean userRequired) {
704 sUserRequired = userRequired;
705 }
706
707 private static void throwIfUserRequired() {
708 if (sUserRequired) {
709 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
710 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700711 }
712 }
713
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700714 /**
715 * Append path segments to each given base path, returning result.
716 *
717 * @hide
718 */
719 public static File[] buildPaths(File[] base, String... segments) {
720 File[] result = new File[base.length];
721 for (int i = 0; i < base.length; i++) {
722 result[i] = buildPath(base[i], segments);
723 }
724 return result;
725 }
726
727 /**
728 * Append path segments to given base path, returning result.
729 *
730 * @hide
731 */
732 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700733 File cur = base;
734 for (String segment : segments) {
735 if (cur == null) {
736 cur = new File(segment);
737 } else {
738 cur = new File(cur, segment);
739 }
740 }
741 return cur;
742 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800743
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800744 private static boolean isStorageDisabled() {
745 return SystemProperties.getBoolean("config.disable_storage", false);
746 }
747
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800748 /**
749 * If the given path exists on emulated external storage, return the
750 * translated backing path hosted on internal storage. This bypasses any
751 * emulation later, improving performance. This is <em>only</em> suitable
752 * for read-only access.
753 * <p>
754 * Returns original path if given path doesn't meet these criteria. Callers
755 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
756 * permission.
757 *
758 * @hide
759 */
760 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700761 // TODO: bring back this optimization
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800762 return path;
763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764}