Environment.java revision 1f4df90bfab8ca42eabe95f19eadff3432eee7fd
1/* 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 21import android.os.storage.IMountService; 22 23/** 24 * Provides access to environment variables. 25 */ 26public class Environment { 27 28 private static final File ROOT_DIRECTORY 29 = getDirectory("ANDROID_ROOT", "/system"); 30 31 private static IMountService mMntSvc = null; 32 33 /** 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 43 private static final File EXTERNAL_STORAGE_DIRECTORY 44 = getDirectory("EXTERNAL_STORAGE", "/sdcard"); 45 46 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 54 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 /** 65 * 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 * 71 * <p>Here is an example of typical code to monitor the state of 72 * external storage:</p> 73 * 74 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java 75 * monitor_storage} 76 */ 77 public static File getExternalStorageDirectory() { 78 return EXTERNAL_STORAGE_DIRECTORY; 79 } 80 81 /** 82 * Standard directory in which to place any audio files that should be 83 * in the regular list of music for the user. 84 * This may be combined with 85 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, 86 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series 87 * of directories to categories a particular audio file as more than one 88 * type. 89 */ 90 public static String DIRECTORY_MUSIC = "Music"; 91 92 /** 93 * Standard directory in which to place any audio files that should be 94 * in the list of podcasts that the user can select (not as regular 95 * music). 96 * This may be combined with {@link #DIRECTORY_MUSIC}, 97 * {@link #DIRECTORY_NOTIFICATIONS}, 98 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series 99 * of directories to categories a particular audio file as more than one 100 * type. 101 */ 102 public static String DIRECTORY_PODCASTS = "Podcasts"; 103 104 /** 105 * Standard directory in which to place any audio files that should be 106 * in the list of ringtones that the user can select (not as regular 107 * music). 108 * This may be combined with {@link #DIRECTORY_MUSIC}, 109 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and 110 * {@link #DIRECTORY_ALARMS} as a series 111 * of directories to categories a particular audio file as more than one 112 * type. 113 */ 114 public static String DIRECTORY_RINGTONES = "Ringtones"; 115 116 /** 117 * Standard directory in which to place any audio files that should be 118 * in the list of alarms that the user can select (not as regular 119 * music). 120 * This may be combined with {@link #DIRECTORY_MUSIC}, 121 * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, 122 * and {@link #DIRECTORY_RINGTONES} as a series 123 * of directories to categories a particular audio file as more than one 124 * type. 125 */ 126 public static String DIRECTORY_ALARMS = "Alarms"; 127 128 /** 129 * Standard directory in which to place any audio files that should be 130 * in the list of notifications that the user can select (not as regular 131 * music). 132 * This may be combined with {@link #DIRECTORY_MUSIC}, 133 * {@link #DIRECTORY_PODCASTS}, 134 * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series 135 * of directories to categories a particular audio file as more than one 136 * type. 137 */ 138 public static String DIRECTORY_NOTIFICATIONS = "Notifications"; 139 140 /** 141 * Standard directory in which to place pictures that are available to 142 * the user. Note that this is primarily a convention for the top-level 143 * public directory, as the media scanner will find and collect pictures 144 * in any directory. 145 */ 146 public static String DIRECTORY_PICTURES = "Pictures"; 147 148 /** 149 * Standard directory in which to place movies that are available to 150 * the user. Note that this is primarily a convention for the top-level 151 * public directory, as the media scanner will find and collect movies 152 * in any directory. 153 */ 154 public static String DIRECTORY_MOVIES = "Movies"; 155 156 /** 157 * Standard directory in which to place files that have been downloaded by 158 * the user. Note that this is primarily a convention for the top-level 159 * public directory, you are free to download files anywhere in your own 160 * private directories. 161 */ 162 public static String DIRECTORY_DOWNLOADS = "Downloads"; 163 164 /** 165 * The traditional location for pictures and videos when mounting the 166 * device as a camera. Note that this is primarily a convention for the 167 * top-level public directory, as this convention makes no sense elsewhere. 168 */ 169 public static String DIRECTORY_DCIM = "DCIM"; 170 171 /** 172 * Get a top-level public external storage directory for placing files of 173 * a particular type. This is where the user will typically place and 174 * manage their own files, so you should be careful about what you put here 175 * to ensure you don't erase their files or get in the way of their own 176 * organization. 177 * 178 * <p>Here is an example of typical code to manipulate a picture on 179 * the public external storage:</p> 180 * 181 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java 182 * public_picture} 183 * 184 * @param type The type of storage directory to return. Should be one of 185 * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS}, 186 * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS}, 187 * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES}, 188 * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, or 189 * {@link #DIRECTORY_DCIM}. May not be null. 190 * 191 * @return Returns the File path for the directory. Note that this 192 * directory may not yet exist, so you must make sure it exists before 193 * using it such as with {@link File#mkdirs File.mkdirs()}. 194 */ 195 public static File getExternalStoragePublicDirectory(String type) { 196 return new File(getExternalStorageDirectory(), type); 197 } 198 199 /** 200 * Returns the path for android-specific data on the SD card. 201 * @hide 202 */ 203 public static File getExternalStorageAndroidDataDir() { 204 return EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY; 205 } 206 207 /** 208 * Generates the raw path to an application's data 209 * @hide 210 */ 211 public static File getExternalStorageAppDataDirectory(String packageName) { 212 return new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName); 213 } 214 215 /** 216 * Generates the raw path to an application's media 217 * @hide 218 */ 219 public static File getExternalStorageAppMediaDirectory(String packageName) { 220 return new File(EXTERNAL_STORAGE_ANDROID_MEDIA_DIRECTORY, packageName); 221 } 222 223 /** 224 * Generates the path to an application's files. 225 * @hide 226 */ 227 public static File getExternalStorageAppFilesDirectory(String packageName) { 228 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, 229 packageName), "files"); 230 } 231 232 /** 233 * Generates the path to an application's cache. 234 * @hide 235 */ 236 public static File getExternalStorageAppCacheDirectory(String packageName) { 237 return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, 238 packageName), "cache"); 239 } 240 241 /** 242 * Gets the Android Download/Cache content directory. 243 */ 244 public static File getDownloadCacheDirectory() { 245 return DOWNLOAD_CACHE_DIRECTORY; 246 } 247 248 /** 249 * getExternalStorageState() returns MEDIA_REMOVED if the media is not present. 250 */ 251 public static final String MEDIA_REMOVED = "removed"; 252 253 /** 254 * getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present 255 * but not mounted. 256 */ 257 public static final String MEDIA_UNMOUNTED = "unmounted"; 258 259 /** 260 * getExternalStorageState() returns MEDIA_CHECKING if the media is present 261 * and being disk-checked 262 */ 263 public static final String MEDIA_CHECKING = "checking"; 264 265 /** 266 * getExternalStorageState() returns MEDIA_NOFS if the media is present 267 * but is blank or is using an unsupported filesystem 268 */ 269 public static final String MEDIA_NOFS = "nofs"; 270 271 /** 272 * getExternalStorageState() returns MEDIA_MOUNTED if the media is present 273 * and mounted at its mount point with read/write access. 274 */ 275 public static final String MEDIA_MOUNTED = "mounted"; 276 277 /** 278 * getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present 279 * and mounted at its mount point with read only access. 280 */ 281 public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro"; 282 283 /** 284 * getExternalStorageState() returns MEDIA_SHARED if the media is present 285 * not mounted, and shared via USB mass storage. 286 */ 287 public static final String MEDIA_SHARED = "shared"; 288 289 /** 290 * getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was 291 * removed before it was unmounted. 292 */ 293 public static final String MEDIA_BAD_REMOVAL = "bad_removal"; 294 295 /** 296 * getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present 297 * but cannot be mounted. Typically this happens if the file system on the 298 * media is corrupted. 299 */ 300 public static final String MEDIA_UNMOUNTABLE = "unmountable"; 301 302 /** 303 * Gets the current state of the external storage device. 304 * Note: This call should be deprecated as it doesn't support 305 * multiple volumes. 306 * 307 * <p>See {@link #getExternalStorageDirectory()} for an example of its use. 308 */ 309 public static String getExternalStorageState() { 310 try { 311 if (mMntSvc == null) { 312 mMntSvc = IMountService.Stub.asInterface(ServiceManager 313 .getService("mount")); 314 } 315 return mMntSvc.getVolumeState(getExternalStorageDirectory().toString()); 316 } catch (Exception rex) { 317 return Environment.MEDIA_REMOVED; 318 } 319 } 320 321 static File getDirectory(String variableName, String defaultPath) { 322 String path = System.getenv(variableName); 323 return path == null ? new File(defaultPath) : new File(path); 324 } 325} 326