blob: 8e0584a3e0bce1de60069234b90acb9c17fddb4b [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;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070023import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080024import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080025
Gilles Debunneee1d6302011-05-13 10:09:32 -070026import java.io.File;
27
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028/**
29 * Provides access to environment variables.
30 */
31public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080032 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Jeff Sharkeyb049e212012-09-07 23:16:01 -070034 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080035 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070036 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
37 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080038 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070039 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070040
Jeff Sharkeydfa45302012-09-12 16:25:22 -070041 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070042 public static final String DIR_ANDROID = "Android";
43 private static final String DIR_DATA = "data";
44 private static final String DIR_MEDIA = "media";
45 private static final String DIR_OBB = "obb";
46 private static final String DIR_FILES = "files";
47 private static final String DIR_CACHE = "cache";
48
49 /** {@hide} */
50 @Deprecated
51 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070052
Jeff Sharkey63d0a062013-03-01 16:12:55 -080053 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070054 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
55 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080056 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Christopher Tate740888f2014-04-18 12:24:57 -070057 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Oscar Montemayora8529f62009-11-18 10:14:20 -080059 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
60
Jeff Sharkeyb049e212012-09-07 23:16:01 -070061 private static UserEnvironment sCurrentUser;
Jeff Sharkey48749fc2013-04-19 13:25:04 -070062 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070063
Jeff Sharkeyb049e212012-09-07 23:16:01 -070064 static {
65 initForCurrentUser();
66 }
67
68 /** {@hide} */
69 public static void initForCurrentUser() {
70 final int userId = UserHandle.myUserId();
71 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070072 }
73
74 /** {@hide} */
75 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070076 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070077
78 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070079 mUserId = userId;
80 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070081
Jeff Sharkey48877892015-03-18 11:27:19 -070082 public File[] getExternalDirs() {
83 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070084 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -070085 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070086 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070087 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070088 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070089 }
90
Jeff Sharkey1abdb712013-08-11 16:28:14 -070091 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -070092 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -070093 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -070094 }
95
Jeff Sharkey1abdb712013-08-11 16:28:14 -070096 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -070097 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -070098 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -070099 }
100
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700101 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700102 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700103 }
104
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700105 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700106 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700107 }
108
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700109 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700110 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700111 }
112
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700113 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700114 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700115 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700116
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700117 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700118 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700119 }
120
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700121 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700122 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700123 }
124
125 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700126 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700127 }
128
129 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700130 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700131 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700132 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800135 * Return root of the "system" partition holding the core Android OS.
136 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 */
138 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800139 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 }
141
Jeff Sharkey48877892015-03-18 11:27:19 -0700142 /** {@hide} */
143 public static File getStorageDirectory() {
144 return DIR_ANDROID_STORAGE;
145 }
146
Jason parksa3cdaa52011-01-13 14:15:43 -0600147 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800148 * Return root directory of the "oem" partition holding OEM customizations,
149 * if any. If present, the partition is mounted read-only.
150 *
151 * @hide
152 */
153 public static File getOemDirectory() {
154 return DIR_OEM_ROOT;
155 }
156
157 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700158 * Return root directory of the "vendor" partition that holds vendor-provided
159 * software that should persist across simple reflashing of the "system" partition.
160 * @hide
161 */
162 public static File getVendorDirectory() {
163 return DIR_VENDOR_ROOT;
164 }
165
166 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600167 * Gets the system directory available for secure storage.
168 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
169 * Otherwise, it returns the unencrypted /data/system directory.
170 * @return File object representing the secure storage system directory.
171 * @hide
172 */
173 public static File getSystemSecureDirectory() {
174 if (isEncryptedFilesystemEnabled()) {
175 return new File(SECURE_DATA_DIRECTORY, "system");
176 } else {
177 return new File(DATA_DIRECTORY, "system");
178 }
179 }
180
181 /**
182 * Gets the data directory for secure storage.
183 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
184 * Otherwise, it returns the unencrypted /data directory.
185 * @return File object representing the data directory for secure storage.
186 * @hide
187 */
188 public static File getSecureDataDirectory() {
189 if (isEncryptedFilesystemEnabled()) {
190 return SECURE_DATA_DIRECTORY;
191 } else {
192 return DATA_DIRECTORY;
193 }
194 }
195
196 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700197 * Return the system directory for a user. This is for use by system services to store
198 * files relating to the user. This directory will be automatically deleted when the user
199 * is removed.
200 *
201 * @hide
202 */
203 public static File getUserSystemDirectory(int userId) {
204 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
205 }
206
207 /**
Robin Lee69591332014-04-28 16:03:22 +0100208 * Returns the config directory for a user. This is for use by system services to store files
209 * relating to the user which should be readable by any app running as that user.
210 *
211 * @hide
212 */
213 public static File getUserConfigDirectory(int userId) {
214 return new File(new File(new File(
215 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
216 }
217
218 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600219 * Returns whether the Encrypted File System feature is enabled on the device or not.
220 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
221 * if disabled.
222 * @hide
223 */
224 public static boolean isEncryptedFilesystemEnabled() {
225 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
226 }
227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 private static final File DATA_DIRECTORY
229 = getDirectory("ANDROID_DATA", "/data");
230
Oscar Montemayora8529f62009-11-18 10:14:20 -0800231 /**
232 * @hide
233 */
234 private static final File SECURE_DATA_DIRECTORY
235 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
236
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700237 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700240 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 */
242 public static File getDataDirectory() {
243 return DATA_DIRECTORY;
244 }
245
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700246 /** {@hide} */
247 public static File getDataAppDirectory(String volumeUuid) {
248 if (TextUtils.isEmpty(volumeUuid)) {
249 return new File("/data/app");
250 } else {
251 return new File("/mnt/expand/" + volumeUuid + "/app");
252 }
253 }
254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700256 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800257 * currently be accessible if it has been mounted by the user on their
258 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700259 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800260 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700261 * <p>
262 * <em>Note: don't be confused by the word "external" here. This directory
263 * can better be thought as media/shared storage. It is a filesystem that
264 * can hold a relatively large amount of data and that is shared across all
265 * applications (does not enforce permissions). Traditionally this is an SD
266 * card, but it may also be implemented as built-in storage in a device that
267 * is distinct from the protected internal storage and can be mounted as a
268 * filesystem on a computer.</em>
269 * <p>
270 * On devices with multiple users (as described by {@link UserManager}),
271 * each user has their own isolated external storage. Applications only have
272 * access to the external storage for the user they're running as.
273 * <p>
274 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700275 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700276 * with. Access to secondary storage is available through
277 * <p>
278 * Applications should not directly use this top-level directory, in order
279 * to avoid polluting the user's root namespace. Any files that are private
280 * to the application should be placed in a directory returned by
281 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700282 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700283 * if the application is uninstalled. Other shared files should be placed in
284 * one of the directories returned by
285 * {@link #getExternalStoragePublicDirectory}.
286 * <p>
287 * Writing to this path requires the
288 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
289 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700290 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700291 * which is automatically granted if you hold the write permission.
292 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700293 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700294 * application only needs to store internal data, consider using
295 * {@link Context#getExternalFilesDir(String)} or
296 * {@link Context#getExternalCacheDir()}, which require no permissions to
297 * read or write.
298 * <p>
299 * This path may change between platform versions, so applications should
300 * only persist relative paths.
301 * <p>
302 * Here is an example of typical code to monitor the state of external
303 * storage:
304 * <p>
305 * {@sample
306 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800307 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700308 *
309 * @see #getExternalStorageState()
310 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 */
312 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700313 throwIfUserRequired();
Jeff Sharkey48877892015-03-18 11:27:19 -0700314 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700315 }
316
317 /** {@hide} */
318 public static File getLegacyExternalStorageDirectory() {
319 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700322 /** {@hide} */
323 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700324 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700325 }
326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800328 * Standard directory in which to place any audio files that should be
329 * in the regular list of music for the user.
330 * This may be combined with
331 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
332 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
333 * of directories to categories a particular audio file as more than one
334 * type.
335 */
336 public static String DIRECTORY_MUSIC = "Music";
337
338 /**
339 * Standard directory in which to place any audio files that should be
340 * in the list of podcasts that the user can select (not as regular
341 * music).
342 * This may be combined with {@link #DIRECTORY_MUSIC},
343 * {@link #DIRECTORY_NOTIFICATIONS},
344 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
345 * of directories to categories a particular audio file as more than one
346 * type.
347 */
348 public static String DIRECTORY_PODCASTS = "Podcasts";
349
350 /**
351 * Standard directory in which to place any audio files that should be
352 * in the list of ringtones that the user can select (not as regular
353 * music).
354 * This may be combined with {@link #DIRECTORY_MUSIC},
355 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
356 * {@link #DIRECTORY_ALARMS} as a series
357 * of directories to categories a particular audio file as more than one
358 * type.
359 */
360 public static String DIRECTORY_RINGTONES = "Ringtones";
361
362 /**
363 * Standard directory in which to place any audio files that should be
364 * in the list of alarms that the user can select (not as regular
365 * music).
366 * This may be combined with {@link #DIRECTORY_MUSIC},
367 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
368 * and {@link #DIRECTORY_RINGTONES} as a series
369 * of directories to categories a particular audio file as more than one
370 * type.
371 */
372 public static String DIRECTORY_ALARMS = "Alarms";
373
374 /**
375 * Standard directory in which to place any audio files that should be
376 * in the list of notifications that the user can select (not as regular
377 * music).
378 * This may be combined with {@link #DIRECTORY_MUSIC},
379 * {@link #DIRECTORY_PODCASTS},
380 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
381 * of directories to categories a particular audio file as more than one
382 * type.
383 */
384 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
385
386 /**
387 * Standard directory in which to place pictures that are available to
388 * the user. Note that this is primarily a convention for the top-level
389 * public directory, as the media scanner will find and collect pictures
390 * in any directory.
391 */
392 public static String DIRECTORY_PICTURES = "Pictures";
393
394 /**
395 * Standard directory in which to place movies that are available to
396 * the user. Note that this is primarily a convention for the top-level
397 * public directory, as the media scanner will find and collect movies
398 * in any directory.
399 */
400 public static String DIRECTORY_MOVIES = "Movies";
401
402 /**
403 * Standard directory in which to place files that have been downloaded by
404 * the user. Note that this is primarily a convention for the top-level
405 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700406 * private directories. Also note that though the constant here is
407 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
408 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800409 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700410 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800411
412 /**
413 * The traditional location for pictures and videos when mounting the
414 * device as a camera. Note that this is primarily a convention for the
415 * top-level public directory, as this convention makes no sense elsewhere.
416 */
417 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700418
419 /**
420 * Standard directory in which to place documents that have been created by
421 * the user.
422 */
423 public static String DIRECTORY_DOCUMENTS = "Documents";
424
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800425 /**
426 * Get a top-level public external storage directory for placing files of
427 * a particular type. This is where the user will typically place and
428 * manage their own files, so you should be careful about what you put here
429 * to ensure you don't erase their files or get in the way of their own
430 * organization.
431 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700432 * <p>On devices with multiple users (as described by {@link UserManager}),
433 * each user has their own isolated external storage. Applications only
434 * have access to the external storage for the user they're running as.</p>
435 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800436 * <p>Here is an example of typical code to manipulate a picture on
437 * the public external storage:</p>
438 *
439 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
440 * public_picture}
441 *
442 * @param type The type of storage directory to return. Should be one of
443 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
444 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
445 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
446 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
447 * {@link #DIRECTORY_DCIM}. May not be null.
448 *
449 * @return Returns the File path for the directory. Note that this
450 * directory may not yet exist, so you must make sure it exists before
451 * using it such as with {@link File#mkdirs File.mkdirs()}.
452 */
453 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700454 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700455 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800456 }
457
458 /**
459 * Returns the path for android-specific data on the SD card.
460 * @hide
461 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700462 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700463 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700464 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800465 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700466
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800467 /**
468 * Generates the raw path to an application's data
469 * @hide
470 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700471 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700472 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700473 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800474 }
475
476 /**
477 * Generates the raw path to an application's media
478 * @hide
479 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700480 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700481 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700482 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800483 }
484
485 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800486 * Generates the raw path to an application's OBB files
487 * @hide
488 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700489 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700490 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700491 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800492 }
493
494 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800495 * Generates the path to an application's files.
496 * @hide
497 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700498 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700499 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700500 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800501 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700502
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800503 /**
504 * Generates the path to an application's cache.
505 * @hide
506 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700507 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700508 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700509 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800510 }
511
512 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700513 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 */
515 public static File getDownloadCacheDirectory() {
516 return DOWNLOAD_CACHE_DIRECTORY;
517 }
518
519 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700520 * Unknown storage state, such as when a path isn't backed by known storage
521 * media.
522 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800523 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700524 */
525 public static final String MEDIA_UNKNOWN = "unknown";
526
527 /**
528 * Storage state if the media is not present.
529 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800530 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 */
532 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700535 * Storage state if the media is present but not mounted.
536 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800537 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 */
539 public static final String MEDIA_UNMOUNTED = "unmounted";
540
541 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700542 * Storage state if the media is present and being disk-checked.
543 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800544 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 */
546 public static final String MEDIA_CHECKING = "checking";
547
548 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700549 * Storage state if the media is present but is blank or is using an
550 * unsupported filesystem.
551 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800552 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 */
554 public static final String MEDIA_NOFS = "nofs";
555
556 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700557 * Storage state if the media is present and mounted at its mount point with
558 * read/write access.
559 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800560 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 */
562 public static final String MEDIA_MOUNTED = "mounted";
563
564 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700565 * Storage state if the media is present and mounted at its mount point with
566 * read-only access.
567 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800568 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 */
570 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
571
572 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700573 * Storage state if the media is present not mounted, and shared via USB
574 * mass storage.
575 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800576 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 */
578 public static final String MEDIA_SHARED = "shared";
579
580 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700581 * Storage state if the media was removed before it was unmounted.
582 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800583 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 */
585 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
586
587 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700588 * Storage state if the media is present but cannot be mounted. Typically
589 * this happens if the file system on the media is corrupted.
590 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800591 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 */
593 public static final String MEDIA_UNMOUNTABLE = "unmountable";
594
595 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700596 * Storage state if the media is in the process of being ejected.
597 *
598 * @see #getExternalStorageState(File)
599 */
600 public static final String MEDIA_EJECTING = "ejecting";
601
602 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700603 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800604 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700605 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700606 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
607 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
608 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
609 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
610 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 */
612 public static String getExternalStorageState() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700613 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800614 return getExternalStorageState(externalDir);
615 }
616
617 /**
618 * @deprecated use {@link #getExternalStorageState(File)}
619 */
620 @Deprecated
621 public static String getStorageState(File path) {
622 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700623 }
624
625 /**
626 * Returns the current state of the storage device that provides the given
627 * path.
628 *
629 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
630 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
631 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
632 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
633 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
634 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800635 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700636 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800637 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700638 return volume.getState();
639 } else {
640 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643
Dianne Hackborn407f6252010-10-04 11:31:17 -0700644 /**
645 * Returns whether the primary "external" storage device is removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700646 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800647 * @return true if the storage device can be removed (such as an SD card),
648 * or false if the storage device is built in and cannot be
649 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700650 */
651 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800652 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700653 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800654 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700655 }
656
Kenny Roote1ff2142010-10-12 11:20:01 -0700657 /**
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800658 * Returns whether the storage device that provides the given path is
659 * removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800660 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800661 * @return true if the storage device can be removed (such as an SD card),
662 * or false if the storage device is built in and cannot be
663 * physically removed.
664 * @throws IllegalArgumentException if the path is not a valid storage
665 * device.
666 */
667 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700668 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800669 if (volume != null) {
670 return volume.isRemovable();
671 } else {
672 throw new IllegalArgumentException("Failed to find storage device at " + path);
673 }
674 }
675
676 /**
677 * Returns whether the primary "external" storage device is emulated. If
678 * true, data stored on this device will be stored on a portion of the
679 * internal storage system.
Andy Stadler50c294f2011-03-07 19:13:42 -0800680 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800681 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
682 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700683 */
684 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800685 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700686 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800687 return isExternalStorageEmulated(externalDir);
688 }
689
690 /**
691 * Returns whether the storage device that provides the given path is
692 * emulated. If true, data stored on this device will be stored on a portion
693 * of the internal storage system.
694 *
695 * @throws IllegalArgumentException if the path is not a valid storage
696 * device.
697 */
698 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700699 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800700 if (volume != null) {
701 return volume.isEmulated();
702 } else {
703 throw new IllegalArgumentException("Failed to find storage device at " + path);
704 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700705 }
706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 static File getDirectory(String variableName, String defaultPath) {
708 String path = System.getenv(variableName);
709 return path == null ? new File(defaultPath) : new File(path);
710 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700711
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700712 /** {@hide} */
713 public static void setUserRequired(boolean userRequired) {
714 sUserRequired = userRequired;
715 }
716
717 private static void throwIfUserRequired() {
718 if (sUserRequired) {
719 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
720 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700721 }
722 }
723
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700724 /**
725 * Append path segments to each given base path, returning result.
726 *
727 * @hide
728 */
729 public static File[] buildPaths(File[] base, String... segments) {
730 File[] result = new File[base.length];
731 for (int i = 0; i < base.length; i++) {
732 result[i] = buildPath(base[i], segments);
733 }
734 return result;
735 }
736
737 /**
738 * Append path segments to given base path, returning result.
739 *
740 * @hide
741 */
742 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700743 File cur = base;
744 for (String segment : segments) {
745 if (cur == null) {
746 cur = new File(segment);
747 } else {
748 cur = new File(cur, segment);
749 }
750 }
751 return cur;
752 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800753
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800754 private static boolean isStorageDisabled() {
755 return SystemProperties.getBoolean("config.disable_storage", false);
756 }
757
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800758 /**
759 * If the given path exists on emulated external storage, return the
760 * translated backing path hosted on internal storage. This bypasses any
761 * emulation later, improving performance. This is <em>only</em> suitable
762 * for read-only access.
763 * <p>
764 * Returns original path if given path doesn't meet these criteria. Callers
765 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
766 * permission.
767 *
768 * @hide
769 */
770 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700771 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773}