blob: 42c637a83e5d2d8c1feec3cef57e95c789dcc369 [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
Philip P. Moltmannf80809f2018-04-04 11:20:44 -070019import android.annotation.TestApi;
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080020import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070021import android.content.Context;
Jeff Sharkey48877892015-03-18 11:27:19 -070022import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070023import android.os.storage.StorageVolume;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070024import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080025import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080026
Gilles Debunneee1d6302011-05-13 10:09:32 -070027import java.io.File;
Jeff Sharkey20356142017-12-06 15:22:05 -070028import java.util.LinkedList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030/**
31 * Provides access to environment variables.
32 */
33public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080034 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
Jeff Sharkeydd02e332018-06-27 14:41:57 -060036 // NOTE: keep credential-protected paths in sync with StrictMode.java
37
Jeff Sharkeyb049e212012-09-07 23:16:01 -070038 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080039 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070040 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060041 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070042 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080043 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080044 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080045 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070046 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090047 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070048
Jeff Sharkeydfa45302012-09-12 16:25:22 -070049 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070050 public static final String DIR_ANDROID = "Android";
51 private static final String DIR_DATA = "data";
52 private static final String DIR_MEDIA = "media";
53 private static final String DIR_OBB = "obb";
54 private static final String DIR_FILES = "files";
55 private static final String DIR_CACHE = "cache";
56
57 /** {@hide} */
58 @Deprecated
59 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070060
Jeff Sharkey63d0a062013-03-01 16:12:55 -080061 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070062 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060063 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070064 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080065 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080066 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080067 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070068 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090069 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Andreas Gamped281b422016-07-08 03:50:27 +000071 private static UserEnvironment sCurrentUser;
72 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070073
Andreas Gamped281b422016-07-08 03:50:27 +000074 static {
75 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070076 }
77
78 /** {@hide} */
Andreas Gamped281b422016-07-08 03:50:27 +000079 public static void initForCurrentUser() {
80 final int userId = UserHandle.myUserId();
81 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070082 }
83
84 /** {@hide} */
85 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070086 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070087
88 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070089 mUserId = userId;
90 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070091
Jeff Sharkey48877892015-03-18 11:27:19 -070092 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -070093 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
94 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070095 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -070096 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070097 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070098 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070099 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700100 }
101
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700102 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700103 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700104 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700105 }
106
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700107 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700108 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700109 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700110 }
111
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700112 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700113 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114 }
115
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700116 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700117 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700118 }
119
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700120 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700121 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700122 }
123
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700124 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700125 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700126 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700127
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700128 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700129 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700130 }
131
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700132 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700133 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700134 }
135
136 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700137 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700138 }
139
140 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700141 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700142 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700143 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800146 * Return root of the "system" partition holding the core Android OS.
147 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 */
149 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800150 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 }
152
Jeff Sharkey48877892015-03-18 11:27:19 -0700153 /** {@hide} */
154 public static File getStorageDirectory() {
155 return DIR_ANDROID_STORAGE;
156 }
157
Jason parksa3cdaa52011-01-13 14:15:43 -0600158 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800159 * Return root directory of the "oem" partition holding OEM customizations,
160 * if any. If present, the partition is mounted read-only.
161 *
162 * @hide
163 */
164 public static File getOemDirectory() {
165 return DIR_OEM_ROOT;
166 }
167
168 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800169 * Return root directory of the "odm" partition holding ODM customizations,
170 * if any. If present, the partition is mounted read-only.
171 *
172 * @hide
173 */
174 public static File getOdmDirectory() {
175 return DIR_ODM_ROOT;
176 }
177
178 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700179 * Return root directory of the "vendor" partition that holds vendor-provided
180 * software that should persist across simple reflashing of the "system" partition.
181 * @hide
182 */
183 public static File getVendorDirectory() {
184 return DIR_VENDOR_ROOT;
185 }
186
Jason parksa3cdaa52011-01-13 14:15:43 -0600187 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900188 * Return root directory of the "product" partition holding product-specific
189 * customizations if any. If present, the partition is mounted read-only.
190 *
191 * @hide
192 */
193 public static File getProductDirectory() {
194 return DIR_PRODUCT_ROOT;
195 }
196
197 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700198 * Return the system directory for a user. This is for use by system
199 * services to store files relating to the user. This directory will be
200 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700201 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700202 * @deprecated This directory is valid and still exists, but callers should
203 * <em>strongly</em> consider switching to
204 * {@link #getDataSystemCeDirectory(int)} which is protected
205 * with user credentials or
206 * {@link #getDataSystemDeDirectory(int)} which supports fast
207 * user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700208 * @hide
209 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700210 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700211 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800212 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700213 }
214
215 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700216 * Returns the config directory for a user. This is for use by system
217 * services to store files relating to the user which should be readable by
218 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100219 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700220 * @deprecated This directory is valid and still exists, but callers should
221 * <em>strongly</em> consider switching to
222 * {@link #getDataMiscCeDirectory(int)} which is protected with
223 * user credentials or {@link #getDataMiscDeDirectory(int)}
224 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100225 * @hide
226 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700227 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100228 public static File getUserConfigDirectory(int userId) {
229 return new File(new File(new File(
230 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
231 }
232
233 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700234 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 */
236 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800237 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
239
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700240 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700241 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700242 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800243 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700244 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700245 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700246 }
247 }
248
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700249 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600250 public static File getExpandDirectory() {
251 return DIR_ANDROID_EXPAND;
252 }
253
254 /** {@hide} */
Jeff Sharkey15447792015-11-05 16:18:51 -0800255 public static File getDataSystemDirectory() {
256 return new File(getDataDirectory(), "system");
257 }
258
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700259 /**
260 * Returns the base directory for per-user system directory, device encrypted.
261 * {@hide}
262 */
263 public static File getDataSystemDeDirectory() {
264 return buildPath(getDataDirectory(), "system_de");
265 }
266
267 /**
268 * Returns the base directory for per-user system directory, credential encrypted.
269 * {@hide}
270 */
271 public static File getDataSystemCeDirectory() {
272 return buildPath(getDataDirectory(), "system_ce");
273 }
274
Jeff Sharkey15447792015-11-05 16:18:51 -0800275 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700276 public static File getDataSystemCeDirectory(int userId) {
277 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800278 }
279
280 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700281 public static File getDataSystemDeDirectory(int userId) {
282 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
283 }
284
285 /** {@hide} */
286 public static File getDataMiscDirectory() {
287 return new File(getDataDirectory(), "misc");
288 }
289
290 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800291 public static File getDataMiscCeDirectory() {
292 return buildPath(getDataDirectory(), "misc_ce");
293 }
294
295 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700296 public static File getDataMiscCeDirectory(int userId) {
297 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
298 }
299
300 /** {@hide} */
301 public static File getDataMiscDeDirectory(int userId) {
302 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800303 }
304
Calin Juravled479b522016-02-24 16:22:03 +0000305 private static File getDataProfilesDeDirectory(int userId) {
306 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
307 }
308
309 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800310 public static File getDataVendorCeDirectory(int userId) {
311 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
312 }
313
314 /** {@hide} */
315 public static File getDataVendorDeDirectory(int userId) {
316 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
317 }
318
319 /** {@hide} */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800320 public static File getDataRefProfilesDePackageDirectory(String packageName) {
321 return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName);
Calin Juravle0bd77622016-07-12 15:56:41 +0100322 }
323
324 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000325 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
326 return buildPath(getDataProfilesDeDirectory(userId), packageName);
327 }
328
329 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700330 public static File getDataAppDirectory(String volumeUuid) {
331 return new File(getDataDirectory(volumeUuid), "app");
332 }
333
334 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700335 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700336 return new File(getDataDirectory(volumeUuid), "user");
337 }
338
339 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700340 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
341 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700342 }
343
344 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700345 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700346 String packageName) {
347 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700348 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800349 }
350
351 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700352 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800353 return new File(getDataDirectory(volumeUuid), "user_de");
354 }
355
356 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700357 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
358 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800359 }
360
361 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700362 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800363 String packageName) {
364 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700365 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700366 }
367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700369 * Return preloads directory.
370 * <p>This directory may contain pre-loaded content such as
371 * {@link #getDataPreloadsDemoDirectory() demo videos} and
372 * {@link #getDataPreloadsAppsDirectory() APK files} .
373 * {@hide}
374 */
375 public static File getDataPreloadsDirectory() {
376 return new File(getDataDirectory(), "preloads");
377 }
378
379 /**
380 * @see #getDataPreloadsDirectory()
381 * {@hide}
382 */
383 public static File getDataPreloadsDemoDirectory() {
384 return new File(getDataPreloadsDirectory(), "demo");
385 }
386
387 /**
388 * @see #getDataPreloadsDirectory()
389 * {@hide}
390 */
391 public static File getDataPreloadsAppsDirectory() {
392 return new File(getDataPreloadsDirectory(), "apps");
393 }
394
395 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700396 * @see #getDataPreloadsDirectory()
397 * {@hide}
398 */
399 public static File getDataPreloadsMediaDirectory() {
400 return new File(getDataPreloadsDirectory(), "media");
401 }
402
403 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700404 * Returns location of preloaded cache directory for package name
405 * @see #getDataPreloadsDirectory()
406 * {@hide}
407 */
408 public static File getDataPreloadsFileCacheDirectory(String packageName) {
409 return new File(getDataPreloadsFileCacheDirectory(), packageName);
410 }
411
412 /**
413 * Returns location of preloaded cache directory.
414 * @see #getDataPreloadsDirectory()
415 * {@hide}
416 */
417 public static File getDataPreloadsFileCacheDirectory() {
418 return new File(getDataPreloadsDirectory(), "file_cache");
419 }
420
421 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700422 * Return the primary shared/external storage directory. This directory may
423 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800424 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700425 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800426 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700427 * <p>
428 * <em>Note: don't be confused by the word "external" here. This directory
429 * can better be thought as media/shared storage. It is a filesystem that
430 * can hold a relatively large amount of data and that is shared across all
431 * applications (does not enforce permissions). Traditionally this is an SD
432 * card, but it may also be implemented as built-in storage in a device that
433 * is distinct from the protected internal storage and can be mounted as a
434 * filesystem on a computer.</em>
435 * <p>
436 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700437 * each user has their own isolated shared storage. Applications only have
438 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700439 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700440 * In devices with multiple shared/external storage directories, this
441 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700442 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700443 * {@link Context#getExternalFilesDirs(String)},
444 * {@link Context#getExternalCacheDirs()}, and
445 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700446 * <p>
447 * Applications should not directly use this top-level directory, in order
448 * to avoid polluting the user's root namespace. Any files that are private
449 * to the application should be placed in a directory returned by
450 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700451 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700452 * if the application is uninstalled. Other shared files should be placed in
453 * one of the directories returned by
454 * {@link #getExternalStoragePublicDirectory}.
455 * <p>
456 * Writing to this path requires the
457 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800458 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700459 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700460 * which is automatically granted if you hold the write permission.
461 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700462 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700463 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700464 * {@link Context#getExternalFilesDir(String)},
465 * {@link Context#getExternalCacheDir()}, or
466 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700467 * read or write.
468 * <p>
469 * This path may change between platform versions, so applications should
470 * only persist relative paths.
471 * <p>
472 * Here is an example of typical code to monitor the state of external
473 * storage:
474 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700475 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800476 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700477 *
478 * @see #getExternalStorageState()
479 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 */
481 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700482 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000483 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700484 }
485
486 /** {@hide} */
487 public static File getLegacyExternalStorageDirectory() {
488 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700491 /** {@hide} */
492 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700493 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700494 }
495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800497 * Standard directory in which to place any audio files that should be
498 * in the regular list of music for the user.
499 * This may be combined with
500 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
501 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
502 * of directories to categories a particular audio file as more than one
503 * type.
504 */
505 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800506
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800507 /**
508 * Standard directory in which to place any audio files that should be
509 * in the list of podcasts that the user can select (not as regular
510 * music).
511 * This may be combined with {@link #DIRECTORY_MUSIC},
512 * {@link #DIRECTORY_NOTIFICATIONS},
513 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
514 * of directories to categories a particular audio file as more than one
515 * type.
516 */
517 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800518
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800519 /**
520 * Standard directory in which to place any audio files that should be
521 * in the list of ringtones that the user can select (not as regular
522 * music).
523 * This may be combined with {@link #DIRECTORY_MUSIC},
524 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
525 * {@link #DIRECTORY_ALARMS} as a series
526 * of directories to categories a particular audio file as more than one
527 * type.
528 */
529 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800530
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800531 /**
532 * Standard directory in which to place any audio files that should be
533 * in the list of alarms that the user can select (not as regular
534 * music).
535 * This may be combined with {@link #DIRECTORY_MUSIC},
536 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
537 * and {@link #DIRECTORY_RINGTONES} as a series
538 * of directories to categories a particular audio file as more than one
539 * type.
540 */
541 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800542
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800543 /**
544 * Standard directory in which to place any audio files that should be
545 * in the list of notifications that the user can select (not as regular
546 * music).
547 * This may be combined with {@link #DIRECTORY_MUSIC},
548 * {@link #DIRECTORY_PODCASTS},
549 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
550 * of directories to categories a particular audio file as more than one
551 * type.
552 */
553 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800554
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800555 /**
556 * Standard directory in which to place pictures that are available to
557 * the user. Note that this is primarily a convention for the top-level
558 * public directory, as the media scanner will find and collect pictures
559 * in any directory.
560 */
561 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800562
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800563 /**
564 * Standard directory in which to place movies that are available to
565 * the user. Note that this is primarily a convention for the top-level
566 * public directory, as the media scanner will find and collect movies
567 * in any directory.
568 */
569 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800570
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800571 /**
572 * Standard directory in which to place files that have been downloaded by
573 * the user. Note that this is primarily a convention for the top-level
574 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700575 * private directories. Also note that though the constant here is
576 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
577 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800578 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700579 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800580
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800581 /**
582 * The traditional location for pictures and videos when mounting the
583 * device as a camera. Note that this is primarily a convention for the
584 * top-level public directory, as this convention makes no sense elsewhere.
585 */
586 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700587
588 /**
589 * Standard directory in which to place documents that have been created by
590 * the user.
591 */
592 public static String DIRECTORY_DOCUMENTS = "Documents";
593
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800594 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800595 * List of standard storage directories.
596 * <p>
597 * Each of its values have its own constant:
598 * <ul>
599 * <li>{@link #DIRECTORY_MUSIC}
600 * <li>{@link #DIRECTORY_PODCASTS}
601 * <li>{@link #DIRECTORY_ALARMS}
602 * <li>{@link #DIRECTORY_RINGTONES}
603 * <li>{@link #DIRECTORY_NOTIFICATIONS}
604 * <li>{@link #DIRECTORY_PICTURES}
605 * <li>{@link #DIRECTORY_MOVIES}
606 * <li>{@link #DIRECTORY_DOWNLOADS}
607 * <li>{@link #DIRECTORY_DCIM}
608 * <li>{@link #DIRECTORY_DOCUMENTS}
609 * </ul>
610 * @hide
611 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800612 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800613 DIRECTORY_MUSIC,
614 DIRECTORY_PODCASTS,
615 DIRECTORY_RINGTONES,
616 DIRECTORY_ALARMS,
617 DIRECTORY_NOTIFICATIONS,
618 DIRECTORY_PICTURES,
619 DIRECTORY_MOVIES,
620 DIRECTORY_DOWNLOADS,
621 DIRECTORY_DCIM,
Steve McKayab3b8932016-02-16 11:37:03 -0800622 DIRECTORY_DOCUMENTS
Felipe Lemec7b1f892016-01-15 15:02:31 -0800623 };
624
625 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800626 * @hide
627 */
628 public static boolean isStandardDirectory(String dir) {
629 for (String valid : STANDARD_DIRECTORIES) {
630 if (valid.equals(dir)) {
631 return true;
632 }
633 }
634 return false;
635 }
636
Jeff Sharkey20356142017-12-06 15:22:05 -0700637 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
638 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
639 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
640 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
641 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
642 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
643 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
644 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
645 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
646 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
647
648 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
649 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
650
651 /**
652 * Classify the content types present on the given external storage device.
653 * <p>
654 * This is typically useful for deciding if an inserted SD card is empty, or
655 * if it contains content like photos that should be preserved.
656 *
657 * @hide
658 */
659 public static int classifyExternalStorageDirectory(File dir) {
660 int res = 0;
661 for (File f : FileUtils.listFilesOrEmpty(dir)) {
662 if (f.isFile() && isInterestingFile(f)) {
663 res |= HAS_OTHER;
664 } else if (f.isDirectory() && hasInterestingFiles(f)) {
665 final String name = f.getName();
666 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
667 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
668 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
669 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
670 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
671 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
672 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
673 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
674 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
675 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
676 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
677 else res |= HAS_OTHER;
678 }
679 }
680 return res;
681 }
682
683 private static boolean hasInterestingFiles(File dir) {
684 final LinkedList<File> explore = new LinkedList<>();
685 explore.add(dir);
686 while (!explore.isEmpty()) {
687 dir = explore.pop();
688 for (File f : FileUtils.listFilesOrEmpty(dir)) {
689 if (isInterestingFile(f)) return true;
690 if (f.isDirectory()) explore.add(f);
691 }
692 }
693 return false;
694 }
695
696 private static boolean isInterestingFile(File file) {
697 if (file.isFile()) {
698 final String name = file.getName().toLowerCase();
699 if (name.endsWith(".exe") || name.equals("autorun.inf")
700 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
701 return false;
702 } else {
703 return true;
704 }
705 } else {
706 return false;
707 }
708 }
709
Felipe Lemeb012f912016-01-22 16:49:55 -0800710 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700711 * Get a top-level shared/external storage directory for placing files of a
712 * particular type. This is where the user will typically place and manage
713 * their own files, so you should be careful about what you put here to
714 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800715 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700716 * <p>
717 * On devices with multiple users (as described by {@link UserManager}),
718 * each user has their own isolated shared storage. Applications only have
719 * access to the shared storage for the user they're running as.
720 * </p>
721 * <p>
722 * Here is an example of typical code to manipulate a picture on the public
723 * shared storage:
724 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800725 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
726 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800727 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700728 * @param type The type of storage directory to return. Should be one of
729 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
730 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
731 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800732 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
733 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700734 * @return Returns the File path for the directory. Note that this directory
735 * may not yet exist, so you must make sure it exists before using
736 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800737 */
738 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700739 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000740 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800741 }
742
743 /**
744 * Returns the path for android-specific data on the SD card.
745 * @hide
746 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700747 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700748 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000749 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800750 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700751
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800752 /**
753 * Generates the raw path to an application's data
754 * @hide
755 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700756 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700757 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000758 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800759 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800760
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800761 /**
762 * Generates the raw path to an application's media
763 * @hide
764 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700765 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700766 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000767 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800768 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800769
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800770 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800771 * Generates the raw path to an application's OBB files
772 * @hide
773 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700774 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700775 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000776 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800777 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800778
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800779 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800780 * Generates the path to an application's files.
781 * @hide
782 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700783 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700784 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000785 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800786 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700787
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800788 /**
789 * Generates the path to an application's cache.
790 * @hide
791 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700792 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700793 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000794 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800795 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800796
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800797 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700798 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 */
800 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800801 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
803
804 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700805 * Unknown storage state, such as when a path isn't backed by known storage
806 * media.
807 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800808 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700809 */
810 public static final String MEDIA_UNKNOWN = "unknown";
811
812 /**
813 * Storage state if the media is not present.
814 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800815 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 */
817 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700820 * Storage state if the media is present but not mounted.
821 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800822 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 */
824 public static final String MEDIA_UNMOUNTED = "unmounted";
825
826 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700827 * Storage state if the media is present and being disk-checked.
828 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800829 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 */
831 public static final String MEDIA_CHECKING = "checking";
832
833 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700834 * Storage state if the media is present but is blank or is using an
835 * unsupported filesystem.
836 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800837 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 */
839 public static final String MEDIA_NOFS = "nofs";
840
841 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700842 * Storage state if the media is present and mounted at its mount point with
843 * read/write access.
844 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800845 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 */
847 public static final String MEDIA_MOUNTED = "mounted";
848
849 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700850 * Storage state if the media is present and mounted at its mount point with
851 * read-only access.
852 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800853 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 */
855 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
856
857 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700858 * Storage state if the media is present not mounted, and shared via USB
859 * mass storage.
860 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800861 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 */
863 public static final String MEDIA_SHARED = "shared";
864
865 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700866 * Storage state if the media was removed before it was unmounted.
867 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800868 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 */
870 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
871
872 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700873 * Storage state if the media is present but cannot be mounted. Typically
874 * this happens if the file system on the media is corrupted.
875 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800876 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 */
878 public static final String MEDIA_UNMOUNTABLE = "unmountable";
879
880 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700881 * Storage state if the media is in the process of being ejected.
882 *
883 * @see #getExternalStorageState(File)
884 */
885 public static final String MEDIA_EJECTING = "ejecting";
886
887 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700888 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800889 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700890 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700891 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
892 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
893 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
894 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
895 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 */
897 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +0000898 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800899 return getExternalStorageState(externalDir);
900 }
901
902 /**
903 * @deprecated use {@link #getExternalStorageState(File)}
904 */
905 @Deprecated
906 public static String getStorageState(File path) {
907 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700908 }
909
910 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700911 * Returns the current state of the shared/external storage media at the
912 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700913 *
914 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
915 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
916 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
917 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
918 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
919 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800920 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700921 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800922 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700923 return volume.getState();
924 } else {
925 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 }
928
Dianne Hackborn407f6252010-10-04 11:31:17 -0700929 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700930 * Returns whether the primary shared/external storage media is physically
931 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700932 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800933 * @return true if the storage device can be removed (such as an SD card),
934 * or false if the storage device is built in and cannot be
935 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700936 */
937 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +0000938 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800939 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700940 }
941
Kenny Roote1ff2142010-10-12 11:20:01 -0700942 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700943 * Returns whether the shared/external storage media at the given path is
944 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800945 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800946 * @return true if the storage device can be removed (such as an SD card),
947 * or false if the storage device is built in and cannot be
948 * physically removed.
949 * @throws IllegalArgumentException if the path is not a valid storage
950 * device.
951 */
952 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700953 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800954 if (volume != null) {
955 return volume.isRemovable();
956 } else {
957 throw new IllegalArgumentException("Failed to find storage device at " + path);
958 }
959 }
960
961 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700962 * Returns whether the primary shared/external storage media is emulated.
963 * <p>
964 * The contents of emulated storage devices are backed by a private user
965 * data partition, which means there is little benefit to apps storing data
966 * here instead of the private directories returned by
967 * {@link Context#getFilesDir()}, etc.
968 * <p>
969 * This returns true when emulated storage is backed by either internal
970 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -0800971 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800972 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
973 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700974 */
975 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +0000976 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800977 return isExternalStorageEmulated(externalDir);
978 }
979
980 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700981 * Returns whether the shared/external storage media at the given path is
982 * emulated.
983 * <p>
984 * The contents of emulated storage devices are backed by a private user
985 * data partition, which means there is little benefit to apps storing data
986 * here instead of the private directories returned by
987 * {@link Context#getFilesDir()}, etc.
988 * <p>
989 * This returns true when emulated storage is backed by either internal
990 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800991 *
992 * @throws IllegalArgumentException if the path is not a valid storage
993 * device.
994 */
995 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700996 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800997 if (volume != null) {
998 return volume.isEmulated();
999 } else {
1000 throw new IllegalArgumentException("Failed to find storage device at " + path);
1001 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001002 }
1003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 static File getDirectory(String variableName, String defaultPath) {
1005 String path = System.getenv(variableName);
1006 return path == null ? new File(defaultPath) : new File(path);
1007 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001008
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001009 /** {@hide} */
1010 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001011 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001012 }
1013
1014 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001015 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001016 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1017 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001018 }
1019 }
1020
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001021 /**
1022 * Append path segments to each given base path, returning result.
1023 *
1024 * @hide
1025 */
1026 public static File[] buildPaths(File[] base, String... segments) {
1027 File[] result = new File[base.length];
1028 for (int i = 0; i < base.length; i++) {
1029 result[i] = buildPath(base[i], segments);
1030 }
1031 return result;
1032 }
1033
1034 /**
1035 * Append path segments to given base path, returning result.
1036 *
1037 * @hide
1038 */
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001039 @TestApi
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001040 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001041 File cur = base;
1042 for (String segment : segments) {
1043 if (cur == null) {
1044 cur = new File(segment);
1045 } else {
1046 cur = new File(cur, segment);
1047 }
1048 }
1049 return cur;
1050 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001051
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001052
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001053 /**
1054 * If the given path exists on emulated external storage, return the
1055 * translated backing path hosted on internal storage. This bypasses any
1056 * emulation later, improving performance. This is <em>only</em> suitable
1057 * for read-only access.
1058 * <p>
1059 * Returns original path if given path doesn't meet these criteria. Callers
1060 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1061 * permission.
1062 *
1063 * @hide
1064 */
1065 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001066 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068}