The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 19 | import android.app.admin.DevicePolicyManager; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 21 | import android.os.storage.StorageManager; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 22 | import android.os.storage.StorageVolume; |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame^] | 23 | import android.text.TextUtils; |
Kenny Root | b2278dc | 2011-01-18 13:03:28 -0800 | [diff] [blame] | 24 | import android.util.Log; |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 25 | |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 26 | import java.io.File; |
| 27 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Provides access to environment variables. |
| 30 | */ |
| 31 | public class Environment { |
Kenny Root | b2278dc | 2011-01-18 13:03:28 -0800 | [diff] [blame] | 32 | private static final String TAG = "Environment"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 34 | private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE"; |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 35 | private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT"; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 36 | private static final String ENV_ANDROID_DATA = "ANDROID_DATA"; |
| 37 | private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE"; |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 38 | private static final String ENV_OEM_ROOT = "OEM_ROOT"; |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 39 | private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT"; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 40 | |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame] | 41 | /** {@hide} */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 42 | public static final String DIR_ANDROID = "Android"; |
| 43 | private static final String DIR_DATA = "data"; |
| 44 | private static final String DIR_MEDIA = "media"; |
| 45 | private static final String DIR_OBB = "obb"; |
| 46 | private static final String DIR_FILES = "files"; |
| 47 | private static final String DIR_CACHE = "cache"; |
| 48 | |
| 49 | /** {@hide} */ |
| 50 | @Deprecated |
| 51 | public static final String DIRECTORY_ANDROID = DIR_ANDROID; |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame] | 52 | |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 53 | private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 54 | private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data"); |
| 55 | private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage"); |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 56 | private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem"); |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 57 | private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 59 | private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled"; |
| 60 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 61 | private static UserEnvironment sCurrentUser; |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 62 | private static boolean sUserRequired; |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 63 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 64 | static { |
| 65 | initForCurrentUser(); |
| 66 | } |
| 67 | |
| 68 | /** {@hide} */ |
| 69 | public static void initForCurrentUser() { |
| 70 | final int userId = UserHandle.myUserId(); |
| 71 | sCurrentUser = new UserEnvironment(userId); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** {@hide} */ |
| 75 | public static class UserEnvironment { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 76 | private final int mUserId; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 77 | |
| 78 | public UserEnvironment(int userId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 79 | mUserId = userId; |
| 80 | } |
Jeff Sharkey | 44cbdec | 2013-10-07 16:49:47 -0700 | [diff] [blame] | 81 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 82 | public File[] getExternalDirs() { |
| 83 | final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 84 | final File[] files = new File[volumes.length]; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 85 | for (int i = 0; i < volumes.length; i++) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 86 | files[i] = volumes[i].getPathFile(); |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 87 | } |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 88 | return files; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 91 | @Deprecated |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 92 | public File getExternalStorageDirectory() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 93 | return getExternalDirs()[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 96 | @Deprecated |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 97 | public File getExternalStoragePublicDirectory(String type) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 98 | return buildExternalStoragePublicDirs(type)[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 101 | public File[] buildExternalStoragePublicDirs(String type) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 102 | return buildPaths(getExternalDirs(), type); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 105 | public File[] buildExternalStorageAndroidDataDirs() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 106 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 109 | public File[] buildExternalStorageAndroidObbDirs() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 110 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 113 | public File[] buildExternalStorageAppDataDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 114 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 115 | } |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 116 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 117 | public File[] buildExternalStorageAppMediaDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 118 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName); |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 121 | public File[] buildExternalStorageAppObbDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 122 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public File[] buildExternalStorageAppFilesDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 126 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public File[] buildExternalStorageAppCacheDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 130 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE); |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 131 | } |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 132 | } |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 133 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | /** |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 135 | * Return root of the "system" partition holding the core Android OS. |
| 136 | * Always present and mounted read-only. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | */ |
| 138 | public static File getRootDirectory() { |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 139 | return DIR_ANDROID_ROOT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 142 | /** {@hide} */ |
| 143 | public static File getStorageDirectory() { |
| 144 | return DIR_ANDROID_STORAGE; |
| 145 | } |
| 146 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 147 | /** |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 148 | * Return root directory of the "oem" partition holding OEM customizations, |
| 149 | * if any. If present, the partition is mounted read-only. |
| 150 | * |
| 151 | * @hide |
| 152 | */ |
| 153 | public static File getOemDirectory() { |
| 154 | return DIR_OEM_ROOT; |
| 155 | } |
| 156 | |
| 157 | /** |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 158 | * Return root directory of the "vendor" partition that holds vendor-provided |
| 159 | * software that should persist across simple reflashing of the "system" partition. |
| 160 | * @hide |
| 161 | */ |
| 162 | public static File getVendorDirectory() { |
| 163 | return DIR_VENDOR_ROOT; |
| 164 | } |
| 165 | |
| 166 | /** |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 167 | * Gets the system directory available for secure storage. |
| 168 | * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system). |
| 169 | * Otherwise, it returns the unencrypted /data/system directory. |
| 170 | * @return File object representing the secure storage system directory. |
| 171 | * @hide |
| 172 | */ |
| 173 | public static File getSystemSecureDirectory() { |
| 174 | if (isEncryptedFilesystemEnabled()) { |
| 175 | return new File(SECURE_DATA_DIRECTORY, "system"); |
| 176 | } else { |
| 177 | return new File(DATA_DIRECTORY, "system"); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Gets the data directory for secure storage. |
| 183 | * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure). |
| 184 | * Otherwise, it returns the unencrypted /data directory. |
| 185 | * @return File object representing the data directory for secure storage. |
| 186 | * @hide |
| 187 | */ |
| 188 | public static File getSecureDataDirectory() { |
| 189 | if (isEncryptedFilesystemEnabled()) { |
| 190 | return SECURE_DATA_DIRECTORY; |
| 191 | } else { |
| 192 | return DATA_DIRECTORY; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 197 | * Return the system directory for a user. This is for use by system services to store |
| 198 | * files relating to the user. This directory will be automatically deleted when the user |
| 199 | * is removed. |
| 200 | * |
| 201 | * @hide |
| 202 | */ |
| 203 | public static File getUserSystemDirectory(int userId) { |
| 204 | return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId)); |
| 205 | } |
| 206 | |
| 207 | /** |
Robin Lee | 6959133 | 2014-04-28 16:03:22 +0100 | [diff] [blame] | 208 | * Returns the config directory for a user. This is for use by system services to store files |
| 209 | * relating to the user which should be readable by any app running as that user. |
| 210 | * |
| 211 | * @hide |
| 212 | */ |
| 213 | public static File getUserConfigDirectory(int userId) { |
| 214 | return new File(new File(new File( |
| 215 | getDataDirectory(), "misc"), "user"), Integer.toString(userId)); |
| 216 | } |
| 217 | |
| 218 | /** |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 219 | * Returns whether the Encrypted File System feature is enabled on the device or not. |
| 220 | * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code> |
| 221 | * if disabled. |
| 222 | * @hide |
| 223 | */ |
| 224 | public static boolean isEncryptedFilesystemEnabled() { |
| 225 | return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false); |
| 226 | } |
| 227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | private static final File DATA_DIRECTORY |
| 229 | = getDirectory("ANDROID_DATA", "/data"); |
| 230 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 231 | /** |
| 232 | * @hide |
| 233 | */ |
| 234 | private static final File SECURE_DATA_DIRECTORY |
| 235 | = getDirectory("ANDROID_SECURE_DATA", "/data/secure"); |
| 236 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 237 | private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | |
| 239 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 240 | * Return the user data directory. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | */ |
| 242 | public static File getDataDirectory() { |
| 243 | return DATA_DIRECTORY; |
| 244 | } |
| 245 | |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame^] | 246 | /** {@hide} */ |
| 247 | public static File getDataAppDirectory(String volumeUuid) { |
| 248 | if (TextUtils.isEmpty(volumeUuid)) { |
| 249 | return new File("/data/app"); |
| 250 | } else { |
| 251 | return new File("/mnt/expand/" + volumeUuid + "/app"); |
| 252 | } |
| 253 | } |
| 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 256 | * Return the primary external storage directory. This directory may not |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 257 | * currently be accessible if it has been mounted by the user on their |
| 258 | * computer, has been removed from the device, or some other problem has |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 259 | * happened. You can determine its current state with |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 260 | * {@link #getExternalStorageState()}. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 261 | * <p> |
| 262 | * <em>Note: don't be confused by the word "external" here. This directory |
| 263 | * can better be thought as media/shared storage. It is a filesystem that |
| 264 | * can hold a relatively large amount of data and that is shared across all |
| 265 | * applications (does not enforce permissions). Traditionally this is an SD |
| 266 | * card, but it may also be implemented as built-in storage in a device that |
| 267 | * is distinct from the protected internal storage and can be mounted as a |
| 268 | * filesystem on a computer.</em> |
| 269 | * <p> |
| 270 | * On devices with multiple users (as described by {@link UserManager}), |
| 271 | * each user has their own isolated external storage. Applications only have |
| 272 | * access to the external storage for the user they're running as. |
| 273 | * <p> |
| 274 | * In devices with multiple "external" storage directories, this directory |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 275 | * represents the "primary" external storage that the user will interact |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 276 | * with. Access to secondary storage is available through |
| 277 | * <p> |
| 278 | * Applications should not directly use this top-level directory, in order |
| 279 | * to avoid polluting the user's root namespace. Any files that are private |
| 280 | * to the application should be placed in a directory returned by |
| 281 | * {@link android.content.Context#getExternalFilesDir |
Dianne Hackborn | acaf028 | 2010-03-30 14:39:35 -0700 | [diff] [blame] | 282 | * Context.getExternalFilesDir}, which the system will take care of deleting |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 283 | * if the application is uninstalled. Other shared files should be placed in |
| 284 | * one of the directories returned by |
| 285 | * {@link #getExternalStoragePublicDirectory}. |
| 286 | * <p> |
| 287 | * Writing to this path requires the |
| 288 | * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission, |
| 289 | * and starting in read access requires the |
Jeff Sharkey | 8c16579 | 2012-10-22 14:08:29 -0700 | [diff] [blame] | 290 | * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission, |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 291 | * which is automatically granted if you hold the write permission. |
| 292 | * <p> |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 293 | * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 294 | * application only needs to store internal data, consider using |
| 295 | * {@link Context#getExternalFilesDir(String)} or |
| 296 | * {@link Context#getExternalCacheDir()}, which require no permissions to |
| 297 | * read or write. |
| 298 | * <p> |
| 299 | * This path may change between platform versions, so applications should |
| 300 | * only persist relative paths. |
| 301 | * <p> |
| 302 | * Here is an example of typical code to monitor the state of external |
| 303 | * storage: |
| 304 | * <p> |
| 305 | * {@sample |
| 306 | * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 307 | * monitor_storage} |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 308 | * |
| 309 | * @see #getExternalStorageState() |
| 310 | * @see #isExternalStorageRemovable() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | */ |
| 312 | public static File getExternalStorageDirectory() { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 313 | throwIfUserRequired(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 314 | return sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** {@hide} */ |
| 318 | public static File getLegacyExternalStorageDirectory() { |
| 319 | return new File(System.getenv(ENV_EXTERNAL_STORAGE)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 322 | /** {@hide} */ |
| 323 | public static File getLegacyExternalStorageObbDirectory() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 324 | return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 325 | } |
| 326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 328 | * Standard directory in which to place any audio files that should be |
| 329 | * in the regular list of music for the user. |
| 330 | * This may be combined with |
| 331 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, |
| 332 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 333 | * of directories to categories a particular audio file as more than one |
| 334 | * type. |
| 335 | */ |
| 336 | public static String DIRECTORY_MUSIC = "Music"; |
| 337 | |
| 338 | /** |
| 339 | * Standard directory in which to place any audio files that should be |
| 340 | * in the list of podcasts that the user can select (not as regular |
| 341 | * music). |
| 342 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 343 | * {@link #DIRECTORY_NOTIFICATIONS}, |
| 344 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 345 | * of directories to categories a particular audio file as more than one |
| 346 | * type. |
| 347 | */ |
| 348 | public static String DIRECTORY_PODCASTS = "Podcasts"; |
| 349 | |
| 350 | /** |
| 351 | * Standard directory in which to place any audio files that should be |
| 352 | * in the list of ringtones that the user can select (not as regular |
| 353 | * music). |
| 354 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 355 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and |
| 356 | * {@link #DIRECTORY_ALARMS} as a series |
| 357 | * of directories to categories a particular audio file as more than one |
| 358 | * type. |
| 359 | */ |
| 360 | public static String DIRECTORY_RINGTONES = "Ringtones"; |
| 361 | |
| 362 | /** |
| 363 | * Standard directory in which to place any audio files that should be |
| 364 | * in the list of alarms that the user can select (not as regular |
| 365 | * music). |
| 366 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 367 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, |
| 368 | * and {@link #DIRECTORY_RINGTONES} as a series |
| 369 | * of directories to categories a particular audio file as more than one |
| 370 | * type. |
| 371 | */ |
| 372 | public static String DIRECTORY_ALARMS = "Alarms"; |
| 373 | |
| 374 | /** |
| 375 | * Standard directory in which to place any audio files that should be |
| 376 | * in the list of notifications that the user can select (not as regular |
| 377 | * music). |
| 378 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 379 | * {@link #DIRECTORY_PODCASTS}, |
| 380 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 381 | * of directories to categories a particular audio file as more than one |
| 382 | * type. |
| 383 | */ |
| 384 | public static String DIRECTORY_NOTIFICATIONS = "Notifications"; |
| 385 | |
| 386 | /** |
| 387 | * Standard directory in which to place pictures that are available to |
| 388 | * the user. Note that this is primarily a convention for the top-level |
| 389 | * public directory, as the media scanner will find and collect pictures |
| 390 | * in any directory. |
| 391 | */ |
| 392 | public static String DIRECTORY_PICTURES = "Pictures"; |
| 393 | |
| 394 | /** |
| 395 | * Standard directory in which to place movies that are available to |
| 396 | * the user. Note that this is primarily a convention for the top-level |
| 397 | * public directory, as the media scanner will find and collect movies |
| 398 | * in any directory. |
| 399 | */ |
| 400 | public static String DIRECTORY_MOVIES = "Movies"; |
| 401 | |
| 402 | /** |
| 403 | * Standard directory in which to place files that have been downloaded by |
| 404 | * the user. Note that this is primarily a convention for the top-level |
| 405 | * public directory, you are free to download files anywhere in your own |
Dianne Hackborn | ce59fb8 | 2010-04-07 17:19:04 -0700 | [diff] [blame] | 406 | * private directories. Also note that though the constant here is |
| 407 | * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for |
| 408 | * backwards compatibility reasons. |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 409 | */ |
Dianne Hackborn | ce59fb8 | 2010-04-07 17:19:04 -0700 | [diff] [blame] | 410 | public static String DIRECTORY_DOWNLOADS = "Download"; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 411 | |
| 412 | /** |
| 413 | * The traditional location for pictures and videos when mounting the |
| 414 | * device as a camera. Note that this is primarily a convention for the |
| 415 | * top-level public directory, as this convention makes no sense elsewhere. |
| 416 | */ |
| 417 | public static String DIRECTORY_DCIM = "DCIM"; |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 418 | |
| 419 | /** |
| 420 | * Standard directory in which to place documents that have been created by |
| 421 | * the user. |
| 422 | */ |
| 423 | public static String DIRECTORY_DOCUMENTS = "Documents"; |
| 424 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 425 | /** |
| 426 | * Get a top-level public external storage directory for placing files of |
| 427 | * a particular type. This is where the user will typically place and |
| 428 | * manage their own files, so you should be careful about what you put here |
| 429 | * to ensure you don't erase their files or get in the way of their own |
| 430 | * organization. |
| 431 | * |
Jeff Sharkey | 8c16579 | 2012-10-22 14:08:29 -0700 | [diff] [blame] | 432 | * <p>On devices with multiple users (as described by {@link UserManager}), |
| 433 | * each user has their own isolated external storage. Applications only |
| 434 | * have access to the external storage for the user they're running as.</p> |
| 435 | * |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 436 | * <p>Here is an example of typical code to manipulate a picture on |
| 437 | * the public external storage:</p> |
| 438 | * |
| 439 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java |
| 440 | * public_picture} |
| 441 | * |
| 442 | * @param type The type of storage directory to return. Should be one of |
| 443 | * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS}, |
| 444 | * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS}, |
| 445 | * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES}, |
| 446 | * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or |
| 447 | * {@link #DIRECTORY_DCIM}. May not be null. |
| 448 | * |
| 449 | * @return Returns the File path for the directory. Note that this |
| 450 | * directory may not yet exist, so you must make sure it exists before |
| 451 | * using it such as with {@link File#mkdirs File.mkdirs()}. |
| 452 | */ |
| 453 | public static File getExternalStoragePublicDirectory(String type) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 454 | throwIfUserRequired(); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 455 | return sCurrentUser.buildExternalStoragePublicDirs(type)[0]; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Returns the path for android-specific data on the SD card. |
| 460 | * @hide |
| 461 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 462 | public static File[] buildExternalStorageAndroidDataDirs() { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 463 | throwIfUserRequired(); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 464 | return sCurrentUser.buildExternalStorageAndroidDataDirs(); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 465 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 466 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 467 | /** |
| 468 | * Generates the raw path to an application's data |
| 469 | * @hide |
| 470 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 471 | public static File[] buildExternalStorageAppDataDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 472 | throwIfUserRequired(); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 473 | return sCurrentUser.buildExternalStorageAppDataDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Generates the raw path to an application's media |
| 478 | * @hide |
| 479 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 480 | public static File[] buildExternalStorageAppMediaDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 481 | throwIfUserRequired(); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 482 | return sCurrentUser.buildExternalStorageAppMediaDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /** |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 486 | * Generates the raw path to an application's OBB files |
| 487 | * @hide |
| 488 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 489 | public static File[] buildExternalStorageAppObbDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 490 | throwIfUserRequired(); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 491 | return sCurrentUser.buildExternalStorageAppObbDirs(packageName); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 495 | * Generates the path to an application's files. |
| 496 | * @hide |
| 497 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 498 | public static File[] buildExternalStorageAppFilesDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 499 | throwIfUserRequired(); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 500 | return sCurrentUser.buildExternalStorageAppFilesDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 501 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 502 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 503 | /** |
| 504 | * Generates the path to an application's cache. |
| 505 | * @hide |
| 506 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 507 | public static File[] buildExternalStorageAppCacheDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 508 | throwIfUserRequired(); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 509 | return sCurrentUser.buildExternalStorageAppCacheDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 513 | * Return the download/cache content directory. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | */ |
| 515 | public static File getDownloadCacheDirectory() { |
| 516 | return DOWNLOAD_CACHE_DIRECTORY; |
| 517 | } |
| 518 | |
| 519 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 520 | * Unknown storage state, such as when a path isn't backed by known storage |
| 521 | * media. |
| 522 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 523 | * @see #getExternalStorageState(File) |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 524 | */ |
| 525 | public static final String MEDIA_UNKNOWN = "unknown"; |
| 526 | |
| 527 | /** |
| 528 | * Storage state if the media is not present. |
| 529 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 530 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | */ |
| 532 | public static final String MEDIA_REMOVED = "removed"; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 535 | * Storage state if the media is present but not mounted. |
| 536 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 537 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | */ |
| 539 | public static final String MEDIA_UNMOUNTED = "unmounted"; |
| 540 | |
| 541 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 542 | * Storage state if the media is present and being disk-checked. |
| 543 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 544 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | */ |
| 546 | public static final String MEDIA_CHECKING = "checking"; |
| 547 | |
| 548 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 549 | * Storage state if the media is present but is blank or is using an |
| 550 | * unsupported filesystem. |
| 551 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 552 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | */ |
| 554 | public static final String MEDIA_NOFS = "nofs"; |
| 555 | |
| 556 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 557 | * Storage state if the media is present and mounted at its mount point with |
| 558 | * read/write access. |
| 559 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 560 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | */ |
| 562 | public static final String MEDIA_MOUNTED = "mounted"; |
| 563 | |
| 564 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 565 | * Storage state if the media is present and mounted at its mount point with |
| 566 | * read-only access. |
| 567 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 568 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | */ |
| 570 | public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro"; |
| 571 | |
| 572 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 573 | * Storage state if the media is present not mounted, and shared via USB |
| 574 | * mass storage. |
| 575 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 576 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | */ |
| 578 | public static final String MEDIA_SHARED = "shared"; |
| 579 | |
| 580 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 581 | * Storage state if the media was removed before it was unmounted. |
| 582 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 583 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | */ |
| 585 | public static final String MEDIA_BAD_REMOVAL = "bad_removal"; |
| 586 | |
| 587 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 588 | * Storage state if the media is present but cannot be mounted. Typically |
| 589 | * this happens if the file system on the media is corrupted. |
| 590 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 591 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | */ |
| 593 | public static final String MEDIA_UNMOUNTABLE = "unmountable"; |
| 594 | |
| 595 | /** |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 596 | * Storage state if the media is in the process of being ejected. |
| 597 | * |
| 598 | * @see #getExternalStorageState(File) |
| 599 | */ |
| 600 | public static final String MEDIA_EJECTING = "ejecting"; |
| 601 | |
| 602 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 603 | * Returns the current state of the primary "external" storage device. |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 604 | * |
Jeff Sharkey | 8c16579 | 2012-10-22 14:08:29 -0700 | [diff] [blame] | 605 | * @see #getExternalStorageDirectory() |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 606 | * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED}, |
| 607 | * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING}, |
| 608 | * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED}, |
| 609 | * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED}, |
| 610 | * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | */ |
| 612 | public static String getExternalStorageState() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 613 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 614 | return getExternalStorageState(externalDir); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * @deprecated use {@link #getExternalStorageState(File)} |
| 619 | */ |
| 620 | @Deprecated |
| 621 | public static String getStorageState(File path) { |
| 622 | return getExternalStorageState(path); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Returns the current state of the storage device that provides the given |
| 627 | * path. |
| 628 | * |
| 629 | * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED}, |
| 630 | * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING}, |
| 631 | * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED}, |
| 632 | * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED}, |
| 633 | * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}. |
| 634 | */ |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 635 | public static String getExternalStorageState(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 636 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 637 | if (volume != null) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 638 | return volume.getState(); |
| 639 | } else { |
| 640 | return MEDIA_UNKNOWN; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 641 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Returns whether the primary "external" storage device is removable. |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 646 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 647 | * @return true if the storage device can be removed (such as an SD card), |
| 648 | * or false if the storage device is built in and cannot be |
| 649 | * physically removed. |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 650 | */ |
| 651 | public static boolean isExternalStorageRemovable() { |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 652 | if (isStorageDisabled()) return false; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 653 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 654 | return isExternalStorageRemovable(externalDir); |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 657 | /** |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 658 | * Returns whether the storage device that provides the given path is |
| 659 | * removable. |
Andy Stadler | 50c294f | 2011-03-07 19:13:42 -0800 | [diff] [blame] | 660 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 661 | * @return true if the storage device can be removed (such as an SD card), |
| 662 | * or false if the storage device is built in and cannot be |
| 663 | * physically removed. |
| 664 | * @throws IllegalArgumentException if the path is not a valid storage |
| 665 | * device. |
| 666 | */ |
| 667 | public static boolean isExternalStorageRemovable(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 668 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 669 | if (volume != null) { |
| 670 | return volume.isRemovable(); |
| 671 | } else { |
| 672 | throw new IllegalArgumentException("Failed to find storage device at " + path); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Returns whether the primary "external" storage device is emulated. If |
| 678 | * true, data stored on this device will be stored on a portion of the |
| 679 | * internal storage system. |
Andy Stadler | 50c294f | 2011-03-07 19:13:42 -0800 | [diff] [blame] | 680 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 681 | * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName, |
| 682 | * boolean) |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 683 | */ |
| 684 | public static boolean isExternalStorageEmulated() { |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 685 | if (isStorageDisabled()) return false; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 686 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 687 | return isExternalStorageEmulated(externalDir); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Returns whether the storage device that provides the given path is |
| 692 | * emulated. If true, data stored on this device will be stored on a portion |
| 693 | * of the internal storage system. |
| 694 | * |
| 695 | * @throws IllegalArgumentException if the path is not a valid storage |
| 696 | * device. |
| 697 | */ |
| 698 | public static boolean isExternalStorageEmulated(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 699 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 700 | if (volume != null) { |
| 701 | return volume.isEmulated(); |
| 702 | } else { |
| 703 | throw new IllegalArgumentException("Failed to find storage device at " + path); |
| 704 | } |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 705 | } |
| 706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | static File getDirectory(String variableName, String defaultPath) { |
| 708 | String path = System.getenv(variableName); |
| 709 | return path == null ? new File(defaultPath) : new File(path); |
| 710 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 711 | |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 712 | /** {@hide} */ |
| 713 | public static void setUserRequired(boolean userRequired) { |
| 714 | sUserRequired = userRequired; |
| 715 | } |
| 716 | |
| 717 | private static void throwIfUserRequired() { |
| 718 | if (sUserRequired) { |
| 719 | Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment", |
| 720 | new Throwable()); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 724 | /** |
| 725 | * Append path segments to each given base path, returning result. |
| 726 | * |
| 727 | * @hide |
| 728 | */ |
| 729 | public static File[] buildPaths(File[] base, String... segments) { |
| 730 | File[] result = new File[base.length]; |
| 731 | for (int i = 0; i < base.length; i++) { |
| 732 | result[i] = buildPath(base[i], segments); |
| 733 | } |
| 734 | return result; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Append path segments to given base path, returning result. |
| 739 | * |
| 740 | * @hide |
| 741 | */ |
| 742 | public static File buildPath(File base, String... segments) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 743 | File cur = base; |
| 744 | for (String segment : segments) { |
| 745 | if (cur == null) { |
| 746 | cur = new File(segment); |
| 747 | } else { |
| 748 | cur = new File(cur, segment); |
| 749 | } |
| 750 | } |
| 751 | return cur; |
| 752 | } |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 753 | |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 754 | private static boolean isStorageDisabled() { |
| 755 | return SystemProperties.getBoolean("config.disable_storage", false); |
| 756 | } |
| 757 | |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 758 | /** |
| 759 | * If the given path exists on emulated external storage, return the |
| 760 | * translated backing path hosted on internal storage. This bypasses any |
| 761 | * emulation later, improving performance. This is <em>only</em> suitable |
| 762 | * for read-only access. |
| 763 | * <p> |
| 764 | * Returns original path if given path doesn't meet these criteria. Callers |
| 765 | * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE} |
| 766 | * permission. |
| 767 | * |
| 768 | * @hide |
| 769 | */ |
| 770 | public static File maybeTranslateEmulatedPathToInternal(File path) { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 771 | return StorageManager.maybeTranslateEmulatedPathToInternal(path); |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 772 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | } |