blob: 208085624e3b09c211370aacac8a6f707d20315e [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} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700247 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700248 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700249 return new File("/data");
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700250 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700251 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700252 }
253 }
254
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700255 /** {@hide} */
256 public static File getDataAppDirectory(String volumeUuid) {
257 return new File(getDataDirectory(volumeUuid), "app");
258 }
259
260 /** {@hide} */
261 public static File getDataUserDirectory(String volumeUuid) {
262 return new File(getDataDirectory(volumeUuid), "user");
263 }
264
265 /** {@hide} */
266 public static File getDataUserDirectory(String volumeUuid, int userId) {
267 return new File(getDataUserDirectory(volumeUuid), String.valueOf(userId));
268 }
269
270 /** {@hide} */
271 public static File getDataUserPackageDirectory(String volumeUuid, int userId,
272 String packageName) {
273 // TODO: keep consistent with installd
274 return new File(getDataUserDirectory(volumeUuid, userId), packageName);
275 }
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700278 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800279 * currently be accessible if it has been mounted by the user on their
280 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700281 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800282 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700283 * <p>
284 * <em>Note: don't be confused by the word "external" here. This directory
285 * can better be thought as media/shared storage. It is a filesystem that
286 * can hold a relatively large amount of data and that is shared across all
287 * applications (does not enforce permissions). Traditionally this is an SD
288 * card, but it may also be implemented as built-in storage in a device that
289 * is distinct from the protected internal storage and can be mounted as a
290 * filesystem on a computer.</em>
291 * <p>
292 * On devices with multiple users (as described by {@link UserManager}),
293 * each user has their own isolated external storage. Applications only have
294 * access to the external storage for the user they're running as.
295 * <p>
296 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700297 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700298 * with. Access to secondary storage is available through
299 * <p>
300 * Applications should not directly use this top-level directory, in order
301 * to avoid polluting the user's root namespace. Any files that are private
302 * to the application should be placed in a directory returned by
303 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700304 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700305 * if the application is uninstalled. Other shared files should be placed in
306 * one of the directories returned by
307 * {@link #getExternalStoragePublicDirectory}.
308 * <p>
309 * Writing to this path requires the
310 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
311 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700312 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700313 * which is automatically granted if you hold the write permission.
314 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700315 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700316 * application only needs to store internal data, consider using
317 * {@link Context#getExternalFilesDir(String)} or
318 * {@link Context#getExternalCacheDir()}, which require no permissions to
319 * read or write.
320 * <p>
321 * This path may change between platform versions, so applications should
322 * only persist relative paths.
323 * <p>
324 * Here is an example of typical code to monitor the state of external
325 * storage:
326 * <p>
327 * {@sample
328 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800329 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700330 *
331 * @see #getExternalStorageState()
332 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
334 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700335 throwIfUserRequired();
Jeff Sharkey48877892015-03-18 11:27:19 -0700336 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700337 }
338
339 /** {@hide} */
340 public static File getLegacyExternalStorageDirectory() {
341 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 }
343
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700344 /** {@hide} */
345 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700346 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700347 }
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800350 * Standard directory in which to place any audio files that should be
351 * in the regular list of music for the user.
352 * This may be combined with
353 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
354 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
355 * of directories to categories a particular audio file as more than one
356 * type.
357 */
358 public static String DIRECTORY_MUSIC = "Music";
359
360 /**
361 * Standard directory in which to place any audio files that should be
362 * in the list of podcasts that the user can select (not as regular
363 * music).
364 * This may be combined with {@link #DIRECTORY_MUSIC},
365 * {@link #DIRECTORY_NOTIFICATIONS},
366 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
367 * of directories to categories a particular audio file as more than one
368 * type.
369 */
370 public static String DIRECTORY_PODCASTS = "Podcasts";
371
372 /**
373 * Standard directory in which to place any audio files that should be
374 * in the list of ringtones that the user can select (not as regular
375 * music).
376 * This may be combined with {@link #DIRECTORY_MUSIC},
377 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
378 * {@link #DIRECTORY_ALARMS} as a series
379 * of directories to categories a particular audio file as more than one
380 * type.
381 */
382 public static String DIRECTORY_RINGTONES = "Ringtones";
383
384 /**
385 * Standard directory in which to place any audio files that should be
386 * in the list of alarms that the user can select (not as regular
387 * music).
388 * This may be combined with {@link #DIRECTORY_MUSIC},
389 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
390 * and {@link #DIRECTORY_RINGTONES} as a series
391 * of directories to categories a particular audio file as more than one
392 * type.
393 */
394 public static String DIRECTORY_ALARMS = "Alarms";
395
396 /**
397 * Standard directory in which to place any audio files that should be
398 * in the list of notifications that the user can select (not as regular
399 * music).
400 * This may be combined with {@link #DIRECTORY_MUSIC},
401 * {@link #DIRECTORY_PODCASTS},
402 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
403 * of directories to categories a particular audio file as more than one
404 * type.
405 */
406 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
407
408 /**
409 * Standard directory in which to place pictures that are available to
410 * the user. Note that this is primarily a convention for the top-level
411 * public directory, as the media scanner will find and collect pictures
412 * in any directory.
413 */
414 public static String DIRECTORY_PICTURES = "Pictures";
415
416 /**
417 * Standard directory in which to place movies that are available to
418 * the user. Note that this is primarily a convention for the top-level
419 * public directory, as the media scanner will find and collect movies
420 * in any directory.
421 */
422 public static String DIRECTORY_MOVIES = "Movies";
423
424 /**
425 * Standard directory in which to place files that have been downloaded by
426 * the user. Note that this is primarily a convention for the top-level
427 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700428 * private directories. Also note that though the constant here is
429 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
430 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800431 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700432 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800433
434 /**
435 * The traditional location for pictures and videos when mounting the
436 * device as a camera. Note that this is primarily a convention for the
437 * top-level public directory, as this convention makes no sense elsewhere.
438 */
439 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700440
441 /**
442 * Standard directory in which to place documents that have been created by
443 * the user.
444 */
445 public static String DIRECTORY_DOCUMENTS = "Documents";
446
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800447 /**
448 * Get a top-level public external storage directory for placing files of
449 * a particular type. This is where the user will typically place and
450 * manage their own files, so you should be careful about what you put here
451 * to ensure you don't erase their files or get in the way of their own
452 * organization.
453 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700454 * <p>On devices with multiple users (as described by {@link UserManager}),
455 * each user has their own isolated external storage. Applications only
456 * have access to the external storage for the user they're running as.</p>
457 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800458 * <p>Here is an example of typical code to manipulate a picture on
459 * the public external storage:</p>
460 *
461 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
462 * public_picture}
463 *
464 * @param type The type of storage directory to return. Should be one of
465 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
466 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
467 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
468 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
469 * {@link #DIRECTORY_DCIM}. May not be null.
470 *
471 * @return Returns the File path for the directory. Note that this
472 * directory may not yet exist, so you must make sure it exists before
473 * using it such as with {@link File#mkdirs File.mkdirs()}.
474 */
475 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700476 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700477 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800478 }
479
480 /**
481 * Returns the path for android-specific data on the SD card.
482 * @hide
483 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700484 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700485 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700486 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800487 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700488
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800489 /**
490 * Generates the raw path to an application's data
491 * @hide
492 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700493 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700494 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700495 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800496 }
497
498 /**
499 * Generates the raw path to an application's media
500 * @hide
501 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700502 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700503 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700504 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800505 }
506
507 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800508 * Generates the raw path to an application's OBB files
509 * @hide
510 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700511 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700512 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700513 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800514 }
515
516 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800517 * Generates the path to an application's files.
518 * @hide
519 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700520 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700521 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700522 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800523 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700524
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800525 /**
526 * Generates the path to an application's cache.
527 * @hide
528 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700529 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700530 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700531 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800532 }
533
534 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700535 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 */
537 public static File getDownloadCacheDirectory() {
538 return DOWNLOAD_CACHE_DIRECTORY;
539 }
540
541 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700542 * Unknown storage state, such as when a path isn't backed by known storage
543 * media.
544 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800545 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700546 */
547 public static final String MEDIA_UNKNOWN = "unknown";
548
549 /**
550 * Storage state if the media is not present.
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_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700557 * Storage state if the media is present but not mounted.
558 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800559 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 */
561 public static final String MEDIA_UNMOUNTED = "unmounted";
562
563 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700564 * Storage state if the media is present and being disk-checked.
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_CHECKING = "checking";
569
570 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700571 * Storage state if the media is present but is blank or is using an
572 * unsupported filesystem.
573 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800574 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 */
576 public static final String MEDIA_NOFS = "nofs";
577
578 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700579 * Storage state if the media is present and mounted at its mount point with
580 * read/write access.
581 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800582 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 */
584 public static final String MEDIA_MOUNTED = "mounted";
585
586 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700587 * Storage state if the media is present and mounted at its mount point with
588 * read-only access.
589 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800590 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 */
592 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
593
594 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700595 * Storage state if the media is present not mounted, and shared via USB
596 * mass storage.
597 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800598 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 */
600 public static final String MEDIA_SHARED = "shared";
601
602 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700603 * Storage state if the media was removed before it was unmounted.
604 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800605 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 */
607 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
608
609 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700610 * Storage state if the media is present but cannot be mounted. Typically
611 * this happens if the file system on the media is corrupted.
612 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800613 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 */
615 public static final String MEDIA_UNMOUNTABLE = "unmountable";
616
617 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700618 * Storage state if the media is in the process of being ejected.
619 *
620 * @see #getExternalStorageState(File)
621 */
622 public static final String MEDIA_EJECTING = "ejecting";
623
624 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700625 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800626 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700627 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700628 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
629 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
630 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
631 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
632 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 */
634 public static String getExternalStorageState() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700635 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800636 return getExternalStorageState(externalDir);
637 }
638
639 /**
640 * @deprecated use {@link #getExternalStorageState(File)}
641 */
642 @Deprecated
643 public static String getStorageState(File path) {
644 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700645 }
646
647 /**
648 * Returns the current state of the storage device that provides the given
649 * path.
650 *
651 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
652 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
653 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
654 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
655 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
656 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800657 public static String getExternalStorageState(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) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700660 return volume.getState();
661 } else {
662 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665
Dianne Hackborn407f6252010-10-04 11:31:17 -0700666 /**
667 * Returns whether the primary "external" storage device is removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700668 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800669 * @return true if the storage device can be removed (such as an SD card),
670 * or false if the storage device is built in and cannot be
671 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700672 */
673 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800674 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700675 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800676 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700677 }
678
Kenny Roote1ff2142010-10-12 11:20:01 -0700679 /**
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800680 * Returns whether the storage device that provides the given path is
681 * removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800682 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800683 * @return true if the storage device can be removed (such as an SD card),
684 * or false if the storage device is built in and cannot be
685 * physically removed.
686 * @throws IllegalArgumentException if the path is not a valid storage
687 * device.
688 */
689 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700690 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800691 if (volume != null) {
692 return volume.isRemovable();
693 } else {
694 throw new IllegalArgumentException("Failed to find storage device at " + path);
695 }
696 }
697
698 /**
699 * Returns whether the primary "external" storage device is emulated. If
700 * true, data stored on this device will be stored on a portion of the
701 * internal storage system.
Andy Stadler50c294f2011-03-07 19:13:42 -0800702 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800703 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
704 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700705 */
706 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800707 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700708 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800709 return isExternalStorageEmulated(externalDir);
710 }
711
712 /**
713 * Returns whether the storage device that provides the given path is
714 * emulated. If true, data stored on this device will be stored on a portion
715 * of the internal storage system.
716 *
717 * @throws IllegalArgumentException if the path is not a valid storage
718 * device.
719 */
720 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700721 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800722 if (volume != null) {
723 return volume.isEmulated();
724 } else {
725 throw new IllegalArgumentException("Failed to find storage device at " + path);
726 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700727 }
728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 static File getDirectory(String variableName, String defaultPath) {
730 String path = System.getenv(variableName);
731 return path == null ? new File(defaultPath) : new File(path);
732 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700733
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700734 /** {@hide} */
735 public static void setUserRequired(boolean userRequired) {
736 sUserRequired = userRequired;
737 }
738
739 private static void throwIfUserRequired() {
740 if (sUserRequired) {
741 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
742 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700743 }
744 }
745
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700746 /**
747 * Append path segments to each given base path, returning result.
748 *
749 * @hide
750 */
751 public static File[] buildPaths(File[] base, String... segments) {
752 File[] result = new File[base.length];
753 for (int i = 0; i < base.length; i++) {
754 result[i] = buildPath(base[i], segments);
755 }
756 return result;
757 }
758
759 /**
760 * Append path segments to given base path, returning result.
761 *
762 * @hide
763 */
764 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700765 File cur = base;
766 for (String segment : segments) {
767 if (cur == null) {
768 cur = new File(segment);
769 } else {
770 cur = new File(cur, segment);
771 }
772 }
773 return cur;
774 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800775
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800776 private static boolean isStorageDisabled() {
777 return SystemProperties.getBoolean("config.disable_storage", false);
778 }
779
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800780 /**
781 * If the given path exists on emulated external storage, return the
782 * translated backing path hosted on internal storage. This bypasses any
783 * emulation later, improving performance. This is <em>only</em> suitable
784 * for read-only access.
785 * <p>
786 * Returns original path if given path doesn't meet these criteria. Callers
787 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
788 * permission.
789 *
790 * @hide
791 */
792 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700793 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795}