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