blob: f7e927e4886340f1ac0e3a700691b23c8e6249aa [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 Sharkeya30e5c32019-02-28 12:02:10 -070019import android.annotation.NonNull;
Jeff Sharkey70eb34a2018-09-13 11:57:03 -060020import android.annotation.SystemApi;
Philip P. Moltmannf80809f2018-04-04 11:20:44 -070021import android.annotation.TestApi;
Andrei Onea24ec3212019-03-15 17:35:05 +000022import android.annotation.UnsupportedAppUsage;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -060023import android.app.AppGlobals;
24import android.app.AppOpsManager;
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080025import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070026import android.content.Context;
Jeff Sharkey48877892015-03-18 11:27:19 -070027import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070028import android.os.storage.StorageVolume;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070029import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080030import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080031
Gilles Debunneee1d6302011-05-13 10:09:32 -070032import java.io.File;
Jeff Sharkey20356142017-12-06 15:22:05 -070033import java.util.LinkedList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035/**
36 * Provides access to environment variables.
37 */
38public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080039 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Jeff Sharkeydd02e332018-06-27 14:41:57 -060041 // NOTE: keep credential-protected paths in sync with StrictMode.java
42
Jeff Sharkeyb049e212012-09-07 23:16:01 -070043 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080044 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070045 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060046 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070047 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080048 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080049 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080050 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070051 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090052 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Dario Freni2bef1762018-06-01 14:02:08 +010053 private static final String ENV_PRODUCT_SERVICES_ROOT = "PRODUCT_SERVICES_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070054
Jeff Sharkeydfa45302012-09-12 16:25:22 -070055 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070056 public static final String DIR_ANDROID = "Android";
Sudheer Shankae93db512019-01-11 16:05:28 -080057 private static final String DIR_SANDBOX = "sandbox";
Jeff Sharkey1abdb712013-08-11 16:28:14 -070058 private static final String DIR_DATA = "data";
59 private static final String DIR_MEDIA = "media";
60 private static final String DIR_OBB = "obb";
61 private static final String DIR_FILES = "files";
62 private static final String DIR_CACHE = "cache";
63
64 /** {@hide} */
65 @Deprecated
66 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070067
Jeff Sharkey63d0a062013-03-01 16:12:55 -080068 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070069 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060070 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070071 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080072 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080073 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080074 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070075 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090076 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Dario Freni2bef1762018-06-01 14:02:08 +010077 private static final File DIR_PRODUCT_SERVICES_ROOT = getDirectory(ENV_PRODUCT_SERVICES_ROOT,
78 "/product_services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
Andrei Onea24ec3212019-03-15 17:35:05 +000080 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000081 private static UserEnvironment sCurrentUser;
82 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070083
Andreas Gamped281b422016-07-08 03:50:27 +000084 static {
85 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070086 }
87
88 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +000089 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000090 public static void initForCurrentUser() {
91 final int userId = UserHandle.myUserId();
92 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070093 }
94
95 /** {@hide} */
96 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070097 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070098
Andrei Onea24ec3212019-03-15 17:35:05 +000099 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700100 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700101 mUserId = userId;
102 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700103
Andrei Onea24ec3212019-03-15 17:35:05 +0000104 @UnsupportedAppUsage
Jeff Sharkey48877892015-03-18 11:27:19 -0700105 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -0700106 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
107 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700108 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700109 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700110 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700111 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700112 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700113 }
114
Andrei Onea24ec3212019-03-15 17:35:05 +0000115 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700116 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700117 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700118 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700119 }
120
Andrei Onea24ec3212019-03-15 17:35:05 +0000121 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700122 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700123 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700124 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700125 }
126
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700127 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700128 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700129 }
130
Sudheer Shankae93db512019-01-11 16:05:28 -0800131 public File[] buildExternalStorageAndroidSandboxDirs() {
132 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_SANDBOX);
133 }
134
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700135 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700136 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700137 }
138
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700139 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700140 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700141 }
142
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700143 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700144 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700145 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700146
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700147 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700148 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700149 }
150
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700151 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700152 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700153 }
154
155 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700156 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700157 }
158
159 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700160 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700161 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700162 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800165 * Return root of the "system" partition holding the core Android OS.
166 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700168 public static @NonNull File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800169 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 }
171
Jeff Sharkey48877892015-03-18 11:27:19 -0700172 /** {@hide} */
Jeff Sharkeyc6091162018-06-29 17:15:40 -0600173 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700174 public static @NonNull File getStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700175 return DIR_ANDROID_STORAGE;
176 }
177
Jason parksa3cdaa52011-01-13 14:15:43 -0600178 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800179 * Return root directory of the "oem" partition holding OEM customizations,
180 * if any. If present, the partition is mounted read-only.
181 *
182 * @hide
183 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600184 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700185 public static @NonNull File getOemDirectory() {
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800186 return DIR_OEM_ROOT;
187 }
188
189 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800190 * Return root directory of the "odm" partition holding ODM customizations,
191 * if any. If present, the partition is mounted read-only.
192 *
193 * @hide
194 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600195 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700196 public static @NonNull File getOdmDirectory() {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800197 return DIR_ODM_ROOT;
198 }
199
200 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700201 * Return root directory of the "vendor" partition that holds vendor-provided
202 * software that should persist across simple reflashing of the "system" partition.
203 * @hide
204 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600205 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700206 public static @NonNull File getVendorDirectory() {
Christopher Tate740888f2014-04-18 12:24:57 -0700207 return DIR_VENDOR_ROOT;
208 }
209
Jason parksa3cdaa52011-01-13 14:15:43 -0600210 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900211 * Return root directory of the "product" partition holding product-specific
212 * customizations if any. If present, the partition is mounted read-only.
213 *
214 * @hide
215 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600216 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000217 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700218 public static @NonNull File getProductDirectory() {
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900219 return DIR_PRODUCT_ROOT;
220 }
221
222 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100223 * Return root directory of the "product_services" partition holding middleware
224 * services if any. If present, the partition is mounted read-only.
225 *
226 * @hide
227 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600228 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700229 public static @NonNull File getProductServicesDirectory() {
Dario Freni2bef1762018-06-01 14:02:08 +0100230 return DIR_PRODUCT_SERVICES_ROOT;
231 }
232
233 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700234 * Return the system directory for a user. This is for use by system
235 * services to store files relating to the user. This directory will be
236 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700237 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600238 * @deprecated This directory is valid and still exists, but but callers
239 * should <em>strongly</em> consider switching to using either
240 * {@link #getDataSystemCeDirectory(int)} or
241 * {@link #getDataSystemDeDirectory(int)}, both of which support
242 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700243 * @hide
244 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700245 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700246 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800247 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700248 }
249
250 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700251 * Returns the config directory for a user. This is for use by system
252 * services to store files relating to the user which should be readable by
253 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100254 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700255 * @deprecated This directory is valid and still exists, but callers should
256 * <em>strongly</em> consider switching to
257 * {@link #getDataMiscCeDirectory(int)} which is protected with
258 * user credentials or {@link #getDataMiscDeDirectory(int)}
259 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100260 * @hide
261 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700262 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100263 public static File getUserConfigDirectory(int userId) {
264 return new File(new File(new File(
265 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
266 }
267
268 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700269 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 */
271 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800272 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
274
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700275 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700276 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700277 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800278 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700279 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700280 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700281 }
282 }
283
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700284 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600285 public static File getExpandDirectory() {
286 return DIR_ANDROID_EXPAND;
287 }
288
289 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000290 @UnsupportedAppUsage
Jeff Sharkey15447792015-11-05 16:18:51 -0800291 public static File getDataSystemDirectory() {
292 return new File(getDataDirectory(), "system");
293 }
294
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700295 /**
296 * Returns the base directory for per-user system directory, device encrypted.
297 * {@hide}
298 */
299 public static File getDataSystemDeDirectory() {
300 return buildPath(getDataDirectory(), "system_de");
301 }
302
303 /**
304 * Returns the base directory for per-user system directory, credential encrypted.
305 * {@hide}
306 */
307 public static File getDataSystemCeDirectory() {
308 return buildPath(getDataDirectory(), "system_ce");
309 }
310
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600311 /**
312 * Return the "credential encrypted" system directory for a user. This is
313 * for use by system services to store files relating to the user. This
314 * directory supports fast user wipe, and will be automatically deleted when
315 * the user is removed.
316 * <p>
317 * Data stored under this path is "credential encrypted", which uses an
318 * encryption key that is entangled with user credentials, such as a PIN or
319 * password. The contents will only be available once the user has been
320 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
321 * <p>
322 * New code should <em>strongly</em> prefer storing sensitive data in these
323 * credential encrypted areas.
324 *
325 * @hide
326 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700327 public static File getDataSystemCeDirectory(int userId) {
328 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800329 }
330
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600331 /**
332 * Return the "device encrypted" system directory for a user. This is for
333 * use by system services to store files relating to the user. This
334 * directory supports fast user wipe, and will be automatically deleted when
335 * the user is removed.
336 * <p>
337 * Data stored under this path is "device encrypted", which uses an
338 * encryption key that is tied to the physical device. The contents will
339 * only be available once the device has finished a {@code dm-verity}
340 * protected boot.
341 * <p>
342 * New code should <em>strongly</em> avoid storing sensitive data in these
343 * device encrypted areas.
344 *
345 * @hide
346 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700347 public static File getDataSystemDeDirectory(int userId) {
348 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
349 }
350
351 /** {@hide} */
352 public static File getDataMiscDirectory() {
353 return new File(getDataDirectory(), "misc");
354 }
355
356 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800357 public static File getDataMiscCeDirectory() {
358 return buildPath(getDataDirectory(), "misc_ce");
359 }
360
361 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700362 public static File getDataMiscCeDirectory(int userId) {
363 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
364 }
365
366 /** {@hide} */
367 public static File getDataMiscDeDirectory(int userId) {
368 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800369 }
370
Calin Juravled479b522016-02-24 16:22:03 +0000371 private static File getDataProfilesDeDirectory(int userId) {
372 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
373 }
374
375 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800376 public static File getDataVendorCeDirectory(int userId) {
377 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
378 }
379
380 /** {@hide} */
381 public static File getDataVendorDeDirectory(int userId) {
382 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
383 }
384
385 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800386 public static File getDataRefProfilesDePackageDirectory(String packageName) {
387 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100388 }
389
390 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000391 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
392 return buildPath(getDataProfilesDeDirectory(userId), packageName);
393 }
394
395 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700396 public static File getDataAppDirectory(String volumeUuid) {
397 return new File(getDataDirectory(volumeUuid), "app");
398 }
399
400 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000401 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery296c8b92019-02-27 12:06:24 +0000402 return new File(getDataDirectory(volumeUuid), "app-staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000403 }
404
405 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700406 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700407 return new File(getDataDirectory(volumeUuid), "user");
408 }
409
410 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700411 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
412 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700413 }
414
415 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700416 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700417 String packageName) {
418 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700419 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800420 }
421
422 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700423 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800424 return new File(getDataDirectory(volumeUuid), "user_de");
425 }
426
427 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700428 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
429 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800430 }
431
432 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700433 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800434 String packageName) {
435 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700436 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700437 }
438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700440 * Return preloads directory.
441 * <p>This directory may contain pre-loaded content such as
442 * {@link #getDataPreloadsDemoDirectory() demo videos} and
443 * {@link #getDataPreloadsAppsDirectory() APK files} .
444 * {@hide}
445 */
446 public static File getDataPreloadsDirectory() {
447 return new File(getDataDirectory(), "preloads");
448 }
449
450 /**
451 * @see #getDataPreloadsDirectory()
452 * {@hide}
453 */
454 public static File getDataPreloadsDemoDirectory() {
455 return new File(getDataPreloadsDirectory(), "demo");
456 }
457
458 /**
459 * @see #getDataPreloadsDirectory()
460 * {@hide}
461 */
462 public static File getDataPreloadsAppsDirectory() {
463 return new File(getDataPreloadsDirectory(), "apps");
464 }
465
466 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700467 * @see #getDataPreloadsDirectory()
468 * {@hide}
469 */
470 public static File getDataPreloadsMediaDirectory() {
471 return new File(getDataPreloadsDirectory(), "media");
472 }
473
474 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700475 * Returns location of preloaded cache directory for package name
476 * @see #getDataPreloadsDirectory()
477 * {@hide}
478 */
479 public static File getDataPreloadsFileCacheDirectory(String packageName) {
480 return new File(getDataPreloadsFileCacheDirectory(), packageName);
481 }
482
483 /**
484 * Returns location of preloaded cache directory.
485 * @see #getDataPreloadsDirectory()
486 * {@hide}
487 */
488 public static File getDataPreloadsFileCacheDirectory() {
489 return new File(getDataPreloadsDirectory(), "file_cache");
490 }
491
492 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800493 * Returns location of packages cache directory.
494 * {@hide}
495 */
496 public static File getPackageCacheDirectory() {
497 return new File(getDataSystemDirectory(), "package_cache");
498 }
499
500 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700501 * Return the primary shared/external storage directory. This directory may
502 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800503 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700504 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800505 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700506 * <p>
507 * <em>Note: don't be confused by the word "external" here. This directory
508 * can better be thought as media/shared storage. It is a filesystem that
509 * can hold a relatively large amount of data and that is shared across all
510 * applications (does not enforce permissions). Traditionally this is an SD
511 * card, but it may also be implemented as built-in storage in a device that
512 * is distinct from the protected internal storage and can be mounted as a
513 * filesystem on a computer.</em>
514 * <p>
515 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700516 * each user has their own isolated shared storage. Applications only have
517 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700518 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700519 * In devices with multiple shared/external storage directories, this
520 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700521 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700522 * {@link Context#getExternalFilesDirs(String)},
523 * {@link Context#getExternalCacheDirs()}, and
524 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700525 * <p>
526 * Applications should not directly use this top-level directory, in order
527 * to avoid polluting the user's root namespace. Any files that are private
528 * to the application should be placed in a directory returned by
529 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700530 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700531 * if the application is uninstalled. Other shared files should be placed in
532 * one of the directories returned by
533 * {@link #getExternalStoragePublicDirectory}.
534 * <p>
535 * Writing to this path requires the
536 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800537 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700538 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700539 * which is automatically granted if you hold the write permission.
540 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700541 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700542 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700543 * {@link Context#getExternalFilesDir(String)},
544 * {@link Context#getExternalCacheDir()}, or
545 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700546 * read or write.
547 * <p>
548 * This path may change between platform versions, so applications should
549 * only persist relative paths.
550 * <p>
551 * Here is an example of typical code to monitor the state of external
552 * storage:
553 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700554 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800555 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700556 *
557 * @see #getExternalStorageState()
558 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 */
560 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700561 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000562 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700563 }
564
565 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000566 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700567 public static File getLegacyExternalStorageDirectory() {
568 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700571 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000572 @UnsupportedAppUsage
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700573 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700574 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700575 }
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800578 * Standard directory in which to place any audio files that should be
579 * in the regular list of music for the user.
580 * This may be combined with
581 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
582 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
583 * of directories to categories a particular audio file as more than one
584 * type.
585 */
586 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800587
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800588 /**
589 * Standard directory in which to place any audio files that should be
590 * in the list of podcasts that the user can select (not as regular
591 * music).
592 * This may be combined with {@link #DIRECTORY_MUSIC},
593 * {@link #DIRECTORY_NOTIFICATIONS},
594 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
595 * of directories to categories a particular audio file as more than one
596 * type.
597 */
598 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800599
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800600 /**
601 * Standard directory in which to place any audio files that should be
602 * in the list of ringtones that the user can select (not as regular
603 * music).
604 * This may be combined with {@link #DIRECTORY_MUSIC},
605 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
606 * {@link #DIRECTORY_ALARMS} as a series
607 * of directories to categories a particular audio file as more than one
608 * type.
609 */
610 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800611
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800612 /**
613 * Standard directory in which to place any audio files that should be
614 * in the list of alarms that the user can select (not as regular
615 * music).
616 * This may be combined with {@link #DIRECTORY_MUSIC},
617 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
618 * and {@link #DIRECTORY_RINGTONES} as a series
619 * of directories to categories a particular audio file as more than one
620 * type.
621 */
622 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800623
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800624 /**
625 * Standard directory in which to place any audio files that should be
626 * in the list of notifications that the user can select (not as regular
627 * music).
628 * This may be combined with {@link #DIRECTORY_MUSIC},
629 * {@link #DIRECTORY_PODCASTS},
630 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
631 * of directories to categories a particular audio file as more than one
632 * type.
633 */
634 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800635
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800636 /**
637 * Standard directory in which to place pictures that are available to
638 * the user. Note that this is primarily a convention for the top-level
639 * public directory, as the media scanner will find and collect pictures
640 * in any directory.
641 */
642 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800643
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800644 /**
645 * Standard directory in which to place movies that are available to
646 * the user. Note that this is primarily a convention for the top-level
647 * public directory, as the media scanner will find and collect movies
648 * in any directory.
649 */
650 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800651
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800652 /**
653 * Standard directory in which to place files that have been downloaded by
654 * the user. Note that this is primarily a convention for the top-level
655 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700656 * private directories. Also note that though the constant here is
657 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
658 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800659 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700660 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800661
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800662 /**
663 * The traditional location for pictures and videos when mounting the
664 * device as a camera. Note that this is primarily a convention for the
665 * top-level public directory, as this convention makes no sense elsewhere.
666 */
667 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700668
669 /**
670 * Standard directory in which to place documents that have been created by
671 * the user.
672 */
673 public static String DIRECTORY_DOCUMENTS = "Documents";
674
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800675 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600676 * Standard directory in which to place screenshots that have been taken by
677 * the user. Typically used as a secondary directory under
678 * {@link #DIRECTORY_PICTURES}.
679 */
680 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
681
682 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700683 * Standard directory in which to place any audio files which are
684 * audiobooks.
685 */
686 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
687
688 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800689 * List of standard storage directories.
690 * <p>
691 * Each of its values have its own constant:
692 * <ul>
693 * <li>{@link #DIRECTORY_MUSIC}
694 * <li>{@link #DIRECTORY_PODCASTS}
695 * <li>{@link #DIRECTORY_ALARMS}
696 * <li>{@link #DIRECTORY_RINGTONES}
697 * <li>{@link #DIRECTORY_NOTIFICATIONS}
698 * <li>{@link #DIRECTORY_PICTURES}
699 * <li>{@link #DIRECTORY_MOVIES}
700 * <li>{@link #DIRECTORY_DOWNLOADS}
701 * <li>{@link #DIRECTORY_DCIM}
702 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700703 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800704 * </ul>
705 * @hide
706 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800707 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800708 DIRECTORY_MUSIC,
709 DIRECTORY_PODCASTS,
710 DIRECTORY_RINGTONES,
711 DIRECTORY_ALARMS,
712 DIRECTORY_NOTIFICATIONS,
713 DIRECTORY_PICTURES,
714 DIRECTORY_MOVIES,
715 DIRECTORY_DOWNLOADS,
716 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700717 DIRECTORY_DOCUMENTS,
718 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800719 };
720
721 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800722 * @hide
723 */
724 public static boolean isStandardDirectory(String dir) {
725 for (String valid : STANDARD_DIRECTORIES) {
726 if (valid.equals(dir)) {
727 return true;
728 }
729 }
730 return false;
731 }
732
Jeff Sharkey20356142017-12-06 15:22:05 -0700733 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
734 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
735 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
736 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
737 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
738 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
739 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
740 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
741 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
742 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700743 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700744
745 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
746 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
747
748 /**
749 * Classify the content types present on the given external storage device.
750 * <p>
751 * This is typically useful for deciding if an inserted SD card is empty, or
752 * if it contains content like photos that should be preserved.
753 *
754 * @hide
755 */
756 public static int classifyExternalStorageDirectory(File dir) {
757 int res = 0;
758 for (File f : FileUtils.listFilesOrEmpty(dir)) {
759 if (f.isFile() && isInterestingFile(f)) {
760 res |= HAS_OTHER;
761 } else if (f.isDirectory() && hasInterestingFiles(f)) {
762 final String name = f.getName();
763 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
764 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
765 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
766 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
767 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
768 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
769 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
770 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
771 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
772 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700773 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700774 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
775 else res |= HAS_OTHER;
776 }
777 }
778 return res;
779 }
780
781 private static boolean hasInterestingFiles(File dir) {
782 final LinkedList<File> explore = new LinkedList<>();
783 explore.add(dir);
784 while (!explore.isEmpty()) {
785 dir = explore.pop();
786 for (File f : FileUtils.listFilesOrEmpty(dir)) {
787 if (isInterestingFile(f)) return true;
788 if (f.isDirectory()) explore.add(f);
789 }
790 }
791 return false;
792 }
793
794 private static boolean isInterestingFile(File file) {
795 if (file.isFile()) {
796 final String name = file.getName().toLowerCase();
797 if (name.endsWith(".exe") || name.equals("autorun.inf")
798 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
799 return false;
800 } else {
801 return true;
802 }
803 } else {
804 return false;
805 }
806 }
807
Felipe Lemeb012f912016-01-22 16:49:55 -0800808 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700809 * Get a top-level shared/external storage directory for placing files of a
810 * particular type. This is where the user will typically place and manage
811 * their own files, so you should be careful about what you put here to
812 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800813 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700814 * <p>
815 * On devices with multiple users (as described by {@link UserManager}),
816 * each user has their own isolated shared storage. Applications only have
817 * access to the shared storage for the user they're running as.
818 * </p>
819 * <p>
820 * Here is an example of typical code to manipulate a picture on the public
821 * shared storage:
822 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800823 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
824 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800825 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700826 * @param type The type of storage directory to return. Should be one of
827 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
828 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
829 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800830 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
831 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700832 * @return Returns the File path for the directory. Note that this directory
833 * may not yet exist, so you must make sure it exists before using
834 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800835 */
836 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700837 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000838 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800839 }
840
841 /**
842 * Returns the path for android-specific data on the SD card.
843 * @hide
844 */
Sudheer Shankae93db512019-01-11 16:05:28 -0800845 public static File[] buildExternalStorageAndroidSandboxDirs() {
846 throwIfUserRequired();
847 return sCurrentUser.buildExternalStorageAndroidSandboxDirs();
848 }
849
850 /**
851 * Returns the path for android-specific data on the SD card.
852 * @hide
853 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000854 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700855 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700856 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000857 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800858 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700859
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800860 /**
861 * Generates the raw path to an application's data
862 * @hide
863 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000864 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700865 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700866 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000867 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800868 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800869
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800870 /**
871 * Generates the raw path to an application's media
872 * @hide
873 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000874 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700875 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700876 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000877 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800878 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800879
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800880 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800881 * Generates the raw path to an application's OBB files
882 * @hide
883 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000884 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700885 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700886 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000887 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800888 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800889
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800890 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800891 * Generates the path to an application's files.
892 * @hide
893 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000894 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700895 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700896 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000897 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800898 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700899
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800900 /**
901 * Generates the path to an application's cache.
902 * @hide
903 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000904 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700905 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700906 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000907 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800908 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800909
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800910 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700911 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 */
913 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800914 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 }
916
917 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700918 * Unknown storage state, such as when a path isn't backed by known storage
919 * media.
920 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800921 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700922 */
923 public static final String MEDIA_UNKNOWN = "unknown";
924
925 /**
926 * Storage state if the media is not present.
927 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800928 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 */
930 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700933 * Storage state if the media is present but not mounted.
934 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800935 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 */
937 public static final String MEDIA_UNMOUNTED = "unmounted";
938
939 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700940 * Storage state if the media is present and being disk-checked.
941 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800942 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 */
944 public static final String MEDIA_CHECKING = "checking";
945
946 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700947 * Storage state if the media is present but is blank or is using an
948 * unsupported filesystem.
949 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800950 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 */
952 public static final String MEDIA_NOFS = "nofs";
953
954 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700955 * Storage state if the media is present and mounted at its mount point with
956 * read/write access.
957 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800958 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 */
960 public static final String MEDIA_MOUNTED = "mounted";
961
962 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700963 * Storage state if the media is present and mounted at its mount point with
964 * read-only access.
965 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800966 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 */
968 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
969
970 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700971 * Storage state if the media is present not mounted, and shared via USB
972 * mass storage.
973 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800974 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 */
976 public static final String MEDIA_SHARED = "shared";
977
978 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700979 * Storage state if the media was removed before it was unmounted.
980 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800981 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 */
983 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
984
985 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700986 * Storage state if the media is present but cannot be mounted. Typically
987 * this happens if the file system on the media is corrupted.
988 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800989 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 */
991 public static final String MEDIA_UNMOUNTABLE = "unmountable";
992
993 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700994 * Storage state if the media is in the process of being ejected.
995 *
996 * @see #getExternalStorageState(File)
997 */
998 public static final String MEDIA_EJECTING = "ejecting";
999
1000 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001001 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -08001002 *
Jeff Sharkey8c165792012-10-22 14:08:29 -07001003 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001004 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1005 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1006 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1007 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1008 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 */
1010 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +00001011 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001012 return getExternalStorageState(externalDir);
1013 }
1014
1015 /**
1016 * @deprecated use {@link #getExternalStorageState(File)}
1017 */
1018 @Deprecated
1019 public static String getStorageState(File path) {
1020 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001021 }
1022
1023 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001024 * Returns the current state of the shared/external storage media at the
1025 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001026 *
1027 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1028 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1029 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1030 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1031 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
1032 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001033 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001034 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001035 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001036 return volume.getState();
1037 } else {
1038 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
Dianne Hackborn407f6252010-10-04 11:31:17 -07001042 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001043 * Returns whether the primary shared/external storage media is physically
1044 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001045 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001046 * @return true if the storage device can be removed (such as an SD card),
1047 * or false if the storage device is built in and cannot be
1048 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001049 */
1050 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001051 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001052 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001053 }
1054
Kenny Roote1ff2142010-10-12 11:20:01 -07001055 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001056 * Returns whether the shared/external storage media at the given path is
1057 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001058 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001059 * @return true if the storage device can be removed (such as an SD card),
1060 * or false if the storage device is built in and cannot be
1061 * physically removed.
1062 * @throws IllegalArgumentException if the path is not a valid storage
1063 * device.
1064 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001065 public static boolean isExternalStorageRemovable(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001066 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001067 if (volume != null) {
1068 return volume.isRemovable();
1069 } else {
1070 throw new IllegalArgumentException("Failed to find storage device at " + path);
1071 }
1072 }
1073
1074 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001075 * Returns whether the primary shared/external storage media is emulated.
1076 * <p>
1077 * The contents of emulated storage devices are backed by a private user
1078 * data partition, which means there is little benefit to apps storing data
1079 * here instead of the private directories returned by
1080 * {@link Context#getFilesDir()}, etc.
1081 * <p>
1082 * This returns true when emulated storage is backed by either internal
1083 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001084 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001085 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1086 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001087 */
1088 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001089 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001090 return isExternalStorageEmulated(externalDir);
1091 }
1092
1093 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001094 * Returns whether the shared/external storage media at the given path is
1095 * emulated.
1096 * <p>
1097 * The contents of emulated storage devices are backed by a private user
1098 * data partition, which means there is little benefit to apps storing data
1099 * here instead of the private directories returned by
1100 * {@link Context#getFilesDir()}, etc.
1101 * <p>
1102 * This returns true when emulated storage is backed by either internal
1103 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001104 *
1105 * @throws IllegalArgumentException if the path is not a valid storage
1106 * device.
1107 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001108 public static boolean isExternalStorageEmulated(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001109 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001110 if (volume != null) {
1111 return volume.isEmulated();
1112 } else {
1113 throw new IllegalArgumentException("Failed to find storage device at " + path);
1114 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001115 }
1116
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001117 /**
1118 * Returns whether the shared/external storage media at the given path is a
1119 * sandboxed view that only contains files owned by the app.
1120 * <p>
1121 * This value may be different from the value requested by
1122 * {@code allowExternalStorageSandbox} in the app's manifest, since an app
1123 * may inherit its sandboxed state based on when it was first installed.
1124 * <p>
1125 * Sandboxed apps can continue to discover and read media belonging to other
1126 * apps via {@link android.provider.MediaStore}.
1127 */
1128 public static boolean isExternalStorageSandboxed() {
1129 final File externalDir = sCurrentUser.getExternalDirs()[0];
1130 return isExternalStorageSandboxed(externalDir);
1131 }
1132
1133 /**
1134 * Returns whether the shared/external storage media at the given path is a
1135 * sandboxed view that only contains files owned by the app.
1136 * <p>
1137 * This value may be different from the value requested by
1138 * {@code allowExternalStorageSandbox} in the app's manifest, since an app
1139 * may inherit its sandboxed state based on when it was first installed.
1140 * <p>
1141 * Sandboxed apps can continue to discover and read media belonging to other
1142 * apps via {@link android.provider.MediaStore}.
1143 *
1144 * @throws IllegalArgumentException if the path is not a valid storage
1145 * device.
1146 */
1147 public static boolean isExternalStorageSandboxed(@NonNull File path) {
1148 final Context context = AppGlobals.getInitialApplication();
1149 final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
1150 return appOps.noteOpNoThrow(AppOpsManager.OP_LEGACY_STORAGE,
1151 context.getApplicationInfo().uid,
1152 context.getPackageName()) != AppOpsManager.MODE_ALLOWED;
1153 }
1154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 static File getDirectory(String variableName, String defaultPath) {
1156 String path = System.getenv(variableName);
1157 return path == null ? new File(defaultPath) : new File(path);
1158 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001159
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001160 /** {@hide} */
1161 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001162 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001163 }
1164
1165 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001166 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001167 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1168 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001169 }
1170 }
1171
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001172 /**
1173 * Append path segments to each given base path, returning result.
1174 *
1175 * @hide
1176 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001177 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001178 public static File[] buildPaths(File[] base, String... segments) {
1179 File[] result = new File[base.length];
1180 for (int i = 0; i < base.length; i++) {
1181 result[i] = buildPath(base[i], segments);
1182 }
1183 return result;
1184 }
1185
1186 /**
1187 * Append path segments to given base path, returning result.
1188 *
1189 * @hide
1190 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001191 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001192 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001193 File cur = base;
1194 for (String segment : segments) {
1195 if (cur == null) {
1196 cur = new File(segment);
1197 } else {
1198 cur = new File(cur, segment);
1199 }
1200 }
1201 return cur;
1202 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001203
1204 /**
1205 * If the given path exists on emulated external storage, return the
1206 * translated backing path hosted on internal storage. This bypasses any
1207 * emulation later, improving performance. This is <em>only</em> suitable
1208 * for read-only access.
1209 * <p>
1210 * Returns original path if given path doesn't meet these criteria. Callers
1211 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1212 * permission.
1213 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001214 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001215 * @hide
1216 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001217 @UnsupportedAppUsage
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001218 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001219 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001220 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222}