blob: 09ccb7238b34b6d58f2b35fb5130424c64e4a5ad [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;
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;
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +000026import android.compat.Compatibility;
Artur Satayevafdb23a2019-12-10 17:47:53 +000027import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070028import android.content.Context;
Jeff Sharkey488162b2019-04-27 17:03:54 -060029import android.content.Intent;
Sudheer Shanka572fdfd2019-06-04 16:46:57 -070030import android.content.pm.PackageManager;
Jeff Sharkey48877892015-03-18 11:27:19 -070031import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070032import android.os.storage.StorageVolume;
Jeff Sharkey488162b2019-04-27 17:03:54 -060033import android.provider.MediaStore;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070034import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080035import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080036
Gilles Debunneee1d6302011-05-13 10:09:32 -070037import java.io.File;
Hung-ying Tyan774d89e2020-02-05 16:55:01 +080038import java.io.IOException;
Jeff Sharkey04b4ba12019-12-15 22:42:42 -070039import java.util.ArrayList;
40import java.util.Collection;
Jeff Sharkey20356142017-12-06 15:22:05 -070041import java.util.LinkedList;
Hung-ying Tyan774d89e2020-02-05 16:55:01 +080042import java.util.List;
shafik81870652020-03-05 12:59:15 +000043import java.util.Objects;
Gilles Debunneee1d6302011-05-13 10:09:32 -070044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045/**
46 * Provides access to environment variables.
47 */
48public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080049 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Jeff Sharkeydd02e332018-06-27 14:41:57 -060051 // NOTE: keep credential-protected paths in sync with StrictMode.java
52
Jeff Sharkeyb049e212012-09-07 23:16:01 -070053 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080054 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070055 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060056 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070057 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080058 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080059 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080060 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070061 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090062 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Jeongik Cha9ec059a2019-07-04 21:12:06 +090063 private static final String ENV_SYSTEM_EXT_ROOT = "SYSTEM_EXT_ROOT";
Dario Frenic3e1bb722019-10-09 15:43:38 +010064 private static final String ENV_APEX_ROOT = "APEX_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070065
Jeff Sharkeydfa45302012-09-12 16:25:22 -070066 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070067 public static final String DIR_ANDROID = "Android";
68 private static final String DIR_DATA = "data";
69 private static final String DIR_MEDIA = "media";
70 private static final String DIR_OBB = "obb";
71 private static final String DIR_FILES = "files";
72 private static final String DIR_CACHE = "cache";
73
74 /** {@hide} */
75 @Deprecated
76 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070077
Jeff Sharkey63d0a062013-03-01 16:12:55 -080078 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070079 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060080 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070081 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080082 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080083 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080084 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070085 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090086 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Jeongik Cha9ec059a2019-07-04 21:12:06 +090087 private static final File DIR_SYSTEM_EXT_ROOT = getDirectory(ENV_SYSTEM_EXT_ROOT,
Dario Frenic3e1bb722019-10-09 15:43:38 +010088 "/system_ext");
89 private static final File DIR_APEX_ROOT = getDirectory(ENV_APEX_ROOT,
90 "/apex");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +000092 /**
93 * See definition in com.android.providers.media.LocalCallingIdentity
94 */
95 private static final long DEFAULT_SCOPED_STORAGE = 149924527L;
96
97 /**
98 * See definition in com.android.providers.media.LocalCallingIdentity
99 */
100 private static final long FORCE_ENABLE_SCOPED_STORAGE = 132649864L;
101
Andrei Onea24ec3212019-03-15 17:35:05 +0000102 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +0000103 private static UserEnvironment sCurrentUser;
104 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -0700105
Andreas Gamped281b422016-07-08 03:50:27 +0000106 static {
107 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700108 }
109
110 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000111 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +0000112 public static void initForCurrentUser() {
113 final int userId = UserHandle.myUserId();
114 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700115 }
116
117 /** {@hide} */
118 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -0700119 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700120
Andrei Onea24ec3212019-03-15 17:35:05 +0000121 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700122 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700123 mUserId = userId;
124 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700125
Andrei Onea24ec3212019-03-15 17:35:05 +0000126 @UnsupportedAppUsage
Jeff Sharkey48877892015-03-18 11:27:19 -0700127 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -0700128 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
129 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700130 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700131 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700132 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700133 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700134 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700135 }
136
Andrei Onea24ec3212019-03-15 17:35:05 +0000137 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700138 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700139 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700140 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700141 }
142
Andrei Onea24ec3212019-03-15 17:35:05 +0000143 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700144 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700145 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700146 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700147 }
148
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700149 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700150 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700151 }
152
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700153 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700154 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700155 }
156
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700157 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700158 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700159 }
160
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700161 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700162 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700163 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700164
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700165 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700166 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700167 }
168
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700169 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700170 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700171 }
172
173 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700174 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700175 }
176
177 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700178 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700179 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700180 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800183 * Return root of the "system" partition holding the core Android OS.
184 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700186 public static @NonNull File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800187 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 }
189
Jeff Sharkeyec19e9b2019-08-22 11:12:11 -0600190 /**
191 * Return root directory where all external storage devices will be mounted.
192 * For example, {@link #getExternalStorageDirectory()} will appear under
193 * this location.
194 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700195 public static @NonNull File getStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700196 return DIR_ANDROID_STORAGE;
197 }
198
Jason parksa3cdaa52011-01-13 14:15:43 -0600199 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800200 * Return root directory of the "oem" partition holding OEM customizations,
201 * if any. If present, the partition is mounted read-only.
202 *
203 * @hide
204 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600205 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700206 public static @NonNull File getOemDirectory() {
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800207 return DIR_OEM_ROOT;
208 }
209
210 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800211 * Return root directory of the "odm" partition holding ODM customizations,
212 * if any. If present, the partition is mounted read-only.
213 *
214 * @hide
215 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600216 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700217 public static @NonNull File getOdmDirectory() {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800218 return DIR_ODM_ROOT;
219 }
220
221 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700222 * Return root directory of the "vendor" partition that holds vendor-provided
223 * software that should persist across simple reflashing of the "system" partition.
224 * @hide
225 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600226 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700227 public static @NonNull File getVendorDirectory() {
Christopher Tate740888f2014-04-18 12:24:57 -0700228 return DIR_VENDOR_ROOT;
229 }
230
Jason parksa3cdaa52011-01-13 14:15:43 -0600231 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900232 * Return root directory of the "product" partition holding product-specific
233 * customizations if any. If present, the partition is mounted read-only.
234 *
235 * @hide
236 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600237 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000238 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700239 public static @NonNull File getProductDirectory() {
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900240 return DIR_PRODUCT_ROOT;
241 }
242
243 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100244 * Return root directory of the "product_services" partition holding middleware
245 * services if any. If present, the partition is mounted read-only.
246 *
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900247 * @deprecated This directory is not guaranteed to exist.
248 * Its name is changed to "system_ext" because the partition's purpose is changed.
249 * {@link #getSystemExtDirectory()}
Dario Freni2bef1762018-06-01 14:02:08 +0100250 * @hide
251 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600252 @SystemApi
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900253 @Deprecated
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700254 public static @NonNull File getProductServicesDirectory() {
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900255 return getDirectory("PRODUCT_SERVICES_ROOT", "/product_services");
256 }
257
258 /**
259 * Return root directory of the "system_ext" partition holding system partition's extension
260 * If present, the partition is mounted read-only.
261 *
262 * @hide
263 */
264 @SystemApi
265 public static @NonNull File getSystemExtDirectory() {
266 return DIR_SYSTEM_EXT_ROOT;
Dario Freni2bef1762018-06-01 14:02:08 +0100267 }
268
269 /**
Dario Frenic3e1bb722019-10-09 15:43:38 +0100270 * Return root directory of the apex mount point, where all the apex modules are made available
271 * to the rest of the system.
272 *
273 * @hide
274 */
275 public static @NonNull File getApexDirectory() {
276 return DIR_APEX_ROOT;
277 }
278
279 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700280 * Return the system directory for a user. This is for use by system
281 * services to store files relating to the user. This directory will be
282 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700283 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600284 * @deprecated This directory is valid and still exists, but but callers
285 * should <em>strongly</em> consider switching to using either
286 * {@link #getDataSystemCeDirectory(int)} or
287 * {@link #getDataSystemDeDirectory(int)}, both of which support
288 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700289 * @hide
290 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700291 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700292 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800293 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700294 }
295
296 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700297 * Returns the config directory for a user. This is for use by system
298 * services to store files relating to the user which should be readable by
299 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100300 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700301 * @deprecated This directory is valid and still exists, but callers should
302 * <em>strongly</em> consider switching to
303 * {@link #getDataMiscCeDirectory(int)} which is protected with
304 * user credentials or {@link #getDataMiscDeDirectory(int)}
305 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100306 * @hide
307 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700308 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100309 public static File getUserConfigDirectory(int userId) {
310 return new File(new File(new File(
311 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
312 }
313
314 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700315 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 */
317 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800318 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 }
320
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700321 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700322 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700323 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800324 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700325 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700326 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700327 }
328 }
329
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700330 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600331 public static File getExpandDirectory() {
332 return DIR_ANDROID_EXPAND;
333 }
334
335 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000336 @UnsupportedAppUsage
Jeff Sharkey15447792015-11-05 16:18:51 -0800337 public static File getDataSystemDirectory() {
338 return new File(getDataDirectory(), "system");
339 }
340
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700341 /**
342 * Returns the base directory for per-user system directory, device encrypted.
343 * {@hide}
344 */
345 public static File getDataSystemDeDirectory() {
346 return buildPath(getDataDirectory(), "system_de");
347 }
348
349 /**
350 * Returns the base directory for per-user system directory, credential encrypted.
351 * {@hide}
352 */
353 public static File getDataSystemCeDirectory() {
354 return buildPath(getDataDirectory(), "system_ce");
355 }
356
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600357 /**
358 * Return the "credential encrypted" system directory for a user. This is
359 * for use by system services to store files relating to the user. This
360 * directory supports fast user wipe, and will be automatically deleted when
361 * the user is removed.
362 * <p>
363 * Data stored under this path is "credential encrypted", which uses an
364 * encryption key that is entangled with user credentials, such as a PIN or
365 * password. The contents will only be available once the user has been
366 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
367 * <p>
368 * New code should <em>strongly</em> prefer storing sensitive data in these
369 * credential encrypted areas.
370 *
371 * @hide
372 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700373 public static File getDataSystemCeDirectory(int userId) {
374 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800375 }
376
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600377 /**
378 * Return the "device encrypted" system directory for a user. This is for
379 * use by system services to store files relating to the user. This
380 * directory supports fast user wipe, and will be automatically deleted when
381 * the user is removed.
382 * <p>
383 * Data stored under this path is "device encrypted", which uses an
384 * encryption key that is tied to the physical device. The contents will
385 * only be available once the device has finished a {@code dm-verity}
386 * protected boot.
387 * <p>
388 * New code should <em>strongly</em> avoid storing sensitive data in these
389 * device encrypted areas.
390 *
391 * @hide
392 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700393 public static File getDataSystemDeDirectory(int userId) {
394 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
395 }
396
397 /** {@hide} */
398 public static File getDataMiscDirectory() {
399 return new File(getDataDirectory(), "misc");
400 }
401
402 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800403 public static File getDataMiscCeDirectory() {
404 return buildPath(getDataDirectory(), "misc_ce");
405 }
406
407 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700408 public static File getDataMiscCeDirectory(int userId) {
409 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
410 }
411
412 /** {@hide} */
413 public static File getDataMiscDeDirectory(int userId) {
414 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800415 }
416
Calin Juravled479b522016-02-24 16:22:03 +0000417 private static File getDataProfilesDeDirectory(int userId) {
418 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
419 }
420
421 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800422 public static File getDataVendorCeDirectory(int userId) {
423 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
424 }
425
426 /** {@hide} */
427 public static File getDataVendorDeDirectory(int userId) {
428 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
429 }
430
431 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800432 public static File getDataRefProfilesDePackageDirectory(String packageName) {
433 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100434 }
435
436 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000437 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
438 return buildPath(getDataProfilesDeDirectory(userId), packageName);
439 }
440
441 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700442 public static File getDataAppDirectory(String volumeUuid) {
443 return new File(getDataDirectory(volumeUuid), "app");
444 }
445
446 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000447 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery296c8b92019-02-27 12:06:24 +0000448 return new File(getDataDirectory(volumeUuid), "app-staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000449 }
450
451 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700452 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700453 return new File(getDataDirectory(volumeUuid), "user");
454 }
455
456 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700457 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
458 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700459 }
460
461 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700462 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700463 String packageName) {
464 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700465 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800466 }
467
468 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700469 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800470 return new File(getDataDirectory(volumeUuid), "user_de");
471 }
472
473 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700474 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
475 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800476 }
477
478 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700479 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800480 String packageName) {
481 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700482 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700483 }
484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700486 * Return preloads directory.
487 * <p>This directory may contain pre-loaded content such as
488 * {@link #getDataPreloadsDemoDirectory() demo videos} and
489 * {@link #getDataPreloadsAppsDirectory() APK files} .
490 * {@hide}
491 */
492 public static File getDataPreloadsDirectory() {
493 return new File(getDataDirectory(), "preloads");
494 }
495
496 /**
497 * @see #getDataPreloadsDirectory()
498 * {@hide}
499 */
500 public static File getDataPreloadsDemoDirectory() {
501 return new File(getDataPreloadsDirectory(), "demo");
502 }
503
504 /**
505 * @see #getDataPreloadsDirectory()
506 * {@hide}
507 */
508 public static File getDataPreloadsAppsDirectory() {
509 return new File(getDataPreloadsDirectory(), "apps");
510 }
511
512 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700513 * @see #getDataPreloadsDirectory()
514 * {@hide}
515 */
516 public static File getDataPreloadsMediaDirectory() {
517 return new File(getDataPreloadsDirectory(), "media");
518 }
519
520 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700521 * Returns location of preloaded cache directory for package name
522 * @see #getDataPreloadsDirectory()
523 * {@hide}
524 */
525 public static File getDataPreloadsFileCacheDirectory(String packageName) {
526 return new File(getDataPreloadsFileCacheDirectory(), packageName);
527 }
528
529 /**
530 * Returns location of preloaded cache directory.
531 * @see #getDataPreloadsDirectory()
532 * {@hide}
533 */
534 public static File getDataPreloadsFileCacheDirectory() {
535 return new File(getDataPreloadsDirectory(), "file_cache");
536 }
537
538 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800539 * Returns location of packages cache directory.
540 * {@hide}
541 */
542 public static File getPackageCacheDirectory() {
543 return new File(getDataSystemDirectory(), "package_cache");
544 }
545
546 /**
Jeff Sharkey04b4ba12019-12-15 22:42:42 -0700547 * Return locations where media files (such as ringtones, notification
548 * sounds, or alarm sounds) may be located on internal storage. These are
549 * typically indexed under {@link MediaStore#VOLUME_INTERNAL}.
550 *
551 * @hide
552 */
553 @SystemApi
554 public static @NonNull Collection<File> getInternalMediaDirectories() {
555 final ArrayList<File> res = new ArrayList<>();
Hung-ying Tyan774d89e2020-02-05 16:55:01 +0800556 addCanonicalFile(res, new File(Environment.getRootDirectory(), "media"));
557 addCanonicalFile(res, new File(Environment.getOemDirectory(), "media"));
558 addCanonicalFile(res, new File(Environment.getProductDirectory(), "media"));
Jeff Sharkey04b4ba12019-12-15 22:42:42 -0700559 return res;
560 }
561
Hung-ying Tyan774d89e2020-02-05 16:55:01 +0800562 private static void addCanonicalFile(List<File> list, File file) {
563 try {
564 list.add(file.getCanonicalFile());
565 } catch (IOException e) {
566 Log.w(TAG, "Failed to resolve " + file + ": " + e);
567 list.add(file);
568 }
569 }
570
Jeff Sharkey04b4ba12019-12-15 22:42:42 -0700571 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700572 * Return the primary shared/external storage directory. This directory may
573 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800574 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700575 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800576 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700577 * <p>
578 * <em>Note: don't be confused by the word "external" here. This directory
579 * can better be thought as media/shared storage. It is a filesystem that
580 * can hold a relatively large amount of data and that is shared across all
581 * applications (does not enforce permissions). Traditionally this is an SD
582 * card, but it may also be implemented as built-in storage in a device that
583 * is distinct from the protected internal storage and can be mounted as a
584 * filesystem on a computer.</em>
585 * <p>
586 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700587 * each user has their own isolated shared storage. Applications only have
588 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700589 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700590 * In devices with multiple shared/external storage directories, this
591 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700592 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700593 * {@link Context#getExternalFilesDirs(String)},
594 * {@link Context#getExternalCacheDirs()}, and
595 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700596 * <p>
597 * Applications should not directly use this top-level directory, in order
598 * to avoid polluting the user's root namespace. Any files that are private
599 * to the application should be placed in a directory returned by
600 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700601 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700602 * if the application is uninstalled. Other shared files should be placed in
603 * one of the directories returned by
604 * {@link #getExternalStoragePublicDirectory}.
605 * <p>
606 * Writing to this path requires the
607 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Jeff Sharkey488162b2019-04-27 17:03:54 -0600608 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read
609 * access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700610 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700611 * which is automatically granted if you hold the write permission.
612 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700613 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700614 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700615 * {@link Context#getExternalFilesDir(String)},
616 * {@link Context#getExternalCacheDir()}, or
617 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700618 * read or write.
619 * <p>
620 * This path may change between platform versions, so applications should
621 * only persist relative paths.
622 * <p>
623 * Here is an example of typical code to monitor the state of external
624 * storage:
625 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700626 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800627 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700628 *
629 * @see #getExternalStorageState()
630 * @see #isExternalStorageRemovable()
Jeff Sharkey488162b2019-04-27 17:03:54 -0600631 * @deprecated To improve user privacy, direct access to shared/external
632 * storage devices is deprecated. When an app targets
633 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
634 * from this method is no longer directly accessible to apps.
635 * Apps can continue to access content stored on shared/external
636 * storage by migrating to alternatives such as
637 * {@link Context#getExternalFilesDir(String)},
638 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600640 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700642 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000643 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700644 }
645
646 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000647 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700648 public static File getLegacyExternalStorageDirectory() {
649 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700652 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000653 @UnsupportedAppUsage
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700654 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700655 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700656 }
657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800659 * Standard directory in which to place any audio files that should be
660 * in the regular list of music for the user.
661 * This may be combined with
662 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
663 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
664 * of directories to categories a particular audio file as more than one
665 * type.
666 */
667 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800668
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800669 /**
670 * Standard directory in which to place any audio files that should be
671 * in the list of podcasts that the user can select (not as regular
672 * music).
673 * This may be combined with {@link #DIRECTORY_MUSIC},
674 * {@link #DIRECTORY_NOTIFICATIONS},
675 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
676 * of directories to categories a particular audio file as more than one
677 * type.
678 */
679 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800680
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800681 /**
682 * Standard directory in which to place any audio files that should be
683 * in the list of ringtones that the user can select (not as regular
684 * music).
685 * This may be combined with {@link #DIRECTORY_MUSIC},
686 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
687 * {@link #DIRECTORY_ALARMS} as a series
688 * of directories to categories a particular audio file as more than one
689 * type.
690 */
691 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800692
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800693 /**
694 * Standard directory in which to place any audio files that should be
695 * in the list of alarms that the user can select (not as regular
696 * music).
697 * This may be combined with {@link #DIRECTORY_MUSIC},
698 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
699 * and {@link #DIRECTORY_RINGTONES} as a series
700 * of directories to categories a particular audio file as more than one
701 * type.
702 */
703 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800704
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800705 /**
706 * Standard directory in which to place any audio files that should be
707 * in the list of notifications that the user can select (not as regular
708 * music).
709 * This may be combined with {@link #DIRECTORY_MUSIC},
710 * {@link #DIRECTORY_PODCASTS},
711 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
712 * of directories to categories a particular audio file as more than one
713 * type.
714 */
715 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800716
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800717 /**
718 * Standard directory in which to place pictures that are available to
719 * the user. Note that this is primarily a convention for the top-level
720 * public directory, as the media scanner will find and collect pictures
721 * in any directory.
722 */
723 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800724
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800725 /**
726 * Standard directory in which to place movies that are available to
727 * the user. Note that this is primarily a convention for the top-level
728 * public directory, as the media scanner will find and collect movies
729 * in any directory.
730 */
731 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800732
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800733 /**
734 * Standard directory in which to place files that have been downloaded by
735 * the user. Note that this is primarily a convention for the top-level
736 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700737 * private directories. Also note that though the constant here is
738 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
739 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800740 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700741 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800742
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800743 /**
744 * The traditional location for pictures and videos when mounting the
745 * device as a camera. Note that this is primarily a convention for the
746 * top-level public directory, as this convention makes no sense elsewhere.
747 */
748 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700749
750 /**
751 * Standard directory in which to place documents that have been created by
752 * the user.
753 */
754 public static String DIRECTORY_DOCUMENTS = "Documents";
755
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800756 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600757 * Standard directory in which to place screenshots that have been taken by
758 * the user. Typically used as a secondary directory under
759 * {@link #DIRECTORY_PICTURES}.
760 */
761 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
762
763 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700764 * Standard directory in which to place any audio files which are
765 * audiobooks.
766 */
767 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
768
769 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800770 * List of standard storage directories.
771 * <p>
772 * Each of its values have its own constant:
773 * <ul>
774 * <li>{@link #DIRECTORY_MUSIC}
775 * <li>{@link #DIRECTORY_PODCASTS}
776 * <li>{@link #DIRECTORY_ALARMS}
777 * <li>{@link #DIRECTORY_RINGTONES}
778 * <li>{@link #DIRECTORY_NOTIFICATIONS}
779 * <li>{@link #DIRECTORY_PICTURES}
780 * <li>{@link #DIRECTORY_MOVIES}
781 * <li>{@link #DIRECTORY_DOWNLOADS}
782 * <li>{@link #DIRECTORY_DCIM}
783 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700784 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800785 * </ul>
786 * @hide
787 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800788 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800789 DIRECTORY_MUSIC,
790 DIRECTORY_PODCASTS,
791 DIRECTORY_RINGTONES,
792 DIRECTORY_ALARMS,
793 DIRECTORY_NOTIFICATIONS,
794 DIRECTORY_PICTURES,
795 DIRECTORY_MOVIES,
796 DIRECTORY_DOWNLOADS,
797 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700798 DIRECTORY_DOCUMENTS,
799 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800800 };
801
802 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800803 * @hide
804 */
805 public static boolean isStandardDirectory(String dir) {
806 for (String valid : STANDARD_DIRECTORIES) {
807 if (valid.equals(dir)) {
808 return true;
809 }
810 }
811 return false;
812 }
813
Jeff Sharkey20356142017-12-06 15:22:05 -0700814 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
815 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
816 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
817 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
818 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
819 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
820 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
821 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
822 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
823 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700824 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700825
826 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
827 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
828
829 /**
830 * Classify the content types present on the given external storage device.
831 * <p>
832 * This is typically useful for deciding if an inserted SD card is empty, or
833 * if it contains content like photos that should be preserved.
834 *
835 * @hide
836 */
837 public static int classifyExternalStorageDirectory(File dir) {
838 int res = 0;
839 for (File f : FileUtils.listFilesOrEmpty(dir)) {
840 if (f.isFile() && isInterestingFile(f)) {
841 res |= HAS_OTHER;
842 } else if (f.isDirectory() && hasInterestingFiles(f)) {
843 final String name = f.getName();
844 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
845 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
846 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
847 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
848 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
849 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
850 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
851 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
852 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
853 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700854 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700855 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
856 else res |= HAS_OTHER;
857 }
858 }
859 return res;
860 }
861
862 private static boolean hasInterestingFiles(File dir) {
863 final LinkedList<File> explore = new LinkedList<>();
864 explore.add(dir);
865 while (!explore.isEmpty()) {
866 dir = explore.pop();
867 for (File f : FileUtils.listFilesOrEmpty(dir)) {
868 if (isInterestingFile(f)) return true;
869 if (f.isDirectory()) explore.add(f);
870 }
871 }
872 return false;
873 }
874
875 private static boolean isInterestingFile(File file) {
876 if (file.isFile()) {
877 final String name = file.getName().toLowerCase();
878 if (name.endsWith(".exe") || name.equals("autorun.inf")
879 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
880 return false;
881 } else {
882 return true;
883 }
884 } else {
885 return false;
886 }
887 }
888
Felipe Lemeb012f912016-01-22 16:49:55 -0800889 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700890 * Get a top-level shared/external storage directory for placing files of a
891 * particular type. This is where the user will typically place and manage
892 * their own files, so you should be careful about what you put here to
893 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800894 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700895 * <p>
896 * On devices with multiple users (as described by {@link UserManager}),
897 * each user has their own isolated shared storage. Applications only have
898 * access to the shared storage for the user they're running as.
899 * </p>
900 * <p>
901 * Here is an example of typical code to manipulate a picture on the public
902 * shared storage:
903 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800904 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
905 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800906 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700907 * @param type The type of storage directory to return. Should be one of
908 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
909 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
910 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800911 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
912 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700913 * @return Returns the File path for the directory. Note that this directory
914 * may not yet exist, so you must make sure it exists before using
915 * it such as with {@link File#mkdirs File.mkdirs()}.
Jeff Sharkey488162b2019-04-27 17:03:54 -0600916 * @deprecated To improve user privacy, direct access to shared/external
917 * storage devices is deprecated. When an app targets
918 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
919 * from this method is no longer directly accessible to apps.
920 * Apps can continue to access content stored on shared/external
921 * storage by migrating to alternatives such as
922 * {@link Context#getExternalFilesDir(String)},
923 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800924 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600925 @Deprecated
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800926 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700927 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000928 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800929 }
930
931 /**
932 * Returns the path for android-specific data on the SD card.
933 * @hide
934 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000935 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700936 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700937 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000938 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800939 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700940
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800941 /**
942 * Generates the raw path to an application's data
943 * @hide
944 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000945 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700946 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700947 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000948 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800949 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800950
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800951 /**
952 * Generates the raw path to an application's media
953 * @hide
954 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000955 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700956 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700957 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000958 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800959 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800960
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800961 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800962 * Generates the raw path to an application's OBB files
963 * @hide
964 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000965 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700966 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700967 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000968 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800969 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800970
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800971 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800972 * Generates the path to an application's files.
973 * @hide
974 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000975 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700976 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700977 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000978 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800979 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700980
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800981 /**
982 * Generates the path to an application's cache.
983 * @hide
984 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000985 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700986 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700987 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000988 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800989 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800990
Sudheer Shanka25f1c6e2019-04-22 17:03:47 -0700991 /** @hide */
992 public static File[] buildExternalStoragePublicDirs(@NonNull String dirType) {
993 throwIfUserRequired();
994 return sCurrentUser.buildExternalStoragePublicDirs(dirType);
995 }
996
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800997 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700998 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 */
1000 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -08001001 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003
1004 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001005 * Unknown storage state, such as when a path isn't backed by known storage
1006 * media.
1007 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001008 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001009 */
1010 public static final String MEDIA_UNKNOWN = "unknown";
1011
1012 /**
1013 * Storage state if the media is not present.
1014 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001015 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 */
1017 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001020 * Storage state if the media is present but not mounted.
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_UNMOUNTED = "unmounted";
1025
1026 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001027 * Storage state if the media is present and being disk-checked.
1028 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001029 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 */
1031 public static final String MEDIA_CHECKING = "checking";
1032
1033 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001034 * Storage state if the media is present but is blank or is using an
1035 * unsupported filesystem.
1036 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001037 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 */
1039 public static final String MEDIA_NOFS = "nofs";
1040
1041 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001042 * Storage state if the media is present and mounted at its mount point with
1043 * read/write access.
1044 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001045 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 */
1047 public static final String MEDIA_MOUNTED = "mounted";
1048
1049 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001050 * Storage state if the media is present and mounted at its mount point with
1051 * read-only access.
1052 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001053 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 */
1055 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
1056
1057 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001058 * Storage state if the media is present not mounted, and shared via USB
1059 * mass storage.
1060 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001061 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 */
1063 public static final String MEDIA_SHARED = "shared";
1064
1065 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001066 * Storage state if the media was removed before it was unmounted.
1067 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001068 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 */
1070 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
1071
1072 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001073 * Storage state if the media is present but cannot be mounted. Typically
1074 * this happens if the file system on the media is corrupted.
1075 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001076 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 */
1078 public static final String MEDIA_UNMOUNTABLE = "unmountable";
1079
1080 /**
Jeff Sharkey48877892015-03-18 11:27:19 -07001081 * Storage state if the media is in the process of being ejected.
1082 *
1083 * @see #getExternalStorageState(File)
1084 */
1085 public static final String MEDIA_EJECTING = "ejecting";
1086
1087 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001088 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -08001089 *
Jeff Sharkey8c165792012-10-22 14:08:29 -07001090 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001091 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1092 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1093 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1094 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1095 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 */
1097 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +00001098 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001099 return getExternalStorageState(externalDir);
1100 }
1101
1102 /**
1103 * @deprecated use {@link #getExternalStorageState(File)}
1104 */
1105 @Deprecated
1106 public static String getStorageState(File path) {
1107 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001108 }
1109
1110 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001111 * Returns the current state of the shared/external storage media at the
1112 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001113 *
1114 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1115 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1116 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1117 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1118 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
1119 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001120 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001121 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001122 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001123 return volume.getState();
1124 } else {
1125 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128
Dianne Hackborn407f6252010-10-04 11:31:17 -07001129 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001130 * Returns whether the primary shared/external storage media is physically
1131 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001132 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001133 * @return true if the storage device can be removed (such as an SD card),
1134 * or false if the storage device is built in and cannot be
1135 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001136 */
1137 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001138 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001139 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001140 }
1141
Kenny Roote1ff2142010-10-12 11:20:01 -07001142 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001143 * Returns whether the shared/external storage media at the given path is
1144 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001145 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001146 * @return true if the storage device can be removed (such as an SD card),
1147 * or false if the storage device is built in and cannot be
1148 * physically removed.
1149 * @throws IllegalArgumentException if the path is not a valid storage
1150 * device.
1151 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001152 public static boolean isExternalStorageRemovable(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001153 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001154 if (volume != null) {
1155 return volume.isRemovable();
1156 } else {
1157 throw new IllegalArgumentException("Failed to find storage device at " + path);
1158 }
1159 }
1160
1161 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001162 * Returns whether the primary shared/external storage media is emulated.
1163 * <p>
1164 * The contents of emulated storage devices are backed by a private user
1165 * data partition, which means there is little benefit to apps storing data
1166 * here instead of the private directories returned by
1167 * {@link Context#getFilesDir()}, etc.
1168 * <p>
1169 * This returns true when emulated storage is backed by either internal
1170 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001171 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001172 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1173 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001174 */
1175 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001176 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001177 return isExternalStorageEmulated(externalDir);
1178 }
1179
1180 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001181 * Returns whether the shared/external storage media at the given path is
1182 * emulated.
1183 * <p>
1184 * The contents of emulated storage devices are backed by a private user
1185 * data partition, which means there is little benefit to apps storing data
1186 * here instead of the private directories returned by
1187 * {@link Context#getFilesDir()}, etc.
1188 * <p>
1189 * This returns true when emulated storage is backed by either internal
1190 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001191 *
1192 * @throws IllegalArgumentException if the path is not a valid storage
1193 * device.
1194 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001195 public static boolean isExternalStorageEmulated(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001196 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001197 if (volume != null) {
1198 return volume.isEmulated();
1199 } else {
1200 throw new IllegalArgumentException("Failed to find storage device at " + path);
1201 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001202 }
1203
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001204 /**
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +00001205 * Returns whether the shared/external storage media is a
1206 * legacy view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001207 * <p>
1208 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001209 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +00001210 * may inherit its legacy state based on when it was first installed, target sdk and other
1211 * factors.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001212 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001213 * Non-legacy apps can continue to discover and read media belonging to
1214 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001215 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001216 public static boolean isExternalStorageLegacy() {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001217 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001218 return isExternalStorageLegacy(externalDir);
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001219 }
1220
1221 /**
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +00001222 * Returns whether the shared/external storage media is a
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001223 * legacy view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001224 * <p>
1225 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001226 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +00001227 * may inherit its legacy state based on when it was first installed, target sdk and other
1228 * factors.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001229 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001230 * Non-legacy apps can continue to discover and read media belonging to
1231 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001232 *
1233 * @throws IllegalArgumentException if the path is not a valid storage
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +00001234 * device.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001235 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001236 public static boolean isExternalStorageLegacy(@NonNull File path) {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001237 final Context context = AppGlobals.getInitialApplication();
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001238 final int uid = context.getApplicationInfo().uid;
1239 if (Process.isIsolated(uid)) {
1240 return false;
1241 }
1242
1243 final PackageManager packageManager = context.getPackageManager();
1244 if (packageManager.isInstantApp()) {
1245 return false;
1246 }
1247
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +00001248 // TODO(b/150672994): Compat flags do not override instant app and isolated process's
1249 // behavior.
1250 boolean defaultScopedStorage = Compatibility.isChangeEnabled(DEFAULT_SCOPED_STORAGE);
1251 boolean forceEnableScopedStorage = Compatibility.isChangeEnabled(
1252 FORCE_ENABLE_SCOPED_STORAGE);
1253 // if Scoped Storage is strictly enforced, the app does *not* have legacy storage access
1254 // Note: does not require packagename/uid as this is directly called from an app process
1255 if (isScopedStorageEnforced(defaultScopedStorage, forceEnableScopedStorage)) {
1256 return false;
1257 }
1258 // if Scoped Storage is strictly disabled, the app has legacy storage access
1259 // Note: does not require packagename/uid as this is directly called from an app process
1260 if (isScopedStorageDisabled(defaultScopedStorage, forceEnableScopedStorage)) {
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001261 return true;
1262 }
1263
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001264 final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
Svet Ganovd563e932019-04-14 13:07:41 -07001265 return appOps.checkOpNoThrow(AppOpsManager.OP_LEGACY_STORAGE,
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001266 uid, context.getOpPackageName()) == AppOpsManager.MODE_ALLOWED;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001267 }
1268
shafik81870652020-03-05 12:59:15 +00001269 /**
1270 * Returns whether the calling app has All Files Access on the primary shared/external storage
1271 * media.
1272 * <p>Declaring the permission {@link android.Manifest.permission#MANAGE_EXTERNAL_STORAGE} isn't
1273 * enough to gain the access.
1274 * <p>To request access, use
1275 * {@link android.provider.Settings#ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION}.
1276 */
1277 public static boolean isExternalStorageManager() {
1278 final File externalDir = sCurrentUser.getExternalDirs()[0];
1279 return isExternalStorageManager(externalDir);
1280 }
1281
1282 /**
1283 * Returns whether the calling app has All Files Access at the given {@code path}
1284 * <p>Declaring the permission {@link android.Manifest.permission#MANAGE_EXTERNAL_STORAGE} isn't
1285 * enough to gain the access.
1286 * <p>To request access, use
1287 * {@link android.provider.Settings#ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION}.
1288 */
1289 public static boolean isExternalStorageManager(@NonNull File path) {
1290 final Context context = Objects.requireNonNull(AppGlobals.getInitialApplication());
1291 String packageName = Objects.requireNonNull(context.getPackageName());
1292 int uid = context.getApplicationInfo().uid;
1293
1294 final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
1295 final int opMode =
1296 appOps.checkOpNoThrow(AppOpsManager.OP_MANAGE_EXTERNAL_STORAGE, uid, packageName);
1297
1298 switch (opMode) {
1299 case AppOpsManager.MODE_DEFAULT:
1300 return PackageManager.PERMISSION_GRANTED
1301 == context.checkPermission(
1302 Manifest.permission.MANAGE_EXTERNAL_STORAGE, Process.myPid(), uid);
1303 case AppOpsManager.MODE_ALLOWED:
1304 return true;
1305 case AppOpsManager.MODE_ERRORED:
1306 case AppOpsManager.MODE_IGNORED:
1307 return false;
1308 default:
1309 throw new IllegalStateException("Unknown AppOpsManager mode " + opMode);
1310 }
1311 }
1312
Abhijeet Kaur3a6027c2020-03-01 13:21:16 +00001313 private static boolean isScopedStorageEnforced(boolean defaultScopedStorage,
1314 boolean forceEnableScopedStorage) {
1315 return defaultScopedStorage && forceEnableScopedStorage;
1316 }
1317
1318 private static boolean isScopedStorageDisabled(boolean defaultScopedStorage,
1319 boolean forceEnableScopedStorage) {
1320 return !defaultScopedStorage && !forceEnableScopedStorage;
1321 }
1322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 static File getDirectory(String variableName, String defaultPath) {
1324 String path = System.getenv(variableName);
1325 return path == null ? new File(defaultPath) : new File(path);
1326 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001327
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001328 /** {@hide} */
1329 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001330 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001331 }
1332
1333 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001334 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001335 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1336 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001337 }
1338 }
1339
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001340 /**
1341 * Append path segments to each given base path, returning result.
1342 *
1343 * @hide
1344 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001345 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001346 public static File[] buildPaths(File[] base, String... segments) {
1347 File[] result = new File[base.length];
1348 for (int i = 0; i < base.length; i++) {
1349 result[i] = buildPath(base[i], segments);
1350 }
1351 return result;
1352 }
1353
1354 /**
1355 * Append path segments to given base path, returning result.
1356 *
1357 * @hide
1358 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001359 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001360 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001361 File cur = base;
1362 for (String segment : segments) {
1363 if (cur == null) {
1364 cur = new File(segment);
1365 } else {
1366 cur = new File(cur, segment);
1367 }
1368 }
1369 return cur;
1370 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001371
1372 /**
1373 * If the given path exists on emulated external storage, return the
1374 * translated backing path hosted on internal storage. This bypasses any
1375 * emulation later, improving performance. This is <em>only</em> suitable
1376 * for read-only access.
1377 * <p>
1378 * Returns original path if given path doesn't meet these criteria. Callers
1379 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1380 * permission.
1381 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001382 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001383 * @hide
1384 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001385 @UnsupportedAppUsage
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001386 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001387 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001388 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390}