blob: 61da5e67e57bcb278ceb77f0886203538d09aabc [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 Sharkey04b4ba12019-12-15 22:42:42 -070037import java.util.ArrayList;
38import java.util.Collection;
Jeff Sharkey20356142017-12-06 15:22:05 -070039import java.util.LinkedList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
42 * Provides access to environment variables.
43 */
44public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080045 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Jeff Sharkeydd02e332018-06-27 14:41:57 -060047 // NOTE: keep credential-protected paths in sync with StrictMode.java
48
Jeff Sharkeyb049e212012-09-07 23:16:01 -070049 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080050 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070051 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060052 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070053 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080054 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080055 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080056 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070057 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090058 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Jeongik Cha9ec059a2019-07-04 21:12:06 +090059 private static final String ENV_SYSTEM_EXT_ROOT = "SYSTEM_EXT_ROOT";
Dario Frenic3e1bb722019-10-09 15:43:38 +010060 private static final String ENV_APEX_ROOT = "APEX_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070061
Jeff Sharkeydfa45302012-09-12 16:25:22 -070062 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070063 public static final String DIR_ANDROID = "Android";
64 private static final String DIR_DATA = "data";
65 private static final String DIR_MEDIA = "media";
66 private static final String DIR_OBB = "obb";
67 private static final String DIR_FILES = "files";
68 private static final String DIR_CACHE = "cache";
69
70 /** {@hide} */
71 @Deprecated
72 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070073
Jeff Sharkey63d0a062013-03-01 16:12:55 -080074 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070075 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060076 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070077 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080078 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080079 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080080 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070081 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090082 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Jeongik Cha9ec059a2019-07-04 21:12:06 +090083 private static final File DIR_SYSTEM_EXT_ROOT = getDirectory(ENV_SYSTEM_EXT_ROOT,
Dario Frenic3e1bb722019-10-09 15:43:38 +010084 "/system_ext");
85 private static final File DIR_APEX_ROOT = getDirectory(ENV_APEX_ROOT,
86 "/apex");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Andrei Onea24ec3212019-03-15 17:35:05 +000088 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000089 private static UserEnvironment sCurrentUser;
90 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070091
Andreas Gamped281b422016-07-08 03:50:27 +000092 static {
93 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070094 }
95
96 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +000097 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000098 public static void initForCurrentUser() {
99 final int userId = UserHandle.myUserId();
100 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700101 }
102
103 /** {@hide} */
104 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -0700105 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700106
Andrei Onea24ec3212019-03-15 17:35:05 +0000107 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700108 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700109 mUserId = userId;
110 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700111
Andrei Onea24ec3212019-03-15 17:35:05 +0000112 @UnsupportedAppUsage
Jeff Sharkey48877892015-03-18 11:27:19 -0700113 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -0700114 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
115 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700116 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700117 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700118 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700119 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700120 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700121 }
122
Andrei Onea24ec3212019-03-15 17:35:05 +0000123 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700124 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700125 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700126 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700127 }
128
Andrei Onea24ec3212019-03-15 17:35:05 +0000129 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700130 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700131 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700132 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700133 }
134
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700135 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700136 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700137 }
138
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700139 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700140 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700141 }
142
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700143 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700144 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700145 }
146
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700147 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700148 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700149 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700150
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700151 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700152 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700153 }
154
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700155 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700156 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700157 }
158
159 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700160 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700161 }
162
163 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700164 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700165 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700166 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800169 * Return root of the "system" partition holding the core Android OS.
170 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700172 public static @NonNull File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800173 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 }
175
Jeff Sharkeyec19e9b2019-08-22 11:12:11 -0600176 /**
177 * Return root directory where all external storage devices will be mounted.
178 * For example, {@link #getExternalStorageDirectory()} will appear under
179 * this location.
180 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700181 public static @NonNull File getStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700182 return DIR_ANDROID_STORAGE;
183 }
184
Jason parksa3cdaa52011-01-13 14:15:43 -0600185 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800186 * Return root directory of the "oem" partition holding OEM customizations,
187 * if any. If present, the partition is mounted read-only.
188 *
189 * @hide
190 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600191 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700192 public static @NonNull File getOemDirectory() {
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800193 return DIR_OEM_ROOT;
194 }
195
196 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800197 * Return root directory of the "odm" partition holding ODM customizations,
198 * if any. If present, the partition is mounted read-only.
199 *
200 * @hide
201 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600202 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700203 public static @NonNull File getOdmDirectory() {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800204 return DIR_ODM_ROOT;
205 }
206
207 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700208 * Return root directory of the "vendor" partition that holds vendor-provided
209 * software that should persist across simple reflashing of the "system" partition.
210 * @hide
211 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600212 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700213 public static @NonNull File getVendorDirectory() {
Christopher Tate740888f2014-04-18 12:24:57 -0700214 return DIR_VENDOR_ROOT;
215 }
216
Jason parksa3cdaa52011-01-13 14:15:43 -0600217 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900218 * Return root directory of the "product" partition holding product-specific
219 * customizations if any. If present, the partition is mounted read-only.
220 *
221 * @hide
222 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600223 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000224 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700225 public static @NonNull File getProductDirectory() {
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900226 return DIR_PRODUCT_ROOT;
227 }
228
229 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100230 * Return root directory of the "product_services" partition holding middleware
231 * services if any. If present, the partition is mounted read-only.
232 *
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900233 * @deprecated This directory is not guaranteed to exist.
234 * Its name is changed to "system_ext" because the partition's purpose is changed.
235 * {@link #getSystemExtDirectory()}
Dario Freni2bef1762018-06-01 14:02:08 +0100236 * @hide
237 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600238 @SystemApi
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900239 @Deprecated
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700240 public static @NonNull File getProductServicesDirectory() {
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900241 return getDirectory("PRODUCT_SERVICES_ROOT", "/product_services");
242 }
243
244 /**
245 * Return root directory of the "system_ext" partition holding system partition's extension
246 * If present, the partition is mounted read-only.
247 *
248 * @hide
249 */
250 @SystemApi
251 public static @NonNull File getSystemExtDirectory() {
252 return DIR_SYSTEM_EXT_ROOT;
Dario Freni2bef1762018-06-01 14:02:08 +0100253 }
254
255 /**
Dario Frenic3e1bb722019-10-09 15:43:38 +0100256 * Return root directory of the apex mount point, where all the apex modules are made available
257 * to the rest of the system.
258 *
259 * @hide
260 */
261 public static @NonNull File getApexDirectory() {
262 return DIR_APEX_ROOT;
263 }
264
265 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700266 * Return the system directory for a user. This is for use by system
267 * services to store files relating to the user. This directory will be
268 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700269 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600270 * @deprecated This directory is valid and still exists, but but callers
271 * should <em>strongly</em> consider switching to using either
272 * {@link #getDataSystemCeDirectory(int)} or
273 * {@link #getDataSystemDeDirectory(int)}, both of which support
274 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700275 * @hide
276 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700277 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700278 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800279 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700280 }
281
282 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700283 * Returns the config directory for a user. This is for use by system
284 * services to store files relating to the user which should be readable by
285 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100286 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700287 * @deprecated This directory is valid and still exists, but callers should
288 * <em>strongly</em> consider switching to
289 * {@link #getDataMiscCeDirectory(int)} which is protected with
290 * user credentials or {@link #getDataMiscDeDirectory(int)}
291 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100292 * @hide
293 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700294 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100295 public static File getUserConfigDirectory(int userId) {
296 return new File(new File(new File(
297 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
298 }
299
300 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700301 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 */
303 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800304 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 }
306
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700307 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700308 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700309 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800310 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700311 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700312 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700313 }
314 }
315
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700316 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600317 public static File getExpandDirectory() {
318 return DIR_ANDROID_EXPAND;
319 }
320
321 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000322 @UnsupportedAppUsage
Jeff Sharkey15447792015-11-05 16:18:51 -0800323 public static File getDataSystemDirectory() {
324 return new File(getDataDirectory(), "system");
325 }
326
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700327 /**
328 * Returns the base directory for per-user system directory, device encrypted.
329 * {@hide}
330 */
331 public static File getDataSystemDeDirectory() {
332 return buildPath(getDataDirectory(), "system_de");
333 }
334
335 /**
336 * Returns the base directory for per-user system directory, credential encrypted.
337 * {@hide}
338 */
339 public static File getDataSystemCeDirectory() {
340 return buildPath(getDataDirectory(), "system_ce");
341 }
342
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600343 /**
344 * Return the "credential encrypted" system directory for a user. This is
345 * for use by system services to store files relating to the user. This
346 * directory supports fast user wipe, and will be automatically deleted when
347 * the user is removed.
348 * <p>
349 * Data stored under this path is "credential encrypted", which uses an
350 * encryption key that is entangled with user credentials, such as a PIN or
351 * password. The contents will only be available once the user has been
352 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
353 * <p>
354 * New code should <em>strongly</em> prefer storing sensitive data in these
355 * credential encrypted areas.
356 *
357 * @hide
358 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700359 public static File getDataSystemCeDirectory(int userId) {
360 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800361 }
362
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600363 /**
364 * Return the "device encrypted" system directory for a user. This is for
365 * use by system services to store files relating to the user. This
366 * directory supports fast user wipe, and will be automatically deleted when
367 * the user is removed.
368 * <p>
369 * Data stored under this path is "device encrypted", which uses an
370 * encryption key that is tied to the physical device. The contents will
371 * only be available once the device has finished a {@code dm-verity}
372 * protected boot.
373 * <p>
374 * New code should <em>strongly</em> avoid storing sensitive data in these
375 * device encrypted areas.
376 *
377 * @hide
378 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700379 public static File getDataSystemDeDirectory(int userId) {
380 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
381 }
382
383 /** {@hide} */
384 public static File getDataMiscDirectory() {
385 return new File(getDataDirectory(), "misc");
386 }
387
388 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800389 public static File getDataMiscCeDirectory() {
390 return buildPath(getDataDirectory(), "misc_ce");
391 }
392
393 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700394 public static File getDataMiscCeDirectory(int userId) {
395 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
396 }
397
398 /** {@hide} */
399 public static File getDataMiscDeDirectory(int userId) {
400 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800401 }
402
Calin Juravled479b522016-02-24 16:22:03 +0000403 private static File getDataProfilesDeDirectory(int userId) {
404 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
405 }
406
407 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800408 public static File getDataVendorCeDirectory(int userId) {
409 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
410 }
411
412 /** {@hide} */
413 public static File getDataVendorDeDirectory(int userId) {
414 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
415 }
416
417 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800418 public static File getDataRefProfilesDePackageDirectory(String packageName) {
419 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100420 }
421
422 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000423 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
424 return buildPath(getDataProfilesDeDirectory(userId), packageName);
425 }
426
427 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700428 public static File getDataAppDirectory(String volumeUuid) {
429 return new File(getDataDirectory(volumeUuid), "app");
430 }
431
432 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000433 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery296c8b92019-02-27 12:06:24 +0000434 return new File(getDataDirectory(volumeUuid), "app-staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000435 }
436
437 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700438 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700439 return new File(getDataDirectory(volumeUuid), "user");
440 }
441
442 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700443 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
444 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700445 }
446
447 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700448 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700449 String packageName) {
450 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700451 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800452 }
453
454 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700455 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800456 return new File(getDataDirectory(volumeUuid), "user_de");
457 }
458
459 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700460 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
461 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800462 }
463
464 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700465 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800466 String packageName) {
467 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700468 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700469 }
470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700472 * Return preloads directory.
473 * <p>This directory may contain pre-loaded content such as
474 * {@link #getDataPreloadsDemoDirectory() demo videos} and
475 * {@link #getDataPreloadsAppsDirectory() APK files} .
476 * {@hide}
477 */
478 public static File getDataPreloadsDirectory() {
479 return new File(getDataDirectory(), "preloads");
480 }
481
482 /**
483 * @see #getDataPreloadsDirectory()
484 * {@hide}
485 */
486 public static File getDataPreloadsDemoDirectory() {
487 return new File(getDataPreloadsDirectory(), "demo");
488 }
489
490 /**
491 * @see #getDataPreloadsDirectory()
492 * {@hide}
493 */
494 public static File getDataPreloadsAppsDirectory() {
495 return new File(getDataPreloadsDirectory(), "apps");
496 }
497
498 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700499 * @see #getDataPreloadsDirectory()
500 * {@hide}
501 */
502 public static File getDataPreloadsMediaDirectory() {
503 return new File(getDataPreloadsDirectory(), "media");
504 }
505
506 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700507 * Returns location of preloaded cache directory for package name
508 * @see #getDataPreloadsDirectory()
509 * {@hide}
510 */
511 public static File getDataPreloadsFileCacheDirectory(String packageName) {
512 return new File(getDataPreloadsFileCacheDirectory(), packageName);
513 }
514
515 /**
516 * Returns location of preloaded cache directory.
517 * @see #getDataPreloadsDirectory()
518 * {@hide}
519 */
520 public static File getDataPreloadsFileCacheDirectory() {
521 return new File(getDataPreloadsDirectory(), "file_cache");
522 }
523
524 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800525 * Returns location of packages cache directory.
526 * {@hide}
527 */
528 public static File getPackageCacheDirectory() {
529 return new File(getDataSystemDirectory(), "package_cache");
530 }
531
532 /**
Jeff Sharkey04b4ba12019-12-15 22:42:42 -0700533 * Return locations where media files (such as ringtones, notification
534 * sounds, or alarm sounds) may be located on internal storage. These are
535 * typically indexed under {@link MediaStore#VOLUME_INTERNAL}.
536 *
537 * @hide
538 */
539 @SystemApi
540 public static @NonNull Collection<File> getInternalMediaDirectories() {
541 final ArrayList<File> res = new ArrayList<>();
542 res.add(new File(Environment.getRootDirectory(), "media"));
543 res.add(new File(Environment.getOemDirectory(), "media"));
544 res.add(new File(Environment.getProductDirectory(), "media"));
545 return res;
546 }
547
548 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700549 * Return the primary shared/external storage directory. This directory may
550 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800551 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700552 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800553 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700554 * <p>
555 * <em>Note: don't be confused by the word "external" here. This directory
556 * can better be thought as media/shared storage. It is a filesystem that
557 * can hold a relatively large amount of data and that is shared across all
558 * applications (does not enforce permissions). Traditionally this is an SD
559 * card, but it may also be implemented as built-in storage in a device that
560 * is distinct from the protected internal storage and can be mounted as a
561 * filesystem on a computer.</em>
562 * <p>
563 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700564 * each user has their own isolated shared storage. Applications only have
565 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700566 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700567 * In devices with multiple shared/external storage directories, this
568 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700569 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700570 * {@link Context#getExternalFilesDirs(String)},
571 * {@link Context#getExternalCacheDirs()}, and
572 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700573 * <p>
574 * Applications should not directly use this top-level directory, in order
575 * to avoid polluting the user's root namespace. Any files that are private
576 * to the application should be placed in a directory returned by
577 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700578 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700579 * if the application is uninstalled. Other shared files should be placed in
580 * one of the directories returned by
581 * {@link #getExternalStoragePublicDirectory}.
582 * <p>
583 * Writing to this path requires the
584 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Jeff Sharkey488162b2019-04-27 17:03:54 -0600585 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read
586 * access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700587 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700588 * which is automatically granted if you hold the write permission.
589 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700590 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700591 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700592 * {@link Context#getExternalFilesDir(String)},
593 * {@link Context#getExternalCacheDir()}, or
594 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700595 * read or write.
596 * <p>
597 * This path may change between platform versions, so applications should
598 * only persist relative paths.
599 * <p>
600 * Here is an example of typical code to monitor the state of external
601 * storage:
602 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700603 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800604 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700605 *
606 * @see #getExternalStorageState()
607 * @see #isExternalStorageRemovable()
Jeff Sharkey488162b2019-04-27 17:03:54 -0600608 * @deprecated To improve user privacy, direct access to shared/external
609 * storage devices is deprecated. When an app targets
610 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
611 * from this method is no longer directly accessible to apps.
612 * Apps can continue to access content stored on shared/external
613 * storage by migrating to alternatives such as
614 * {@link Context#getExternalFilesDir(String)},
615 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600617 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700619 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000620 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700621 }
622
623 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000624 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700625 public static File getLegacyExternalStorageDirectory() {
626 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700629 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000630 @UnsupportedAppUsage
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700631 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700632 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700633 }
634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800636 * Standard directory in which to place any audio files that should be
637 * in the regular list of music for the user.
638 * This may be combined with
639 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
640 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
641 * of directories to categories a particular audio file as more than one
642 * type.
643 */
644 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800645
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800646 /**
647 * Standard directory in which to place any audio files that should be
648 * in the list of podcasts that the user can select (not as regular
649 * music).
650 * This may be combined with {@link #DIRECTORY_MUSIC},
651 * {@link #DIRECTORY_NOTIFICATIONS},
652 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
653 * of directories to categories a particular audio file as more than one
654 * type.
655 */
656 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800657
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800658 /**
659 * Standard directory in which to place any audio files that should be
660 * in the list of ringtones that the user can select (not as regular
661 * music).
662 * This may be combined with {@link #DIRECTORY_MUSIC},
663 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
664 * {@link #DIRECTORY_ALARMS} as a series
665 * of directories to categories a particular audio file as more than one
666 * type.
667 */
668 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800669
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800670 /**
671 * Standard directory in which to place any audio files that should be
672 * in the list of alarms that the user can select (not as regular
673 * music).
674 * This may be combined with {@link #DIRECTORY_MUSIC},
675 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
676 * and {@link #DIRECTORY_RINGTONES} as a series
677 * of directories to categories a particular audio file as more than one
678 * type.
679 */
680 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800681
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800682 /**
683 * Standard directory in which to place any audio files that should be
684 * in the list of notifications that the user can select (not as regular
685 * music).
686 * This may be combined with {@link #DIRECTORY_MUSIC},
687 * {@link #DIRECTORY_PODCASTS},
688 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
689 * of directories to categories a particular audio file as more than one
690 * type.
691 */
692 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800693
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800694 /**
695 * Standard directory in which to place pictures that are available to
696 * the user. Note that this is primarily a convention for the top-level
697 * public directory, as the media scanner will find and collect pictures
698 * in any directory.
699 */
700 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800701
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800702 /**
703 * Standard directory in which to place movies that are available to
704 * the user. Note that this is primarily a convention for the top-level
705 * public directory, as the media scanner will find and collect movies
706 * in any directory.
707 */
708 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800709
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800710 /**
711 * Standard directory in which to place files that have been downloaded by
712 * the user. Note that this is primarily a convention for the top-level
713 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700714 * private directories. Also note that though the constant here is
715 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
716 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800717 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700718 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800719
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800720 /**
721 * The traditional location for pictures and videos when mounting the
722 * device as a camera. Note that this is primarily a convention for the
723 * top-level public directory, as this convention makes no sense elsewhere.
724 */
725 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700726
727 /**
728 * Standard directory in which to place documents that have been created by
729 * the user.
730 */
731 public static String DIRECTORY_DOCUMENTS = "Documents";
732
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800733 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600734 * Standard directory in which to place screenshots that have been taken by
735 * the user. Typically used as a secondary directory under
736 * {@link #DIRECTORY_PICTURES}.
737 */
738 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
739
740 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700741 * Standard directory in which to place any audio files which are
742 * audiobooks.
743 */
744 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
745
746 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800747 * List of standard storage directories.
748 * <p>
749 * Each of its values have its own constant:
750 * <ul>
751 * <li>{@link #DIRECTORY_MUSIC}
752 * <li>{@link #DIRECTORY_PODCASTS}
753 * <li>{@link #DIRECTORY_ALARMS}
754 * <li>{@link #DIRECTORY_RINGTONES}
755 * <li>{@link #DIRECTORY_NOTIFICATIONS}
756 * <li>{@link #DIRECTORY_PICTURES}
757 * <li>{@link #DIRECTORY_MOVIES}
758 * <li>{@link #DIRECTORY_DOWNLOADS}
759 * <li>{@link #DIRECTORY_DCIM}
760 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700761 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800762 * </ul>
763 * @hide
764 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800765 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800766 DIRECTORY_MUSIC,
767 DIRECTORY_PODCASTS,
768 DIRECTORY_RINGTONES,
769 DIRECTORY_ALARMS,
770 DIRECTORY_NOTIFICATIONS,
771 DIRECTORY_PICTURES,
772 DIRECTORY_MOVIES,
773 DIRECTORY_DOWNLOADS,
774 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700775 DIRECTORY_DOCUMENTS,
776 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800777 };
778
779 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800780 * @hide
781 */
782 public static boolean isStandardDirectory(String dir) {
783 for (String valid : STANDARD_DIRECTORIES) {
784 if (valid.equals(dir)) {
785 return true;
786 }
787 }
788 return false;
789 }
790
Jeff Sharkey20356142017-12-06 15:22:05 -0700791 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
792 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
793 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
794 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
795 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
796 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
797 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
798 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
799 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
800 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700801 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700802
803 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
804 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
805
806 /**
807 * Classify the content types present on the given external storage device.
808 * <p>
809 * This is typically useful for deciding if an inserted SD card is empty, or
810 * if it contains content like photos that should be preserved.
811 *
812 * @hide
813 */
814 public static int classifyExternalStorageDirectory(File dir) {
815 int res = 0;
816 for (File f : FileUtils.listFilesOrEmpty(dir)) {
817 if (f.isFile() && isInterestingFile(f)) {
818 res |= HAS_OTHER;
819 } else if (f.isDirectory() && hasInterestingFiles(f)) {
820 final String name = f.getName();
821 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
822 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
823 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
824 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
825 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
826 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
827 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
828 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
829 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
830 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700831 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700832 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
833 else res |= HAS_OTHER;
834 }
835 }
836 return res;
837 }
838
839 private static boolean hasInterestingFiles(File dir) {
840 final LinkedList<File> explore = new LinkedList<>();
841 explore.add(dir);
842 while (!explore.isEmpty()) {
843 dir = explore.pop();
844 for (File f : FileUtils.listFilesOrEmpty(dir)) {
845 if (isInterestingFile(f)) return true;
846 if (f.isDirectory()) explore.add(f);
847 }
848 }
849 return false;
850 }
851
852 private static boolean isInterestingFile(File file) {
853 if (file.isFile()) {
854 final String name = file.getName().toLowerCase();
855 if (name.endsWith(".exe") || name.equals("autorun.inf")
856 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
857 return false;
858 } else {
859 return true;
860 }
861 } else {
862 return false;
863 }
864 }
865
Felipe Lemeb012f912016-01-22 16:49:55 -0800866 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700867 * Get a top-level shared/external storage directory for placing files of a
868 * particular type. This is where the user will typically place and manage
869 * their own files, so you should be careful about what you put here to
870 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800871 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700872 * <p>
873 * On devices with multiple users (as described by {@link UserManager}),
874 * each user has their own isolated shared storage. Applications only have
875 * access to the shared storage for the user they're running as.
876 * </p>
877 * <p>
878 * Here is an example of typical code to manipulate a picture on the public
879 * shared storage:
880 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800881 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
882 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800883 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700884 * @param type The type of storage directory to return. Should be one of
885 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
886 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
887 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800888 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
889 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700890 * @return Returns the File path for the directory. Note that this directory
891 * may not yet exist, so you must make sure it exists before using
892 * it such as with {@link File#mkdirs File.mkdirs()}.
Jeff Sharkey488162b2019-04-27 17:03:54 -0600893 * @deprecated To improve user privacy, direct access to shared/external
894 * storage devices is deprecated. When an app targets
895 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
896 * from this method is no longer directly accessible to apps.
897 * Apps can continue to access content stored on shared/external
898 * storage by migrating to alternatives such as
899 * {@link Context#getExternalFilesDir(String)},
900 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800901 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600902 @Deprecated
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800903 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700904 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000905 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800906 }
907
908 /**
909 * Returns the path for android-specific data on the SD card.
910 * @hide
911 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000912 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700913 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700914 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000915 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800916 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700917
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800918 /**
919 * Generates the raw path to an application's data
920 * @hide
921 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000922 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700923 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700924 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000925 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800926 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800927
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800928 /**
929 * Generates the raw path to an application's media
930 * @hide
931 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000932 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700933 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700934 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000935 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800936 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800937
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800938 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800939 * Generates the raw path to an application's OBB files
940 * @hide
941 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000942 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700943 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700944 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000945 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800946 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800947
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800948 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800949 * Generates the path to an application's files.
950 * @hide
951 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000952 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700953 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700954 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000955 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800956 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700957
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800958 /**
959 * Generates the path to an application's cache.
960 * @hide
961 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000962 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700963 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700964 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000965 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800966 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800967
Sudheer Shanka25f1c6e2019-04-22 17:03:47 -0700968 /** @hide */
969 public static File[] buildExternalStoragePublicDirs(@NonNull String dirType) {
970 throwIfUserRequired();
971 return sCurrentUser.buildExternalStoragePublicDirs(dirType);
972 }
973
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800974 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700975 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 */
977 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800978 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980
981 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700982 * Unknown storage state, such as when a path isn't backed by known storage
983 * media.
984 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800985 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700986 */
987 public static final String MEDIA_UNKNOWN = "unknown";
988
989 /**
990 * Storage state if the media is not present.
991 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800992 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 */
994 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700997 * Storage state if the media is present but not mounted.
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_UNMOUNTED = "unmounted";
1002
1003 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001004 * Storage state if the media is present and being disk-checked.
1005 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001006 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 */
1008 public static final String MEDIA_CHECKING = "checking";
1009
1010 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001011 * Storage state if the media is present but is blank or is using an
1012 * unsupported filesystem.
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_NOFS = "nofs";
1017
1018 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001019 * Storage state if the media is present and mounted at its mount point with
1020 * read/write access.
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_MOUNTED = "mounted";
1025
1026 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001027 * Storage state if the media is present and mounted at its mount point with
1028 * read-only access.
1029 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001030 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
1033
1034 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001035 * Storage state if the media is present not mounted, and shared via USB
1036 * mass storage.
1037 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001038 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final String MEDIA_SHARED = "shared";
1041
1042 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001043 * Storage state if the media was removed before it was unmounted.
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_BAD_REMOVAL = "bad_removal";
1048
1049 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001050 * Storage state if the media is present but cannot be mounted. Typically
1051 * this happens if the file system on the media is corrupted.
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_UNMOUNTABLE = "unmountable";
1056
1057 /**
Jeff Sharkey48877892015-03-18 11:27:19 -07001058 * Storage state if the media is in the process of being ejected.
1059 *
1060 * @see #getExternalStorageState(File)
1061 */
1062 public static final String MEDIA_EJECTING = "ejecting";
1063
1064 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001065 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -08001066 *
Jeff Sharkey8c165792012-10-22 14:08:29 -07001067 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001068 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1069 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1070 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1071 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1072 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 */
1074 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +00001075 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001076 return getExternalStorageState(externalDir);
1077 }
1078
1079 /**
1080 * @deprecated use {@link #getExternalStorageState(File)}
1081 */
1082 @Deprecated
1083 public static String getStorageState(File path) {
1084 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001085 }
1086
1087 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001088 * Returns the current state of the shared/external storage media at the
1089 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001090 *
1091 * @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}.
1096 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001097 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001098 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001099 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001100 return volume.getState();
1101 } else {
1102 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
1105
Dianne Hackborn407f6252010-10-04 11:31:17 -07001106 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001107 * Returns whether the primary shared/external storage media is physically
1108 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001109 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001110 * @return true if the storage device can be removed (such as an SD card),
1111 * or false if the storage device is built in and cannot be
1112 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001113 */
1114 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001115 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001116 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001117 }
1118
Kenny Roote1ff2142010-10-12 11:20:01 -07001119 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001120 * Returns whether the shared/external storage media at the given path is
1121 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001122 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001123 * @return true if the storage device can be removed (such as an SD card),
1124 * or false if the storage device is built in and cannot be
1125 * physically removed.
1126 * @throws IllegalArgumentException if the path is not a valid storage
1127 * device.
1128 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001129 public static boolean isExternalStorageRemovable(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001130 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001131 if (volume != null) {
1132 return volume.isRemovable();
1133 } else {
1134 throw new IllegalArgumentException("Failed to find storage device at " + path);
1135 }
1136 }
1137
1138 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001139 * Returns whether the primary shared/external storage media is emulated.
1140 * <p>
1141 * The contents of emulated storage devices are backed by a private user
1142 * data partition, which means there is little benefit to apps storing data
1143 * here instead of the private directories returned by
1144 * {@link Context#getFilesDir()}, etc.
1145 * <p>
1146 * This returns true when emulated storage is backed by either internal
1147 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001148 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001149 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1150 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001151 */
1152 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001153 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001154 return isExternalStorageEmulated(externalDir);
1155 }
1156
1157 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001158 * Returns whether the shared/external storage media at the given path is
1159 * emulated.
1160 * <p>
1161 * The contents of emulated storage devices are backed by a private user
1162 * data partition, which means there is little benefit to apps storing data
1163 * here instead of the private directories returned by
1164 * {@link Context#getFilesDir()}, etc.
1165 * <p>
1166 * This returns true when emulated storage is backed by either internal
1167 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001168 *
1169 * @throws IllegalArgumentException if the path is not a valid storage
1170 * device.
1171 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001172 public static boolean isExternalStorageEmulated(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001173 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001174 if (volume != null) {
1175 return volume.isEmulated();
1176 } else {
1177 throw new IllegalArgumentException("Failed to find storage device at " + path);
1178 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001179 }
1180
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001181 /**
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001182 * Returns whether the primary shared/external storage media is a legacy
1183 * view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001184 * <p>
1185 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001186 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
1187 * may inherit its legacy state based on when it was first installed.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001188 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001189 * Non-legacy apps can continue to discover and read media belonging to
1190 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001191 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001192 public static boolean isExternalStorageLegacy() {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001193 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001194 return isExternalStorageLegacy(externalDir);
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001195 }
1196
1197 /**
1198 * Returns whether the shared/external storage media at the given path is a
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001199 * legacy view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001200 * <p>
1201 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001202 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
1203 * may inherit its legacy state based on when it was first installed.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001204 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001205 * Non-legacy apps can continue to discover and read media belonging to
1206 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001207 *
1208 * @throws IllegalArgumentException if the path is not a valid storage
1209 * device.
1210 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001211 public static boolean isExternalStorageLegacy(@NonNull File path) {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001212 final Context context = AppGlobals.getInitialApplication();
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001213 final int uid = context.getApplicationInfo().uid;
1214 if (Process.isIsolated(uid)) {
1215 return false;
1216 }
1217
1218 final PackageManager packageManager = context.getPackageManager();
1219 if (packageManager.isInstantApp()) {
1220 return false;
1221 }
1222
1223 if (packageManager.checkPermission(Manifest.permission.WRITE_MEDIA_STORAGE,
1224 context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
1225 return true;
1226 }
1227
1228 if (packageManager.checkPermission(Manifest.permission.INSTALL_PACKAGES,
1229 context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
1230 return true;
1231 }
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001232 final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001233 final String[] packagesForUid = packageManager.getPackagesForUid(uid);
1234 for (String packageName : packagesForUid) {
1235 if (appOps.checkOpNoThrow(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES,
1236 uid, packageName) == AppOpsManager.MODE_ALLOWED) {
1237 return true;
1238 }
1239 }
Jeff Sharkeye9fcabc2019-04-04 11:19:22 -06001240
Svet Ganovd563e932019-04-14 13:07:41 -07001241 return appOps.checkOpNoThrow(AppOpsManager.OP_LEGACY_STORAGE,
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001242 uid, context.getOpPackageName()) == AppOpsManager.MODE_ALLOWED;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001243 }
1244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 static File getDirectory(String variableName, String defaultPath) {
1246 String path = System.getenv(variableName);
1247 return path == null ? new File(defaultPath) : new File(path);
1248 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001249
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001250 /** {@hide} */
1251 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001252 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001253 }
1254
1255 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001256 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001257 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1258 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001259 }
1260 }
1261
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001262 /**
1263 * Append path segments to each given base path, returning result.
1264 *
1265 * @hide
1266 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001267 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001268 public static File[] buildPaths(File[] base, String... segments) {
1269 File[] result = new File[base.length];
1270 for (int i = 0; i < base.length; i++) {
1271 result[i] = buildPath(base[i], segments);
1272 }
1273 return result;
1274 }
1275
1276 /**
1277 * Append path segments to given base path, returning result.
1278 *
1279 * @hide
1280 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001281 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001282 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001283 File cur = base;
1284 for (String segment : segments) {
1285 if (cur == null) {
1286 cur = new File(segment);
1287 } else {
1288 cur = new File(cur, segment);
1289 }
1290 }
1291 return cur;
1292 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001293
1294 /**
1295 * If the given path exists on emulated external storage, return the
1296 * translated backing path hosted on internal storage. This bypasses any
1297 * emulation later, improving performance. This is <em>only</em> suitable
1298 * for read-only access.
1299 * <p>
1300 * Returns original path if given path doesn't meet these criteria. Callers
1301 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1302 * permission.
1303 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001304 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001305 * @hide
1306 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001307 @UnsupportedAppUsage
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001308 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001309 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001310 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312}