blob: 62731e84a401b9343ac4ac3f207ebe9d7b1b181b [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";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070044
Jeff Sharkeydfa45302012-09-12 16:25:22 -070045 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070046 public static final String DIR_ANDROID = "Android";
47 private static final String DIR_DATA = "data";
48 private static final String DIR_MEDIA = "media";
49 private static final String DIR_OBB = "obb";
50 private static final String DIR_FILES = "files";
51 private static final String DIR_CACHE = "cache";
52
53 /** {@hide} */
54 @Deprecated
55 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070056
Jeff Sharkey63d0a062013-03-01 16:12:55 -080057 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070058 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -060059 private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
Jeff Sharkey48877892015-03-18 11:27:19 -070060 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080061 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080062 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080063 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070064 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
Andreas Gamped281b422016-07-08 03:50:27 +000066 private static UserEnvironment sCurrentUser;
67 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070068
Andreas Gamped281b422016-07-08 03:50:27 +000069 static {
70 initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -070071 }
72
73 /** {@hide} */
Andreas Gamped281b422016-07-08 03:50:27 +000074 public static void initForCurrentUser() {
75 final int userId = UserHandle.myUserId();
76 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070077 }
78
79 /** {@hide} */
80 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070081 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070082
83 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070084 mUserId = userId;
85 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070086
Jeff Sharkey48877892015-03-18 11:27:19 -070087 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -070088 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
89 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070090 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -070091 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070092 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070093 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070094 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070095 }
96
Jeff Sharkey1abdb712013-08-11 16:28:14 -070097 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -070098 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -070099 return getExternalDirs()[0];
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 getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700104 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700105 }
106
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700107 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700108 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700109 }
110
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700111 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700112 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700113 }
114
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700115 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700116 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700117 }
118
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700119 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700120 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700121 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700122
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700123 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700124 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700125 }
126
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700127 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700128 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700129 }
130
131 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700132 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700133 }
134
135 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700136 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700137 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700138 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800141 * Return root of the "system" partition holding the core Android OS.
142 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 */
144 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800145 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147
Jeff Sharkey48877892015-03-18 11:27:19 -0700148 /** {@hide} */
149 public static File getStorageDirectory() {
150 return DIR_ANDROID_STORAGE;
151 }
152
Jason parksa3cdaa52011-01-13 14:15:43 -0600153 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800154 * Return root directory of the "oem" partition holding OEM customizations,
155 * if any. If present, the partition is mounted read-only.
156 *
157 * @hide
158 */
159 public static File getOemDirectory() {
160 return DIR_OEM_ROOT;
161 }
162
163 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800164 * Return root directory of the "odm" partition holding ODM customizations,
165 * if any. If present, the partition is mounted read-only.
166 *
167 * @hide
168 */
169 public static File getOdmDirectory() {
170 return DIR_ODM_ROOT;
171 }
172
173 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700174 * Return root directory of the "vendor" partition that holds vendor-provided
175 * software that should persist across simple reflashing of the "system" partition.
176 * @hide
177 */
178 public static File getVendorDirectory() {
179 return DIR_VENDOR_ROOT;
180 }
181
Jason parksa3cdaa52011-01-13 14:15:43 -0600182 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700183 * Return the system directory for a user. This is for use by system
184 * services to store files relating to the user. This directory will be
185 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700186 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700187 * @deprecated This directory is valid and still exists, but callers should
188 * <em>strongly</em> consider switching to
189 * {@link #getDataSystemCeDirectory(int)} which is protected
190 * with user credentials or
191 * {@link #getDataSystemDeDirectory(int)} which supports fast
192 * user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700193 * @hide
194 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700195 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700196 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800197 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700198 }
199
200 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700201 * Returns the config directory for a user. This is for use by system
202 * services to store files relating to the user which should be readable by
203 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100204 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700205 * @deprecated This directory is valid and still exists, but callers should
206 * <em>strongly</em> consider switching to
207 * {@link #getDataMiscCeDirectory(int)} which is protected with
208 * user credentials or {@link #getDataMiscDeDirectory(int)}
209 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100210 * @hide
211 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700212 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100213 public static File getUserConfigDirectory(int userId) {
214 return new File(new File(new File(
215 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
216 }
217
218 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700219 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 */
221 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800222 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 }
224
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700225 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700226 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700227 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800228 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700229 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700230 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700231 }
232 }
233
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700234 /** {@hide} */
Jeff Sharkeycf3f0a12016-03-17 19:57:58 -0600235 public static File getExpandDirectory() {
236 return DIR_ANDROID_EXPAND;
237 }
238
239 /** {@hide} */
Jeff Sharkey15447792015-11-05 16:18:51 -0800240 public static File getDataSystemDirectory() {
241 return new File(getDataDirectory(), "system");
242 }
243
Amith Yamasanid04aaa32016-06-13 12:09:36 -0700244 /**
245 * Returns the base directory for per-user system directory, device encrypted.
246 * {@hide}
247 */
248 public static File getDataSystemDeDirectory() {
249 return buildPath(getDataDirectory(), "system_de");
250 }
251
252 /**
253 * Returns the base directory for per-user system directory, credential encrypted.
254 * {@hide}
255 */
256 public static File getDataSystemCeDirectory() {
257 return buildPath(getDataDirectory(), "system_ce");
258 }
259
Jeff Sharkey15447792015-11-05 16:18:51 -0800260 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700261 public static File getDataSystemCeDirectory(int userId) {
262 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800263 }
264
265 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700266 public static File getDataSystemDeDirectory(int userId) {
267 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
268 }
269
270 /** {@hide} */
271 public static File getDataMiscDirectory() {
272 return new File(getDataDirectory(), "misc");
273 }
274
275 /** {@hide} */
Amith Yamasania2807132017-01-26 12:35:14 -0800276 public static File getDataMiscCeDirectory() {
277 return buildPath(getDataDirectory(), "misc_ce");
278 }
279
280 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700281 public static File getDataMiscCeDirectory(int userId) {
282 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
283 }
284
285 /** {@hide} */
286 public static File getDataMiscDeDirectory(int userId) {
287 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800288 }
289
Calin Juravled479b522016-02-24 16:22:03 +0000290 private static File getDataProfilesDeDirectory(int userId) {
291 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
292 }
293
294 /** {@hide} */
Andreas Huber7fe20532018-01-22 11:26:44 -0800295 public static File getDataVendorCeDirectory(int userId) {
296 return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId));
297 }
298
299 /** {@hide} */
300 public static File getDataVendorDeDirectory(int userId) {
301 return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId));
302 }
303
304 /** {@hide} */
Calin Juravlefd9f8ae2017-11-29 18:26:55 -0800305 public static File getProfileSnapshotPath(String packageName, String codePath) {
306 return buildPath(buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName,
307 "primary.prof.snapshot"));
Calin Juravle0bd77622016-07-12 15:56:41 +0100308 }
309
310 /** {@hide} */
Calin Juravled479b522016-02-24 16:22:03 +0000311 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
312 return buildPath(getDataProfilesDeDirectory(userId), packageName);
313 }
314
315 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700316 public static File getDataAppDirectory(String volumeUuid) {
317 return new File(getDataDirectory(volumeUuid), "app");
318 }
319
320 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700321 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700322 return new File(getDataDirectory(volumeUuid), "user");
323 }
324
325 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700326 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
327 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700328 }
329
330 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700331 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700332 String packageName) {
333 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700334 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800335 }
336
337 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700338 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800339 return new File(getDataDirectory(volumeUuid), "user_de");
340 }
341
342 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700343 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
344 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800345 }
346
347 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700348 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800349 String packageName) {
350 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700351 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700352 }
353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
Fyodor Kupolov88257582016-05-24 16:06:56 -0700355 * Return preloads directory.
356 * <p>This directory may contain pre-loaded content such as
357 * {@link #getDataPreloadsDemoDirectory() demo videos} and
358 * {@link #getDataPreloadsAppsDirectory() APK files} .
359 * {@hide}
360 */
361 public static File getDataPreloadsDirectory() {
362 return new File(getDataDirectory(), "preloads");
363 }
364
365 /**
366 * @see #getDataPreloadsDirectory()
367 * {@hide}
368 */
369 public static File getDataPreloadsDemoDirectory() {
370 return new File(getDataPreloadsDirectory(), "demo");
371 }
372
373 /**
374 * @see #getDataPreloadsDirectory()
375 * {@hide}
376 */
377 public static File getDataPreloadsAppsDirectory() {
378 return new File(getDataPreloadsDirectory(), "apps");
379 }
380
381 /**
Fyodor Kupolov19551a82016-08-22 14:46:08 -0700382 * @see #getDataPreloadsDirectory()
383 * {@hide}
384 */
385 public static File getDataPreloadsMediaDirectory() {
386 return new File(getDataPreloadsDirectory(), "media");
387 }
388
389 /**
Fyodor Kupolov6e687062016-09-09 17:42:12 -0700390 * Returns location of preloaded cache directory for package name
391 * @see #getDataPreloadsDirectory()
392 * {@hide}
393 */
394 public static File getDataPreloadsFileCacheDirectory(String packageName) {
395 return new File(getDataPreloadsFileCacheDirectory(), packageName);
396 }
397
398 /**
399 * Returns location of preloaded cache directory.
400 * @see #getDataPreloadsDirectory()
401 * {@hide}
402 */
403 public static File getDataPreloadsFileCacheDirectory() {
404 return new File(getDataPreloadsDirectory(), "file_cache");
405 }
406
407 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700408 * Return the primary shared/external storage directory. This directory may
409 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800410 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700411 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800412 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700413 * <p>
414 * <em>Note: don't be confused by the word "external" here. This directory
415 * can better be thought as media/shared storage. It is a filesystem that
416 * can hold a relatively large amount of data and that is shared across all
417 * applications (does not enforce permissions). Traditionally this is an SD
418 * card, but it may also be implemented as built-in storage in a device that
419 * is distinct from the protected internal storage and can be mounted as a
420 * filesystem on a computer.</em>
421 * <p>
422 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700423 * each user has their own isolated shared storage. Applications only have
424 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700425 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700426 * In devices with multiple shared/external storage directories, this
427 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700428 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700429 * {@link Context#getExternalFilesDirs(String)},
430 * {@link Context#getExternalCacheDirs()}, and
431 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700432 * <p>
433 * Applications should not directly use this top-level directory, in order
434 * to avoid polluting the user's root namespace. Any files that are private
435 * to the application should be placed in a directory returned by
436 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700437 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700438 * if the application is uninstalled. Other shared files should be placed in
439 * one of the directories returned by
440 * {@link #getExternalStoragePublicDirectory}.
441 * <p>
442 * Writing to this path requires the
443 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800444 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700445 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700446 * which is automatically granted if you hold the write permission.
447 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700448 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700449 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700450 * {@link Context#getExternalFilesDir(String)},
451 * {@link Context#getExternalCacheDir()}, or
452 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700453 * read or write.
454 * <p>
455 * This path may change between platform versions, so applications should
456 * only persist relative paths.
457 * <p>
458 * Here is an example of typical code to monitor the state of external
459 * storage:
460 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700461 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800462 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700463 *
464 * @see #getExternalStorageState()
465 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 */
467 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700468 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000469 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700470 }
471
472 /** {@hide} */
473 public static File getLegacyExternalStorageDirectory() {
474 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
476
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700477 /** {@hide} */
478 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700479 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700480 }
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800483 * Standard directory in which to place any audio files that should be
484 * in the regular list of music for the user.
485 * This may be combined with
486 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
487 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
488 * of directories to categories a particular audio file as more than one
489 * type.
490 */
491 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800492
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800493 /**
494 * Standard directory in which to place any audio files that should be
495 * in the list of podcasts that the user can select (not as regular
496 * music).
497 * This may be combined with {@link #DIRECTORY_MUSIC},
498 * {@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_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -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 ringtones that the user can select (not as regular
508 * music).
509 * This may be combined with {@link #DIRECTORY_MUSIC},
510 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
511 * {@link #DIRECTORY_ALARMS} as a series
512 * of directories to categories a particular audio file as more than one
513 * type.
514 */
515 public static String DIRECTORY_RINGTONES = "Ringtones";
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 alarms 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},
523 * and {@link #DIRECTORY_RINGTONES} as a series
524 * of directories to categories a particular audio file as more than one
525 * type.
526 */
527 public static String DIRECTORY_ALARMS = "Alarms";
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 notifications that the user can select (not as regular
532 * music).
533 * This may be combined with {@link #DIRECTORY_MUSIC},
534 * {@link #DIRECTORY_PODCASTS},
535 * {@link #DIRECTORY_ALARMS}, 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_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800540
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800541 /**
542 * Standard directory in which to place pictures that are available to
543 * the user. Note that this is primarily a convention for the top-level
544 * public directory, as the media scanner will find and collect pictures
545 * in any directory.
546 */
547 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800548
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800549 /**
550 * Standard directory in which to place movies that are available to
551 * the user. Note that this is primarily a convention for the top-level
552 * public directory, as the media scanner will find and collect movies
553 * in any directory.
554 */
555 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800556
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800557 /**
558 * Standard directory in which to place files that have been downloaded by
559 * the user. Note that this is primarily a convention for the top-level
560 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700561 * private directories. Also note that though the constant here is
562 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
563 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800564 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700565 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800566
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800567 /**
568 * The traditional location for pictures and videos when mounting the
569 * device as a camera. Note that this is primarily a convention for the
570 * top-level public directory, as this convention makes no sense elsewhere.
571 */
572 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700573
574 /**
575 * Standard directory in which to place documents that have been created by
576 * the user.
577 */
578 public static String DIRECTORY_DOCUMENTS = "Documents";
579
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800580 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800581 * List of standard storage directories.
582 * <p>
583 * Each of its values have its own constant:
584 * <ul>
585 * <li>{@link #DIRECTORY_MUSIC}
586 * <li>{@link #DIRECTORY_PODCASTS}
587 * <li>{@link #DIRECTORY_ALARMS}
588 * <li>{@link #DIRECTORY_RINGTONES}
589 * <li>{@link #DIRECTORY_NOTIFICATIONS}
590 * <li>{@link #DIRECTORY_PICTURES}
591 * <li>{@link #DIRECTORY_MOVIES}
592 * <li>{@link #DIRECTORY_DOWNLOADS}
593 * <li>{@link #DIRECTORY_DCIM}
594 * <li>{@link #DIRECTORY_DOCUMENTS}
595 * </ul>
596 * @hide
597 */
Felipe Leme3e166b22016-02-24 10:17:41 -0800598 public static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800599 DIRECTORY_MUSIC,
600 DIRECTORY_PODCASTS,
601 DIRECTORY_RINGTONES,
602 DIRECTORY_ALARMS,
603 DIRECTORY_NOTIFICATIONS,
604 DIRECTORY_PICTURES,
605 DIRECTORY_MOVIES,
606 DIRECTORY_DOWNLOADS,
607 DIRECTORY_DCIM,
Steve McKayab3b8932016-02-16 11:37:03 -0800608 DIRECTORY_DOCUMENTS
Felipe Lemec7b1f892016-01-15 15:02:31 -0800609 };
610
611 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800612 * @hide
613 */
614 public static boolean isStandardDirectory(String dir) {
615 for (String valid : STANDARD_DIRECTORIES) {
616 if (valid.equals(dir)) {
617 return true;
618 }
619 }
620 return false;
621 }
622
Jeff Sharkey20356142017-12-06 15:22:05 -0700623 /** {@hide} */ public static final int HAS_MUSIC = 1 << 0;
624 /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1;
625 /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2;
626 /** {@hide} */ public static final int HAS_ALARMS = 1 << 3;
627 /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4;
628 /** {@hide} */ public static final int HAS_PICTURES = 1 << 5;
629 /** {@hide} */ public static final int HAS_MOVIES = 1 << 6;
630 /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7;
631 /** {@hide} */ public static final int HAS_DCIM = 1 << 8;
632 /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9;
633
634 /** {@hide} */ public static final int HAS_ANDROID = 1 << 16;
635 /** {@hide} */ public static final int HAS_OTHER = 1 << 17;
636
637 /**
638 * Classify the content types present on the given external storage device.
639 * <p>
640 * This is typically useful for deciding if an inserted SD card is empty, or
641 * if it contains content like photos that should be preserved.
642 *
643 * @hide
644 */
645 public static int classifyExternalStorageDirectory(File dir) {
646 int res = 0;
647 for (File f : FileUtils.listFilesOrEmpty(dir)) {
648 if (f.isFile() && isInterestingFile(f)) {
649 res |= HAS_OTHER;
650 } else if (f.isDirectory() && hasInterestingFiles(f)) {
651 final String name = f.getName();
652 if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC;
653 else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS;
654 else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES;
655 else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS;
656 else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS;
657 else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES;
658 else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES;
659 else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS;
660 else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM;
661 else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS;
662 else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID;
663 else res |= HAS_OTHER;
664 }
665 }
666 return res;
667 }
668
669 private static boolean hasInterestingFiles(File dir) {
670 final LinkedList<File> explore = new LinkedList<>();
671 explore.add(dir);
672 while (!explore.isEmpty()) {
673 dir = explore.pop();
674 for (File f : FileUtils.listFilesOrEmpty(dir)) {
675 if (isInterestingFile(f)) return true;
676 if (f.isDirectory()) explore.add(f);
677 }
678 }
679 return false;
680 }
681
682 private static boolean isInterestingFile(File file) {
683 if (file.isFile()) {
684 final String name = file.getName().toLowerCase();
685 if (name.endsWith(".exe") || name.equals("autorun.inf")
686 || name.equals("launchpad.zip") || name.equals(".nomedia")) {
687 return false;
688 } else {
689 return true;
690 }
691 } else {
692 return false;
693 }
694 }
695
Felipe Lemeb012f912016-01-22 16:49:55 -0800696 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700697 * Get a top-level shared/external storage directory for placing files of a
698 * particular type. This is where the user will typically place and manage
699 * their own files, so you should be careful about what you put here to
700 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800701 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700702 * <p>
703 * On devices with multiple users (as described by {@link UserManager}),
704 * each user has their own isolated shared storage. Applications only have
705 * access to the shared storage for the user they're running as.
706 * </p>
707 * <p>
708 * Here is an example of typical code to manipulate a picture on the public
709 * shared storage:
710 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800711 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
712 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800713 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700714 * @param type The type of storage directory to return. Should be one of
715 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
716 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
717 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800718 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
719 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700720 * @return Returns the File path for the directory. Note that this directory
721 * may not yet exist, so you must make sure it exists before using
722 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800723 */
724 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700725 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000726 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800727 }
728
729 /**
730 * Returns the path for android-specific data on the SD card.
731 * @hide
732 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700733 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700734 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000735 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800736 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700737
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800738 /**
739 * Generates the raw path to an application's data
740 * @hide
741 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700742 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700743 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000744 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800745 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800746
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800747 /**
748 * Generates the raw path to an application's media
749 * @hide
750 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700751 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700752 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000753 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800754 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800755
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800756 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800757 * Generates the raw path to an application's OBB files
758 * @hide
759 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700760 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700761 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000762 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800763 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800764
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800765 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800766 * Generates the path to an application's files.
767 * @hide
768 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700769 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700770 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000771 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800772 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700773
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800774 /**
775 * Generates the path to an application's cache.
776 * @hide
777 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700778 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700779 throwIfUserRequired();
Andreas Gamped281b422016-07-08 03:50:27 +0000780 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800781 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800782
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800783 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700784 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 */
786 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800787 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 }
789
790 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700791 * Unknown storage state, such as when a path isn't backed by known storage
792 * media.
793 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800794 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700795 */
796 public static final String MEDIA_UNKNOWN = "unknown";
797
798 /**
799 * Storage state if the media is not present.
800 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800801 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 */
803 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700806 * Storage state if the media is present but not mounted.
807 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800808 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 */
810 public static final String MEDIA_UNMOUNTED = "unmounted";
811
812 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700813 * Storage state if the media is present and being disk-checked.
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_CHECKING = "checking";
818
819 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700820 * Storage state if the media is present but is blank or is using an
821 * unsupported filesystem.
822 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800823 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 */
825 public static final String MEDIA_NOFS = "nofs";
826
827 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700828 * Storage state if the media is present and mounted at its mount point with
829 * read/write access.
830 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800831 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 */
833 public static final String MEDIA_MOUNTED = "mounted";
834
835 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700836 * Storage state if the media is present and mounted at its mount point with
837 * read-only access.
838 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800839 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 */
841 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
842
843 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700844 * Storage state if the media is present not mounted, and shared via USB
845 * mass storage.
846 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800847 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 */
849 public static final String MEDIA_SHARED = "shared";
850
851 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700852 * Storage state if the media was removed before it was unmounted.
853 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800854 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 */
856 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
857
858 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700859 * Storage state if the media is present but cannot be mounted. Typically
860 * this happens if the file system on the media is corrupted.
861 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800862 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 */
864 public static final String MEDIA_UNMOUNTABLE = "unmountable";
865
866 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700867 * Storage state if the media is in the process of being ejected.
868 *
869 * @see #getExternalStorageState(File)
870 */
871 public static final String MEDIA_EJECTING = "ejecting";
872
873 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700874 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800875 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700876 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700877 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
878 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
879 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
880 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
881 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 */
883 public static String getExternalStorageState() {
Andreas Gamped281b422016-07-08 03:50:27 +0000884 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800885 return getExternalStorageState(externalDir);
886 }
887
888 /**
889 * @deprecated use {@link #getExternalStorageState(File)}
890 */
891 @Deprecated
892 public static String getStorageState(File path) {
893 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700894 }
895
896 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700897 * Returns the current state of the shared/external storage media at the
898 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700899 *
900 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
901 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
902 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
903 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
904 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
905 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800906 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700907 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800908 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700909 return volume.getState();
910 } else {
911 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
Dianne Hackborn407f6252010-10-04 11:31:17 -0700915 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700916 * Returns whether the primary shared/external storage media is physically
917 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700918 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800919 * @return true if the storage device can be removed (such as an SD card),
920 * or false if the storage device is built in and cannot be
921 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700922 */
923 public static boolean isExternalStorageRemovable() {
Andreas Gamped281b422016-07-08 03:50:27 +0000924 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800925 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700926 }
927
Kenny Roote1ff2142010-10-12 11:20:01 -0700928 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700929 * Returns whether the shared/external storage media at the given path is
930 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800931 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800932 * @return true if the storage device can be removed (such as an SD card),
933 * or false if the storage device is built in and cannot be
934 * physically removed.
935 * @throws IllegalArgumentException if the path is not a valid storage
936 * device.
937 */
938 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700939 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800940 if (volume != null) {
941 return volume.isRemovable();
942 } else {
943 throw new IllegalArgumentException("Failed to find storage device at " + path);
944 }
945 }
946
947 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700948 * Returns whether the primary shared/external storage media is emulated.
949 * <p>
950 * The contents of emulated storage devices are backed by a private user
951 * data partition, which means there is little benefit to apps storing data
952 * here instead of the private directories returned by
953 * {@link Context#getFilesDir()}, etc.
954 * <p>
955 * This returns true when emulated storage is backed by either internal
956 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -0800957 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800958 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
959 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700960 */
961 public static boolean isExternalStorageEmulated() {
Andreas Gamped281b422016-07-08 03:50:27 +0000962 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800963 return isExternalStorageEmulated(externalDir);
964 }
965
966 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700967 * Returns whether the shared/external storage media at the given path is
968 * emulated.
969 * <p>
970 * The contents of emulated storage devices are backed by a private user
971 * data partition, which means there is little benefit to apps storing data
972 * here instead of the private directories returned by
973 * {@link Context#getFilesDir()}, etc.
974 * <p>
975 * This returns true when emulated storage is backed by either internal
976 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800977 *
978 * @throws IllegalArgumentException if the path is not a valid storage
979 * device.
980 */
981 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700982 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800983 if (volume != null) {
984 return volume.isEmulated();
985 } else {
986 throw new IllegalArgumentException("Failed to find storage device at " + path);
987 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700988 }
989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 static File getDirectory(String variableName, String defaultPath) {
991 String path = System.getenv(variableName);
992 return path == null ? new File(defaultPath) : new File(path);
993 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700994
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700995 /** {@hide} */
996 public static void setUserRequired(boolean userRequired) {
Andreas Gamped281b422016-07-08 03:50:27 +0000997 sUserRequired = userRequired;
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700998 }
999
1000 private static void throwIfUserRequired() {
Andreas Gamped281b422016-07-08 03:50:27 +00001001 if (sUserRequired) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -07001002 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
1003 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001004 }
1005 }
1006
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001007 /**
1008 * Append path segments to each given base path, returning result.
1009 *
1010 * @hide
1011 */
1012 public static File[] buildPaths(File[] base, String... segments) {
1013 File[] result = new File[base.length];
1014 for (int i = 0; i < base.length; i++) {
1015 result[i] = buildPath(base[i], segments);
1016 }
1017 return result;
1018 }
1019
1020 /**
1021 * Append path segments to given base path, returning result.
1022 *
1023 * @hide
1024 */
1025 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -07001026 File cur = base;
1027 for (String segment : segments) {
1028 if (cur == null) {
1029 cur = new File(segment);
1030 } else {
1031 cur = new File(cur, segment);
1032 }
1033 }
1034 return cur;
1035 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001036
Jeff Sharkey4ca728c2014-01-10 16:27:19 -08001037
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001038 /**
1039 * If the given path exists on emulated external storage, return the
1040 * translated backing path hosted on internal storage. This bypasses any
1041 * emulation later, improving performance. This is <em>only</em> suitable
1042 * for read-only access.
1043 * <p>
1044 * Returns original path if given path doesn't meet these criteria. Callers
1045 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
1046 * permission.
1047 *
1048 * @hide
1049 */
1050 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07001051 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -08001052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053}