blob: 64d6da57718f37b4cb2e6e038ad048d72067113f [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 Sharkey1abdb712013-08-11 16:28:14 -0700279 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800280 * currently be accessible if it has been mounted by the user on their
281 * 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}),
294 * each user has their own isolated external storage. Applications only have
295 * access to the external storage for the user they're running as.
296 * <p>
297 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700298 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700299 * with. Access to secondary storage is available through
300 * <p>
301 * Applications should not directly use this top-level directory, in order
302 * to avoid polluting the user's root namespace. Any files that are private
303 * to the application should be placed in a directory returned by
304 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700305 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700306 * if the application is uninstalled. Other shared files should be placed in
307 * one of the directories returned by
308 * {@link #getExternalStoragePublicDirectory}.
309 * <p>
310 * Writing to this path requires the
311 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
312 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700313 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700314 * which is automatically granted if you hold the write permission.
315 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700316 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700317 * application only needs to store internal data, consider using
318 * {@link Context#getExternalFilesDir(String)} or
319 * {@link Context#getExternalCacheDir()}, which require no permissions to
320 * read or write.
321 * <p>
322 * This path may change between platform versions, so applications should
323 * only persist relative paths.
324 * <p>
325 * Here is an example of typical code to monitor the state of external
326 * storage:
327 * <p>
328 * {@sample
329 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800330 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700331 *
332 * @see #getExternalStorageState()
333 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 */
335 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700336 throwIfUserRequired();
Jeff Sharkey48877892015-03-18 11:27:19 -0700337 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700338 }
339
340 /** {@hide} */
341 public static File getLegacyExternalStorageDirectory() {
342 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700345 /** {@hide} */
346 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700347 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700348 }
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800351 * Standard directory in which to place any audio files that should be
352 * in the regular list of music for the user.
353 * This may be combined with
354 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
355 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
356 * of directories to categories a particular audio file as more than one
357 * type.
358 */
359 public static String DIRECTORY_MUSIC = "Music";
360
361 /**
362 * Standard directory in which to place any audio files that should be
363 * in the list of podcasts that the user can select (not as regular
364 * music).
365 * This may be combined with {@link #DIRECTORY_MUSIC},
366 * {@link #DIRECTORY_NOTIFICATIONS},
367 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
368 * of directories to categories a particular audio file as more than one
369 * type.
370 */
371 public static String DIRECTORY_PODCASTS = "Podcasts";
372
373 /**
374 * Standard directory in which to place any audio files that should be
375 * in the list of ringtones that the user can select (not as regular
376 * music).
377 * This may be combined with {@link #DIRECTORY_MUSIC},
378 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
379 * {@link #DIRECTORY_ALARMS} as a series
380 * of directories to categories a particular audio file as more than one
381 * type.
382 */
383 public static String DIRECTORY_RINGTONES = "Ringtones";
384
385 /**
386 * Standard directory in which to place any audio files that should be
387 * in the list of alarms that the user can select (not as regular
388 * music).
389 * This may be combined with {@link #DIRECTORY_MUSIC},
390 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
391 * and {@link #DIRECTORY_RINGTONES} as a series
392 * of directories to categories a particular audio file as more than one
393 * type.
394 */
395 public static String DIRECTORY_ALARMS = "Alarms";
396
397 /**
398 * Standard directory in which to place any audio files that should be
399 * in the list of notifications that the user can select (not as regular
400 * music).
401 * This may be combined with {@link #DIRECTORY_MUSIC},
402 * {@link #DIRECTORY_PODCASTS},
403 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
404 * of directories to categories a particular audio file as more than one
405 * type.
406 */
407 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
408
409 /**
410 * Standard directory in which to place pictures that are available to
411 * the user. Note that this is primarily a convention for the top-level
412 * public directory, as the media scanner will find and collect pictures
413 * in any directory.
414 */
415 public static String DIRECTORY_PICTURES = "Pictures";
416
417 /**
418 * Standard directory in which to place movies that are available to
419 * the user. Note that this is primarily a convention for the top-level
420 * public directory, as the media scanner will find and collect movies
421 * in any directory.
422 */
423 public static String DIRECTORY_MOVIES = "Movies";
424
425 /**
426 * Standard directory in which to place files that have been downloaded by
427 * the user. Note that this is primarily a convention for the top-level
428 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700429 * private directories. Also note that though the constant here is
430 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
431 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800432 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700433 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800434
435 /**
436 * The traditional location for pictures and videos when mounting the
437 * device as a camera. Note that this is primarily a convention for the
438 * top-level public directory, as this convention makes no sense elsewhere.
439 */
440 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700441
442 /**
443 * Standard directory in which to place documents that have been created by
444 * the user.
445 */
446 public static String DIRECTORY_DOCUMENTS = "Documents";
447
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800448 /**
449 * Get a top-level public external storage directory for placing files of
450 * a particular type. This is where the user will typically place and
451 * manage their own files, so you should be careful about what you put here
452 * to ensure you don't erase their files or get in the way of their own
453 * organization.
454 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700455 * <p>On devices with multiple users (as described by {@link UserManager}),
456 * each user has their own isolated external storage. Applications only
457 * have access to the external storage for the user they're running as.</p>
458 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800459 * <p>Here is an example of typical code to manipulate a picture on
460 * the public external storage:</p>
461 *
462 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
463 * public_picture}
464 *
465 * @param type The type of storage directory to return. Should be one of
466 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
467 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
468 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
469 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
470 * {@link #DIRECTORY_DCIM}. May not be null.
471 *
472 * @return Returns the File path for the directory. Note that this
473 * directory may not yet exist, so you must make sure it exists before
474 * using it such as with {@link File#mkdirs File.mkdirs()}.
475 */
476 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700477 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700478 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800479 }
480
481 /**
482 * Returns the path for android-specific data on the SD card.
483 * @hide
484 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700485 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700486 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700487 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800488 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700489
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800490 /**
491 * Generates the raw path to an application's data
492 * @hide
493 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700494 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700495 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700496 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800497 }
498
499 /**
500 * Generates the raw path to an application's media
501 * @hide
502 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700503 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700504 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700505 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800506 }
507
508 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800509 * Generates the raw path to an application's OBB files
510 * @hide
511 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700512 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700513 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700514 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800515 }
516
517 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800518 * Generates the path to an application's files.
519 * @hide
520 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700521 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700522 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700523 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800524 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700525
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800526 /**
527 * Generates the path to an application's cache.
528 * @hide
529 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700530 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700531 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700532 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800533 }
534
535 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700536 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 */
538 public static File getDownloadCacheDirectory() {
539 return DOWNLOAD_CACHE_DIRECTORY;
540 }
541
542 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700543 * Unknown storage state, such as when a path isn't backed by known storage
544 * media.
545 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800546 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700547 */
548 public static final String MEDIA_UNKNOWN = "unknown";
549
550 /**
551 * Storage state if the media is not present.
552 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800553 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 */
555 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700558 * Storage state if the media is present but not mounted.
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_UNMOUNTED = "unmounted";
563
564 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700565 * Storage state if the media is present and being disk-checked.
566 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800567 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 */
569 public static final String MEDIA_CHECKING = "checking";
570
571 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700572 * Storage state if the media is present but is blank or is using an
573 * unsupported filesystem.
574 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800575 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 */
577 public static final String MEDIA_NOFS = "nofs";
578
579 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700580 * Storage state if the media is present and mounted at its mount point with
581 * read/write access.
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_MOUNTED = "mounted";
586
587 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700588 * Storage state if the media is present and mounted at its mount point with
589 * read-only access.
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_MOUNTED_READ_ONLY = "mounted_ro";
594
595 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700596 * Storage state if the media is present not mounted, and shared via USB
597 * mass storage.
598 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800599 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 */
601 public static final String MEDIA_SHARED = "shared";
602
603 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700604 * Storage state if the media was removed before it was unmounted.
605 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800606 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 */
608 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
609
610 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700611 * Storage state if the media is present but cannot be mounted. Typically
612 * this happens if the file system on the media is corrupted.
613 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800614 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 */
616 public static final String MEDIA_UNMOUNTABLE = "unmountable";
617
618 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700619 * Storage state if the media is in the process of being ejected.
620 *
621 * @see #getExternalStorageState(File)
622 */
623 public static final String MEDIA_EJECTING = "ejecting";
624
625 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700626 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800627 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700628 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700629 * @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}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 */
635 public static String getExternalStorageState() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700636 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800637 return getExternalStorageState(externalDir);
638 }
639
640 /**
641 * @deprecated use {@link #getExternalStorageState(File)}
642 */
643 @Deprecated
644 public static String getStorageState(File path) {
645 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700646 }
647
648 /**
649 * Returns the current state of the storage device that provides the given
650 * path.
651 *
652 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
653 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
654 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
655 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
656 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
657 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800658 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700659 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800660 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700661 return volume.getState();
662 } else {
663 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666
Dianne Hackborn407f6252010-10-04 11:31:17 -0700667 /**
668 * Returns whether the primary "external" storage device is removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700669 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800670 * @return true if the storage device can be removed (such as an SD card),
671 * or false if the storage device is built in and cannot be
672 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700673 */
674 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800675 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700676 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800677 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700678 }
679
Kenny Roote1ff2142010-10-12 11:20:01 -0700680 /**
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800681 * Returns whether the storage device that provides the given path is
682 * removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800683 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800684 * @return true if the storage device can be removed (such as an SD card),
685 * or false if the storage device is built in and cannot be
686 * physically removed.
687 * @throws IllegalArgumentException if the path is not a valid storage
688 * device.
689 */
690 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700691 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800692 if (volume != null) {
693 return volume.isRemovable();
694 } else {
695 throw new IllegalArgumentException("Failed to find storage device at " + path);
696 }
697 }
698
699 /**
700 * Returns whether the primary "external" storage device is emulated. If
701 * true, data stored on this device will be stored on a portion of the
702 * internal storage system.
Andy Stadler50c294f2011-03-07 19:13:42 -0800703 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800704 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
705 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700706 */
707 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800708 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700709 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800710 return isExternalStorageEmulated(externalDir);
711 }
712
713 /**
714 * Returns whether the storage device that provides the given path is
715 * emulated. If true, data stored on this device will be stored on a portion
716 * of the internal storage system.
717 *
718 * @throws IllegalArgumentException if the path is not a valid storage
719 * device.
720 */
721 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700722 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800723 if (volume != null) {
724 return volume.isEmulated();
725 } else {
726 throw new IllegalArgumentException("Failed to find storage device at " + path);
727 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700728 }
729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 static File getDirectory(String variableName, String defaultPath) {
731 String path = System.getenv(variableName);
732 return path == null ? new File(defaultPath) : new File(path);
733 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700734
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700735 /** {@hide} */
736 public static void setUserRequired(boolean userRequired) {
737 sUserRequired = userRequired;
738 }
739
740 private static void throwIfUserRequired() {
741 if (sUserRequired) {
742 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
743 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700744 }
745 }
746
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700747 /**
748 * Append path segments to each given base path, returning result.
749 *
750 * @hide
751 */
752 public static File[] buildPaths(File[] base, String... segments) {
753 File[] result = new File[base.length];
754 for (int i = 0; i < base.length; i++) {
755 result[i] = buildPath(base[i], segments);
756 }
757 return result;
758 }
759
760 /**
761 * Append path segments to given base path, returning result.
762 *
763 * @hide
764 */
765 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700766 File cur = base;
767 for (String segment : segments) {
768 if (cur == null) {
769 cur = new File(segment);
770 } else {
771 cur = new File(cur, segment);
772 }
773 }
774 return cur;
775 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800776
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800777 private static boolean isStorageDisabled() {
778 return SystemProperties.getBoolean("config.disable_storage", false);
779 }
780
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800781 /**
782 * If the given path exists on emulated external storage, return the
783 * translated backing path hosted on internal storage. This bypasses any
784 * emulation later, improving performance. This is <em>only</em> suitable
785 * for read-only access.
786 * <p>
787 * Returns original path if given path doesn't meet these criteria. Callers
788 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
789 * permission.
790 *
791 * @hide
792 */
793 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700794 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796}