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