blob: c32c264367aeb0aad6dd87812dd15c2238cfebfa [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";
57 private static final String DIR_DATA = "data";
58 private static final String DIR_MEDIA = "media";
59 private static final String DIR_OBB = "obb";
60 private static final String DIR_FILES = "files";
61 private static final String DIR_CACHE = "cache";
62
63 /** {@hide} */
64 @Deprecated
65 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070066
Jeff Sharkey63d0a062013-03-01 16:12:55 -080067 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070068 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060069 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070070 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080071 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080072 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080073 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070074 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090075 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Dario Freni2bef1762018-06-01 14:02:08 +010076 private static final File DIR_PRODUCT_SERVICES_ROOT = getDirectory(ENV_PRODUCT_SERVICES_ROOT,
77 "/product_services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
Andrei Onea24ec3212019-03-15 17:35:05 +000079 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000080 private static UserEnvironment sCurrentUser;
81 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070082
Andreas Gamped281b422016-07-08 03:50:27 +000083 static {
84 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070085 }
86
87 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +000088 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000089 public static void initForCurrentUser() {
90 final int userId = UserHandle.myUserId();
91 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070092 }
93
94 /** {@hide} */
95 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070096 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070097
Andrei Onea24ec3212019-03-15 17:35:05 +000098 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -070099 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700100 mUserId = userId;
101 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700102
Andrei Onea24ec3212019-03-15 17:35:05 +0000103 @UnsupportedAppUsage
Jeff Sharkey48877892015-03-18 11:27:19 -0700104 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -0700105 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
106 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700107 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700108 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700109 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700110 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700111 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700112 }
113
Andrei Onea24ec3212019-03-15 17:35:05 +0000114 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700115 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700116 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700117 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700118 }
119
Andrei Onea24ec3212019-03-15 17:35:05 +0000120 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700121 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700122 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700123 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700124 }
125
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700126 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700127 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700128 }
129
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700130 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700131 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700132 }
133
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700134 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700135 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700136 }
137
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700138 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700139 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700140 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700141
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700142 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700143 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700144 }
145
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700146 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700147 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700148 }
149
150 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700151 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700152 }
153
154 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700155 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700156 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700157 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800160 * Return root of the "system" partition holding the core Android OS.
161 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700163 public static @NonNull File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800164 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
166
Jeff Sharkey48877892015-03-18 11:27:19 -0700167 /** {@hide} */
Jeff Sharkeyc6091162018-06-29 17:15:40 -0600168 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700169 public static @NonNull File getStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700170 return DIR_ANDROID_STORAGE;
171 }
172
Jason parksa3cdaa52011-01-13 14:15:43 -0600173 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800174 * Return root directory of the "oem" partition holding OEM customizations,
175 * if any. If present, the partition is mounted read-only.
176 *
177 * @hide
178 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600179 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700180 public static @NonNull File getOemDirectory() {
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800181 return DIR_OEM_ROOT;
182 }
183
184 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800185 * Return root directory of the "odm" partition holding ODM customizations,
186 * if any. If present, the partition is mounted read-only.
187 *
188 * @hide
189 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600190 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700191 public static @NonNull File getOdmDirectory() {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800192 return DIR_ODM_ROOT;
193 }
194
195 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700196 * Return root directory of the "vendor" partition that holds vendor-provided
197 * software that should persist across simple reflashing of the "system" partition.
198 * @hide
199 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600200 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700201 public static @NonNull File getVendorDirectory() {
Christopher Tate740888f2014-04-18 12:24:57 -0700202 return DIR_VENDOR_ROOT;
203 }
204
Jason parksa3cdaa52011-01-13 14:15:43 -0600205 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900206 * Return root directory of the "product" partition holding product-specific
207 * customizations if any. If present, the partition is mounted read-only.
208 *
209 * @hide
210 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600211 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000212 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700213 public static @NonNull File getProductDirectory() {
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900214 return DIR_PRODUCT_ROOT;
215 }
216
217 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100218 * Return root directory of the "product_services" partition holding middleware
219 * services if any. If present, the partition is mounted read-only.
220 *
221 * @hide
222 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600223 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700224 public static @NonNull File getProductServicesDirectory() {
Dario Freni2bef1762018-06-01 14:02:08 +0100225 return DIR_PRODUCT_SERVICES_ROOT;
226 }
227
228 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700229 * Return the system directory for a user. This is for use by system
230 * services to store files relating to the user. This directory will be
231 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700232 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600233 * @deprecated This directory is valid and still exists, but but callers
234 * should <em>strongly</em> consider switching to using either
235 * {@link #getDataSystemCeDirectory(int)} or
236 * {@link #getDataSystemDeDirectory(int)}, both of which support
237 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700238 * @hide
239 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700240 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700241 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800242 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700243 }
244
245 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700246 * Returns the config directory for a user. This is for use by system
247 * services to store files relating to the user which should be readable by
248 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100249 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700250 * @deprecated This directory is valid and still exists, but callers should
251 * <em>strongly</em> consider switching to
252 * {@link #getDataMiscCeDirectory(int)} which is protected with
253 * user credentials or {@link #getDataMiscDeDirectory(int)}
254 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100255 * @hide
256 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700257 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100258 public static File getUserConfigDirectory(int userId) {
259 return new File(new File(new File(
260 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
261 }
262
263 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700264 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 */
266 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800267 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 }
269
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700270 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700271 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700272 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800273 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700274 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700275 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700276 }
277 }
278
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700279 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600280 public static File getExpandDirectory() {
281 return DIR_ANDROID_EXPAND;
282 }
283
284 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000285 @UnsupportedAppUsage
Jeff Sharkey15447792015-11-05 16:18:51 -0800286 public static File getDataSystemDirectory() {
287 return new File(getDataDirectory(), "system");
288 }
289
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700290 /**
291 * Returns the base directory for per-user system directory, device encrypted.
292 * {@hide}
293 */
294 public static File getDataSystemDeDirectory() {
295 return buildPath(getDataDirectory(), "system_de");
296 }
297
298 /**
299 * Returns the base directory for per-user system directory, credential encrypted.
300 * {@hide}
301 */
302 public static File getDataSystemCeDirectory() {
303 return buildPath(getDataDirectory(), "system_ce");
304 }
305
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600306 /**
307 * Return the "credential encrypted" system directory for a user. This is
308 * for use by system services to store files relating to the user. This
309 * directory supports fast user wipe, and will be automatically deleted when
310 * the user is removed.
311 * <p>
312 * Data stored under this path is "credential encrypted", which uses an
313 * encryption key that is entangled with user credentials, such as a PIN or
314 * password. The contents will only be available once the user has been
315 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
316 * <p>
317 * New code should <em>strongly</em> prefer storing sensitive data in these
318 * credential encrypted areas.
319 *
320 * @hide
321 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700322 public static File getDataSystemCeDirectory(int userId) {
323 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800324 }
325
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600326 /**
327 * Return the "device encrypted" system directory for a user. This is for
328 * use by system services to store files relating to the user. This
329 * directory supports fast user wipe, and will be automatically deleted when
330 * the user is removed.
331 * <p>
332 * Data stored under this path is "device encrypted", which uses an
333 * encryption key that is tied to the physical device. The contents will
334 * only be available once the device has finished a {@code dm-verity}
335 * protected boot.
336 * <p>
337 * New code should <em>strongly</em> avoid storing sensitive data in these
338 * device encrypted areas.
339 *
340 * @hide
341 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700342 public static File getDataSystemDeDirectory(int userId) {
343 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
344 }
345
346 /** {@hide} */
347 public static File getDataMiscDirectory() {
348 return new File(getDataDirectory(), "misc");
349 }
350
351 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800352 public static File getDataMiscCeDirectory() {
353 return buildPath(getDataDirectory(), "misc_ce");
354 }
355
356 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700357 public static File getDataMiscCeDirectory(int userId) {
358 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
359 }
360
361 /** {@hide} */
362 public static File getDataMiscDeDirectory(int userId) {
363 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800364 }
365
Calin Juravled479b522016-02-24 16:22:03 +0000366 private static File getDataProfilesDeDirectory(int userId) {
367 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
368 }
369
370 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800371 public static File getDataVendorCeDirectory(int userId) {
372 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
373 }
374
375 /** {@hide} */
376 public static File getDataVendorDeDirectory(int userId) {
377 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
378 }
379
380 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800381 public static File getDataRefProfilesDePackageDirectory(String packageName) {
382 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100383 }
384
385 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000386 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
387 return buildPath(getDataProfilesDeDirectory(userId), packageName);
388 }
389
390 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700391 public static File getDataAppDirectory(String volumeUuid) {
392 return new File(getDataDirectory(volumeUuid), "app");
393 }
394
395 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000396 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery296c8b92019-02-27 12:06:24 +0000397 return new File(getDataDirectory(volumeUuid), "app-staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000398 }
399
400 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700401 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700402 return new File(getDataDirectory(volumeUuid), "user");
403 }
404
405 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700406 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
407 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700408 }
409
410 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700411 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700412 String packageName) {
413 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700414 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800415 }
416
417 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700418 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800419 return new File(getDataDirectory(volumeUuid), "user_de");
420 }
421
422 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700423 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
424 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800425 }
426
427 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700428 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800429 String packageName) {
430 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700431 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700432 }
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700435 * Return preloads directory.
436 * <p>This directory may contain pre-loaded content such as
437 * {@link #getDataPreloadsDemoDirectory() demo videos} and
438 * {@link #getDataPreloadsAppsDirectory() APK files} .
439 * {@hide}
440 */
441 public static File getDataPreloadsDirectory() {
442 return new File(getDataDirectory(), "preloads");
443 }
444
445 /**
446 * @see #getDataPreloadsDirectory()
447 * {@hide}
448 */
449 public static File getDataPreloadsDemoDirectory() {
450 return new File(getDataPreloadsDirectory(), "demo");
451 }
452
453 /**
454 * @see #getDataPreloadsDirectory()
455 * {@hide}
456 */
457 public static File getDataPreloadsAppsDirectory() {
458 return new File(getDataPreloadsDirectory(), "apps");
459 }
460
461 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700462 * @see #getDataPreloadsDirectory()
463 * {@hide}
464 */
465 public static File getDataPreloadsMediaDirectory() {
466 return new File(getDataPreloadsDirectory(), "media");
467 }
468
469 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700470 * Returns location of preloaded cache directory for package name
471 * @see #getDataPreloadsDirectory()
472 * {@hide}
473 */
474 public static File getDataPreloadsFileCacheDirectory(String packageName) {
475 return new File(getDataPreloadsFileCacheDirectory(), packageName);
476 }
477
478 /**
479 * Returns location of preloaded cache directory.
480 * @see #getDataPreloadsDirectory()
481 * {@hide}
482 */
483 public static File getDataPreloadsFileCacheDirectory() {
484 return new File(getDataPreloadsDirectory(), "file_cache");
485 }
486
487 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800488 * Returns location of packages cache directory.
489 * {@hide}
490 */
491 public static File getPackageCacheDirectory() {
492 return new File(getDataSystemDirectory(), "package_cache");
493 }
494
495 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700496 * Return the primary shared/external storage directory. This directory may
497 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800498 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700499 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800500 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700501 * <p>
502 * <em>Note: don't be confused by the word "external" here. This directory
503 * can better be thought as media/shared storage. It is a filesystem that
504 * can hold a relatively large amount of data and that is shared across all
505 * applications (does not enforce permissions). Traditionally this is an SD
506 * card, but it may also be implemented as built-in storage in a device that
507 * is distinct from the protected internal storage and can be mounted as a
508 * filesystem on a computer.</em>
509 * <p>
510 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700511 * each user has their own isolated shared storage. Applications only have
512 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700513 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700514 * In devices with multiple shared/external storage directories, this
515 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700516 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700517 * {@link Context#getExternalFilesDirs(String)},
518 * {@link Context#getExternalCacheDirs()}, and
519 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700520 * <p>
521 * Applications should not directly use this top-level directory, in order
522 * to avoid polluting the user's root namespace. Any files that are private
523 * to the application should be placed in a directory returned by
524 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700525 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700526 * if the application is uninstalled. Other shared files should be placed in
527 * one of the directories returned by
528 * {@link #getExternalStoragePublicDirectory}.
529 * <p>
530 * Writing to this path requires the
531 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800532 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700533 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700534 * which is automatically granted if you hold the write permission.
535 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700536 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700537 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700538 * {@link Context#getExternalFilesDir(String)},
539 * {@link Context#getExternalCacheDir()}, or
540 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700541 * read or write.
542 * <p>
543 * This path may change between platform versions, so applications should
544 * only persist relative paths.
545 * <p>
546 * Here is an example of typical code to monitor the state of external
547 * storage:
548 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700549 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800550 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700551 *
552 * @see #getExternalStorageState()
553 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 */
555 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700556 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000557 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700558 }
559
560 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000561 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700562 public static File getLegacyExternalStorageDirectory() {
563 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700566 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000567 @UnsupportedAppUsage
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700568 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700569 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700570 }
571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800573 * Standard directory in which to place any audio files that should be
574 * in the regular list of music for the user.
575 * This may be combined with
576 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
577 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
578 * of directories to categories a particular audio file as more than one
579 * type.
580 */
581 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800582
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800583 /**
584 * Standard directory in which to place any audio files that should be
585 * in the list of podcasts that the user can select (not as regular
586 * music).
587 * This may be combined with {@link #DIRECTORY_MUSIC},
588 * {@link #DIRECTORY_NOTIFICATIONS},
589 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
590 * of directories to categories a particular audio file as more than one
591 * type.
592 */
593 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800594
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800595 /**
596 * Standard directory in which to place any audio files that should be
597 * in the list of ringtones that the user can select (not as regular
598 * music).
599 * This may be combined with {@link #DIRECTORY_MUSIC},
600 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
601 * {@link #DIRECTORY_ALARMS} as a series
602 * of directories to categories a particular audio file as more than one
603 * type.
604 */
605 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800606
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800607 /**
608 * Standard directory in which to place any audio files that should be
609 * in the list of alarms that the user can select (not as regular
610 * music).
611 * This may be combined with {@link #DIRECTORY_MUSIC},
612 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
613 * and {@link #DIRECTORY_RINGTONES} as a series
614 * of directories to categories a particular audio file as more than one
615 * type.
616 */
617 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800618
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800619 /**
620 * Standard directory in which to place any audio files that should be
621 * in the list of notifications that the user can select (not as regular
622 * music).
623 * This may be combined with {@link #DIRECTORY_MUSIC},
624 * {@link #DIRECTORY_PODCASTS},
625 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
626 * of directories to categories a particular audio file as more than one
627 * type.
628 */
629 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800630
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800631 /**
632 * Standard directory in which to place pictures 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 pictures
635 * in any directory.
636 */
637 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800638
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800639 /**
640 * Standard directory in which to place movies that are available to
641 * the user. Note that this is primarily a convention for the top-level
642 * public directory, as the media scanner will find and collect movies
643 * in any directory.
644 */
645 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800646
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800647 /**
648 * Standard directory in which to place files that have been downloaded by
649 * the user. Note that this is primarily a convention for the top-level
650 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700651 * private directories. Also note that though the constant here is
652 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
653 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800654 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700655 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800656
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800657 /**
658 * The traditional location for pictures and videos when mounting the
659 * device as a camera. Note that this is primarily a convention for the
660 * top-level public directory, as this convention makes no sense elsewhere.
661 */
662 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700663
664 /**
665 * Standard directory in which to place documents that have been created by
666 * the user.
667 */
668 public static String DIRECTORY_DOCUMENTS = "Documents";
669
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800670 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600671 * Standard directory in which to place screenshots that have been taken by
672 * the user. Typically used as a secondary directory under
673 * {@link #DIRECTORY_PICTURES}.
674 */
675 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
676
677 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700678 * Standard directory in which to place any audio files which are
679 * audiobooks.
680 */
681 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
682
683 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800684 * List of standard storage directories.
685 * <p>
686 * Each of its values have its own constant:
687 * <ul>
688 * <li>{@link #DIRECTORY_MUSIC}
689 * <li>{@link #DIRECTORY_PODCASTS}
690 * <li>{@link #DIRECTORY_ALARMS}
691 * <li>{@link #DIRECTORY_RINGTONES}
692 * <li>{@link #DIRECTORY_NOTIFICATIONS}
693 * <li>{@link #DIRECTORY_PICTURES}
694 * <li>{@link #DIRECTORY_MOVIES}
695 * <li>{@link #DIRECTORY_DOWNLOADS}
696 * <li>{@link #DIRECTORY_DCIM}
697 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700698 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800699 * </ul>
700 * @hide
701 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800702 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800703 DIRECTORY_MUSIC,
704 DIRECTORY_PODCASTS,
705 DIRECTORY_RINGTONES,
706 DIRECTORY_ALARMS,
707 DIRECTORY_NOTIFICATIONS,
708 DIRECTORY_PICTURES,
709 DIRECTORY_MOVIES,
710 DIRECTORY_DOWNLOADS,
711 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700712 DIRECTORY_DOCUMENTS,
713 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800714 };
715
716 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800717 * @hide
718 */
719 public static boolean isStandardDirectory(String dir) {
720 for (String valid : STANDARD_DIRECTORIES) {
721 if (valid.equals(dir)) {
722 return true;
723 }
724 }
725 return false;
726 }
727
Jeff Sharkey20356142017-12-06 15:22:05 -0700728 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
729 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
730 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
731 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
732 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
733 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
734 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
735 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
736 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
737 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700738 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700739
740 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
741 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
742
743 /**
744 * Classify the content types present on the given external storage device.
745 * <p>
746 * This is typically useful for deciding if an inserted SD card is empty, or
747 * if it contains content like photos that should be preserved.
748 *
749 * @hide
750 */
751 public static int classifyExternalStorageDirectory(File dir) {
752 int res = 0;
753 for (File f : FileUtils.listFilesOrEmpty(dir)) {
754 if (f.isFile() && isInterestingFile(f)) {
755 res |= HAS_OTHER;
756 } else if (f.isDirectory() && hasInterestingFiles(f)) {
757 final String name = f.getName();
758 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
759 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
760 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
761 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
762 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
763 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
764 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
765 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
766 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
767 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700768 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700769 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
770 else res |= HAS_OTHER;
771 }
772 }
773 return res;
774 }
775
776 private static boolean hasInterestingFiles(File dir) {
777 final LinkedList<File> explore = new LinkedList<>();
778 explore.add(dir);
779 while (!explore.isEmpty()) {
780 dir = explore.pop();
781 for (File f : FileUtils.listFilesOrEmpty(dir)) {
782 if (isInterestingFile(f)) return true;
783 if (f.isDirectory()) explore.add(f);
784 }
785 }
786 return false;
787 }
788
789 private static boolean isInterestingFile(File file) {
790 if (file.isFile()) {
791 final String name = file.getName().toLowerCase();
792 if (name.endsWith(".exe") || name.equals("autorun.inf")
793 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
794 return false;
795 } else {
796 return true;
797 }
798 } else {
799 return false;
800 }
801 }
802
Felipe Lemeb012f912016-01-22 16:49:55 -0800803 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700804 * Get a top-level shared/external storage directory for placing files of a
805 * particular type. This is where the user will typically place and manage
806 * their own files, so you should be careful about what you put here to
807 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800808 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700809 * <p>
810 * On devices with multiple users (as described by {@link UserManager}),
811 * each user has their own isolated shared storage. Applications only have
812 * access to the shared storage for the user they're running as.
813 * </p>
814 * <p>
815 * Here is an example of typical code to manipulate a picture on the public
816 * shared storage:
817 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800818 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
819 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800820 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700821 * @param type The type of storage directory to return. Should be one of
822 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
823 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
824 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800825 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
826 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700827 * @return Returns the File path for the directory. Note that this directory
828 * may not yet exist, so you must make sure it exists before using
829 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800830 */
831 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700832 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000833 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800834 }
835
836 /**
837 * Returns the path for android-specific data on the SD card.
838 * @hide
839 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000840 @UnsupportedAppUsage
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 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000850 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700851 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700852 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000853 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800854 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800855
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800856 /**
857 * Generates the raw path to an application's media
858 * @hide
859 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000860 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700861 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700862 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000863 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800864 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800865
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800866 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800867 * Generates the raw path to an application's OBB files
868 * @hide
869 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000870 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700871 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700872 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000873 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800874 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800875
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800876 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800877 * Generates the path to an application's files.
878 * @hide
879 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000880 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700881 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700882 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000883 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800884 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700885
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800886 /**
887 * Generates the path to an application's cache.
888 * @hide
889 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000890 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700891 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700892 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000893 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800894 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800895
Sudheer Shanka25f1c6e2019-04-22 17:03:47 -0700896 /** @hide */
897 public static File[] buildExternalStoragePublicDirs(@NonNull String dirType) {
898 throwIfUserRequired();
899 return sCurrentUser.buildExternalStoragePublicDirs(dirType);
900 }
901
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800902 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700903 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 */
905 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800906 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
908
909 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700910 * Unknown storage state, such as when a path isn't backed by known storage
911 * media.
912 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800913 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700914 */
915 public static final String MEDIA_UNKNOWN = "unknown";
916
917 /**
918 * Storage state if the media is not present.
919 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800920 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 */
922 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700925 * Storage state if the media is present but not mounted.
926 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800927 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 */
929 public static final String MEDIA_UNMOUNTED = "unmounted";
930
931 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700932 * Storage state if the media is present and being disk-checked.
933 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800934 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 */
936 public static final String MEDIA_CHECKING = "checking";
937
938 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700939 * Storage state if the media is present but is blank or is using an
940 * unsupported filesystem.
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_NOFS = "nofs";
945
946 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700947 * Storage state if the media is present and mounted at its mount point with
948 * read/write access.
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_MOUNTED = "mounted";
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-only 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_READ_ONLY = "mounted_ro";
961
962 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700963 * Storage state if the media is present not mounted, and shared via USB
964 * mass storage.
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_SHARED = "shared";
969
970 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700971 * Storage state if the media was removed before it was unmounted.
972 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800973 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 */
975 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
976
977 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700978 * Storage state if the media is present but cannot be mounted. Typically
979 * this happens if the file system on the media is corrupted.
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_UNMOUNTABLE = "unmountable";
984
985 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700986 * Storage state if the media is in the process of being ejected.
987 *
988 * @see #getExternalStorageState(File)
989 */
990 public static final String MEDIA_EJECTING = "ejecting";
991
992 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700993 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800994 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700995 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700996 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
997 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
998 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
999 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1000 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 */
1002 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +00001003 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001004 return getExternalStorageState(externalDir);
1005 }
1006
1007 /**
1008 * @deprecated use {@link #getExternalStorageState(File)}
1009 */
1010 @Deprecated
1011 public static String getStorageState(File path) {
1012 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001013 }
1014
1015 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001016 * Returns the current state of the shared/external storage media at the
1017 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001018 *
1019 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1020 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1021 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1022 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1023 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
1024 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001025 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001026 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001027 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001028 return volume.getState();
1029 } else {
1030 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 }
1033
Dianne Hackborn407f6252010-10-04 11:31:17 -07001034 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001035 * Returns whether the primary shared/external storage media is physically
1036 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001037 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001038 * @return true if the storage device can be removed (such as an SD card),
1039 * or false if the storage device is built in and cannot be
1040 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001041 */
1042 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001043 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001044 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001045 }
1046
Kenny Roote1ff2142010-10-12 11:20:01 -07001047 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001048 * Returns whether the shared/external storage media at the given path is
1049 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001050 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001051 * @return true if the storage device can be removed (such as an SD card),
1052 * or false if the storage device is built in and cannot be
1053 * physically removed.
1054 * @throws IllegalArgumentException if the path is not a valid storage
1055 * device.
1056 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001057 public static boolean isExternalStorageRemovable(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001058 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001059 if (volume != null) {
1060 return volume.isRemovable();
1061 } else {
1062 throw new IllegalArgumentException("Failed to find storage device at " + path);
1063 }
1064 }
1065
1066 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001067 * Returns whether the primary shared/external storage media is emulated.
1068 * <p>
1069 * The contents of emulated storage devices are backed by a private user
1070 * data partition, which means there is little benefit to apps storing data
1071 * here instead of the private directories returned by
1072 * {@link Context#getFilesDir()}, etc.
1073 * <p>
1074 * This returns true when emulated storage is backed by either internal
1075 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001076 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001077 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1078 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001079 */
1080 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001081 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001082 return isExternalStorageEmulated(externalDir);
1083 }
1084
1085 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001086 * Returns whether the shared/external storage media at the given path is
1087 * emulated.
1088 * <p>
1089 * The contents of emulated storage devices are backed by a private user
1090 * data partition, which means there is little benefit to apps storing data
1091 * here instead of the private directories returned by
1092 * {@link Context#getFilesDir()}, etc.
1093 * <p>
1094 * This returns true when emulated storage is backed by either internal
1095 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001096 *
1097 * @throws IllegalArgumentException if the path is not a valid storage
1098 * device.
1099 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001100 public static boolean isExternalStorageEmulated(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001101 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001102 if (volume != null) {
1103 return volume.isEmulated();
1104 } else {
1105 throw new IllegalArgumentException("Failed to find storage device at " + path);
1106 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001107 }
1108
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001109 /**
1110 * Returns whether the shared/external storage media at the given path is a
1111 * sandboxed view that only contains files owned by the app.
1112 * <p>
1113 * This value may be different from the value requested by
1114 * {@code allowExternalStorageSandbox} in the app's manifest, since an app
1115 * may inherit its sandboxed state based on when it was first installed.
1116 * <p>
1117 * Sandboxed apps can continue to discover and read media belonging to other
1118 * apps via {@link android.provider.MediaStore}.
1119 */
1120 public static boolean isExternalStorageSandboxed() {
1121 final File externalDir = sCurrentUser.getExternalDirs()[0];
1122 return isExternalStorageSandboxed(externalDir);
1123 }
1124
1125 /**
1126 * Returns whether the shared/external storage media at the given path is a
1127 * sandboxed view that only contains files owned by the app.
1128 * <p>
1129 * This value may be different from the value requested by
1130 * {@code allowExternalStorageSandbox} in the app's manifest, since an app
1131 * may inherit its sandboxed state based on when it was first installed.
1132 * <p>
1133 * Sandboxed apps can continue to discover and read media belonging to other
1134 * apps via {@link android.provider.MediaStore}.
1135 *
1136 * @throws IllegalArgumentException if the path is not a valid storage
1137 * device.
1138 */
1139 public static boolean isExternalStorageSandboxed(@NonNull File path) {
1140 final Context context = AppGlobals.getInitialApplication();
1141 final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
Jeff Sharkeye9fcabc2019-04-04 11:19:22 -06001142
Svet Ganovd563e932019-04-14 13:07:41 -07001143 return appOps.checkOpNoThrow(AppOpsManager.OP_LEGACY_STORAGE,
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001144 context.getApplicationInfo().uid,
Svet Ganovd563e932019-04-14 13:07:41 -07001145 context.getOpPackageName()) != AppOpsManager.MODE_ALLOWED;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001146 }
1147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 static File getDirectory(String variableName, String defaultPath) {
1149 String path = System.getenv(variableName);
1150 return path == null ? new File(defaultPath) : new File(path);
1151 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001152
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001153 /** {@hide} */
1154 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001155 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001156 }
1157
1158 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001159 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001160 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1161 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001162 }
1163 }
1164
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001165 /**
1166 * Append path segments to each given base path, returning result.
1167 *
1168 * @hide
1169 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001170 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001171 public static File[] buildPaths(File[] base, String... segments) {
1172 File[] result = new File[base.length];
1173 for (int i = 0; i < base.length; i++) {
1174 result[i] = buildPath(base[i], segments);
1175 }
1176 return result;
1177 }
1178
1179 /**
1180 * Append path segments to given base path, returning result.
1181 *
1182 * @hide
1183 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001184 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001185 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001186 File cur = base;
1187 for (String segment : segments) {
1188 if (cur == null) {
1189 cur = new File(segment);
1190 } else {
1191 cur = new File(cur, segment);
1192 }
1193 }
1194 return cur;
1195 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001196
1197 /**
1198 * If the given path exists on emulated external storage, return the
1199 * translated backing path hosted on internal storage. This bypasses any
1200 * emulation later, improving performance. This is <em>only</em> suitable
1201 * for read-only access.
1202 * <p>
1203 * Returns original path if given path doesn't meet these criteria. Callers
1204 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1205 * permission.
1206 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001207 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001208 * @hide
1209 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001210 @UnsupportedAppUsage
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001211 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001212 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001213 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215}