blob: aed6bc9a63fc14a984932f38e4dddc62453588d8 [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";
Sudheer Shankae93db512019-01-11 16:05:28 -080053 private static final String DIR_SANDBOX = "sandbox";
Jeff Sharkey1abdb712013-08-11 16:28:14 -070054 private static final String DIR_DATA = "data";
55 private static final String DIR_MEDIA = "media";
56 private static final String DIR_OBB = "obb";
57 private static final String DIR_FILES = "files";
58 private static final String DIR_CACHE = "cache";
59
60 /** {@hide} */
61 @Deprecated
62 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070063
Jeff Sharkey63d0a062013-03-01 16:12:55 -080064 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070065 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060066 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070067 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080068 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080069 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080070 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070071 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090072 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Dario Freni2bef1762018-06-01 14:02:08 +010073 private static final File DIR_PRODUCT_SERVICES_ROOT = getDirectory(ENV_PRODUCT_SERVICES_ROOT,
74 "/product_services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
Andreas Gamped281b422016-07-08 03:50:27 +000076 private static UserEnvironment sCurrentUser;
77 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070078
Andreas Gamped281b422016-07-08 03:50:27 +000079 static {
80 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070081 }
82
83 /** {@hide} */
Andreas Gamped281b422016-07-08 03:50:27 +000084 public static void initForCurrentUser() {
85 final int userId = UserHandle.myUserId();
86 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070087 }
88
89 /** {@hide} */
90 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070091 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070092
93 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070094 mUserId = userId;
95 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070096
Jeff Sharkey48877892015-03-18 11:27:19 -070097 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -070098 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
99 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700100 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700101 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700102 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700103 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700104 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700105 }
106
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700107 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700108 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700109 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700110 }
111
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700112 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700113 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700114 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700115 }
116
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700117 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700118 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700119 }
120
Sudheer Shankae93db512019-01-11 16:05:28 -0800121 public File[] buildExternalStorageAndroidSandboxDirs() {
122 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_SANDBOX);
123 }
124
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700125 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700126 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700127 }
128
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700129 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700130 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700131 }
132
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700133 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700134 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700135 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700136
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700137 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700138 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700139 }
140
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700141 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700142 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700143 }
144
145 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700146 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700147 }
148
149 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700150 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700151 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700152 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800155 * Return root of the "system" partition holding the core Android OS.
156 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 */
158 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800159 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
161
Jeff Sharkey48877892015-03-18 11:27:19 -0700162 /** {@hide} */
Jeff Sharkeyc6091162018-06-29 17:15:40 -0600163 @TestApi
Jeff Sharkey48877892015-03-18 11:27:19 -0700164 public static File getStorageDirectory() {
165 return DIR_ANDROID_STORAGE;
166 }
167
Jason parksa3cdaa52011-01-13 14:15:43 -0600168 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800169 * Return root directory of the "oem" partition holding OEM customizations,
170 * if any. If present, the partition is mounted read-only.
171 *
172 * @hide
173 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600174 @SystemApi
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800175 public static File getOemDirectory() {
176 return DIR_OEM_ROOT;
177 }
178
179 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800180 * Return root directory of the "odm" partition holding ODM customizations,
181 * if any. If present, the partition is mounted read-only.
182 *
183 * @hide
184 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600185 @SystemApi
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800186 public static File getOdmDirectory() {
187 return DIR_ODM_ROOT;
188 }
189
190 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700191 * Return root directory of the "vendor" partition that holds vendor-provided
192 * software that should persist across simple reflashing of the "system" partition.
193 * @hide
194 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600195 @SystemApi
Christopher Tate740888f2014-04-18 12:24:57 -0700196 public static File getVendorDirectory() {
197 return DIR_VENDOR_ROOT;
198 }
199
Jason parksa3cdaa52011-01-13 14:15:43 -0600200 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900201 * Return root directory of the "product" partition holding product-specific
202 * customizations if any. If present, the partition is mounted read-only.
203 *
204 * @hide
205 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600206 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000207 @TestApi
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900208 public static File getProductDirectory() {
209 return DIR_PRODUCT_ROOT;
210 }
211
212 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100213 * Return root directory of the "product_services" partition holding middleware
214 * services if any. If present, the partition is mounted read-only.
215 *
216 * @hide
217 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600218 @SystemApi
Dario Freni2bef1762018-06-01 14:02:08 +0100219 public static File getProductServicesDirectory() {
220 return DIR_PRODUCT_SERVICES_ROOT;
221 }
222
223 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700224 * Return the system directory for a user. This is for use by system
225 * services to store files relating to the user. This directory will be
226 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700227 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600228 * @deprecated This directory is valid and still exists, but but callers
229 * should <em>strongly</em> consider switching to using either
230 * {@link #getDataSystemCeDirectory(int)} or
231 * {@link #getDataSystemDeDirectory(int)}, both of which support
232 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700233 * @hide
234 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700235 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700236 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800237 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700238 }
239
240 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700241 * Returns the config directory for a user. This is for use by system
242 * services to store files relating to the user which should be readable by
243 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100244 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700245 * @deprecated This directory is valid and still exists, but callers should
246 * <em>strongly</em> consider switching to
247 * {@link #getDataMiscCeDirectory(int)} which is protected with
248 * user credentials or {@link #getDataMiscDeDirectory(int)}
249 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100250 * @hide
251 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700252 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100253 public static File getUserConfigDirectory(int userId) {
254 return new File(new File(new File(
255 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
256 }
257
258 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700259 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
261 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800262 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
264
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700265 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700266 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700267 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800268 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700269 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700270 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700271 }
272 }
273
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700274 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600275 public static File getExpandDirectory() {
276 return DIR_ANDROID_EXPAND;
277 }
278
279 /** {@hide} */
Jeff Sharkey15447792015-11-05 16:18:51 -0800280 public static File getDataSystemDirectory() {
281 return new File(getDataDirectory(), "system");
282 }
283
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700284 /**
285 * Returns the base directory for per-user system directory, device encrypted.
286 * {@hide}
287 */
288 public static File getDataSystemDeDirectory() {
289 return buildPath(getDataDirectory(), "system_de");
290 }
291
292 /**
293 * Returns the base directory for per-user system directory, credential encrypted.
294 * {@hide}
295 */
296 public static File getDataSystemCeDirectory() {
297 return buildPath(getDataDirectory(), "system_ce");
298 }
299
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600300 /**
301 * Return the "credential encrypted" system directory for a user. This is
302 * for use by system services to store files relating to the user. This
303 * directory supports fast user wipe, and will be automatically deleted when
304 * the user is removed.
305 * <p>
306 * Data stored under this path is "credential encrypted", which uses an
307 * encryption key that is entangled with user credentials, such as a PIN or
308 * password. The contents will only be available once the user has been
309 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
310 * <p>
311 * New code should <em>strongly</em> prefer storing sensitive data in these
312 * credential encrypted areas.
313 *
314 * @hide
315 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700316 public static File getDataSystemCeDirectory(int userId) {
317 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800318 }
319
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600320 /**
321 * Return the "device encrypted" system directory for a user. This is for
322 * use by system services to store files relating to the user. This
323 * directory supports fast user wipe, and will be automatically deleted when
324 * the user is removed.
325 * <p>
326 * Data stored under this path is "device encrypted", which uses an
327 * encryption key that is tied to the physical device. The contents will
328 * only be available once the device has finished a {@code dm-verity}
329 * protected boot.
330 * <p>
331 * New code should <em>strongly</em> avoid storing sensitive data in these
332 * device encrypted areas.
333 *
334 * @hide
335 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700336 public static File getDataSystemDeDirectory(int userId) {
337 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
338 }
339
340 /** {@hide} */
341 public static File getDataMiscDirectory() {
342 return new File(getDataDirectory(), "misc");
343 }
344
345 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800346 public static File getDataMiscCeDirectory() {
347 return buildPath(getDataDirectory(), "misc_ce");
348 }
349
350 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700351 public static File getDataMiscCeDirectory(int userId) {
352 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
353 }
354
355 /** {@hide} */
356 public static File getDataMiscDeDirectory(int userId) {
357 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800358 }
359
Calin Juravled479b522016-02-24 16:22:03 +0000360 private static File getDataProfilesDeDirectory(int userId) {
361 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
362 }
363
364 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800365 public static File getDataVendorCeDirectory(int userId) {
366 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
367 }
368
369 /** {@hide} */
370 public static File getDataVendorDeDirectory(int userId) {
371 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
372 }
373
374 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800375 public static File getDataRefProfilesDePackageDirectory(String packageName) {
376 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100377 }
378
379 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000380 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
381 return buildPath(getDataProfilesDeDirectory(userId), packageName);
382 }
383
384 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700385 public static File getDataAppDirectory(String volumeUuid) {
386 return new File(getDataDirectory(volumeUuid), "app");
387 }
388
389 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000390 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery296c8b92019-02-27 12:06:24 +0000391 return new File(getDataDirectory(volumeUuid), "app-staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000392 }
393
394 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700395 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700396 return new File(getDataDirectory(volumeUuid), "user");
397 }
398
399 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700400 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
401 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700402 }
403
404 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700405 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700406 String packageName) {
407 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700408 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800409 }
410
411 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700412 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800413 return new File(getDataDirectory(volumeUuid), "user_de");
414 }
415
416 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700417 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
418 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800419 }
420
421 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700422 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800423 String packageName) {
424 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700425 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700426 }
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700429 * Return preloads directory.
430 * <p>This directory may contain pre-loaded content such as
431 * {@link #getDataPreloadsDemoDirectory() demo videos} and
432 * {@link #getDataPreloadsAppsDirectory() APK files} .
433 * {@hide}
434 */
435 public static File getDataPreloadsDirectory() {
436 return new File(getDataDirectory(), "preloads");
437 }
438
439 /**
440 * @see #getDataPreloadsDirectory()
441 * {@hide}
442 */
443 public static File getDataPreloadsDemoDirectory() {
444 return new File(getDataPreloadsDirectory(), "demo");
445 }
446
447 /**
448 * @see #getDataPreloadsDirectory()
449 * {@hide}
450 */
451 public static File getDataPreloadsAppsDirectory() {
452 return new File(getDataPreloadsDirectory(), "apps");
453 }
454
455 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700456 * @see #getDataPreloadsDirectory()
457 * {@hide}
458 */
459 public static File getDataPreloadsMediaDirectory() {
460 return new File(getDataPreloadsDirectory(), "media");
461 }
462
463 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700464 * Returns location of preloaded cache directory for package name
465 * @see #getDataPreloadsDirectory()
466 * {@hide}
467 */
468 public static File getDataPreloadsFileCacheDirectory(String packageName) {
469 return new File(getDataPreloadsFileCacheDirectory(), packageName);
470 }
471
472 /**
473 * Returns location of preloaded cache directory.
474 * @see #getDataPreloadsDirectory()
475 * {@hide}
476 */
477 public static File getDataPreloadsFileCacheDirectory() {
478 return new File(getDataPreloadsDirectory(), "file_cache");
479 }
480
481 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800482 * Returns location of packages cache directory.
483 * {@hide}
484 */
485 public static File getPackageCacheDirectory() {
486 return new File(getDataSystemDirectory(), "package_cache");
487 }
488
489 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700490 * Return the primary shared/external storage directory. This directory may
491 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800492 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700493 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800494 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700495 * <p>
496 * <em>Note: don't be confused by the word "external" here. This directory
497 * can better be thought as media/shared storage. It is a filesystem that
498 * can hold a relatively large amount of data and that is shared across all
499 * applications (does not enforce permissions). Traditionally this is an SD
500 * card, but it may also be implemented as built-in storage in a device that
501 * is distinct from the protected internal storage and can be mounted as a
502 * filesystem on a computer.</em>
503 * <p>
504 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700505 * each user has their own isolated shared storage. Applications only have
506 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700507 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700508 * In devices with multiple shared/external storage directories, this
509 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700510 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700511 * {@link Context#getExternalFilesDirs(String)},
512 * {@link Context#getExternalCacheDirs()}, and
513 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700514 * <p>
515 * Applications should not directly use this top-level directory, in order
516 * to avoid polluting the user's root namespace. Any files that are private
517 * to the application should be placed in a directory returned by
518 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700519 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700520 * if the application is uninstalled. Other shared files should be placed in
521 * one of the directories returned by
522 * {@link #getExternalStoragePublicDirectory}.
523 * <p>
524 * Writing to this path requires the
525 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800526 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700527 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700528 * which is automatically granted if you hold the write permission.
529 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700530 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700531 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700532 * {@link Context#getExternalFilesDir(String)},
533 * {@link Context#getExternalCacheDir()}, or
534 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700535 * read or write.
536 * <p>
537 * This path may change between platform versions, so applications should
538 * only persist relative paths.
539 * <p>
540 * Here is an example of typical code to monitor the state of external
541 * storage:
542 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700543 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800544 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700545 *
546 * @see #getExternalStorageState()
547 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 */
549 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700550 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000551 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700552 }
553
554 /** {@hide} */
555 public static File getLegacyExternalStorageDirectory() {
556 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700559 /** {@hide} */
560 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700561 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700562 }
563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800565 * Standard directory in which to place any audio files that should be
566 * in the regular list of music for the user.
567 * This may be combined with
568 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
569 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
570 * of directories to categories a particular audio file as more than one
571 * type.
572 */
573 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800574
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800575 /**
576 * Standard directory in which to place any audio files that should be
577 * in the list of podcasts that the user can select (not as regular
578 * music).
579 * This may be combined with {@link #DIRECTORY_MUSIC},
580 * {@link #DIRECTORY_NOTIFICATIONS},
581 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
582 * of directories to categories a particular audio file as more than one
583 * type.
584 */
585 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800586
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800587 /**
588 * Standard directory in which to place any audio files that should be
589 * in the list of ringtones that the user can select (not as regular
590 * music).
591 * This may be combined with {@link #DIRECTORY_MUSIC},
592 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
593 * {@link #DIRECTORY_ALARMS} as a series
594 * of directories to categories a particular audio file as more than one
595 * type.
596 */
597 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800598
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800599 /**
600 * Standard directory in which to place any audio files that should be
601 * in the list of alarms that the user can select (not as regular
602 * music).
603 * This may be combined with {@link #DIRECTORY_MUSIC},
604 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
605 * and {@link #DIRECTORY_RINGTONES} as a series
606 * of directories to categories a particular audio file as more than one
607 * type.
608 */
609 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800610
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800611 /**
612 * Standard directory in which to place any audio files that should be
613 * in the list of notifications that the user can select (not as regular
614 * music).
615 * This may be combined with {@link #DIRECTORY_MUSIC},
616 * {@link #DIRECTORY_PODCASTS},
617 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
618 * of directories to categories a particular audio file as more than one
619 * type.
620 */
621 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800622
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800623 /**
624 * Standard directory in which to place pictures that are available to
625 * the user. Note that this is primarily a convention for the top-level
626 * public directory, as the media scanner will find and collect pictures
627 * in any directory.
628 */
629 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800630
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800631 /**
632 * Standard directory in which to place movies that are available to
633 * the user. Note that this is primarily a convention for the top-level
634 * public directory, as the media scanner will find and collect movies
635 * in any directory.
636 */
637 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800638
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800639 /**
640 * Standard directory in which to place files that have been downloaded by
641 * the user. Note that this is primarily a convention for the top-level
642 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700643 * private directories. Also note that though the constant here is
644 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
645 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800646 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700647 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800648
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800649 /**
650 * The traditional location for pictures and videos when mounting the
651 * device as a camera. Note that this is primarily a convention for the
652 * top-level public directory, as this convention makes no sense elsewhere.
653 */
654 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700655
656 /**
657 * Standard directory in which to place documents that have been created by
658 * the user.
659 */
660 public static String DIRECTORY_DOCUMENTS = "Documents";
661
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800662 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600663 * Standard directory in which to place screenshots that have been taken by
664 * the user. Typically used as a secondary directory under
665 * {@link #DIRECTORY_PICTURES}.
666 */
667 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
668
669 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700670 * Standard directory in which to place any audio files which are
671 * audiobooks.
672 */
673 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
674
675 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800676 * List of standard storage directories.
677 * <p>
678 * Each of its values have its own constant:
679 * <ul>
680 * <li>{@link #DIRECTORY_MUSIC}
681 * <li>{@link #DIRECTORY_PODCASTS}
682 * <li>{@link #DIRECTORY_ALARMS}
683 * <li>{@link #DIRECTORY_RINGTONES}
684 * <li>{@link #DIRECTORY_NOTIFICATIONS}
685 * <li>{@link #DIRECTORY_PICTURES}
686 * <li>{@link #DIRECTORY_MOVIES}
687 * <li>{@link #DIRECTORY_DOWNLOADS}
688 * <li>{@link #DIRECTORY_DCIM}
689 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700690 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800691 * </ul>
692 * @hide
693 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800694 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800695 DIRECTORY_MUSIC,
696 DIRECTORY_PODCASTS,
697 DIRECTORY_RINGTONES,
698 DIRECTORY_ALARMS,
699 DIRECTORY_NOTIFICATIONS,
700 DIRECTORY_PICTURES,
701 DIRECTORY_MOVIES,
702 DIRECTORY_DOWNLOADS,
703 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700704 DIRECTORY_DOCUMENTS,
705 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800706 };
707
708 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800709 * @hide
710 */
711 public static boolean isStandardDirectory(String dir) {
712 for (String valid : STANDARD_DIRECTORIES) {
713 if (valid.equals(dir)) {
714 return true;
715 }
716 }
717 return false;
718 }
719
Jeff Sharkey20356142017-12-06 15:22:05 -0700720 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
721 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
722 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
723 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
724 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
725 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
726 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
727 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
728 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
729 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700730 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700731
732 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
733 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
734
735 /**
736 * Classify the content types present on the given external storage device.
737 * <p>
738 * This is typically useful for deciding if an inserted SD card is empty, or
739 * if it contains content like photos that should be preserved.
740 *
741 * @hide
742 */
743 public static int classifyExternalStorageDirectory(File dir) {
744 int res = 0;
745 for (File f : FileUtils.listFilesOrEmpty(dir)) {
746 if (f.isFile() && isInterestingFile(f)) {
747 res |= HAS_OTHER;
748 } else if (f.isDirectory() && hasInterestingFiles(f)) {
749 final String name = f.getName();
750 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
751 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
752 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
753 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
754 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
755 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
756 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
757 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
758 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
759 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700760 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700761 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
762 else res |= HAS_OTHER;
763 }
764 }
765 return res;
766 }
767
768 private static boolean hasInterestingFiles(File dir) {
769 final LinkedList<File> explore = new LinkedList<>();
770 explore.add(dir);
771 while (!explore.isEmpty()) {
772 dir = explore.pop();
773 for (File f : FileUtils.listFilesOrEmpty(dir)) {
774 if (isInterestingFile(f)) return true;
775 if (f.isDirectory()) explore.add(f);
776 }
777 }
778 return false;
779 }
780
781 private static boolean isInterestingFile(File file) {
782 if (file.isFile()) {
783 final String name = file.getName().toLowerCase();
784 if (name.endsWith(".exe") || name.equals("autorun.inf")
785 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
786 return false;
787 } else {
788 return true;
789 }
790 } else {
791 return false;
792 }
793 }
794
Felipe Lemeb012f912016-01-22 16:49:55 -0800795 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700796 * Get a top-level shared/external storage directory for placing files of a
797 * particular type. This is where the user will typically place and manage
798 * their own files, so you should be careful about what you put here to
799 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800800 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700801 * <p>
802 * On devices with multiple users (as described by {@link UserManager}),
803 * each user has their own isolated shared storage. Applications only have
804 * access to the shared storage for the user they're running as.
805 * </p>
806 * <p>
807 * Here is an example of typical code to manipulate a picture on the public
808 * shared storage:
809 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800810 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
811 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800812 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700813 * @param type The type of storage directory to return. Should be one of
814 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
815 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
816 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800817 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
818 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700819 * @return Returns the File path for the directory. Note that this directory
820 * may not yet exist, so you must make sure it exists before using
821 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800822 */
823 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700824 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000825 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800826 }
827
828 /**
829 * Returns the path for android-specific data on the SD card.
830 * @hide
831 */
Sudheer Shankae93db512019-01-11 16:05:28 -0800832 public static File[] buildExternalStorageAndroidSandboxDirs() {
833 throwIfUserRequired();
834 return sCurrentUser.buildExternalStorageAndroidSandboxDirs();
835 }
836
837 /**
838 * Returns the path for android-specific data on the SD card.
839 * @hide
840 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700841 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700842 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000843 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800844 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700845
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800846 /**
847 * Generates the raw path to an application's data
848 * @hide
849 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700850 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700851 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000852 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800853 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800854
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800855 /**
856 * Generates the raw path to an application's media
857 * @hide
858 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700859 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700860 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000861 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800862 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800863
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800864 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800865 * Generates the raw path to an application's OBB files
866 * @hide
867 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700868 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700869 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000870 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800871 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800872
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800873 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800874 * Generates the path to an application's files.
875 * @hide
876 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700877 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700878 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000879 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800880 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700881
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800882 /**
883 * Generates the path to an application's cache.
884 * @hide
885 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700886 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700887 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000888 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800889 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800890
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800891 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700892 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 */
894 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800895 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 }
897
898 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700899 * Unknown storage state, such as when a path isn't backed by known storage
900 * media.
901 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800902 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700903 */
904 public static final String MEDIA_UNKNOWN = "unknown";
905
906 /**
907 * Storage state if the media is not present.
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_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700914 * Storage state if the media is present but not mounted.
915 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800916 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 */
918 public static final String MEDIA_UNMOUNTED = "unmounted";
919
920 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700921 * Storage state if the media is present and being disk-checked.
922 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800923 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 */
925 public static final String MEDIA_CHECKING = "checking";
926
927 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700928 * Storage state if the media is present but is blank or is using an
929 * unsupported filesystem.
930 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800931 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 */
933 public static final String MEDIA_NOFS = "nofs";
934
935 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700936 * Storage state if the media is present and mounted at its mount point with
937 * read/write access.
938 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800939 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 */
941 public static final String MEDIA_MOUNTED = "mounted";
942
943 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700944 * Storage state if the media is present and mounted at its mount point with
945 * read-only access.
946 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800947 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 */
949 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
950
951 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700952 * Storage state if the media is present not mounted, and shared via USB
953 * mass storage.
954 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800955 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 */
957 public static final String MEDIA_SHARED = "shared";
958
959 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700960 * Storage state if the media was removed before it was unmounted.
961 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800962 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 */
964 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
965
966 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700967 * Storage state if the media is present but cannot be mounted. Typically
968 * this happens if the file system on the media is corrupted.
969 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800970 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 */
972 public static final String MEDIA_UNMOUNTABLE = "unmountable";
973
974 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700975 * Storage state if the media is in the process of being ejected.
976 *
977 * @see #getExternalStorageState(File)
978 */
979 public static final String MEDIA_EJECTING = "ejecting";
980
981 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700982 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800983 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700984 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700985 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
986 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
987 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
988 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
989 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 */
991 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +0000992 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800993 return getExternalStorageState(externalDir);
994 }
995
996 /**
997 * @deprecated use {@link #getExternalStorageState(File)}
998 */
999 @Deprecated
1000 public static String getStorageState(File path) {
1001 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001002 }
1003
1004 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001005 * Returns the current state of the shared/external storage media at the
1006 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001007 *
1008 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1009 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1010 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1011 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1012 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
1013 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001014 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001015 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001016 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001017 return volume.getState();
1018 } else {
1019 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022
Dianne Hackborn407f6252010-10-04 11:31:17 -07001023 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001024 * Returns whether the primary shared/external storage media is physically
1025 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001026 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001027 * @return true if the storage device can be removed (such as an SD card),
1028 * or false if the storage device is built in and cannot be
1029 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001030 */
1031 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001032 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001033 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001034 }
1035
Kenny Roote1ff2142010-10-12 11:20:01 -07001036 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001037 * Returns whether the shared/external storage media at the given path is
1038 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001039 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001040 * @return true if the storage device can be removed (such as an SD card),
1041 * or false if the storage device is built in and cannot be
1042 * physically removed.
1043 * @throws IllegalArgumentException if the path is not a valid storage
1044 * device.
1045 */
1046 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001047 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001048 if (volume != null) {
1049 return volume.isRemovable();
1050 } else {
1051 throw new IllegalArgumentException("Failed to find storage device at " + path);
1052 }
1053 }
1054
1055 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001056 * Returns whether the primary shared/external storage media is emulated.
1057 * <p>
1058 * The contents of emulated storage devices are backed by a private user
1059 * data partition, which means there is little benefit to apps storing data
1060 * here instead of the private directories returned by
1061 * {@link Context#getFilesDir()}, etc.
1062 * <p>
1063 * This returns true when emulated storage is backed by either internal
1064 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001065 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001066 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1067 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001068 */
1069 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001070 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001071 return isExternalStorageEmulated(externalDir);
1072 }
1073
1074 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001075 * Returns whether the shared/external storage media at the given path is
1076 * emulated.
1077 * <p>
1078 * The contents of emulated storage devices are backed by a private user
1079 * data partition, which means there is little benefit to apps storing data
1080 * here instead of the private directories returned by
1081 * {@link Context#getFilesDir()}, etc.
1082 * <p>
1083 * This returns true when emulated storage is backed by either internal
1084 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001085 *
1086 * @throws IllegalArgumentException if the path is not a valid storage
1087 * device.
1088 */
1089 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001090 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001091 if (volume != null) {
1092 return volume.isEmulated();
1093 } else {
1094 throw new IllegalArgumentException("Failed to find storage device at " + path);
1095 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001096 }
1097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 static File getDirectory(String variableName, String defaultPath) {
1099 String path = System.getenv(variableName);
1100 return path == null ? new File(defaultPath) : new File(path);
1101 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001102
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001103 /** {@hide} */
1104 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001105 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001106 }
1107
1108 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001109 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001110 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1111 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001112 }
1113 }
1114
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001115 /**
1116 * Append path segments to each given base path, returning result.
1117 *
1118 * @hide
1119 */
1120 public static File[] buildPaths(File[] base, String... segments) {
1121 File[] result = new File[base.length];
1122 for (int i = 0; i < base.length; i++) {
1123 result[i] = buildPath(base[i], segments);
1124 }
1125 return result;
1126 }
1127
1128 /**
1129 * Append path segments to given base path, returning result.
1130 *
1131 * @hide
1132 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001133 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001134 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001135 File cur = base;
1136 for (String segment : segments) {
1137 if (cur == null) {
1138 cur = new File(segment);
1139 } else {
1140 cur = new File(cur, segment);
1141 }
1142 }
1143 return cur;
1144 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001145
1146 /**
1147 * If the given path exists on emulated external storage, return the
1148 * translated backing path hosted on internal storage. This bypasses any
1149 * emulation later, improving performance. This is <em>only</em> suitable
1150 * for read-only access.
1151 * <p>
1152 * Returns original path if given path doesn't meet these criteria. Callers
1153 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1154 * permission.
1155 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001156 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001157 * @hide
1158 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001159 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001160 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001161 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163}