blob: 812391c48e5fa22da51eacd6df9547a2a96bb21b [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
19import java.io.File;
20
San Mehatb1043402010-02-05 08:26:50 -080021import android.os.storage.IMountService;
San Mehat7fd0fee2009-12-17 07:12:23 -080022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023/**
24 * Provides access to environment variables.
25 */
26public class Environment {
27
28 private static final File ROOT_DIRECTORY
29 = getDirectory("ANDROID_ROOT", "/system");
30
San Mehat7fd0fee2009-12-17 07:12:23 -080031 private static IMountService mMntSvc = null;
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 /**
34 * Gets the Android root directory.
35 */
36 public static File getRootDirectory() {
37 return ROOT_DIRECTORY;
38 }
39
40 private static final File DATA_DIRECTORY
41 = getDirectory("ANDROID_DATA", "/data");
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 private static final File EXTERNAL_STORAGE_DIRECTORY
44 = getDirectory("EXTERNAL_STORAGE", "/sdcard");
45
Dianne Hackborne83cefce2010-02-04 17:38:14 -080046 private static final File EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY
47 = new File (new File(getDirectory("EXTERNAL_STORAGE", "/sdcard"),
48 "Android"), "data");
49
50 private static final File EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY
51 = new File (new File(getDirectory("EXTERNAL_STORAGE", "/sdcard"),
52 "Android"), "media");
53
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 private static final File DOWNLOAD_CACHE_DIRECTORY
55 = getDirectory("DOWNLOAD_CACHE", "/cache");
56
57 /**
58 * Gets the Android data directory.
59 */
60 public static File getDataDirectory() {
61 return DATA_DIRECTORY;
62 }
63
64 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -080065 * Gets the Android external storage directory. This directory may not
66 * currently be accessible if it has been mounted by the user on their
67 * computer, has been removed from the device, or some other problem has
68 * happened. You can determine its current state with
69 * {@link #getExternalStorageState()}.
70 *
Dianne Hackbornacaf0282010-03-30 14:39:35 -070071 * <p>Applications should not directly use this top-level directory, in
72 * order to avoid polluting the user's root namespace. Any files that are
73 * private to the application should be placed in a directory returned
74 * by {@link android.content.Context#getExternalFilesDir
75 * Context.getExternalFilesDir}, which the system will take care of deleting
76 * if the application is uninstalled. Other shared files should be placed
77 * in one of the directories returned by
78 * {@link #getExternalStoragePublicDirectory}.
79 *
Dianne Hackborne83cefce2010-02-04 17:38:14 -080080 * <p>Here is an example of typical code to monitor the state of
81 * external storage:</p>
82 *
83 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
84 * monitor_storage}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 */
86 public static File getExternalStorageDirectory() {
87 return EXTERNAL_STORAGE_DIRECTORY;
88 }
89
90 /**
Dianne Hackborne83cefce2010-02-04 17:38:14 -080091 * Standard directory in which to place any audio files that should be
92 * in the regular list of music for the user.
93 * This may be combined with
94 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
95 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
96 * of directories to categories a particular audio file as more than one
97 * type.
98 */
99 public static String DIRECTORY_MUSIC = "Music";
100
101 /**
102 * Standard directory in which to place any audio files that should be
103 * in the list of podcasts that the user can select (not as regular
104 * music).
105 * This may be combined with {@link #DIRECTORY_MUSIC},
106 * {@link #DIRECTORY_NOTIFICATIONS},
107 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
108 * of directories to categories a particular audio file as more than one
109 * type.
110 */
111 public static String DIRECTORY_PODCASTS = "Podcasts";
112
113 /**
114 * Standard directory in which to place any audio files that should be
115 * in the list of ringtones that the user can select (not as regular
116 * music).
117 * This may be combined with {@link #DIRECTORY_MUSIC},
118 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and
119 * {@link #DIRECTORY_ALARMS} as a series
120 * of directories to categories a particular audio file as more than one
121 * type.
122 */
123 public static String DIRECTORY_RINGTONES = "Ringtones";
124
125 /**
126 * Standard directory in which to place any audio files that should be
127 * in the list of alarms that the user can select (not as regular
128 * music).
129 * This may be combined with {@link #DIRECTORY_MUSIC},
130 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS},
131 * and {@link #DIRECTORY_RINGTONES} as a series
132 * of directories to categories a particular audio file as more than one
133 * type.
134 */
135 public static String DIRECTORY_ALARMS = "Alarms";
136
137 /**
138 * Standard directory in which to place any audio files that should be
139 * in the list of notifications that the user can select (not as regular
140 * music).
141 * This may be combined with {@link #DIRECTORY_MUSIC},
142 * {@link #DIRECTORY_PODCASTS},
143 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series
144 * of directories to categories a particular audio file as more than one
145 * type.
146 */
147 public static String DIRECTORY_NOTIFICATIONS = "Notifications";
148
149 /**
150 * Standard directory in which to place pictures that are available to
151 * the user. Note that this is primarily a convention for the top-level
152 * public directory, as the media scanner will find and collect pictures
153 * in any directory.
154 */
155 public static String DIRECTORY_PICTURES = "Pictures";
156
157 /**
158 * Standard directory in which to place movies that are available to
159 * the user. Note that this is primarily a convention for the top-level
160 * public directory, as the media scanner will find and collect movies
161 * in any directory.
162 */
163 public static String DIRECTORY_MOVIES = "Movies";
164
165 /**
166 * Standard directory in which to place files that have been downloaded by
167 * the user. Note that this is primarily a convention for the top-level
168 * public directory, you are free to download files anywhere in your own
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700169 * private directories. Also note that though the constant here is
170 * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for
171 * backwards compatibility reasons.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800172 */
Dianne Hackbornce59fb82010-04-07 17:19:04 -0700173 public static String DIRECTORY_DOWNLOADS = "Download";
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800174
175 /**
176 * The traditional location for pictures and videos when mounting the
177 * device as a camera. Note that this is primarily a convention for the
178 * top-level public directory, as this convention makes no sense elsewhere.
179 */
180 public static String DIRECTORY_DCIM = "DCIM";
181
182 /**
183 * Get a top-level public external storage directory for placing files of
184 * a particular type. This is where the user will typically place and
185 * manage their own files, so you should be careful about what you put here
186 * to ensure you don't erase their files or get in the way of their own
187 * organization.
188 *
189 * <p>Here is an example of typical code to manipulate a picture on
190 * the public external storage:</p>
191 *
192 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
193 * public_picture}
194 *
195 * @param type The type of storage directory to return. Should be one of
196 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
197 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
198 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
199 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
200 * {@link #DIRECTORY_DCIM}. May not be null.
201 *
202 * @return Returns the File path for the directory. Note that this
203 * directory may not yet exist, so you must make sure it exists before
204 * using it such as with {@link File#mkdirs File.mkdirs()}.
205 */
206 public static File getExternalStoragePublicDirectory(String type) {
207 return new File(getExternalStorageDirectory(), type);
208 }
209
210 /**
211 * Returns the path for android-specific data on the SD card.
212 * @hide
213 */
214 public static File getExternalStorageAndroidDataDir() {
215 return EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY;
216 }
217
218 /**
219 * Generates the raw path to an application's data
220 * @hide
221 */
222 public static File getExternalStorageAppDataDirectory(String packageName) {
223 return new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName);
224 }
225
226 /**
227 * Generates the raw path to an application's media
228 * @hide
229 */
230 public static File getExternalStorageAppMediaDirectory(String packageName) {
231 return new File(EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY, packageName);
232 }
233
234 /**
235 * Generates the path to an application's files.
236 * @hide
237 */
238 public static File getExternalStorageAppFilesDirectory(String packageName) {
239 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
240 packageName), "files");
241 }
242
243 /**
244 * Generates the path to an application's cache.
245 * @hide
246 */
247 public static File getExternalStorageAppCacheDirectory(String packageName) {
248 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
249 packageName), "cache");
250 }
251
252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * Gets the Android Download/Cache content directory.
254 */
255 public static File getDownloadCacheDirectory() {
256 return DOWNLOAD_CACHE_DIRECTORY;
257 }
258
259 /**
260 * getExternalStorageState() returns MEDIA_REMOVED if the media is not present.
261 */
262 public static final String MEDIA_REMOVED = "removed";
263
264 /**
265 * getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present
266 * but not mounted.
267 */
268 public static final String MEDIA_UNMOUNTED = "unmounted";
269
270 /**
271 * getExternalStorageState() returns MEDIA_CHECKING if the media is present
272 * and being disk-checked
273 */
274 public static final String MEDIA_CHECKING = "checking";
275
276 /**
277 * getExternalStorageState() returns MEDIA_NOFS if the media is present
278 * but is blank or is using an unsupported filesystem
279 */
280 public static final String MEDIA_NOFS = "nofs";
281
282 /**
283 * getExternalStorageState() returns MEDIA_MOUNTED if the media is present
284 * and mounted at its mount point with read/write access.
285 */
286 public static final String MEDIA_MOUNTED = "mounted";
287
288 /**
289 * getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present
290 * and mounted at its mount point with read only access.
291 */
292 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
293
294 /**
295 * getExternalStorageState() returns MEDIA_SHARED if the media is present
296 * not mounted, and shared via USB mass storage.
297 */
298 public static final String MEDIA_SHARED = "shared";
299
300 /**
301 * getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was
302 * removed before it was unmounted.
303 */
304 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
305
306 /**
307 * getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present
308 * but cannot be mounted. Typically this happens if the file system on the
309 * media is corrupted.
310 */
311 public static final String MEDIA_UNMOUNTABLE = "unmountable";
312
313 /**
314 * Gets the current state of the external storage device.
San Mehat7fd0fee2009-12-17 07:12:23 -0800315 * Note: This call should be deprecated as it doesn't support
316 * multiple volumes.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800317 *
318 * <p>See {@link #getExternalStorageDirectory()} for an example of its use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 */
320 public static String getExternalStorageState() {
San Mehat7fd0fee2009-12-17 07:12:23 -0800321 try {
322 if (mMntSvc == null) {
323 mMntSvc = IMountService.Stub.asInterface(ServiceManager
324 .getService("mount"));
325 }
326 return mMntSvc.getVolumeState(getExternalStorageDirectory().toString());
San Mehata6a72812010-01-07 22:39:53 -0800327 } catch (Exception rex) {
San Mehat7fd0fee2009-12-17 07:12:23 -0800328 return Environment.MEDIA_REMOVED;
329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
331
332 static File getDirectory(String variableName, String defaultPath) {
333 String path = System.getenv(variableName);
334 return path == null ? new File(defaultPath) : new File(path);
335 }
336}