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