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 | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame^] | 19 | import android.annotation.SystemApi; |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 20 | import android.annotation.TestApi; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 21 | import android.app.admin.DevicePolicyManager; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 23 | import android.os.storage.StorageManager; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 24 | import android.os.storage.StorageVolume; |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
Kenny Root | b2278dc | 2011-01-18 13:03:28 -0800 | [diff] [blame] | 26 | import android.util.Log; |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 27 | |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 28 | import java.io.File; |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 29 | import java.util.LinkedList; |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Provides access to environment variables. |
| 33 | */ |
| 34 | public class Environment { |
Kenny Root | b2278dc | 2011-01-18 13:03:28 -0800 | [diff] [blame] | 35 | private static final String TAG = "Environment"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 37 | // NOTE: keep credential-protected paths in sync with StrictMode.java |
| 38 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 39 | private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE"; |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 40 | private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT"; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 41 | private static final String ENV_ANDROID_DATA = "ANDROID_DATA"; |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 42 | private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND"; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 43 | private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE"; |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 44 | private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE"; |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 45 | private static final String ENV_OEM_ROOT = "OEM_ROOT"; |
Hung-ying Tyan | bdc9d58 | 2015-11-20 11:53:39 +0800 | [diff] [blame] | 46 | private static final String ENV_ODM_ROOT = "ODM_ROOT"; |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 47 | private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT"; |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 48 | private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT"; |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 49 | private static final String ENV_PRODUCT_SERVICES_ROOT = "PRODUCT_SERVICES_ROOT"; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 50 | |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame] | 51 | /** {@hide} */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 52 | public static final String DIR_ANDROID = "Android"; |
| 53 | private static final String DIR_DATA = "data"; |
| 54 | private static final String DIR_MEDIA = "media"; |
| 55 | private static final String DIR_OBB = "obb"; |
| 56 | private static final String DIR_FILES = "files"; |
| 57 | private static final String DIR_CACHE = "cache"; |
| 58 | |
| 59 | /** {@hide} */ |
| 60 | @Deprecated |
| 61 | public static final String DIRECTORY_ANDROID = DIR_ANDROID; |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame] | 62 | |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 63 | private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 64 | private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data"); |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 65 | private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 66 | private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage"); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 67 | private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache"); |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 68 | private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem"); |
Hung-ying Tyan | bdc9d58 | 2015-11-20 11:53:39 +0800 | [diff] [blame] | 69 | private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm"); |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 70 | private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor"); |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 71 | private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product"); |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 72 | private static final File DIR_PRODUCT_SERVICES_ROOT = getDirectory(ENV_PRODUCT_SERVICES_ROOT, |
| 73 | "/product_services"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 75 | private static UserEnvironment sCurrentUser; |
| 76 | private static boolean sUserRequired; |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 77 | |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 78 | static { |
| 79 | initForCurrentUser(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** {@hide} */ |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 83 | public static void initForCurrentUser() { |
| 84 | final int userId = UserHandle.myUserId(); |
| 85 | sCurrentUser = new UserEnvironment(userId); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** {@hide} */ |
| 89 | public static class UserEnvironment { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 90 | private final int mUserId; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 91 | |
| 92 | public UserEnvironment(int userId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 93 | mUserId = userId; |
| 94 | } |
Jeff Sharkey | 44cbdec | 2013-10-07 16:49:47 -0700 | [diff] [blame] | 95 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 96 | public File[] getExternalDirs() { |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 97 | final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId, |
| 98 | StorageManager.FLAG_FOR_WRITE); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 99 | final File[] files = new File[volumes.length]; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 100 | for (int i = 0; i < volumes.length; i++) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 101 | files[i] = volumes[i].getPathFile(); |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 102 | } |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 103 | return files; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 106 | @Deprecated |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 107 | public File getExternalStorageDirectory() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 108 | return getExternalDirs()[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 111 | @Deprecated |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 112 | public File getExternalStoragePublicDirectory(String type) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 113 | return buildExternalStoragePublicDirs(type)[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 116 | public File[] buildExternalStoragePublicDirs(String type) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 117 | return buildPaths(getExternalDirs(), type); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 120 | public File[] buildExternalStorageAndroidDataDirs() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 121 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 124 | public File[] buildExternalStorageAndroidObbDirs() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 125 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 128 | public File[] buildExternalStorageAppDataDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 129 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 130 | } |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 131 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 132 | public File[] buildExternalStorageAppMediaDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 133 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName); |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 136 | public File[] buildExternalStorageAppObbDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 137 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public File[] buildExternalStorageAppFilesDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 141 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public File[] buildExternalStorageAppCacheDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 145 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE); |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 146 | } |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 147 | } |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | /** |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 150 | * Return root of the "system" partition holding the core Android OS. |
| 151 | * Always present and mounted read-only. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | */ |
| 153 | public static File getRootDirectory() { |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 154 | return DIR_ANDROID_ROOT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 157 | /** {@hide} */ |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 158 | @TestApi |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 159 | public static File getStorageDirectory() { |
| 160 | return DIR_ANDROID_STORAGE; |
| 161 | } |
| 162 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 163 | /** |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 164 | * Return root directory of the "oem" partition holding OEM customizations, |
| 165 | * if any. If present, the partition is mounted read-only. |
| 166 | * |
| 167 | * @hide |
| 168 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame^] | 169 | @SystemApi |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 170 | public static File getOemDirectory() { |
| 171 | return DIR_OEM_ROOT; |
| 172 | } |
| 173 | |
| 174 | /** |
Hung-ying Tyan | bdc9d58 | 2015-11-20 11:53:39 +0800 | [diff] [blame] | 175 | * Return root directory of the "odm" partition holding ODM customizations, |
| 176 | * if any. If present, the partition is mounted read-only. |
| 177 | * |
| 178 | * @hide |
| 179 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame^] | 180 | @SystemApi |
Hung-ying Tyan | bdc9d58 | 2015-11-20 11:53:39 +0800 | [diff] [blame] | 181 | public static File getOdmDirectory() { |
| 182 | return DIR_ODM_ROOT; |
| 183 | } |
| 184 | |
| 185 | /** |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 186 | * Return root directory of the "vendor" partition that holds vendor-provided |
| 187 | * software that should persist across simple reflashing of the "system" partition. |
| 188 | * @hide |
| 189 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame^] | 190 | @SystemApi |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 191 | public static File getVendorDirectory() { |
| 192 | return DIR_VENDOR_ROOT; |
| 193 | } |
| 194 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 195 | /** |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 196 | * Return root directory of the "product" partition holding product-specific |
| 197 | * customizations if any. If present, the partition is mounted read-only. |
| 198 | * |
| 199 | * @hide |
| 200 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame^] | 201 | @SystemApi |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 202 | public static File getProductDirectory() { |
| 203 | return DIR_PRODUCT_ROOT; |
| 204 | } |
| 205 | |
| 206 | /** |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 207 | * Return root directory of the "product_services" partition holding middleware |
| 208 | * services if any. If present, the partition is mounted read-only. |
| 209 | * |
| 210 | * @hide |
| 211 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame^] | 212 | @SystemApi |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 213 | public static File getProductServicesDirectory() { |
| 214 | return DIR_PRODUCT_SERVICES_ROOT; |
| 215 | } |
| 216 | |
| 217 | /** |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 218 | * Return the system directory for a user. This is for use by system |
| 219 | * services to store files relating to the user. This directory will be |
| 220 | * automatically deleted when the user is removed. |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 221 | * |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 222 | * @deprecated This directory is valid and still exists, but callers should |
| 223 | * <em>strongly</em> consider switching to |
| 224 | * {@link #getDataSystemCeDirectory(int)} which is protected |
| 225 | * with user credentials or |
| 226 | * {@link #getDataSystemDeDirectory(int)} which supports fast |
| 227 | * user wipe. |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 228 | * @hide |
| 229 | */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 230 | @Deprecated |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 231 | public static File getUserSystemDirectory(int userId) { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 232 | return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId)); |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 236 | * Returns the config directory for a user. This is for use by system |
| 237 | * services to store files relating to the user which should be readable by |
| 238 | * any app running as that user. |
Robin Lee | 6959133 | 2014-04-28 16:03:22 +0100 | [diff] [blame] | 239 | * |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 240 | * @deprecated This directory is valid and still exists, but callers should |
| 241 | * <em>strongly</em> consider switching to |
| 242 | * {@link #getDataMiscCeDirectory(int)} which is protected with |
| 243 | * user credentials or {@link #getDataMiscDeDirectory(int)} |
| 244 | * which supports fast user wipe. |
Robin Lee | 6959133 | 2014-04-28 16:03:22 +0100 | [diff] [blame] | 245 | * @hide |
| 246 | */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 247 | @Deprecated |
Robin Lee | 6959133 | 2014-04-28 16:03:22 +0100 | [diff] [blame] | 248 | public static File getUserConfigDirectory(int userId) { |
| 249 | return new File(new File(new File( |
| 250 | getDataDirectory(), "misc"), "user"), Integer.toString(userId)); |
| 251 | } |
| 252 | |
| 253 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 254 | * Return the user data directory. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | */ |
| 256 | public static File getDataDirectory() { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 257 | return DIR_ANDROID_DATA; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 260 | /** {@hide} */ |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 261 | public static File getDataDirectory(String volumeUuid) { |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 262 | if (TextUtils.isEmpty(volumeUuid)) { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 263 | return DIR_ANDROID_DATA; |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 264 | } else { |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 265 | return new File("/mnt/expand/" + volumeUuid); |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 269 | /** {@hide} */ |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 270 | public static File getExpandDirectory() { |
| 271 | return DIR_ANDROID_EXPAND; |
| 272 | } |
| 273 | |
| 274 | /** {@hide} */ |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 275 | public static File getDataSystemDirectory() { |
| 276 | return new File(getDataDirectory(), "system"); |
| 277 | } |
| 278 | |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 279 | /** |
| 280 | * Returns the base directory for per-user system directory, device encrypted. |
| 281 | * {@hide} |
| 282 | */ |
| 283 | public static File getDataSystemDeDirectory() { |
| 284 | return buildPath(getDataDirectory(), "system_de"); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Returns the base directory for per-user system directory, credential encrypted. |
| 289 | * {@hide} |
| 290 | */ |
| 291 | public static File getDataSystemCeDirectory() { |
| 292 | return buildPath(getDataDirectory(), "system_ce"); |
| 293 | } |
| 294 | |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 295 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 296 | public static File getDataSystemCeDirectory(int userId) { |
| 297 | return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId)); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 301 | public static File getDataSystemDeDirectory(int userId) { |
| 302 | return buildPath(getDataDirectory(), "system_de", String.valueOf(userId)); |
| 303 | } |
| 304 | |
| 305 | /** {@hide} */ |
| 306 | public static File getDataMiscDirectory() { |
| 307 | return new File(getDataDirectory(), "misc"); |
| 308 | } |
| 309 | |
| 310 | /** {@hide} */ |
Amith Yamasani | a280713 | 2017-01-26 12:35:14 -0800 | [diff] [blame] | 311 | public static File getDataMiscCeDirectory() { |
| 312 | return buildPath(getDataDirectory(), "misc_ce"); |
| 313 | } |
| 314 | |
| 315 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 316 | public static File getDataMiscCeDirectory(int userId) { |
| 317 | return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId)); |
| 318 | } |
| 319 | |
| 320 | /** {@hide} */ |
| 321 | public static File getDataMiscDeDirectory(int userId) { |
| 322 | return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId)); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 325 | private static File getDataProfilesDeDirectory(int userId) { |
| 326 | return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId)); |
| 327 | } |
| 328 | |
| 329 | /** {@hide} */ |
Andreas Huber | 7fe2053 | 2018-01-22 11:26:44 -0800 | [diff] [blame] | 330 | public static File getDataVendorCeDirectory(int userId) { |
| 331 | return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId)); |
| 332 | } |
| 333 | |
| 334 | /** {@hide} */ |
| 335 | public static File getDataVendorDeDirectory(int userId) { |
| 336 | return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId)); |
| 337 | } |
| 338 | |
| 339 | /** {@hide} */ |
Calin Juravle | 6ae39fc | 2018-01-19 20:32:47 -0800 | [diff] [blame] | 340 | public static File getDataRefProfilesDePackageDirectory(String packageName) { |
| 341 | return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName); |
Calin Juravle | 0bd7762 | 2016-07-12 15:56:41 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** {@hide} */ |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 345 | public static File getDataProfilesDePackageDirectory(int userId, String packageName) { |
| 346 | return buildPath(getDataProfilesDeDirectory(userId), packageName); |
| 347 | } |
| 348 | |
| 349 | /** {@hide} */ |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 350 | public static File getDataAppDirectory(String volumeUuid) { |
| 351 | return new File(getDataDirectory(volumeUuid), "app"); |
| 352 | } |
| 353 | |
| 354 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 355 | public static File getDataUserCeDirectory(String volumeUuid) { |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 356 | return new File(getDataDirectory(volumeUuid), "user"); |
| 357 | } |
| 358 | |
| 359 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 360 | public static File getDataUserCeDirectory(String volumeUuid, int userId) { |
| 361 | return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId)); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 365 | public static File getDataUserCePackageDirectory(String volumeUuid, int userId, |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 366 | String packageName) { |
| 367 | // TODO: keep consistent with installd |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 368 | return new File(getDataUserCeDirectory(volumeUuid, userId), packageName); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 372 | public static File getDataUserDeDirectory(String volumeUuid) { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 373 | return new File(getDataDirectory(volumeUuid), "user_de"); |
| 374 | } |
| 375 | |
| 376 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 377 | public static File getDataUserDeDirectory(String volumeUuid, int userId) { |
| 378 | return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId)); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 382 | public static File getDataUserDePackageDirectory(String volumeUuid, int userId, |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 383 | String packageName) { |
| 384 | // TODO: keep consistent with installd |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 385 | return new File(getDataUserDeDirectory(volumeUuid, userId), packageName); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 386 | } |
| 387 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | /** |
Fyodor Kupolov | 8825758 | 2016-05-24 16:06:56 -0700 | [diff] [blame] | 389 | * Return preloads directory. |
| 390 | * <p>This directory may contain pre-loaded content such as |
| 391 | * {@link #getDataPreloadsDemoDirectory() demo videos} and |
| 392 | * {@link #getDataPreloadsAppsDirectory() APK files} . |
| 393 | * {@hide} |
| 394 | */ |
| 395 | public static File getDataPreloadsDirectory() { |
| 396 | return new File(getDataDirectory(), "preloads"); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @see #getDataPreloadsDirectory() |
| 401 | * {@hide} |
| 402 | */ |
| 403 | public static File getDataPreloadsDemoDirectory() { |
| 404 | return new File(getDataPreloadsDirectory(), "demo"); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @see #getDataPreloadsDirectory() |
| 409 | * {@hide} |
| 410 | */ |
| 411 | public static File getDataPreloadsAppsDirectory() { |
| 412 | return new File(getDataPreloadsDirectory(), "apps"); |
| 413 | } |
| 414 | |
| 415 | /** |
Fyodor Kupolov | 19551a8 | 2016-08-22 14:46:08 -0700 | [diff] [blame] | 416 | * @see #getDataPreloadsDirectory() |
| 417 | * {@hide} |
| 418 | */ |
| 419 | public static File getDataPreloadsMediaDirectory() { |
| 420 | return new File(getDataPreloadsDirectory(), "media"); |
| 421 | } |
| 422 | |
| 423 | /** |
Fyodor Kupolov | 6e68706 | 2016-09-09 17:42:12 -0700 | [diff] [blame] | 424 | * Returns location of preloaded cache directory for package name |
| 425 | * @see #getDataPreloadsDirectory() |
| 426 | * {@hide} |
| 427 | */ |
| 428 | public static File getDataPreloadsFileCacheDirectory(String packageName) { |
| 429 | return new File(getDataPreloadsFileCacheDirectory(), packageName); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Returns location of preloaded cache directory. |
| 434 | * @see #getDataPreloadsDirectory() |
| 435 | * {@hide} |
| 436 | */ |
| 437 | public static File getDataPreloadsFileCacheDirectory() { |
| 438 | return new File(getDataPreloadsDirectory(), "file_cache"); |
| 439 | } |
| 440 | |
| 441 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 442 | * Return the primary shared/external storage directory. This directory may |
| 443 | * not currently be accessible if it has been mounted by the user on their |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 444 | * computer, has been removed from the device, or some other problem has |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 445 | * happened. You can determine its current state with |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 446 | * {@link #getExternalStorageState()}. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 447 | * <p> |
| 448 | * <em>Note: don't be confused by the word "external" here. This directory |
| 449 | * can better be thought as media/shared storage. It is a filesystem that |
| 450 | * can hold a relatively large amount of data and that is shared across all |
| 451 | * applications (does not enforce permissions). Traditionally this is an SD |
| 452 | * card, but it may also be implemented as built-in storage in a device that |
| 453 | * is distinct from the protected internal storage and can be mounted as a |
| 454 | * filesystem on a computer.</em> |
| 455 | * <p> |
| 456 | * On devices with multiple users (as described by {@link UserManager}), |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 457 | * each user has their own isolated shared storage. Applications only have |
| 458 | * access to the shared storage for the user they're running as. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 459 | * <p> |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 460 | * In devices with multiple shared/external storage directories, this |
| 461 | * directory represents the primary storage that the user will interact |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 462 | * with. Access to secondary storage is available through |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 463 | * {@link Context#getExternalFilesDirs(String)}, |
| 464 | * {@link Context#getExternalCacheDirs()}, and |
| 465 | * {@link Context#getExternalMediaDirs()}. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 466 | * <p> |
| 467 | * Applications should not directly use this top-level directory, in order |
| 468 | * to avoid polluting the user's root namespace. Any files that are private |
| 469 | * to the application should be placed in a directory returned by |
| 470 | * {@link android.content.Context#getExternalFilesDir |
Dianne Hackborn | acaf028 | 2010-03-30 14:39:35 -0700 | [diff] [blame] | 471 | * Context.getExternalFilesDir}, which the system will take care of deleting |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 472 | * if the application is uninstalled. Other shared files should be placed in |
| 473 | * one of the directories returned by |
| 474 | * {@link #getExternalStoragePublicDirectory}. |
| 475 | * <p> |
| 476 | * Writing to this path requires the |
| 477 | * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission, |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 478 | * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the |
Jeff Sharkey | 8c16579 | 2012-10-22 14:08:29 -0700 | [diff] [blame] | 479 | * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission, |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 480 | * which is automatically granted if you hold the write permission. |
| 481 | * <p> |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 482 | * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 483 | * application only needs to store internal data, consider using |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 484 | * {@link Context#getExternalFilesDir(String)}, |
| 485 | * {@link Context#getExternalCacheDir()}, or |
| 486 | * {@link Context#getExternalMediaDirs()}, which require no permissions to |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 487 | * read or write. |
| 488 | * <p> |
| 489 | * This path may change between platform versions, so applications should |
| 490 | * only persist relative paths. |
| 491 | * <p> |
| 492 | * Here is an example of typical code to monitor the state of external |
| 493 | * storage: |
| 494 | * <p> |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 495 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 496 | * monitor_storage} |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 497 | * |
| 498 | * @see #getExternalStorageState() |
| 499 | * @see #isExternalStorageRemovable() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | */ |
| 501 | public static File getExternalStorageDirectory() { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 502 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 503 | return sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /** {@hide} */ |
| 507 | public static File getLegacyExternalStorageDirectory() { |
| 508 | return new File(System.getenv(ENV_EXTERNAL_STORAGE)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 511 | /** {@hide} */ |
| 512 | public static File getLegacyExternalStorageObbDirectory() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 513 | return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 514 | } |
| 515 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 517 | * Standard directory in which to place any audio files that should be |
| 518 | * in the regular list of music for the user. |
| 519 | * This may be combined with |
| 520 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, |
| 521 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 522 | * of directories to categories a particular audio file as more than one |
| 523 | * type. |
| 524 | */ |
| 525 | public static String DIRECTORY_MUSIC = "Music"; |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 526 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 527 | /** |
| 528 | * Standard directory in which to place any audio files that should be |
| 529 | * in the list of podcasts that the user can select (not as regular |
| 530 | * music). |
| 531 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 532 | * {@link #DIRECTORY_NOTIFICATIONS}, |
| 533 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 534 | * of directories to categories a particular audio file as more than one |
| 535 | * type. |
| 536 | */ |
| 537 | public static String DIRECTORY_PODCASTS = "Podcasts"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 538 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 539 | /** |
| 540 | * Standard directory in which to place any audio files that should be |
| 541 | * in the list of ringtones that the user can select (not as regular |
| 542 | * music). |
| 543 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 544 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and |
| 545 | * {@link #DIRECTORY_ALARMS} as a series |
| 546 | * of directories to categories a particular audio file as more than one |
| 547 | * type. |
| 548 | */ |
| 549 | public static String DIRECTORY_RINGTONES = "Ringtones"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 550 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 551 | /** |
| 552 | * Standard directory in which to place any audio files that should be |
| 553 | * in the list of alarms that the user can select (not as regular |
| 554 | * music). |
| 555 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 556 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, |
| 557 | * and {@link #DIRECTORY_RINGTONES} as a series |
| 558 | * of directories to categories a particular audio file as more than one |
| 559 | * type. |
| 560 | */ |
| 561 | public static String DIRECTORY_ALARMS = "Alarms"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 562 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 563 | /** |
| 564 | * Standard directory in which to place any audio files that should be |
| 565 | * in the list of notifications that the user can select (not as regular |
| 566 | * music). |
| 567 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 568 | * {@link #DIRECTORY_PODCASTS}, |
| 569 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 570 | * of directories to categories a particular audio file as more than one |
| 571 | * type. |
| 572 | */ |
| 573 | public static String DIRECTORY_NOTIFICATIONS = "Notifications"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 574 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 575 | /** |
| 576 | * Standard directory in which to place pictures that are available to |
| 577 | * the user. Note that this is primarily a convention for the top-level |
| 578 | * public directory, as the media scanner will find and collect pictures |
| 579 | * in any directory. |
| 580 | */ |
| 581 | public static String DIRECTORY_PICTURES = "Pictures"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 582 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 583 | /** |
| 584 | * Standard directory in which to place movies that are available to |
| 585 | * the user. Note that this is primarily a convention for the top-level |
| 586 | * public directory, as the media scanner will find and collect movies |
| 587 | * in any directory. |
| 588 | */ |
| 589 | public static String DIRECTORY_MOVIES = "Movies"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 590 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 591 | /** |
| 592 | * Standard directory in which to place files that have been downloaded by |
| 593 | * the user. Note that this is primarily a convention for the top-level |
| 594 | * public directory, you are free to download files anywhere in your own |
Dianne Hackborn | ce59fb8 | 2010-04-07 17:19:04 -0700 | [diff] [blame] | 595 | * private directories. Also note that though the constant here is |
| 596 | * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for |
| 597 | * backwards compatibility reasons. |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 598 | */ |
Dianne Hackborn | ce59fb8 | 2010-04-07 17:19:04 -0700 | [diff] [blame] | 599 | public static String DIRECTORY_DOWNLOADS = "Download"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 600 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 601 | /** |
| 602 | * The traditional location for pictures and videos when mounting the |
| 603 | * device as a camera. Note that this is primarily a convention for the |
| 604 | * top-level public directory, as this convention makes no sense elsewhere. |
| 605 | */ |
| 606 | public static String DIRECTORY_DCIM = "DCIM"; |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 607 | |
| 608 | /** |
| 609 | * Standard directory in which to place documents that have been created by |
| 610 | * the user. |
| 611 | */ |
| 612 | public static String DIRECTORY_DOCUMENTS = "Documents"; |
| 613 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 614 | /** |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 615 | * List of standard storage directories. |
| 616 | * <p> |
| 617 | * Each of its values have its own constant: |
| 618 | * <ul> |
| 619 | * <li>{@link #DIRECTORY_MUSIC} |
| 620 | * <li>{@link #DIRECTORY_PODCASTS} |
| 621 | * <li>{@link #DIRECTORY_ALARMS} |
| 622 | * <li>{@link #DIRECTORY_RINGTONES} |
| 623 | * <li>{@link #DIRECTORY_NOTIFICATIONS} |
| 624 | * <li>{@link #DIRECTORY_PICTURES} |
| 625 | * <li>{@link #DIRECTORY_MOVIES} |
| 626 | * <li>{@link #DIRECTORY_DOWNLOADS} |
| 627 | * <li>{@link #DIRECTORY_DCIM} |
| 628 | * <li>{@link #DIRECTORY_DOCUMENTS} |
| 629 | * </ul> |
| 630 | * @hide |
| 631 | */ |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 632 | public static final String[] STANDARD_DIRECTORIES = { |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 633 | DIRECTORY_MUSIC, |
| 634 | DIRECTORY_PODCASTS, |
| 635 | DIRECTORY_RINGTONES, |
| 636 | DIRECTORY_ALARMS, |
| 637 | DIRECTORY_NOTIFICATIONS, |
| 638 | DIRECTORY_PICTURES, |
| 639 | DIRECTORY_MOVIES, |
| 640 | DIRECTORY_DOWNLOADS, |
| 641 | DIRECTORY_DCIM, |
Steve McKay | ab3b893 | 2016-02-16 11:37:03 -0800 | [diff] [blame] | 642 | DIRECTORY_DOCUMENTS |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 643 | }; |
| 644 | |
| 645 | /** |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 646 | * @hide |
| 647 | */ |
| 648 | public static boolean isStandardDirectory(String dir) { |
| 649 | for (String valid : STANDARD_DIRECTORIES) { |
| 650 | if (valid.equals(dir)) { |
| 651 | return true; |
| 652 | } |
| 653 | } |
| 654 | return false; |
| 655 | } |
| 656 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 657 | /** {@hide} */ public static final int HAS_MUSIC = 1 << 0; |
| 658 | /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1; |
| 659 | /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2; |
| 660 | /** {@hide} */ public static final int HAS_ALARMS = 1 << 3; |
| 661 | /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4; |
| 662 | /** {@hide} */ public static final int HAS_PICTURES = 1 << 5; |
| 663 | /** {@hide} */ public static final int HAS_MOVIES = 1 << 6; |
| 664 | /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7; |
| 665 | /** {@hide} */ public static final int HAS_DCIM = 1 << 8; |
| 666 | /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9; |
| 667 | |
| 668 | /** {@hide} */ public static final int HAS_ANDROID = 1 << 16; |
| 669 | /** {@hide} */ public static final int HAS_OTHER = 1 << 17; |
| 670 | |
| 671 | /** |
| 672 | * Classify the content types present on the given external storage device. |
| 673 | * <p> |
| 674 | * This is typically useful for deciding if an inserted SD card is empty, or |
| 675 | * if it contains content like photos that should be preserved. |
| 676 | * |
| 677 | * @hide |
| 678 | */ |
| 679 | public static int classifyExternalStorageDirectory(File dir) { |
| 680 | int res = 0; |
| 681 | for (File f : FileUtils.listFilesOrEmpty(dir)) { |
| 682 | if (f.isFile() && isInterestingFile(f)) { |
| 683 | res |= HAS_OTHER; |
| 684 | } else if (f.isDirectory() && hasInterestingFiles(f)) { |
| 685 | final String name = f.getName(); |
| 686 | if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC; |
| 687 | else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS; |
| 688 | else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES; |
| 689 | else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS; |
| 690 | else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS; |
| 691 | else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES; |
| 692 | else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES; |
| 693 | else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS; |
| 694 | else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM; |
| 695 | else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS; |
| 696 | else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID; |
| 697 | else res |= HAS_OTHER; |
| 698 | } |
| 699 | } |
| 700 | return res; |
| 701 | } |
| 702 | |
| 703 | private static boolean hasInterestingFiles(File dir) { |
| 704 | final LinkedList<File> explore = new LinkedList<>(); |
| 705 | explore.add(dir); |
| 706 | while (!explore.isEmpty()) { |
| 707 | dir = explore.pop(); |
| 708 | for (File f : FileUtils.listFilesOrEmpty(dir)) { |
| 709 | if (isInterestingFile(f)) return true; |
| 710 | if (f.isDirectory()) explore.add(f); |
| 711 | } |
| 712 | } |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | private static boolean isInterestingFile(File file) { |
| 717 | if (file.isFile()) { |
| 718 | final String name = file.getName().toLowerCase(); |
| 719 | if (name.endsWith(".exe") || name.equals("autorun.inf") |
| 720 | || name.equals("launchpad.zip") || name.equals(".nomedia")) { |
| 721 | return false; |
| 722 | } else { |
| 723 | return true; |
| 724 | } |
| 725 | } else { |
| 726 | return false; |
| 727 | } |
| 728 | } |
| 729 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 730 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 731 | * Get a top-level shared/external storage directory for placing files of a |
| 732 | * particular type. This is where the user will typically place and manage |
| 733 | * their own files, so you should be careful about what you put here to |
| 734 | * ensure you don't erase their files or get in the way of their own |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 735 | * organization. |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 736 | * <p> |
| 737 | * On devices with multiple users (as described by {@link UserManager}), |
| 738 | * each user has their own isolated shared storage. Applications only have |
| 739 | * access to the shared storage for the user they're running as. |
| 740 | * </p> |
| 741 | * <p> |
| 742 | * Here is an example of typical code to manipulate a picture on the public |
| 743 | * shared storage: |
| 744 | * </p> |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 745 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java |
| 746 | * public_picture} |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 747 | * |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 748 | * @param type The type of storage directory to return. Should be one of |
| 749 | * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS}, |
| 750 | * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS}, |
| 751 | * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES}, |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 752 | * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, |
| 753 | * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null. |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 754 | * @return Returns the File path for the directory. Note that this directory |
| 755 | * may not yet exist, so you must make sure it exists before using |
| 756 | * it such as with {@link File#mkdirs File.mkdirs()}. |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 757 | */ |
| 758 | public static File getExternalStoragePublicDirectory(String type) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 759 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 760 | return sCurrentUser.buildExternalStoragePublicDirs(type)[0]; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Returns the path for android-specific data on the SD card. |
| 765 | * @hide |
| 766 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 767 | public static File[] buildExternalStorageAndroidDataDirs() { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 768 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 769 | return sCurrentUser.buildExternalStorageAndroidDataDirs(); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 770 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 771 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 772 | /** |
| 773 | * Generates the raw path to an application's data |
| 774 | * @hide |
| 775 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 776 | public static File[] buildExternalStorageAppDataDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 777 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 778 | return sCurrentUser.buildExternalStorageAppDataDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 779 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 780 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 781 | /** |
| 782 | * Generates the raw path to an application's media |
| 783 | * @hide |
| 784 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 785 | public static File[] buildExternalStorageAppMediaDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 786 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 787 | return sCurrentUser.buildExternalStorageAppMediaDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 788 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 789 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 790 | /** |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 791 | * Generates the raw path to an application's OBB files |
| 792 | * @hide |
| 793 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 794 | public static File[] buildExternalStorageAppObbDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 795 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 796 | return sCurrentUser.buildExternalStorageAppObbDirs(packageName); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 797 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 798 | |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 799 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 800 | * Generates the path to an application's files. |
| 801 | * @hide |
| 802 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 803 | public static File[] buildExternalStorageAppFilesDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 804 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 805 | return sCurrentUser.buildExternalStorageAppFilesDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 806 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 807 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 808 | /** |
| 809 | * Generates the path to an application's cache. |
| 810 | * @hide |
| 811 | */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 812 | public static File[] buildExternalStorageAppCacheDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 813 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 814 | return sCurrentUser.buildExternalStorageAppCacheDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 815 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 816 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 817 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 818 | * Return the download/cache content directory. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | */ |
| 820 | public static File getDownloadCacheDirectory() { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 821 | return DIR_DOWNLOAD_CACHE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 825 | * Unknown storage state, such as when a path isn't backed by known storage |
| 826 | * media. |
| 827 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 828 | * @see #getExternalStorageState(File) |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 829 | */ |
| 830 | public static final String MEDIA_UNKNOWN = "unknown"; |
| 831 | |
| 832 | /** |
| 833 | * Storage state if the media is not present. |
| 834 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 835 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | */ |
| 837 | public static final String MEDIA_REMOVED = "removed"; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 840 | * Storage state if the media is present but not mounted. |
| 841 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 842 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | */ |
| 844 | public static final String MEDIA_UNMOUNTED = "unmounted"; |
| 845 | |
| 846 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 847 | * Storage state if the media is present and being disk-checked. |
| 848 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 849 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | */ |
| 851 | public static final String MEDIA_CHECKING = "checking"; |
| 852 | |
| 853 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 854 | * Storage state if the media is present but is blank or is using an |
| 855 | * unsupported filesystem. |
| 856 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 857 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | */ |
| 859 | public static final String MEDIA_NOFS = "nofs"; |
| 860 | |
| 861 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 862 | * Storage state if the media is present and mounted at its mount point with |
| 863 | * read/write access. |
| 864 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 865 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | */ |
| 867 | public static final String MEDIA_MOUNTED = "mounted"; |
| 868 | |
| 869 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 870 | * Storage state if the media is present and mounted at its mount point with |
| 871 | * read-only access. |
| 872 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 873 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | */ |
| 875 | public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro"; |
| 876 | |
| 877 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 878 | * Storage state if the media is present not mounted, and shared via USB |
| 879 | * mass storage. |
| 880 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 881 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | */ |
| 883 | public static final String MEDIA_SHARED = "shared"; |
| 884 | |
| 885 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 886 | * Storage state if the media was removed before it was unmounted. |
| 887 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 888 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | */ |
| 890 | public static final String MEDIA_BAD_REMOVAL = "bad_removal"; |
| 891 | |
| 892 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 893 | * Storage state if the media is present but cannot be mounted. Typically |
| 894 | * this happens if the file system on the media is corrupted. |
| 895 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 896 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | */ |
| 898 | public static final String MEDIA_UNMOUNTABLE = "unmountable"; |
| 899 | |
| 900 | /** |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 901 | * Storage state if the media is in the process of being ejected. |
| 902 | * |
| 903 | * @see #getExternalStorageState(File) |
| 904 | */ |
| 905 | public static final String MEDIA_EJECTING = "ejecting"; |
| 906 | |
| 907 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 908 | * Returns the current state of the primary shared/external storage media. |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 909 | * |
Jeff Sharkey | 8c16579 | 2012-10-22 14:08:29 -0700 | [diff] [blame] | 910 | * @see #getExternalStorageDirectory() |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 911 | * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED}, |
| 912 | * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING}, |
| 913 | * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED}, |
| 914 | * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED}, |
| 915 | * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | */ |
| 917 | public static String getExternalStorageState() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 918 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 919 | return getExternalStorageState(externalDir); |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * @deprecated use {@link #getExternalStorageState(File)} |
| 924 | */ |
| 925 | @Deprecated |
| 926 | public static String getStorageState(File path) { |
| 927 | return getExternalStorageState(path); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 931 | * Returns the current state of the shared/external storage media at the |
| 932 | * given path. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 933 | * |
| 934 | * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED}, |
| 935 | * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING}, |
| 936 | * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED}, |
| 937 | * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED}, |
| 938 | * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}. |
| 939 | */ |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 940 | public static String getExternalStorageState(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 941 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 942 | if (volume != null) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 943 | return volume.getState(); |
| 944 | } else { |
| 945 | return MEDIA_UNKNOWN; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 946 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 949 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 950 | * Returns whether the primary shared/external storage media is physically |
| 951 | * removable. |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 952 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 953 | * @return true if the storage device can be removed (such as an SD card), |
| 954 | * or false if the storage device is built in and cannot be |
| 955 | * physically removed. |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 956 | */ |
| 957 | public static boolean isExternalStorageRemovable() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 958 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 959 | return isExternalStorageRemovable(externalDir); |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 962 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 963 | * Returns whether the shared/external storage media at the given path is |
| 964 | * physically removable. |
Andy Stadler | 50c294f | 2011-03-07 19:13:42 -0800 | [diff] [blame] | 965 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 966 | * @return true if the storage device can be removed (such as an SD card), |
| 967 | * or false if the storage device is built in and cannot be |
| 968 | * physically removed. |
| 969 | * @throws IllegalArgumentException if the path is not a valid storage |
| 970 | * device. |
| 971 | */ |
| 972 | public static boolean isExternalStorageRemovable(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 973 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 974 | if (volume != null) { |
| 975 | return volume.isRemovable(); |
| 976 | } else { |
| 977 | throw new IllegalArgumentException("Failed to find storage device at " + path); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 982 | * Returns whether the primary shared/external storage media is emulated. |
| 983 | * <p> |
| 984 | * The contents of emulated storage devices are backed by a private user |
| 985 | * data partition, which means there is little benefit to apps storing data |
| 986 | * here instead of the private directories returned by |
| 987 | * {@link Context#getFilesDir()}, etc. |
| 988 | * <p> |
| 989 | * This returns true when emulated storage is backed by either internal |
| 990 | * storage or an adopted storage device. |
Andy Stadler | 50c294f | 2011-03-07 19:13:42 -0800 | [diff] [blame] | 991 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 992 | * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName, |
| 993 | * boolean) |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 994 | */ |
| 995 | public static boolean isExternalStorageEmulated() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 996 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 997 | return isExternalStorageEmulated(externalDir); |
| 998 | } |
| 999 | |
| 1000 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 1001 | * Returns whether the shared/external storage media at the given path is |
| 1002 | * emulated. |
| 1003 | * <p> |
| 1004 | * The contents of emulated storage devices are backed by a private user |
| 1005 | * data partition, which means there is little benefit to apps storing data |
| 1006 | * here instead of the private directories returned by |
| 1007 | * {@link Context#getFilesDir()}, etc. |
| 1008 | * <p> |
| 1009 | * This returns true when emulated storage is backed by either internal |
| 1010 | * storage or an adopted storage device. |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1011 | * |
| 1012 | * @throws IllegalArgumentException if the path is not a valid storage |
| 1013 | * device. |
| 1014 | */ |
| 1015 | public static boolean isExternalStorageEmulated(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1016 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1017 | if (volume != null) { |
| 1018 | return volume.isEmulated(); |
| 1019 | } else { |
| 1020 | throw new IllegalArgumentException("Failed to find storage device at " + path); |
| 1021 | } |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | static File getDirectory(String variableName, String defaultPath) { |
| 1025 | String path = System.getenv(variableName); |
| 1026 | return path == null ? new File(defaultPath) : new File(path); |
| 1027 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1028 | |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 1029 | /** {@hide} */ |
| 1030 | public static void setUserRequired(boolean userRequired) { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 1031 | sUserRequired = userRequired; |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | private static void throwIfUserRequired() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 1035 | if (sUserRequired) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 1036 | Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment", |
| 1037 | new Throwable()); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1041 | /** |
| 1042 | * Append path segments to each given base path, returning result. |
| 1043 | * |
| 1044 | * @hide |
| 1045 | */ |
| 1046 | public static File[] buildPaths(File[] base, String... segments) { |
| 1047 | File[] result = new File[base.length]; |
| 1048 | for (int i = 0; i < base.length; i++) { |
| 1049 | result[i] = buildPath(base[i], segments); |
| 1050 | } |
| 1051 | return result; |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * Append path segments to given base path, returning result. |
| 1056 | * |
| 1057 | * @hide |
| 1058 | */ |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 1059 | @TestApi |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1060 | public static File buildPath(File base, String... segments) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1061 | File cur = base; |
| 1062 | for (String segment : segments) { |
| 1063 | if (cur == null) { |
| 1064 | cur = new File(segment); |
| 1065 | } else { |
| 1066 | cur = new File(cur, segment); |
| 1067 | } |
| 1068 | } |
| 1069 | return cur; |
| 1070 | } |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 1071 | |
| 1072 | /** |
| 1073 | * If the given path exists on emulated external storage, return the |
| 1074 | * translated backing path hosted on internal storage. This bypasses any |
| 1075 | * emulation later, improving performance. This is <em>only</em> suitable |
| 1076 | * for read-only access. |
| 1077 | * <p> |
| 1078 | * Returns original path if given path doesn't meet these criteria. Callers |
| 1079 | * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE} |
| 1080 | * permission. |
| 1081 | * |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame^] | 1082 | * @deprecated disabled now that FUSE has been replaced by sdcardfs |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 1083 | * @hide |
| 1084 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame^] | 1085 | @Deprecated |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 1086 | public static File maybeTranslateEmulatedPathToInternal(File path) { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1087 | return StorageManager.maybeTranslateEmulatedPathToInternal(path); |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 1088 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | } |