blob: f346fe7a721c36fba7862703439a67a314de10ec [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() {
Jeff Sharkey46349872015-07-28 10:49:47 -070083 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
84 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070085 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -070086 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070087 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070088 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070089 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070090 }
91
Jeff Sharkey1abdb712013-08-11 16:28:14 -070092 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -070093 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -070094 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -070095 }
96
Jeff Sharkey1abdb712013-08-11 16:28:14 -070097 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -070098 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -070099 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700100 }
101
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700102 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700103 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700104 }
105
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700106 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700107 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700108 }
109
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700110 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700111 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700112 }
113
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700114 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700115 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700116 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700117
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700118 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700119 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700120 }
121
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700122 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700123 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700124 }
125
126 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700127 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700128 }
129
130 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700131 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700132 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700133 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800136 * Return root of the "system" partition holding the core Android OS.
137 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 */
139 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800140 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 }
142
Jeff Sharkey48877892015-03-18 11:27:19 -0700143 /** {@hide} */
144 public static File getStorageDirectory() {
145 return DIR_ANDROID_STORAGE;
146 }
147
Jason parksa3cdaa52011-01-13 14:15:43 -0600148 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800149 * Return root directory of the "oem" partition holding OEM customizations,
150 * if any. If present, the partition is mounted read-only.
151 *
152 * @hide
153 */
154 public static File getOemDirectory() {
155 return DIR_OEM_ROOT;
156 }
157
158 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700159 * Return root directory of the "vendor" partition that holds vendor-provided
160 * software that should persist across simple reflashing of the "system" partition.
161 * @hide
162 */
163 public static File getVendorDirectory() {
164 return DIR_VENDOR_ROOT;
165 }
166
167 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600168 * Gets the system directory available for secure storage.
169 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
170 * Otherwise, it returns the unencrypted /data/system directory.
171 * @return File object representing the secure storage system directory.
172 * @hide
173 */
174 public static File getSystemSecureDirectory() {
175 if (isEncryptedFilesystemEnabled()) {
176 return new File(SECURE_DATA_DIRECTORY, "system");
177 } else {
178 return new File(DATA_DIRECTORY, "system");
179 }
180 }
181
182 /**
183 * Gets the data directory for secure storage.
184 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
185 * Otherwise, it returns the unencrypted /data directory.
186 * @return File object representing the data directory for secure storage.
187 * @hide
188 */
189 public static File getSecureDataDirectory() {
190 if (isEncryptedFilesystemEnabled()) {
191 return SECURE_DATA_DIRECTORY;
192 } else {
193 return DATA_DIRECTORY;
194 }
195 }
196
197 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700198 * Return the system directory for a user. This is for use by system services to store
199 * files relating to the user. This directory will be automatically deleted when the user
200 * is removed.
201 *
202 * @hide
203 */
204 public static File getUserSystemDirectory(int userId) {
205 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
206 }
207
208 /**
Robin Lee69591332014-04-28 16:03:22 +0100209 * Returns the config directory for a user. This is for use by system services to store files
210 * relating to the user which should be readable by any app running as that user.
211 *
212 * @hide
213 */
214 public static File getUserConfigDirectory(int userId) {
215 return new File(new File(new File(
216 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
217 }
218
219 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600220 * Returns whether the Encrypted File System feature is enabled on the device or not.
221 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
222 * if disabled.
223 * @hide
224 */
225 public static boolean isEncryptedFilesystemEnabled() {
226 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
227 }
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 private static final File DATA_DIRECTORY
230 = getDirectory("ANDROID_DATA", "/data");
231
Oscar Montemayora8529f62009-11-18 10:14:20 -0800232 /**
233 * @hide
234 */
235 private static final File SECURE_DATA_DIRECTORY
236 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
237
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700238 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
240 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700241 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 */
243 public static File getDataDirectory() {
244 return DATA_DIRECTORY;
245 }
246
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700247 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700248 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700249 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700250 return new File("/data");
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700251 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700252 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700253 }
254 }
255
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700256 /** {@hide} */
257 public static File getDataAppDirectory(String volumeUuid) {
258 return new File(getDataDirectory(volumeUuid), "app");
259 }
260
261 /** {@hide} */
262 public static File getDataUserDirectory(String volumeUuid) {
263 return new File(getDataDirectory(volumeUuid), "user");
264 }
265
266 /** {@hide} */
267 public static File getDataUserDirectory(String volumeUuid, int userId) {
268 return new File(getDataUserDirectory(volumeUuid), String.valueOf(userId));
269 }
270
271 /** {@hide} */
272 public static File getDataUserPackageDirectory(String volumeUuid, int userId,
273 String packageName) {
274 // TODO: keep consistent with installd
275 return new File(getDataUserDirectory(volumeUuid, userId), packageName);
276 }
277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700279 * Return the primary shared/external storage directory. This directory may
280 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800281 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700282 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800283 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700284 * <p>
285 * <em>Note: don't be confused by the word "external" here. This directory
286 * can better be thought as media/shared storage. It is a filesystem that
287 * can hold a relatively large amount of data and that is shared across all
288 * applications (does not enforce permissions). Traditionally this is an SD
289 * card, but it may also be implemented as built-in storage in a device that
290 * is distinct from the protected internal storage and can be mounted as a
291 * filesystem on a computer.</em>
292 * <p>
293 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700294 * each user has their own isolated shared storage. Applications only have
295 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700296 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700297 * In devices with multiple shared/external storage directories, this
298 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700299 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700300 * {@link Context#getExternalFilesDirs(String)},
301 * {@link Context#getExternalCacheDirs()}, and
302 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700303 * <p>
304 * Applications should not directly use this top-level directory, in order
305 * to avoid polluting the user's root namespace. Any files that are private
306 * to the application should be placed in a directory returned by
307 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700308 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700309 * if the application is uninstalled. Other shared files should be placed in
310 * one of the directories returned by
311 * {@link #getExternalStoragePublicDirectory}.
312 * <p>
313 * Writing to this path requires the
314 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
315 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700316 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700317 * which is automatically granted if you hold the write permission.
318 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700319 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700320 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700321 * {@link Context#getExternalFilesDir(String)},
322 * {@link Context#getExternalCacheDir()}, or
323 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700324 * read or write.
325 * <p>
326 * This path may change between platform versions, so applications should
327 * only persist relative paths.
328 * <p>
329 * Here is an example of typical code to monitor the state of external
330 * storage:
331 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700332 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800333 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700334 *
335 * @see #getExternalStorageState()
336 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 */
338 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700339 throwIfUserRequired();
Jeff Sharkey48877892015-03-18 11:27:19 -0700340 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700341 }
342
343 /** {@hide} */
344 public static File getLegacyExternalStorageDirectory() {
345 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700348 /** {@hide} */
349 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700350 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700351 }
352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800354 * Standard directory in which to place any audio files that should be
355 * in the regular list of music for the user.
356 * This may be combined with
357 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
358 * {@link #DIRECTORY_ALARMS}, 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_MUSIC = "Music";
363
364 /**
365 * Standard directory in which to place any audio files that should be
366 * in the list of podcasts that the user can select (not as regular
367 * music).
368 * This may be combined with {@link #DIRECTORY_MUSIC},
369 * {@link #DIRECTORY_NOTIFICATIONS},
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_PODCASTS = "Podcasts";
375
376 /**
377 * Standard directory in which to place any audio files that should be
378 * in the list of ringtones that the user can select (not as regular
379 * music).
380 * This may be combined with {@link #DIRECTORY_MUSIC},
381 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
382 * {@link #DIRECTORY_ALARMS} as a series
383 * of directories to categories a particular audio file as more than one
384 * type.
385 */
386 public static String DIRECTORY_RINGTONES = "Ringtones";
387
388 /**
389 * Standard directory in which to place any audio files that should be
390 * in the list of alarms that the user can select (not as regular
391 * music).
392 * This may be combined with {@link #DIRECTORY_MUSIC},
393 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
394 * and {@link #DIRECTORY_RINGTONES} as a series
395 * of directories to categories a particular audio file as more than one
396 * type.
397 */
398 public static String DIRECTORY_ALARMS = "Alarms";
399
400 /**
401 * Standard directory in which to place any audio files that should be
402 * in the list of notifications that the user can select (not as regular
403 * music).
404 * This may be combined with {@link #DIRECTORY_MUSIC},
405 * {@link #DIRECTORY_PODCASTS},
406 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
407 * of directories to categories a particular audio file as more than one
408 * type.
409 */
410 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
411
412 /**
413 * Standard directory in which to place pictures that are available to
414 * the user. Note that this is primarily a convention for the top-level
415 * public directory, as the media scanner will find and collect pictures
416 * in any directory.
417 */
418 public static String DIRECTORY_PICTURES = "Pictures";
419
420 /**
421 * Standard directory in which to place movies that are available to
422 * the user. Note that this is primarily a convention for the top-level
423 * public directory, as the media scanner will find and collect movies
424 * in any directory.
425 */
426 public static String DIRECTORY_MOVIES = "Movies";
427
428 /**
429 * Standard directory in which to place files that have been downloaded by
430 * the user. Note that this is primarily a convention for the top-level
431 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700432 * private directories. Also note that though the constant here is
433 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
434 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800435 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700436 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800437
438 /**
439 * The traditional location for pictures and videos when mounting the
440 * device as a camera. Note that this is primarily a convention for the
441 * top-level public directory, as this convention makes no sense elsewhere.
442 */
443 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700444
445 /**
446 * Standard directory in which to place documents that have been created by
447 * the user.
448 */
449 public static String DIRECTORY_DOCUMENTS = "Documents";
450
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800451 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700452 * Get a top-level shared/external storage directory for placing files of a
453 * particular type. This is where the user will typically place and manage
454 * their own files, so you should be careful about what you put here to
455 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800456 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700457 * <p>
458 * On devices with multiple users (as described by {@link UserManager}),
459 * each user has their own isolated shared storage. Applications only have
460 * access to the shared storage for the user they're running as.
461 * </p>
462 * <p>
463 * Here is an example of typical code to manipulate a picture on the public
464 * shared storage:
465 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800466 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
467 * public_picture}
468 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700469 * @param type The type of storage directory to return. Should be one of
470 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
471 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
472 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
473 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
474 * {@link #DIRECTORY_DCIM}. May not be null.
475 * @return Returns the File path for the directory. Note that this directory
476 * may not yet exist, so you must make sure it exists before using
477 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800478 */
479 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700480 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700481 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800482 }
483
484 /**
485 * Returns the path for android-specific data on the SD card.
486 * @hide
487 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700488 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700489 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700490 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800491 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700492
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800493 /**
494 * Generates the raw path to an application's data
495 * @hide
496 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700497 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700498 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700499 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800500 }
501
502 /**
503 * Generates the raw path to an application's media
504 * @hide
505 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700506 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700507 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700508 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800509 }
510
511 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800512 * Generates the raw path to an application's OBB files
513 * @hide
514 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700515 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700516 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700517 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800518 }
519
520 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800521 * Generates the path to an application's files.
522 * @hide
523 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700524 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700525 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700526 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800527 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700528
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800529 /**
530 * Generates the path to an application's cache.
531 * @hide
532 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700533 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700534 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700535 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800536 }
537
538 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700539 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 */
541 public static File getDownloadCacheDirectory() {
542 return DOWNLOAD_CACHE_DIRECTORY;
543 }
544
545 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700546 * Unknown storage state, such as when a path isn't backed by known storage
547 * media.
548 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800549 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700550 */
551 public static final String MEDIA_UNKNOWN = "unknown";
552
553 /**
554 * Storage state if the media is not present.
555 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800556 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 */
558 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700561 * Storage state if the media is present but not mounted.
562 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800563 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 */
565 public static final String MEDIA_UNMOUNTED = "unmounted";
566
567 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700568 * Storage state if the media is present and being disk-checked.
569 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800570 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 */
572 public static final String MEDIA_CHECKING = "checking";
573
574 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700575 * Storage state if the media is present but is blank or is using an
576 * unsupported filesystem.
577 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800578 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 */
580 public static final String MEDIA_NOFS = "nofs";
581
582 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700583 * Storage state if the media is present and mounted at its mount point with
584 * read/write access.
585 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800586 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 */
588 public static final String MEDIA_MOUNTED = "mounted";
589
590 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700591 * Storage state if the media is present and mounted at its mount point with
592 * read-only access.
593 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800594 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 */
596 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
597
598 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700599 * Storage state if the media is present not mounted, and shared via USB
600 * mass storage.
601 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800602 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 */
604 public static final String MEDIA_SHARED = "shared";
605
606 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700607 * Storage state if the media was removed before it was unmounted.
608 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800609 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 */
611 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
612
613 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700614 * Storage state if the media is present but cannot be mounted. Typically
615 * this happens if the file system on the media is corrupted.
616 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800617 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 */
619 public static final String MEDIA_UNMOUNTABLE = "unmountable";
620
621 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700622 * Storage state if the media is in the process of being ejected.
623 *
624 * @see #getExternalStorageState(File)
625 */
626 public static final String MEDIA_EJECTING = "ejecting";
627
628 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700629 * Returns the current state of the primary shared/external storage media.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800630 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700631 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700632 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
633 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
634 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
635 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
636 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 */
638 public static String getExternalStorageState() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700639 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800640 return getExternalStorageState(externalDir);
641 }
642
643 /**
644 * @deprecated use {@link #getExternalStorageState(File)}
645 */
646 @Deprecated
647 public static String getStorageState(File path) {
648 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700649 }
650
651 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700652 * Returns the current state of the shared/external storage media at the
653 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700654 *
655 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
656 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
657 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
658 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
659 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
660 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800661 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700662 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800663 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700664 return volume.getState();
665 } else {
666 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
669
Dianne Hackborn407f6252010-10-04 11:31:17 -0700670 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700671 * Returns whether the primary shared/external storage media is physically
672 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700673 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800674 * @return true if the storage device can be removed (such as an SD card),
675 * or false if the storage device is built in and cannot be
676 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700677 */
678 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800679 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700680 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800681 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700682 }
683
Kenny Roote1ff2142010-10-12 11:20:01 -0700684 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700685 * Returns whether the shared/external storage media at the given path is
686 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800687 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800688 * @return true if the storage device can be removed (such as an SD card),
689 * or false if the storage device is built in and cannot be
690 * physically removed.
691 * @throws IllegalArgumentException if the path is not a valid storage
692 * device.
693 */
694 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700695 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800696 if (volume != null) {
697 return volume.isRemovable();
698 } else {
699 throw new IllegalArgumentException("Failed to find storage device at " + path);
700 }
701 }
702
703 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700704 * Returns whether the primary shared/external storage media is emulated.
705 * <p>
706 * The contents of emulated storage devices are backed by a private user
707 * data partition, which means there is little benefit to apps storing data
708 * here instead of the private directories returned by
709 * {@link Context#getFilesDir()}, etc.
710 * <p>
711 * This returns true when emulated storage is backed by either internal
712 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -0800713 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800714 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
715 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700716 */
717 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800718 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700719 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800720 return isExternalStorageEmulated(externalDir);
721 }
722
723 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700724 * Returns whether the shared/external storage media at the given path is
725 * emulated.
726 * <p>
727 * The contents of emulated storage devices are backed by a private user
728 * data partition, which means there is little benefit to apps storing data
729 * here instead of the private directories returned by
730 * {@link Context#getFilesDir()}, etc.
731 * <p>
732 * This returns true when emulated storage is backed by either internal
733 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800734 *
735 * @throws IllegalArgumentException if the path is not a valid storage
736 * device.
737 */
738 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700739 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800740 if (volume != null) {
741 return volume.isEmulated();
742 } else {
743 throw new IllegalArgumentException("Failed to find storage device at " + path);
744 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700745 }
746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 static File getDirectory(String variableName, String defaultPath) {
748 String path = System.getenv(variableName);
749 return path == null ? new File(defaultPath) : new File(path);
750 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700751
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700752 /** {@hide} */
753 public static void setUserRequired(boolean userRequired) {
754 sUserRequired = userRequired;
755 }
756
757 private static void throwIfUserRequired() {
758 if (sUserRequired) {
759 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
760 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700761 }
762 }
763
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700764 /**
765 * Append path segments to each given base path, returning result.
766 *
767 * @hide
768 */
769 public static File[] buildPaths(File[] base, String... segments) {
770 File[] result = new File[base.length];
771 for (int i = 0; i < base.length; i++) {
772 result[i] = buildPath(base[i], segments);
773 }
774 return result;
775 }
776
777 /**
778 * Append path segments to given base path, returning result.
779 *
780 * @hide
781 */
782 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700783 File cur = base;
784 for (String segment : segments) {
785 if (cur == null) {
786 cur = new File(segment);
787 } else {
788 cur = new File(cur, segment);
789 }
790 }
791 return cur;
792 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800793
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800794 private static boolean isStorageDisabled() {
795 return SystemProperties.getBoolean("config.disable_storage", false);
796 }
797
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800798 /**
799 * If the given path exists on emulated external storage, return the
800 * translated backing path hosted on internal storage. This bypasses any
801 * emulation later, improving performance. This is <em>only</em> suitable
802 * for read-only access.
803 * <p>
804 * Returns original path if given path doesn't meet these criteria. Callers
805 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
806 * permission.
807 *
808 * @hide
809 */
810 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700811 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813}