blob: d543a0222d467f1ba6660bac29b94ce36394f799 [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 Sharkey4ca728c2014-01-10 16:27:19 -080019import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070020import android.content.Context;
Jeff Sharkey48877892015-03-18 11:27:19 -070021import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070022import android.os.storage.StorageVolume;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -070023import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080024import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080025
Gilles Debunneee1d6302011-05-13 10:09:32 -070026import java.io.File;
Jeff Sharkey20356142017-12-06 15:22:05 -070027import java.util.LinkedList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029/**
30 * Provides access to environment variables.
31 */
32public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080033 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Jeff Sharkeyb049e212012-09-07 23:16:01 -070035 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080036 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070037 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060038 private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
Jeff Sharkey48877892015-03-18 11:27:19 -070039 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080040 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080041 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080042 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070043 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090044 private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070045
Jeff Sharkeydfa45302012-09-12 16:25:22 -070046 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070047 public static final String DIR_ANDROID = "Android";
48 private static final String DIR_DATA = "data";
49 private static final String DIR_MEDIA = "media";
50 private static final String DIR_OBB = "obb";
51 private static final String DIR_FILES = "files";
52 private static final String DIR_CACHE = "cache";
53
54 /** {@hide} */
55 @Deprecated
56 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070057
Jeff Sharkey63d0a062013-03-01 16:12:55 -080058 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070059 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060060 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070061 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080062 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080063 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080064 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070065 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090066 private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Andreas Gamped281b422016-07-08 03:50:27 +000068 private static UserEnvironment sCurrentUser;
69 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070070
Andreas Gamped281b422016-07-08 03:50:27 +000071 static {
72 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070073 }
74
75 /** {@hide} */
Andreas Gamped281b422016-07-08 03:50:27 +000076 public static void initForCurrentUser() {
77 final int userId = UserHandle.myUserId();
78 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070079 }
80
81 /** {@hide} */
82 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070083 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070084
85 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070086 mUserId = userId;
87 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070088
Jeff Sharkey48877892015-03-18 11:27:19 -070089 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -070090 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
91 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070092 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -070093 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070094 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070095 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070096 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070097 }
98
Jeff Sharkey1abdb712013-08-11 16:28:14 -070099 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700100 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700101 return getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700102 }
103
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700104 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700105 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700106 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700107 }
108
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700109 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700110 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700111 }
112
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700113 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700114 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700115 }
116
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700117 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700118 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700119 }
120
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700121 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700122 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700123 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700124
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700125 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700126 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700127 }
128
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700129 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700130 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700131 }
132
133 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700134 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700135 }
136
137 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700138 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700139 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700140 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800143 * Return root of the "system" partition holding the core Android OS.
144 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 */
146 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800147 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 }
149
Jeff Sharkey48877892015-03-18 11:27:19 -0700150 /** {@hide} */
151 public static File getStorageDirectory() {
152 return DIR_ANDROID_STORAGE;
153 }
154
Jason parksa3cdaa52011-01-13 14:15:43 -0600155 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800156 * Return root directory of the "oem" partition holding OEM customizations,
157 * if any. If present, the partition is mounted read-only.
158 *
159 * @hide
160 */
161 public static File getOemDirectory() {
162 return DIR_OEM_ROOT;
163 }
164
165 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800166 * Return root directory of the "odm" partition holding ODM customizations,
167 * if any. If present, the partition is mounted read-only.
168 *
169 * @hide
170 */
171 public static File getOdmDirectory() {
172 return DIR_ODM_ROOT;
173 }
174
175 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700176 * Return root directory of the "vendor" partition that holds vendor-provided
177 * software that should persist across simple reflashing of the "system" partition.
178 * @hide
179 */
180 public static File getVendorDirectory() {
181 return DIR_VENDOR_ROOT;
182 }
183
Jason parksa3cdaa52011-01-13 14:15:43 -0600184 /**
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900185 * Return root directory of the "product" partition holding product-specific
186 * customizations if any. If present, the partition is mounted read-only.
187 *
188 * @hide
189 */
190 public static File getProductDirectory() {
191 return DIR_PRODUCT_ROOT;
192 }
193
194 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700195 * Return the system directory for a user. This is for use by system
196 * services to store files relating to the user. This directory will be
197 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700198 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700199 * @deprecated This directory is valid and still exists, but callers should
200 * <em>strongly</em> consider switching to
201 * {@link #getDataSystemCeDirectory(int)} which is protected
202 * with user credentials or
203 * {@link #getDataSystemDeDirectory(int)} which supports fast
204 * user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700205 * @hide
206 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700207 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700208 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800209 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700210 }
211
212 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700213 * Returns the config directory for a user. This is for use by system
214 * services to store files relating to the user which should be readable by
215 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100216 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700217 * @deprecated This directory is valid and still exists, but callers should
218 * <em>strongly</em> consider switching to
219 * {@link #getDataMiscCeDirectory(int)} which is protected with
220 * user credentials or {@link #getDataMiscDeDirectory(int)}
221 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100222 * @hide
223 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700224 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100225 public static File getUserConfigDirectory(int userId) {
226 return new File(new File(new File(
227 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
228 }
229
230 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700231 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 */
233 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800234 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 }
236
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700237 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700238 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700239 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800240 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700241 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700242 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700243 }
244 }
245
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700246 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600247 public static File getExpandDirectory() {
248 return DIR_ANDROID_EXPAND;
249 }
250
251 /** {@hide} */
Jeff Sharkey15447792015-11-05 16:18:51 -0800252 public static File getDataSystemDirectory() {
253 return new File(getDataDirectory(), "system");
254 }
255
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700256 /**
257 * Returns the base directory for per-user system directory, device encrypted.
258 * {@hide}
259 */
260 public static File getDataSystemDeDirectory() {
261 return buildPath(getDataDirectory(), "system_de");
262 }
263
264 /**
265 * Returns the base directory for per-user system directory, credential encrypted.
266 * {@hide}
267 */
268 public static File getDataSystemCeDirectory() {
269 return buildPath(getDataDirectory(), "system_ce");
270 }
271
Jeff Sharkey15447792015-11-05 16:18:51 -0800272 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700273 public static File getDataSystemCeDirectory(int userId) {
274 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800275 }
276
277 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700278 public static File getDataSystemDeDirectory(int userId) {
279 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
280 }
281
282 /** {@hide} */
283 public static File getDataMiscDirectory() {
284 return new File(getDataDirectory(), "misc");
285 }
286
287 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800288 public static File getDataMiscCeDirectory() {
289 return buildPath(getDataDirectory(), "misc_ce");
290 }
291
292 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700293 public static File getDataMiscCeDirectory(int userId) {
294 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
295 }
296
297 /** {@hide} */
298 public static File getDataMiscDeDirectory(int userId) {
299 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800300 }
301
Calin Juravled479b522016-02-24 16:22:03 +0000302 private static File getDataProfilesDeDirectory(int userId) {
303 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
304 }
305
306 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800307 public static File getDataVendorCeDirectory(int userId) {
308 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
309 }
310
311 /** {@hide} */
312 public static File getDataVendorDeDirectory(int userId) {
313 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
314 }
315
316 /** {@hide} */
Calin Juravlefd9f8ae2017-11-29 18:26:55 -0800317 public static File getProfileSnapshotPath(String packageName, String codePath) {
318 return buildPath(buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName,
319 "primary.prof.snapshot"));
Calin Juravle0bd77622016-07-12 15:56:41 +0100320 }
321
322 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000323 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
324 return buildPath(getDataProfilesDeDirectory(userId), packageName);
325 }
326
327 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700328 public static File getDataAppDirectory(String volumeUuid) {
329 return new File(getDataDirectory(volumeUuid), "app");
330 }
331
332 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700333 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700334 return new File(getDataDirectory(volumeUuid), "user");
335 }
336
337 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700338 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
339 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700340 }
341
342 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700343 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700344 String packageName) {
345 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700346 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800347 }
348
349 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700350 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800351 return new File(getDataDirectory(volumeUuid), "user_de");
352 }
353
354 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700355 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
356 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800357 }
358
359 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700360 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800361 String packageName) {
362 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700363 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700364 }
365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700367 * Return preloads directory.
368 * <p>This directory may contain pre-loaded content such as
369 * {@link #getDataPreloadsDemoDirectory() demo videos} and
370 * {@link #getDataPreloadsAppsDirectory() APK files} .
371 * {@hide}
372 */
373 public static File getDataPreloadsDirectory() {
374 return new File(getDataDirectory(), "preloads");
375 }
376
377 /**
378 * @see #getDataPreloadsDirectory()
379 * {@hide}
380 */
381 public static File getDataPreloadsDemoDirectory() {
382 return new File(getDataPreloadsDirectory(), "demo");
383 }
384
385 /**
386 * @see #getDataPreloadsDirectory()
387 * {@hide}
388 */
389 public static File getDataPreloadsAppsDirectory() {
390 return new File(getDataPreloadsDirectory(), "apps");
391 }
392
393 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700394 * @see #getDataPreloadsDirectory()
395 * {@hide}
396 */
397 public static File getDataPreloadsMediaDirectory() {
398 return new File(getDataPreloadsDirectory(), "media");
399 }
400
401 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700402 * Returns location of preloaded cache directory for package name
403 * @see #getDataPreloadsDirectory()
404 * {@hide}
405 */
406 public static File getDataPreloadsFileCacheDirectory(String packageName) {
407 return new File(getDataPreloadsFileCacheDirectory(), packageName);
408 }
409
410 /**
411 * Returns location of preloaded cache directory.
412 * @see #getDataPreloadsDirectory()
413 * {@hide}
414 */
415 public static File getDataPreloadsFileCacheDirectory() {
416 return new File(getDataPreloadsDirectory(), "file_cache");
417 }
418
419 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700420 * Return the primary shared/external storage directory. This directory may
421 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800422 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700423 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800424 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700425 * <p>
426 * <em>Note: don't be confused by the word "external" here. This directory
427 * can better be thought as media/shared storage. It is a filesystem that
428 * can hold a relatively large amount of data and that is shared across all
429 * applications (does not enforce permissions). Traditionally this is an SD
430 * card, but it may also be implemented as built-in storage in a device that
431 * is distinct from the protected internal storage and can be mounted as a
432 * filesystem on a computer.</em>
433 * <p>
434 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700435 * each user has their own isolated shared storage. Applications only have
436 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700437 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700438 * In devices with multiple shared/external storage directories, this
439 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700440 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700441 * {@link Context#getExternalFilesDirs(String)},
442 * {@link Context#getExternalCacheDirs()}, and
443 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700444 * <p>
445 * Applications should not directly use this top-level directory, in order
446 * to avoid polluting the user's root namespace. Any files that are private
447 * to the application should be placed in a directory returned by
448 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700449 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700450 * if the application is uninstalled. Other shared files should be placed in
451 * one of the directories returned by
452 * {@link #getExternalStoragePublicDirectory}.
453 * <p>
454 * Writing to this path requires the
455 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800456 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700457 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700458 * which is automatically granted if you hold the write permission.
459 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700460 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700461 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700462 * {@link Context#getExternalFilesDir(String)},
463 * {@link Context#getExternalCacheDir()}, or
464 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700465 * read or write.
466 * <p>
467 * This path may change between platform versions, so applications should
468 * only persist relative paths.
469 * <p>
470 * Here is an example of typical code to monitor the state of external
471 * storage:
472 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700473 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800474 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700475 *
476 * @see #getExternalStorageState()
477 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 */
479 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700480 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000481 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700482 }
483
484 /** {@hide} */
485 public static File getLegacyExternalStorageDirectory() {
486 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700489 /** {@hide} */
490 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700491 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700492 }
493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800495 * Standard directory in which to place any audio files that should be
496 * in the regular list of music for the user.
497 * This may be combined with
498 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
499 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
500 * of directories to categories a particular audio file as more than one
501 * type.
502 */
503 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800504
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800505 /**
506 * Standard directory in which to place any audio files that should be
507 * in the list of podcasts that the user can select (not as regular
508 * music).
509 * This may be combined with {@link #DIRECTORY_MUSIC},
510 * {@link #DIRECTORY_NOTIFICATIONS},
511 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
512 * of directories to categories a particular audio file as more than one
513 * type.
514 */
515 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800516
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800517 /**
518 * Standard directory in which to place any audio files that should be
519 * in the list of ringtones that the user can select (not as regular
520 * music).
521 * This may be combined with {@link #DIRECTORY_MUSIC},
522 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
523 * {@link #DIRECTORY_ALARMS} as a series
524 * of directories to categories a particular audio file as more than one
525 * type.
526 */
527 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800528
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800529 /**
530 * Standard directory in which to place any audio files that should be
531 * in the list of alarms that the user can select (not as regular
532 * music).
533 * This may be combined with {@link #DIRECTORY_MUSIC},
534 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
535 * and {@link #DIRECTORY_RINGTONES} as a series
536 * of directories to categories a particular audio file as more than one
537 * type.
538 */
539 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800540
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800541 /**
542 * Standard directory in which to place any audio files that should be
543 * in the list of notifications that the user can select (not as regular
544 * music).
545 * This may be combined with {@link #DIRECTORY_MUSIC},
546 * {@link #DIRECTORY_PODCASTS},
547 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
548 * of directories to categories a particular audio file as more than one
549 * type.
550 */
551 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800552
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800553 /**
554 * Standard directory in which to place pictures that are available to
555 * the user. Note that this is primarily a convention for the top-level
556 * public directory, as the media scanner will find and collect pictures
557 * in any directory.
558 */
559 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800560
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800561 /**
562 * Standard directory in which to place movies that are available to
563 * the user. Note that this is primarily a convention for the top-level
564 * public directory, as the media scanner will find and collect movies
565 * in any directory.
566 */
567 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800568
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800569 /**
570 * Standard directory in which to place files that have been downloaded by
571 * the user. Note that this is primarily a convention for the top-level
572 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700573 * private directories. Also note that though the constant here is
574 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
575 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800576 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700577 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800578
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800579 /**
580 * The traditional location for pictures and videos when mounting the
581 * device as a camera. Note that this is primarily a convention for the
582 * top-level public directory, as this convention makes no sense elsewhere.
583 */
584 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700585
586 /**
587 * Standard directory in which to place documents that have been created by
588 * the user.
589 */
590 public static String DIRECTORY_DOCUMENTS = "Documents";
591
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800592 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800593 * List of standard storage directories.
594 * <p>
595 * Each of its values have its own constant:
596 * <ul>
597 * <li>{@link #DIRECTORY_MUSIC}
598 * <li>{@link #DIRECTORY_PODCASTS}
599 * <li>{@link #DIRECTORY_ALARMS}
600 * <li>{@link #DIRECTORY_RINGTONES}
601 * <li>{@link #DIRECTORY_NOTIFICATIONS}
602 * <li>{@link #DIRECTORY_PICTURES}
603 * <li>{@link #DIRECTORY_MOVIES}
604 * <li>{@link #DIRECTORY_DOWNLOADS}
605 * <li>{@link #DIRECTORY_DCIM}
606 * <li>{@link #DIRECTORY_DOCUMENTS}
607 * </ul>
608 * @hide
609 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800610 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800611 DIRECTORY_MUSIC,
612 DIRECTORY_PODCASTS,
613 DIRECTORY_RINGTONES,
614 DIRECTORY_ALARMS,
615 DIRECTORY_NOTIFICATIONS,
616 DIRECTORY_PICTURES,
617 DIRECTORY_MOVIES,
618 DIRECTORY_DOWNLOADS,
619 DIRECTORY_DCIM,
Steve McKayab3b8932016-02-16 11:37:03 -0800620 DIRECTORY_DOCUMENTS
Felipe Lemec7b1f892016-01-15 15:02:31 -0800621 };
622
623 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800624 * @hide
625 */
626 public static boolean isStandardDirectory(String dir) {
627 for (String valid : STANDARD_DIRECTORIES) {
628 if (valid.equals(dir)) {
629 return true;
630 }
631 }
632 return false;
633 }
634
Jeff Sharkey20356142017-12-06 15:22:05 -0700635 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
636 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
637 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
638 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
639 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
640 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
641 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
642 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
643 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
644 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
645
646 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
647 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
648
649 /**
650 * Classify the content types present on the given external storage device.
651 * <p>
652 * This is typically useful for deciding if an inserted SD card is empty, or
653 * if it contains content like photos that should be preserved.
654 *
655 * @hide
656 */
657 public static int classifyExternalStorageDirectory(File dir) {
658 int res = 0;
659 for (File f : FileUtils.listFilesOrEmpty(dir)) {
660 if (f.isFile() && isInterestingFile(f)) {
661 res |= HAS_OTHER;
662 } else if (f.isDirectory() && hasInterestingFiles(f)) {
663 final String name = f.getName();
664 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
665 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
666 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
667 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
668 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
669 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
670 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
671 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
672 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
673 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
674 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
675 else res |= HAS_OTHER;
676 }
677 }
678 return res;
679 }
680
681 private static boolean hasInterestingFiles(File dir) {
682 final LinkedList<File> explore = new LinkedList<>();
683 explore.add(dir);
684 while (!explore.isEmpty()) {
685 dir = explore.pop();
686 for (File f : FileUtils.listFilesOrEmpty(dir)) {
687 if (isInterestingFile(f)) return true;
688 if (f.isDirectory()) explore.add(f);
689 }
690 }
691 return false;
692 }
693
694 private static boolean isInterestingFile(File file) {
695 if (file.isFile()) {
696 final String name = file.getName().toLowerCase();
697 if (name.endsWith(".exe") || name.equals("autorun.inf")
698 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
699 return false;
700 } else {
701 return true;
702 }
703 } else {
704 return false;
705 }
706 }
707
Felipe Lemeb012f912016-01-22 16:49:55 -0800708 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700709 * Get a top-level shared/external storage directory for placing files of a
710 * particular type. This is where the user will typically place and manage
711 * their own files, so you should be careful about what you put here to
712 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800713 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700714 * <p>
715 * On devices with multiple users (as described by {@link UserManager}),
716 * each user has their own isolated shared storage. Applications only have
717 * access to the shared storage for the user they're running as.
718 * </p>
719 * <p>
720 * Here is an example of typical code to manipulate a picture on the public
721 * shared storage:
722 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800723 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
724 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800725 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700726 * @param type The type of storage directory to return. Should be one of
727 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
728 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
729 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800730 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
731 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700732 * @return Returns the File path for the directory. Note that this directory
733 * may not yet exist, so you must make sure it exists before using
734 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800735 */
736 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700737 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000738 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800739 }
740
741 /**
742 * Returns the path for android-specific data on the SD card.
743 * @hide
744 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700745 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700746 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000747 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800748 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700749
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800750 /**
751 * Generates the raw path to an application's data
752 * @hide
753 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700754 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700755 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000756 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800757 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800758
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800759 /**
760 * Generates the raw path to an application's media
761 * @hide
762 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700763 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700764 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000765 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800766 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800767
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800768 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800769 * Generates the raw path to an application's OBB files
770 * @hide
771 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700772 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700773 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000774 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800775 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800776
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800777 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800778 * Generates the path to an application's files.
779 * @hide
780 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700781 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700782 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000783 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800784 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700785
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800786 /**
787 * Generates the path to an application's cache.
788 * @hide
789 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700790 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700791 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000792 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800793 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800794
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800795 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700796 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 */
798 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800799 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 }
801
802 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700803 * Unknown storage state, such as when a path isn't backed by known storage
804 * media.
805 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800806 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700807 */
808 public static final String MEDIA_UNKNOWN = "unknown";
809
810 /**
811 * Storage state if the media is not present.
812 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800813 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 */
815 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700818 * Storage state if the media is present but not mounted.
819 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800820 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 */
822 public static final String MEDIA_UNMOUNTED = "unmounted";
823
824 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700825 * Storage state if the media is present and being disk-checked.
826 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800827 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 */
829 public static final String MEDIA_CHECKING = "checking";
830
831 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700832 * Storage state if the media is present but is blank or is using an
833 * unsupported filesystem.
834 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800835 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 */
837 public static final String MEDIA_NOFS = "nofs";
838
839 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700840 * Storage state if the media is present and mounted at its mount point with
841 * read/write access.
842 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800843 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 */
845 public static final String MEDIA_MOUNTED = "mounted";
846
847 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700848 * Storage state if the media is present and mounted at its mount point with
849 * read-only access.
850 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800851 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 */
853 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
854
855 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700856 * Storage state if the media is present not mounted, and shared via USB
857 * mass storage.
858 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800859 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 */
861 public static final String MEDIA_SHARED = "shared";
862
863 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700864 * Storage state if the media was removed before it was unmounted.
865 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800866 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 */
868 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
869
870 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700871 * Storage state if the media is present but cannot be mounted. Typically
872 * this happens if the file system on the media is corrupted.
873 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800874 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 */
876 public static final String MEDIA_UNMOUNTABLE = "unmountable";
877
878 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700879 * Storage state if the media is in the process of being ejected.
880 *
881 * @see #getExternalStorageState(File)
882 */
883 public static final String MEDIA_EJECTING = "ejecting";
884
885 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700886 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800887 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700888 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700889 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
890 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
891 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
892 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
893 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 */
895 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +0000896 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800897 return getExternalStorageState(externalDir);
898 }
899
900 /**
901 * @deprecated use {@link #getExternalStorageState(File)}
902 */
903 @Deprecated
904 public static String getStorageState(File path) {
905 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700906 }
907
908 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700909 * Returns the current state of the shared/external storage media at the
910 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700911 *
912 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
913 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
914 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
915 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
916 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
917 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800918 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700919 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800920 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700921 return volume.getState();
922 } else {
923 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 }
926
Dianne Hackborn407f6252010-10-04 11:31:17 -0700927 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700928 * Returns whether the primary shared/external storage media is physically
929 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700930 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800931 * @return true if the storage device can be removed (such as an SD card),
932 * or false if the storage device is built in and cannot be
933 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700934 */
935 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +0000936 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800937 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700938 }
939
Kenny Roote1ff2142010-10-12 11:20:01 -0700940 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700941 * Returns whether the shared/external storage media at the given path is
942 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800943 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800944 * @return true if the storage device can be removed (such as an SD card),
945 * or false if the storage device is built in and cannot be
946 * physically removed.
947 * @throws IllegalArgumentException if the path is not a valid storage
948 * device.
949 */
950 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700951 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800952 if (volume != null) {
953 return volume.isRemovable();
954 } else {
955 throw new IllegalArgumentException("Failed to find storage device at " + path);
956 }
957 }
958
959 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700960 * Returns whether the primary shared/external storage media is emulated.
961 * <p>
962 * The contents of emulated storage devices are backed by a private user
963 * data partition, which means there is little benefit to apps storing data
964 * here instead of the private directories returned by
965 * {@link Context#getFilesDir()}, etc.
966 * <p>
967 * This returns true when emulated storage is backed by either internal
968 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -0800969 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800970 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
971 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700972 */
973 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +0000974 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800975 return isExternalStorageEmulated(externalDir);
976 }
977
978 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700979 * Returns whether the shared/external storage media at the given path is
980 * emulated.
981 * <p>
982 * The contents of emulated storage devices are backed by a private user
983 * data partition, which means there is little benefit to apps storing data
984 * here instead of the private directories returned by
985 * {@link Context#getFilesDir()}, etc.
986 * <p>
987 * This returns true when emulated storage is backed by either internal
988 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800989 *
990 * @throws IllegalArgumentException if the path is not a valid storage
991 * device.
992 */
993 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700994 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800995 if (volume != null) {
996 return volume.isEmulated();
997 } else {
998 throw new IllegalArgumentException("Failed to find storage device at " + path);
999 }
Kenny Roote1ff2142010-10-12 11:20:01 -07001000 }
1001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 static File getDirectory(String variableName, String defaultPath) {
1003 String path = System.getenv(variableName);
1004 return path == null ? new File(defaultPath) : new File(path);
1005 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001006
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001007 /** {@hide} */
1008 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +00001009 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001010 }
1011
1012 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001013 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001014 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1015 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001016 }
1017 }
1018
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001019 /**
1020 * Append path segments to each given base path, returning result.
1021 *
1022 * @hide
1023 */
1024 public static File[] buildPaths(File[] base, String... segments) {
1025 File[] result = new File[base.length];
1026 for (int i = 0; i < base.length; i++) {
1027 result[i] = buildPath(base[i], segments);
1028 }
1029 return result;
1030 }
1031
1032 /**
1033 * Append path segments to given base path, returning result.
1034 *
1035 * @hide
1036 */
1037 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001038 File cur = base;
1039 for (String segment : segments) {
1040 if (cur == null) {
1041 cur = new File(segment);
1042 } else {
1043 cur = new File(cur, segment);
1044 }
1045 }
1046 return cur;
1047 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001048
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001049
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001050 /**
1051 * If the given path exists on emulated external storage, return the
1052 * translated backing path hosted on internal storage. This bypasses any
1053 * emulation later, improving performance. This is <em>only</em> suitable
1054 * for read-only access.
1055 * <p>
1056 * Returns original path if given path doesn't meet these criteria. Callers
1057 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1058 * permission.
1059 *
1060 * @hide
1061 */
1062 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001063 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065}