blob: e84b6953c91ef19c17e8951859c3a5d4fde94e24 [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
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700194 public File[] buildExternalStorageAppMediaDirsForVold(String packageName) {
195 return buildPaths(mExternalDirsForVold, DIR_ANDROID, DIR_MEDIA, packageName);
196 }
197
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700198 public File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700199 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_OBB, packageName);
200 }
201
202 public File[] buildExternalStorageAppObbDirsForVold(String packageName) {
203 return buildPaths(mExternalDirsForVold, DIR_ANDROID, DIR_OBB, packageName);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700204 }
205
206 public File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700207 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700208 }
209
210 public File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700211 return buildPaths(mExternalDirsForApp, DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700212 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700213 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800216 * Return root of the "system" partition holding the core Android OS.
217 * Always present and mounted read-only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800220 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222
Jason parksa3cdaa52011-01-13 14:15:43 -0600223 /**
Jeff Sharkey1be762c2014-03-06 09:56:23 -0800224 * Return root directory of the "oem" partition holding OEM customizations,
225 * if any. If present, the partition is mounted read-only.
226 *
227 * @hide
228 */
229 public static File getOemDirectory() {
230 return DIR_OEM_ROOT;
231 }
232
233 /**
Christopher Tate740888f2014-04-18 12:24:57 -0700234 * Return root directory of the "vendor" partition that holds vendor-provided
235 * software that should persist across simple reflashing of the "system" partition.
236 * @hide
237 */
238 public static File getVendorDirectory() {
239 return DIR_VENDOR_ROOT;
240 }
241
242 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600243 * Gets the system directory available for secure storage.
244 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
245 * Otherwise, it returns the unencrypted /data/system directory.
246 * @return File object representing the secure storage system directory.
247 * @hide
248 */
249 public static File getSystemSecureDirectory() {
250 if (isEncryptedFilesystemEnabled()) {
251 return new File(SECURE_DATA_DIRECTORY, "system");
252 } else {
253 return new File(DATA_DIRECTORY, "system");
254 }
255 }
256
257 /**
258 * Gets the data directory for secure storage.
259 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
260 * Otherwise, it returns the unencrypted /data directory.
261 * @return File object representing the data directory for secure storage.
262 * @hide
263 */
264 public static File getSecureDataDirectory() {
265 if (isEncryptedFilesystemEnabled()) {
266 return SECURE_DATA_DIRECTORY;
267 } else {
268 return DATA_DIRECTORY;
269 }
270 }
271
272 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700273 * Return directory used for internal media storage, which is protected by
274 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
275 *
276 * @hide
277 */
278 public static File getMediaStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700279 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700280 return sCurrentUser.getMediaDir();
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700281 }
282
283 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700284 * Return the system directory for a user. This is for use by system services to store
285 * files relating to the user. This directory will be automatically deleted when the user
286 * is removed.
287 *
288 * @hide
289 */
290 public static File getUserSystemDirectory(int userId) {
291 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
292 }
293
294 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600295 * Returns whether the Encrypted File System feature is enabled on the device or not.
296 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
297 * if disabled.
298 * @hide
299 */
300 public static boolean isEncryptedFilesystemEnabled() {
301 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
302 }
303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 private static final File DATA_DIRECTORY
305 = getDirectory("ANDROID_DATA", "/data");
306
Oscar Montemayora8529f62009-11-18 10:14:20 -0800307 /**
308 * @hide
309 */
310 private static final File SECURE_DATA_DIRECTORY
311 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
312
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700313 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314
315 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700316 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 */
318 public static File getDataDirectory() {
319 return DATA_DIRECTORY;
320 }
321
322 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700323 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800324 * currently be accessible if it has been mounted by the user on their
325 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700326 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800327 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700328 * <p>
329 * <em>Note: don't be confused by the word "external" here. This directory
330 * can better be thought as media/shared storage. It is a filesystem that
331 * can hold a relatively large amount of data and that is shared across all
332 * applications (does not enforce permissions). Traditionally this is an SD
333 * card, but it may also be implemented as built-in storage in a device that
334 * is distinct from the protected internal storage and can be mounted as a
335 * filesystem on a computer.</em>
336 * <p>
337 * On devices with multiple users (as described by {@link UserManager}),
338 * each user has their own isolated external storage. Applications only have
339 * access to the external storage for the user they're running as.
340 * <p>
341 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700342 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700343 * with. Access to secondary storage is available through
344 * <p>
345 * Applications should not directly use this top-level directory, in order
346 * to avoid polluting the user's root namespace. Any files that are private
347 * to the application should be placed in a directory returned by
348 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700349 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700350 * if the application is uninstalled. Other shared files should be placed in
351 * one of the directories returned by
352 * {@link #getExternalStoragePublicDirectory}.
353 * <p>
354 * Writing to this path requires the
355 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
356 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700357 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700358 * which is automatically granted if you hold the write permission.
359 * <p>
Chet Haasee8222dd2013-09-05 07:44:18 -0700360 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700361 * application only needs to store internal data, consider using
362 * {@link Context#getExternalFilesDir(String)} or
363 * {@link Context#getExternalCacheDir()}, which require no permissions to
364 * read or write.
365 * <p>
366 * This path may change between platform versions, so applications should
367 * only persist relative paths.
368 * <p>
369 * Here is an example of typical code to monitor the state of external
370 * storage:
371 * <p>
372 * {@sample
373 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800374 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700375 *
376 * @see #getExternalStorageState()
377 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 */
379 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700380 throwIfUserRequired();
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700381 return sCurrentUser.getExternalDirsForApp()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700382 }
383
384 /** {@hide} */
385 public static File getLegacyExternalStorageDirectory() {
386 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
388
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700389 /** {@hide} */
390 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700391 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700392 }
393
394 /** {@hide} */
395 public static File getEmulatedStorageSource(int userId) {
396 // /mnt/shell/emulated/0
397 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), String.valueOf(userId));
398 }
399
400 /** {@hide} */
401 public static File getEmulatedStorageObbSource() {
402 // /mnt/shell/emulated/obb
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700403 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700404 }
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800407 * Standard directory in which to place any audio files that should be
408 * in the regular list of music for the user.
409 * This may be combined with
410 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
411 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
412 * of directories to categories a particular audio file as more than one
413 * type.
414 */
415 public static String DIRECTORY_MUSIC = "Music";
416
417 /**
418 * Standard directory in which to place any audio files that should be
419 * in the list of podcasts that the user can select (not as regular
420 * music).
421 * This may be combined with {@link #DIRECTORY_MUSIC},
422 * {@link #DIRECTORY_NOTIFICATIONS},
423 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
424 * of directories to categories a particular audio file as more than one
425 * type.
426 */
427 public static String DIRECTORY_PODCASTS = "Podcasts";
428
429 /**
430 * Standard directory in which to place any audio files that should be
431 * in the list of ringtones that the user can select (not as regular
432 * music).
433 * This may be combined with {@link #DIRECTORY_MUSIC},
434 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
435 * {@link #DIRECTORY_ALARMS} as a series
436 * of directories to categories a particular audio file as more than one
437 * type.
438 */
439 public static String DIRECTORY_RINGTONES = "Ringtones";
440
441 /**
442 * Standard directory in which to place any audio files that should be
443 * in the list of alarms that the user can select (not as regular
444 * music).
445 * This may be combined with {@link #DIRECTORY_MUSIC},
446 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
447 * and {@link #DIRECTORY_RINGTONES} as a series
448 * of directories to categories a particular audio file as more than one
449 * type.
450 */
451 public static String DIRECTORY_ALARMS = "Alarms";
452
453 /**
454 * Standard directory in which to place any audio files that should be
455 * in the list of notifications that the user can select (not as regular
456 * music).
457 * This may be combined with {@link #DIRECTORY_MUSIC},
458 * {@link #DIRECTORY_PODCASTS},
459 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
460 * of directories to categories a particular audio file as more than one
461 * type.
462 */
463 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
464
465 /**
466 * Standard directory in which to place pictures that are available to
467 * the user. Note that this is primarily a convention for the top-level
468 * public directory, as the media scanner will find and collect pictures
469 * in any directory.
470 */
471 public static String DIRECTORY_PICTURES = "Pictures";
472
473 /**
474 * Standard directory in which to place movies that are available to
475 * the user. Note that this is primarily a convention for the top-level
476 * public directory, as the media scanner will find and collect movies
477 * in any directory.
478 */
479 public static String DIRECTORY_MOVIES = "Movies";
480
481 /**
482 * Standard directory in which to place files that have been downloaded by
483 * the user. Note that this is primarily a convention for the top-level
484 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700485 * private directories. Also note that though the constant here is
486 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
487 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800488 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700489 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800490
491 /**
492 * The traditional location for pictures and videos when mounting the
493 * device as a camera. Note that this is primarily a convention for the
494 * top-level public directory, as this convention makes no sense elsewhere.
495 */
496 public static String DIRECTORY_DCIM = "DCIM";
Jeff Sharkey3e1189b2013-09-12 21:59:06 -0700497
498 /**
499 * Standard directory in which to place documents that have been created by
500 * the user.
501 */
502 public static String DIRECTORY_DOCUMENTS = "Documents";
503
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800504 /**
505 * Get a top-level public external storage directory for placing files of
506 * a particular type. This is where the user will typically place and
507 * manage their own files, so you should be careful about what you put here
508 * to ensure you don't erase their files or get in the way of their own
509 * organization.
510 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700511 * <p>On devices with multiple users (as described by {@link UserManager}),
512 * each user has their own isolated external storage. Applications only
513 * have access to the external storage for the user they're running as.</p>
514 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800515 * <p>Here is an example of typical code to manipulate a picture on
516 * the public external storage:</p>
517 *
518 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
519 * public_picture}
520 *
521 * @param type The type of storage directory to return. Should be one of
522 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
523 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
524 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
525 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
526 * {@link #DIRECTORY_DCIM}. May not be null.
527 *
528 * @return Returns the File path for the directory. Note that this
529 * directory may not yet exist, so you must make sure it exists before
530 * using it such as with {@link File#mkdirs File.mkdirs()}.
531 */
532 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700533 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700534 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800535 }
536
537 /**
538 * Returns the path for android-specific data on the SD card.
539 * @hide
540 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700541 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700542 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700543 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800544 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700545
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800546 /**
547 * Generates the raw path to an application's data
548 * @hide
549 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700550 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700551 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700552 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800553 }
554
555 /**
556 * Generates the raw path to an application's media
557 * @hide
558 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700559 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700560 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700561 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800562 }
563
564 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800565 * Generates the raw path to an application's OBB files
566 * @hide
567 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700568 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700569 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700570 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800571 }
572
573 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800574 * Generates the path to an application's files.
575 * @hide
576 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700577 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700578 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700579 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800580 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700581
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800582 /**
583 * Generates the path to an application's cache.
584 * @hide
585 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700586 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700587 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700588 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800589 }
590
591 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700592 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 */
594 public static File getDownloadCacheDirectory() {
595 return DOWNLOAD_CACHE_DIRECTORY;
596 }
597
598 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700599 * Unknown storage state, such as when a path isn't backed by known storage
600 * media.
601 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800602 * @see #getExternalStorageState(File)
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700603 */
604 public static final String MEDIA_UNKNOWN = "unknown";
605
606 /**
607 * Storage state if the media is not present.
608 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800609 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 */
611 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700614 * Storage state if the media is present but not mounted.
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_UNMOUNTED = "unmounted";
619
620 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700621 * Storage state if the media is present and being disk-checked.
622 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800623 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 */
625 public static final String MEDIA_CHECKING = "checking";
626
627 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700628 * Storage state if the media is present but is blank or is using an
629 * unsupported filesystem.
630 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800631 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 */
633 public static final String MEDIA_NOFS = "nofs";
634
635 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700636 * Storage state if the media is present and mounted at its mount point with
637 * read/write access.
638 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800639 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 */
641 public static final String MEDIA_MOUNTED = "mounted";
642
643 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700644 * Storage state if the media is present and mounted at its mount point with
645 * read-only access.
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_MOUNTED_READ_ONLY = "mounted_ro";
650
651 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700652 * Storage state if the media is present not mounted, and shared via USB
653 * mass storage.
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_SHARED = "shared";
658
659 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700660 * Storage state if the media was removed before it was unmounted.
661 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800662 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 */
664 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
665
666 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700667 * Storage state if the media is present but cannot be mounted. Typically
668 * this happens if the file system on the media is corrupted.
669 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800670 * @see #getExternalStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 */
672 public static final String MEDIA_UNMOUNTABLE = "unmountable";
673
674 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700675 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800676 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700677 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700678 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
679 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
680 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
681 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
682 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 */
684 public static String getExternalStorageState() {
Jeff Sharkeya6d19992013-10-14 16:43:42 -0700685 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800686 return getExternalStorageState(externalDir);
687 }
688
689 /**
690 * @deprecated use {@link #getExternalStorageState(File)}
691 */
692 @Deprecated
693 public static String getStorageState(File path) {
694 return getExternalStorageState(path);
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700695 }
696
697 /**
698 * Returns the current state of the storage device that provides the given
699 * path.
700 *
701 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
702 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
703 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
704 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
705 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
706 */
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800707 public static String getExternalStorageState(File path) {
708 final StorageVolume volume = getStorageVolume(path);
709 if (volume != null) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700710 final IMountService mountService = IMountService.Stub.asInterface(
711 ServiceManager.getService("mount"));
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800712 try {
713 return mountService.getVolumeState(volume.getPath());
714 } catch (RemoteException e) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700715 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700716 }
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800717
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700718 return Environment.MEDIA_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720
Dianne Hackborn407f6252010-10-04 11:31:17 -0700721 /**
722 * Returns whether the primary "external" storage device is removable.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700723 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800724 * @return true if the storage device can be removed (such as an SD card),
725 * or false if the storage device is built in and cannot be
726 * physically removed.
Dianne Hackborn407f6252010-10-04 11:31:17 -0700727 */
728 public static boolean isExternalStorageRemovable() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800729 if (isStorageDisabled()) return false;
730 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
731 return isExternalStorageRemovable(externalDir);
Dianne Hackborn407f6252010-10-04 11:31:17 -0700732 }
733
Kenny Roote1ff2142010-10-12 11:20:01 -0700734 /**
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800735 * Returns whether the storage device that provides the given path is
736 * removable.
Andy Stadler50c294f2011-03-07 19:13:42 -0800737 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800738 * @return true if the storage device can be removed (such as an SD card),
739 * or false if the storage device is built in and cannot be
740 * physically removed.
741 * @throws IllegalArgumentException if the path is not a valid storage
742 * device.
743 */
744 public static boolean isExternalStorageRemovable(File path) {
745 final StorageVolume volume = getStorageVolume(path);
746 if (volume != null) {
747 return volume.isRemovable();
748 } else {
749 throw new IllegalArgumentException("Failed to find storage device at " + path);
750 }
751 }
752
753 /**
754 * Returns whether the primary "external" storage device is emulated. If
755 * true, data stored on this device will be stored on a portion of the
756 * internal storage system.
Andy Stadler50c294f2011-03-07 19:13:42 -0800757 *
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800758 * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName,
759 * boolean)
Kenny Roote1ff2142010-10-12 11:20:01 -0700760 */
761 public static boolean isExternalStorageEmulated() {
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800762 if (isStorageDisabled()) return false;
763 final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
764 return isExternalStorageEmulated(externalDir);
765 }
766
767 /**
768 * Returns whether the storage device that provides the given path is
769 * emulated. If true, data stored on this device will be stored on a portion
770 * of the internal storage system.
771 *
772 * @throws IllegalArgumentException if the path is not a valid storage
773 * device.
774 */
775 public static boolean isExternalStorageEmulated(File path) {
776 final StorageVolume volume = getStorageVolume(path);
777 if (volume != null) {
778 return volume.isEmulated();
779 } else {
780 throw new IllegalArgumentException("Failed to find storage device at " + path);
781 }
Kenny Roote1ff2142010-10-12 11:20:01 -0700782 }
783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 static File getDirectory(String variableName, String defaultPath) {
785 String path = System.getenv(variableName);
786 return path == null ? new File(defaultPath) : new File(path);
787 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700788
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800789 private static String getCanonicalPathOrNull(String variableName) {
790 String path = System.getenv(variableName);
791 if (path == null) {
792 return null;
793 }
794 try {
795 return new File(path).getCanonicalPath();
796 } catch (IOException e) {
797 Log.w(TAG, "Unable to resolve canonical path for " + path);
798 return null;
799 }
800 }
801
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700802 /** {@hide} */
803 public static void setUserRequired(boolean userRequired) {
804 sUserRequired = userRequired;
805 }
806
807 private static void throwIfUserRequired() {
808 if (sUserRequired) {
809 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
810 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700811 }
812 }
813
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700814 /**
815 * Append path segments to each given base path, returning result.
816 *
817 * @hide
818 */
819 public static File[] buildPaths(File[] base, String... segments) {
820 File[] result = new File[base.length];
821 for (int i = 0; i < base.length; i++) {
822 result[i] = buildPath(base[i], segments);
823 }
824 return result;
825 }
826
827 /**
828 * Append path segments to given base path, returning result.
829 *
830 * @hide
831 */
832 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700833 File cur = base;
834 for (String segment : segments) {
835 if (cur == null) {
836 cur = new File(segment);
837 } else {
838 cur = new File(cur, segment);
839 }
840 }
841 return cur;
842 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800843
Jeff Sharkey4ca728c2014-01-10 16:27:19 -0800844 private static boolean isStorageDisabled() {
845 return SystemProperties.getBoolean("config.disable_storage", false);
846 }
847
848 private static StorageVolume getStorageVolume(File path) {
849 try {
850 path = path.getCanonicalFile();
851 } catch (IOException e) {
852 return null;
853 }
854
855 try {
856 final IMountService mountService = IMountService.Stub.asInterface(
857 ServiceManager.getService("mount"));
858 final StorageVolume[] volumes = mountService.getVolumeList();
859 for (StorageVolume volume : volumes) {
860 if (FileUtils.contains(volume.getPathFile(), path)) {
861 return volume;
862 }
863 }
864 } catch (RemoteException e) {
865 }
866
867 return null;
868 }
869
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800870 /**
871 * If the given path exists on emulated external storage, return the
872 * translated backing path hosted on internal storage. This bypasses any
873 * emulation later, improving performance. This is <em>only</em> suitable
874 * for read-only access.
875 * <p>
876 * Returns original path if given path doesn't meet these criteria. Callers
877 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
878 * permission.
879 *
880 * @hide
881 */
882 public static File maybeTranslateEmulatedPathToInternal(File path) {
883 // Fast return if not emulated, or missing variables
884 if (!Environment.isExternalStorageEmulated()
885 || CANONCIAL_EMULATED_STORAGE_TARGET == null) {
886 return path;
887 }
888
889 try {
890 final String rawPath = path.getCanonicalPath();
891 if (rawPath.startsWith(CANONCIAL_EMULATED_STORAGE_TARGET)) {
892 final File internalPath = new File(DIR_MEDIA_STORAGE,
893 rawPath.substring(CANONCIAL_EMULATED_STORAGE_TARGET.length()));
894 if (internalPath.exists()) {
895 return internalPath;
896 }
897 }
898 } catch (IOException e) {
899 Log.w(TAG, "Failed to resolve canonical path for " + path);
900 }
901
902 // Unable to translate to internal path; use original
903 return path;
904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905}