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