blob: e98a26b71c45553bf9b0fcc31e1ecfeb595271c8 [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";
Christopher Tate740888f2014-04-18 12:24:57 -070045 private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070046
Jeff Sharkeydfa45302012-09-12 16:25:22 -070047 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070048 public static final String DIR_ANDROID = "Android";
49 private static final String DIR_DATA = "data";
50 private static final String DIR_MEDIA = "media";
51 private static final String DIR_OBB = "obb";
52 private static final String DIR_FILES = "files";
53 private static final String DIR_CACHE = "cache";
54
55 /** {@hide} */
56 @Deprecated
57 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070058
Jeff Sharkey63d0a062013-03-01 16:12:55 -080059 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
Jeff Sharkey1be762c2014-03-06 09:56:23 -080060 private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
Christopher Tate740888f2014-04-18 12:24:57 -070061 private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");
Jeff Sharkey63d0a062013-03-01 16:12:55 -080062 private static final File DIR_MEDIA_STORAGE = getDirectory(ENV_MEDIA_STORAGE, "/data/media");
63
64 private static final String CANONCIAL_EMULATED_STORAGE_TARGET = getCanonicalPathOrNull(
65 ENV_EMULATED_STORAGE_TARGET);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
Oscar Montemayora8529f62009-11-18 10:14:20 -080067 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
68
Jeff Sharkeyb049e212012-09-07 23:16:01 -070069 private static UserEnvironment sCurrentUser;
Jeff Sharkey48749fc2013-04-19 13:25:04 -070070 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070071
Jeff Sharkeyb049e212012-09-07 23:16:01 -070072 static {
73 initForCurrentUser();
74 }
75
76 /** {@hide} */
77 public static void initForCurrentUser() {
78 final int userId = UserHandle.myUserId();
79 sCurrentUser = new UserEnvironment(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -070080 }
81
82 /** {@hide} */
83 public static class UserEnvironment {
84 // TODO: generalize further to create package-specific environment
85
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070086 /** External storage dirs, as visible to vold */
87 private final File[] mExternalDirsForVold;
88 /** External storage dirs, as visible to apps */
89 private final File[] mExternalDirsForApp;
90 /** Primary emulated storage dir for direct access */
91 private final File mEmulatedDirForDirect;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070092
93 public UserEnvironment(int userId) {
94 // See storage config details at http://source.android.com/tech/storage/
95 String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070096 String rawEmulatedSource = System.getenv(ENV_EMULATED_STORAGE_SOURCE);
97 String rawEmulatedTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
Jeff Sharkey44cbdec2013-10-07 16:49:47 -070098
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -070099 String rawMediaStorage = System.getenv(ENV_MEDIA_STORAGE);
100 if (TextUtils.isEmpty(rawMediaStorage)) {
101 rawMediaStorage = "/data/media";
102 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700103
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700104 ArrayList<File> externalForVold = Lists.newArrayList();
105 ArrayList<File> externalForApp = Lists.newArrayList();
106
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700107 if (!TextUtils.isEmpty(rawEmulatedTarget)) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700108 // Device has emulated storage; external storage paths should have
109 // userId burned into them.
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700110 final String rawUserId = Integer.toString(userId);
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700111 final File emulatedSourceBase = new File(rawEmulatedSource);
112 final File emulatedTargetBase = new File(rawEmulatedTarget);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700113 final File mediaBase = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700114
115 // /storage/emulated/0
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700116 externalForVold.add(buildPath(emulatedSourceBase, rawUserId));
117 externalForApp.add(buildPath(emulatedTargetBase, rawUserId));
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700118 // /data/media/0
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700119 mEmulatedDirForDirect = buildPath(mediaBase, rawUserId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700120
121 } else {
122 // Device has physical external storage; use plain paths.
123 if (TextUtils.isEmpty(rawExternalStorage)) {
124 Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default");
125 rawExternalStorage = "/storage/sdcard0";
126 }
127
128 // /storage/sdcard0
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700129 externalForVold.add(new File(rawExternalStorage));
130 externalForApp.add(new File(rawExternalStorage));
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700131 // /data/media
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700132 mEmulatedDirForDirect = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700133 }
Jeff Sharkey44cbdec2013-10-07 16:49:47 -0700134
135 // Splice in any secondary storage paths, but only for owner
136 final String rawSecondaryStorage = System.getenv(ENV_SECONDARY_STORAGE);
137 if (!TextUtils.isEmpty(rawSecondaryStorage) && userId == UserHandle.USER_OWNER) {
138 for (String secondaryPath : rawSecondaryStorage.split(":")) {
139 externalForVold.add(new File(secondaryPath));
140 externalForApp.add(new File(secondaryPath));
141 }
142 }
143
144 mExternalDirsForVold = externalForVold.toArray(new File[externalForVold.size()]);
145 mExternalDirsForApp = externalForApp.toArray(new File[externalForApp.size()]);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700146 }
147
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700148 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700149 public File getExternalStorageDirectory() {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700150 return mExternalDirsForApp[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700151 }
152
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700153 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700154 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700155 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700156 }
157
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700158 public File[] getExternalDirsForVold() {
159 return mExternalDirsForVold;
160 }
161
162 public File[] getExternalDirsForApp() {
163 return mExternalDirsForApp;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700164 }
165
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700166 public File getMediaDir() {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700167 return mEmulatedDirForDirect;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700168 }
169
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700170 public File[] buildExternalStoragePublicDirs(String type) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700171 return buildPaths(mExternalDirsForApp, type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700172 }
173
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700174 public File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700175 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700176 }
177
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700178 public File[] buildExternalStorageAndroidObbDirs() {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700179 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700180 }
181
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700182 public File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700183 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA, packageName);
184 }
185
186 public File[] buildExternalStorageAppDataDirsForVold(String packageName) {
187 return buildPaths(mExternalDirsForVold, DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700188 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700189
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700190 public File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700191 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_MEDIA, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700192 }
193
194 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700195 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_OBB, packageName);
196 }
197
198 public File[] buildExternalStorageAppObbDirsForVold(String packageName) {
199 return buildPaths(mExternalDirsForVold, DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700200 }
201
202 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700203 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700204 }
205
206 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700207 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700208 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700209 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800212 * Return root of the "system" partition holding the core Android OS.
213 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 */
215 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800216 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 }
218
Jason parksa3cdaa52011-01-13 14:15:43 -0600219 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800220 * Return root directory of the "oem" partition holding OEM customizations,
221 * if any. If present, the partition is mounted read-only.
222 *
223 * @hide
224 */
225 public static File getOemDirectory() {
226 return DIR_OEM_ROOT;
227 }
228
229 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700230 * Return root directory of the "vendor" partition that holds vendor-provided
231 * software that should persist across simple reflashing of the "system" partition.
232 * @hide
233 */
234 public static File getVendorDirectory() {
235 return DIR_VENDOR_ROOT;
236 }
237
238 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600239 * Gets the system directory available for secure storage.
240 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
241 * Otherwise, it returns the unencrypted /data/system directory.
242 * @return File object representing the secure storage system directory.
243 * @hide
244 */
245 public static File getSystemSecureDirectory() {
246 if (isEncryptedFilesystemEnabled()) {
247 return new File(SECURE_DATA_DIRECTORY, "system");
248 } else {
249 return new File(DATA_DIRECTORY, "system");
250 }
251 }
252
253 /**
254 * Gets the data directory for secure storage.
255 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
256 * Otherwise, it returns the unencrypted /data directory.
257 * @return File object representing the data directory for secure storage.
258 * @hide
259 */
260 public static File getSecureDataDirectory() {
261 if (isEncryptedFilesystemEnabled()) {
262 return SECURE_DATA_DIRECTORY;
263 } else {
264 return DATA_DIRECTORY;
265 }
266 }
267
268 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700269 * Return directory used for internal media storage, which is protected by
270 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
271 *
272 * @hide
273 */
274 public static File getMediaStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700275 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700276 return sCurrentUser.getMediaDir();
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700277 }
278
279 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700280 * Return the system directory for a user. This is for use by system services to store
281 * files relating to the user. This directory will be automatically deleted when the user
282 * is removed.
283 *
284 * @hide
285 */
286 public static File getUserSystemDirectory(int userId) {
287 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
288 }
289
290 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600291 * Returns whether the Encrypted File System feature is enabled on the device or not.
292 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
293 * if disabled.
294 * @hide
295 */
296 public static boolean isEncryptedFilesystemEnabled() {
297 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
298 }
299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 private static final File DATA_DIRECTORY
301 = getDirectory("ANDROID_DATA", "/data");
302
Oscar Montemayora8529f62009-11-18 10:14:20 -0800303 /**
304 * @hide
305 */
306 private static final File SECURE_DATA_DIRECTORY
307 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
308
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700309 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310
311 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700312 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 */
314 public static File getDataDirectory() {
315 return DATA_DIRECTORY;
316 }
317
318 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700319 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800320 * currently be accessible if it has been mounted by the user on their
321 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700322 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800323 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700324 * <p>
325 * <em>Note: don't be confused by the word "external" here. This directory
326 * can better be thought as media/shared storage. It is a filesystem that
327 * can hold a relatively large amount of data and that is shared across all
328 * applications (does not enforce permissions). Traditionally this is an SD
329 * card, but it may also be implemented as built-in storage in a device that
330 * is distinct from the protected internal storage and can be mounted as a
331 * filesystem on a computer.</em>
332 * <p>
333 * On devices with multiple users (as described by {@link UserManager}),
334 * each user has their own isolated external storage. Applications only have
335 * access to the external storage for the user they're running as.
336 * <p>
337 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700338 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700339 * with. Access to secondary storage is available through
340 * <p>
341 * Applications should not directly use this top-level directory, in order
342 * to avoid polluting the user's root namespace. Any files that are private
343 * to the application should be placed in a directory returned by
344 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700345 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700346 * if the application is uninstalled. Other shared files should be placed in
347 * one of the directories returned by
348 * {@link #getExternalStoragePublicDirectory}.
349 * <p>
350 * Writing to this path requires the
351 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
352 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700353 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700354 * which is automatically granted if you hold the write permission.
355 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700356 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700357 * application only needs to store internal data, consider using
358 * {@link Context#getExternalFilesDir(String)} or
359 * {@link Context#getExternalCacheDir()}, which require no permissions to
360 * read or write.
361 * <p>
362 * This path may change between platform versions, so applications should
363 * only persist relative paths.
364 * <p>
365 * Here is an example of typical code to monitor the state of external
366 * storage:
367 * <p>
368 * {@sample
369 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800370 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700371 *
372 * @see #getExternalStorageState()
373 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 */
375 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700376 throwIfUserRequired();
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700377 return sCurrentUser.getExternalDirsForApp()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700378 }
379
380 /** {@hide} */
381 public static File getLegacyExternalStorageDirectory() {
382 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
384
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700385 /** {@hide} */
386 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700387 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700388 }
389
390 /** {@hide} */
391 public static File getEmulatedStorageSource(int userId) {
392 // /mnt/shell/emulated/0
393 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), String.valueOf(userId));
394 }
395
396 /** {@hide} */
397 public static File getEmulatedStorageObbSource() {
398 // /mnt/shell/emulated/obb
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700399 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700400 }
401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800403 * Standard directory in which to place any audio files that should be
404 * in the regular list of music for the user.
405 * This may be combined with
406 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
407 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
408 * of directories to categories a particular audio file as more than one
409 * type.
410 */
411 public static String DIRECTORY_MUSIC = "Music";
412
413 /**
414 * Standard directory in which to place any audio files that should be
415 * in the list of podcasts that the user can select (not as regular
416 * music).
417 * This may be combined with {@link #DIRECTORY_MUSIC},
418 * {@link #DIRECTORY_NOTIFICATIONS},
419 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
420 * of directories to categories a particular audio file as more than one
421 * type.
422 */
423 public static String DIRECTORY_PODCASTS = "Podcasts";
424
425 /**
426 * Standard directory in which to place any audio files that should be
427 * in the list of ringtones that the user can select (not as regular
428 * music).
429 * This may be combined with {@link #DIRECTORY_MUSIC},
430 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
431 * {@link #DIRECTORY_ALARMS} as a series
432 * of directories to categories a particular audio file as more than one
433 * type.
434 */
435 public static String DIRECTORY_RINGTONES = "Ringtones";
436
437 /**
438 * Standard directory in which to place any audio files that should be
439 * in the list of alarms that the user can select (not as regular
440 * music).
441 * This may be combined with {@link #DIRECTORY_MUSIC},
442 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
443 * and {@link #DIRECTORY_RINGTONES} as a series
444 * of directories to categories a particular audio file as more than one
445 * type.
446 */
447 public static String DIRECTORY_ALARMS = "Alarms";
448
449 /**
450 * Standard directory in which to place any audio files that should be
451 * in the list of notifications that the user can select (not as regular
452 * music).
453 * This may be combined with {@link #DIRECTORY_MUSIC},
454 * {@link #DIRECTORY_PODCASTS},
455 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
456 * of directories to categories a particular audio file as more than one
457 * type.
458 */
459 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
460
461 /**
462 * Standard directory in which to place pictures that are available to
463 * the user. Note that this is primarily a convention for the top-level
464 * public directory, as the media scanner will find and collect pictures
465 * in any directory.
466 */
467 public static String DIRECTORY_PICTURES = "Pictures";
468
469 /**
470 * Standard directory in which to place movies that are available to
471 * the user. Note that this is primarily a convention for the top-level
472 * public directory, as the media scanner will find and collect movies
473 * in any directory.
474 */
475 public static String DIRECTORY_MOVIES = "Movies";
476
477 /**
478 * Standard directory in which to place files that have been downloaded by
479 * the user. Note that this is primarily a convention for the top-level
480 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700481 * private directories. Also note that though the constant here is
482 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
483 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800484 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700485 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800486
487 /**
488 * The traditional location for pictures and videos when mounting the
489 * device as a camera. Note that this is primarily a convention for the
490 * top-level public directory, as this convention makes no sense elsewhere.
491 */
492 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700493
494 /**
495 * Standard directory in which to place documents that have been created by
496 * the user.
497 */
498 public static String DIRECTORY_DOCUMENTS = "Documents";
499
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800500 /**
501 * Get a top-level public external storage directory for placing files of
502 * a particular type. This is where the user will typically place and
503 * manage their own files, so you should be careful about what you put here
504 * to ensure you don't erase their files or get in the way of their own
505 * organization.
506 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700507 * <p>On devices with multiple users (as described by {@link UserManager}),
508 * each user has their own isolated external storage. Applications only
509 * have access to the external storage for the user they're running as.</p>
510 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800511 * <p>Here is an example of typical code to manipulate a picture on
512 * the public external storage:</p>
513 *
514 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
515 * public_picture}
516 *
517 * @param type The type of storage directory to return. Should be one of
518 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
519 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
520 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
521 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
522 * {@link #DIRECTORY_DCIM}. May not be null.
523 *
524 * @return Returns the File path for the directory. Note that this
525 * directory may not yet exist, so you must make sure it exists before
526 * using it such as with {@link File#mkdirs File.mkdirs()}.
527 */
528 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700529 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700530 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800531 }
532
533 /**
534 * Returns the path for android-specific data on the SD card.
535 * @hide
536 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700537 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700538 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700539 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800540 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700541
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800542 /**
543 * Generates the raw path to an application's data
544 * @hide
545 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700546 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700547 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700548 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800549 }
550
551 /**
552 * Generates the raw path to an application's media
553 * @hide
554 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700555 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700556 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700557 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800558 }
559
560 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800561 * Generates the raw path to an application's OBB files
562 * @hide
563 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700564 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700565 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700566 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800567 }
568
569 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800570 * Generates the path to an application's files.
571 * @hide
572 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700573 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700574 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700575 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800576 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700577
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800578 /**
579 * Generates the path to an application's cache.
580 * @hide
581 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700582 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700583 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700584 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800585 }
586
587 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700588 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 */
590 public static File getDownloadCacheDirectory() {
591 return DOWNLOAD_CACHE_DIRECTORY;
592 }
593
594 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700595 * Unknown storage state, such as when a path isn't backed by known storage
596 * media.
597 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800598 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700599 */
600 public static final String MEDIA_UNKNOWN = "unknown";
601
602 /**
603 * Storage state if the media is not present.
604 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800605 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 */
607 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700610 * Storage state if the media is present but not mounted.
611 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800612 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 */
614 public static final String MEDIA_UNMOUNTED = "unmounted";
615
616 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700617 * Storage state if the media is present and being disk-checked.
618 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800619 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 */
621 public static final String MEDIA_CHECKING = "checking";
622
623 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700624 * Storage state if the media is present but is blank or is using an
625 * unsupported filesystem.
626 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800627 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 */
629 public static final String MEDIA_NOFS = "nofs";
630
631 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700632 * Storage state if the media is present and mounted at its mount point with
633 * read/write access.
634 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800635 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 */
637 public static final String MEDIA_MOUNTED = "mounted";
638
639 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700640 * Storage state if the media is present and mounted at its mount point with
641 * read-only access.
642 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800643 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 */
645 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
646
647 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700648 * Storage state if the media is present not mounted, and shared via USB
649 * mass storage.
650 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800651 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 */
653 public static final String MEDIA_SHARED = "shared";
654
655 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700656 * Storage state if the media was removed before it was unmounted.
657 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800658 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 */
660 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
661
662 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700663 * Storage state if the media is present but cannot be mounted. Typically
664 * this happens if the file system on the media is corrupted.
665 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800666 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 */
668 public static final String MEDIA_UNMOUNTABLE = "unmountable";
669
670 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700671 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800672 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700673 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700674 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
675 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
676 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
677 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
678 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
680 public static String getExternalStorageState() {
Jeff Sharkeya6d19992013-10-14 16:43:42 -0700681 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800682 return getExternalStorageState(externalDir);
683 }
684
685 /**
686 * @deprecated use {@link #getExternalStorageState(File)}
687 */
688 @Deprecated
689 public static String getStorageState(File path) {
690 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700691 }
692
693 /**
694 * Returns the current state of the storage device that provides the given
695 * path.
696 *
697 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
698 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
699 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
700 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
701 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
702 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800703 public static String getExternalStorageState(File path) {
704 final StorageVolume volume = getStorageVolume(path);
705 if (volume != null) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700706 final IMountService mountService = IMountService.Stub.asInterface(
707 ServiceManager.getService("mount"));
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800708 try {
709 return mountService.getVolumeState(volume.getPath());
710 } catch (RemoteException e) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700711 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700712 }
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800713
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700714 return Environment.MEDIA_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716
Dianne Hackborn407f6252010-10-04 11:31:17 -0700717 /**
718 * Returns whether the primary "external" storage device is removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700719 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800720 * @return true if the storage device can be removed (such as an SD card),
721 * or false if the storage device is built in and cannot be
722 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700723 */
724 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800725 if (isStorageDisabled()) return false;
726 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
727 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700728 }
729
Kenny Roote1ff2142010-10-12 11:20:01 -0700730 /**
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800731 * Returns whether the storage device that provides the given path is
732 * removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800733 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800734 * @return true if the storage device can be removed (such as an SD card),
735 * or false if the storage device is built in and cannot be
736 * physically removed.
737 * @throws IllegalArgumentException if the path is not a valid storage
738 * device.
739 */
740 public static boolean isExternalStorageRemovable(File path) {
741 final StorageVolume volume = getStorageVolume(path);
742 if (volume != null) {
743 return volume.isRemovable();
744 } else {
745 throw new IllegalArgumentException("Failed to find storage device at " + path);
746 }
747 }
748
749 /**
750 * Returns whether the primary "external" storage device is emulated. If
751 * true, data stored on this device will be stored on a portion of the
752 * internal storage system.
Andy Stadler50c294f2011-03-07 19:13:42 -0800753 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800754 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
755 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700756 */
757 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800758 if (isStorageDisabled()) return false;
759 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
760 return isExternalStorageEmulated(externalDir);
761 }
762
763 /**
764 * Returns whether the storage device that provides the given path is
765 * emulated. If true, data stored on this device will be stored on a portion
766 * of the internal storage system.
767 *
768 * @throws IllegalArgumentException if the path is not a valid storage
769 * device.
770 */
771 public static boolean isExternalStorageEmulated(File path) {
772 final StorageVolume volume = getStorageVolume(path);
773 if (volume != null) {
774 return volume.isEmulated();
775 } else {
776 throw new IllegalArgumentException("Failed to find storage device at " + path);
777 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700778 }
779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 static File getDirectory(String variableName, String defaultPath) {
781 String path = System.getenv(variableName);
782 return path == null ? new File(defaultPath) : new File(path);
783 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700784
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800785 private static String getCanonicalPathOrNull(String variableName) {
786 String path = System.getenv(variableName);
787 if (path == null) {
788 return null;
789 }
790 try {
791 return new File(path).getCanonicalPath();
792 } catch (IOException e) {
793 Log.w(TAG, "Unable to resolve canonical path for " + path);
794 return null;
795 }
796 }
797
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700798 /** {@hide} */
799 public static void setUserRequired(boolean userRequired) {
800 sUserRequired = userRequired;
801 }
802
803 private static void throwIfUserRequired() {
804 if (sUserRequired) {
805 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
806 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700807 }
808 }
809
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700810 /**
811 * Append path segments to each given base path, returning result.
812 *
813 * @hide
814 */
815 public static File[] buildPaths(File[] base, String... segments) {
816 File[] result = new File[base.length];
817 for (int i = 0; i < base.length; i++) {
818 result[i] = buildPath(base[i], segments);
819 }
820 return result;
821 }
822
823 /**
824 * Append path segments to given base path, returning result.
825 *
826 * @hide
827 */
828 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700829 File cur = base;
830 for (String segment : segments) {
831 if (cur == null) {
832 cur = new File(segment);
833 } else {
834 cur = new File(cur, segment);
835 }
836 }
837 return cur;
838 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800839
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800840 private static boolean isStorageDisabled() {
841 return SystemProperties.getBoolean("config.disable_storage", false);
842 }
843
844 private static StorageVolume getStorageVolume(File path) {
845 try {
846 path = path.getCanonicalFile();
847 } catch (IOException e) {
848 return null;
849 }
850
851 try {
852 final IMountService mountService = IMountService.Stub.asInterface(
853 ServiceManager.getService("mount"));
854 final StorageVolume[] volumes = mountService.getVolumeList();
855 for (StorageVolume volume : volumes) {
856 if (FileUtils.contains(volume.getPathFile(), path)) {
857 return volume;
858 }
859 }
860 } catch (RemoteException e) {
861 }
862
863 return null;
864 }
865
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800866 /**
867 * If the given path exists on emulated external storage, return the
868 * translated backing path hosted on internal storage. This bypasses any
869 * emulation later, improving performance. This is <em>only</em> suitable
870 * for read-only access.
871 * <p>
872 * Returns original path if given path doesn't meet these criteria. Callers
873 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
874 * permission.
875 *
876 * @hide
877 */
878 public static File maybeTranslateEmulatedPathToInternal(File path) {
879 // Fast return if not emulated, or missing variables
880 if (!Environment.isExternalStorageEmulated()
881 || CANONCIAL_EMULATED_STORAGE_TARGET == null) {
882 return path;
883 }
884
885 try {
886 final String rawPath = path.getCanonicalPath();
887 if (rawPath.startsWith(CANONCIAL_EMULATED_STORAGE_TARGET)) {
888 final File internalPath = new File(DIR_MEDIA_STORAGE,
889 rawPath.substring(CANONCIAL_EMULATED_STORAGE_TARGET.length()));
890 if (internalPath.exists()) {
891 return internalPath;
892 }
893 }
894 } catch (IOException e) {
895 Log.w(TAG, "Failed to resolve canonical path for " + path);
896 }
897
898 // Unable to translate to internal path; use original
899 return path;
900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901}