blob: f7e7d39954527f1221a7e6cec2c17395d916f128 [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
169 * private directories.
170 */
171 public static String DIRECTORY_DOWNLOADS = "Downloads";
172
173 /**
174 * The traditional location for pictures and videos when mounting the
175 * device as a camera. Note that this is primarily a convention for the
176 * top-level public directory, as this convention makes no sense elsewhere.
177 */
178 public static String DIRECTORY_DCIM = "DCIM";
179
180 /**
181 * Get a top-level public external storage directory for placing files of
182 * a particular type. This is where the user will typically place and
183 * manage their own files, so you should be careful about what you put here
184 * to ensure you don't erase their files or get in the way of their own
185 * organization.
186 *
187 * <p>Here is an example of typical code to manipulate a picture on
188 * the public external storage:</p>
189 *
190 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
191 * public_picture}
192 *
193 * @param type The type of storage directory to return. Should be one of
194 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS},
195 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS},
196 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES},
197 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or
198 * {@link #DIRECTORY_DCIM}. May not be null.
199 *
200 * @return Returns the File path for the directory. Note that this
201 * directory may not yet exist, so you must make sure it exists before
202 * using it such as with {@link File#mkdirs File.mkdirs()}.
203 */
204 public static File getExternalStoragePublicDirectory(String type) {
205 return new File(getExternalStorageDirectory(), type);
206 }
207
208 /**
209 * Returns the path for android-specific data on the SD card.
210 * @hide
211 */
212 public static File getExternalStorageAndroidDataDir() {
213 return EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY;
214 }
215
216 /**
217 * Generates the raw path to an application's data
218 * @hide
219 */
220 public static File getExternalStorageAppDataDirectory(String packageName) {
221 return new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName);
222 }
223
224 /**
225 * Generates the raw path to an application's media
226 * @hide
227 */
228 public static File getExternalStorageAppMediaDirectory(String packageName) {
229 return new File(EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY, packageName);
230 }
231
232 /**
233 * Generates the path to an application's files.
234 * @hide
235 */
236 public static File getExternalStorageAppFilesDirectory(String packageName) {
237 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
238 packageName), "files");
239 }
240
241 /**
242 * Generates the path to an application's cache.
243 * @hide
244 */
245 public static File getExternalStorageAppCacheDirectory(String packageName) {
246 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY,
247 packageName), "cache");
248 }
249
250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * Gets the Android Download/Cache content directory.
252 */
253 public static File getDownloadCacheDirectory() {
254 return DOWNLOAD_CACHE_DIRECTORY;
255 }
256
257 /**
258 * getExternalStorageState() returns MEDIA_REMOVED if the media is not present.
259 */
260 public static final String MEDIA_REMOVED = "removed";
261
262 /**
263 * getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present
264 * but not mounted.
265 */
266 public static final String MEDIA_UNMOUNTED = "unmounted";
267
268 /**
269 * getExternalStorageState() returns MEDIA_CHECKING if the media is present
270 * and being disk-checked
271 */
272 public static final String MEDIA_CHECKING = "checking";
273
274 /**
275 * getExternalStorageState() returns MEDIA_NOFS if the media is present
276 * but is blank or is using an unsupported filesystem
277 */
278 public static final String MEDIA_NOFS = "nofs";
279
280 /**
281 * getExternalStorageState() returns MEDIA_MOUNTED if the media is present
282 * and mounted at its mount point with read/write access.
283 */
284 public static final String MEDIA_MOUNTED = "mounted";
285
286 /**
287 * getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present
288 * and mounted at its mount point with read only access.
289 */
290 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
291
292 /**
293 * getExternalStorageState() returns MEDIA_SHARED if the media is present
294 * not mounted, and shared via USB mass storage.
295 */
296 public static final String MEDIA_SHARED = "shared";
297
298 /**
299 * getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was
300 * removed before it was unmounted.
301 */
302 public static final String MEDIA_BAD_REMOVAL = "bad_removal";
303
304 /**
305 * getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present
306 * but cannot be mounted. Typically this happens if the file system on the
307 * media is corrupted.
308 */
309 public static final String MEDIA_UNMOUNTABLE = "unmountable";
310
311 /**
312 * Gets the current state of the external storage device.
San Mehat7fd0fee2009-12-17 07:12:23 -0800313 * Note: This call should be deprecated as it doesn't support
314 * multiple volumes.
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800315 *
316 * <p>See {@link #getExternalStorageDirectory()} for an example of its use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 */
318 public static String getExternalStorageState() {
San Mehat7fd0fee2009-12-17 07:12:23 -0800319 try {
320 if (mMntSvc == null) {
321 mMntSvc = IMountService.Stub.asInterface(ServiceManager
322 .getService("mount"));
323 }
324 return mMntSvc.getVolumeState(getExternalStorageDirectory().toString());
San Mehata6a72812010-01-07 22:39:53 -0800325 } catch (Exception rex) {
San Mehat7fd0fee2009-12-17 07:12:23 -0800326 return Environment.MEDIA_REMOVED;
327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 }
329
330 static File getDirectory(String variableName, String defaultPath) {
331 String path = System.getenv(variableName);
332 return path == null ? new File(defaultPath) : new File(path);
333 }
334}