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