blob: e96398a76ef24fc427e30251bd8d573a166c0191 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.os;
18
Jeff Sharkey4ca728c2014-01-10 16:27:19 -080019import android.app.admin.DevicePolicyManager;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070020import android.content.Context;
San Mehatb1043402010-02-05 08:26:50 -080021import android.os.storage.IMountService;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070022import android.os.storage.StorageVolume;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070023import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080024import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080025
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070026import com.google.android.collect.Lists;
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080027
Gilles Debunneee1d6302011-05-13 10:09:32 -070028import java.io.File;
Jeff Sharkey63d0a062013-03-01 16:12:55 -080029import java.io.IOException;
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070030import java.util.ArrayList;
Gilles Debunneee1d6302011-05-13 10:09:32 -070031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032/**
33 * Provides access to environment variables.
34 */
35public class Environment {
Kenny Rootb2278dc2011-01-18 13:03:28 -080036 private static final String TAG = "Environment";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Jeff Sharkeyb049e212012-09-07 23:16:01 -070038 private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
Jeff Sharkey4fbbda42012-09-24 18:34:07 -070039 private static final String ENV_EMULATED_STORAGE_SOURCE = "EMULATED_STORAGE_SOURCE";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070040 private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET";
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070041 private static final String ENV_MEDIA_STORAGE = "MEDIA_STORAGE";
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070042 private static final String ENV_SECONDARY_STORAGE = "SECONDARY_STORAGE";
Jeff Sharkey63d0a062013-03-01 16:12:55 -080043 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkey1be762c2014-03-06 09:56:23 -080044 private static final String ENV_OEM_ROOT = "OEM_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070045
Jeff Sharkeydfa45302012-09-12 16:25:22 -070046 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070047 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 Sharkeydfa45302012-09-12 16:25:22 -070057
Jeff Sharkey63d0a062013-03-01 16:12:55 -080058 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080059 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Jeff Sharkey63d0a062013-03-01 16:12:55 -080060 private static final File DIR_MEDIA_STORAGE = getDirectory(ENV_MEDIA_STORAGE, "/data/media");
61
62 private static final String CANONCIAL_EMULATED_STORAGE_TARGET = getCanonicalPathOrNull(
63 ENV_EMULATED_STORAGE_TARGET);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Oscar Montemayora8529f62009-11-18 10:14:20 -080065 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
66
Jeff Sharkeyb049e212012-09-07 23:16:01 -070067 private static UserEnvironment sCurrentUser;
Jeff Sharkey48749fc2013-04-19 13:25:04 -070068 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070069
Jeff Sharkeyb049e212012-09-07 23:16:01 -070070 static {
71 initForCurrentUser();
72 }
73
74 /** {@hide} */
75 public static void initForCurrentUser() {
76 final int userId = UserHandle.myUserId();
77 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070078 }
79
80 /** {@hide} */
81 public static class UserEnvironment {
82 // TODO: generalize further to create package-specific environment
83
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070084 /** External storage dirs, as visible to vold */
85 private final File[] mExternalDirsForVold;
86 /** External storage dirs, as visible to apps */
87 private final File[] mExternalDirsForApp;
88 /** Primary emulated storage dir for direct access */
89 private final File mEmulatedDirForDirect;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070090
91 public UserEnvironment(int userId) {
92 // See storage config details at http://source.android.com/tech/storage/
93 String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070094 String rawEmulatedSource = System.getenv(ENV_EMULATED_STORAGE_SOURCE);
95 String rawEmulatedTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070096
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070097 String rawMediaStorage = System.getenv(ENV_MEDIA_STORAGE);
98 if (TextUtils.isEmpty(rawMediaStorage)) {
99 rawMediaStorage = "/data/media";
100 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700101
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700102 ArrayList<File> externalForVold = Lists.newArrayList();
103 ArrayList<File> externalForApp = Lists.newArrayList();
104
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700105 if (!TextUtils.isEmpty(rawEmulatedTarget)) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700106 // Device has emulated storage; external storage paths should have
107 // userId burned into them.
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700108 final String rawUserId = Integer.toString(userId);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700109 final File emulatedSourceBase = new File(rawEmulatedSource);
110 final File emulatedTargetBase = new File(rawEmulatedTarget);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700111 final File mediaBase = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700112
113 // /storage/emulated/0
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700114 externalForVold.add(buildPath(emulatedSourceBase, rawUserId));
115 externalForApp.add(buildPath(emulatedTargetBase, rawUserId));
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700116 // /data/media/0
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700117 mEmulatedDirForDirect = buildPath(mediaBase, rawUserId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700118
119 } else {
120 // Device has physical external storage; use plain paths.
121 if (TextUtils.isEmpty(rawExternalStorage)) {
122 Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default");
123 rawExternalStorage = "/storage/sdcard0";
124 }
125
126 // /storage/sdcard0
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700127 externalForVold.add(new File(rawExternalStorage));
128 externalForApp.add(new File(rawExternalStorage));
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700129 // /data/media
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700130 mEmulatedDirForDirect = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700131 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700132
133 // Splice in any secondary storage paths, but only for owner
134 final String rawSecondaryStorage = System.getenv(ENV_SECONDARY_STORAGE);
135 if (!TextUtils.isEmpty(rawSecondaryStorage) && userId == UserHandle.USER_OWNER) {
136 for (String secondaryPath : rawSecondaryStorage.split(":")) {
137 externalForVold.add(new File(secondaryPath));
138 externalForApp.add(new File(secondaryPath));
139 }
140 }
141
142 mExternalDirsForVold = externalForVold.toArray(new File[externalForVold.size()]);
143 mExternalDirsForApp = externalForApp.toArray(new File[externalForApp.size()]);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700144 }
145
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700146 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700147 public File getExternalStorageDirectory() {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700148 return mExternalDirsForApp[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700149 }
150
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700151 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700152 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700153 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700154 }
155
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700156 public File[] getExternalDirsForVold() {
157 return mExternalDirsForVold;
158 }
159
160 public File[] getExternalDirsForApp() {
161 return mExternalDirsForApp;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700162 }
163
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700164 public File getMediaDir() {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700165 return mEmulatedDirForDirect;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700166 }
167
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700168 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700169 return buildPaths(mExternalDirsForApp, type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700170 }
171
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700172 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700173 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700174 }
175
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700176 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700177 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700178 }
179
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700180 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700181 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA, packageName);
182 }
183
184 public File[] buildExternalStorageAppDataDirsForVold(String packageName) {
185 return buildPaths(mExternalDirsForVold, DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700186 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700187
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700188 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700189 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700190 }
191
192 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700193 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_OBB, packageName);
194 }
195
196 public File[] buildExternalStorageAppObbDirsForVold(String packageName) {
197 return buildPaths(mExternalDirsForVold, DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700198 }
199
200 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700201 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700202 }
203
204 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700205 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700206 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700207 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800210 * Return root of the "system" partition holding the core Android OS.
211 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 */
213 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800214 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 }
216
Jason parksa3cdaa52011-01-13 14:15:43 -0600217 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800218 * Return root directory of the "oem" partition holding OEM customizations,
219 * if any. If present, the partition is mounted read-only.
220 *
221 * @hide
222 */
223 public static File getOemDirectory() {
224 return DIR_OEM_ROOT;
225 }
226
227 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600228 * Gets the system directory available for secure storage.
229 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
230 * Otherwise, it returns the unencrypted /data/system directory.
231 * @return File object representing the secure storage system directory.
232 * @hide
233 */
234 public static File getSystemSecureDirectory() {
235 if (isEncryptedFilesystemEnabled()) {
236 return new File(SECURE_DATA_DIRECTORY, "system");
237 } else {
238 return new File(DATA_DIRECTORY, "system");
239 }
240 }
241
242 /**
243 * Gets the data directory for secure storage.
244 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
245 * Otherwise, it returns the unencrypted /data directory.
246 * @return File object representing the data directory for secure storage.
247 * @hide
248 */
249 public static File getSecureDataDirectory() {
250 if (isEncryptedFilesystemEnabled()) {
251 return SECURE_DATA_DIRECTORY;
252 } else {
253 return DATA_DIRECTORY;
254 }
255 }
256
257 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700258 * Return directory used for internal media storage, which is protected by
259 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
260 *
261 * @hide
262 */
263 public static File getMediaStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700264 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700265 return sCurrentUser.getMediaDir();
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700266 }
267
268 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700269 * Return the system directory for a user. This is for use by system services to store
270 * files relating to the user. This directory will be automatically deleted when the user
271 * is removed.
272 *
273 * @hide
274 */
275 public static File getUserSystemDirectory(int userId) {
276 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
277 }
278
279 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600280 * Returns whether the Encrypted File System feature is enabled on the device or not.
281 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
282 * if disabled.
283 * @hide
284 */
285 public static boolean isEncryptedFilesystemEnabled() {
286 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
287 }
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 private static final File DATA_DIRECTORY
290 = getDirectory("ANDROID_DATA", "/data");
291
Oscar Montemayora8529f62009-11-18 10:14:20 -0800292 /**
293 * @hide
294 */
295 private static final File SECURE_DATA_DIRECTORY
296 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
297
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700298 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
300 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700301 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 */
303 public static File getDataDirectory() {
304 return DATA_DIRECTORY;
305 }
306
307 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700308 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800309 * currently be accessible if it has been mounted by the user on their
310 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700311 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800312 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700313 * <p>
314 * <em>Note: don't be confused by the word "external" here. This directory
315 * can better be thought as media/shared storage. It is a filesystem that
316 * can hold a relatively large amount of data and that is shared across all
317 * applications (does not enforce permissions). Traditionally this is an SD
318 * card, but it may also be implemented as built-in storage in a device that
319 * is distinct from the protected internal storage and can be mounted as a
320 * filesystem on a computer.</em>
321 * <p>
322 * On devices with multiple users (as described by {@link UserManager}),
323 * each user has their own isolated external storage. Applications only have
324 * access to the external storage for the user they're running as.
325 * <p>
326 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700327 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700328 * with. Access to secondary storage is available through
329 * <p>
330 * Applications should not directly use this top-level directory, in order
331 * to avoid polluting the user's root namespace. Any files that are private
332 * to the application should be placed in a directory returned by
333 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700334 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700335 * if the application is uninstalled. Other shared files should be placed in
336 * one of the directories returned by
337 * {@link #getExternalStoragePublicDirectory}.
338 * <p>
339 * Writing to this path requires the
340 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
341 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700342 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700343 * which is automatically granted if you hold the write permission.
344 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700345 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700346 * application only needs to store internal data, consider using
347 * {@link Context#getExternalFilesDir(String)} or
348 * {@link Context#getExternalCacheDir()}, which require no permissions to
349 * read or write.
350 * <p>
351 * This path may change between platform versions, so applications should
352 * only persist relative paths.
353 * <p>
354 * Here is an example of typical code to monitor the state of external
355 * storage:
356 * <p>
357 * {@sample
358 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800359 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700360 *
361 * @see #getExternalStorageState()
362 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 */
364 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700365 throwIfUserRequired();
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700366 return sCurrentUser.getExternalDirsForApp()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700367 }
368
369 /** {@hide} */
370 public static File getLegacyExternalStorageDirectory() {
371 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700374 /** {@hide} */
375 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700376 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700377 }
378
379 /** {@hide} */
380 public static File getEmulatedStorageSource(int userId) {
381 // /mnt/shell/emulated/0
382 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), String.valueOf(userId));
383 }
384
385 /** {@hide} */
386 public static File getEmulatedStorageObbSource() {
387 // /mnt/shell/emulated/obb
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700388 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700389 }
390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800392 * Standard directory in which to place any audio files that should be
393 * in the regular list of music for the user.
394 * This may be combined with
395 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
396 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
397 * of directories to categories a particular audio file as more than one
398 * type.
399 */
400 public static String DIRECTORY_MUSIC = "Music";
401
402 /**
403 * Standard directory in which to place any audio files that should be
404 * in the list of podcasts that the user can select (not as regular
405 * music).
406 * This may be combined with {@link #DIRECTORY_MUSIC},
407 * {@link #DIRECTORY_NOTIFICATIONS},
408 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
409 * of directories to categories a particular audio file as more than one
410 * type.
411 */
412 public static String DIRECTORY_PODCASTS = "Podcasts";
413
414 /**
415 * Standard directory in which to place any audio files that should be
416 * in the list of ringtones that the user can select (not as regular
417 * music).
418 * This may be combined with {@link #DIRECTORY_MUSIC},
419 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
420 * {@link #DIRECTORY_ALARMS} as a series
421 * of directories to categories a particular audio file as more than one
422 * type.
423 */
424 public static String DIRECTORY_RINGTONES = "Ringtones";
425
426 /**
427 * Standard directory in which to place any audio files that should be
428 * in the list of alarms that the user can select (not as regular
429 * music).
430 * This may be combined with {@link #DIRECTORY_MUSIC},
431 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
432 * and {@link #DIRECTORY_RINGTONES} as a series
433 * of directories to categories a particular audio file as more than one
434 * type.
435 */
436 public static String DIRECTORY_ALARMS = "Alarms";
437
438 /**
439 * Standard directory in which to place any audio files that should be
440 * in the list of notifications that the user can select (not as regular
441 * music).
442 * This may be combined with {@link #DIRECTORY_MUSIC},
443 * {@link #DIRECTORY_PODCASTS},
444 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
445 * of directories to categories a particular audio file as more than one
446 * type.
447 */
448 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
449
450 /**
451 * Standard directory in which to place pictures that are available to
452 * the user. Note that this is primarily a convention for the top-level
453 * public directory, as the media scanner will find and collect pictures
454 * in any directory.
455 */
456 public static String DIRECTORY_PICTURES = "Pictures";
457
458 /**
459 * Standard directory in which to place movies that are available to
460 * the user. Note that this is primarily a convention for the top-level
461 * public directory, as the media scanner will find and collect movies
462 * in any directory.
463 */
464 public static String DIRECTORY_MOVIES = "Movies";
465
466 /**
467 * Standard directory in which to place files that have been downloaded by
468 * the user. Note that this is primarily a convention for the top-level
469 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700470 * private directories. Also note that though the constant here is
471 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
472 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800473 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700474 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800475
476 /**
477 * The traditional location for pictures and videos when mounting the
478 * device as a camera. Note that this is primarily a convention for the
479 * top-level public directory, as this convention makes no sense elsewhere.
480 */
481 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700482
483 /**
484 * Standard directory in which to place documents that have been created by
485 * the user.
486 */
487 public static String DIRECTORY_DOCUMENTS = "Documents";
488
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800489 /**
490 * Get a top-level public external storage directory for placing files of
491 * a particular type. This is where the user will typically place and
492 * manage their own files, so you should be careful about what you put here
493 * to ensure you don't erase their files or get in the way of their own
494 * organization.
495 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700496 * <p>On devices with multiple users (as described by {@link UserManager}),
497 * each user has their own isolated external storage. Applications only
498 * have access to the external storage for the user they're running as.</p>
499 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800500 * <p>Here is an example of typical code to manipulate a picture on
501 * the public external storage:</p>
502 *
503 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
504 * public_picture}
505 *
506 * @param type The type of storage directory to return. Should be one of
507 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
508 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
509 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
510 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
511 * {@link #DIRECTORY_DCIM}. May not be null.
512 *
513 * @return Returns the File path for the directory. Note that this
514 * directory may not yet exist, so you must make sure it exists before
515 * using it such as with {@link File#mkdirs File.mkdirs()}.
516 */
517 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700518 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700519 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800520 }
521
522 /**
523 * Returns the path for android-specific data on the SD card.
524 * @hide
525 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700526 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700527 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700528 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800529 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700530
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800531 /**
532 * Generates the raw path to an application's data
533 * @hide
534 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700535 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700536 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700537 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800538 }
539
540 /**
541 * Generates the raw path to an application's media
542 * @hide
543 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700544 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700545 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700546 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800547 }
548
549 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800550 * Generates the raw path to an application's OBB files
551 * @hide
552 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700553 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700554 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700555 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800556 }
557
558 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800559 * Generates the path to an application's files.
560 * @hide
561 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700562 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700563 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700564 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800565 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700566
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800567 /**
568 * Generates the path to an application's cache.
569 * @hide
570 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700571 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700572 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700573 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800574 }
575
576 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700577 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 */
579 public static File getDownloadCacheDirectory() {
580 return DOWNLOAD_CACHE_DIRECTORY;
581 }
582
583 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700584 * Unknown storage state, such as when a path isn't backed by known storage
585 * media.
586 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800587 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700588 */
589 public static final String MEDIA_UNKNOWN = "unknown";
590
591 /**
592 * Storage state if the media is not present.
593 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800594 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 */
596 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700599 * Storage state if the media is present but not mounted.
600 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800601 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 */
603 public static final String MEDIA_UNMOUNTED = "unmounted";
604
605 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700606 * Storage state if the media is present and being disk-checked.
607 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800608 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 */
610 public static final String MEDIA_CHECKING = "checking";
611
612 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700613 * Storage state if the media is present but is blank or is using an
614 * unsupported filesystem.
615 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800616 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 */
618 public static final String MEDIA_NOFS = "nofs";
619
620 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700621 * Storage state if the media is present and mounted at its mount point with
622 * read/write access.
623 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800624 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 */
626 public static final String MEDIA_MOUNTED = "mounted";
627
628 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700629 * Storage state if the media is present and mounted at its mount point with
630 * read-only access.
631 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800632 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 */
634 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
635
636 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700637 * Storage state if the media is present not mounted, and shared via USB
638 * mass storage.
639 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800640 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 */
642 public static final String MEDIA_SHARED = "shared";
643
644 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700645 * Storage state if the media was removed before it was unmounted.
646 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800647 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 */
649 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
650
651 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700652 * Storage state if the media is present but cannot be mounted. Typically
653 * this happens if the file system on the media is corrupted.
654 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800655 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 */
657 public static final String MEDIA_UNMOUNTABLE = "unmountable";
658
659 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700660 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800661 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700662 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700663 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
664 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
665 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
666 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
667 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 */
669 public static String getExternalStorageState() {
Jeff Sharkeya6d19992013-10-14 16:43:42 -0700670 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800671 return getExternalStorageState(externalDir);
672 }
673
674 /**
675 * @deprecated use {@link #getExternalStorageState(File)}
676 */
677 @Deprecated
678 public static String getStorageState(File path) {
679 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700680 }
681
682 /**
683 * Returns the current state of the storage device that provides the given
684 * path.
685 *
686 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
687 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
688 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
689 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
690 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
691 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800692 public static String getExternalStorageState(File path) {
693 final StorageVolume volume = getStorageVolume(path);
694 if (volume != null) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700695 final IMountService mountService = IMountService.Stub.asInterface(
696 ServiceManager.getService("mount"));
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800697 try {
698 return mountService.getVolumeState(volume.getPath());
699 } catch (RemoteException e) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700700 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700701 }
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800702
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700703 return Environment.MEDIA_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
Dianne Hackborn407f6252010-10-04 11:31:17 -0700706 /**
707 * Returns whether the primary "external" storage device is removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700708 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800709 * @return true if the storage device can be removed (such as an SD card),
710 * or false if the storage device is built in and cannot be
711 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700712 */
713 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800714 if (isStorageDisabled()) return false;
715 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
716 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700717 }
718
Kenny Roote1ff2142010-10-12 11:20:01 -0700719 /**
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800720 * Returns whether the storage device that provides the given path is
721 * removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800722 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800723 * @return true if the storage device can be removed (such as an SD card),
724 * or false if the storage device is built in and cannot be
725 * physically removed.
726 * @throws IllegalArgumentException if the path is not a valid storage
727 * device.
728 */
729 public static boolean isExternalStorageRemovable(File path) {
730 final StorageVolume volume = getStorageVolume(path);
731 if (volume != null) {
732 return volume.isRemovable();
733 } else {
734 throw new IllegalArgumentException("Failed to find storage device at " + path);
735 }
736 }
737
738 /**
739 * Returns whether the primary "external" storage device is emulated. If
740 * true, data stored on this device will be stored on a portion of the
741 * internal storage system.
Andy Stadler50c294f2011-03-07 19:13:42 -0800742 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800743 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
744 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700745 */
746 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800747 if (isStorageDisabled()) return false;
748 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
749 return isExternalStorageEmulated(externalDir);
750 }
751
752 /**
753 * Returns whether the storage device that provides the given path is
754 * emulated. If true, data stored on this device will be stored on a portion
755 * of the internal storage system.
756 *
757 * @throws IllegalArgumentException if the path is not a valid storage
758 * device.
759 */
760 public static boolean isExternalStorageEmulated(File path) {
761 final StorageVolume volume = getStorageVolume(path);
762 if (volume != null) {
763 return volume.isEmulated();
764 } else {
765 throw new IllegalArgumentException("Failed to find storage device at " + path);
766 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700767 }
768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 static File getDirectory(String variableName, String defaultPath) {
770 String path = System.getenv(variableName);
771 return path == null ? new File(defaultPath) : new File(path);
772 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700773
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800774 private static String getCanonicalPathOrNull(String variableName) {
775 String path = System.getenv(variableName);
776 if (path == null) {
777 return null;
778 }
779 try {
780 return new File(path).getCanonicalPath();
781 } catch (IOException e) {
782 Log.w(TAG, "Unable to resolve canonical path for " + path);
783 return null;
784 }
785 }
786
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700787 /** {@hide} */
788 public static void setUserRequired(boolean userRequired) {
789 sUserRequired = userRequired;
790 }
791
792 private static void throwIfUserRequired() {
793 if (sUserRequired) {
794 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
795 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700796 }
797 }
798
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700799 /**
800 * Append path segments to each given base path, returning result.
801 *
802 * @hide
803 */
804 public static File[] buildPaths(File[] base, String... segments) {
805 File[] result = new File[base.length];
806 for (int i = 0; i < base.length; i++) {
807 result[i] = buildPath(base[i], segments);
808 }
809 return result;
810 }
811
812 /**
813 * Append path segments to given base path, returning result.
814 *
815 * @hide
816 */
817 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700818 File cur = base;
819 for (String segment : segments) {
820 if (cur == null) {
821 cur = new File(segment);
822 } else {
823 cur = new File(cur, segment);
824 }
825 }
826 return cur;
827 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800828
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800829 private static boolean isStorageDisabled() {
830 return SystemProperties.getBoolean("config.disable_storage", false);
831 }
832
833 private static StorageVolume getStorageVolume(File path) {
834 try {
835 path = path.getCanonicalFile();
836 } catch (IOException e) {
837 return null;
838 }
839
840 try {
841 final IMountService mountService = IMountService.Stub.asInterface(
842 ServiceManager.getService("mount"));
843 final StorageVolume[] volumes = mountService.getVolumeList();
844 for (StorageVolume volume : volumes) {
845 if (FileUtils.contains(volume.getPathFile(), path)) {
846 return volume;
847 }
848 }
849 } catch (RemoteException e) {
850 }
851
852 return null;
853 }
854
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800855 /**
856 * If the given path exists on emulated external storage, return the
857 * translated backing path hosted on internal storage. This bypasses any
858 * emulation later, improving performance. This is <em>only</em> suitable
859 * for read-only access.
860 * <p>
861 * Returns original path if given path doesn't meet these criteria. Callers
862 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
863 * permission.
864 *
865 * @hide
866 */
867 public static File maybeTranslateEmulatedPathToInternal(File path) {
868 // Fast return if not emulated, or missing variables
869 if (!Environment.isExternalStorageEmulated()
870 || CANONCIAL_EMULATED_STORAGE_TARGET == null) {
871 return path;
872 }
873
874 try {
875 final String rawPath = path.getCanonicalPath();
876 if (rawPath.startsWith(CANONCIAL_EMULATED_STORAGE_TARGET)) {
877 final File internalPath = new File(DIR_MEDIA_STORAGE,
878 rawPath.substring(CANONCIAL_EMULATED_STORAGE_TARGET.length()));
879 if (internalPath.exists()) {
880 return internalPath;
881 }
882 }
883 } catch (IOException e) {
884 Log.w(TAG, "Failed to resolve canonical path for " + path);
885 }
886
887 // Unable to translate to internal path; use original
888 return path;
889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890}