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