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