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