blob: 3b9456f79875ded96bd2cfb2e9a5d9083d1373d4 [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 Sharkey1abdb712013-08-11 16:28:14 -070019import android.content.Context;
San Mehatb1043402010-02-05 08:26:50 -080020import android.os.storage.IMountService;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070021import android.os.storage.StorageManager;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070022import android.os.storage.StorageVolume;
Dan Morrille4d9a012013-03-28 18:10:43 -070023import android.os.SystemProperties;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070024import android.text.TextUtils;
Kenny Rootb2278dc2011-01-18 13:03:28 -080025import android.util.Log;
San Mehat7fd0fee2009-12-17 07:12:23 -080026
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080027import com.android.internal.annotations.GuardedBy;
28
Gilles Debunneee1d6302011-05-13 10:09:32 -070029import java.io.File;
Jeff Sharkey63d0a062013-03-01 16:12:55 -080030import java.io.IOException;
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 Sharkey63d0a062013-03-01 16:12:55 -080042 private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
Jeff Sharkeyb049e212012-09-07 23:16:01 -070043
Jeff Sharkeydfa45302012-09-12 16:25:22 -070044 /** {@hide} */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070045 public static final String DIR_ANDROID = "Android";
46 private static final String DIR_DATA = "data";
47 private static final String DIR_MEDIA = "media";
48 private static final String DIR_OBB = "obb";
49 private static final String DIR_FILES = "files";
50 private static final String DIR_CACHE = "cache";
51
52 /** {@hide} */
53 @Deprecated
54 public static final String DIRECTORY_ANDROID = DIR_ANDROID;
Jeff Sharkeydfa45302012-09-12 16:25:22 -070055
Jeff Sharkey63d0a062013-03-01 16:12:55 -080056 private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
57 private static final File DIR_MEDIA_STORAGE = getDirectory(ENV_MEDIA_STORAGE, "/data/media");
58
59 private static final String CANONCIAL_EMULATED_STORAGE_TARGET = getCanonicalPathOrNull(
60 ENV_EMULATED_STORAGE_TARGET);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
Oscar Montemayora8529f62009-11-18 10:14:20 -080062 private static final String SYSTEM_PROPERTY_EFS_ENABLED = "persist.security.efs.enabled";
63
Jeff Sharkeyb049e212012-09-07 23:16:01 -070064 private static UserEnvironment sCurrentUser;
Jeff Sharkey48749fc2013-04-19 13:25:04 -070065 private static boolean sUserRequired;
Kenny Roote1ff2142010-10-12 11:20:01 -070066
Jeff Sharkeyb049e212012-09-07 23:16:01 -070067 private static final Object sLock = new Object();
68
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080069 @GuardedBy("sLock")
Jeff Sharkeyb049e212012-09-07 23:16:01 -070070 private static volatile StorageVolume sPrimaryVolume;
Mike Lockwood2f6a3882011-05-09 19:08:06 -070071
72 private static StorageVolume getPrimaryVolume() {
Dan Morrille4d9a012013-03-28 18:10:43 -070073 if (SystemProperties.getBoolean("config.disable_storage", false)) {
74 return null;
75 }
76
Jeff Sharkeyb049e212012-09-07 23:16:01 -070077 if (sPrimaryVolume == null) {
78 synchronized (sLock) {
79 if (sPrimaryVolume == null) {
Mike Lockwood2f6a3882011-05-09 19:08:06 -070080 try {
81 IMountService mountService = IMountService.Stub.asInterface(ServiceManager
82 .getService("mount"));
Jeff Sharkeyb049e212012-09-07 23:16:01 -070083 final StorageVolume[] volumes = mountService.getVolumeList();
84 sPrimaryVolume = StorageManager.getPrimaryVolume(volumes);
Mike Lockwood2f6a3882011-05-09 19:08:06 -070085 } catch (Exception e) {
86 Log.e(TAG, "couldn't talk to MountService", e);
87 }
88 }
89 }
90 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -070091 return sPrimaryVolume;
92 }
93
94 static {
95 initForCurrentUser();
96 }
97
98 /** {@hide} */
99 public static void initForCurrentUser() {
100 final int userId = UserHandle.myUserId();
101 sCurrentUser = new UserEnvironment(userId);
102
103 synchronized (sLock) {
104 sPrimaryVolume = null;
105 }
106 }
107
108 /** {@hide} */
109 public static class UserEnvironment {
110 // TODO: generalize further to create package-specific environment
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700111 // TODO: add support for secondary external storage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700112
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700113 private final File[] mExternalDirs;
114 private final File mMediaDir;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700115
116 public UserEnvironment(int userId) {
117 // See storage config details at http://source.android.com/tech/storage/
118 String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
119 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700120 String rawMediaStorage = System.getenv(ENV_MEDIA_STORAGE);
121 if (TextUtils.isEmpty(rawMediaStorage)) {
122 rawMediaStorage = "/data/media";
123 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700124
125 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
126 // Device has emulated storage; external storage paths should have
127 // userId burned into them.
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700128 final String rawUserId = Integer.toString(userId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700129 final File emulatedBase = new File(rawEmulatedStorageTarget);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700130 final File mediaBase = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700131
132 // /storage/emulated/0
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700133 mExternalDirs = new File[] { buildPath(emulatedBase, rawUserId) };
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700134 // /data/media/0
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700135 mMediaDir = buildPath(mediaBase, rawUserId);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700136
137 } else {
138 // Device has physical external storage; use plain paths.
139 if (TextUtils.isEmpty(rawExternalStorage)) {
140 Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default");
141 rawExternalStorage = "/storage/sdcard0";
142 }
143
144 // /storage/sdcard0
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700145 mExternalDirs = new File[] { new File(rawExternalStorage) };
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700146 // /data/media
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700147 mMediaDir = new File(rawMediaStorage);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700148 }
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 getExternalStorageDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700153 return mExternalDirs[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700154 }
155
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700156 @Deprecated
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700157 public File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700158 return buildExternalStoragePublicDirs(type)[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700159 }
160
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700161 public File[] getExternalDirs() {
162 return mExternalDirs;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700163 }
164
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700165 public File getMediaDir() {
166 return mMediaDir;
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700167 }
168
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700169 public File[] buildExternalStoragePublicDirs(String type) {
170 return buildPaths(mExternalDirs, type);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700171 }
172
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700173 public File[] buildExternalStorageAndroidDataDirs() {
174 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_DATA);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700175 }
176
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700177 public File[] buildExternalStorageAndroidObbDirs() {
178 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_OBB);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700179 }
180
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700181 public File[] buildExternalStorageAppDataDirs(String packageName) {
182 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_DATA, packageName);
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700183 }
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700184
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700185 public File[] buildExternalStorageAppMediaDirs(String packageName) {
186 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_MEDIA, packageName);
187 }
188
189 public File[] buildExternalStorageAppObbDirs(String packageName) {
190 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_OBB, packageName);
191 }
192
193 public File[] buildExternalStorageAppFilesDirs(String packageName) {
194 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);
195 }
196
197 public File[] buildExternalStorageAppCacheDirs(String packageName) {
198 return buildPaths(mExternalDirs, DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE);
Jeff Sharkey3fe5bf62012-09-18 15:54:52 -0700199 }
Mike Lockwood2f6a3882011-05-09 19:08:06 -0700200 }
San Mehat7fd0fee2009-12-17 07:12:23 -0800201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
203 * Gets the Android root directory.
204 */
205 public static File getRootDirectory() {
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800206 return DIR_ANDROID_ROOT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 }
208
Jason parksa3cdaa52011-01-13 14:15:43 -0600209 /**
210 * Gets the system directory available for secure storage.
211 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure/system).
212 * Otherwise, it returns the unencrypted /data/system directory.
213 * @return File object representing the secure storage system directory.
214 * @hide
215 */
216 public static File getSystemSecureDirectory() {
217 if (isEncryptedFilesystemEnabled()) {
218 return new File(SECURE_DATA_DIRECTORY, "system");
219 } else {
220 return new File(DATA_DIRECTORY, "system");
221 }
222 }
223
224 /**
225 * Gets the data directory for secure storage.
226 * If Encrypted File system is enabled, it returns an encrypted directory (/data/secure).
227 * Otherwise, it returns the unencrypted /data directory.
228 * @return File object representing the data directory for secure storage.
229 * @hide
230 */
231 public static File getSecureDataDirectory() {
232 if (isEncryptedFilesystemEnabled()) {
233 return SECURE_DATA_DIRECTORY;
234 } else {
235 return DATA_DIRECTORY;
236 }
237 }
238
239 /**
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700240 * Return directory used for internal media storage, which is protected by
241 * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
242 *
243 * @hide
244 */
245 public static File getMediaStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700246 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700247 return sCurrentUser.getMediaDir();
Jeff Sharkeyd525baa2012-05-22 18:25:32 -0700248 }
249
250 /**
Amith Yamasani61f57372012-08-31 12:12:28 -0700251 * Return the system directory for a user. This is for use by system services to store
252 * files relating to the user. This directory will be automatically deleted when the user
253 * is removed.
254 *
255 * @hide
256 */
257 public static File getUserSystemDirectory(int userId) {
258 return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
259 }
260
261 /**
Jason parksa3cdaa52011-01-13 14:15:43 -0600262 * Returns whether the Encrypted File System feature is enabled on the device or not.
263 * @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>
264 * if disabled.
265 * @hide
266 */
267 public static boolean isEncryptedFilesystemEnabled() {
268 return SystemProperties.getBoolean(SYSTEM_PROPERTY_EFS_ENABLED, false);
269 }
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 private static final File DATA_DIRECTORY
272 = getDirectory("ANDROID_DATA", "/data");
273
Oscar Montemayora8529f62009-11-18 10:14:20 -0800274 /**
275 * @hide
276 */
277 private static final File SECURE_DATA_DIRECTORY
278 = getDirectory("ANDROID_SECURE_DATA", "/data/secure");
279
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700280 private static final File DOWNLOAD_CACHE_DIRECTORY = getDirectory("DOWNLOAD_CACHE", "/cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281
282 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700283 * Return the user data directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 */
285 public static File getDataDirectory() {
286 return DATA_DIRECTORY;
287 }
288
289 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700290 * Return the primary external storage directory. This directory may not
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800291 * currently be accessible if it has been mounted by the user on their
292 * computer, has been removed from the device, or some other problem has
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700293 * happened. You can determine its current state with
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800294 * {@link #getExternalStorageState()}.
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700295 * <p>
296 * <em>Note: don't be confused by the word "external" here. This directory
297 * can better be thought as media/shared storage. It is a filesystem that
298 * can hold a relatively large amount of data and that is shared across all
299 * applications (does not enforce permissions). Traditionally this is an SD
300 * card, but it may also be implemented as built-in storage in a device that
301 * is distinct from the protected internal storage and can be mounted as a
302 * filesystem on a computer.</em>
303 * <p>
304 * On devices with multiple users (as described by {@link UserManager}),
305 * each user has their own isolated external storage. Applications only have
306 * access to the external storage for the user they're running as.
307 * <p>
308 * In devices with multiple "external" storage directories, this directory
Dianne Hackborn407f6252010-10-04 11:31:17 -0700309 * represents the "primary" external storage that the user will interact
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700310 * with. Access to secondary storage is available through
311 * <p>
312 * Applications should not directly use this top-level directory, in order
313 * to avoid polluting the user's root namespace. Any files that are private
314 * to the application should be placed in a directory returned by
315 * {@link android.content.Context#getExternalFilesDir
Dianne Hackbornacaf0282010-03-30 14:39:35 -0700316 * Context.getExternalFilesDir}, which the system will take care of deleting
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700317 * if the application is uninstalled. Other shared files should be placed in
318 * one of the directories returned by
319 * {@link #getExternalStoragePublicDirectory}.
320 * <p>
321 * Writing to this path requires the
322 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
323 * and starting in read access requires the
Jeff Sharkey8c165792012-10-22 14:08:29 -0700324 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700325 * which is automatically granted if you hold the write permission.
326 * <p>
327 * Starting in {@link android.os.Build.VERSION_CODES#KEY_LIME_PIE}, if your
328 * application only needs to store internal data, consider using
329 * {@link Context#getExternalFilesDir(String)} or
330 * {@link Context#getExternalCacheDir()}, which require no permissions to
331 * read or write.
332 * <p>
333 * This path may change between platform versions, so applications should
334 * only persist relative paths.
335 * <p>
336 * Here is an example of typical code to monitor the state of external
337 * storage:
338 * <p>
339 * {@sample
340 * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800341 * monitor_storage}
Dianne Hackborn407f6252010-10-04 11:31:17 -0700342 *
343 * @see #getExternalStorageState()
344 * @see #isExternalStorageRemovable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 */
346 public static File getExternalStorageDirectory() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700347 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700348 return sCurrentUser.getExternalDirs()[0];
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700349 }
350
351 /** {@hide} */
352 public static File getLegacyExternalStorageDirectory() {
353 return new File(System.getenv(ENV_EXTERNAL_STORAGE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 }
355
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700356 /** {@hide} */
357 public static File getLegacyExternalStorageObbDirectory() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700358 return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700359 }
360
361 /** {@hide} */
362 public static File getEmulatedStorageSource(int userId) {
363 // /mnt/shell/emulated/0
364 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), String.valueOf(userId));
365 }
366
367 /** {@hide} */
368 public static File getEmulatedStorageObbSource() {
369 // /mnt/shell/emulated/obb
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700370 return new File(System.getenv(ENV_EMULATED_STORAGE_SOURCE), DIR_OBB);
Jeff Sharkey4fbbda42012-09-24 18:34:07 -0700371 }
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800374 * Standard directory in which to place any audio files that should be
375 * in the regular list of music for the user.
376 * This may be combined with
377 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
378 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
379 * of directories to categories a particular audio file as more than one
380 * type.
381 */
382 public static String DIRECTORY_MUSIC = "Music";
383
384 /**
385 * Standard directory in which to place any audio files that should be
386 * in the list of podcasts that the user can select (not as regular
387 * music).
388 * This may be combined with {@link #DIRECTORY_MUSIC},
389 * {@link #DIRECTORY_NOTIFICATIONS},
390 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
391 * of directories to categories a particular audio file as more than one
392 * type.
393 */
394 public static String DIRECTORY_PODCASTS = "Podcasts";
395
396 /**
397 * Standard directory in which to place any audio files that should be
398 * in the list of ringtones that the user can select (not as regular
399 * music).
400 * This may be combined with {@link #DIRECTORY_MUSIC},
401 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
402 * {@link #DIRECTORY_ALARMS} as a series
403 * of directories to categories a particular audio file as more than one
404 * type.
405 */
406 public static String DIRECTORY_RINGTONES = "Ringtones";
407
408 /**
409 * Standard directory in which to place any audio files that should be
410 * in the list of alarms that the user can select (not as regular
411 * music).
412 * This may be combined with {@link #DIRECTORY_MUSIC},
413 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
414 * and {@link #DIRECTORY_RINGTONES} as a series
415 * of directories to categories a particular audio file as more than one
416 * type.
417 */
418 public static String DIRECTORY_ALARMS = "Alarms";
419
420 /**
421 * Standard directory in which to place any audio files that should be
422 * in the list of notifications that the user can select (not as regular
423 * music).
424 * This may be combined with {@link #DIRECTORY_MUSIC},
425 * {@link #DIRECTORY_PODCASTS},
426 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
427 * of directories to categories a particular audio file as more than one
428 * type.
429 */
430 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
431
432 /**
433 * Standard directory in which to place pictures that are available to
434 * the user. Note that this is primarily a convention for the top-level
435 * public directory, as the media scanner will find and collect pictures
436 * in any directory.
437 */
438 public static String DIRECTORY_PICTURES = "Pictures";
439
440 /**
441 * Standard directory in which to place movies that are available to
442 * the user. Note that this is primarily a convention for the top-level
443 * public directory, as the media scanner will find and collect movies
444 * in any directory.
445 */
446 public static String DIRECTORY_MOVIES = "Movies";
447
448 /**
449 * Standard directory in which to place files that have been downloaded by
450 * the user. Note that this is primarily a convention for the top-level
451 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700452 * private directories. Also note that though the constant here is
453 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
454 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800455 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700456 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800457
458 /**
459 * The traditional location for pictures and videos when mounting the
460 * device as a camera. Note that this is primarily a convention for the
461 * top-level public directory, as this convention makes no sense elsewhere.
462 */
463 public static String DIRECTORY_DCIM = "DCIM";
464
465 /**
466 * Get a top-level public external storage directory for placing files of
467 * a particular type. This is where the user will typically place and
468 * manage their own files, so you should be careful about what you put here
469 * to ensure you don't erase their files or get in the way of their own
470 * organization.
471 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700472 * <p>On devices with multiple users (as described by {@link UserManager}),
473 * each user has their own isolated external storage. Applications only
474 * have access to the external storage for the user they're running as.</p>
475 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800476 * <p>Here is an example of typical code to manipulate a picture on
477 * the public external storage:</p>
478 *
479 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
480 * public_picture}
481 *
482 * @param type The type of storage directory to return. Should be one of
483 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
484 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
485 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
486 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
487 * {@link #DIRECTORY_DCIM}. May not be null.
488 *
489 * @return Returns the File path for the directory. Note that this
490 * directory may not yet exist, so you must make sure it exists before
491 * using it such as with {@link File#mkdirs File.mkdirs()}.
492 */
493 public static File getExternalStoragePublicDirectory(String type) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700494 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700495 return sCurrentUser.buildExternalStoragePublicDirs(type)[0];
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800496 }
497
498 /**
499 * Returns the path for android-specific data on the SD card.
500 * @hide
501 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700502 public static File[] buildExternalStorageAndroidDataDirs() {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700503 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700504 return sCurrentUser.buildExternalStorageAndroidDataDirs();
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800505 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700506
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800507 /**
508 * Generates the raw path to an application's data
509 * @hide
510 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700511 public static File[] buildExternalStorageAppDataDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700512 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700513 return sCurrentUser.buildExternalStorageAppDataDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800514 }
515
516 /**
517 * Generates the raw path to an application's media
518 * @hide
519 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700520 public static File[] buildExternalStorageAppMediaDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700521 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700522 return sCurrentUser.buildExternalStorageAppMediaDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800523 }
524
525 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800526 * Generates the raw path to an application's OBB files
527 * @hide
528 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700529 public static File[] buildExternalStorageAppObbDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700530 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700531 return sCurrentUser.buildExternalStorageAppObbDirs(packageName);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800532 }
533
534 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800535 * Generates the path to an application's files.
536 * @hide
537 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700538 public static File[] buildExternalStorageAppFilesDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700539 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700540 return sCurrentUser.buildExternalStorageAppFilesDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800541 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700542
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800543 /**
544 * Generates the path to an application's cache.
545 * @hide
546 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700547 public static File[] buildExternalStorageAppCacheDirs(String packageName) {
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700548 throwIfUserRequired();
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700549 return sCurrentUser.buildExternalStorageAppCacheDirs(packageName);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800550 }
551
552 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700553 * Return the download/cache content directory.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 */
555 public static File getDownloadCacheDirectory() {
556 return DOWNLOAD_CACHE_DIRECTORY;
557 }
558
559 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700560 * Unknown storage state, such as when a path isn't backed by known storage
561 * media.
562 *
563 * @see #getStorageState(File)
564 */
565 public static final String MEDIA_UNKNOWN = "unknown";
566
567 /**
568 * Storage state if the media is not present.
569 *
570 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 */
572 public static final String MEDIA_REMOVED = "removed";
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700575 * Storage state if the media is present but not mounted.
576 *
577 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 */
579 public static final String MEDIA_UNMOUNTED = "unmounted";
580
581 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700582 * Storage state if the media is present and being disk-checked.
583 *
584 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 */
586 public static final String MEDIA_CHECKING = "checking";
587
588 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700589 * Storage state if the media is present but is blank or is using an
590 * unsupported filesystem.
591 *
592 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 */
594 public static final String MEDIA_NOFS = "nofs";
595
596 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700597 * Storage state if the media is present and mounted at its mount point with
598 * read/write access.
599 *
600 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 */
602 public static final String MEDIA_MOUNTED = "mounted";
603
604 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700605 * Storage state if the media is present and mounted at its mount point with
606 * read-only access.
607 *
608 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 */
610 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
611
612 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700613 * Storage state if the media is present not mounted, and shared via USB
614 * mass storage.
615 *
616 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 */
618 public static final String MEDIA_SHARED = "shared";
619
620 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700621 * Storage state if the media was removed before it was unmounted.
622 *
623 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 */
625 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
626
627 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700628 * Storage state if the media is present but cannot be mounted. Typically
629 * this happens if the file system on the media is corrupted.
630 *
631 * @see #getStorageState(File)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 */
633 public static final String MEDIA_UNMOUNTABLE = "unmountable";
634
635 /**
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700636 * Returns the current state of the primary "external" storage device.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800637 *
Jeff Sharkey8c165792012-10-22 14:08:29 -0700638 * @see #getExternalStorageDirectory()
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700639 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
640 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
641 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
642 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
643 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 */
645 public static String getExternalStorageState() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700646 return getStorageState(getExternalStorageDirectory());
647 }
648
649 /**
650 * Returns the current state of the storage device that provides the given
651 * path.
652 *
653 * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
654 * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
655 * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
656 * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
657 * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
658 */
659 public static String getStorageState(File path) {
660 final String rawPath;
San Mehat7fd0fee2009-12-17 07:12:23 -0800661 try {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700662 rawPath = path.getCanonicalPath();
663 } catch (IOException e) {
664 Log.w(TAG, "Failed to resolve target path: " + e);
665 return Environment.MEDIA_UNKNOWN;
San Mehat7fd0fee2009-12-17 07:12:23 -0800666 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700667
668 try {
669 final IMountService mountService = IMountService.Stub.asInterface(
670 ServiceManager.getService("mount"));
671 final StorageVolume[] volumes = mountService.getVolumeList();
672 for (StorageVolume volume : volumes) {
673 if (rawPath.startsWith(volume.getPath())) {
674 return mountService.getVolumeState(volume.getPath());
675 }
676 }
677 } catch (RemoteException e) {
678 Log.w(TAG, "Failed to find external storage state: " + e);
679 }
680 return Environment.MEDIA_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682
Dianne Hackborn407f6252010-10-04 11:31:17 -0700683 /**
684 * Returns whether the primary "external" storage device is removable.
685 * If true is returned, this device is for example an SD card that the
686 * user can remove. If false is returned, the storage is built into
687 * the device and can not be physically removed.
688 *
689 * <p>See {@link #getExternalStorageDirectory()} for more information.
690 */
691 public static boolean isExternalStorageRemovable() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700692 final StorageVolume primary = getPrimaryVolume();
693 return (primary != null && primary.isRemovable());
Dianne Hackborn407f6252010-10-04 11:31:17 -0700694 }
695
Kenny Roote1ff2142010-10-12 11:20:01 -0700696 /**
697 * Returns whether the device has an external storage device which is
Andy Stadler50c294f2011-03-07 19:13:42 -0800698 * emulated. If true, the device does not have real external storage, and the directory
699 * returned by {@link #getExternalStorageDirectory()} will be allocated using a portion of
700 * the internal storage system.
701 *
702 * <p>Certain system services, such as the package manager, use this
Kenny Roote1ff2142010-10-12 11:20:01 -0700703 * to determine where to install an application.
Andy Stadler50c294f2011-03-07 19:13:42 -0800704 *
705 * <p>Emulated external storage may also be encrypted - see
706 * {@link android.app.admin.DevicePolicyManager#setStorageEncryption(
707 * android.content.ComponentName, boolean)} for additional details.
Kenny Roote1ff2142010-10-12 11:20:01 -0700708 */
709 public static boolean isExternalStorageEmulated() {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700710 final StorageVolume primary = getPrimaryVolume();
711 return (primary != null && primary.isEmulated());
Kenny Roote1ff2142010-10-12 11:20:01 -0700712 }
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 static File getDirectory(String variableName, String defaultPath) {
715 String path = System.getenv(variableName);
716 return path == null ? new File(defaultPath) : new File(path);
717 }
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700718
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800719 private static String getCanonicalPathOrNull(String variableName) {
720 String path = System.getenv(variableName);
721 if (path == null) {
722 return null;
723 }
724 try {
725 return new File(path).getCanonicalPath();
726 } catch (IOException e) {
727 Log.w(TAG, "Unable to resolve canonical path for " + path);
728 return null;
729 }
730 }
731
Jeff Sharkey48749fc2013-04-19 13:25:04 -0700732 /** {@hide} */
733 public static void setUserRequired(boolean userRequired) {
734 sUserRequired = userRequired;
735 }
736
737 private static void throwIfUserRequired() {
738 if (sUserRequired) {
739 Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment",
740 new Throwable());
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700741 }
742 }
743
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700744 /**
745 * Append path segments to each given base path, returning result.
746 *
747 * @hide
748 */
749 public static File[] buildPaths(File[] base, String... segments) {
750 File[] result = new File[base.length];
751 for (int i = 0; i < base.length; i++) {
752 result[i] = buildPath(base[i], segments);
753 }
754 return result;
755 }
756
757 /**
758 * Append path segments to given base path, returning result.
759 *
760 * @hide
761 */
762 public static File buildPath(File base, String... segments) {
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700763 File cur = base;
764 for (String segment : segments) {
765 if (cur == null) {
766 cur = new File(segment);
767 } else {
768 cur = new File(cur, segment);
769 }
770 }
771 return cur;
772 }
Jeff Sharkey63d0a062013-03-01 16:12:55 -0800773
774 /**
775 * If the given path exists on emulated external storage, return the
776 * translated backing path hosted on internal storage. This bypasses any
777 * emulation later, improving performance. This is <em>only</em> suitable
778 * for read-only access.
779 * <p>
780 * Returns original path if given path doesn't meet these criteria. Callers
781 * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}
782 * permission.
783 *
784 * @hide
785 */
786 public static File maybeTranslateEmulatedPathToInternal(File path) {
787 // Fast return if not emulated, or missing variables
788 if (!Environment.isExternalStorageEmulated()
789 || CANONCIAL_EMULATED_STORAGE_TARGET == null) {
790 return path;
791 }
792
793 try {
794 final String rawPath = path.getCanonicalPath();
795 if (rawPath.startsWith(CANONCIAL_EMULATED_STORAGE_TARGET)) {
796 final File internalPath = new File(DIR_MEDIA_STORAGE,
797 rawPath.substring(CANONCIAL_EMULATED_STORAGE_TARGET.length()));
798 if (internalPath.exists()) {
799 return internalPath;
800 }
801 }
802 } catch (IOException e) {
803 Log.w(TAG, "Failed to resolve canonical path for " + path);
804 }
805
806 // Unable to translate to internal path; use original
807 return path;
808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809}