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 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 19 | import android.os.storage.IMountService; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 20 | import android.os.storage.StorageManager; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 21 | import android.os.storage.StorageVolume; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
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"; |
| 34 | private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET"; |
| 35 | |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame^] | 36 | /** {@hide} */ |
| 37 | public static String DIRECTORY_ANDROID = "Android"; |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | private static final File ROOT_DIRECTORY |
| 40 | = getDirectory("ANDROID_ROOT", "/system"); |
| 41 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 42 | private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled"; |
| 43 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 44 | private static UserEnvironment sCurrentUser; |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 45 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 46 | private static final Object sLock = new Object(); |
| 47 | |
| 48 | // @GuardedBy("sLock") |
| 49 | private static volatile StorageVolume sPrimaryVolume; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 50 | |
| 51 | private static StorageVolume getPrimaryVolume() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 52 | if (sPrimaryVolume == null) { |
| 53 | synchronized (sLock) { |
| 54 | if (sPrimaryVolume == null) { |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 55 | try { |
| 56 | IMountService mountService = IMountService.Stub.asInterface(ServiceManager |
| 57 | .getService("mount")); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 58 | final StorageVolume[] volumes = mountService.getVolumeList(); |
| 59 | sPrimaryVolume = StorageManager.getPrimaryVolume(volumes); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 60 | } catch (Exception e) { |
| 61 | Log.e(TAG, "couldn't talk to MountService", e); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 66 | return sPrimaryVolume; |
| 67 | } |
| 68 | |
| 69 | static { |
| 70 | initForCurrentUser(); |
| 71 | } |
| 72 | |
| 73 | /** {@hide} */ |
| 74 | public static void initForCurrentUser() { |
| 75 | final int userId = UserHandle.myUserId(); |
| 76 | sCurrentUser = new UserEnvironment(userId); |
| 77 | |
| 78 | synchronized (sLock) { |
| 79 | sPrimaryVolume = null; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** {@hide} */ |
| 84 | public static class UserEnvironment { |
| 85 | // TODO: generalize further to create package-specific environment |
| 86 | |
| 87 | private final File mExternalStorage; |
| 88 | private final File mExternalStorageAndroidData; |
| 89 | private final File mExternalStorageAndroidMedia; |
| 90 | private final File mExternalStorageAndroidObb; |
| 91 | |
| 92 | public UserEnvironment(int userId) { |
| 93 | // See storage config details at http://source.android.com/tech/storage/ |
| 94 | String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE); |
| 95 | String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET); |
| 96 | |
| 97 | if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) { |
| 98 | // Device has emulated storage; external storage paths should have |
| 99 | // userId burned into them. |
| 100 | final File emulatedBase = new File(rawEmulatedStorageTarget); |
| 101 | |
| 102 | // /storage/emulated/0 |
| 103 | mExternalStorage = buildPath(emulatedBase, Integer.toString(userId)); |
| 104 | // /storage/emulated/obb |
| 105 | mExternalStorageAndroidObb = buildPath(emulatedBase, "obb"); |
| 106 | |
| 107 | } else { |
| 108 | // Device has physical external storage; use plain paths. |
| 109 | if (TextUtils.isEmpty(rawExternalStorage)) { |
| 110 | Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default"); |
| 111 | rawExternalStorage = "/storage/sdcard0"; |
| 112 | } |
| 113 | |
| 114 | // /storage/sdcard0 |
| 115 | mExternalStorage = new File(rawExternalStorage); |
| 116 | // /storage/sdcard0/Android/obb |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame^] | 117 | mExternalStorageAndroidObb = buildPath(mExternalStorage, DIRECTORY_ANDROID, "obb"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame^] | 120 | mExternalStorageAndroidData = buildPath(mExternalStorage, DIRECTORY_ANDROID, "data"); |
| 121 | mExternalStorageAndroidMedia = buildPath(mExternalStorage, DIRECTORY_ANDROID, "media"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | public File getExternalStorageDirectory() { |
| 125 | return mExternalStorage; |
| 126 | } |
| 127 | |
| 128 | public File getExternalStoragePublicDirectory(String type) { |
| 129 | return new File(mExternalStorage, type); |
| 130 | } |
| 131 | |
| 132 | public File getExternalStorageAndroidDataDir() { |
| 133 | return mExternalStorageAndroidData; |
| 134 | } |
| 135 | |
| 136 | public File getExternalStorageAppDataDirectory(String packageName) { |
| 137 | return new File(mExternalStorageAndroidData, packageName); |
| 138 | } |
| 139 | |
| 140 | public File getExternalStorageAppMediaDirectory(String packageName) { |
| 141 | return new File(mExternalStorageAndroidMedia, packageName); |
| 142 | } |
| 143 | |
| 144 | public File getExternalStorageAppObbDirectory(String packageName) { |
| 145 | return new File(mExternalStorageAndroidObb, packageName); |
| 146 | } |
| 147 | |
| 148 | public File getExternalStorageAppFilesDirectory(String packageName) { |
| 149 | return new File(new File(mExternalStorageAndroidData, packageName), "files"); |
| 150 | } |
| 151 | |
| 152 | public File getExternalStorageAppCacheDirectory(String packageName) { |
| 153 | return new File(new File(mExternalStorageAndroidData, packageName), "cache"); |
| 154 | } |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 155 | } |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | /** |
| 158 | * Gets the Android root directory. |
| 159 | */ |
| 160 | public static File getRootDirectory() { |
| 161 | return ROOT_DIRECTORY; |
| 162 | } |
| 163 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 164 | /** |
| 165 | * Gets the system directory available for secure storage. |
| 166 | * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system). |
| 167 | * Otherwise, it returns the unencrypted /data/system directory. |
| 168 | * @return File object representing the secure storage system directory. |
| 169 | * @hide |
| 170 | */ |
| 171 | public static File getSystemSecureDirectory() { |
| 172 | if (isEncryptedFilesystemEnabled()) { |
| 173 | return new File(SECURE_DATA_DIRECTORY, "system"); |
| 174 | } else { |
| 175 | return new File(DATA_DIRECTORY, "system"); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Gets the data directory for secure storage. |
| 181 | * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure). |
| 182 | * Otherwise, it returns the unencrypted /data directory. |
| 183 | * @return File object representing the data directory for secure storage. |
| 184 | * @hide |
| 185 | */ |
| 186 | public static File getSecureDataDirectory() { |
| 187 | if (isEncryptedFilesystemEnabled()) { |
| 188 | return SECURE_DATA_DIRECTORY; |
| 189 | } else { |
| 190 | return DATA_DIRECTORY; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
Jeff Sharkey | d525baa | 2012-05-22 18:25:32 -0700 | [diff] [blame] | 195 | * Return directory used for internal media storage, which is protected by |
| 196 | * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}. |
| 197 | * |
| 198 | * @hide |
| 199 | */ |
| 200 | public static File getMediaStorageDirectory() { |
| 201 | return MEDIA_STORAGE_DIRECTORY; |
| 202 | } |
| 203 | |
| 204 | /** |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 205 | * Return the system directory for a user. This is for use by system services to store |
| 206 | * files relating to the user. This directory will be automatically deleted when the user |
| 207 | * is removed. |
| 208 | * |
| 209 | * @hide |
| 210 | */ |
| 211 | public static File getUserSystemDirectory(int userId) { |
| 212 | return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId)); |
| 213 | } |
| 214 | |
| 215 | /** |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 216 | * Returns whether the Encrypted File System feature is enabled on the device or not. |
| 217 | * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code> |
| 218 | * if disabled. |
| 219 | * @hide |
| 220 | */ |
| 221 | public static boolean isEncryptedFilesystemEnabled() { |
| 222 | return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false); |
| 223 | } |
| 224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | private static final File DATA_DIRECTORY |
| 226 | = getDirectory("ANDROID_DATA", "/data"); |
| 227 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 228 | /** |
| 229 | * @hide |
| 230 | */ |
| 231 | private static final File SECURE_DATA_DIRECTORY |
| 232 | = getDirectory("ANDROID_SECURE_DATA", "/data/secure"); |
| 233 | |
Jeff Sharkey | d525baa | 2012-05-22 18:25:32 -0700 | [diff] [blame] | 234 | /** @hide */ |
| 235 | private static final File MEDIA_STORAGE_DIRECTORY |
| 236 | = getDirectory("MEDIA_STORAGE", "/data/media"); |
| 237 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 238 | 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] | 239 | |
| 240 | /** |
| 241 | * Gets the Android data directory. |
| 242 | */ |
| 243 | public static File getDataDirectory() { |
| 244 | return DATA_DIRECTORY; |
| 245 | } |
| 246 | |
| 247 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 248 | * Gets the Android external storage directory. This directory may not |
| 249 | * currently be accessible if it has been mounted by the user on their |
| 250 | * computer, has been removed from the device, or some other problem has |
| 251 | * happened. You can determine its current state with |
| 252 | * {@link #getExternalStorageState()}. |
| 253 | * |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 254 | * <p><em>Note: don't be confused by the word "external" here. This |
| 255 | * directory can better be thought as media/shared storage. It is a |
| 256 | * filesystem that can hold a relatively large amount of data and that |
| 257 | * is shared across all applications (does not enforce permissions). |
| 258 | * Traditionally this is an SD card, but it may also be implemented as |
| 259 | * built-in storage in a device that is distinct from the protected |
| 260 | * internal storage and can be mounted as a filesystem on a computer.</em></p> |
| 261 | * |
| 262 | * <p>In devices with multiple "external" storage directories (such as |
| 263 | * both secure app storage and mountable shared storage), this directory |
| 264 | * represents the "primary" external storage that the user will interact |
| 265 | * with.</p> |
| 266 | * |
Dianne Hackborn | acaf028 | 2010-03-30 14:39:35 -0700 | [diff] [blame] | 267 | * <p>Applications should not directly use this top-level directory, in |
| 268 | * order to avoid polluting the user's root namespace. Any files that are |
| 269 | * private to the application should be placed in a directory returned |
| 270 | * by {@link android.content.Context#getExternalFilesDir |
| 271 | * Context.getExternalFilesDir}, which the system will take care of deleting |
| 272 | * if the application is uninstalled. Other shared files should be placed |
| 273 | * in one of the directories returned by |
| 274 | * {@link #getExternalStoragePublicDirectory}. |
| 275 | * |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 276 | * <p>Here is an example of typical code to monitor the state of |
| 277 | * external storage:</p> |
| 278 | * |
| 279 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java |
| 280 | * monitor_storage} |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 281 | * |
| 282 | * @see #getExternalStorageState() |
| 283 | * @see #isExternalStorageRemovable() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | */ |
| 285 | public static File getExternalStorageDirectory() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 286 | throwIfSystem(); |
| 287 | return sCurrentUser.getExternalStorageDirectory(); |
| 288 | } |
| 289 | |
| 290 | /** {@hide} */ |
| 291 | public static File getLegacyExternalStorageDirectory() { |
| 292 | return new File(System.getenv(ENV_EXTERNAL_STORAGE)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 296 | * Standard directory in which to place any audio files that should be |
| 297 | * in the regular list of music for the user. |
| 298 | * This may be combined with |
| 299 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, |
| 300 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 301 | * of directories to categories a particular audio file as more than one |
| 302 | * type. |
| 303 | */ |
| 304 | public static String DIRECTORY_MUSIC = "Music"; |
| 305 | |
| 306 | /** |
| 307 | * Standard directory in which to place any audio files that should be |
| 308 | * in the list of podcasts that the user can select (not as regular |
| 309 | * music). |
| 310 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 311 | * {@link #DIRECTORY_NOTIFICATIONS}, |
| 312 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 313 | * of directories to categories a particular audio file as more than one |
| 314 | * type. |
| 315 | */ |
| 316 | public static String DIRECTORY_PODCASTS = "Podcasts"; |
| 317 | |
| 318 | /** |
| 319 | * Standard directory in which to place any audio files that should be |
| 320 | * in the list of ringtones that the user can select (not as regular |
| 321 | * music). |
| 322 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 323 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and |
| 324 | * {@link #DIRECTORY_ALARMS} as a series |
| 325 | * of directories to categories a particular audio file as more than one |
| 326 | * type. |
| 327 | */ |
| 328 | public static String DIRECTORY_RINGTONES = "Ringtones"; |
| 329 | |
| 330 | /** |
| 331 | * Standard directory in which to place any audio files that should be |
| 332 | * in the list of alarms that the user can select (not as regular |
| 333 | * music). |
| 334 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 335 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, |
| 336 | * and {@link #DIRECTORY_RINGTONES} as a series |
| 337 | * of directories to categories a particular audio file as more than one |
| 338 | * type. |
| 339 | */ |
| 340 | public static String DIRECTORY_ALARMS = "Alarms"; |
| 341 | |
| 342 | /** |
| 343 | * Standard directory in which to place any audio files that should be |
| 344 | * in the list of notifications that the user can select (not as regular |
| 345 | * music). |
| 346 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 347 | * {@link #DIRECTORY_PODCASTS}, |
| 348 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 349 | * of directories to categories a particular audio file as more than one |
| 350 | * type. |
| 351 | */ |
| 352 | public static String DIRECTORY_NOTIFICATIONS = "Notifications"; |
| 353 | |
| 354 | /** |
| 355 | * Standard directory in which to place pictures that are available to |
| 356 | * the user. Note that this is primarily a convention for the top-level |
| 357 | * public directory, as the media scanner will find and collect pictures |
| 358 | * in any directory. |
| 359 | */ |
| 360 | public static String DIRECTORY_PICTURES = "Pictures"; |
| 361 | |
| 362 | /** |
| 363 | * Standard directory in which to place movies that are available to |
| 364 | * the user. Note that this is primarily a convention for the top-level |
| 365 | * public directory, as the media scanner will find and collect movies |
| 366 | * in any directory. |
| 367 | */ |
| 368 | public static String DIRECTORY_MOVIES = "Movies"; |
| 369 | |
| 370 | /** |
| 371 | * Standard directory in which to place files that have been downloaded by |
| 372 | * the user. Note that this is primarily a convention for the top-level |
| 373 | * public directory, you are free to download files anywhere in your own |
Dianne Hackborn | ce59fb8 | 2010-04-07 17:19:04 -0700 | [diff] [blame] | 374 | * private directories. Also note that though the constant here is |
| 375 | * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for |
| 376 | * backwards compatibility reasons. |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 377 | */ |
Dianne Hackborn | ce59fb8 | 2010-04-07 17:19:04 -0700 | [diff] [blame] | 378 | public static String DIRECTORY_DOWNLOADS = "Download"; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 379 | |
| 380 | /** |
| 381 | * The traditional location for pictures and videos when mounting the |
| 382 | * device as a camera. Note that this is primarily a convention for the |
| 383 | * top-level public directory, as this convention makes no sense elsewhere. |
| 384 | */ |
| 385 | public static String DIRECTORY_DCIM = "DCIM"; |
| 386 | |
| 387 | /** |
| 388 | * Get a top-level public external storage directory for placing files of |
| 389 | * a particular type. This is where the user will typically place and |
| 390 | * manage their own files, so you should be careful about what you put here |
| 391 | * to ensure you don't erase their files or get in the way of their own |
| 392 | * organization. |
| 393 | * |
| 394 | * <p>Here is an example of typical code to manipulate a picture on |
| 395 | * the public external storage:</p> |
| 396 | * |
| 397 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java |
| 398 | * public_picture} |
| 399 | * |
| 400 | * @param type The type of storage directory to return. Should be one of |
| 401 | * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS}, |
| 402 | * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS}, |
| 403 | * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES}, |
| 404 | * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or |
| 405 | * {@link #DIRECTORY_DCIM}. May not be null. |
| 406 | * |
| 407 | * @return Returns the File path for the directory. Note that this |
| 408 | * directory may not yet exist, so you must make sure it exists before |
| 409 | * using it such as with {@link File#mkdirs File.mkdirs()}. |
| 410 | */ |
| 411 | public static File getExternalStoragePublicDirectory(String type) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 412 | throwIfSystem(); |
| 413 | return sCurrentUser.getExternalStoragePublicDirectory(type); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Returns the path for android-specific data on the SD card. |
| 418 | * @hide |
| 419 | */ |
| 420 | public static File getExternalStorageAndroidDataDir() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 421 | throwIfSystem(); |
| 422 | return sCurrentUser.getExternalStorageAndroidDataDir(); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Generates the raw path to an application's data |
| 427 | * @hide |
| 428 | */ |
| 429 | public static File getExternalStorageAppDataDirectory(String packageName) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 430 | throwIfSystem(); |
| 431 | return sCurrentUser.getExternalStorageAppDataDirectory(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Generates the raw path to an application's media |
| 436 | * @hide |
| 437 | */ |
| 438 | public static File getExternalStorageAppMediaDirectory(String packageName) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 439 | throwIfSystem(); |
| 440 | return sCurrentUser.getExternalStorageAppMediaDirectory(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 444 | * Generates the raw path to an application's OBB files |
| 445 | * @hide |
| 446 | */ |
| 447 | public static File getExternalStorageAppObbDirectory(String packageName) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 448 | throwIfSystem(); |
| 449 | return sCurrentUser.getExternalStorageAppObbDirectory(packageName); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 453 | * Generates the path to an application's files. |
| 454 | * @hide |
| 455 | */ |
| 456 | public static File getExternalStorageAppFilesDirectory(String packageName) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 457 | throwIfSystem(); |
| 458 | return sCurrentUser.getExternalStorageAppFilesDirectory(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 459 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 460 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 461 | /** |
| 462 | * Generates the path to an application's cache. |
| 463 | * @hide |
| 464 | */ |
| 465 | public static File getExternalStorageAppCacheDirectory(String packageName) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 466 | throwIfSystem(); |
| 467 | return sCurrentUser.getExternalStorageAppCacheDirectory(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | * Gets the Android Download/Cache content directory. |
| 472 | */ |
| 473 | public static File getDownloadCacheDirectory() { |
| 474 | return DOWNLOAD_CACHE_DIRECTORY; |
| 475 | } |
| 476 | |
| 477 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 478 | * {@link #getExternalStorageState()} returns MEDIA_REMOVED if the media is not present. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | */ |
| 480 | public static final String MEDIA_REMOVED = "removed"; |
| 481 | |
| 482 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 483 | * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTED if the media is present |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | * but not mounted. |
| 485 | */ |
| 486 | public static final String MEDIA_UNMOUNTED = "unmounted"; |
| 487 | |
| 488 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 489 | * {@link #getExternalStorageState()} returns MEDIA_CHECKING if the media is present |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | * and being disk-checked |
| 491 | */ |
| 492 | public static final String MEDIA_CHECKING = "checking"; |
| 493 | |
| 494 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 495 | * {@link #getExternalStorageState()} returns MEDIA_NOFS if the media is present |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | * but is blank or is using an unsupported filesystem |
| 497 | */ |
| 498 | public static final String MEDIA_NOFS = "nofs"; |
| 499 | |
| 500 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 501 | * {@link #getExternalStorageState()} returns MEDIA_MOUNTED if the media is present |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | * and mounted at its mount point with read/write access. |
| 503 | */ |
| 504 | public static final String MEDIA_MOUNTED = "mounted"; |
| 505 | |
| 506 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 507 | * {@link #getExternalStorageState()} returns MEDIA_MOUNTED_READ_ONLY if the media is present |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | * and mounted at its mount point with read only access. |
| 509 | */ |
| 510 | public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro"; |
| 511 | |
| 512 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 513 | * {@link #getExternalStorageState()} returns MEDIA_SHARED if the media is present |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | * not mounted, and shared via USB mass storage. |
| 515 | */ |
| 516 | public static final String MEDIA_SHARED = "shared"; |
| 517 | |
| 518 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 519 | * {@link #getExternalStorageState()} returns MEDIA_BAD_REMOVAL if the media was |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | * removed before it was unmounted. |
| 521 | */ |
| 522 | public static final String MEDIA_BAD_REMOVAL = "bad_removal"; |
| 523 | |
| 524 | /** |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 525 | * {@link #getExternalStorageState()} returns MEDIA_UNMOUNTABLE if the media is present |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | * but cannot be mounted. Typically this happens if the file system on the |
| 527 | * media is corrupted. |
| 528 | */ |
| 529 | public static final String MEDIA_UNMOUNTABLE = "unmountable"; |
| 530 | |
| 531 | /** |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 532 | * Gets the current state of the primary "external" storage device. |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 533 | * |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 534 | * <p>See {@link #getExternalStorageDirectory()} for more information. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | */ |
| 536 | public static String getExternalStorageState() { |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 537 | try { |
Kenny Root | b2278dc | 2011-01-18 13:03:28 -0800 | [diff] [blame] | 538 | IMountService mountService = IMountService.Stub.asInterface(ServiceManager |
| 539 | .getService("mount")); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 540 | final StorageVolume primary = getPrimaryVolume(); |
| 541 | return mountService.getVolumeState(primary.getPath()); |
| 542 | } catch (RemoteException rex) { |
| 543 | Log.w(TAG, "Failed to read external storage state; assuming REMOVED: " + rex); |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 544 | return Environment.MEDIA_REMOVED; |
| 545 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 548 | /** |
| 549 | * Returns whether the primary "external" storage device is removable. |
| 550 | * If true is returned, this device is for example an SD card that the |
| 551 | * user can remove. If false is returned, the storage is built into |
| 552 | * the device and can not be physically removed. |
| 553 | * |
| 554 | * <p>See {@link #getExternalStorageDirectory()} for more information. |
| 555 | */ |
| 556 | public static boolean isExternalStorageRemovable() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 557 | final StorageVolume primary = getPrimaryVolume(); |
| 558 | return (primary != null && primary.isRemovable()); |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 561 | /** |
| 562 | * Returns whether the device has an external storage device which is |
Andy Stadler | 50c294f | 2011-03-07 19:13:42 -0800 | [diff] [blame] | 563 | * emulated. If true, the device does not have real external storage, and the directory |
| 564 | * returned by {@link #getExternalStorageDirectory()} will be allocated using a portion of |
| 565 | * the internal storage system. |
| 566 | * |
| 567 | * <p>Certain system services, such as the package manager, use this |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 568 | * to determine where to install an application. |
Andy Stadler | 50c294f | 2011-03-07 19:13:42 -0800 | [diff] [blame] | 569 | * |
| 570 | * <p>Emulated external storage may also be encrypted - see |
| 571 | * {@link android.app.admin.DevicePolicyManager#setStorageEncryption( |
| 572 | * android.content.ComponentName, boolean)} for additional details. |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 573 | */ |
| 574 | public static boolean isExternalStorageEmulated() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 575 | final StorageVolume primary = getPrimaryVolume(); |
| 576 | return (primary != null && primary.isEmulated()); |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 577 | } |
| 578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | static File getDirectory(String variableName, String defaultPath) { |
| 580 | String path = System.getenv(variableName); |
| 581 | return path == null ? new File(defaultPath) : new File(path); |
| 582 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 583 | |
| 584 | private static void throwIfSystem() { |
| 585 | if (Process.myUid() == Process.SYSTEM_UID) { |
| 586 | Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable()); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | private static File buildPath(File base, String... segments) { |
| 591 | File cur = base; |
| 592 | for (String segment : segments) { |
| 593 | if (cur == null) { |
| 594 | cur = new File(segment); |
| 595 | } else { |
| 596 | cur = new File(cur, segment); |
| 597 | } |
| 598 | } |
| 599 | return cur; |
| 600 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | } |