blob: 21a1e0f0a1085ae4829a54578a5b45cd88e8ed09 [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;
Artur Satayevafdb23a2019-12-10 17:47:53 +000026import android.compat.annotation.UnsupportedAppUsage;
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;
Hung-ying Tyan774d89e2020-02-05 16:55:01 +080037import java.io.IOException;
Jeff Sharkey04b4ba12019-12-15 22:42:42 -070038import java.util.ArrayList;
39import java.util.Collection;
Jeff Sharkey20356142017-12-06 15:22:05 -070040import java.util.LinkedList;
Hung-ying Tyan774d89e2020-02-05 16:55:01 +080041import java.util.List;
Gilles Debunneee1d6302011-05-13 10:09:32 -070042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043/**
44 * Provides access to environment variables.
45 */
46public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080047 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Jeff Sharkeydd02e332018-06-27 14:41:57 -060049 // NOTE: keep credential-protected paths in sync with StrictMode.java
50
Jeff Sharkeyb049e212012-09-07 23:16:01 -070051 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080052 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070053 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060054 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070055 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080056 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080057 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080058 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070059 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090060 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Jeongik Cha9ec059a2019-07-04 21:12:06 +090061 private static final String ENV_SYSTEM_EXT_ROOT = "SYSTEM_EXT_ROOT";
Dario Frenic3e1bb722019-10-09 15:43:38 +010062 private static final String ENV_APEX_ROOT = "APEX_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070063
Jeff Sharkeydfa45302012-09-12 16:25:22 -070064 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070065 public static final String DIR_ANDROID = "Android";
66 private static final String DIR_DATA = "data";
67 private static final String DIR_MEDIA = "media";
68 private static final String DIR_OBB = "obb";
69 private static final String DIR_FILES = "files";
70 private static final String DIR_CACHE = "cache";
71
72 /** {@hide} */
73 @Deprecated
74 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070075
Jeff Sharkey63d0a062013-03-01 16:12:55 -080076 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070077 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060078 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070079 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080080 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080081 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080082 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070083 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090084 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Jeongik Cha9ec059a2019-07-04 21:12:06 +090085 private static final File DIR_SYSTEM_EXT_ROOT = getDirectory(ENV_SYSTEM_EXT_ROOT,
Dario Frenic3e1bb722019-10-09 15:43:38 +010086 "/system_ext");
87 private static final File DIR_APEX_ROOT = getDirectory(ENV_APEX_ROOT,
88 "/apex");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
Andrei Onea24ec3212019-03-15 17:35:05 +000090 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000091 private static UserEnvironment sCurrentUser;
92 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070093
Andreas Gamped281b422016-07-08 03:50:27 +000094 static {
95 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070096 }
97
98 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +000099 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +0000100 public static void initForCurrentUser() {
101 final int userId = UserHandle.myUserId();
102 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700103 }
104
105 /** {@hide} */
106 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -0700107 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700108
Andrei Onea24ec3212019-03-15 17:35:05 +0000109 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700110 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700111 mUserId = userId;
112 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700113
Andrei Onea24ec3212019-03-15 17:35:05 +0000114 @UnsupportedAppUsage
Jeff Sharkey48877892015-03-18 11:27:19 -0700115 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -0700116 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
117 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700118 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700119 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700120 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700121 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700122 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700123 }
124
Andrei Onea24ec3212019-03-15 17:35:05 +0000125 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700126 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700127 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700128 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700129 }
130
Andrei Onea24ec3212019-03-15 17:35:05 +0000131 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700132 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700133 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700134 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700135 }
136
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700137 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700138 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700139 }
140
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700141 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700142 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700143 }
144
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700145 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700146 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700147 }
148
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700149 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700150 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700151 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700152
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700153 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700154 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700155 }
156
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700157 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700158 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700159 }
160
161 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700162 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700163 }
164
165 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700166 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700167 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700168 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800171 * Return root of the "system" partition holding the core Android OS.
172 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700174 public static @NonNull File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800175 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 }
177
Jeff Sharkeyec19e9b2019-08-22 11:12:11 -0600178 /**
179 * Return root directory where all external storage devices will be mounted.
180 * For example, {@link #getExternalStorageDirectory()} will appear under
181 * this location.
182 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700183 public static @NonNull File getStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700184 return DIR_ANDROID_STORAGE;
185 }
186
Jason parksa3cdaa52011-01-13 14:15:43 -0600187 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800188 * Return root directory of the "oem" partition holding OEM customizations,
189 * if any. If present, the partition is mounted read-only.
190 *
191 * @hide
192 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600193 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700194 public static @NonNull File getOemDirectory() {
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800195 return DIR_OEM_ROOT;
196 }
197
198 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800199 * Return root directory of the "odm" partition holding ODM customizations,
200 * if any. If present, the partition is mounted read-only.
201 *
202 * @hide
203 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600204 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700205 public static @NonNull File getOdmDirectory() {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800206 return DIR_ODM_ROOT;
207 }
208
209 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700210 * Return root directory of the "vendor" partition that holds vendor-provided
211 * software that should persist across simple reflashing of the "system" partition.
212 * @hide
213 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600214 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700215 public static @NonNull File getVendorDirectory() {
Christopher Tate740888f2014-04-18 12:24:57 -0700216 return DIR_VENDOR_ROOT;
217 }
218
Jason parksa3cdaa52011-01-13 14:15:43 -0600219 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900220 * Return root directory of the "product" partition holding product-specific
221 * customizations if any. If present, the partition is mounted read-only.
222 *
223 * @hide
224 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600225 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000226 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700227 public static @NonNull File getProductDirectory() {
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900228 return DIR_PRODUCT_ROOT;
229 }
230
231 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100232 * Return root directory of the "product_services" partition holding middleware
233 * services if any. If present, the partition is mounted read-only.
234 *
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900235 * @deprecated This directory is not guaranteed to exist.
236 * Its name is changed to "system_ext" because the partition's purpose is changed.
237 * {@link #getSystemExtDirectory()}
Dario Freni2bef1762018-06-01 14:02:08 +0100238 * @hide
239 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600240 @SystemApi
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900241 @Deprecated
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700242 public static @NonNull File getProductServicesDirectory() {
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900243 return getDirectory("PRODUCT_SERVICES_ROOT", "/product_services");
244 }
245
246 /**
247 * Return root directory of the "system_ext" partition holding system partition's extension
248 * If present, the partition is mounted read-only.
249 *
250 * @hide
251 */
252 @SystemApi
253 public static @NonNull File getSystemExtDirectory() {
254 return DIR_SYSTEM_EXT_ROOT;
Dario Freni2bef1762018-06-01 14:02:08 +0100255 }
256
257 /**
Dario Frenic3e1bb722019-10-09 15:43:38 +0100258 * Return root directory of the apex mount point, where all the apex modules are made available
259 * to the rest of the system.
260 *
261 * @hide
262 */
263 public static @NonNull File getApexDirectory() {
264 return DIR_APEX_ROOT;
265 }
266
267 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700268 * Return the system directory for a user. This is for use by system
269 * services to store files relating to the user. This directory will be
270 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700271 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600272 * @deprecated This directory is valid and still exists, but but callers
273 * should <em>strongly</em> consider switching to using either
274 * {@link #getDataSystemCeDirectory(int)} or
275 * {@link #getDataSystemDeDirectory(int)}, both of which support
276 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700277 * @hide
278 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700279 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700280 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800281 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700282 }
283
284 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700285 * Returns the config directory for a user. This is for use by system
286 * services to store files relating to the user which should be readable by
287 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100288 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700289 * @deprecated This directory is valid and still exists, but callers should
290 * <em>strongly</em> consider switching to
291 * {@link #getDataMiscCeDirectory(int)} which is protected with
292 * user credentials or {@link #getDataMiscDeDirectory(int)}
293 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100294 * @hide
295 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700296 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100297 public static File getUserConfigDirectory(int userId) {
298 return new File(new File(new File(
299 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
300 }
301
302 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700303 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 */
305 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800306 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700309 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700310 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700311 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800312 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700313 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700314 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700315 }
316 }
317
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700318 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600319 public static File getExpandDirectory() {
320 return DIR_ANDROID_EXPAND;
321 }
322
323 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000324 @UnsupportedAppUsage
Jeff Sharkey15447792015-11-05 16:18:51 -0800325 public static File getDataSystemDirectory() {
326 return new File(getDataDirectory(), "system");
327 }
328
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700329 /**
330 * Returns the base directory for per-user system directory, device encrypted.
331 * {@hide}
332 */
333 public static File getDataSystemDeDirectory() {
334 return buildPath(getDataDirectory(), "system_de");
335 }
336
337 /**
338 * Returns the base directory for per-user system directory, credential encrypted.
339 * {@hide}
340 */
341 public static File getDataSystemCeDirectory() {
342 return buildPath(getDataDirectory(), "system_ce");
343 }
344
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600345 /**
346 * Return the "credential encrypted" system directory for a user. This is
347 * for use by system services to store files relating to the user. This
348 * directory supports fast user wipe, and will be automatically deleted when
349 * the user is removed.
350 * <p>
351 * Data stored under this path is "credential encrypted", which uses an
352 * encryption key that is entangled with user credentials, such as a PIN or
353 * password. The contents will only be available once the user has been
354 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
355 * <p>
356 * New code should <em>strongly</em> prefer storing sensitive data in these
357 * credential encrypted areas.
358 *
359 * @hide
360 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700361 public static File getDataSystemCeDirectory(int userId) {
362 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800363 }
364
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600365 /**
366 * Return the "device encrypted" system directory for a user. This is for
367 * use by system services to store files relating to the user. This
368 * directory supports fast user wipe, and will be automatically deleted when
369 * the user is removed.
370 * <p>
371 * Data stored under this path is "device encrypted", which uses an
372 * encryption key that is tied to the physical device. The contents will
373 * only be available once the device has finished a {@code dm-verity}
374 * protected boot.
375 * <p>
376 * New code should <em>strongly</em> avoid storing sensitive data in these
377 * device encrypted areas.
378 *
379 * @hide
380 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700381 public static File getDataSystemDeDirectory(int userId) {
382 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
383 }
384
385 /** {@hide} */
386 public static File getDataMiscDirectory() {
387 return new File(getDataDirectory(), "misc");
388 }
389
390 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800391 public static File getDataMiscCeDirectory() {
392 return buildPath(getDataDirectory(), "misc_ce");
393 }
394
395 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700396 public static File getDataMiscCeDirectory(int userId) {
397 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
398 }
399
400 /** {@hide} */
401 public static File getDataMiscDeDirectory(int userId) {
402 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800403 }
404
Calin Juravled479b522016-02-24 16:22:03 +0000405 private static File getDataProfilesDeDirectory(int userId) {
406 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
407 }
408
409 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800410 public static File getDataVendorCeDirectory(int userId) {
411 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
412 }
413
414 /** {@hide} */
415 public static File getDataVendorDeDirectory(int userId) {
416 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
417 }
418
419 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800420 public static File getDataRefProfilesDePackageDirectory(String packageName) {
421 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100422 }
423
424 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000425 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
426 return buildPath(getDataProfilesDeDirectory(userId), packageName);
427 }
428
429 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700430 public static File getDataAppDirectory(String volumeUuid) {
431 return new File(getDataDirectory(volumeUuid), "app");
432 }
433
434 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000435 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery296c8b92019-02-27 12:06:24 +0000436 return new File(getDataDirectory(volumeUuid), "app-staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000437 }
438
439 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700440 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700441 return new File(getDataDirectory(volumeUuid), "user");
442 }
443
444 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700445 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
446 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700447 }
448
449 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700450 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700451 String packageName) {
452 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700453 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800454 }
455
456 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700457 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800458 return new File(getDataDirectory(volumeUuid), "user_de");
459 }
460
461 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700462 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
463 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800464 }
465
466 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700467 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800468 String packageName) {
469 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700470 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700471 }
472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700474 * Return preloads directory.
475 * <p>This directory may contain pre-loaded content such as
476 * {@link #getDataPreloadsDemoDirectory() demo videos} and
477 * {@link #getDataPreloadsAppsDirectory() APK files} .
478 * {@hide}
479 */
480 public static File getDataPreloadsDirectory() {
481 return new File(getDataDirectory(), "preloads");
482 }
483
484 /**
485 * @see #getDataPreloadsDirectory()
486 * {@hide}
487 */
488 public static File getDataPreloadsDemoDirectory() {
489 return new File(getDataPreloadsDirectory(), "demo");
490 }
491
492 /**
493 * @see #getDataPreloadsDirectory()
494 * {@hide}
495 */
496 public static File getDataPreloadsAppsDirectory() {
497 return new File(getDataPreloadsDirectory(), "apps");
498 }
499
500 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700501 * @see #getDataPreloadsDirectory()
502 * {@hide}
503 */
504 public static File getDataPreloadsMediaDirectory() {
505 return new File(getDataPreloadsDirectory(), "media");
506 }
507
508 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700509 * Returns location of preloaded cache directory for package name
510 * @see #getDataPreloadsDirectory()
511 * {@hide}
512 */
513 public static File getDataPreloadsFileCacheDirectory(String packageName) {
514 return new File(getDataPreloadsFileCacheDirectory(), packageName);
515 }
516
517 /**
518 * Returns location of preloaded cache directory.
519 * @see #getDataPreloadsDirectory()
520 * {@hide}
521 */
522 public static File getDataPreloadsFileCacheDirectory() {
523 return new File(getDataPreloadsDirectory(), "file_cache");
524 }
525
526 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800527 * Returns location of packages cache directory.
528 * {@hide}
529 */
530 public static File getPackageCacheDirectory() {
531 return new File(getDataSystemDirectory(), "package_cache");
532 }
533
534 /**
Jeff Sharkey04b4ba12019-12-15 22:42:42 -0700535 * Return locations where media files (such as ringtones, notification
536 * sounds, or alarm sounds) may be located on internal storage. These are
537 * typically indexed under {@link MediaStore#VOLUME_INTERNAL}.
538 *
539 * @hide
540 */
541 @SystemApi
542 public static @NonNull Collection<File> getInternalMediaDirectories() {
543 final ArrayList<File> res = new ArrayList<>();
Hung-ying Tyan774d89e2020-02-05 16:55:01 +0800544 addCanonicalFile(res, new File(Environment.getRootDirectory(), "media"));
545 addCanonicalFile(res, new File(Environment.getOemDirectory(), "media"));
546 addCanonicalFile(res, new File(Environment.getProductDirectory(), "media"));
Jeff Sharkey04b4ba12019-12-15 22:42:42 -0700547 return res;
548 }
549
Hung-ying Tyan774d89e2020-02-05 16:55:01 +0800550 private static void addCanonicalFile(List<File> list, File file) {
551 try {
552 list.add(file.getCanonicalFile());
553 } catch (IOException e) {
554 Log.w(TAG, "Failed to resolve " + file + ": " + e);
555 list.add(file);
556 }
557 }
558
Jeff Sharkey04b4ba12019-12-15 22:42:42 -0700559 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700560 * Return the primary shared/external storage directory. This directory may
561 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800562 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700563 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800564 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700565 * <p>
566 * <em>Note: don't be confused by the word "external" here. This directory
567 * can better be thought as media/shared storage. It is a filesystem that
568 * can hold a relatively large amount of data and that is shared across all
569 * applications (does not enforce permissions). Traditionally this is an SD
570 * card, but it may also be implemented as built-in storage in a device that
571 * is distinct from the protected internal storage and can be mounted as a
572 * filesystem on a computer.</em>
573 * <p>
574 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700575 * each user has their own isolated shared storage. Applications only have
576 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700577 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700578 * In devices with multiple shared/external storage directories, this
579 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700580 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700581 * {@link Context#getExternalFilesDirs(String)},
582 * {@link Context#getExternalCacheDirs()}, and
583 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700584 * <p>
585 * Applications should not directly use this top-level directory, in order
586 * to avoid polluting the user's root namespace. Any files that are private
587 * to the application should be placed in a directory returned by
588 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700589 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700590 * if the application is uninstalled. Other shared files should be placed in
591 * one of the directories returned by
592 * {@link #getExternalStoragePublicDirectory}.
593 * <p>
594 * Writing to this path requires the
595 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Jeff Sharkey488162b2019-04-27 17:03:54 -0600596 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read
597 * access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700598 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700599 * which is automatically granted if you hold the write permission.
600 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700601 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700602 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700603 * {@link Context#getExternalFilesDir(String)},
604 * {@link Context#getExternalCacheDir()}, or
605 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700606 * read or write.
607 * <p>
608 * This path may change between platform versions, so applications should
609 * only persist relative paths.
610 * <p>
611 * Here is an example of typical code to monitor the state of external
612 * storage:
613 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700614 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800615 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700616 *
617 * @see #getExternalStorageState()
618 * @see #isExternalStorageRemovable()
Jeff Sharkey488162b2019-04-27 17:03:54 -0600619 * @deprecated To improve user privacy, direct access to shared/external
620 * storage devices is deprecated. When an app targets
621 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
622 * from this method is no longer directly accessible to apps.
623 * Apps can continue to access content stored on shared/external
624 * storage by migrating to alternatives such as
625 * {@link Context#getExternalFilesDir(String)},
626 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600628 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700630 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000631 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700632 }
633
634 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000635 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700636 public static File getLegacyExternalStorageDirectory() {
637 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700640 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000641 @UnsupportedAppUsage
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700642 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700643 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700644 }
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800647 * Standard directory in which to place any audio files that should be
648 * in the regular list of music for the user.
649 * This may be combined with
650 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
651 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
652 * of directories to categories a particular audio file as more than one
653 * type.
654 */
655 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800656
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800657 /**
658 * Standard directory in which to place any audio files that should be
659 * in the list of podcasts that the user can select (not as regular
660 * music).
661 * This may be combined with {@link #DIRECTORY_MUSIC},
662 * {@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_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -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 ringtones that the user can select (not as regular
672 * music).
673 * This may be combined with {@link #DIRECTORY_MUSIC},
674 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
675 * {@link #DIRECTORY_ALARMS} as a series
676 * of directories to categories a particular audio file as more than one
677 * type.
678 */
679 public static String DIRECTORY_RINGTONES = "Ringtones";
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 alarms 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},
687 * and {@link #DIRECTORY_RINGTONES} as a series
688 * of directories to categories a particular audio file as more than one
689 * type.
690 */
691 public static String DIRECTORY_ALARMS = "Alarms";
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 notifications that the user can select (not as regular
696 * music).
697 * This may be combined with {@link #DIRECTORY_MUSIC},
698 * {@link #DIRECTORY_PODCASTS},
699 * {@link #DIRECTORY_ALARMS}, 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_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800704
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800705 /**
706 * Standard directory in which to place pictures that are available to
707 * the user. Note that this is primarily a convention for the top-level
708 * public directory, as the media scanner will find and collect pictures
709 * in any directory.
710 */
711 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800712
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800713 /**
714 * Standard directory in which to place movies that are available to
715 * the user. Note that this is primarily a convention for the top-level
716 * public directory, as the media scanner will find and collect movies
717 * in any directory.
718 */
719 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800720
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800721 /**
722 * Standard directory in which to place files that have been downloaded by
723 * the user. Note that this is primarily a convention for the top-level
724 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700725 * private directories. Also note that though the constant here is
726 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
727 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800728 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700729 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800730
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800731 /**
732 * The traditional location for pictures and videos when mounting the
733 * device as a camera. Note that this is primarily a convention for the
734 * top-level public directory, as this convention makes no sense elsewhere.
735 */
736 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700737
738 /**
739 * Standard directory in which to place documents that have been created by
740 * the user.
741 */
742 public static String DIRECTORY_DOCUMENTS = "Documents";
743
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800744 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600745 * Standard directory in which to place screenshots that have been taken by
746 * the user. Typically used as a secondary directory under
747 * {@link #DIRECTORY_PICTURES}.
748 */
749 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
750
751 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700752 * Standard directory in which to place any audio files which are
753 * audiobooks.
754 */
755 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
756
757 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800758 * List of standard storage directories.
759 * <p>
760 * Each of its values have its own constant:
761 * <ul>
762 * <li>{@link #DIRECTORY_MUSIC}
763 * <li>{@link #DIRECTORY_PODCASTS}
764 * <li>{@link #DIRECTORY_ALARMS}
765 * <li>{@link #DIRECTORY_RINGTONES}
766 * <li>{@link #DIRECTORY_NOTIFICATIONS}
767 * <li>{@link #DIRECTORY_PICTURES}
768 * <li>{@link #DIRECTORY_MOVIES}
769 * <li>{@link #DIRECTORY_DOWNLOADS}
770 * <li>{@link #DIRECTORY_DCIM}
771 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700772 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800773 * </ul>
774 * @hide
775 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800776 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800777 DIRECTORY_MUSIC,
778 DIRECTORY_PODCASTS,
779 DIRECTORY_RINGTONES,
780 DIRECTORY_ALARMS,
781 DIRECTORY_NOTIFICATIONS,
782 DIRECTORY_PICTURES,
783 DIRECTORY_MOVIES,
784 DIRECTORY_DOWNLOADS,
785 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700786 DIRECTORY_DOCUMENTS,
787 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800788 };
789
790 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800791 * @hide
792 */
793 public static boolean isStandardDirectory(String dir) {
794 for (String valid : STANDARD_DIRECTORIES) {
795 if (valid.equals(dir)) {
796 return true;
797 }
798 }
799 return false;
800 }
801
Jeff Sharkey20356142017-12-06 15:22:05 -0700802 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
803 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
804 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
805 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
806 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
807 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
808 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
809 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
810 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
811 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700812 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700813
814 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
815 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
816
817 /**
818 * Classify the content types present on the given external storage device.
819 * <p>
820 * This is typically useful for deciding if an inserted SD card is empty, or
821 * if it contains content like photos that should be preserved.
822 *
823 * @hide
824 */
825 public static int classifyExternalStorageDirectory(File dir) {
826 int res = 0;
827 for (File f : FileUtils.listFilesOrEmpty(dir)) {
828 if (f.isFile() && isInterestingFile(f)) {
829 res |= HAS_OTHER;
830 } else if (f.isDirectory() && hasInterestingFiles(f)) {
831 final String name = f.getName();
832 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
833 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
834 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
835 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
836 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
837 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
838 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
839 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
840 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
841 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700842 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700843 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
844 else res |= HAS_OTHER;
845 }
846 }
847 return res;
848 }
849
850 private static boolean hasInterestingFiles(File dir) {
851 final LinkedList<File> explore = new LinkedList<>();
852 explore.add(dir);
853 while (!explore.isEmpty()) {
854 dir = explore.pop();
855 for (File f : FileUtils.listFilesOrEmpty(dir)) {
856 if (isInterestingFile(f)) return true;
857 if (f.isDirectory()) explore.add(f);
858 }
859 }
860 return false;
861 }
862
863 private static boolean isInterestingFile(File file) {
864 if (file.isFile()) {
865 final String name = file.getName().toLowerCase();
866 if (name.endsWith(".exe") || name.equals("autorun.inf")
867 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
868 return false;
869 } else {
870 return true;
871 }
872 } else {
873 return false;
874 }
875 }
876
Felipe Lemeb012f912016-01-22 16:49:55 -0800877 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700878 * Get a top-level shared/external storage directory for placing files of a
879 * particular type. This is where the user will typically place and manage
880 * their own files, so you should be careful about what you put here to
881 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800882 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700883 * <p>
884 * On devices with multiple users (as described by {@link UserManager}),
885 * each user has their own isolated shared storage. Applications only have
886 * access to the shared storage for the user they're running as.
887 * </p>
888 * <p>
889 * Here is an example of typical code to manipulate a picture on the public
890 * shared storage:
891 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800892 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
893 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800894 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700895 * @param type The type of storage directory to return. Should be one of
896 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
897 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
898 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800899 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
900 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700901 * @return Returns the File path for the directory. Note that this directory
902 * may not yet exist, so you must make sure it exists before using
903 * it such as with {@link File#mkdirs File.mkdirs()}.
Jeff Sharkey488162b2019-04-27 17:03:54 -0600904 * @deprecated To improve user privacy, direct access to shared/external
905 * storage devices is deprecated. When an app targets
906 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
907 * from this method is no longer directly accessible to apps.
908 * Apps can continue to access content stored on shared/external
909 * storage by migrating to alternatives such as
910 * {@link Context#getExternalFilesDir(String)},
911 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800912 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600913 @Deprecated
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800914 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700915 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000916 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800917 }
918
919 /**
920 * Returns the path for android-specific data on the SD card.
921 * @hide
922 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000923 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700924 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700925 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000926 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800927 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700928
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800929 /**
930 * Generates the raw path to an application's data
931 * @hide
932 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000933 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700934 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700935 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000936 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800937 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800938
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800939 /**
940 * Generates the raw path to an application's media
941 * @hide
942 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000943 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700944 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700945 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000946 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800947 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800948
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800949 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800950 * Generates the raw path to an application's OBB files
951 * @hide
952 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000953 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700954 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700955 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000956 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800957 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800958
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800959 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800960 * Generates the path to an application's files.
961 * @hide
962 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000963 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700964 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700965 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000966 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800967 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700968
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800969 /**
970 * Generates the path to an application's cache.
971 * @hide
972 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000973 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700974 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700975 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000976 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800977 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800978
Sudheer Shanka25f1c6e2019-04-22 17:03:47 -0700979 /** @hide */
980 public static File[] buildExternalStoragePublicDirs(@NonNull String dirType) {
981 throwIfUserRequired();
982 return sCurrentUser.buildExternalStoragePublicDirs(dirType);
983 }
984
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800985 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700986 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 */
988 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800989 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991
992 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700993 * Unknown storage state, such as when a path isn't backed by known storage
994 * media.
995 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800996 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700997 */
998 public static final String MEDIA_UNKNOWN = "unknown";
999
1000 /**
1001 * Storage state if the media is not present.
1002 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001003 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 */
1005 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001008 * Storage state if the media is present but not mounted.
1009 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001010 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 */
1012 public static final String MEDIA_UNMOUNTED = "unmounted";
1013
1014 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001015 * Storage state if the media is present and being disk-checked.
1016 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001017 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 */
1019 public static final String MEDIA_CHECKING = "checking";
1020
1021 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001022 * Storage state if the media is present but is blank or is using an
1023 * unsupported filesystem.
1024 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001025 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 */
1027 public static final String MEDIA_NOFS = "nofs";
1028
1029 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001030 * Storage state if the media is present and mounted at its mount point with
1031 * read/write access.
1032 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001033 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final String MEDIA_MOUNTED = "mounted";
1036
1037 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001038 * Storage state if the media is present and mounted at its mount point with
1039 * read-only access.
1040 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001041 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
1044
1045 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001046 * Storage state if the media is present not mounted, and shared via USB
1047 * mass storage.
1048 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001049 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 */
1051 public static final String MEDIA_SHARED = "shared";
1052
1053 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001054 * Storage state if the media was removed before it was unmounted.
1055 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001056 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 */
1058 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
1059
1060 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001061 * Storage state if the media is present but cannot be mounted. Typically
1062 * this happens if the file system on the media is corrupted.
1063 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001064 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 */
1066 public static final String MEDIA_UNMOUNTABLE = "unmountable";
1067
1068 /**
Jeff Sharkey48877892015-03-18 11:27:19 -07001069 * Storage state if the media is in the process of being ejected.
1070 *
1071 * @see #getExternalStorageState(File)
1072 */
1073 public static final String MEDIA_EJECTING = "ejecting";
1074
1075 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001076 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -08001077 *
Jeff Sharkey8c165792012-10-22 14:08:29 -07001078 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001079 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1080 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1081 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1082 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1083 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 */
1085 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +00001086 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001087 return getExternalStorageState(externalDir);
1088 }
1089
1090 /**
1091 * @deprecated use {@link #getExternalStorageState(File)}
1092 */
1093 @Deprecated
1094 public static String getStorageState(File path) {
1095 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001096 }
1097
1098 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001099 * Returns the current state of the shared/external storage media at the
1100 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001101 *
1102 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1103 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1104 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1105 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1106 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
1107 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001108 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001109 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001110 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001111 return volume.getState();
1112 } else {
1113 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116
Dianne Hackborn407f6252010-10-04 11:31:17 -07001117 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001118 * Returns whether the primary shared/external storage media is physically
1119 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001120 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001121 * @return true if the storage device can be removed (such as an SD card),
1122 * or false if the storage device is built in and cannot be
1123 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001124 */
1125 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001126 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001127 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001128 }
1129
Kenny Roote1ff2142010-10-12 11:20:01 -07001130 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001131 * Returns whether the shared/external storage media at the given path is
1132 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001133 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001134 * @return true if the storage device can be removed (such as an SD card),
1135 * or false if the storage device is built in and cannot be
1136 * physically removed.
1137 * @throws IllegalArgumentException if the path is not a valid storage
1138 * device.
1139 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001140 public static boolean isExternalStorageRemovable(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001141 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001142 if (volume != null) {
1143 return volume.isRemovable();
1144 } else {
1145 throw new IllegalArgumentException("Failed to find storage device at " + path);
1146 }
1147 }
1148
1149 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001150 * Returns whether the primary shared/external storage media is emulated.
1151 * <p>
1152 * The contents of emulated storage devices are backed by a private user
1153 * data partition, which means there is little benefit to apps storing data
1154 * here instead of the private directories returned by
1155 * {@link Context#getFilesDir()}, etc.
1156 * <p>
1157 * This returns true when emulated storage is backed by either internal
1158 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001159 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001160 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1161 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001162 */
1163 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001164 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001165 return isExternalStorageEmulated(externalDir);
1166 }
1167
1168 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001169 * Returns whether the shared/external storage media at the given path is
1170 * emulated.
1171 * <p>
1172 * The contents of emulated storage devices are backed by a private user
1173 * data partition, which means there is little benefit to apps storing data
1174 * here instead of the private directories returned by
1175 * {@link Context#getFilesDir()}, etc.
1176 * <p>
1177 * This returns true when emulated storage is backed by either internal
1178 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001179 *
1180 * @throws IllegalArgumentException if the path is not a valid storage
1181 * device.
1182 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001183 public static boolean isExternalStorageEmulated(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001184 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001185 if (volume != null) {
1186 return volume.isEmulated();
1187 } else {
1188 throw new IllegalArgumentException("Failed to find storage device at " + path);
1189 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001190 }
1191
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001192 /**
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001193 * Returns whether the primary shared/external storage media is a legacy
1194 * view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001195 * <p>
1196 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001197 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
1198 * may inherit its legacy state based on when it was first installed.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001199 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001200 * Non-legacy apps can continue to discover and read media belonging to
1201 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001202 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001203 public static boolean isExternalStorageLegacy() {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001204 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001205 return isExternalStorageLegacy(externalDir);
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001206 }
1207
1208 /**
1209 * Returns whether the shared/external storage media at the given path is a
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001210 * legacy view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001211 * <p>
1212 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001213 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
1214 * may inherit its legacy state based on when it was first installed.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001215 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001216 * Non-legacy apps can continue to discover and read media belonging to
1217 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001218 *
1219 * @throws IllegalArgumentException if the path is not a valid storage
1220 * device.
1221 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001222 public static boolean isExternalStorageLegacy(@NonNull File path) {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001223 final Context context = AppGlobals.getInitialApplication();
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001224 final int uid = context.getApplicationInfo().uid;
1225 if (Process.isIsolated(uid)) {
1226 return false;
1227 }
1228
1229 final PackageManager packageManager = context.getPackageManager();
1230 if (packageManager.isInstantApp()) {
1231 return false;
1232 }
1233
1234 if (packageManager.checkPermission(Manifest.permission.WRITE_MEDIA_STORAGE,
1235 context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
1236 return true;
1237 }
1238
1239 if (packageManager.checkPermission(Manifest.permission.INSTALL_PACKAGES,
1240 context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
1241 return true;
1242 }
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001243 final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001244 final String[] packagesForUid = packageManager.getPackagesForUid(uid);
1245 for (String packageName : packagesForUid) {
1246 if (appOps.checkOpNoThrow(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES,
1247 uid, packageName) == AppOpsManager.MODE_ALLOWED) {
1248 return true;
1249 }
1250 }
Jeff Sharkeye9fcabc2019-04-04 11:19:22 -06001251
Svet Ganovd563e932019-04-14 13:07:41 -07001252 return appOps.checkOpNoThrow(AppOpsManager.OP_LEGACY_STORAGE,
Sudheer Shanka572fdfd2019-06-04 16:46:57 -07001253 uid, context.getOpPackageName()) == AppOpsManager.MODE_ALLOWED;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001254 }
1255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 static File getDirectory(String variableName, String defaultPath) {
1257 String path = System.getenv(variableName);
1258 return path == null ? new File(defaultPath) : new File(path);
1259 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001260
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001261 /** {@hide} */
1262 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001263 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001264 }
1265
1266 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001267 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001268 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1269 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001270 }
1271 }
1272
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001273 /**
1274 * Append path segments to each given base path, returning result.
1275 *
1276 * @hide
1277 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001278 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001279 public static File[] buildPaths(File[] base, String... segments) {
1280 File[] result = new File[base.length];
1281 for (int i = 0; i < base.length; i++) {
1282 result[i] = buildPath(base[i], segments);
1283 }
1284 return result;
1285 }
1286
1287 /**
1288 * Append path segments to given base path, returning result.
1289 *
1290 * @hide
1291 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001292 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001293 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001294 File cur = base;
1295 for (String segment : segments) {
1296 if (cur == null) {
1297 cur = new File(segment);
1298 } else {
1299 cur = new File(cur, segment);
1300 }
1301 }
1302 return cur;
1303 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001304
1305 /**
1306 * If the given path exists on emulated external storage, return the
1307 * translated backing path hosted on internal storage. This bypasses any
1308 * emulation later, improving performance. This is <em>only</em> suitable
1309 * for read-only access.
1310 * <p>
1311 * Returns original path if given path doesn't meet these criteria. Callers
1312 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1313 * permission.
1314 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001315 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001316 * @hide
1317 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001318 @UnsupportedAppUsage
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001319 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001320 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001321 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323}