blob: 894bfc904bb6151becc53cc392b56dc044b0afbc [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;
27
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028/**
29 * Provides access to environment variables.
30 */
31public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080032 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Jeff Sharkeyb049e212012-09-07 23:16:01 -070034 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080035 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey48877892015-03-18 11:27:19 -070036 private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
37 private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
Jeff Sharkey15447792015-11-05 16:18:51 -080038 private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080039 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080040 private static final String ENV_ODM_ROOT = "ODM_ROOT";
Christopher Tate740888f2014-04-18 12:24:57 -070041 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070042
Jeff Sharkeydfa45302012-09-12 16:25:22 -070043 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070044 public static final String DIR_ANDROID = "Android";
45 private static final String DIR_DATA = "data";
46 private static final String DIR_MEDIA = "media";
47 private static final String DIR_OBB = "obb";
48 private static final String DIR_FILES = "files";
49 private static final String DIR_CACHE = "cache";
50
51 /** {@hide} */
52 @Deprecated
53 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070054
Jeff Sharkey63d0a062013-03-01 16:12:55 -080055 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey48877892015-03-18 11:27:19 -070056 private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
57 private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
Jeff Sharkey15447792015-11-05 16:18:51 -080058 private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080059 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080060 private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
Christopher Tate740888f2014-04-18 12:24:57 -070061 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Jeff Sharkeyb049e212012-09-07 23:16:01 -070063 private static UserEnvironment sCurrentUser;
Jeff Sharkey48749fc2013-04-19 13:25:04 -070064 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070065
Jeff Sharkeyb049e212012-09-07 23:16:01 -070066 static {
67 initForCurrentUser();
68 }
69
70 /** {@hide} */
71 public static void initForCurrentUser() {
72 final int userId = UserHandle.myUserId();
73 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070074 }
75
76 /** {@hide} */
77 public static class UserEnvironment {
Jeff Sharkey48877892015-03-18 11:27:19 -070078 private final int mUserId;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070079
80 public UserEnvironment(int userId) {
Jeff Sharkey48877892015-03-18 11:27:19 -070081 mUserId = userId;
82 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070083
Jeff Sharkey48877892015-03-18 11:27:19 -070084 public File[] getExternalDirs() {
Jeff Sharkey46349872015-07-28 10:49:47 -070085 final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,
86 StorageManager.FLAG_FOR_WRITE);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070087 final File[] files = new File[volumes.length];
Jeff Sharkey48877892015-03-18 11:27:19 -070088 for (int i = 0; i < volumes.length; i++) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070089 files[i] = volumes[i].getPathFile();
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070090 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070091 return files;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070092 }
93
Jeff Sharkey1abdb712013-08-11 16:28:14 -070094 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -070095 public File getExternalStorageDirectory() {
Jeff Sharkey48877892015-03-18 11:27:19 -070096 return getExternalDirs()[0];
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 getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700101 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700102 }
103
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700104 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700105 return buildPaths(getExternalDirs(), type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700106 }
107
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700108 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700109 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700110 }
111
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700112 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700113 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114 }
115
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700116 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700117 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700118 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700119
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700120 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700121 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700122 }
123
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700124 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700125 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700126 }
127
128 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700129 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700130 }
131
132 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700133 return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700134 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700135 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800138 * Return root of the "system" partition holding the core Android OS.
139 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 */
141 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800142 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 }
144
Jeff Sharkey48877892015-03-18 11:27:19 -0700145 /** {@hide} */
146 public static File getStorageDirectory() {
147 return DIR_ANDROID_STORAGE;
148 }
149
Jason parksa3cdaa52011-01-13 14:15:43 -0600150 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800151 * Return root directory of the "oem" partition holding OEM customizations,
152 * if any. If present, the partition is mounted read-only.
153 *
154 * @hide
155 */
156 public static File getOemDirectory() {
157 return DIR_OEM_ROOT;
158 }
159
160 /**
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800161 * Return root directory of the "odm" partition holding ODM customizations,
162 * if any. If present, the partition is mounted read-only.
163 *
164 * @hide
165 */
166 public static File getOdmDirectory() {
167 return DIR_ODM_ROOT;
168 }
169
170 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700171 * Return root directory of the "vendor" partition that holds vendor-provided
172 * software that should persist across simple reflashing of the "system" partition.
173 * @hide
174 */
175 public static File getVendorDirectory() {
176 return DIR_VENDOR_ROOT;
177 }
178
Jason parksa3cdaa52011-01-13 14:15:43 -0600179 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700180 * Return the system directory for a user. This is for use by system
181 * services to store files relating to the user. This directory will be
182 * automatically deleted when the user is removed.
Amith Yamasani61f57372012-08-31 12:12:28 -0700183 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700184 * @deprecated This directory is valid and still exists, but callers should
185 * <em>strongly</em> consider switching to
186 * {@link #getDataSystemCeDirectory(int)} which is protected
187 * with user credentials or
188 * {@link #getDataSystemDeDirectory(int)} which supports fast
189 * user wipe.
Amith Yamasani61f57372012-08-31 12:12:28 -0700190 * @hide
191 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700192 @Deprecated
Amith Yamasani61f57372012-08-31 12:12:28 -0700193 public static File getUserSystemDirectory(int userId) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800194 return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId));
Amith Yamasani61f57372012-08-31 12:12:28 -0700195 }
196
197 /**
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700198 * Returns the config directory for a user. This is for use by system
199 * services to store files relating to the user which should be readable by
200 * any app running as that user.
Robin Lee69591332014-04-28 16:03:22 +0100201 *
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700202 * @deprecated This directory is valid and still exists, but callers should
203 * <em>strongly</em> consider switching to
204 * {@link #getDataMiscCeDirectory(int)} which is protected with
205 * user credentials or {@link #getDataMiscDeDirectory(int)}
206 * which supports fast user wipe.
Robin Lee69591332014-04-28 16:03:22 +0100207 * @hide
208 */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700209 @Deprecated
Robin Lee69591332014-04-28 16:03:22 +0100210 public static File getUserConfigDirectory(int userId) {
211 return new File(new File(new File(
212 getDataDirectory(), "misc"), "user"), Integer.toString(userId));
213 }
214
215 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700216 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 */
218 public static File getDataDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800219 return DIR_ANDROID_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
221
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700222 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700223 public static File getDataDirectory(String volumeUuid) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700224 if (TextUtils.isEmpty(volumeUuid)) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800225 return DIR_ANDROID_DATA;
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700226 } else {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700227 return new File("/mnt/expand/" + volumeUuid);
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -0700228 }
229 }
230
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700231 /** {@hide} */
Jeff Sharkey15447792015-11-05 16:18:51 -0800232 public static File getDataSystemDirectory() {
233 return new File(getDataDirectory(), "system");
234 }
235
236 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700237 public static File getDataSystemCeDirectory(int userId) {
238 return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800239 }
240
241 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700242 public static File getDataSystemDeDirectory(int userId) {
243 return buildPath(getDataDirectory(), "system_de", String.valueOf(userId));
244 }
245
246 /** {@hide} */
247 public static File getDataMiscDirectory() {
248 return new File(getDataDirectory(), "misc");
249 }
250
251 /** {@hide} */
252 public static File getDataMiscCeDirectory(int userId) {
253 return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId));
254 }
255
256 /** {@hide} */
257 public static File getDataMiscDeDirectory(int userId) {
258 return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800259 }
260
Calin Juravled479b522016-02-24 16:22:03 +0000261 private static File getDataProfilesDeDirectory(int userId) {
262 return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
263 }
264
265 /** {@hide} */
266 public static File getDataProfilesDePackageDirectory(int userId, String packageName) {
267 return buildPath(getDataProfilesDeDirectory(userId), packageName);
268 }
269
270 /** {@hide} */
271 public static File getDataProfilesDeForeignDexDirectory(int userId) {
272 return buildPath(getDataProfilesDeDirectory(userId), "foreign-dex");
273 }
274
Jeff Sharkey15447792015-11-05 16:18:51 -0800275 /** {@hide} */
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700276 public static File getDataAppDirectory(String volumeUuid) {
277 return new File(getDataDirectory(volumeUuid), "app");
278 }
279
280 /** {@hide} */
Todd Kennedy2699f062015-11-20 13:07:17 -0800281 public static File getDataAppEphemeralDirectory(String volumeUuid) {
282 return new File(getDataDirectory(volumeUuid), "app-ephemeral");
283 }
284
285 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700286 public static File getDataUserCeDirectory(String volumeUuid) {
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700287 return new File(getDataDirectory(volumeUuid), "user");
288 }
289
290 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700291 public static File getDataUserCeDirectory(String volumeUuid, int userId) {
292 return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700293 }
294
295 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700296 public static File getDataUserCePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700297 String packageName) {
298 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700299 return new File(getDataUserCeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey15447792015-11-05 16:18:51 -0800300 }
301
302 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700303 public static File getDataUserDeDirectory(String volumeUuid) {
Jeff Sharkey15447792015-11-05 16:18:51 -0800304 return new File(getDataDirectory(volumeUuid), "user_de");
305 }
306
307 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700308 public static File getDataUserDeDirectory(String volumeUuid, int userId) {
309 return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId));
Jeff Sharkey15447792015-11-05 16:18:51 -0800310 }
311
312 /** {@hide} */
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700313 public static File getDataUserDePackageDirectory(String volumeUuid, int userId,
Jeff Sharkey15447792015-11-05 16:18:51 -0800314 String packageName) {
315 // TODO: keep consistent with installd
Jeff Sharkey8212ae02016-02-10 14:46:43 -0700316 return new File(getDataUserDeDirectory(volumeUuid, userId), packageName);
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700317 }
318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700320 * Return the primary shared/external storage directory. This directory may
321 * not currently be accessible if it has been mounted by the user on their
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800322 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700323 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800324 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700325 * <p>
326 * <em>Note: don't be confused by the word "external" here. This directory
327 * can better be thought as media/shared storage. It is a filesystem that
328 * can hold a relatively large amount of data and that is shared across all
329 * applications (does not enforce permissions). Traditionally this is an SD
330 * card, but it may also be implemented as built-in storage in a device that
331 * is distinct from the protected internal storage and can be mounted as a
332 * filesystem on a computer.</em>
333 * <p>
334 * On devices with multiple users (as described by {@link UserManager}),
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700335 * each user has their own isolated shared storage. Applications only have
336 * access to the shared storage for the user they're running as.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700337 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700338 * In devices with multiple shared/external storage directories, this
339 * directory represents the primary storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700340 * with. Access to secondary storage is available through
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700341 * {@link Context#getExternalFilesDirs(String)},
342 * {@link Context#getExternalCacheDirs()}, and
343 * {@link Context#getExternalMediaDirs()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700344 * <p>
345 * Applications should not directly use this top-level directory, in order
346 * to avoid polluting the user's root namespace. Any files that are private
347 * to the application should be placed in a directory returned by
348 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700349 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700350 * if the application is uninstalled. Other shared files should be placed in
351 * one of the directories returned by
352 * {@link #getExternalStoragePublicDirectory}.
353 * <p>
354 * Writing to this path requires the
355 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
Felipe Leme04a5d402016-02-08 16:44:06 -0800356 * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700357 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700358 * which is automatically granted if you hold the write permission.
359 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700360 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700361 * application only needs to store internal data, consider using
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700362 * {@link Context#getExternalFilesDir(String)},
363 * {@link Context#getExternalCacheDir()}, or
364 * {@link Context#getExternalMediaDirs()}, which require no permissions to
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700365 * read or write.
366 * <p>
367 * This path may change between platform versions, so applications should
368 * only persist relative paths.
369 * <p>
370 * Here is an example of typical code to monitor the state of external
371 * storage:
372 * <p>
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700373 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800374 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700375 *
376 * @see #getExternalStorageState()
377 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 */
379 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700380 throwIfUserRequired();
Jeff Sharkey48877892015-03-18 11:27:19 -0700381 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700382 }
383
384 /** {@hide} */
385 public static File getLegacyExternalStorageDirectory() {
386 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
388
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700389 /** {@hide} */
390 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700391 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700392 }
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800395 * Standard directory in which to place any audio files that should be
396 * in the regular list of music for the user.
397 * This may be combined with
398 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
399 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
400 * of directories to categories a particular audio file as more than one
401 * type.
402 */
403 public static String DIRECTORY_MUSIC = "Music";
Felipe Lemec7b1f892016-01-15 15:02:31 -0800404
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800405 /**
406 * Standard directory in which to place any audio files that should be
407 * in the list of podcasts that the user can select (not as regular
408 * music).
409 * This may be combined with {@link #DIRECTORY_MUSIC},
410 * {@link #DIRECTORY_NOTIFICATIONS},
411 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
412 * of directories to categories a particular audio file as more than one
413 * type.
414 */
415 public static String DIRECTORY_PODCASTS = "Podcasts";
Felipe Lemeb012f912016-01-22 16:49:55 -0800416
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800417 /**
418 * Standard directory in which to place any audio files that should be
419 * in the list of ringtones that the user can select (not as regular
420 * music).
421 * This may be combined with {@link #DIRECTORY_MUSIC},
422 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
423 * {@link #DIRECTORY_ALARMS} as a series
424 * of directories to categories a particular audio file as more than one
425 * type.
426 */
427 public static String DIRECTORY_RINGTONES = "Ringtones";
Felipe Lemeb012f912016-01-22 16:49:55 -0800428
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800429 /**
430 * Standard directory in which to place any audio files that should be
431 * in the list of alarms that the user can select (not as regular
432 * music).
433 * This may be combined with {@link #DIRECTORY_MUSIC},
434 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
435 * and {@link #DIRECTORY_RINGTONES} as a series
436 * of directories to categories a particular audio file as more than one
437 * type.
438 */
439 public static String DIRECTORY_ALARMS = "Alarms";
Felipe Lemeb012f912016-01-22 16:49:55 -0800440
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800441 /**
442 * Standard directory in which to place any audio files that should be
443 * in the list of notifications that the user can select (not as regular
444 * music).
445 * This may be combined with {@link #DIRECTORY_MUSIC},
446 * {@link #DIRECTORY_PODCASTS},
447 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
448 * of directories to categories a particular audio file as more than one
449 * type.
450 */
451 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
Felipe Lemeb012f912016-01-22 16:49:55 -0800452
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800453 /**
454 * Standard directory in which to place pictures that are available to
455 * the user. Note that this is primarily a convention for the top-level
456 * public directory, as the media scanner will find and collect pictures
457 * in any directory.
458 */
459 public static String DIRECTORY_PICTURES = "Pictures";
Felipe Lemeb012f912016-01-22 16:49:55 -0800460
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800461 /**
462 * Standard directory in which to place movies that are available to
463 * the user. Note that this is primarily a convention for the top-level
464 * public directory, as the media scanner will find and collect movies
465 * in any directory.
466 */
467 public static String DIRECTORY_MOVIES = "Movies";
Felipe Lemeb012f912016-01-22 16:49:55 -0800468
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800469 /**
470 * Standard directory in which to place files that have been downloaded by
471 * the user. Note that this is primarily a convention for the top-level
472 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700473 * private directories. Also note that though the constant here is
474 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
475 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800476 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700477 public static String DIRECTORY_DOWNLOADS = "Download";
Felipe Lemeb012f912016-01-22 16:49:55 -0800478
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800479 /**
480 * The traditional location for pictures and videos when mounting the
481 * device as a camera. Note that this is primarily a convention for the
482 * top-level public directory, as this convention makes no sense elsewhere.
483 */
484 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700485
486 /**
487 * Standard directory in which to place documents that have been created by
488 * the user.
489 */
490 public static String DIRECTORY_DOCUMENTS = "Documents";
491
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800492 /**
Felipe Lemec7b1f892016-01-15 15:02:31 -0800493 * List of standard storage directories.
494 * <p>
495 * Each of its values have its own constant:
496 * <ul>
497 * <li>{@link #DIRECTORY_MUSIC}
498 * <li>{@link #DIRECTORY_PODCASTS}
499 * <li>{@link #DIRECTORY_ALARMS}
500 * <li>{@link #DIRECTORY_RINGTONES}
501 * <li>{@link #DIRECTORY_NOTIFICATIONS}
502 * <li>{@link #DIRECTORY_PICTURES}
503 * <li>{@link #DIRECTORY_MOVIES}
504 * <li>{@link #DIRECTORY_DOWNLOADS}
505 * <li>{@link #DIRECTORY_DCIM}
506 * <li>{@link #DIRECTORY_DOCUMENTS}
507 * </ul>
508 * @hide
509 */
Felipe Lemeb012f912016-01-22 16:49:55 -0800510 private static final String[] STANDARD_DIRECTORIES = {
Felipe Lemec7b1f892016-01-15 15:02:31 -0800511 DIRECTORY_MUSIC,
512 DIRECTORY_PODCASTS,
513 DIRECTORY_RINGTONES,
514 DIRECTORY_ALARMS,
515 DIRECTORY_NOTIFICATIONS,
516 DIRECTORY_PICTURES,
517 DIRECTORY_MOVIES,
518 DIRECTORY_DOWNLOADS,
519 DIRECTORY_DCIM,
Steve McKayab3b8932016-02-16 11:37:03 -0800520 DIRECTORY_DOCUMENTS
Felipe Lemec7b1f892016-01-15 15:02:31 -0800521 };
522
523 /**
Felipe Lemeb012f912016-01-22 16:49:55 -0800524 * @hide
525 */
526 public static boolean isStandardDirectory(String dir) {
527 for (String valid : STANDARD_DIRECTORIES) {
528 if (valid.equals(dir)) {
529 return true;
530 }
531 }
532 return false;
533 }
534
535 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700536 * Get a top-level shared/external storage directory for placing files of a
537 * particular type. This is where the user will typically place and manage
538 * their own files, so you should be careful about what you put here to
539 * ensure you don't erase their files or get in the way of their own
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800540 * organization.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700541 * <p>
542 * On devices with multiple users (as described by {@link UserManager}),
543 * each user has their own isolated shared storage. Applications only have
544 * access to the shared storage for the user they're running as.
545 * </p>
546 * <p>
547 * Here is an example of typical code to manipulate a picture on the public
548 * shared storage:
549 * </p>
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800550 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
551 * public_picture}
Felipe Lemec7b1f892016-01-15 15:02:31 -0800552 *
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700553 * @param type The type of storage directory to return. Should be one of
554 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
555 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
556 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
Felipe Lemec7b1f892016-01-15 15:02:31 -0800557 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS},
558 * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null.
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700559 * @return Returns the File path for the directory. Note that this directory
560 * may not yet exist, so you must make sure it exists before using
561 * it such as with {@link File#mkdirs File.mkdirs()}.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800562 */
563 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700564 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700565 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800566 }
567
568 /**
569 * Returns the path for android-specific data on the SD card.
570 * @hide
571 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700572 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700573 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700574 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800575 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700576
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800577 /**
578 * Generates the raw path to an application's data
579 * @hide
580 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700581 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700582 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700583 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800584 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800585
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800586 /**
587 * Generates the raw path to an application's media
588 * @hide
589 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700590 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700591 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700592 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800593 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800594
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800595 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800596 * Generates the raw path to an application's OBB files
597 * @hide
598 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700599 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700600 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700601 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800602 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800603
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800604 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800605 * Generates the path to an application's files.
606 * @hide
607 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700608 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700609 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700610 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800611 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700612
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800613 /**
614 * Generates the path to an application's cache.
615 * @hide
616 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700617 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700618 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700619 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800620 }
Felipe Lemeb012f912016-01-22 16:49:55 -0800621
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800622 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700623 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 */
625 public static File getDownloadCacheDirectory() {
Jeff Sharkey15447792015-11-05 16:18:51 -0800626 return DIR_DOWNLOAD_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628
629 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700630 * Unknown storage state, such as when a path isn't backed by known storage
631 * media.
632 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800633 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700634 */
635 public static final String MEDIA_UNKNOWN = "unknown";
636
637 /**
638 * Storage state if the media is not present.
639 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800640 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 */
642 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700645 * Storage state if the media is present but not mounted.
646 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800647 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 */
649 public static final String MEDIA_UNMOUNTED = "unmounted";
650
651 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700652 * Storage state if the media is present and being disk-checked.
653 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800654 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 */
656 public static final String MEDIA_CHECKING = "checking";
657
658 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700659 * Storage state if the media is present but is blank or is using an
660 * unsupported filesystem.
661 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800662 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 */
664 public static final String MEDIA_NOFS = "nofs";
665
666 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700667 * Storage state if the media is present and mounted at its mount point with
668 * read/write access.
669 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800670 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 */
672 public static final String MEDIA_MOUNTED = "mounted";
673
674 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700675 * Storage state if the media is present and mounted at its mount point with
676 * read-only access.
677 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800678 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
680 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
681
682 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700683 * Storage state if the media is present not mounted, and shared via USB
684 * mass storage.
685 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800686 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 */
688 public static final String MEDIA_SHARED = "shared";
689
690 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700691 * Storage state if the media was removed before it was unmounted.
692 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800693 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 */
695 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
696
697 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700698 * Storage state if the media is present but cannot be mounted. Typically
699 * this happens if the file system on the media is corrupted.
700 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800701 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 */
703 public static final String MEDIA_UNMOUNTABLE = "unmountable";
704
705 /**
Jeff Sharkey48877892015-03-18 11:27:19 -0700706 * Storage state if the media is in the process of being ejected.
707 *
708 * @see #getExternalStorageState(File)
709 */
710 public static final String MEDIA_EJECTING = "ejecting";
711
712 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700713 * Returns the current state of the primary shared/external storage media.
Felipe Lemec7b1f892016-01-15 15:02:31 -0800714 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700715 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700716 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
717 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
718 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
719 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
720 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 */
722 public static String getExternalStorageState() {
Jeff Sharkey48877892015-03-18 11:27:19 -0700723 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800724 return getExternalStorageState(externalDir);
725 }
726
727 /**
728 * @deprecated use {@link #getExternalStorageState(File)}
729 */
730 @Deprecated
731 public static String getStorageState(File path) {
732 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700733 }
734
735 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700736 * Returns the current state of the shared/external storage media at the
737 * given path.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700738 *
739 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
740 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
741 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
742 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
743 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
744 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800745 public static String getExternalStorageState(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700746 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800747 if (volume != null) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700748 return volume.getState();
749 } else {
750 return MEDIA_UNKNOWN;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753
Dianne Hackborn407f6252010-10-04 11:31:17 -0700754 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700755 * Returns whether the primary shared/external storage media is physically
756 * removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700757 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800758 * @return true if the storage device can be removed (such as an SD card),
759 * or false if the storage device is built in and cannot be
760 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700761 */
762 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800763 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700764 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800765 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700766 }
767
Kenny Roote1ff2142010-10-12 11:20:01 -0700768 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700769 * Returns whether the shared/external storage media at the given path is
770 * physically removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800771 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800772 * @return true if the storage device can be removed (such as an SD card),
773 * or false if the storage device is built in and cannot be
774 * physically removed.
775 * @throws IllegalArgumentException if the path is not a valid storage
776 * device.
777 */
778 public static boolean isExternalStorageRemovable(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700779 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800780 if (volume != null) {
781 return volume.isRemovable();
782 } else {
783 throw new IllegalArgumentException("Failed to find storage device at " + path);
784 }
785 }
786
787 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700788 * Returns whether the primary shared/external storage media is emulated.
789 * <p>
790 * The contents of emulated storage devices are backed by a private user
791 * data partition, which means there is little benefit to apps storing data
792 * here instead of the private directories returned by
793 * {@link Context#getFilesDir()}, etc.
794 * <p>
795 * This returns true when emulated storage is backed by either internal
796 * storage or an adopted storage device.
Andy Stadler50c294f2011-03-07 19:13:42 -0800797 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800798 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
799 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700800 */
801 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800802 if (isStorageDisabled()) return false;
Jeff Sharkey48877892015-03-18 11:27:19 -0700803 final File externalDir = sCurrentUser.getExternalDirs()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800804 return isExternalStorageEmulated(externalDir);
805 }
806
807 /**
Jeff Sharkey59d28dc82015-10-14 13:56:23 -0700808 * Returns whether the shared/external storage media at the given path is
809 * emulated.
810 * <p>
811 * The contents of emulated storage devices are backed by a private user
812 * data partition, which means there is little benefit to apps storing data
813 * here instead of the private directories returned by
814 * {@link Context#getFilesDir()}, etc.
815 * <p>
816 * This returns true when emulated storage is backed by either internal
817 * storage or an adopted storage device.
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800818 *
819 * @throws IllegalArgumentException if the path is not a valid storage
820 * device.
821 */
822 public static boolean isExternalStorageEmulated(File path) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700823 final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId());
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800824 if (volume != null) {
825 return volume.isEmulated();
826 } else {
827 throw new IllegalArgumentException("Failed to find storage device at " + path);
828 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700829 }
830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 static File getDirectory(String variableName, String defaultPath) {
832 String path = System.getenv(variableName);
833 return path == null ? new File(defaultPath) : new File(path);
834 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700835
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700836 /** {@hide} */
837 public static void setUserRequired(boolean userRequired) {
838 sUserRequired = userRequired;
839 }
840
841 private static void throwIfUserRequired() {
842 if (sUserRequired) {
843 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
844 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700845 }
846 }
847
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700848 /**
849 * Append path segments to each given base path, returning result.
850 *
851 * @hide
852 */
853 public static File[] buildPaths(File[] base, String... segments) {
854 File[] result = new File[base.length];
855 for (int i = 0; i < base.length; i++) {
856 result[i] = buildPath(base[i], segments);
857 }
858 return result;
859 }
860
861 /**
862 * Append path segments to given base path, returning result.
863 *
864 * @hide
865 */
866 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700867 File cur = base;
868 for (String segment : segments) {
869 if (cur == null) {
870 cur = new File(segment);
871 } else {
872 cur = new File(cur, segment);
873 }
874 }
875 return cur;
876 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800877
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800878 private static boolean isStorageDisabled() {
879 return SystemProperties.getBoolean("config.disable_storage", false);
880 }
881
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800882 /**
883 * If the given path exists on emulated external storage, return the
884 * translated backing path hosted on internal storage. This bypasses any
885 * emulation later, improving performance. This is <em>only</em> suitable
886 * for read-only access.
887 * <p>
888 * Returns original path if given path doesn't meet these criteria. Callers
889 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
890 * permission.
891 *
892 * @hide
893 */
894 public static File maybeTranslateEmulatedPathToInternal(File path) {
Jeff Sharkey50a05452015-04-29 11:24:52 -0700895 return StorageManager.maybeTranslateEmulatedPathToInternal(path);
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897}