blob: 6d5fe53b3f3d16670a62649fb7ae4deef5e082f3 [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
Sudheer Shanka572fdfd2019-06-04 16:46:57 -070019import android.Manifest;
Jeff Sharkeya30e5c32019-02-28 12:02:10 -070020import android.annotation.NonNull;
Jeff Sharkey70eb34a2018-09-13 11:57:03 -060021import android.annotation.SystemApi;
Philip P. Moltmannf80809f2018-04-04 11:20:44 -070022import android.annotation.TestApi;
Andrei Onea24ec3212019-03-15 17:35:05 +000023import android.annotation.UnsupportedAppUsage;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -060024import android.app.AppGlobals;
25import android.app.AppOpsManager;
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080026import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070027import android.content.Context;
Jeff Sharkey488162b2019-04-27 17:03:54 -060028import android.content.Intent;
Sudheer Shanka572fdfd2019-06-04 16:46:57 -070029import android.content.pm.PackageManager;
Jeff Sharkey48877892015-03-18 11:27:19 -070030import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070031import android.os.storage.StorageVolume;
Jeff Sharkey488162b2019-04-27 17:03:54 -060032import android.provider.MediaStore;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070033import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080034import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080035
Gilles Debunneee1d6302011-05-13 10:09:32 -070036import java.io.File;
Jeff Sharkey20356142017-12-06 15:22:05 -070037import java.util.LinkedList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039/**
40 * Provides access to environment variables.
41 */
42public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080043 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Jeff Sharkeydd02e332018-06-27 14:41:57 -060045 // NOTE: keep credential-protected paths in sync with StrictMode.java
46
Jeff Sharkeyb049e212012-09-07 23:16:01 -070047 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080048 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070049 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060050 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070051 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080052 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080053 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080054 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070055 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090056 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Jeongik Cha9ec059a2019-07-04 21:12:06 +090057 private static final String ENV_SYSTEM_EXT_ROOT = "SYSTEM_EXT_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070058
Jeff Sharkeydfa45302012-09-12 16:25:22 -070059 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070060 public static final String DIR_ANDROID = "Android";
61 private static final String DIR_DATA = "data";
62 private static final String DIR_MEDIA = "media";
63 private static final String DIR_OBB = "obb";
64 private static final String DIR_FILES = "files";
65 private static final String DIR_CACHE = "cache";
66
67 /** {@hide} */
68 @Deprecated
69 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070070
Jeff Sharkey63d0a062013-03-01 16:12:55 -080071 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070072 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060073 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070074 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080075 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080076 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080077 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070078 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090079 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Jeongik Cha9ec059a2019-07-04 21:12:06 +090080 private static final File DIR_SYSTEM_EXT_ROOT = getDirectory(ENV_SYSTEM_EXT_ROOT,
81 "/system_ext");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Andrei Onea24ec3212019-03-15 17:35:05 +000083 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000084 private static UserEnvironment sCurrentUser;
85 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070086
Andreas Gamped281b422016-07-08 03:50:27 +000087 static {
88 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070089 }
90
91 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +000092 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000093 public static void initForCurrentUser() {
94 final int userId = UserHandle.myUserId();
95 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070096 }
97
98 /** {@hide} */
99 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -0700100 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700101
Andrei Onea24ec3212019-03-15 17:35:05 +0000102 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700103 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700104 mUserId = userId;
105 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700106
Andrei Onea24ec3212019-03-15 17:35:05 +0000107 @UnsupportedAppUsage
Jeff Sharkey48877892015-03-18 11:27:19 -0700108 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -0700109 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
110 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700111 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700112 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700113 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700114 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700115 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700116 }
117
Andrei Onea24ec3212019-03-15 17:35:05 +0000118 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700119 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700120 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700121 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700122 }
123
Andrei Onea24ec3212019-03-15 17:35:05 +0000124 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700125 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700126 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700127 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700128 }
129
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700130 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700131 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700132 }
133
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700134 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700135 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700136 }
137
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700138 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700139 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700140 }
141
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700142 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700143 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700144 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700145
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700146 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700147 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700148 }
149
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700150 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700151 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700152 }
153
154 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700155 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700156 }
157
158 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700159 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700160 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700161 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800164 * Return root of the "system" partition holding the core Android OS.
165 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700167 public static @NonNull File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800168 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
170
Jeff Sharkeyec19e9b2019-08-22 11:12:11 -0600171 /**
172 * Return root directory where all external storage devices will be mounted.
173 * For example, {@link #getExternalStorageDirectory()} will appear under
174 * this location.
175 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700176 public static @NonNull File getStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700177 return DIR_ANDROID_STORAGE;
178 }
179
Jason parksa3cdaa52011-01-13 14:15:43 -0600180 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800181 * Return root directory of the "oem" partition holding OEM customizations,
182 * if any. If present, the partition is mounted read-only.
183 *
184 * @hide
185 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600186 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700187 public static @NonNull File getOemDirectory() {
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800188 return DIR_OEM_ROOT;
189 }
190
191 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800192 * Return root directory of the "odm" partition holding ODM customizations,
193 * if any. If present, the partition is mounted read-only.
194 *
195 * @hide
196 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600197 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700198 public static @NonNull File getOdmDirectory() {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800199 return DIR_ODM_ROOT;
200 }
201
202 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700203 * Return root directory of the "vendor" partition that holds vendor-provided
204 * software that should persist across simple reflashing of the "system" partition.
205 * @hide
206 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600207 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700208 public static @NonNull File getVendorDirectory() {
Christopher Tate740888f2014-04-18 12:24:57 -0700209 return DIR_VENDOR_ROOT;
210 }
211
Jason parksa3cdaa52011-01-13 14:15:43 -0600212 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900213 * Return root directory of the "product" partition holding product-specific
214 * customizations if any. If present, the partition is mounted read-only.
215 *
216 * @hide
217 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600218 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000219 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700220 public static @NonNull File getProductDirectory() {
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900221 return DIR_PRODUCT_ROOT;
222 }
223
224 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100225 * Return root directory of the "product_services" partition holding middleware
226 * services if any. If present, the partition is mounted read-only.
227 *
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900228 * @deprecated This directory is not guaranteed to exist.
229 * Its name is changed to "system_ext" because the partition's purpose is changed.
230 * {@link #getSystemExtDirectory()}
Dario Freni2bef1762018-06-01 14:02:08 +0100231 * @hide
232 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600233 @SystemApi
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900234 @Deprecated
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700235 public static @NonNull File getProductServicesDirectory() {
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900236 return getDirectory("PRODUCT_SERVICES_ROOT", "/product_services");
237 }
238
239 /**
240 * Return root directory of the "system_ext" partition holding system partition's extension
241 * If present, the partition is mounted read-only.
242 *
243 * @hide
244 */
245 @SystemApi
246 public static @NonNull File getSystemExtDirectory() {
247 return DIR_SYSTEM_EXT_ROOT;
Dario Freni2bef1762018-06-01 14:02:08 +0100248 }
249
250 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700251 * Return the system directory for a user. This is for use by system
252 * services to store files relating to the user. This directory will be
253 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700254 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600255 * @deprecated This directory is valid and still exists, but but callers
256 * should <em>strongly</em> consider switching to using either
257 * {@link #getDataSystemCeDirectory(int)} or
258 * {@link #getDataSystemDeDirectory(int)}, both of which support
259 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700260 * @hide
261 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700262 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700263 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800264 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700265 }
266
267 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700268 * Returns the config directory for a user. This is for use by system
269 * services to store files relating to the user which should be readable by
270 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100271 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700272 * @deprecated This directory is valid and still exists, but callers should
273 * <em>strongly</em> consider switching to
274 * {@link #getDataMiscCeDirectory(int)} which is protected with
275 * user credentials or {@link #getDataMiscDeDirectory(int)}
276 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100277 * @hide
278 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700279 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100280 public static File getUserConfigDirectory(int userId) {
281 return new File(new File(new File(
282 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
283 }
284
285 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700286 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 */
288 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800289 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 }
291
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700292 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700293 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700294 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800295 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700296 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700297 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700298 }
299 }
300
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700301 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600302 public static File getExpandDirectory() {
303 return DIR_ANDROID_EXPAND;
304 }
305
306 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000307 @UnsupportedAppUsage
Jeff Sharkey15447792015-11-05 16:18:51 -0800308 public static File getDataSystemDirectory() {
309 return new File(getDataDirectory(), "system");
310 }
311
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700312 /**
313 * Returns the base directory for per-user system directory, device encrypted.
314 * {@hide}
315 */
316 public static File getDataSystemDeDirectory() {
317 return buildPath(getDataDirectory(), "system_de");
318 }
319
320 /**
321 * Returns the base directory for per-user system directory, credential encrypted.
322 * {@hide}
323 */
324 public static File getDataSystemCeDirectory() {
325 return buildPath(getDataDirectory(), "system_ce");
326 }
327
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600328 /**
329 * Return the "credential encrypted" system directory for a user. This is
330 * for use by system services to store files relating to the user. This
331 * directory supports fast user wipe, and will be automatically deleted when
332 * the user is removed.
333 * <p>
334 * Data stored under this path is "credential encrypted", which uses an
335 * encryption key that is entangled with user credentials, such as a PIN or
336 * password. The contents will only be available once the user has been
337 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
338 * <p>
339 * New code should <em>strongly</em> prefer storing sensitive data in these
340 * credential encrypted areas.
341 *
342 * @hide
343 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700344 public static File getDataSystemCeDirectory(int userId) {
345 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800346 }
347
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600348 /**
349 * Return the "device encrypted" system directory for a user. This is for
350 * use by system services to store files relating to the user. This
351 * directory supports fast user wipe, and will be automatically deleted when
352 * the user is removed.
353 * <p>
354 * Data stored under this path is "device encrypted", which uses an
355 * encryption key that is tied to the physical device. The contents will
356 * only be available once the device has finished a {@code dm-verity}
357 * protected boot.
358 * <p>
359 * New code should <em>strongly</em> avoid storing sensitive data in these
360 * device encrypted areas.
361 *
362 * @hide
363 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700364 public static File getDataSystemDeDirectory(int userId) {
365 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
366 }
367
368 /** {@hide} */
369 public static File getDataMiscDirectory() {
370 return new File(getDataDirectory(), "misc");
371 }
372
373 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800374 public static File getDataMiscCeDirectory() {
375 return buildPath(getDataDirectory(), "misc_ce");
376 }
377
378 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700379 public static File getDataMiscCeDirectory(int userId) {
380 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
381 }
382
383 /** {@hide} */
384 public static File getDataMiscDeDirectory(int userId) {
385 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800386 }
387
Calin Juravled479b522016-02-24 16:22:03 +0000388 private static File getDataProfilesDeDirectory(int userId) {
389 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
390 }
391
392 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800393 public static File getDataVendorCeDirectory(int userId) {
394 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
395 }
396
397 /** {@hide} */
398 public static File getDataVendorDeDirectory(int userId) {
399 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
400 }
401
402 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800403 public static File getDataRefProfilesDePackageDirectory(String packageName) {
404 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100405 }
406
407 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000408 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
409 return buildPath(getDataProfilesDeDirectory(userId), packageName);
410 }
411
412 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700413 public static File getDataAppDirectory(String volumeUuid) {
414 return new File(getDataDirectory(volumeUuid), "app");
415 }
416
417 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000418 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery296c8b92019-02-27 12:06:24 +0000419 return new File(getDataDirectory(volumeUuid), "app-staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000420 }
421
422 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700423 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700424 return new File(getDataDirectory(volumeUuid), "user");
425 }
426
427 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700428 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
429 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700430 }
431
432 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700433 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700434 String packageName) {
435 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700436 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800437 }
438
439 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700440 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800441 return new File(getDataDirectory(volumeUuid), "user_de");
442 }
443
444 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700445 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
446 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800447 }
448
449 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700450 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800451 String packageName) {
452 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700453 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700454 }
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700457 * Return preloads directory.
458 * <p>This directory may contain pre-loaded content such as
459 * {@link #getDataPreloadsDemoDirectory() demo videos} and
460 * {@link #getDataPreloadsAppsDirectory() APK files} .
461 * {@hide}
462 */
463 public static File getDataPreloadsDirectory() {
464 return new File(getDataDirectory(), "preloads");
465 }
466
467 /**
468 * @see #getDataPreloadsDirectory()
469 * {@hide}
470 */
471 public static File getDataPreloadsDemoDirectory() {
472 return new File(getDataPreloadsDirectory(), "demo");
473 }
474
475 /**
476 * @see #getDataPreloadsDirectory()
477 * {@hide}
478 */
479 public static File getDataPreloadsAppsDirectory() {
480 return new File(getDataPreloadsDirectory(), "apps");
481 }
482
483 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700484 * @see #getDataPreloadsDirectory()
485 * {@hide}
486 */
487 public static File getDataPreloadsMediaDirectory() {
488 return new File(getDataPreloadsDirectory(), "media");
489 }
490
491 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700492 * Returns location of preloaded cache directory for package name
493 * @see #getDataPreloadsDirectory()
494 * {@hide}
495 */
496 public static File getDataPreloadsFileCacheDirectory(String packageName) {
497 return new File(getDataPreloadsFileCacheDirectory(), packageName);
498 }
499
500 /**
501 * Returns location of preloaded cache directory.
502 * @see #getDataPreloadsDirectory()
503 * {@hide}
504 */
505 public static File getDataPreloadsFileCacheDirectory() {
506 return new File(getDataPreloadsDirectory(), "file_cache");
507 }
508
509 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800510 * Returns location of packages cache directory.
511 * {@hide}
512 */
513 public static File getPackageCacheDirectory() {
514 return new File(getDataSystemDirectory(), "package_cache");
515 }
516
517 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700518 * Return the primary shared/external storage directory. This directory may
519 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800520 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700521 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800522 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700523 * <p>
524 * <em>Note: don't be confused by the word "external" here. This directory
525 * can better be thought as media/shared storage. It is a filesystem that
526 * can hold a relatively large amount of data and that is shared across all
527 * applications (does not enforce permissions). Traditionally this is an SD
528 * card, but it may also be implemented as built-in storage in a device that
529 * is distinct from the protected internal storage and can be mounted as a
530 * filesystem on a computer.</em>
531 * <p>
532 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700533 * each user has their own isolated shared storage. Applications only have
534 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700535 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700536 * In devices with multiple shared/external storage directories, this
537 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700538 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700539 * {@link Context#getExternalFilesDirs(String)},
540 * {@link Context#getExternalCacheDirs()}, and
541 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700542 * <p>
543 * Applications should not directly use this top-level directory, in order
544 * to avoid polluting the user's root namespace. Any files that are private
545 * to the application should be placed in a directory returned by
546 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700547 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700548 * if the application is uninstalled. Other shared files should be placed in
549 * one of the directories returned by
550 * {@link #getExternalStoragePublicDirectory}.
551 * <p>
552 * Writing to this path requires the
553 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Jeff Sharkey488162b2019-04-27 17:03:54 -0600554 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read
555 * access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700556 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700557 * which is automatically granted if you hold the write permission.
558 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700559 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700560 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700561 * {@link Context#getExternalFilesDir(String)},
562 * {@link Context#getExternalCacheDir()}, or
563 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700564 * read or write.
565 * <p>
566 * This path may change between platform versions, so applications should
567 * only persist relative paths.
568 * <p>
569 * Here is an example of typical code to monitor the state of external
570 * storage:
571 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700572 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800573 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700574 *
575 * @see #getExternalStorageState()
576 * @see #isExternalStorageRemovable()
Jeff Sharkey488162b2019-04-27 17:03:54 -0600577 * @deprecated To improve user privacy, direct access to shared/external
578 * storage devices is deprecated. When an app targets
579 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
580 * from this method is no longer directly accessible to apps.
581 * Apps can continue to access content stored on shared/external
582 * storage by migrating to alternatives such as
583 * {@link Context#getExternalFilesDir(String)},
584 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600586 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700588 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000589 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700590 }
591
592 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000593 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700594 public static File getLegacyExternalStorageDirectory() {
595 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
597
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700598 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000599 @UnsupportedAppUsage
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700600 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700601 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700602 }
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800605 * Standard directory in which to place any audio files that should be
606 * in the regular list of music for the user.
607 * This may be combined with
608 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
609 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
610 * of directories to categories a particular audio file as more than one
611 * type.
612 */
613 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800614
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800615 /**
616 * Standard directory in which to place any audio files that should be
617 * in the list of podcasts that the user can select (not as regular
618 * music).
619 * This may be combined with {@link #DIRECTORY_MUSIC},
620 * {@link #DIRECTORY_NOTIFICATIONS},
621 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
622 * of directories to categories a particular audio file as more than one
623 * type.
624 */
625 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800626
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800627 /**
628 * Standard directory in which to place any audio files that should be
629 * in the list of ringtones that the user can select (not as regular
630 * music).
631 * This may be combined with {@link #DIRECTORY_MUSIC},
632 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
633 * {@link #DIRECTORY_ALARMS} as a series
634 * of directories to categories a particular audio file as more than one
635 * type.
636 */
637 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800638
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800639 /**
640 * Standard directory in which to place any audio files that should be
641 * in the list of alarms that the user can select (not as regular
642 * music).
643 * This may be combined with {@link #DIRECTORY_MUSIC},
644 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
645 * and {@link #DIRECTORY_RINGTONES} as a series
646 * of directories to categories a particular audio file as more than one
647 * type.
648 */
649 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800650
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800651 /**
652 * Standard directory in which to place any audio files that should be
653 * in the list of notifications that the user can select (not as regular
654 * music).
655 * This may be combined with {@link #DIRECTORY_MUSIC},
656 * {@link #DIRECTORY_PODCASTS},
657 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
658 * of directories to categories a particular audio file as more than one
659 * type.
660 */
661 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800662
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800663 /**
664 * Standard directory in which to place pictures that are available to
665 * the user. Note that this is primarily a convention for the top-level
666 * public directory, as the media scanner will find and collect pictures
667 * in any directory.
668 */
669 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800670
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800671 /**
672 * Standard directory in which to place movies that are available to
673 * the user. Note that this is primarily a convention for the top-level
674 * public directory, as the media scanner will find and collect movies
675 * in any directory.
676 */
677 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800678
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800679 /**
680 * Standard directory in which to place files that have been downloaded by
681 * the user. Note that this is primarily a convention for the top-level
682 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700683 * private directories. Also note that though the constant here is
684 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
685 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800686 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700687 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800688
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800689 /**
690 * The traditional location for pictures and videos when mounting the
691 * device as a camera. Note that this is primarily a convention for the
692 * top-level public directory, as this convention makes no sense elsewhere.
693 */
694 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700695
696 /**
697 * Standard directory in which to place documents that have been created by
698 * the user.
699 */
700 public static String DIRECTORY_DOCUMENTS = "Documents";
701
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800702 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600703 * Standard directory in which to place screenshots that have been taken by
704 * the user. Typically used as a secondary directory under
705 * {@link #DIRECTORY_PICTURES}.
706 */
707 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
708
709 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700710 * Standard directory in which to place any audio files which are
711 * audiobooks.
712 */
713 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
714
715 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800716 * List of standard storage directories.
717 * <p>
718 * Each of its values have its own constant:
719 * <ul>
720 * <li>{@link #DIRECTORY_MUSIC}
721 * <li>{@link #DIRECTORY_PODCASTS}
722 * <li>{@link #DIRECTORY_ALARMS}
723 * <li>{@link #DIRECTORY_RINGTONES}
724 * <li>{@link #DIRECTORY_NOTIFICATIONS}
725 * <li>{@link #DIRECTORY_PICTURES}
726 * <li>{@link #DIRECTORY_MOVIES}
727 * <li>{@link #DIRECTORY_DOWNLOADS}
728 * <li>{@link #DIRECTORY_DCIM}
729 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700730 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800731 * </ul>
732 * @hide
733 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800734 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800735 DIRECTORY_MUSIC,
736 DIRECTORY_PODCASTS,
737 DIRECTORY_RINGTONES,
738 DIRECTORY_ALARMS,
739 DIRECTORY_NOTIFICATIONS,
740 DIRECTORY_PICTURES,
741 DIRECTORY_MOVIES,
742 DIRECTORY_DOWNLOADS,
743 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700744 DIRECTORY_DOCUMENTS,
745 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800746 };
747
748 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800749 * @hide
750 */
751 public static boolean isStandardDirectory(String dir) {
752 for (String valid : STANDARD_DIRECTORIES) {
753 if (valid.equals(dir)) {
754 return true;
755 }
756 }
757 return false;
758 }
759
Jeff Sharkey20356142017-12-06 15:22:05 -0700760 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
761 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
762 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
763 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
764 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
765 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
766 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
767 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
768 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
769 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700770 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700771
772 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
773 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
774
775 /**
776 * Classify the content types present on the given external storage device.
777 * <p>
778 * This is typically useful for deciding if an inserted SD card is empty, or
779 * if it contains content like photos that should be preserved.
780 *
781 * @hide
782 */
783 public static int classifyExternalStorageDirectory(File dir) {
784 int res = 0;
785 for (File f : FileUtils.listFilesOrEmpty(dir)) {
786 if (f.isFile() && isInterestingFile(f)) {
787 res |= HAS_OTHER;
788 } else if (f.isDirectory() && hasInterestingFiles(f)) {
789 final String name = f.getName();
790 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
791 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
792 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
793 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
794 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
795 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
796 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
797 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
798 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
799 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700800 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700801 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
802 else res |= HAS_OTHER;
803 }
804 }
805 return res;
806 }
807
808 private static boolean hasInterestingFiles(File dir) {
809 final LinkedList<File> explore = new LinkedList<>();
810 explore.add(dir);
811 while (!explore.isEmpty()) {
812 dir = explore.pop();
813 for (File f : FileUtils.listFilesOrEmpty(dir)) {
814 if (isInterestingFile(f)) return true;
815 if (f.isDirectory()) explore.add(f);
816 }
817 }
818 return false;
819 }
820
821 private static boolean isInterestingFile(File file) {
822 if (file.isFile()) {
823 final String name = file.getName().toLowerCase();
824 if (name.endsWith(".exe") || name.equals("autorun.inf")
825 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
826 return false;
827 } else {
828 return true;
829 }
830 } else {
831 return false;
832 }
833 }
834
Felipe Lemeb012f912016-01-22 16:49:55 -0800835 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700836 * Get a top-level shared/external storage directory for placing files of a
837 * particular type. This is where the user will typically place and manage
838 * their own files, so you should be careful about what you put here to
839 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800840 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700841 * <p>
842 * On devices with multiple users (as described by {@link UserManager}),
843 * each user has their own isolated shared storage. Applications only have
844 * access to the shared storage for the user they're running as.
845 * </p>
846 * <p>
847 * Here is an example of typical code to manipulate a picture on the public
848 * shared storage:
849 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800850 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
851 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800852 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700853 * @param type The type of storage directory to return. Should be one of
854 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
855 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
856 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800857 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
858 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700859 * @return Returns the File path for the directory. Note that this directory
860 * may not yet exist, so you must make sure it exists before using
861 * it such as with {@link File#mkdirs File.mkdirs()}.
Jeff Sharkey488162b2019-04-27 17:03:54 -0600862 * @deprecated To improve user privacy, direct access to shared/external
863 * storage devices is deprecated. When an app targets
864 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
865 * from this method is no longer directly accessible to apps.
866 * Apps can continue to access content stored on shared/external
867 * storage by migrating to alternatives such as
868 * {@link Context#getExternalFilesDir(String)},
869 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800870 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600871 @Deprecated
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800872 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700873 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000874 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800875 }
876
877 /**
878 * Returns the path for android-specific data on the SD card.
879 * @hide
880 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000881 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700882 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700883 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000884 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800885 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700886
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800887 /**
888 * Generates the raw path to an application's data
889 * @hide
890 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000891 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700892 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700893 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000894 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800895 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800896
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800897 /**
898 * Generates the raw path to an application's media
899 * @hide
900 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000901 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700902 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700903 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000904 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800905 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800906
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800907 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800908 * Generates the raw path to an application's OBB files
909 * @hide
910 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000911 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700912 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700913 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000914 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800915 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800916
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800917 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800918 * Generates the path to an application's files.
919 * @hide
920 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000921 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700922 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700923 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000924 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800925 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700926
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800927 /**
928 * Generates the path to an application's cache.
929 * @hide
930 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000931 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700932 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700933 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000934 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800935 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800936
Sudheer Shanka25f1c6e2019-04-22 17:03:47 -0700937 /** @hide */
938 public static File[] buildExternalStoragePublicDirs(@NonNull String dirType) {
939 throwIfUserRequired();
940 return sCurrentUser.buildExternalStoragePublicDirs(dirType);
941 }
942
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800943 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700944 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 */
946 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800947 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949
950 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700951 * Unknown storage state, such as when a path isn't backed by known storage
952 * media.
953 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800954 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700955 */
956 public static final String MEDIA_UNKNOWN = "unknown";
957
958 /**
959 * Storage state if the media is not present.
960 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800961 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 */
963 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700966 * Storage state if the media is present but not mounted.
967 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800968 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 */
970 public static final String MEDIA_UNMOUNTED = "unmounted";
971
972 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700973 * Storage state if the media is present and being disk-checked.
974 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800975 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 */
977 public static final String MEDIA_CHECKING = "checking";
978
979 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700980 * Storage state if the media is present but is blank or is using an
981 * unsupported filesystem.
982 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800983 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 */
985 public static final String MEDIA_NOFS = "nofs";
986
987 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700988 * Storage state if the media is present and mounted at its mount point with
989 * read/write access.
990 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800991 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 */
993 public static final String MEDIA_MOUNTED = "mounted";
994
995 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700996 * Storage state if the media is present and mounted at its mount point with
997 * read-only access.
998 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800999 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 */
1001 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
1002
1003 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001004 * Storage state if the media is present not mounted, and shared via USB
1005 * mass storage.
1006 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001007 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 */
1009 public static final String MEDIA_SHARED = "shared";
1010
1011 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001012 * Storage state if the media was removed before it was unmounted.
1013 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001014 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 */
1016 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
1017
1018 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001019 * Storage state if the media is present but cannot be mounted. Typically
1020 * this happens if the file system on the media is corrupted.
1021 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001022 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 */
1024 public static final String MEDIA_UNMOUNTABLE = "unmountable";
1025
1026 /**
Jeff Sharkey48877892015-03-18 11:27:19 -07001027 * Storage state if the media is in the process of being ejected.
1028 *
1029 * @see #getExternalStorageState(File)
1030 */
1031 public static final String MEDIA_EJECTING = "ejecting";
1032
1033 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001034 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -08001035 *
Jeff Sharkey8c165792012-10-22 14:08:29 -07001036 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001037 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1038 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1039 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1040 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1041 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +00001044 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001045 return getExternalStorageState(externalDir);
1046 }
1047
1048 /**
1049 * @deprecated use {@link #getExternalStorageState(File)}
1050 */
1051 @Deprecated
1052 public static String getStorageState(File path) {
1053 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001054 }
1055
1056 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001057 * Returns the current state of the shared/external storage media at the
1058 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001059 *
1060 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1061 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1062 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1063 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1064 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
1065 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001066 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001067 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001068 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001069 return volume.getState();
1070 } else {
1071 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074
Dianne Hackborn407f6252010-10-04 11:31:17 -07001075 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001076 * Returns whether the primary shared/external storage media is physically
1077 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001078 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001079 * @return true if the storage device can be removed (such as an SD card),
1080 * or false if the storage device is built in and cannot be
1081 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001082 */
1083 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001084 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001085 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001086 }
1087
Kenny Roote1ff2142010-10-12 11:20:01 -07001088 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001089 * Returns whether the shared/external storage media at the given path is
1090 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001091 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001092 * @return true if the storage device can be removed (such as an SD card),
1093 * or false if the storage device is built in and cannot be
1094 * physically removed.
1095 * @throws IllegalArgumentException if the path is not a valid storage
1096 * device.
1097 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001098 public static boolean isExternalStorageRemovable(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001099 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001100 if (volume != null) {
1101 return volume.isRemovable();
1102 } else {
1103 throw new IllegalArgumentException("Failed to find storage device at " + path);
1104 }
1105 }
1106
1107 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001108 * Returns whether the primary shared/external storage media is emulated.
1109 * <p>
1110 * The contents of emulated storage devices are backed by a private user
1111 * data partition, which means there is little benefit to apps storing data
1112 * here instead of the private directories returned by
1113 * {@link Context#getFilesDir()}, etc.
1114 * <p>
1115 * This returns true when emulated storage is backed by either internal
1116 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001117 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001118 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1119 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001120 */
1121 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001122 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001123 return isExternalStorageEmulated(externalDir);
1124 }
1125
1126 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001127 * Returns whether the shared/external storage media at the given path is
1128 * emulated.
1129 * <p>
1130 * The contents of emulated storage devices are backed by a private user
1131 * data partition, which means there is little benefit to apps storing data
1132 * here instead of the private directories returned by
1133 * {@link Context#getFilesDir()}, etc.
1134 * <p>
1135 * This returns true when emulated storage is backed by either internal
1136 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001137 *
1138 * @throws IllegalArgumentException if the path is not a valid storage
1139 * device.
1140 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001141 public static boolean isExternalStorageEmulated(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001142 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001143 if (volume != null) {
1144 return volume.isEmulated();
1145 } else {
1146 throw new IllegalArgumentException("Failed to find storage device at " + path);
1147 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001148 }
1149
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001150 /**
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001151 * Returns whether the primary shared/external storage media is a legacy
1152 * view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001153 * <p>
1154 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001155 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
1156 * may inherit its legacy state based on when it was first installed.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001157 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001158 * Non-legacy apps can continue to discover and read media belonging to
1159 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001160 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001161 public static boolean isExternalStorageLegacy() {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001162 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001163 return isExternalStorageLegacy(externalDir);
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001164 }
1165
1166 /**
1167 * Returns whether the shared/external storage media at the given path is a
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001168 * legacy view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001169 * <p>
1170 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001171 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
1172 * may inherit its legacy state based on when it was first installed.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001173 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001174 * Non-legacy apps can continue to discover and read media belonging to
1175 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001176 *
1177 * @throws IllegalArgumentException if the path is not a valid storage
1178 * device.
1179 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001180 public static boolean isExternalStorageLegacy(@NonNull File path) {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001181 final Context context = AppGlobals.getInitialApplication();
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001182 final int uid = context.getApplicationInfo().uid;
1183 if (Process.isIsolated(uid)) {
1184 return false;
1185 }
1186
1187 final PackageManager packageManager = context.getPackageManager();
1188 if (packageManager.isInstantApp()) {
1189 return false;
1190 }
1191
1192 if (packageManager.checkPermission(Manifest.permission.WRITE_MEDIA_STORAGE,
1193 context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
1194 return true;
1195 }
1196
1197 if (packageManager.checkPermission(Manifest.permission.INSTALL_PACKAGES,
1198 context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
1199 return true;
1200 }
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001201 final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001202 final String[] packagesForUid = packageManager.getPackagesForUid(uid);
1203 for (String packageName : packagesForUid) {
1204 if (appOps.checkOpNoThrow(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES,
1205 uid, packageName) == AppOpsManager.MODE_ALLOWED) {
1206 return true;
1207 }
1208 }
Jeff Sharkeye9fcabc2019-04-04 11:19:22 -06001209
Svet Ganovd563e932019-04-14 13:07:41 -07001210 return appOps.checkOpNoThrow(AppOpsManager.OP_LEGACY_STORAGE,
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001211 uid, context.getOpPackageName()) == AppOpsManager.MODE_ALLOWED;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001212 }
1213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 static File getDirectory(String variableName, String defaultPath) {
1215 String path = System.getenv(variableName);
1216 return path == null ? new File(defaultPath) : new File(path);
1217 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001218
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001219 /** {@hide} */
1220 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001221 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001222 }
1223
1224 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001225 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001226 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1227 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001228 }
1229 }
1230
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001231 /**
1232 * Append path segments to each given base path, returning result.
1233 *
1234 * @hide
1235 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001236 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001237 public static File[] buildPaths(File[] base, String... segments) {
1238 File[] result = new File[base.length];
1239 for (int i = 0; i < base.length; i++) {
1240 result[i] = buildPath(base[i], segments);
1241 }
1242 return result;
1243 }
1244
1245 /**
1246 * Append path segments to given base path, returning result.
1247 *
1248 * @hide
1249 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001250 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001251 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001252 File cur = base;
1253 for (String segment : segments) {
1254 if (cur == null) {
1255 cur = new File(segment);
1256 } else {
1257 cur = new File(cur, segment);
1258 }
1259 }
1260 return cur;
1261 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001262
1263 /**
1264 * If the given path exists on emulated external storage, return the
1265 * translated backing path hosted on internal storage. This bypasses any
1266 * emulation later, improving performance. This is <em>only</em> suitable
1267 * for read-only access.
1268 * <p>
1269 * Returns original path if given path doesn't meet these criteria. Callers
1270 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1271 * permission.
1272 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001273 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001274 * @hide
1275 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001276 @UnsupportedAppUsage
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001277 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001278 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001279 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281}