blob: a236300a56cf8783c7bc88b981dc19e05dc7a980 [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 Sharkey70eb34a2018-09-13 11:57:03 -060019import android.annotation.SystemApi;
Philip P. Moltmannf80809f2018-04-04 11:20:44 -070020import android.annotation.TestApi;
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080021import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070022import android.content.Context;
Jeff Sharkey48877892015-03-18 11:27:19 -070023import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070024import android.os.storage.StorageVolume;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070025import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080026import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080027
Gilles Debunneee1d6302011-05-13 10:09:32 -070028import java.io.File;
Jeff Sharkey20356142017-12-06 15:22:05 -070029import java.util.LinkedList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031/**
32 * Provides access to environment variables.
33 */
34public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080035 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Jeff Sharkeydd02e332018-06-27 14:41:57 -060037 // NOTE: keep credential-protected paths in sync with StrictMode.java
38
Jeff Sharkeyb049e212012-09-07 23:16:01 -070039 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080040 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070041 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060042 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070043 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080044 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080045 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080046 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070047 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090048 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Dario Freni2bef1762018-06-01 14:02:08 +010049 private static final String ENV_PRODUCT_SERVICES_ROOT = "PRODUCT_SERVICES_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070050
Jeff Sharkeydfa45302012-09-12 16:25:22 -070051 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070052 public static final String DIR_ANDROID = "Android";
53 private static final String DIR_DATA = "data";
54 private static final String DIR_MEDIA = "media";
55 private static final String DIR_OBB = "obb";
56 private static final String DIR_FILES = "files";
57 private static final String DIR_CACHE = "cache";
58
59 /** {@hide} */
60 @Deprecated
61 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070062
Jeff Sharkey63d0a062013-03-01 16:12:55 -080063 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070064 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060065 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070066 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080067 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080068 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080069 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070070 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090071 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Dario Freni2bef1762018-06-01 14:02:08 +010072 private static final File DIR_PRODUCT_SERVICES_ROOT = getDirectory(ENV_PRODUCT_SERVICES_ROOT,
73 "/product_services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
Andreas Gamped281b422016-07-08 03:50:27 +000075 private static UserEnvironment sCurrentUser;
76 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070077
Andreas Gamped281b422016-07-08 03:50:27 +000078 static {
79 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070080 }
81
82 /** {@hide} */
Andreas Gamped281b422016-07-08 03:50:27 +000083 public static void initForCurrentUser() {
84 final int userId = UserHandle.myUserId();
85 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070086 }
87
88 /** {@hide} */
89 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070090 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070091
92 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070093 mUserId = userId;
94 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070095
Jeff Sharkey48877892015-03-18 11:27:19 -070096 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -070097 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
98 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070099 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700100 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700101 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700102 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700103 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700104 }
105
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700106 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700107 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700108 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700109 }
110
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700111 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700112 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700113 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114 }
115
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700116 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700117 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700118 }
119
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700120 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700121 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700122 }
123
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700124 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700125 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700126 }
127
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700128 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700129 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700130 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700131
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700132 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700133 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700134 }
135
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700136 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700137 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700138 }
139
140 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700141 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700142 }
143
144 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700145 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700146 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700147 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800150 * Return root of the "system" partition holding the core Android OS.
151 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
153 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800154 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
156
Jeff Sharkey48877892015-03-18 11:27:19 -0700157 /** {@hide} */
Jeff Sharkeyc6091162018-06-29 17:15:40 -0600158 @TestApi
Jeff Sharkey48877892015-03-18 11:27:19 -0700159 public static File getStorageDirectory() {
160 return DIR_ANDROID_STORAGE;
161 }
162
Jason parksa3cdaa52011-01-13 14:15:43 -0600163 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800164 * Return root directory of the "oem" partition holding OEM customizations,
165 * if any. If present, the partition is mounted read-only.
166 *
167 * @hide
168 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600169 @SystemApi
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800170 public static File getOemDirectory() {
171 return DIR_OEM_ROOT;
172 }
173
174 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800175 * Return root directory of the "odm" partition holding ODM customizations,
176 * if any. If present, the partition is mounted read-only.
177 *
178 * @hide
179 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600180 @SystemApi
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800181 public static File getOdmDirectory() {
182 return DIR_ODM_ROOT;
183 }
184
185 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700186 * Return root directory of the "vendor" partition that holds vendor-provided
187 * software that should persist across simple reflashing of the "system" partition.
188 * @hide
189 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600190 @SystemApi
Christopher Tate740888f2014-04-18 12:24:57 -0700191 public static File getVendorDirectory() {
192 return DIR_VENDOR_ROOT;
193 }
194
Jason parksa3cdaa52011-01-13 14:15:43 -0600195 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900196 * Return root directory of the "product" partition holding product-specific
197 * customizations if any. If present, the partition is mounted read-only.
198 *
199 * @hide
200 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600201 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000202 @TestApi
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900203 public static File getProductDirectory() {
204 return DIR_PRODUCT_ROOT;
205 }
206
207 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100208 * Return root directory of the "product_services" partition holding middleware
209 * services if any. If present, the partition is mounted read-only.
210 *
211 * @hide
212 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600213 @SystemApi
Dario Freni2bef1762018-06-01 14:02:08 +0100214 public static File getProductServicesDirectory() {
215 return DIR_PRODUCT_SERVICES_ROOT;
216 }
217
218 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700219 * Return the system directory for a user. This is for use by system
220 * services to store files relating to the user. This directory will be
221 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700222 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600223 * @deprecated This directory is valid and still exists, but but callers
224 * should <em>strongly</em> consider switching to using either
225 * {@link #getDataSystemCeDirectory(int)} or
226 * {@link #getDataSystemDeDirectory(int)}, both of which support
227 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700228 * @hide
229 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700230 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700231 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800232 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700233 }
234
235 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700236 * Returns the config directory for a user. This is for use by system
237 * services to store files relating to the user which should be readable by
238 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100239 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700240 * @deprecated This directory is valid and still exists, but callers should
241 * <em>strongly</em> consider switching to
242 * {@link #getDataMiscCeDirectory(int)} which is protected with
243 * user credentials or {@link #getDataMiscDeDirectory(int)}
244 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100245 * @hide
246 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700247 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100248 public static File getUserConfigDirectory(int userId) {
249 return new File(new File(new File(
250 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
251 }
252
253 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700254 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 */
256 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800257 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
259
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700260 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700261 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700262 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800263 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700264 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700265 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700266 }
267 }
268
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700269 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600270 public static File getExpandDirectory() {
271 return DIR_ANDROID_EXPAND;
272 }
273
274 /** {@hide} */
Jeff Sharkey15447792015-11-05 16:18:51 -0800275 public static File getDataSystemDirectory() {
276 return new File(getDataDirectory(), "system");
277 }
278
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700279 /**
280 * Returns the base directory for per-user system directory, device encrypted.
281 * {@hide}
282 */
283 public static File getDataSystemDeDirectory() {
284 return buildPath(getDataDirectory(), "system_de");
285 }
286
287 /**
288 * Returns the base directory for per-user system directory, credential encrypted.
289 * {@hide}
290 */
291 public static File getDataSystemCeDirectory() {
292 return buildPath(getDataDirectory(), "system_ce");
293 }
294
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600295 /**
296 * Return the "credential encrypted" system directory for a user. This is
297 * for use by system services to store files relating to the user. This
298 * directory supports fast user wipe, and will be automatically deleted when
299 * the user is removed.
300 * <p>
301 * Data stored under this path is "credential encrypted", which uses an
302 * encryption key that is entangled with user credentials, such as a PIN or
303 * password. The contents will only be available once the user has been
304 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
305 * <p>
306 * New code should <em>strongly</em> prefer storing sensitive data in these
307 * credential encrypted areas.
308 *
309 * @hide
310 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700311 public static File getDataSystemCeDirectory(int userId) {
312 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800313 }
314
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600315 /**
316 * Return the "device encrypted" system directory for a user. This is for
317 * use by system services to store files relating to the user. This
318 * directory supports fast user wipe, and will be automatically deleted when
319 * the user is removed.
320 * <p>
321 * Data stored under this path is "device encrypted", which uses an
322 * encryption key that is tied to the physical device. The contents will
323 * only be available once the device has finished a {@code dm-verity}
324 * protected boot.
325 * <p>
326 * New code should <em>strongly</em> avoid storing sensitive data in these
327 * device encrypted areas.
328 *
329 * @hide
330 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700331 public static File getDataSystemDeDirectory(int userId) {
332 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
333 }
334
335 /** {@hide} */
336 public static File getDataMiscDirectory() {
337 return new File(getDataDirectory(), "misc");
338 }
339
340 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800341 public static File getDataMiscCeDirectory() {
342 return buildPath(getDataDirectory(), "misc_ce");
343 }
344
345 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700346 public static File getDataMiscCeDirectory(int userId) {
347 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
348 }
349
350 /** {@hide} */
351 public static File getDataMiscDeDirectory(int userId) {
352 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800353 }
354
Calin Juravled479b522016-02-24 16:22:03 +0000355 private static File getDataProfilesDeDirectory(int userId) {
356 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
357 }
358
359 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800360 public static File getDataVendorCeDirectory(int userId) {
361 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
362 }
363
364 /** {@hide} */
365 public static File getDataVendorDeDirectory(int userId) {
366 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
367 }
368
369 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800370 public static File getDataRefProfilesDePackageDirectory(String packageName) {
371 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100372 }
373
374 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000375 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
376 return buildPath(getDataProfilesDeDirectory(userId), packageName);
377 }
378
379 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700380 public static File getDataAppDirectory(String volumeUuid) {
381 return new File(getDataDirectory(volumeUuid), "app");
382 }
383
384 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000385 public static File getDataStagingDirectory(String volumeUuid) {
386 return new File(getDataDirectory(volumeUuid), "staging");
387 }
388
389 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700390 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700391 return new File(getDataDirectory(volumeUuid), "user");
392 }
393
394 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700395 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
396 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700397 }
398
399 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700400 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700401 String packageName) {
402 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700403 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800404 }
405
406 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700407 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800408 return new File(getDataDirectory(volumeUuid), "user_de");
409 }
410
411 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700412 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
413 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800414 }
415
416 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700417 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800418 String packageName) {
419 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700420 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700421 }
422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700424 * Return preloads directory.
425 * <p>This directory may contain pre-loaded content such as
426 * {@link #getDataPreloadsDemoDirectory() demo videos} and
427 * {@link #getDataPreloadsAppsDirectory() APK files} .
428 * {@hide}
429 */
430 public static File getDataPreloadsDirectory() {
431 return new File(getDataDirectory(), "preloads");
432 }
433
434 /**
435 * @see #getDataPreloadsDirectory()
436 * {@hide}
437 */
438 public static File getDataPreloadsDemoDirectory() {
439 return new File(getDataPreloadsDirectory(), "demo");
440 }
441
442 /**
443 * @see #getDataPreloadsDirectory()
444 * {@hide}
445 */
446 public static File getDataPreloadsAppsDirectory() {
447 return new File(getDataPreloadsDirectory(), "apps");
448 }
449
450 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700451 * @see #getDataPreloadsDirectory()
452 * {@hide}
453 */
454 public static File getDataPreloadsMediaDirectory() {
455 return new File(getDataPreloadsDirectory(), "media");
456 }
457
458 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700459 * Returns location of preloaded cache directory for package name
460 * @see #getDataPreloadsDirectory()
461 * {@hide}
462 */
463 public static File getDataPreloadsFileCacheDirectory(String packageName) {
464 return new File(getDataPreloadsFileCacheDirectory(), packageName);
465 }
466
467 /**
468 * Returns location of preloaded cache directory.
469 * @see #getDataPreloadsDirectory()
470 * {@hide}
471 */
472 public static File getDataPreloadsFileCacheDirectory() {
473 return new File(getDataPreloadsDirectory(), "file_cache");
474 }
475
476 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800477 * Returns location of packages cache directory.
478 * {@hide}
479 */
480 public static File getPackageCacheDirectory() {
481 return new File(getDataSystemDirectory(), "package_cache");
482 }
483
484 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700485 * Return the primary shared/external storage directory. This directory may
486 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800487 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700488 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800489 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700490 * <p>
491 * <em>Note: don't be confused by the word "external" here. This directory
492 * can better be thought as media/shared storage. It is a filesystem that
493 * can hold a relatively large amount of data and that is shared across all
494 * applications (does not enforce permissions). Traditionally this is an SD
495 * card, but it may also be implemented as built-in storage in a device that
496 * is distinct from the protected internal storage and can be mounted as a
497 * filesystem on a computer.</em>
498 * <p>
499 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700500 * each user has their own isolated shared storage. Applications only have
501 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700502 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700503 * In devices with multiple shared/external storage directories, this
504 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700505 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700506 * {@link Context#getExternalFilesDirs(String)},
507 * {@link Context#getExternalCacheDirs()}, and
508 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700509 * <p>
510 * Applications should not directly use this top-level directory, in order
511 * to avoid polluting the user's root namespace. Any files that are private
512 * to the application should be placed in a directory returned by
513 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700514 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700515 * if the application is uninstalled. Other shared files should be placed in
516 * one of the directories returned by
517 * {@link #getExternalStoragePublicDirectory}.
518 * <p>
519 * Writing to this path requires the
520 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800521 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700522 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700523 * which is automatically granted if you hold the write permission.
524 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700525 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700526 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700527 * {@link Context#getExternalFilesDir(String)},
528 * {@link Context#getExternalCacheDir()}, or
529 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700530 * read or write.
531 * <p>
532 * This path may change between platform versions, so applications should
533 * only persist relative paths.
534 * <p>
535 * Here is an example of typical code to monitor the state of external
536 * storage:
537 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700538 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800539 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700540 *
541 * @see #getExternalStorageState()
542 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 */
544 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700545 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000546 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700547 }
548
549 /** {@hide} */
550 public static File getLegacyExternalStorageDirectory() {
551 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700554 /** {@hide} */
555 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700556 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700557 }
558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800560 * Standard directory in which to place any audio files that should be
561 * in the regular list of music for the user.
562 * This may be combined with
563 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
564 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
565 * of directories to categories a particular audio file as more than one
566 * type.
567 */
568 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800569
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800570 /**
571 * Standard directory in which to place any audio files that should be
572 * in the list of podcasts that the user can select (not as regular
573 * music).
574 * This may be combined with {@link #DIRECTORY_MUSIC},
575 * {@link #DIRECTORY_NOTIFICATIONS},
576 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
577 * of directories to categories a particular audio file as more than one
578 * type.
579 */
580 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800581
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800582 /**
583 * Standard directory in which to place any audio files that should be
584 * in the list of ringtones that the user can select (not as regular
585 * music).
586 * This may be combined with {@link #DIRECTORY_MUSIC},
587 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
588 * {@link #DIRECTORY_ALARMS} as a series
589 * of directories to categories a particular audio file as more than one
590 * type.
591 */
592 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800593
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800594 /**
595 * Standard directory in which to place any audio files that should be
596 * in the list of alarms that the user can select (not as regular
597 * music).
598 * This may be combined with {@link #DIRECTORY_MUSIC},
599 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
600 * and {@link #DIRECTORY_RINGTONES} as a series
601 * of directories to categories a particular audio file as more than one
602 * type.
603 */
604 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800605
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800606 /**
607 * Standard directory in which to place any audio files that should be
608 * in the list of notifications that the user can select (not as regular
609 * music).
610 * This may be combined with {@link #DIRECTORY_MUSIC},
611 * {@link #DIRECTORY_PODCASTS},
612 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
613 * of directories to categories a particular audio file as more than one
614 * type.
615 */
616 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800617
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800618 /**
619 * Standard directory in which to place pictures that are available to
620 * the user. Note that this is primarily a convention for the top-level
621 * public directory, as the media scanner will find and collect pictures
622 * in any directory.
623 */
624 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800625
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800626 /**
627 * Standard directory in which to place movies that are available to
628 * the user. Note that this is primarily a convention for the top-level
629 * public directory, as the media scanner will find and collect movies
630 * in any directory.
631 */
632 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800633
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800634 /**
635 * Standard directory in which to place files that have been downloaded by
636 * the user. Note that this is primarily a convention for the top-level
637 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700638 * private directories. Also note that though the constant here is
639 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
640 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800641 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700642 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800643
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800644 /**
645 * The traditional location for pictures and videos when mounting the
646 * device as a camera. Note that this is primarily a convention for the
647 * top-level public directory, as this convention makes no sense elsewhere.
648 */
649 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700650
651 /**
652 * Standard directory in which to place documents that have been created by
653 * the user.
654 */
655 public static String DIRECTORY_DOCUMENTS = "Documents";
656
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800657 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600658 * Standard directory in which to place screenshots that have been taken by
659 * the user. Typically used as a secondary directory under
660 * {@link #DIRECTORY_PICTURES}.
661 */
662 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
663
664 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700665 * Standard directory in which to place any audio files which are
666 * audiobooks.
667 */
668 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
669
670 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800671 * List of standard storage directories.
672 * <p>
673 * Each of its values have its own constant:
674 * <ul>
675 * <li>{@link #DIRECTORY_MUSIC}
676 * <li>{@link #DIRECTORY_PODCASTS}
677 * <li>{@link #DIRECTORY_ALARMS}
678 * <li>{@link #DIRECTORY_RINGTONES}
679 * <li>{@link #DIRECTORY_NOTIFICATIONS}
680 * <li>{@link #DIRECTORY_PICTURES}
681 * <li>{@link #DIRECTORY_MOVIES}
682 * <li>{@link #DIRECTORY_DOWNLOADS}
683 * <li>{@link #DIRECTORY_DCIM}
684 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700685 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800686 * </ul>
687 * @hide
688 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800689 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800690 DIRECTORY_MUSIC,
691 DIRECTORY_PODCASTS,
692 DIRECTORY_RINGTONES,
693 DIRECTORY_ALARMS,
694 DIRECTORY_NOTIFICATIONS,
695 DIRECTORY_PICTURES,
696 DIRECTORY_MOVIES,
697 DIRECTORY_DOWNLOADS,
698 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700699 DIRECTORY_DOCUMENTS,
700 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800701 };
702
703 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800704 * @hide
705 */
706 public static boolean isStandardDirectory(String dir) {
707 for (String valid : STANDARD_DIRECTORIES) {
708 if (valid.equals(dir)) {
709 return true;
710 }
711 }
712 return false;
713 }
714
Jeff Sharkey20356142017-12-06 15:22:05 -0700715 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
716 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
717 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
718 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
719 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
720 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
721 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
722 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
723 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
724 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700725 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700726
727 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
728 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
729
730 /**
731 * Classify the content types present on the given external storage device.
732 * <p>
733 * This is typically useful for deciding if an inserted SD card is empty, or
734 * if it contains content like photos that should be preserved.
735 *
736 * @hide
737 */
738 public static int classifyExternalStorageDirectory(File dir) {
739 int res = 0;
740 for (File f : FileUtils.listFilesOrEmpty(dir)) {
741 if (f.isFile() && isInterestingFile(f)) {
742 res |= HAS_OTHER;
743 } else if (f.isDirectory() && hasInterestingFiles(f)) {
744 final String name = f.getName();
745 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
746 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
747 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
748 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
749 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
750 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
751 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
752 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
753 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
754 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700755 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700756 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
757 else res |= HAS_OTHER;
758 }
759 }
760 return res;
761 }
762
763 private static boolean hasInterestingFiles(File dir) {
764 final LinkedList<File> explore = new LinkedList<>();
765 explore.add(dir);
766 while (!explore.isEmpty()) {
767 dir = explore.pop();
768 for (File f : FileUtils.listFilesOrEmpty(dir)) {
769 if (isInterestingFile(f)) return true;
770 if (f.isDirectory()) explore.add(f);
771 }
772 }
773 return false;
774 }
775
776 private static boolean isInterestingFile(File file) {
777 if (file.isFile()) {
778 final String name = file.getName().toLowerCase();
779 if (name.endsWith(".exe") || name.equals("autorun.inf")
780 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
781 return false;
782 } else {
783 return true;
784 }
785 } else {
786 return false;
787 }
788 }
789
Felipe Lemeb012f912016-01-22 16:49:55 -0800790 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700791 * Get a top-level shared/external storage directory for placing files of a
792 * particular type. This is where the user will typically place and manage
793 * their own files, so you should be careful about what you put here to
794 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800795 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700796 * <p>
797 * On devices with multiple users (as described by {@link UserManager}),
798 * each user has their own isolated shared storage. Applications only have
799 * access to the shared storage for the user they're running as.
800 * </p>
801 * <p>
802 * Here is an example of typical code to manipulate a picture on the public
803 * shared storage:
804 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800805 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
806 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800807 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700808 * @param type The type of storage directory to return. Should be one of
809 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
810 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
811 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800812 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
813 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700814 * @return Returns the File path for the directory. Note that this directory
815 * may not yet exist, so you must make sure it exists before using
816 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800817 */
818 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700819 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000820 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800821 }
822
823 /**
824 * Returns the path for android-specific data on the SD card.
825 * @hide
826 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700827 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700828 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000829 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800830 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700831
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800832 /**
833 * Generates the raw path to an application's data
834 * @hide
835 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700836 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700837 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000838 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800839 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800840
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800841 /**
842 * Generates the raw path to an application's media
843 * @hide
844 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700845 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700846 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000847 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800848 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800849
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800850 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800851 * Generates the raw path to an application's OBB files
852 * @hide
853 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700854 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700855 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000856 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800857 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800858
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800859 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800860 * Generates the path to an application's files.
861 * @hide
862 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700863 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700864 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000865 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800866 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700867
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800868 /**
869 * Generates the path to an application's cache.
870 * @hide
871 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700872 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700873 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000874 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800875 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800876
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800877 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700878 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 */
880 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800881 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883
884 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700885 * Unknown storage state, such as when a path isn't backed by known storage
886 * media.
887 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800888 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700889 */
890 public static final String MEDIA_UNKNOWN = "unknown";
891
892 /**
893 * Storage state if the media is not present.
894 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800895 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 */
897 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700900 * Storage state if the media is present but not mounted.
901 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800902 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 */
904 public static final String MEDIA_UNMOUNTED = "unmounted";
905
906 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700907 * Storage state if the media is present and being disk-checked.
908 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800909 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 */
911 public static final String MEDIA_CHECKING = "checking";
912
913 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700914 * Storage state if the media is present but is blank or is using an
915 * unsupported filesystem.
916 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800917 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 */
919 public static final String MEDIA_NOFS = "nofs";
920
921 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700922 * Storage state if the media is present and mounted at its mount point with
923 * read/write access.
924 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800925 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 */
927 public static final String MEDIA_MOUNTED = "mounted";
928
929 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700930 * Storage state if the media is present and mounted at its mount point with
931 * read-only access.
932 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800933 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 */
935 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
936
937 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700938 * Storage state if the media is present not mounted, and shared via USB
939 * mass storage.
940 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800941 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 */
943 public static final String MEDIA_SHARED = "shared";
944
945 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700946 * Storage state if the media was removed before it was unmounted.
947 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800948 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 */
950 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
951
952 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700953 * Storage state if the media is present but cannot be mounted. Typically
954 * this happens if the file system on the media is corrupted.
955 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800956 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 */
958 public static final String MEDIA_UNMOUNTABLE = "unmountable";
959
960 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700961 * Storage state if the media is in the process of being ejected.
962 *
963 * @see #getExternalStorageState(File)
964 */
965 public static final String MEDIA_EJECTING = "ejecting";
966
967 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700968 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800969 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700970 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700971 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
972 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
973 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
974 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
975 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 */
977 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +0000978 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800979 return getExternalStorageState(externalDir);
980 }
981
982 /**
983 * @deprecated use {@link #getExternalStorageState(File)}
984 */
985 @Deprecated
986 public static String getStorageState(File path) {
987 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700988 }
989
990 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700991 * Returns the current state of the shared/external storage media at the
992 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700993 *
994 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
995 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
996 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
997 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
998 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
999 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001000 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001001 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001002 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001003 return volume.getState();
1004 } else {
1005 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
1008
Dianne Hackborn407f6252010-10-04 11:31:17 -07001009 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001010 * Returns whether the primary shared/external storage media is physically
1011 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001012 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001013 * @return true if the storage device can be removed (such as an SD card),
1014 * or false if the storage device is built in and cannot be
1015 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001016 */
1017 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001018 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001019 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001020 }
1021
Kenny Roote1ff2142010-10-12 11:20:01 -07001022 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001023 * Returns whether the shared/external storage media at the given path is
1024 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001025 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001026 * @return true if the storage device can be removed (such as an SD card),
1027 * or false if the storage device is built in and cannot be
1028 * physically removed.
1029 * @throws IllegalArgumentException if the path is not a valid storage
1030 * device.
1031 */
1032 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001033 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001034 if (volume != null) {
1035 return volume.isRemovable();
1036 } else {
1037 throw new IllegalArgumentException("Failed to find storage device at " + path);
1038 }
1039 }
1040
1041 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001042 * Returns whether the primary shared/external storage media is emulated.
1043 * <p>
1044 * The contents of emulated storage devices are backed by a private user
1045 * data partition, which means there is little benefit to apps storing data
1046 * here instead of the private directories returned by
1047 * {@link Context#getFilesDir()}, etc.
1048 * <p>
1049 * This returns true when emulated storage is backed by either internal
1050 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001051 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001052 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1053 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001054 */
1055 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001056 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001057 return isExternalStorageEmulated(externalDir);
1058 }
1059
1060 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001061 * Returns whether the shared/external storage media at the given path is
1062 * emulated.
1063 * <p>
1064 * The contents of emulated storage devices are backed by a private user
1065 * data partition, which means there is little benefit to apps storing data
1066 * here instead of the private directories returned by
1067 * {@link Context#getFilesDir()}, etc.
1068 * <p>
1069 * This returns true when emulated storage is backed by either internal
1070 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001071 *
1072 * @throws IllegalArgumentException if the path is not a valid storage
1073 * device.
1074 */
1075 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001076 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001077 if (volume != null) {
1078 return volume.isEmulated();
1079 } else {
1080 throw new IllegalArgumentException("Failed to find storage device at " + path);
1081 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001082 }
1083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 static File getDirectory(String variableName, String defaultPath) {
1085 String path = System.getenv(variableName);
1086 return path == null ? new File(defaultPath) : new File(path);
1087 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001088
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001089 /** {@hide} */
1090 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001091 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001092 }
1093
1094 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001095 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001096 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1097 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001098 }
1099 }
1100
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001101 /**
1102 * Append path segments to each given base path, returning result.
1103 *
1104 * @hide
1105 */
1106 public static File[] buildPaths(File[] base, String... segments) {
1107 File[] result = new File[base.length];
1108 for (int i = 0; i < base.length; i++) {
1109 result[i] = buildPath(base[i], segments);
1110 }
1111 return result;
1112 }
1113
1114 /**
1115 * Append path segments to given base path, returning result.
1116 *
1117 * @hide
1118 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001119 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001120 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001121 File cur = base;
1122 for (String segment : segments) {
1123 if (cur == null) {
1124 cur = new File(segment);
1125 } else {
1126 cur = new File(cur, segment);
1127 }
1128 }
1129 return cur;
1130 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001131
1132 /**
1133 * If the given path exists on emulated external storage, return the
1134 * translated backing path hosted on internal storage. This bypasses any
1135 * emulation later, improving performance. This is <em>only</em> suitable
1136 * for read-only access.
1137 * <p>
1138 * Returns original path if given path doesn't meet these criteria. Callers
1139 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1140 * permission.
1141 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001142 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001143 * @hide
1144 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001145 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001146 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001147 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149}