blob: 3716b3edaed42e666b2ecd086cf3ce61220ced72 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Jeff Sharkeya30e5c32019-02-28 12:02:10 -070019import android.annotation.NonNull;
Jeff Sharkey70eb34a2018-09-13 11:57:03 -060020import android.annotation.SystemApi;
Philip P. Moltmannf80809f2018-04-04 11:20:44 -070021import android.annotation.TestApi;
Andrei Onea24ec3212019-03-15 17:35:05 +000022import android.annotation.UnsupportedAppUsage;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -060023import android.app.AppGlobals;
24import android.app.AppOpsManager;
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080025import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070026import android.content.Context;
Jeff Sharkey488162b2019-04-27 17:03:54 -060027import android.content.Intent;
Jeff Sharkey48877892015-03-18 11:27:19 -070028import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070029import android.os.storage.StorageVolume;
Jeff Sharkey488162b2019-04-27 17:03:54 -060030import android.provider.MediaStore;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070031import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080032import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080033
Gilles Debunneee1d6302011-05-13 10:09:32 -070034import java.io.File;
Jeff Sharkey20356142017-12-06 15:22:05 -070035import java.util.LinkedList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037/**
38 * Provides access to environment variables.
39 */
40public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080041 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Jeff Sharkeydd02e332018-06-27 14:41:57 -060043 // NOTE: keep credential-protected paths in sync with StrictMode.java
44
Jeff Sharkeyb049e212012-09-07 23:16:01 -070045 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080046 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070047 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060048 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070049 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080050 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080051 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080052 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070053 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090054 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Dario Freni2bef1762018-06-01 14:02:08 +010055 private static final String ENV_PRODUCT_SERVICES_ROOT = "PRODUCT_SERVICES_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070056
Jeff Sharkeydfa45302012-09-12 16:25:22 -070057 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070058 public static final String DIR_ANDROID = "Android";
59 private static final String DIR_DATA = "data";
60 private static final String DIR_MEDIA = "media";
61 private static final String DIR_OBB = "obb";
62 private static final String DIR_FILES = "files";
63 private static final String DIR_CACHE = "cache";
64
65 /** {@hide} */
66 @Deprecated
67 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070068
Jeff Sharkey63d0a062013-03-01 16:12:55 -080069 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070070 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060071 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070072 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080073 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080074 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080075 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070076 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090077 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Dario Freni2bef1762018-06-01 14:02:08 +010078 private static final File DIR_PRODUCT_SERVICES_ROOT = getDirectory(ENV_PRODUCT_SERVICES_ROOT,
79 "/product_services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Andrei Onea24ec3212019-03-15 17:35:05 +000081 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000082 private static UserEnvironment sCurrentUser;
83 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070084
Andreas Gamped281b422016-07-08 03:50:27 +000085 static {
86 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070087 }
88
89 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +000090 @UnsupportedAppUsage
Andreas Gamped281b422016-07-08 03:50:27 +000091 public static void initForCurrentUser() {
92 final int userId = UserHandle.myUserId();
93 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070094 }
95
96 /** {@hide} */
97 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070098 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070099
Andrei Onea24ec3212019-03-15 17:35:05 +0000100 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700101 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700102 mUserId = userId;
103 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700104
Andrei Onea24ec3212019-03-15 17:35:05 +0000105 @UnsupportedAppUsage
Jeff Sharkey48877892015-03-18 11:27:19 -0700106 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -0700107 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
108 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700109 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700110 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700111 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700112 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700113 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114 }
115
Andrei Onea24ec3212019-03-15 17:35:05 +0000116 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700117 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700118 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700119 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700120 }
121
Andrei Onea24ec3212019-03-15 17:35:05 +0000122 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700123 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700124 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700125 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700126 }
127
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700128 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700129 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700130 }
131
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700132 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700133 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700134 }
135
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700136 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700137 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700138 }
139
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700140 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700141 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700142 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700143
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700144 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700145 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700146 }
147
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700148 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700149 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700150 }
151
152 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700153 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700154 }
155
156 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700157 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700158 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700159 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800162 * Return root of the "system" partition holding the core Android OS.
163 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700165 public static @NonNull File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800166 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
168
Jeff Sharkey48877892015-03-18 11:27:19 -0700169 /** {@hide} */
Jeff Sharkeyc6091162018-06-29 17:15:40 -0600170 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700171 public static @NonNull File getStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700172 return DIR_ANDROID_STORAGE;
173 }
174
Jason parksa3cdaa52011-01-13 14:15:43 -0600175 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800176 * Return root directory of the "oem" partition holding OEM customizations,
177 * if any. If present, the partition is mounted read-only.
178 *
179 * @hide
180 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600181 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700182 public static @NonNull File getOemDirectory() {
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800183 return DIR_OEM_ROOT;
184 }
185
186 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800187 * Return root directory of the "odm" partition holding ODM customizations,
188 * if any. If present, the partition is mounted read-only.
189 *
190 * @hide
191 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600192 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700193 public static @NonNull File getOdmDirectory() {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800194 return DIR_ODM_ROOT;
195 }
196
197 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700198 * Return root directory of the "vendor" partition that holds vendor-provided
199 * software that should persist across simple reflashing of the "system" partition.
200 * @hide
201 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600202 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700203 public static @NonNull File getVendorDirectory() {
Christopher Tate740888f2014-04-18 12:24:57 -0700204 return DIR_VENDOR_ROOT;
205 }
206
Jason parksa3cdaa52011-01-13 14:15:43 -0600207 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900208 * Return root directory of the "product" partition holding product-specific
209 * customizations if any. If present, the partition is mounted read-only.
210 *
211 * @hide
212 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600213 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000214 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700215 public static @NonNull File getProductDirectory() {
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900216 return DIR_PRODUCT_ROOT;
217 }
218
219 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100220 * Return root directory of the "product_services" partition holding middleware
221 * services if any. If present, the partition is mounted read-only.
222 *
223 * @hide
224 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600225 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700226 public static @NonNull File getProductServicesDirectory() {
Dario Freni2bef1762018-06-01 14:02:08 +0100227 return DIR_PRODUCT_SERVICES_ROOT;
228 }
229
230 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700231 * Return the system directory for a user. This is for use by system
232 * services to store files relating to the user. This directory will be
233 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700234 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600235 * @deprecated This directory is valid and still exists, but but callers
236 * should <em>strongly</em> consider switching to using either
237 * {@link #getDataSystemCeDirectory(int)} or
238 * {@link #getDataSystemDeDirectory(int)}, both of which support
239 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700240 * @hide
241 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700242 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700243 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800244 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700245 }
246
247 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700248 * Returns the config directory for a user. This is for use by system
249 * services to store files relating to the user which should be readable by
250 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100251 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700252 * @deprecated This directory is valid and still exists, but callers should
253 * <em>strongly</em> consider switching to
254 * {@link #getDataMiscCeDirectory(int)} which is protected with
255 * user credentials or {@link #getDataMiscDeDirectory(int)}
256 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100257 * @hide
258 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700259 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100260 public static File getUserConfigDirectory(int userId) {
261 return new File(new File(new File(
262 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
263 }
264
265 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700266 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 */
268 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800269 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700272 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700273 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700274 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800275 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700276 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700277 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700278 }
279 }
280
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700281 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600282 public static File getExpandDirectory() {
283 return DIR_ANDROID_EXPAND;
284 }
285
286 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000287 @UnsupportedAppUsage
Jeff Sharkey15447792015-11-05 16:18:51 -0800288 public static File getDataSystemDirectory() {
289 return new File(getDataDirectory(), "system");
290 }
291
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700292 /**
293 * Returns the base directory for per-user system directory, device encrypted.
294 * {@hide}
295 */
296 public static File getDataSystemDeDirectory() {
297 return buildPath(getDataDirectory(), "system_de");
298 }
299
300 /**
301 * Returns the base directory for per-user system directory, credential encrypted.
302 * {@hide}
303 */
304 public static File getDataSystemCeDirectory() {
305 return buildPath(getDataDirectory(), "system_ce");
306 }
307
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600308 /**
309 * Return the "credential encrypted" system directory for a user. This is
310 * for use by system services to store files relating to the user. This
311 * directory supports fast user wipe, and will be automatically deleted when
312 * the user is removed.
313 * <p>
314 * Data stored under this path is "credential encrypted", which uses an
315 * encryption key that is entangled with user credentials, such as a PIN or
316 * password. The contents will only be available once the user has been
317 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
318 * <p>
319 * New code should <em>strongly</em> prefer storing sensitive data in these
320 * credential encrypted areas.
321 *
322 * @hide
323 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700324 public static File getDataSystemCeDirectory(int userId) {
325 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800326 }
327
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600328 /**
329 * Return the "device encrypted" system directory for a user. This is for
330 * use by system services to store files relating to the user. This
331 * directory supports fast user wipe, and will be automatically deleted when
332 * the user is removed.
333 * <p>
334 * Data stored under this path is "device encrypted", which uses an
335 * encryption key that is tied to the physical device. The contents will
336 * only be available once the device has finished a {@code dm-verity}
337 * protected boot.
338 * <p>
339 * New code should <em>strongly</em> avoid storing sensitive data in these
340 * device encrypted areas.
341 *
342 * @hide
343 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700344 public static File getDataSystemDeDirectory(int userId) {
345 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
346 }
347
348 /** {@hide} */
349 public static File getDataMiscDirectory() {
350 return new File(getDataDirectory(), "misc");
351 }
352
353 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800354 public static File getDataMiscCeDirectory() {
355 return buildPath(getDataDirectory(), "misc_ce");
356 }
357
358 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700359 public static File getDataMiscCeDirectory(int userId) {
360 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
361 }
362
363 /** {@hide} */
364 public static File getDataMiscDeDirectory(int userId) {
365 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800366 }
367
Calin Juravled479b522016-02-24 16:22:03 +0000368 private static File getDataProfilesDeDirectory(int userId) {
369 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
370 }
371
372 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800373 public static File getDataVendorCeDirectory(int userId) {
374 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
375 }
376
377 /** {@hide} */
378 public static File getDataVendorDeDirectory(int userId) {
379 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
380 }
381
382 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800383 public static File getDataRefProfilesDePackageDirectory(String packageName) {
384 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100385 }
386
387 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000388 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
389 return buildPath(getDataProfilesDeDirectory(userId), packageName);
390 }
391
392 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700393 public static File getDataAppDirectory(String volumeUuid) {
394 return new File(getDataDirectory(volumeUuid), "app");
395 }
396
397 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000398 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery296c8b92019-02-27 12:06:24 +0000399 return new File(getDataDirectory(volumeUuid), "app-staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000400 }
401
402 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700403 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700404 return new File(getDataDirectory(volumeUuid), "user");
405 }
406
407 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700408 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
409 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700410 }
411
412 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700413 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700414 String packageName) {
415 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700416 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800417 }
418
419 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700420 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800421 return new File(getDataDirectory(volumeUuid), "user_de");
422 }
423
424 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700425 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
426 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800427 }
428
429 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700430 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800431 String packageName) {
432 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700433 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700434 }
435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700437 * Return preloads directory.
438 * <p>This directory may contain pre-loaded content such as
439 * {@link #getDataPreloadsDemoDirectory() demo videos} and
440 * {@link #getDataPreloadsAppsDirectory() APK files} .
441 * {@hide}
442 */
443 public static File getDataPreloadsDirectory() {
444 return new File(getDataDirectory(), "preloads");
445 }
446
447 /**
448 * @see #getDataPreloadsDirectory()
449 * {@hide}
450 */
451 public static File getDataPreloadsDemoDirectory() {
452 return new File(getDataPreloadsDirectory(), "demo");
453 }
454
455 /**
456 * @see #getDataPreloadsDirectory()
457 * {@hide}
458 */
459 public static File getDataPreloadsAppsDirectory() {
460 return new File(getDataPreloadsDirectory(), "apps");
461 }
462
463 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700464 * @see #getDataPreloadsDirectory()
465 * {@hide}
466 */
467 public static File getDataPreloadsMediaDirectory() {
468 return new File(getDataPreloadsDirectory(), "media");
469 }
470
471 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700472 * Returns location of preloaded cache directory for package name
473 * @see #getDataPreloadsDirectory()
474 * {@hide}
475 */
476 public static File getDataPreloadsFileCacheDirectory(String packageName) {
477 return new File(getDataPreloadsFileCacheDirectory(), packageName);
478 }
479
480 /**
481 * Returns location of preloaded cache directory.
482 * @see #getDataPreloadsDirectory()
483 * {@hide}
484 */
485 public static File getDataPreloadsFileCacheDirectory() {
486 return new File(getDataPreloadsDirectory(), "file_cache");
487 }
488
489 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800490 * Returns location of packages cache directory.
491 * {@hide}
492 */
493 public static File getPackageCacheDirectory() {
494 return new File(getDataSystemDirectory(), "package_cache");
495 }
496
497 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700498 * Return the primary shared/external storage directory. This directory may
499 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800500 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700501 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800502 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700503 * <p>
504 * <em>Note: don't be confused by the word "external" here. This directory
505 * can better be thought as media/shared storage. It is a filesystem that
506 * can hold a relatively large amount of data and that is shared across all
507 * applications (does not enforce permissions). Traditionally this is an SD
508 * card, but it may also be implemented as built-in storage in a device that
509 * is distinct from the protected internal storage and can be mounted as a
510 * filesystem on a computer.</em>
511 * <p>
512 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700513 * each user has their own isolated shared storage. Applications only have
514 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700515 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700516 * In devices with multiple shared/external storage directories, this
517 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700518 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700519 * {@link Context#getExternalFilesDirs(String)},
520 * {@link Context#getExternalCacheDirs()}, and
521 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700522 * <p>
523 * Applications should not directly use this top-level directory, in order
524 * to avoid polluting the user's root namespace. Any files that are private
525 * to the application should be placed in a directory returned by
526 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700527 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700528 * if the application is uninstalled. Other shared files should be placed in
529 * one of the directories returned by
530 * {@link #getExternalStoragePublicDirectory}.
531 * <p>
532 * Writing to this path requires the
533 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Jeff Sharkey488162b2019-04-27 17:03:54 -0600534 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read
535 * access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700536 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700537 * which is automatically granted if you hold the write permission.
538 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700539 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700540 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700541 * {@link Context#getExternalFilesDir(String)},
542 * {@link Context#getExternalCacheDir()}, or
543 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700544 * read or write.
545 * <p>
546 * This path may change between platform versions, so applications should
547 * only persist relative paths.
548 * <p>
549 * Here is an example of typical code to monitor the state of external
550 * storage:
551 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700552 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800553 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700554 *
555 * @see #getExternalStorageState()
556 * @see #isExternalStorageRemovable()
Jeff Sharkey488162b2019-04-27 17:03:54 -0600557 * @deprecated To improve user privacy, direct access to shared/external
558 * storage devices is deprecated. When an app targets
559 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
560 * from this method is no longer directly accessible to apps.
561 * Apps can continue to access content stored on shared/external
562 * storage by migrating to alternatives such as
563 * {@link Context#getExternalFilesDir(String)},
564 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600566 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700568 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000569 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700570 }
571
572 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000573 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700574 public static File getLegacyExternalStorageDirectory() {
575 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
577
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700578 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000579 @UnsupportedAppUsage
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700580 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700581 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700582 }
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800585 * Standard directory in which to place any audio files that should be
586 * in the regular list of music for the user.
587 * This may be combined with
588 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
589 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
590 * of directories to categories a particular audio file as more than one
591 * type.
592 */
593 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800594
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800595 /**
596 * Standard directory in which to place any audio files that should be
597 * in the list of podcasts that the user can select (not as regular
598 * music).
599 * This may be combined with {@link #DIRECTORY_MUSIC},
600 * {@link #DIRECTORY_NOTIFICATIONS},
601 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
602 * of directories to categories a particular audio file as more than one
603 * type.
604 */
605 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800606
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800607 /**
608 * Standard directory in which to place any audio files that should be
609 * in the list of ringtones that the user can select (not as regular
610 * music).
611 * This may be combined with {@link #DIRECTORY_MUSIC},
612 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
613 * {@link #DIRECTORY_ALARMS} as a series
614 * of directories to categories a particular audio file as more than one
615 * type.
616 */
617 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800618
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800619 /**
620 * Standard directory in which to place any audio files that should be
621 * in the list of alarms that the user can select (not as regular
622 * music).
623 * This may be combined with {@link #DIRECTORY_MUSIC},
624 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
625 * and {@link #DIRECTORY_RINGTONES} as a series
626 * of directories to categories a particular audio file as more than one
627 * type.
628 */
629 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800630
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800631 /**
632 * Standard directory in which to place any audio files that should be
633 * in the list of notifications that the user can select (not as regular
634 * music).
635 * This may be combined with {@link #DIRECTORY_MUSIC},
636 * {@link #DIRECTORY_PODCASTS},
637 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
638 * of directories to categories a particular audio file as more than one
639 * type.
640 */
641 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800642
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800643 /**
644 * Standard directory in which to place pictures that are available to
645 * the user. Note that this is primarily a convention for the top-level
646 * public directory, as the media scanner will find and collect pictures
647 * in any directory.
648 */
649 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800650
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800651 /**
652 * Standard directory in which to place movies that are available to
653 * the user. Note that this is primarily a convention for the top-level
654 * public directory, as the media scanner will find and collect movies
655 * in any directory.
656 */
657 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800658
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800659 /**
660 * Standard directory in which to place files that have been downloaded by
661 * the user. Note that this is primarily a convention for the top-level
662 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700663 * private directories. Also note that though the constant here is
664 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
665 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800666 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700667 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800668
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800669 /**
670 * The traditional location for pictures and videos when mounting the
671 * device as a camera. Note that this is primarily a convention for the
672 * top-level public directory, as this convention makes no sense elsewhere.
673 */
674 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700675
676 /**
677 * Standard directory in which to place documents that have been created by
678 * the user.
679 */
680 public static String DIRECTORY_DOCUMENTS = "Documents";
681
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800682 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600683 * Standard directory in which to place screenshots that have been taken by
684 * the user. Typically used as a secondary directory under
685 * {@link #DIRECTORY_PICTURES}.
686 */
687 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
688
689 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700690 * Standard directory in which to place any audio files which are
691 * audiobooks.
692 */
693 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
694
695 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800696 * List of standard storage directories.
697 * <p>
698 * Each of its values have its own constant:
699 * <ul>
700 * <li>{@link #DIRECTORY_MUSIC}
701 * <li>{@link #DIRECTORY_PODCASTS}
702 * <li>{@link #DIRECTORY_ALARMS}
703 * <li>{@link #DIRECTORY_RINGTONES}
704 * <li>{@link #DIRECTORY_NOTIFICATIONS}
705 * <li>{@link #DIRECTORY_PICTURES}
706 * <li>{@link #DIRECTORY_MOVIES}
707 * <li>{@link #DIRECTORY_DOWNLOADS}
708 * <li>{@link #DIRECTORY_DCIM}
709 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700710 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800711 * </ul>
712 * @hide
713 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800714 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800715 DIRECTORY_MUSIC,
716 DIRECTORY_PODCASTS,
717 DIRECTORY_RINGTONES,
718 DIRECTORY_ALARMS,
719 DIRECTORY_NOTIFICATIONS,
720 DIRECTORY_PICTURES,
721 DIRECTORY_MOVIES,
722 DIRECTORY_DOWNLOADS,
723 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700724 DIRECTORY_DOCUMENTS,
725 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800726 };
727
728 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800729 * @hide
730 */
731 public static boolean isStandardDirectory(String dir) {
732 for (String valid : STANDARD_DIRECTORIES) {
733 if (valid.equals(dir)) {
734 return true;
735 }
736 }
737 return false;
738 }
739
Jeff Sharkey20356142017-12-06 15:22:05 -0700740 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
741 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
742 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
743 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
744 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
745 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
746 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
747 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
748 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
749 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700750 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700751
752 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
753 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
754
755 /**
756 * Classify the content types present on the given external storage device.
757 * <p>
758 * This is typically useful for deciding if an inserted SD card is empty, or
759 * if it contains content like photos that should be preserved.
760 *
761 * @hide
762 */
763 public static int classifyExternalStorageDirectory(File dir) {
764 int res = 0;
765 for (File f : FileUtils.listFilesOrEmpty(dir)) {
766 if (f.isFile() && isInterestingFile(f)) {
767 res |= HAS_OTHER;
768 } else if (f.isDirectory() && hasInterestingFiles(f)) {
769 final String name = f.getName();
770 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
771 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
772 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
773 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
774 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
775 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
776 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
777 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
778 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
779 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700780 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700781 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
782 else res |= HAS_OTHER;
783 }
784 }
785 return res;
786 }
787
788 private static boolean hasInterestingFiles(File dir) {
789 final LinkedList<File> explore = new LinkedList<>();
790 explore.add(dir);
791 while (!explore.isEmpty()) {
792 dir = explore.pop();
793 for (File f : FileUtils.listFilesOrEmpty(dir)) {
794 if (isInterestingFile(f)) return true;
795 if (f.isDirectory()) explore.add(f);
796 }
797 }
798 return false;
799 }
800
801 private static boolean isInterestingFile(File file) {
802 if (file.isFile()) {
803 final String name = file.getName().toLowerCase();
804 if (name.endsWith(".exe") || name.equals("autorun.inf")
805 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
806 return false;
807 } else {
808 return true;
809 }
810 } else {
811 return false;
812 }
813 }
814
Felipe Lemeb012f912016-01-22 16:49:55 -0800815 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700816 * Get a top-level shared/external storage directory for placing files of a
817 * particular type. This is where the user will typically place and manage
818 * their own files, so you should be careful about what you put here to
819 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800820 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700821 * <p>
822 * On devices with multiple users (as described by {@link UserManager}),
823 * each user has their own isolated shared storage. Applications only have
824 * access to the shared storage for the user they're running as.
825 * </p>
826 * <p>
827 * Here is an example of typical code to manipulate a picture on the public
828 * shared storage:
829 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800830 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
831 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800832 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700833 * @param type The type of storage directory to return. Should be one of
834 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
835 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
836 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800837 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
838 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700839 * @return Returns the File path for the directory. Note that this directory
840 * may not yet exist, so you must make sure it exists before using
841 * it such as with {@link File#mkdirs File.mkdirs()}.
Jeff Sharkey488162b2019-04-27 17:03:54 -0600842 * @deprecated To improve user privacy, direct access to shared/external
843 * storage devices is deprecated. When an app targets
844 * {@link android.os.Build.VERSION_CODES#Q}, the path returned
845 * from this method is no longer directly accessible to apps.
846 * Apps can continue to access content stored on shared/external
847 * storage by migrating to alternatives such as
848 * {@link Context#getExternalFilesDir(String)},
849 * {@link MediaStore}, or {@link Intent#ACTION_OPEN_DOCUMENT}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800850 */
Jeff Sharkey488162b2019-04-27 17:03:54 -0600851 @Deprecated
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800852 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700853 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000854 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800855 }
856
857 /**
858 * Returns the path for android-specific data on the SD card.
859 * @hide
860 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000861 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700862 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700863 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000864 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800865 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700866
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800867 /**
868 * Generates the raw path to an application's data
869 * @hide
870 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000871 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700872 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700873 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000874 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800875 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800876
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800877 /**
878 * Generates the raw path to an application's media
879 * @hide
880 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000881 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700882 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700883 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000884 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800885 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800886
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800887 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800888 * Generates the raw path to an application's OBB files
889 * @hide
890 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000891 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700892 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700893 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000894 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800895 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800896
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800897 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800898 * Generates the path to an application's files.
899 * @hide
900 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000901 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700902 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700903 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000904 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800905 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700906
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800907 /**
908 * Generates the path to an application's cache.
909 * @hide
910 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000911 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700912 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700913 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000914 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800915 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800916
Sudheer Shanka25f1c6e2019-04-22 17:03:47 -0700917 /** @hide */
918 public static File[] buildExternalStoragePublicDirs(@NonNull String dirType) {
919 throwIfUserRequired();
920 return sCurrentUser.buildExternalStoragePublicDirs(dirType);
921 }
922
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800923 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700924 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 */
926 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800927 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929
930 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700931 * Unknown storage state, such as when a path isn't backed by known storage
932 * media.
933 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800934 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700935 */
936 public static final String MEDIA_UNKNOWN = "unknown";
937
938 /**
939 * Storage state if the media is not present.
940 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800941 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 */
943 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700946 * Storage state if the media is present but not mounted.
947 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800948 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 */
950 public static final String MEDIA_UNMOUNTED = "unmounted";
951
952 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700953 * Storage state if the media is present and being disk-checked.
954 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800955 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 */
957 public static final String MEDIA_CHECKING = "checking";
958
959 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700960 * Storage state if the media is present but is blank or is using an
961 * unsupported filesystem.
962 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800963 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 */
965 public static final String MEDIA_NOFS = "nofs";
966
967 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700968 * Storage state if the media is present and mounted at its mount point with
969 * read/write access.
970 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800971 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 */
973 public static final String MEDIA_MOUNTED = "mounted";
974
975 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700976 * Storage state if the media is present and mounted at its mount point with
977 * read-only access.
978 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800979 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 */
981 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
982
983 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700984 * Storage state if the media is present not mounted, and shared via USB
985 * mass storage.
986 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800987 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 */
989 public static final String MEDIA_SHARED = "shared";
990
991 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700992 * Storage state if the media was removed before it was unmounted.
993 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800994 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 */
996 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
997
998 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700999 * Storage state if the media is present but cannot be mounted. Typically
1000 * this happens if the file system on the media is corrupted.
1001 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001002 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 */
1004 public static final String MEDIA_UNMOUNTABLE = "unmountable";
1005
1006 /**
Jeff Sharkey48877892015-03-18 11:27:19 -07001007 * Storage state if the media is in the process of being ejected.
1008 *
1009 * @see #getExternalStorageState(File)
1010 */
1011 public static final String MEDIA_EJECTING = "ejecting";
1012
1013 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001014 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -08001015 *
Jeff Sharkey8c165792012-10-22 14:08:29 -07001016 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001017 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1018 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1019 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1020 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1021 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 */
1023 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +00001024 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001025 return getExternalStorageState(externalDir);
1026 }
1027
1028 /**
1029 * @deprecated use {@link #getExternalStorageState(File)}
1030 */
1031 @Deprecated
1032 public static String getStorageState(File path) {
1033 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001034 }
1035
1036 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001037 * Returns the current state of the shared/external storage media at the
1038 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001039 *
1040 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1041 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1042 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1043 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1044 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
1045 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001046 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001047 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001048 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001049 return volume.getState();
1050 } else {
1051 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 }
1054
Dianne Hackborn407f6252010-10-04 11:31:17 -07001055 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001056 * Returns whether the primary shared/external storage media is physically
1057 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001058 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001059 * @return true if the storage device can be removed (such as an SD card),
1060 * or false if the storage device is built in and cannot be
1061 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001062 */
1063 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001064 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001065 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001066 }
1067
Kenny Roote1ff2142010-10-12 11:20:01 -07001068 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001069 * Returns whether the shared/external storage media at the given path is
1070 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001071 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001072 * @return true if the storage device can be removed (such as an SD card),
1073 * or false if the storage device is built in and cannot be
1074 * physically removed.
1075 * @throws IllegalArgumentException if the path is not a valid storage
1076 * device.
1077 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001078 public static boolean isExternalStorageRemovable(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001079 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001080 if (volume != null) {
1081 return volume.isRemovable();
1082 } else {
1083 throw new IllegalArgumentException("Failed to find storage device at " + path);
1084 }
1085 }
1086
1087 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001088 * Returns whether the primary shared/external storage media is emulated.
1089 * <p>
1090 * The contents of emulated storage devices are backed by a private user
1091 * data partition, which means there is little benefit to apps storing data
1092 * here instead of the private directories returned by
1093 * {@link Context#getFilesDir()}, etc.
1094 * <p>
1095 * This returns true when emulated storage is backed by either internal
1096 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001097 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001098 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1099 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001100 */
1101 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001102 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001103 return isExternalStorageEmulated(externalDir);
1104 }
1105
1106 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001107 * Returns whether the shared/external storage media at the given path is
1108 * emulated.
1109 * <p>
1110 * The contents of emulated storage devices are backed by a private user
1111 * data partition, which means there is little benefit to apps storing data
1112 * here instead of the private directories returned by
1113 * {@link Context#getFilesDir()}, etc.
1114 * <p>
1115 * This returns true when emulated storage is backed by either internal
1116 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001117 *
1118 * @throws IllegalArgumentException if the path is not a valid storage
1119 * device.
1120 */
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001121 public static boolean isExternalStorageEmulated(@NonNull File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001122 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001123 if (volume != null) {
1124 return volume.isEmulated();
1125 } else {
1126 throw new IllegalArgumentException("Failed to find storage device at " + path);
1127 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001128 }
1129
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001130 /**
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001131 * Returns whether the primary shared/external storage media is a legacy
1132 * view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001133 * <p>
1134 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001135 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
1136 * may inherit its legacy state based on when it was first installed.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001137 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001138 * Non-legacy apps can continue to discover and read media belonging to
1139 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001140 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001141 public static boolean isExternalStorageLegacy() {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001142 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001143 return isExternalStorageLegacy(externalDir);
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001144 }
1145
1146 /**
1147 * Returns whether the shared/external storage media at the given path is a
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001148 * legacy view that includes files not owned by the app.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001149 * <p>
1150 * This value may be different from the value requested by
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001151 * {@code requestLegacyExternalStorage} in the app's manifest, since an app
1152 * may inherit its legacy state based on when it was first installed.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001153 * <p>
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001154 * Non-legacy apps can continue to discover and read media belonging to
1155 * other apps via {@link android.provider.MediaStore}.
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001156 *
1157 * @throws IllegalArgumentException if the path is not a valid storage
1158 * device.
1159 */
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001160 public static boolean isExternalStorageLegacy(@NonNull File path) {
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001161 final Context context = AppGlobals.getInitialApplication();
1162 final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
Jeff Sharkeye9fcabc2019-04-04 11:19:22 -06001163
Svet Ganovd563e932019-04-14 13:07:41 -07001164 return appOps.checkOpNoThrow(AppOpsManager.OP_LEGACY_STORAGE,
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001165 context.getApplicationInfo().uid,
Jeff Sharkey8926fc12019-04-21 09:38:42 -06001166 context.getOpPackageName()) == AppOpsManager.MODE_ALLOWED;
Jeff Sharkeybcff13c2019-03-28 14:29:35 -06001167 }
1168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 static File getDirectory(String variableName, String defaultPath) {
1170 String path = System.getenv(variableName);
1171 return path == null ? new File(defaultPath) : new File(path);
1172 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001173
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001174 /** {@hide} */
1175 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001176 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001177 }
1178
1179 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001180 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001181 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1182 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001183 }
1184 }
1185
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001186 /**
1187 * Append path segments to each given base path, returning result.
1188 *
1189 * @hide
1190 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001191 @UnsupportedAppUsage
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001192 public static File[] buildPaths(File[] base, String... segments) {
1193 File[] result = new File[base.length];
1194 for (int i = 0; i < base.length; i++) {
1195 result[i] = buildPath(base[i], segments);
1196 }
1197 return result;
1198 }
1199
1200 /**
1201 * Append path segments to given base path, returning result.
1202 *
1203 * @hide
1204 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001205 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001206 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001207 File cur = base;
1208 for (String segment : segments) {
1209 if (cur == null) {
1210 cur = new File(segment);
1211 } else {
1212 cur = new File(cur, segment);
1213 }
1214 }
1215 return cur;
1216 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001217
1218 /**
1219 * If the given path exists on emulated external storage, return the
1220 * translated backing path hosted on internal storage. This bypasses any
1221 * emulation later, improving performance. This is <em>only</em> suitable
1222 * for read-only access.
1223 * <p>
1224 * Returns original path if given path doesn't meet these criteria. Callers
1225 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1226 * permission.
1227 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001228 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001229 * @hide
1230 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001231 @UnsupportedAppUsage
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001232 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001233 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001234 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236}