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