blob: b3e35b6237ccd82ae13d74a87e60188585d9ea31 [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;
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080022import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070023import android.content.Context;
Jeff Sharkey48877892015-03-18 11:27:19 -070024import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070025import android.os.storage.StorageVolume;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070026import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080027import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080028
Gilles Debunneee1d6302011-05-13 10:09:32 -070029import java.io.File;
Jeff Sharkey20356142017-12-06 15:22:05 -070030import java.util.LinkedList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032/**
33 * Provides access to environment variables.
34 */
35public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080036 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Jeff Sharkeydd02e332018-06-27 14:41:57 -060038 // NOTE: keep credential-protected paths in sync with StrictMode.java
39
Jeff Sharkeyb049e212012-09-07 23:16:01 -070040 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080041 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070042 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060043 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070044 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080045 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080046 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080047 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070048 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090049 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Dario Freni2bef1762018-06-01 14:02:08 +010050 private static final String ENV_PRODUCT_SERVICES_ROOT = "PRODUCT_SERVICES_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070051
Jeff Sharkeydfa45302012-09-12 16:25:22 -070052 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070053 public static final String DIR_ANDROID = "Android";
Sudheer Shankae93db512019-01-11 16:05:28 -080054 private static final String DIR_SANDBOX = "sandbox";
Jeff Sharkey1abdb712013-08-11 16:28:14 -070055 private static final String DIR_DATA = "data";
56 private static final String DIR_MEDIA = "media";
57 private static final String DIR_OBB = "obb";
58 private static final String DIR_FILES = "files";
59 private static final String DIR_CACHE = "cache";
60
61 /** {@hide} */
62 @Deprecated
63 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070064
Jeff Sharkey63d0a062013-03-01 16:12:55 -080065 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070066 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060067 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070068 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080069 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080070 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080071 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070072 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090073 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
Dario Freni2bef1762018-06-01 14:02:08 +010074 private static final File DIR_PRODUCT_SERVICES_ROOT = getDirectory(ENV_PRODUCT_SERVICES_ROOT,
75 "/product_services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
Andreas Gamped281b422016-07-08 03:50:27 +000077 private static UserEnvironment sCurrentUser;
78 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070079
Andreas Gamped281b422016-07-08 03:50:27 +000080 static {
81 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070082 }
83
84 /** {@hide} */
Andreas Gamped281b422016-07-08 03:50:27 +000085 public static void initForCurrentUser() {
86 final int userId = UserHandle.myUserId();
87 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070088 }
89
90 /** {@hide} */
91 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070092 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070093
94 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070095 mUserId = userId;
96 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070097
Jeff Sharkey48877892015-03-18 11:27:19 -070098 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -070099 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
100 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700101 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -0700102 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700103 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700104 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700105 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700106 }
107
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700108 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700109 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700110 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700111 }
112
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700113 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700115 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700116 }
117
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700118 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700119 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700120 }
121
Sudheer Shankae93db512019-01-11 16:05:28 -0800122 public File[] buildExternalStorageAndroidSandboxDirs() {
123 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_SANDBOX);
124 }
125
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700126 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700127 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700128 }
129
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700130 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700131 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700132 }
133
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700134 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700135 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700136 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700137
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700138 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700139 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700140 }
141
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700142 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700143 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700144 }
145
146 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700147 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700148 }
149
150 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700151 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700152 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700153 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800156 * Return root of the "system" partition holding the core Android OS.
157 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 */
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700159 public static @NonNull File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800160 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 }
162
Jeff Sharkey48877892015-03-18 11:27:19 -0700163 /** {@hide} */
Jeff Sharkeyc6091162018-06-29 17:15:40 -0600164 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700165 public static @NonNull File getStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700166 return DIR_ANDROID_STORAGE;
167 }
168
Jason parksa3cdaa52011-01-13 14:15:43 -0600169 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800170 * Return root directory of the "oem" partition holding OEM customizations,
171 * if any. If present, the partition is mounted read-only.
172 *
173 * @hide
174 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600175 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700176 public static @NonNull File getOemDirectory() {
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800177 return DIR_OEM_ROOT;
178 }
179
180 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800181 * Return root directory of the "odm" partition holding ODM customizations,
182 * if any. If present, the partition is mounted read-only.
183 *
184 * @hide
185 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600186 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700187 public static @NonNull File getOdmDirectory() {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800188 return DIR_ODM_ROOT;
189 }
190
191 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700192 * Return root directory of the "vendor" partition that holds vendor-provided
193 * software that should persist across simple reflashing of the "system" partition.
194 * @hide
195 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600196 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700197 public static @NonNull File getVendorDirectory() {
Christopher Tate740888f2014-04-18 12:24:57 -0700198 return DIR_VENDOR_ROOT;
199 }
200
Jason parksa3cdaa52011-01-13 14:15:43 -0600201 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900202 * Return root directory of the "product" partition holding product-specific
203 * customizations if any. If present, the partition is mounted read-only.
204 *
205 * @hide
206 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600207 @SystemApi
Anton Hansson09e47be2018-11-08 12:56:18 +0000208 @TestApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700209 public static @NonNull File getProductDirectory() {
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900210 return DIR_PRODUCT_ROOT;
211 }
212
213 /**
Dario Freni2bef1762018-06-01 14:02:08 +0100214 * Return root directory of the "product_services" partition holding middleware
215 * services if any. If present, the partition is mounted read-only.
216 *
217 * @hide
218 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -0600219 @SystemApi
Jeff Sharkeya30e5c32019-02-28 12:02:10 -0700220 public static @NonNull File getProductServicesDirectory() {
Dario Freni2bef1762018-06-01 14:02:08 +0100221 return DIR_PRODUCT_SERVICES_ROOT;
222 }
223
224 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700225 * Return the system directory for a user. This is for use by system
226 * services to store files relating to the user. This directory will be
227 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700228 *
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600229 * @deprecated This directory is valid and still exists, but but callers
230 * should <em>strongly</em> consider switching to using either
231 * {@link #getDataSystemCeDirectory(int)} or
232 * {@link #getDataSystemDeDirectory(int)}, both of which support
233 * fast user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700234 * @hide
235 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700236 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700237 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800238 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700239 }
240
241 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700242 * Returns the config directory for a user. This is for use by system
243 * services to store files relating to the user which should be readable by
244 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100245 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700246 * @deprecated This directory is valid and still exists, but callers should
247 * <em>strongly</em> consider switching to
248 * {@link #getDataMiscCeDirectory(int)} which is protected with
249 * user credentials or {@link #getDataMiscDeDirectory(int)}
250 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100251 * @hide
252 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700253 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100254 public static File getUserConfigDirectory(int userId) {
255 return new File(new File(new File(
256 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
257 }
258
259 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700260 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 */
262 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800263 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700266 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700267 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700268 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800269 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700270 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700271 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700272 }
273 }
274
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700275 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600276 public static File getExpandDirectory() {
277 return DIR_ANDROID_EXPAND;
278 }
279
280 /** {@hide} */
Jeff Sharkey15447792015-11-05 16:18:51 -0800281 public static File getDataSystemDirectory() {
282 return new File(getDataDirectory(), "system");
283 }
284
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700285 /**
286 * Returns the base directory for per-user system directory, device encrypted.
287 * {@hide}
288 */
289 public static File getDataSystemDeDirectory() {
290 return buildPath(getDataDirectory(), "system_de");
291 }
292
293 /**
294 * Returns the base directory for per-user system directory, credential encrypted.
295 * {@hide}
296 */
297 public static File getDataSystemCeDirectory() {
298 return buildPath(getDataDirectory(), "system_ce");
299 }
300
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600301 /**
302 * Return the "credential encrypted" system directory for a user. This is
303 * for use by system services to store files relating to the user. This
304 * directory supports fast user wipe, and will be automatically deleted when
305 * the user is removed.
306 * <p>
307 * Data stored under this path is "credential encrypted", which uses an
308 * encryption key that is entangled with user credentials, such as a PIN or
309 * password. The contents will only be available once the user has been
310 * unlocked, as reported by {@code SystemService.onUnlockUser()}.
311 * <p>
312 * New code should <em>strongly</em> prefer storing sensitive data in these
313 * credential encrypted areas.
314 *
315 * @hide
316 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700317 public static File getDataSystemCeDirectory(int userId) {
318 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800319 }
320
Jeff Sharkey3a6a61e2018-10-03 10:45:51 -0600321 /**
322 * Return the "device encrypted" system directory for a user. This is for
323 * use by system services to store files relating to the user. This
324 * directory supports fast user wipe, and will be automatically deleted when
325 * the user is removed.
326 * <p>
327 * Data stored under this path is "device encrypted", which uses an
328 * encryption key that is tied to the physical device. The contents will
329 * only be available once the device has finished a {@code dm-verity}
330 * protected boot.
331 * <p>
332 * New code should <em>strongly</em> avoid storing sensitive data in these
333 * device encrypted areas.
334 *
335 * @hide
336 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700337 public static File getDataSystemDeDirectory(int userId) {
338 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
339 }
340
341 /** {@hide} */
342 public static File getDataMiscDirectory() {
343 return new File(getDataDirectory(), "misc");
344 }
345
346 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800347 public static File getDataMiscCeDirectory() {
348 return buildPath(getDataDirectory(), "misc_ce");
349 }
350
351 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700352 public static File getDataMiscCeDirectory(int userId) {
353 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
354 }
355
356 /** {@hide} */
357 public static File getDataMiscDeDirectory(int userId) {
358 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800359 }
360
Calin Juravled479b522016-02-24 16:22:03 +0000361 private static File getDataProfilesDeDirectory(int userId) {
362 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
363 }
364
365 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800366 public static File getDataVendorCeDirectory(int userId) {
367 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
368 }
369
370 /** {@hide} */
371 public static File getDataVendorDeDirectory(int userId) {
372 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
373 }
374
375 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800376 public static File getDataRefProfilesDePackageDirectory(String packageName) {
377 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100378 }
379
380 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000381 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
382 return buildPath(getDataProfilesDeDirectory(userId), packageName);
383 }
384
385 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700386 public static File getDataAppDirectory(String volumeUuid) {
387 return new File(getDataDirectory(volumeUuid), "app");
388 }
389
390 /** {@hide} */
Dario Frenia8f4b132018-12-30 00:36:49 +0000391 public static File getDataStagingDirectory(String volumeUuid) {
Gavin Corkery5ae38562019-02-18 14:11:57 +0000392 return new File(getDataDirectory(volumeUuid), "pkg_staging");
Dario Frenia8f4b132018-12-30 00:36:49 +0000393 }
394
395 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700396 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700397 return new File(getDataDirectory(volumeUuid), "user");
398 }
399
400 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700401 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
402 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700403 }
404
405 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700406 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700407 String packageName) {
408 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700409 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800410 }
411
412 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700413 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800414 return new File(getDataDirectory(volumeUuid), "user_de");
415 }
416
417 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700418 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
419 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800420 }
421
422 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700423 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800424 String packageName) {
425 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700426 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700427 }
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700430 * Return preloads directory.
431 * <p>This directory may contain pre-loaded content such as
432 * {@link #getDataPreloadsDemoDirectory() demo videos} and
433 * {@link #getDataPreloadsAppsDirectory() APK files} .
434 * {@hide}
435 */
436 public static File getDataPreloadsDirectory() {
437 return new File(getDataDirectory(), "preloads");
438 }
439
440 /**
441 * @see #getDataPreloadsDirectory()
442 * {@hide}
443 */
444 public static File getDataPreloadsDemoDirectory() {
445 return new File(getDataPreloadsDirectory(), "demo");
446 }
447
448 /**
449 * @see #getDataPreloadsDirectory()
450 * {@hide}
451 */
452 public static File getDataPreloadsAppsDirectory() {
453 return new File(getDataPreloadsDirectory(), "apps");
454 }
455
456 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700457 * @see #getDataPreloadsDirectory()
458 * {@hide}
459 */
460 public static File getDataPreloadsMediaDirectory() {
461 return new File(getDataPreloadsDirectory(), "media");
462 }
463
464 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700465 * Returns location of preloaded cache directory for package name
466 * @see #getDataPreloadsDirectory()
467 * {@hide}
468 */
469 public static File getDataPreloadsFileCacheDirectory(String packageName) {
470 return new File(getDataPreloadsFileCacheDirectory(), packageName);
471 }
472
473 /**
474 * Returns location of preloaded cache directory.
475 * @see #getDataPreloadsDirectory()
476 * {@hide}
477 */
478 public static File getDataPreloadsFileCacheDirectory() {
479 return new File(getDataPreloadsDirectory(), "file_cache");
480 }
481
482 /**
Sudheer Shankabe0febe2018-11-07 18:24:37 -0800483 * Returns location of packages cache directory.
484 * {@hide}
485 */
486 public static File getPackageCacheDirectory() {
487 return new File(getDataSystemDirectory(), "package_cache");
488 }
489
490 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700491 * Return the primary shared/external storage directory. This directory may
492 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800493 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700494 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800495 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700496 * <p>
497 * <em>Note: don't be confused by the word "external" here. This directory
498 * can better be thought as media/shared storage. It is a filesystem that
499 * can hold a relatively large amount of data and that is shared across all
500 * applications (does not enforce permissions). Traditionally this is an SD
501 * card, but it may also be implemented as built-in storage in a device that
502 * is distinct from the protected internal storage and can be mounted as a
503 * filesystem on a computer.</em>
504 * <p>
505 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700506 * each user has their own isolated shared storage. Applications only have
507 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700508 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700509 * In devices with multiple shared/external storage directories, this
510 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700511 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700512 * {@link Context#getExternalFilesDirs(String)},
513 * {@link Context#getExternalCacheDirs()}, and
514 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700515 * <p>
516 * Applications should not directly use this top-level directory, in order
517 * to avoid polluting the user's root namespace. Any files that are private
518 * to the application should be placed in a directory returned by
519 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700520 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700521 * if the application is uninstalled. Other shared files should be placed in
522 * one of the directories returned by
523 * {@link #getExternalStoragePublicDirectory}.
524 * <p>
525 * Writing to this path requires the
526 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800527 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700528 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700529 * which is automatically granted if you hold the write permission.
530 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700531 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700532 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700533 * {@link Context#getExternalFilesDir(String)},
534 * {@link Context#getExternalCacheDir()}, or
535 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700536 * read or write.
537 * <p>
538 * This path may change between platform versions, so applications should
539 * only persist relative paths.
540 * <p>
541 * Here is an example of typical code to monitor the state of external
542 * storage:
543 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700544 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800545 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700546 *
547 * @see #getExternalStorageState()
548 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 */
550 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700551 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000552 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700553 }
554
555 /** {@hide} */
556 public static File getLegacyExternalStorageDirectory() {
557 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700560 /** {@hide} */
561 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700562 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700563 }
564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800566 * Standard directory in which to place any audio files that should be
567 * in the regular list of music for the user.
568 * This may be combined with
569 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
570 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
571 * of directories to categories a particular audio file as more than one
572 * type.
573 */
574 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800575
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800576 /**
577 * Standard directory in which to place any audio files that should be
578 * in the list of podcasts that the user can select (not as regular
579 * music).
580 * This may be combined with {@link #DIRECTORY_MUSIC},
581 * {@link #DIRECTORY_NOTIFICATIONS},
582 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
583 * of directories to categories a particular audio file as more than one
584 * type.
585 */
586 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800587
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800588 /**
589 * Standard directory in which to place any audio files that should be
590 * in the list of ringtones that the user can select (not as regular
591 * music).
592 * This may be combined with {@link #DIRECTORY_MUSIC},
593 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
594 * {@link #DIRECTORY_ALARMS} as a series
595 * of directories to categories a particular audio file as more than one
596 * type.
597 */
598 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800599
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800600 /**
601 * Standard directory in which to place any audio files that should be
602 * in the list of alarms that the user can select (not as regular
603 * music).
604 * This may be combined with {@link #DIRECTORY_MUSIC},
605 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
606 * and {@link #DIRECTORY_RINGTONES} as a series
607 * of directories to categories a particular audio file as more than one
608 * type.
609 */
610 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800611
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800612 /**
613 * Standard directory in which to place any audio files that should be
614 * in the list of notifications that the user can select (not as regular
615 * music).
616 * This may be combined with {@link #DIRECTORY_MUSIC},
617 * {@link #DIRECTORY_PODCASTS},
618 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
619 * of directories to categories a particular audio file as more than one
620 * type.
621 */
622 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800623
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800624 /**
625 * Standard directory in which to place pictures that are available to
626 * the user. Note that this is primarily a convention for the top-level
627 * public directory, as the media scanner will find and collect pictures
628 * in any directory.
629 */
630 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800631
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800632 /**
633 * Standard directory in which to place movies that are available to
634 * the user. Note that this is primarily a convention for the top-level
635 * public directory, as the media scanner will find and collect movies
636 * in any directory.
637 */
638 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800639
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800640 /**
641 * Standard directory in which to place files that have been downloaded by
642 * the user. Note that this is primarily a convention for the top-level
643 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700644 * private directories. Also note that though the constant here is
645 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
646 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800647 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700648 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800649
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800650 /**
651 * The traditional location for pictures and videos when mounting the
652 * device as a camera. Note that this is primarily a convention for the
653 * top-level public directory, as this convention makes no sense elsewhere.
654 */
655 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700656
657 /**
658 * Standard directory in which to place documents that have been created by
659 * the user.
660 */
661 public static String DIRECTORY_DOCUMENTS = "Documents";
662
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800663 /**
Jeff Sharkeyc8e49242018-11-02 14:34:44 -0600664 * Standard directory in which to place screenshots that have been taken by
665 * the user. Typically used as a secondary directory under
666 * {@link #DIRECTORY_PICTURES}.
667 */
668 public static String DIRECTORY_SCREENSHOTS = "Screenshots";
669
670 /**
Jeff Sharkey10887d52018-11-30 13:44:41 -0700671 * Standard directory in which to place any audio files which are
672 * audiobooks.
673 */
674 public static String DIRECTORY_AUDIOBOOKS = "Audiobooks";
675
676 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800677 * List of standard storage directories.
678 * <p>
679 * Each of its values have its own constant:
680 * <ul>
681 * <li>{@link #DIRECTORY_MUSIC}
682 * <li>{@link #DIRECTORY_PODCASTS}
683 * <li>{@link #DIRECTORY_ALARMS}
684 * <li>{@link #DIRECTORY_RINGTONES}
685 * <li>{@link #DIRECTORY_NOTIFICATIONS}
686 * <li>{@link #DIRECTORY_PICTURES}
687 * <li>{@link #DIRECTORY_MOVIES}
688 * <li>{@link #DIRECTORY_DOWNLOADS}
689 * <li>{@link #DIRECTORY_DCIM}
690 * <li>{@link #DIRECTORY_DOCUMENTS}
Jeff Sharkey10887d52018-11-30 13:44:41 -0700691 * <li>{@link #DIRECTORY_AUDIOBOOKS}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800692 * </ul>
693 * @hide
694 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800695 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800696 DIRECTORY_MUSIC,
697 DIRECTORY_PODCASTS,
698 DIRECTORY_RINGTONES,
699 DIRECTORY_ALARMS,
700 DIRECTORY_NOTIFICATIONS,
701 DIRECTORY_PICTURES,
702 DIRECTORY_MOVIES,
703 DIRECTORY_DOWNLOADS,
704 DIRECTORY_DCIM,
Jeff Sharkey10887d52018-11-30 13:44:41 -0700705 DIRECTORY_DOCUMENTS,
706 DIRECTORY_AUDIOBOOKS,
Felipe Lemec7b1f892016-01-15 15:02:31 -0800707 };
708
709 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800710 * @hide
711 */
712 public static boolean isStandardDirectory(String dir) {
713 for (String valid : STANDARD_DIRECTORIES) {
714 if (valid.equals(dir)) {
715 return true;
716 }
717 }
718 return false;
719 }
720
Jeff Sharkey20356142017-12-06 15:22:05 -0700721 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
722 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
723 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
724 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
725 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
726 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
727 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
728 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
729 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
730 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700731 /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10;
Jeff Sharkey20356142017-12-06 15:22:05 -0700732
733 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
734 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
735
736 /**
737 * Classify the content types present on the given external storage device.
738 * <p>
739 * This is typically useful for deciding if an inserted SD card is empty, or
740 * if it contains content like photos that should be preserved.
741 *
742 * @hide
743 */
744 public static int classifyExternalStorageDirectory(File dir) {
745 int res = 0;
746 for (File f : FileUtils.listFilesOrEmpty(dir)) {
747 if (f.isFile() && isInterestingFile(f)) {
748 res |= HAS_OTHER;
749 } else if (f.isDirectory() && hasInterestingFiles(f)) {
750 final String name = f.getName();
751 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
752 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
753 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
754 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
755 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
756 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
757 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
758 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
759 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
760 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
Jeff Sharkey10887d52018-11-30 13:44:41 -0700761 else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS;
Jeff Sharkey20356142017-12-06 15:22:05 -0700762 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
763 else res |= HAS_OTHER;
764 }
765 }
766 return res;
767 }
768
769 private static boolean hasInterestingFiles(File dir) {
770 final LinkedList<File> explore = new LinkedList<>();
771 explore.add(dir);
772 while (!explore.isEmpty()) {
773 dir = explore.pop();
774 for (File f : FileUtils.listFilesOrEmpty(dir)) {
775 if (isInterestingFile(f)) return true;
776 if (f.isDirectory()) explore.add(f);
777 }
778 }
779 return false;
780 }
781
782 private static boolean isInterestingFile(File file) {
783 if (file.isFile()) {
784 final String name = file.getName().toLowerCase();
785 if (name.endsWith(".exe") || name.equals("autorun.inf")
786 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
787 return false;
788 } else {
789 return true;
790 }
791 } else {
792 return false;
793 }
794 }
795
Felipe Lemeb012f912016-01-22 16:49:55 -0800796 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700797 * Get a top-level shared/external storage directory for placing files of a
798 * particular type. This is where the user will typically place and manage
799 * their own files, so you should be careful about what you put here to
800 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800801 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700802 * <p>
803 * On devices with multiple users (as described by {@link UserManager}),
804 * each user has their own isolated shared storage. Applications only have
805 * access to the shared storage for the user they're running as.
806 * </p>
807 * <p>
808 * Here is an example of typical code to manipulate a picture on the public
809 * shared storage:
810 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800811 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
812 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800813 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700814 * @param type The type of storage directory to return. Should be one of
815 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
816 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
817 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800818 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
819 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700820 * @return Returns the File path for the directory. Note that this directory
821 * may not yet exist, so you must make sure it exists before using
822 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800823 */
824 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700825 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000826 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800827 }
828
829 /**
830 * Returns the path for android-specific data on the SD card.
831 * @hide
832 */
Sudheer Shankae93db512019-01-11 16:05:28 -0800833 public static File[] buildExternalStorageAndroidSandboxDirs() {
834 throwIfUserRequired();
835 return sCurrentUser.buildExternalStorageAndroidSandboxDirs();
836 }
837
838 /**
839 * Returns the path for android-specific data on the SD card.
840 * @hide
841 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700842 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700843 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000844 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800845 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700846
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800847 /**
848 * Generates the raw path to an application's data
849 * @hide
850 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700851 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700852 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000853 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800854 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800855
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800856 /**
857 * Generates the raw path to an application's media
858 * @hide
859 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700860 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700861 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000862 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800863 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800864
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800865 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800866 * Generates the raw path to an application's OBB files
867 * @hide
868 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700869 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700870 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000871 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800872 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800873
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800874 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800875 * Generates the path to an application's files.
876 * @hide
877 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700878 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700879 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000880 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800881 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700882
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800883 /**
884 * Generates the path to an application's cache.
885 * @hide
886 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700887 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700888 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000889 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800890 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800891
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800892 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700893 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 */
895 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800896 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898
899 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700900 * Unknown storage state, such as when a path isn't backed by known storage
901 * media.
902 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800903 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700904 */
905 public static final String MEDIA_UNKNOWN = "unknown";
906
907 /**
908 * Storage state if the media is not present.
909 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800910 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 */
912 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700915 * Storage state if the media is present but not mounted.
916 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800917 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 */
919 public static final String MEDIA_UNMOUNTED = "unmounted";
920
921 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700922 * Storage state if the media is present and being disk-checked.
923 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800924 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 */
926 public static final String MEDIA_CHECKING = "checking";
927
928 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700929 * Storage state if the media is present but is blank or is using an
930 * unsupported filesystem.
931 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800932 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 */
934 public static final String MEDIA_NOFS = "nofs";
935
936 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700937 * Storage state if the media is present and mounted at its mount point with
938 * read/write access.
939 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800940 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 */
942 public static final String MEDIA_MOUNTED = "mounted";
943
944 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700945 * Storage state if the media is present and mounted at its mount point with
946 * read-only access.
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_MOUNTED_READ_ONLY = "mounted_ro";
951
952 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700953 * Storage state if the media is present not mounted, and shared via USB
954 * mass storage.
955 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800956 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 */
958 public static final String MEDIA_SHARED = "shared";
959
960 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700961 * Storage state if the media was removed before it was unmounted.
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_BAD_REMOVAL = "bad_removal";
966
967 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700968 * Storage state if the media is present but cannot be mounted. Typically
969 * this happens if the file system on the media is corrupted.
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_UNMOUNTABLE = "unmountable";
974
975 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700976 * Storage state if the media is in the process of being ejected.
977 *
978 * @see #getExternalStorageState(File)
979 */
980 public static final String MEDIA_EJECTING = "ejecting";
981
982 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700983 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800984 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700985 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700986 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
987 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
988 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
989 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
990 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 */
992 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +0000993 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800994 return getExternalStorageState(externalDir);
995 }
996
997 /**
998 * @deprecated use {@link #getExternalStorageState(File)}
999 */
1000 @Deprecated
1001 public static String getStorageState(File path) {
1002 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001003 }
1004
1005 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001006 * Returns the current state of the shared/external storage media at the
1007 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001008 *
1009 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
1010 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
1011 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
1012 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
1013 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
1014 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001015 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001016 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001017 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001018 return volume.getState();
1019 } else {
1020 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023
Dianne Hackborn407f6252010-10-04 11:31:17 -07001024 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001025 * Returns whether the primary shared/external storage media is physically
1026 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001027 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001028 * @return true if the storage device can be removed (such as an SD card),
1029 * or false if the storage device is built in and cannot be
1030 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -07001031 */
1032 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +00001033 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001034 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -07001035 }
1036
Kenny Roote1ff2142010-10-12 11:20:01 -07001037 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001038 * Returns whether the shared/external storage media at the given path is
1039 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -08001040 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001041 * @return true if the storage device can be removed (such as an SD card),
1042 * or false if the storage device is built in and cannot be
1043 * physically removed.
1044 * @throws IllegalArgumentException if the path is not a valid storage
1045 * device.
1046 */
1047 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001048 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001049 if (volume != null) {
1050 return volume.isRemovable();
1051 } else {
1052 throw new IllegalArgumentException("Failed to find storage device at " + path);
1053 }
1054 }
1055
1056 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001057 * Returns whether the primary shared/external storage media is emulated.
1058 * <p>
1059 * The contents of emulated storage devices are backed by a private user
1060 * data partition, which means there is little benefit to apps storing data
1061 * here instead of the private directories returned by
1062 * {@link Context#getFilesDir()}, etc.
1063 * <p>
1064 * This returns true when emulated storage is backed by either internal
1065 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -08001066 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001067 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
1068 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -07001069 */
1070 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +00001071 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001072 return isExternalStorageEmulated(externalDir);
1073 }
1074
1075 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -07001076 * Returns whether the shared/external storage media at the given path is
1077 * emulated.
1078 * <p>
1079 * The contents of emulated storage devices are backed by a private user
1080 * data partition, which means there is little benefit to apps storing data
1081 * here instead of the private directories returned by
1082 * {@link Context#getFilesDir()}, etc.
1083 * <p>
1084 * This returns true when emulated storage is backed by either internal
1085 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001086 *
1087 * @throws IllegalArgumentException if the path is not a valid storage
1088 * device.
1089 */
1090 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -07001091 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001092 if (volume != null) {
1093 return volume.isEmulated();
1094 } else {
1095 throw new IllegalArgumentException("Failed to find storage device at " + path);
1096 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001097 }
1098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 static File getDirectory(String variableName, String defaultPath) {
1100 String path = System.getenv(variableName);
1101 return path == null ? new File(defaultPath) : new File(path);
1102 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001103
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001104 /** {@hide} */
1105 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001106 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001107 }
1108
1109 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001110 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001111 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1112 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001113 }
1114 }
1115
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001116 /**
1117 * Append path segments to each given base path, returning result.
1118 *
1119 * @hide
1120 */
1121 public static File[] buildPaths(File[] base, String... segments) {
1122 File[] result = new File[base.length];
1123 for (int i = 0; i < base.length; i++) {
1124 result[i] = buildPath(base[i], segments);
1125 }
1126 return result;
1127 }
1128
1129 /**
1130 * Append path segments to given base path, returning result.
1131 *
1132 * @hide
1133 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001134 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001135 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001136 File cur = base;
1137 for (String segment : segments) {
1138 if (cur == null) {
1139 cur = new File(segment);
1140 } else {
1141 cur = new File(cur, segment);
1142 }
1143 }
1144 return cur;
1145 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001146
1147 /**
1148 * If the given path exists on emulated external storage, return the
1149 * translated backing path hosted on internal storage. This bypasses any
1150 * emulation later, improving performance. This is <em>only</em> suitable
1151 * for read-only access.
1152 * <p>
1153 * Returns original path if given path doesn't meet these criteria. Callers
1154 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1155 * permission.
1156 *
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001157 * @deprecated disabled now that FUSE has been replaced by sdcardfs
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001158 * @hide
1159 */
Jeff Sharkey70eb34a2018-09-13 11:57:03 -06001160 @Deprecated
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001161 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001162 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164}