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