blob: 8d6d9ed567b6d2c0b8fd7f929945cbd48f45357e [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";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060037 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070038 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080039 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080040 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080041 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070042 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070043
Jeff Sharkeydfa45302012-09-12 16:25:22 -070044 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070045 public static final String DIR_ANDROID = "Android";
46 private static final String DIR_DATA = "data";
47 private static final String DIR_MEDIA = "media";
48 private static final String DIR_OBB = "obb";
49 private static final String DIR_FILES = "files";
50 private static final String DIR_CACHE = "cache";
51
52 /** {@hide} */
53 @Deprecated
54 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070055
Jeff Sharkey63d0a062013-03-01 16:12:55 -080056 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070057 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060058 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070059 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080060 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080061 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080062 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070063 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Andreas Gamped281b422016-07-08 03:50:27 +000065 private static UserEnvironment sCurrentUser;
66 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070067
Andreas Gamped281b422016-07-08 03:50:27 +000068 static {
69 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070070 }
71
72 /** {@hide} */
Andreas Gamped281b422016-07-08 03:50:27 +000073 public static void initForCurrentUser() {
74 final int userId = UserHandle.myUserId();
75 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070076 }
77
78 /** {@hide} */
79 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070080 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070081
82 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070083 mUserId = userId;
84 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070085
Jeff Sharkey48877892015-03-18 11:27:19 -070086 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -070087 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
88 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070089 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -070090 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070091 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070092 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070093 return files;
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 getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -070098 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -070099 }
100
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700101 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700102 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700103 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700104 }
105
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700106 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700107 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700108 }
109
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700110 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700111 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700112 }
113
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700114 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700115 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700116 }
117
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700118 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700119 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700120 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700121
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700122 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700123 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700124 }
125
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700126 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700127 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700128 }
129
130 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700131 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700132 }
133
134 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700135 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700136 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700137 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800140 * Return root of the "system" partition holding the core Android OS.
141 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 */
143 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800144 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
146
Jeff Sharkey48877892015-03-18 11:27:19 -0700147 /** {@hide} */
148 public static File getStorageDirectory() {
149 return DIR_ANDROID_STORAGE;
150 }
151
Jason parksa3cdaa52011-01-13 14:15:43 -0600152 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800153 * Return root directory of the "oem" partition holding OEM customizations,
154 * if any. If present, the partition is mounted read-only.
155 *
156 * @hide
157 */
158 public static File getOemDirectory() {
159 return DIR_OEM_ROOT;
160 }
161
162 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800163 * Return root directory of the "odm" partition holding ODM customizations,
164 * if any. If present, the partition is mounted read-only.
165 *
166 * @hide
167 */
168 public static File getOdmDirectory() {
169 return DIR_ODM_ROOT;
170 }
171
172 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700173 * Return root directory of the "vendor" partition that holds vendor-provided
174 * software that should persist across simple reflashing of the "system" partition.
175 * @hide
176 */
177 public static File getVendorDirectory() {
178 return DIR_VENDOR_ROOT;
179 }
180
Jason parksa3cdaa52011-01-13 14:15:43 -0600181 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700182 * Return the system directory for a user. This is for use by system
183 * services to store files relating to the user. This directory will be
184 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700185 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700186 * @deprecated This directory is valid and still exists, but callers should
187 * <em>strongly</em> consider switching to
188 * {@link #getDataSystemCeDirectory(int)} which is protected
189 * with user credentials or
190 * {@link #getDataSystemDeDirectory(int)} which supports fast
191 * user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700192 * @hide
193 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700194 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700195 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800196 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700197 }
198
199 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700200 * Returns the config directory for a user. This is for use by system
201 * services to store files relating to the user which should be readable by
202 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100203 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700204 * @deprecated This directory is valid and still exists, but callers should
205 * <em>strongly</em> consider switching to
206 * {@link #getDataMiscCeDirectory(int)} which is protected with
207 * user credentials or {@link #getDataMiscDeDirectory(int)}
208 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100209 * @hide
210 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700211 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100212 public static File getUserConfigDirectory(int userId) {
213 return new File(new File(new File(
214 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
215 }
216
217 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700218 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 */
220 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800221 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 }
223
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700224 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700225 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700226 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800227 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700228 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700229 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700230 }
231 }
232
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700233 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600234 public static File getExpandDirectory() {
235 return DIR_ANDROID_EXPAND;
236 }
237
238 /** {@hide} */
Jeff Sharkey15447792015-11-05 16:18:51 -0800239 public static File getDataSystemDirectory() {
240 return new File(getDataDirectory(), "system");
241 }
242
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700243 /**
244 * Returns the base directory for per-user system directory, device encrypted.
245 * {@hide}
246 */
247 public static File getDataSystemDeDirectory() {
248 return buildPath(getDataDirectory(), "system_de");
249 }
250
251 /**
252 * Returns the base directory for per-user system directory, credential encrypted.
253 * {@hide}
254 */
255 public static File getDataSystemCeDirectory() {
256 return buildPath(getDataDirectory(), "system_ce");
257 }
258
Jeff Sharkey15447792015-11-05 16:18:51 -0800259 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700260 public static File getDataSystemCeDirectory(int userId) {
261 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800262 }
263
264 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700265 public static File getDataSystemDeDirectory(int userId) {
266 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
267 }
268
269 /** {@hide} */
270 public static File getDataMiscDirectory() {
271 return new File(getDataDirectory(), "misc");
272 }
273
274 /** {@hide} */
275 public static File getDataMiscCeDirectory(int userId) {
276 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
277 }
278
279 /** {@hide} */
280 public static File getDataMiscDeDirectory(int userId) {
281 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800282 }
283
Calin Juravled479b522016-02-24 16:22:03 +0000284 private static File getDataProfilesDeDirectory(int userId) {
285 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
286 }
287
288 /** {@hide} */
Calin Juravle0bd77622016-07-12 15:56:41 +0100289 public static File getReferenceProfile(String packageName) {
290 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
291 }
292
293 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000294 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
295 return buildPath(getDataProfilesDeDirectory(userId), packageName);
296 }
297
298 /** {@hide} */
299 public static File getDataProfilesDeForeignDexDirectory(int userId) {
300 return buildPath(getDataProfilesDeDirectory(userId), "foreign-dex");
301 }
302
Jeff Sharkey15447792015-11-05 16:18:51 -0800303 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700304 public static File getDataAppDirectory(String volumeUuid) {
305 return new File(getDataDirectory(volumeUuid), "app");
306 }
307
308 /** {@hide} */
Todd Kennedy2699f062015-11-20 13:07:17 -0800309 public static File getDataAppEphemeralDirectory(String volumeUuid) {
310 return new File(getDataDirectory(volumeUuid), "app-ephemeral");
311 }
312
313 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700314 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700315 return new File(getDataDirectory(volumeUuid), "user");
316 }
317
318 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700319 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
320 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700321 }
322
323 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700324 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700325 String packageName) {
326 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700327 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800328 }
329
330 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700331 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800332 return new File(getDataDirectory(volumeUuid), "user_de");
333 }
334
335 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700336 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
337 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800338 }
339
340 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700341 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800342 String packageName) {
343 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700344 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700345 }
346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700348 * Return preloads directory.
349 * <p>This directory may contain pre-loaded content such as
350 * {@link #getDataPreloadsDemoDirectory() demo videos} and
351 * {@link #getDataPreloadsAppsDirectory() APK files} .
352 * {@hide}
353 */
354 public static File getDataPreloadsDirectory() {
355 return new File(getDataDirectory(), "preloads");
356 }
357
358 /**
359 * @see #getDataPreloadsDirectory()
360 * {@hide}
361 */
362 public static File getDataPreloadsDemoDirectory() {
363 return new File(getDataPreloadsDirectory(), "demo");
364 }
365
366 /**
367 * @see #getDataPreloadsDirectory()
368 * {@hide}
369 */
370 public static File getDataPreloadsAppsDirectory() {
371 return new File(getDataPreloadsDirectory(), "apps");
372 }
373
374 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700375 * Return the primary shared/external storage directory. This directory may
376 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800377 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700378 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800379 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700380 * <p>
381 * <em>Note: don't be confused by the word "external" here. This directory
382 * can better be thought as media/shared storage. It is a filesystem that
383 * can hold a relatively large amount of data and that is shared across all
384 * applications (does not enforce permissions). Traditionally this is an SD
385 * card, but it may also be implemented as built-in storage in a device that
386 * is distinct from the protected internal storage and can be mounted as a
387 * filesystem on a computer.</em>
388 * <p>
389 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700390 * each user has their own isolated shared storage. Applications only have
391 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700392 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700393 * In devices with multiple shared/external storage directories, this
394 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700395 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700396 * {@link Context#getExternalFilesDirs(String)},
397 * {@link Context#getExternalCacheDirs()}, and
398 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700399 * <p>
400 * Applications should not directly use this top-level directory, in order
401 * to avoid polluting the user's root namespace. Any files that are private
402 * to the application should be placed in a directory returned by
403 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700404 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700405 * if the application is uninstalled. Other shared files should be placed in
406 * one of the directories returned by
407 * {@link #getExternalStoragePublicDirectory}.
408 * <p>
409 * Writing to this path requires the
410 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800411 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700412 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700413 * which is automatically granted if you hold the write permission.
414 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700415 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700416 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700417 * {@link Context#getExternalFilesDir(String)},
418 * {@link Context#getExternalCacheDir()}, or
419 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700420 * read or write.
421 * <p>
422 * This path may change between platform versions, so applications should
423 * only persist relative paths.
424 * <p>
425 * Here is an example of typical code to monitor the state of external
426 * storage:
427 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700428 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800429 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700430 *
431 * @see #getExternalStorageState()
432 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 */
434 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700435 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000436 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700437 }
438
439 /** {@hide} */
440 public static File getLegacyExternalStorageDirectory() {
441 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700444 /** {@hide} */
445 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700446 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700447 }
448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800450 * Standard directory in which to place any audio files that should be
451 * in the regular list of music for the user.
452 * This may be combined with
453 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
454 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
455 * of directories to categories a particular audio file as more than one
456 * type.
457 */
458 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800459
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800460 /**
461 * Standard directory in which to place any audio files that should be
462 * in the list of podcasts that the user can select (not as regular
463 * music).
464 * This may be combined with {@link #DIRECTORY_MUSIC},
465 * {@link #DIRECTORY_NOTIFICATIONS},
466 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
467 * of directories to categories a particular audio file as more than one
468 * type.
469 */
470 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800471
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800472 /**
473 * Standard directory in which to place any audio files that should be
474 * in the list of ringtones that the user can select (not as regular
475 * music).
476 * This may be combined with {@link #DIRECTORY_MUSIC},
477 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
478 * {@link #DIRECTORY_ALARMS} as a series
479 * of directories to categories a particular audio file as more than one
480 * type.
481 */
482 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800483
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800484 /**
485 * Standard directory in which to place any audio files that should be
486 * in the list of alarms that the user can select (not as regular
487 * music).
488 * This may be combined with {@link #DIRECTORY_MUSIC},
489 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
490 * and {@link #DIRECTORY_RINGTONES} as a series
491 * of directories to categories a particular audio file as more than one
492 * type.
493 */
494 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800495
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800496 /**
497 * Standard directory in which to place any audio files that should be
498 * in the list of notifications that the user can select (not as regular
499 * music).
500 * This may be combined with {@link #DIRECTORY_MUSIC},
501 * {@link #DIRECTORY_PODCASTS},
502 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
503 * of directories to categories a particular audio file as more than one
504 * type.
505 */
506 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800507
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800508 /**
509 * Standard directory in which to place pictures that are available to
510 * the user. Note that this is primarily a convention for the top-level
511 * public directory, as the media scanner will find and collect pictures
512 * in any directory.
513 */
514 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800515
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800516 /**
517 * Standard directory in which to place movies that are available to
518 * the user. Note that this is primarily a convention for the top-level
519 * public directory, as the media scanner will find and collect movies
520 * in any directory.
521 */
522 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800523
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800524 /**
525 * Standard directory in which to place files that have been downloaded by
526 * the user. Note that this is primarily a convention for the top-level
527 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700528 * private directories. Also note that though the constant here is
529 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
530 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800531 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700532 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800533
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800534 /**
535 * The traditional location for pictures and videos when mounting the
536 * device as a camera. Note that this is primarily a convention for the
537 * top-level public directory, as this convention makes no sense elsewhere.
538 */
539 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700540
541 /**
542 * Standard directory in which to place documents that have been created by
543 * the user.
544 */
545 public static String DIRECTORY_DOCUMENTS = "Documents";
546
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800547 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800548 * List of standard storage directories.
549 * <p>
550 * Each of its values have its own constant:
551 * <ul>
552 * <li>{@link #DIRECTORY_MUSIC}
553 * <li>{@link #DIRECTORY_PODCASTS}
554 * <li>{@link #DIRECTORY_ALARMS}
555 * <li>{@link #DIRECTORY_RINGTONES}
556 * <li>{@link #DIRECTORY_NOTIFICATIONS}
557 * <li>{@link #DIRECTORY_PICTURES}
558 * <li>{@link #DIRECTORY_MOVIES}
559 * <li>{@link #DIRECTORY_DOWNLOADS}
560 * <li>{@link #DIRECTORY_DCIM}
561 * <li>{@link #DIRECTORY_DOCUMENTS}
562 * </ul>
563 * @hide
564 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800565 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800566 DIRECTORY_MUSIC,
567 DIRECTORY_PODCASTS,
568 DIRECTORY_RINGTONES,
569 DIRECTORY_ALARMS,
570 DIRECTORY_NOTIFICATIONS,
571 DIRECTORY_PICTURES,
572 DIRECTORY_MOVIES,
573 DIRECTORY_DOWNLOADS,
574 DIRECTORY_DCIM,
Steve McKayab3b8932016-02-16 11:37:03 -0800575 DIRECTORY_DOCUMENTS
Felipe Lemec7b1f892016-01-15 15:02:31 -0800576 };
577
578 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800579 * @hide
580 */
581 public static boolean isStandardDirectory(String dir) {
582 for (String valid : STANDARD_DIRECTORIES) {
583 if (valid.equals(dir)) {
584 return true;
585 }
586 }
587 return false;
588 }
589
590 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700591 * Get a top-level shared/external storage directory for placing files of a
592 * particular type. This is where the user will typically place and manage
593 * their own files, so you should be careful about what you put here to
594 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800595 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700596 * <p>
597 * On devices with multiple users (as described by {@link UserManager}),
598 * each user has their own isolated shared storage. Applications only have
599 * access to the shared storage for the user they're running as.
600 * </p>
601 * <p>
602 * Here is an example of typical code to manipulate a picture on the public
603 * shared storage:
604 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800605 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
606 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800607 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700608 * @param type The type of storage directory to return. Should be one of
609 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
610 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
611 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800612 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
613 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700614 * @return Returns the File path for the directory. Note that this directory
615 * may not yet exist, so you must make sure it exists before using
616 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800617 */
618 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700619 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000620 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800621 }
622
623 /**
624 * Returns the path for android-specific data on the SD card.
625 * @hide
626 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700627 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700628 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000629 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800630 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700631
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800632 /**
633 * Generates the raw path to an application's data
634 * @hide
635 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700636 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700637 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000638 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800639 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800640
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800641 /**
642 * Generates the raw path to an application's media
643 * @hide
644 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700645 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700646 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000647 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800648 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800649
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800650 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800651 * Generates the raw path to an application's OBB files
652 * @hide
653 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700654 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700655 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000656 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800657 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800658
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800659 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800660 * Generates the path to an application's files.
661 * @hide
662 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700663 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700664 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000665 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800666 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700667
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800668 /**
669 * Generates the path to an application's cache.
670 * @hide
671 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700672 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700673 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000674 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800675 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800676
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800677 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700678 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
680 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800681 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683
684 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700685 * Unknown storage state, such as when a path isn't backed by known storage
686 * media.
687 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800688 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700689 */
690 public static final String MEDIA_UNKNOWN = "unknown";
691
692 /**
693 * Storage state if the media is not present.
694 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800695 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 */
697 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700700 * Storage state if the media is present but not mounted.
701 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800702 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 */
704 public static final String MEDIA_UNMOUNTED = "unmounted";
705
706 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700707 * Storage state if the media is present and being disk-checked.
708 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800709 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 */
711 public static final String MEDIA_CHECKING = "checking";
712
713 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700714 * Storage state if the media is present but is blank or is using an
715 * unsupported filesystem.
716 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800717 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 */
719 public static final String MEDIA_NOFS = "nofs";
720
721 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700722 * Storage state if the media is present and mounted at its mount point with
723 * read/write access.
724 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800725 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 */
727 public static final String MEDIA_MOUNTED = "mounted";
728
729 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700730 * Storage state if the media is present and mounted at its mount point with
731 * read-only access.
732 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800733 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 */
735 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
736
737 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700738 * Storage state if the media is present not mounted, and shared via USB
739 * mass storage.
740 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800741 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 */
743 public static final String MEDIA_SHARED = "shared";
744
745 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700746 * Storage state if the media was removed before it was unmounted.
747 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800748 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 */
750 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
751
752 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700753 * Storage state if the media is present but cannot be mounted. Typically
754 * this happens if the file system on the media is corrupted.
755 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800756 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 */
758 public static final String MEDIA_UNMOUNTABLE = "unmountable";
759
760 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700761 * Storage state if the media is in the process of being ejected.
762 *
763 * @see #getExternalStorageState(File)
764 */
765 public static final String MEDIA_EJECTING = "ejecting";
766
767 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700768 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800769 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700770 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700771 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
772 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
773 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
774 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
775 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 */
777 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +0000778 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800779 return getExternalStorageState(externalDir);
780 }
781
782 /**
783 * @deprecated use {@link #getExternalStorageState(File)}
784 */
785 @Deprecated
786 public static String getStorageState(File path) {
787 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700788 }
789
790 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700791 * Returns the current state of the shared/external storage media at the
792 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700793 *
794 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
795 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
796 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
797 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
798 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
799 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800800 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700801 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800802 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700803 return volume.getState();
804 } else {
805 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 }
808
Dianne Hackborn407f6252010-10-04 11:31:17 -0700809 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700810 * Returns whether the primary shared/external storage media is physically
811 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700812 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800813 * @return true if the storage device can be removed (such as an SD card),
814 * or false if the storage device is built in and cannot be
815 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700816 */
817 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800818 if (isStorageDisabled()) return false;
Andreas Gamped281b422016-07-08 03:50:27 +0000819 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800820 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700821 }
822
Kenny Roote1ff2142010-10-12 11:20:01 -0700823 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700824 * Returns whether the shared/external storage media at the given path is
825 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800826 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800827 * @return true if the storage device can be removed (such as an SD card),
828 * or false if the storage device is built in and cannot be
829 * physically removed.
830 * @throws IllegalArgumentException if the path is not a valid storage
831 * device.
832 */
833 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700834 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800835 if (volume != null) {
836 return volume.isRemovable();
837 } else {
838 throw new IllegalArgumentException("Failed to find storage device at " + path);
839 }
840 }
841
842 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700843 * Returns whether the primary shared/external storage media is emulated.
844 * <p>
845 * The contents of emulated storage devices are backed by a private user
846 * data partition, which means there is little benefit to apps storing data
847 * here instead of the private directories returned by
848 * {@link Context#getFilesDir()}, etc.
849 * <p>
850 * This returns true when emulated storage is backed by either internal
851 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -0800852 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800853 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
854 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700855 */
856 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800857 if (isStorageDisabled()) return false;
Andreas Gamped281b422016-07-08 03:50:27 +0000858 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800859 return isExternalStorageEmulated(externalDir);
860 }
861
862 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700863 * Returns whether the shared/external storage media at the given path is
864 * emulated.
865 * <p>
866 * The contents of emulated storage devices are backed by a private user
867 * data partition, which means there is little benefit to apps storing data
868 * here instead of the private directories returned by
869 * {@link Context#getFilesDir()}, etc.
870 * <p>
871 * This returns true when emulated storage is backed by either internal
872 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800873 *
874 * @throws IllegalArgumentException if the path is not a valid storage
875 * device.
876 */
877 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700878 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800879 if (volume != null) {
880 return volume.isEmulated();
881 } else {
882 throw new IllegalArgumentException("Failed to find storage device at " + path);
883 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700884 }
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 static File getDirectory(String variableName, String defaultPath) {
887 String path = System.getenv(variableName);
888 return path == null ? new File(defaultPath) : new File(path);
889 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700890
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700891 /** {@hide} */
892 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +0000893 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700894 }
895
896 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +0000897 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700898 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
899 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700900 }
901 }
902
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700903 /**
904 * Append path segments to each given base path, returning result.
905 *
906 * @hide
907 */
908 public static File[] buildPaths(File[] base, String... segments) {
909 File[] result = new File[base.length];
910 for (int i = 0; i < base.length; i++) {
911 result[i] = buildPath(base[i], segments);
912 }
913 return result;
914 }
915
916 /**
917 * Append path segments to given base path, returning result.
918 *
919 * @hide
920 */
921 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700922 File cur = base;
923 for (String segment : segments) {
924 if (cur == null) {
925 cur = new File(segment);
926 } else {
927 cur = new File(cur, segment);
928 }
929 }
930 return cur;
931 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800932
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800933 private static boolean isStorageDisabled() {
934 return SystemProperties.getBoolean("config.disable_storage", false);
935 }
936
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800937 /**
938 * If the given path exists on emulated external storage, return the
939 * translated backing path hosted on internal storage. This bypasses any
940 * emulation later, improving performance. This is <em>only</em> suitable
941 * for read-only access.
942 * <p>
943 * Returns original path if given path doesn't meet these criteria. Callers
944 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
945 * permission.
946 *
947 * @hide
948 */
949 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700950 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952}