Context.java revision c68c913d357e2955d4bd7ca52829071e531c7825
1/* 2 * Copyright (C) 2006 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.content; 18 19import android.content.pm.ApplicationInfo; 20import android.content.pm.PackageManager; 21import android.content.res.AssetManager; 22import android.content.res.Resources; 23import android.content.res.TypedArray; 24import android.database.DatabaseErrorHandler; 25import android.database.sqlite.SQLiteDatabase; 26import android.database.sqlite.SQLiteDatabase.CursorFactory; 27import android.graphics.Bitmap; 28import android.graphics.drawable.Drawable; 29import android.media.MediaScannerConnection.OnScanCompletedListener; 30import android.net.Uri; 31import android.os.Bundle; 32import android.os.Handler; 33import android.os.Looper; 34import android.util.AttributeSet; 35 36import java.io.File; 37import java.io.FileInputStream; 38import java.io.FileNotFoundException; 39import java.io.FileOutputStream; 40import java.io.IOException; 41import java.io.InputStream; 42 43/** 44 * Interface to global information about an application environment. This is 45 * an abstract class whose implementation is provided by 46 * the Android system. It 47 * allows access to application-specific resources and classes, as well as 48 * up-calls for application-level operations such as launching activities, 49 * broadcasting and receiving intents, etc. 50 */ 51public abstract class Context { 52 /** 53 * File creation mode: the default mode, where the created file can only 54 * be accessed by the calling application (or all applications sharing the 55 * same user ID). 56 * @see #MODE_WORLD_READABLE 57 * @see #MODE_WORLD_WRITEABLE 58 */ 59 public static final int MODE_PRIVATE = 0x0000; 60 /** 61 * File creation mode: allow all other applications to have read access 62 * to the created file. 63 * @see #MODE_PRIVATE 64 * @see #MODE_WORLD_WRITEABLE 65 */ 66 public static final int MODE_WORLD_READABLE = 0x0001; 67 /** 68 * File creation mode: allow all other applications to have write access 69 * to the created file. 70 * @see #MODE_PRIVATE 71 * @see #MODE_WORLD_READABLE 72 */ 73 public static final int MODE_WORLD_WRITEABLE = 0x0002; 74 /** 75 * File creation mode: for use with {@link #openFileOutput}, if the file 76 * already exists then write data to the end of the existing file 77 * instead of erasing it. 78 * @see #openFileOutput 79 */ 80 public static final int MODE_APPEND = 0x8000; 81 82 /** 83 * SharedPreference loading flag: when set, the file on disk will 84 * be checked for modification even if the shared preferences 85 * instance is already loaded in this process. This behavior is 86 * sometimes desired in cases where the application has multiple 87 * processes, all writing to the same SharedPreferences file. 88 * Generally there are better forms of communication between 89 * processes, though. 90 * 91 * <p>This was the legacy (but undocumented) behavior in and 92 * before Gingerbread (Android 2.3) and this flag is implied when 93 * targetting such releases. For applications targetting SDK 94 * versions <em>greater than</em> Android 2.3, this flag must be 95 * explicitly set if desired. 96 * 97 * @see #getSharedPreferences 98 */ 99 public static final int MODE_MULTI_PROCESS = 0x0004; 100 101 /** 102 * Flag for {@link #bindService}: automatically create the service as long 103 * as the binding exists. Note that while this will create the service, 104 * its {@link android.app.Service#onStartCommand} 105 * method will still only be called due to an 106 * explicit call to {@link #startService}. Even without that, though, 107 * this still provides you with access to the service object while the 108 * service is created. 109 * 110 * <p>Note that prior to {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, 111 * not supplying this flag would also impact how important the system 112 * consider's the target service's process to be. When set, the only way 113 * for it to be raised was by binding from a service in which case it will 114 * only be important when that activity is in the foreground. Now to 115 * achieve this behavior you must explicitly supply the new flag 116 * {@link #BIND_ADJUST_WITH_ACTIVITY}. For compatibility, old applications 117 * that don't specify {@link #BIND_AUTO_CREATE} will automatically have 118 * the flags {@link #BIND_WAIVE_PRIORITY} and 119 * {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve 120 * the same result. 121 */ 122 public static final int BIND_AUTO_CREATE = 0x0001; 123 124 /** 125 * Flag for {@link #bindService}: include debugging help for mismatched 126 * calls to unbind. When this flag is set, the callstack of the following 127 * {@link #unbindService} call is retained, to be printed if a later 128 * incorrect unbind call is made. Note that doing this requires retaining 129 * information about the binding that was made for the lifetime of the app, 130 * resulting in a leak -- this should only be used for debugging. 131 */ 132 public static final int BIND_DEBUG_UNBIND = 0x0002; 133 134 /** 135 * Flag for {@link #bindService}: don't allow this binding to raise 136 * the target service's process to the foreground scheduling priority. 137 * It will still be raised to at least the same memory priority 138 * as the client (so that its process will not be killable in any 139 * situation where the client is not killable), but for CPU scheduling 140 * purposes it may be left in the background. This only has an impact 141 * in the situation where the binding client is a foreground process 142 * and the target service is in a background process. 143 */ 144 public static final int BIND_NOT_FOREGROUND = 0x0004; 145 146 /** 147 * Flag for {@link #bindService}: indicates that the client application 148 * binding to this service considers the service to be more important than 149 * the app itself. When set, the platform will try to have the out of 150 * memory kill the app before it kills the service it is bound to, though 151 * this is not guaranteed to be the case. 152 */ 153 public static final int BIND_ABOVE_CLIENT = 0x0008; 154 155 /** 156 * Flag for {@link #bindService}: allow the process hosting the bound 157 * service to go through its normal memory management. It will be 158 * treated more like a running service, allowing the system to 159 * (temporarily) expunge the process if low on memory or for some other 160 * whim it may have, and being more aggressive about making it a candidate 161 * to be killed (and restarted) if running for a long time. 162 */ 163 public static final int BIND_ALLOW_OOM_MANAGEMENT = 0x0010; 164 165 /** 166 * Flag for {@link #bindService}: don't impact the scheduling or 167 * memory management priority of the target service's hosting process. 168 * Allows the service's process to be managed on the background LRU list 169 * just like a regular application process in the background. 170 */ 171 public static final int BIND_WAIVE_PRIORITY = 0x0020; 172 173 /** 174 * Flag for {@link #bindService}: this service is very important to 175 * the client, so should be brought to the foreground process level 176 * when the client is. Normally a process can only be raised to the 177 * visibility level by a client, even if that client is in the foreground. 178 */ 179 public static final int BIND_IMPORTANT = 0x0040; 180 181 /** 182 * Flag for {@link #bindService}: If binding from an activity, allow the 183 * target service's process importance to be raised based on whether the 184 * activity is visible to the user, regardless whether another flag is 185 * used to reduce the amount that the client process's overall importance 186 * is used to impact it. 187 */ 188 public static final int BIND_ADJUST_WITH_ACTIVITY = 0x0040; 189 190 /** Return an AssetManager instance for your application's package. */ 191 public abstract AssetManager getAssets(); 192 193 /** Return a Resources instance for your application's package. */ 194 public abstract Resources getResources(); 195 196 /** Return PackageManager instance to find global package information. */ 197 public abstract PackageManager getPackageManager(); 198 199 /** Return a ContentResolver instance for your application's package. */ 200 public abstract ContentResolver getContentResolver(); 201 202 /** 203 * Return the Looper for the main thread of the current process. This is 204 * the thread used to dispatch calls to application components (activities, 205 * services, etc). 206 */ 207 public abstract Looper getMainLooper(); 208 209 /** 210 * Return the context of the single, global Application object of the 211 * current process. This generally should only be used if you need a 212 * Context whose lifecycle is separate from the current context, that is 213 * tied to the lifetime of the process rather than the current component. 214 * 215 * <p>Consider for example how this interacts with 216 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)}: 217 * <ul> 218 * <li> <p>If used from an Activity context, the receiver is being registered 219 * within that activity. This means that you are expected to unregister 220 * before the activity is done being destroyed; in fact if you do not do 221 * so, the framework will clean up your leaked registration as it removes 222 * the activity and log an error. Thus, if you use the Activity context 223 * to register a receiver that is static (global to the process, not 224 * associated with an Activity instance) then that registration will be 225 * removed on you at whatever point the activity you used is destroyed. 226 * <li> <p>If used from the Context returned here, the receiver is being 227 * registered with the global state associated with your application. Thus 228 * it will never be unregistered for you. This is necessary if the receiver 229 * is associated with static data, not a particular component. However 230 * using the ApplicationContext elsewhere can easily lead to serious leaks 231 * if you forget to unregister, unbind, etc. 232 * </ul> 233 */ 234 public abstract Context getApplicationContext(); 235 236 /** 237 * Add a new {@link ComponentCallbacks} to the base application of the 238 * Context, which will be called at the same times as the ComponentCallbacks 239 * methods of activities and other components are called. Note that you 240 * <em>must</em> be sure to use {@link #unregisterComponentCallbacks} when 241 * appropriate in the future; this will not be removed for you. 242 */ 243 public void registerComponentCallbacks(ComponentCallbacks callback) { 244 getApplicationContext().registerComponentCallbacks(callback); 245 } 246 247 /** 248 * Remove a {@link ComponentCallbacks} objec that was previously registered 249 * with {@link #registerComponentCallbacks(ComponentCallbacks)}. 250 */ 251 public void unregisterComponentCallbacks(ComponentCallbacks callback) { 252 getApplicationContext().unregisterComponentCallbacks(callback); 253 } 254 255 /** 256 * Return a localized, styled CharSequence from the application's package's 257 * default string table. 258 * 259 * @param resId Resource id for the CharSequence text 260 */ 261 public final CharSequence getText(int resId) { 262 return getResources().getText(resId); 263 } 264 265 /** 266 * Return a localized string from the application's package's 267 * default string table. 268 * 269 * @param resId Resource id for the string 270 */ 271 public final String getString(int resId) { 272 return getResources().getString(resId); 273 } 274 275 /** 276 * Return a localized formatted string from the application's package's 277 * default string table, substituting the format arguments as defined in 278 * {@link java.util.Formatter} and {@link java.lang.String#format}. 279 * 280 * @param resId Resource id for the format string 281 * @param formatArgs The format arguments that will be used for substitution. 282 */ 283 284 public final String getString(int resId, Object... formatArgs) { 285 return getResources().getString(resId, formatArgs); 286 } 287 288 /** 289 * Set the base theme for this context. Note that this should be called 290 * before any views are instantiated in the Context (for example before 291 * calling {@link android.app.Activity#setContentView} or 292 * {@link android.view.LayoutInflater#inflate}). 293 * 294 * @param resid The style resource describing the theme. 295 */ 296 public abstract void setTheme(int resid); 297 298 /** @hide Needed for some internal implementation... not public because 299 * you can't assume this actually means anything. */ 300 public int getThemeResId() { 301 return 0; 302 } 303 304 /** 305 * Return the Theme object associated with this Context. 306 */ 307 public abstract Resources.Theme getTheme(); 308 309 /** 310 * Retrieve styled attribute information in this Context's theme. See 311 * {@link Resources.Theme#obtainStyledAttributes(int[])} 312 * for more information. 313 * 314 * @see Resources.Theme#obtainStyledAttributes(int[]) 315 */ 316 public final TypedArray obtainStyledAttributes( 317 int[] attrs) { 318 return getTheme().obtainStyledAttributes(attrs); 319 } 320 321 /** 322 * Retrieve styled attribute information in this Context's theme. See 323 * {@link Resources.Theme#obtainStyledAttributes(int, int[])} 324 * for more information. 325 * 326 * @see Resources.Theme#obtainStyledAttributes(int, int[]) 327 */ 328 public final TypedArray obtainStyledAttributes( 329 int resid, int[] attrs) throws Resources.NotFoundException { 330 return getTheme().obtainStyledAttributes(resid, attrs); 331 } 332 333 /** 334 * Retrieve styled attribute information in this Context's theme. See 335 * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} 336 * for more information. 337 * 338 * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 339 */ 340 public final TypedArray obtainStyledAttributes( 341 AttributeSet set, int[] attrs) { 342 return getTheme().obtainStyledAttributes(set, attrs, 0, 0); 343 } 344 345 /** 346 * Retrieve styled attribute information in this Context's theme. See 347 * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} 348 * for more information. 349 * 350 * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 351 */ 352 public final TypedArray obtainStyledAttributes( 353 AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) { 354 return getTheme().obtainStyledAttributes( 355 set, attrs, defStyleAttr, defStyleRes); 356 } 357 358 /** 359 * Return a class loader you can use to retrieve classes in this package. 360 */ 361 public abstract ClassLoader getClassLoader(); 362 363 /** Return the name of this application's package. */ 364 public abstract String getPackageName(); 365 366 /** Return the full application info for this context's package. */ 367 public abstract ApplicationInfo getApplicationInfo(); 368 369 /** 370 * Return the full path to this context's primary Android package. 371 * The Android package is a ZIP file which contains the application's 372 * primary resources. 373 * 374 * <p>Note: this is not generally useful for applications, since they should 375 * not be directly accessing the file system. 376 * 377 * @return String Path to the resources. 378 */ 379 public abstract String getPackageResourcePath(); 380 381 /** 382 * Return the full path to this context's primary Android package. 383 * The Android package is a ZIP file which contains application's 384 * primary code and assets. 385 * 386 * <p>Note: this is not generally useful for applications, since they should 387 * not be directly accessing the file system. 388 * 389 * @return String Path to the code and assets. 390 */ 391 public abstract String getPackageCodePath(); 392 393 /** 394 * {@hide} 395 * Return the full path to the shared prefs file for the given prefs group name. 396 * 397 * <p>Note: this is not generally useful for applications, since they should 398 * not be directly accessing the file system. 399 */ 400 public abstract File getSharedPrefsFile(String name); 401 402 /** 403 * Retrieve and hold the contents of the preferences file 'name', returning 404 * a SharedPreferences through which you can retrieve and modify its 405 * values. Only one instance of the SharedPreferences object is returned 406 * to any callers for the same name, meaning they will see each other's 407 * edits as soon as they are made. 408 * 409 * @param name Desired preferences file. If a preferences file by this name 410 * does not exist, it will be created when you retrieve an 411 * editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). 412 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 413 * default operation, {@link #MODE_WORLD_READABLE} 414 * and {@link #MODE_WORLD_WRITEABLE} to control permissions. The bit 415 * {@link #MODE_MULTI_PROCESS} can also be used if multiple processes 416 * are mutating the same SharedPreferences file. {@link #MODE_MULTI_PROCESS} 417 * is always on in apps targetting Gingerbread (Android 2.3) and below, and 418 * off by default in later versions. 419 * 420 * @return Returns the single SharedPreferences instance that can be used 421 * to retrieve and modify the preference values. 422 * 423 * @see #MODE_PRIVATE 424 * @see #MODE_WORLD_READABLE 425 * @see #MODE_WORLD_WRITEABLE 426 * @see #MODE_MULTI_PROCESS 427 */ 428 public abstract SharedPreferences getSharedPreferences(String name, 429 int mode); 430 431 /** 432 * Open a private file associated with this Context's application package 433 * for reading. 434 * 435 * @param name The name of the file to open; can not contain path 436 * separators. 437 * 438 * @return FileInputStream Resulting input stream. 439 * 440 * @see #openFileOutput 441 * @see #fileList 442 * @see #deleteFile 443 * @see java.io.FileInputStream#FileInputStream(String) 444 */ 445 public abstract FileInputStream openFileInput(String name) 446 throws FileNotFoundException; 447 448 /** 449 * Open a private file associated with this Context's application package 450 * for writing. Creates the file if it doesn't already exist. 451 * 452 * @param name The name of the file to open; can not contain path 453 * separators. 454 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 455 * default operation, {@link #MODE_APPEND} to append to an existing file, 456 * {@link #MODE_WORLD_READABLE} and {@link #MODE_WORLD_WRITEABLE} to control 457 * permissions. 458 * 459 * @return FileOutputStream Resulting output stream. 460 * 461 * @see #MODE_APPEND 462 * @see #MODE_PRIVATE 463 * @see #MODE_WORLD_READABLE 464 * @see #MODE_WORLD_WRITEABLE 465 * @see #openFileInput 466 * @see #fileList 467 * @see #deleteFile 468 * @see java.io.FileOutputStream#FileOutputStream(String) 469 */ 470 public abstract FileOutputStream openFileOutput(String name, int mode) 471 throws FileNotFoundException; 472 473 /** 474 * Delete the given private file associated with this Context's 475 * application package. 476 * 477 * @param name The name of the file to delete; can not contain path 478 * separators. 479 * 480 * @return True if the file was successfully deleted; else 481 * false. 482 * 483 * @see #openFileInput 484 * @see #openFileOutput 485 * @see #fileList 486 * @see java.io.File#delete() 487 */ 488 public abstract boolean deleteFile(String name); 489 490 /** 491 * Returns the absolute path on the filesystem where a file created with 492 * {@link #openFileOutput} is stored. 493 * 494 * @param name The name of the file for which you would like to get 495 * its path. 496 * 497 * @return Returns an absolute path to the given file. 498 * 499 * @see #openFileOutput 500 * @see #getFilesDir 501 * @see #getDir 502 */ 503 public abstract File getFileStreamPath(String name); 504 505 /** 506 * Returns the absolute path to the directory on the filesystem where 507 * files created with {@link #openFileOutput} are stored. 508 * 509 * @return Returns the path of the directory holding application files. 510 * 511 * @see #openFileOutput 512 * @see #getFileStreamPath 513 * @see #getDir 514 */ 515 public abstract File getFilesDir(); 516 517 /** 518 * Returns the absolute path to the directory on the external filesystem 519 * (that is somewhere on {@link android.os.Environment#getExternalStorageDirectory() 520 * Environment.getExternalStorageDirectory()}) where the application can 521 * place persistent files it owns. These files are private to the 522 * applications, and not typically visible to the user as media. 523 * 524 * <p>This is like {@link #getFilesDir()} in that these 525 * files will be deleted when the application is uninstalled, however there 526 * are some important differences: 527 * 528 * <ul> 529 * <li>External files are not always available: they will disappear if the 530 * user mounts the external storage on a computer or removes it. See the 531 * APIs on {@link android.os.Environment} for information in the storage state. 532 * <li>There is no security enforced with these files. All applications 533 * can read and write files placed here. 534 * </ul> 535 * 536 * <p>Here is an example of typical code to manipulate a file in 537 * an application's private storage:</p> 538 * 539 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java 540 * private_file} 541 * 542 * <p>If you supply a non-null <var>type</var> to this function, the returned 543 * file will be a path to a sub-directory of the given type. Though these files 544 * are not automatically scanned by the media scanner, you can explicitly 545 * add them to the media database with 546 * {@link android.media.MediaScannerConnection#scanFile(Context, String[], String[], 547 * OnScanCompletedListener) MediaScannerConnection.scanFile}. 548 * Note that this is not the same as 549 * {@link android.os.Environment#getExternalStoragePublicDirectory 550 * Environment.getExternalStoragePublicDirectory()}, which provides 551 * directories of media shared by all applications. The 552 * directories returned here are 553 * owned by the application, and their contents will be removed when the 554 * application is uninstalled. Unlike 555 * {@link android.os.Environment#getExternalStoragePublicDirectory 556 * Environment.getExternalStoragePublicDirectory()}, the directory 557 * returned here will be automatically created for you. 558 * 559 * <p>Here is an example of typical code to manipulate a picture in 560 * an application's private storage and add it to the media database:</p> 561 * 562 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java 563 * private_picture} 564 * 565 * @param type The type of files directory to return. May be null for 566 * the root of the files directory or one of 567 * the following Environment constants for a subdirectory: 568 * {@link android.os.Environment#DIRECTORY_MUSIC}, 569 * {@link android.os.Environment#DIRECTORY_PODCASTS}, 570 * {@link android.os.Environment#DIRECTORY_RINGTONES}, 571 * {@link android.os.Environment#DIRECTORY_ALARMS}, 572 * {@link android.os.Environment#DIRECTORY_NOTIFICATIONS}, 573 * {@link android.os.Environment#DIRECTORY_PICTURES}, or 574 * {@link android.os.Environment#DIRECTORY_MOVIES}. 575 * 576 * @return Returns the path of the directory holding application files 577 * on external storage. Returns null if external storage is not currently 578 * mounted so it could not ensure the path exists; you will need to call 579 * this method again when it is available. 580 * 581 * @see #getFilesDir 582 * @see android.os.Environment#getExternalStoragePublicDirectory 583 */ 584 public abstract File getExternalFilesDir(String type); 585 586 /** 587 * Return the directory where this application's OBB files (if there 588 * are any) can be found. Note if the application does not have any OBB 589 * files, this directory may not exist. 590 */ 591 public abstract File getObbDir(); 592 593 /** 594 * Returns the absolute path to the application specific cache directory 595 * on the filesystem. These files will be ones that get deleted first when the 596 * device runs low on storage. 597 * There is no guarantee when these files will be deleted. 598 * 599 * <strong>Note: you should not <em>rely</em> on the system deleting these 600 * files for you; you should always have a reasonable maximum, such as 1 MB, 601 * for the amount of space you consume with cache files, and prune those 602 * files when exceeding that space.</strong> 603 * 604 * @return Returns the path of the directory holding application cache files. 605 * 606 * @see #openFileOutput 607 * @see #getFileStreamPath 608 * @see #getDir 609 */ 610 public abstract File getCacheDir(); 611 612 /** 613 * Returns the absolute path to the directory on the external filesystem 614 * (that is somewhere on {@link android.os.Environment#getExternalStorageDirectory() 615 * Environment.getExternalStorageDirectory()} where the application can 616 * place cache files it owns. 617 * 618 * <p>This is like {@link #getCacheDir()} in that these 619 * files will be deleted when the application is uninstalled, however there 620 * are some important differences: 621 * 622 * <ul> 623 * <li>The platform does not monitor the space available in external storage, 624 * and thus will not automatically delete these files. Note that you should 625 * be managing the maximum space you will use for these anyway, just like 626 * with {@link #getCacheDir()}. 627 * <li>External files are not always available: they will disappear if the 628 * user mounts the external storage on a computer or removes it. See the 629 * APIs on {@link android.os.Environment} for information in the storage state. 630 * <li>There is no security enforced with these files. All applications 631 * can read and write files placed here. 632 * </ul> 633 * 634 * @return Returns the path of the directory holding application cache files 635 * on external storage. Returns null if external storage is not currently 636 * mounted so it could not ensure the path exists; you will need to call 637 * this method again when it is available. 638 * 639 * @see #getCacheDir 640 */ 641 public abstract File getExternalCacheDir(); 642 643 /** 644 * Returns an array of strings naming the private files associated with 645 * this Context's application package. 646 * 647 * @return Array of strings naming the private files. 648 * 649 * @see #openFileInput 650 * @see #openFileOutput 651 * @see #deleteFile 652 */ 653 public abstract String[] fileList(); 654 655 /** 656 * Retrieve, creating if needed, a new directory in which the application 657 * can place its own custom data files. You can use the returned File 658 * object to create and access files in this directory. Note that files 659 * created through a File object will only be accessible by your own 660 * application; you can only set the mode of the entire directory, not 661 * of individual files. 662 * 663 * @param name Name of the directory to retrieve. This is a directory 664 * that is created as part of your application data. 665 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 666 * default operation, {@link #MODE_WORLD_READABLE} and 667 * {@link #MODE_WORLD_WRITEABLE} to control permissions. 668 * 669 * @return Returns a File object for the requested directory. The directory 670 * will have been created if it does not already exist. 671 * 672 * @see #openFileOutput(String, int) 673 */ 674 public abstract File getDir(String name, int mode); 675 676 /** 677 * Open a new private SQLiteDatabase associated with this Context's 678 * application package. Create the database file if it doesn't exist. 679 * 680 * @param name The name (unique in the application package) of the database. 681 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 682 * default operation, {@link #MODE_WORLD_READABLE} 683 * and {@link #MODE_WORLD_WRITEABLE} to control permissions. 684 * @param factory An optional factory class that is called to instantiate a 685 * cursor when query is called. 686 * 687 * @return The contents of a newly created database with the given name. 688 * @throws android.database.sqlite.SQLiteException if the database file could not be opened. 689 * 690 * @see #MODE_PRIVATE 691 * @see #MODE_WORLD_READABLE 692 * @see #MODE_WORLD_WRITEABLE 693 * @see #deleteDatabase 694 */ 695 public abstract SQLiteDatabase openOrCreateDatabase(String name, 696 int mode, CursorFactory factory); 697 698 /** 699 * Open a new private SQLiteDatabase associated with this Context's 700 * application package. Creates the database file if it doesn't exist. 701 * 702 * <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be 703 * used to handle corruption when sqlite reports database corruption.</p> 704 * 705 * @param name The name (unique in the application package) of the database. 706 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 707 * default operation, {@link #MODE_WORLD_READABLE} 708 * and {@link #MODE_WORLD_WRITEABLE} to control permissions. 709 * @param factory An optional factory class that is called to instantiate a 710 * cursor when query is called. 711 * @param errorHandler the {@link DatabaseErrorHandler} to be used when sqlite reports database 712 * corruption. if null, {@link android.database.DefaultDatabaseErrorHandler} is assumed. 713 * @return The contents of a newly created database with the given name. 714 * @throws android.database.sqlite.SQLiteException if the database file could not be opened. 715 * 716 * @see #MODE_PRIVATE 717 * @see #MODE_WORLD_READABLE 718 * @see #MODE_WORLD_WRITEABLE 719 * @see #deleteDatabase 720 */ 721 public abstract SQLiteDatabase openOrCreateDatabase(String name, 722 int mode, CursorFactory factory, DatabaseErrorHandler errorHandler); 723 724 /** 725 * Delete an existing private SQLiteDatabase associated with this Context's 726 * application package. 727 * 728 * @param name The name (unique in the application package) of the 729 * database. 730 * 731 * @return True if the database was successfully deleted; else false. 732 * 733 * @see #openOrCreateDatabase 734 */ 735 public abstract boolean deleteDatabase(String name); 736 737 /** 738 * Returns the absolute path on the filesystem where a database created with 739 * {@link #openOrCreateDatabase} is stored. 740 * 741 * @param name The name of the database for which you would like to get 742 * its path. 743 * 744 * @return Returns an absolute path to the given database. 745 * 746 * @see #openOrCreateDatabase 747 */ 748 public abstract File getDatabasePath(String name); 749 750 /** 751 * Returns an array of strings naming the private databases associated with 752 * this Context's application package. 753 * 754 * @return Array of strings naming the private databases. 755 * 756 * @see #openOrCreateDatabase 757 * @see #deleteDatabase 758 */ 759 public abstract String[] databaseList(); 760 761 /** 762 * @deprecated Use {@link android.app.WallpaperManager#getDrawable 763 * WallpaperManager.get()} instead. 764 */ 765 @Deprecated 766 public abstract Drawable getWallpaper(); 767 768 /** 769 * @deprecated Use {@link android.app.WallpaperManager#peekDrawable 770 * WallpaperManager.peek()} instead. 771 */ 772 @Deprecated 773 public abstract Drawable peekWallpaper(); 774 775 /** 776 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumWidth() 777 * WallpaperManager.getDesiredMinimumWidth()} instead. 778 */ 779 @Deprecated 780 public abstract int getWallpaperDesiredMinimumWidth(); 781 782 /** 783 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumHeight() 784 * WallpaperManager.getDesiredMinimumHeight()} instead. 785 */ 786 @Deprecated 787 public abstract int getWallpaperDesiredMinimumHeight(); 788 789 /** 790 * @deprecated Use {@link android.app.WallpaperManager#setBitmap(Bitmap) 791 * WallpaperManager.set()} instead. 792 */ 793 @Deprecated 794 public abstract void setWallpaper(Bitmap bitmap) throws IOException; 795 796 /** 797 * @deprecated Use {@link android.app.WallpaperManager#setStream(InputStream) 798 * WallpaperManager.set()} instead. 799 */ 800 @Deprecated 801 public abstract void setWallpaper(InputStream data) throws IOException; 802 803 /** 804 * @deprecated Use {@link android.app.WallpaperManager#clear 805 * WallpaperManager.clear()} instead. 806 */ 807 @Deprecated 808 public abstract void clearWallpaper() throws IOException; 809 810 /** 811 * Launch a new activity. You will not receive any information about when 812 * the activity exits. 813 * 814 * <p>Note that if this method is being called from outside of an 815 * {@link android.app.Activity} Context, then the Intent must include 816 * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because, 817 * without being started from an existing Activity, there is no existing 818 * task in which to place the new activity and thus it needs to be placed 819 * in its own separate task. 820 * 821 * <p>This method throws {@link ActivityNotFoundException} 822 * if there was no Activity found to run the given Intent. 823 * 824 * @param intent The description of the activity to start. 825 * 826 * @throws ActivityNotFoundException 827 * 828 * @see PackageManager#resolveActivity 829 */ 830 public abstract void startActivity(Intent intent); 831 832 /** 833 * Launch multiple new activities. This is generally the same as calling 834 * {@link #startActivity(Intent)} for the first Intent in the array, 835 * that activity during its creation calling {@link #startActivity(Intent)} 836 * for the second entry, etc. Note that unlike that approach, generally 837 * none of the activities except the last in the array will be created 838 * at this point, but rather will be created when the user first visits 839 * them (due to pressing back from the activity on top). 840 * 841 * <p>This method throws {@link ActivityNotFoundException} 842 * if there was no Activity found for <em>any</em> given Intent. In this 843 * case the state of the activity stack is undefined (some Intents in the 844 * list may be on it, some not), so you probably want to avoid such situations. 845 * 846 * @param intents An array of Intents to be started. 847 * 848 * @throws ActivityNotFoundException 849 * 850 * @see PackageManager#resolveActivity 851 */ 852 public abstract void startActivities(Intent[] intents); 853 854 /** 855 * Like {@link #startActivity(Intent)}, but taking a IntentSender 856 * to start. If the IntentSender is for an activity, that activity will be started 857 * as if you had called the regular {@link #startActivity(Intent)} 858 * here; otherwise, its associated action will be executed (such as 859 * sending a broadcast) as if you had called 860 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it. 861 * 862 * @param intent The IntentSender to launch. 863 * @param fillInIntent If non-null, this will be provided as the 864 * intent parameter to {@link IntentSender#sendIntent}. 865 * @param flagsMask Intent flags in the original IntentSender that you 866 * would like to change. 867 * @param flagsValues Desired values for any bits set in 868 * <var>flagsMask</var> 869 * @param extraFlags Always set to 0. 870 */ 871 public abstract void startIntentSender(IntentSender intent, 872 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 873 throws IntentSender.SendIntentException; 874 875 /** 876 * Broadcast the given intent to all interested BroadcastReceivers. This 877 * call is asynchronous; it returns immediately, and you will continue 878 * executing while the receivers are run. No results are propagated from 879 * receivers and receivers can not abort the broadcast. If you want 880 * to allow receivers to propagate results or abort the broadcast, you must 881 * send an ordered broadcast using 882 * {@link #sendOrderedBroadcast(Intent, String)}. 883 * 884 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 885 * 886 * @param intent The Intent to broadcast; all receivers matching this 887 * Intent will receive the broadcast. 888 * 889 * @see android.content.BroadcastReceiver 890 * @see #registerReceiver 891 * @see #sendBroadcast(Intent, String) 892 * @see #sendOrderedBroadcast(Intent, String) 893 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 894 */ 895 public abstract void sendBroadcast(Intent intent); 896 897 /** 898 * Broadcast the given intent to all interested BroadcastReceivers, allowing 899 * an optional required permission to be enforced. This 900 * call is asynchronous; it returns immediately, and you will continue 901 * executing while the receivers are run. No results are propagated from 902 * receivers and receivers can not abort the broadcast. If you want 903 * to allow receivers to propagate results or abort the broadcast, you must 904 * send an ordered broadcast using 905 * {@link #sendOrderedBroadcast(Intent, String)}. 906 * 907 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 908 * 909 * @param intent The Intent to broadcast; all receivers matching this 910 * Intent will receive the broadcast. 911 * @param receiverPermission (optional) String naming a permission that 912 * a receiver must hold in order to receive your broadcast. 913 * If null, no permission is required. 914 * 915 * @see android.content.BroadcastReceiver 916 * @see #registerReceiver 917 * @see #sendBroadcast(Intent) 918 * @see #sendOrderedBroadcast(Intent, String) 919 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 920 */ 921 public abstract void sendBroadcast(Intent intent, 922 String receiverPermission); 923 924 /** 925 * Broadcast the given intent to all interested BroadcastReceivers, delivering 926 * them one at a time to allow more preferred receivers to consume the 927 * broadcast before it is delivered to less preferred receivers. This 928 * call is asynchronous; it returns immediately, and you will continue 929 * executing while the receivers are run. 930 * 931 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 932 * 933 * @param intent The Intent to broadcast; all receivers matching this 934 * Intent will receive the broadcast. 935 * @param receiverPermission (optional) String naming a permissions that 936 * a receiver must hold in order to receive your broadcast. 937 * If null, no permission is required. 938 * 939 * @see android.content.BroadcastReceiver 940 * @see #registerReceiver 941 * @see #sendBroadcast(Intent) 942 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 943 */ 944 public abstract void sendOrderedBroadcast(Intent intent, 945 String receiverPermission); 946 947 /** 948 * Version of {@link #sendBroadcast(Intent)} that allows you to 949 * receive data back from the broadcast. This is accomplished by 950 * supplying your own BroadcastReceiver when calling, which will be 951 * treated as a final receiver at the end of the broadcast -- its 952 * {@link BroadcastReceiver#onReceive} method will be called with 953 * the result values collected from the other receivers. The broadcast will 954 * be serialized in the same way as calling 955 * {@link #sendOrderedBroadcast(Intent, String)}. 956 * 957 * <p>Like {@link #sendBroadcast(Intent)}, this method is 958 * asynchronous; it will return before 959 * resultReceiver.onReceive() is called. 960 * 961 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 962 * 963 * @param intent The Intent to broadcast; all receivers matching this 964 * Intent will receive the broadcast. 965 * @param receiverPermission String naming a permissions that 966 * a receiver must hold in order to receive your broadcast. 967 * If null, no permission is required. 968 * @param resultReceiver Your own BroadcastReceiver to treat as the final 969 * receiver of the broadcast. 970 * @param scheduler A custom Handler with which to schedule the 971 * resultReceiver callback; if null it will be 972 * scheduled in the Context's main thread. 973 * @param initialCode An initial value for the result code. Often 974 * Activity.RESULT_OK. 975 * @param initialData An initial value for the result data. Often 976 * null. 977 * @param initialExtras An initial value for the result extras. Often 978 * null. 979 * 980 * @see #sendBroadcast(Intent) 981 * @see #sendBroadcast(Intent, String) 982 * @see #sendOrderedBroadcast(Intent, String) 983 * @see #sendStickyBroadcast(Intent) 984 * @see #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle) 985 * @see android.content.BroadcastReceiver 986 * @see #registerReceiver 987 * @see android.app.Activity#RESULT_OK 988 */ 989 public abstract void sendOrderedBroadcast(Intent intent, 990 String receiverPermission, BroadcastReceiver resultReceiver, 991 Handler scheduler, int initialCode, String initialData, 992 Bundle initialExtras); 993 994 /** 995 * Perform a {@link #sendBroadcast(Intent)} that is "sticky," meaning the 996 * Intent you are sending stays around after the broadcast is complete, 997 * so that others can quickly retrieve that data through the return 998 * value of {@link #registerReceiver(BroadcastReceiver, IntentFilter)}. In 999 * all other ways, this behaves the same as 1000 * {@link #sendBroadcast(Intent)}. 1001 * 1002 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 1003 * permission in order to use this API. If you do not hold that 1004 * permission, {@link SecurityException} will be thrown. 1005 * 1006 * @param intent The Intent to broadcast; all receivers matching this 1007 * Intent will receive the broadcast, and the Intent will be held to 1008 * be re-broadcast to future receivers. 1009 * 1010 * @see #sendBroadcast(Intent) 1011 * @see #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle) 1012 */ 1013 public abstract void sendStickyBroadcast(Intent intent); 1014 1015 /** 1016 * Version of {@link #sendStickyBroadcast} that allows you to 1017 * receive data back from the broadcast. This is accomplished by 1018 * supplying your own BroadcastReceiver when calling, which will be 1019 * treated as a final receiver at the end of the broadcast -- its 1020 * {@link BroadcastReceiver#onReceive} method will be called with 1021 * the result values collected from the other receivers. The broadcast will 1022 * be serialized in the same way as calling 1023 * {@link #sendOrderedBroadcast(Intent, String)}. 1024 * 1025 * <p>Like {@link #sendBroadcast(Intent)}, this method is 1026 * asynchronous; it will return before 1027 * resultReceiver.onReceive() is called. Note that the sticky data 1028 * stored is only the data you initially supply to the broadcast, not 1029 * the result of any changes made by the receivers. 1030 * 1031 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1032 * 1033 * @param intent The Intent to broadcast; all receivers matching this 1034 * Intent will receive the broadcast. 1035 * @param resultReceiver Your own BroadcastReceiver to treat as the final 1036 * receiver of the broadcast. 1037 * @param scheduler A custom Handler with which to schedule the 1038 * resultReceiver callback; if null it will be 1039 * scheduled in the Context's main thread. 1040 * @param initialCode An initial value for the result code. Often 1041 * Activity.RESULT_OK. 1042 * @param initialData An initial value for the result data. Often 1043 * null. 1044 * @param initialExtras An initial value for the result extras. Often 1045 * null. 1046 * 1047 * @see #sendBroadcast(Intent) 1048 * @see #sendBroadcast(Intent, String) 1049 * @see #sendOrderedBroadcast(Intent, String) 1050 * @see #sendStickyBroadcast(Intent) 1051 * @see android.content.BroadcastReceiver 1052 * @see #registerReceiver 1053 * @see android.app.Activity#RESULT_OK 1054 */ 1055 public abstract void sendStickyOrderedBroadcast(Intent intent, 1056 BroadcastReceiver resultReceiver, 1057 Handler scheduler, int initialCode, String initialData, 1058 Bundle initialExtras); 1059 1060 1061 /** 1062 * Remove the data previously sent with {@link #sendStickyBroadcast}, 1063 * so that it is as if the sticky broadcast had never happened. 1064 * 1065 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 1066 * permission in order to use this API. If you do not hold that 1067 * permission, {@link SecurityException} will be thrown. 1068 * 1069 * @param intent The Intent that was previously broadcast. 1070 * 1071 * @see #sendStickyBroadcast 1072 */ 1073 public abstract void removeStickyBroadcast(Intent intent); 1074 1075 /** 1076 * Register a BroadcastReceiver to be run in the main activity thread. The 1077 * <var>receiver</var> will be called with any broadcast Intent that 1078 * matches <var>filter</var>, in the main application thread. 1079 * 1080 * <p>The system may broadcast Intents that are "sticky" -- these stay 1081 * around after the broadcast as finished, to be sent to any later 1082 * registrations. If your IntentFilter matches one of these sticky 1083 * Intents, that Intent will be returned by this function 1084 * <strong>and</strong> sent to your <var>receiver</var> as if it had just 1085 * been broadcast. 1086 * 1087 * <p>There may be multiple sticky Intents that match <var>filter</var>, 1088 * in which case each of these will be sent to <var>receiver</var>. In 1089 * this case, only one of these can be returned directly by the function; 1090 * which of these that is returned is arbitrarily decided by the system. 1091 * 1092 * <p>If you know the Intent your are registering for is sticky, you can 1093 * supply null for your <var>receiver</var>. In this case, no receiver is 1094 * registered -- the function simply returns the sticky Intent that 1095 * matches <var>filter</var>. In the case of multiple matches, the same 1096 * rules as described above apply. 1097 * 1098 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1099 * 1100 * <p>As of {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, receivers 1101 * registered with this method will correctly respect the 1102 * {@link Intent#setPackage(String)} specified for an Intent being broadcast. 1103 * Prior to that, it would be ignored and delivered to all matching registered 1104 * receivers. Be careful if using this for security.</p> 1105 * 1106 * <p class="note">Note: this method <em>cannot be called from a 1107 * {@link BroadcastReceiver} component;</em> that is, from a BroadcastReceiver 1108 * that is declared in an application's manifest. It is okay, however, to call 1109 * this method from another BroadcastReceiver that has itself been registered 1110 * at run time with {@link #registerReceiver}, since the lifetime of such a 1111 * registered BroadcastReceiver is tied to the object that registered it.</p> 1112 * 1113 * @param receiver The BroadcastReceiver to handle the broadcast. 1114 * @param filter Selects the Intent broadcasts to be received. 1115 * 1116 * @return The first sticky intent found that matches <var>filter</var>, 1117 * or null if there are none. 1118 * 1119 * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 1120 * @see #sendBroadcast 1121 * @see #unregisterReceiver 1122 */ 1123 public abstract Intent registerReceiver(BroadcastReceiver receiver, 1124 IntentFilter filter); 1125 1126 /** 1127 * Register to receive intent broadcasts, to run in the context of 1128 * <var>scheduler</var>. See 1129 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)} for more 1130 * information. This allows you to enforce permissions on who can 1131 * broadcast intents to your receiver, or have the receiver run in 1132 * a different thread than the main application thread. 1133 * 1134 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1135 * 1136 * <p>As of {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, receivers 1137 * registered with this method will correctly respect the 1138 * {@link Intent#setPackage(String)} specified for an Intent being broadcast. 1139 * Prior to that, it would be ignored and delivered to all matching registered 1140 * receivers. Be careful if using this for security.</p> 1141 * 1142 * @param receiver The BroadcastReceiver to handle the broadcast. 1143 * @param filter Selects the Intent broadcasts to be received. 1144 * @param broadcastPermission String naming a permissions that a 1145 * broadcaster must hold in order to send an Intent to you. If null, 1146 * no permission is required. 1147 * @param scheduler Handler identifying the thread that will receive 1148 * the Intent. If null, the main thread of the process will be used. 1149 * 1150 * @return The first sticky intent found that matches <var>filter</var>, 1151 * or null if there are none. 1152 * 1153 * @see #registerReceiver(BroadcastReceiver, IntentFilter) 1154 * @see #sendBroadcast 1155 * @see #unregisterReceiver 1156 */ 1157 public abstract Intent registerReceiver(BroadcastReceiver receiver, 1158 IntentFilter filter, 1159 String broadcastPermission, 1160 Handler scheduler); 1161 1162 /** 1163 * Unregister a previously registered BroadcastReceiver. <em>All</em> 1164 * filters that have been registered for this BroadcastReceiver will be 1165 * removed. 1166 * 1167 * @param receiver The BroadcastReceiver to unregister. 1168 * 1169 * @see #registerReceiver 1170 */ 1171 public abstract void unregisterReceiver(BroadcastReceiver receiver); 1172 1173 /** 1174 * Request that a given application service be started. The Intent 1175 * can either contain the complete class name of a specific service 1176 * implementation to start, or an abstract definition through the 1177 * action and other fields of the kind of service to start. If this service 1178 * is not already running, it will be instantiated and started (creating a 1179 * process for it if needed); if it is running then it remains running. 1180 * 1181 * <p>Every call to this method will result in a corresponding call to 1182 * the target service's {@link android.app.Service#onStartCommand} method, 1183 * with the <var>intent</var> given here. This provides a convenient way 1184 * to submit jobs to a service without having to bind and call on to its 1185 * interface. 1186 * 1187 * <p>Using startService() overrides the default service lifetime that is 1188 * managed by {@link #bindService}: it requires the service to remain 1189 * running until {@link #stopService} is called, regardless of whether 1190 * any clients are connected to it. Note that calls to startService() 1191 * are not nesting: no matter how many times you call startService(), 1192 * a single call to {@link #stopService} will stop it. 1193 * 1194 * <p>The system attempts to keep running services around as much as 1195 * possible. The only time they should be stopped is if the current 1196 * foreground application is using so many resources that the service needs 1197 * to be killed. If any errors happen in the service's process, it will 1198 * automatically be restarted. 1199 * 1200 * <p>This function will throw {@link SecurityException} if you do not 1201 * have permission to start the given service. 1202 * 1203 * @param service Identifies the service to be started. The Intent may 1204 * specify either an explicit component name to start, or a logical 1205 * description (action, category, etc) to match an 1206 * {@link IntentFilter} published by a service. Additional values 1207 * may be included in the Intent extras to supply arguments along with 1208 * this specific start call. 1209 * 1210 * @return If the service is being started or is already running, the 1211 * {@link ComponentName} of the actual service that was started is 1212 * returned; else if the service does not exist null is returned. 1213 * 1214 * @throws SecurityException 1215 * 1216 * @see #stopService 1217 * @see #bindService 1218 */ 1219 public abstract ComponentName startService(Intent service); 1220 1221 /** 1222 * Request that a given application service be stopped. If the service is 1223 * not running, nothing happens. Otherwise it is stopped. Note that calls 1224 * to startService() are not counted -- this stops the service no matter 1225 * how many times it was started. 1226 * 1227 * <p>Note that if a stopped service still has {@link ServiceConnection} 1228 * objects bound to it with the {@link #BIND_AUTO_CREATE} set, it will 1229 * not be destroyed until all of these bindings are removed. See 1230 * the {@link android.app.Service} documentation for more details on a 1231 * service's lifecycle. 1232 * 1233 * <p>This function will throw {@link SecurityException} if you do not 1234 * have permission to stop the given service. 1235 * 1236 * @param service Description of the service to be stopped. The Intent may 1237 * specify either an explicit component name to start, or a logical 1238 * description (action, category, etc) to match an 1239 * {@link IntentFilter} published by a service. 1240 * 1241 * @return If there is a service matching the given Intent that is already 1242 * running, then it is stopped and true is returned; else false is returned. 1243 * 1244 * @throws SecurityException 1245 * 1246 * @see #startService 1247 */ 1248 public abstract boolean stopService(Intent service); 1249 1250 /** 1251 * Connect to an application service, creating it if needed. This defines 1252 * a dependency between your application and the service. The given 1253 * <var>conn</var> will receive the service object when its created and be 1254 * told if it dies and restarts. The service will be considered required 1255 * by the system only for as long as the calling context exists. For 1256 * example, if this Context is an Activity that is stopped, the service will 1257 * not be required to continue running until the Activity is resumed. 1258 * 1259 * <p>This function will throw {@link SecurityException} if you do not 1260 * have permission to bind to the given service. 1261 * 1262 * <p class="note">Note: this method <em>can not be called from an 1263 * {@link BroadcastReceiver} component</em>. A pattern you can use to 1264 * communicate from an BroadcastReceiver to a Service is to call 1265 * {@link #startService} with the arguments containing the command to be 1266 * sent, with the service calling its 1267 * {@link android.app.Service#stopSelf(int)} method when done executing 1268 * that command. See the API demo App/Service/Service Start Arguments 1269 * Controller for an illustration of this. It is okay, however, to use 1270 * this method from an BroadcastReceiver that has been registered with 1271 * {@link #registerReceiver}, since the lifetime of this BroadcastReceiver 1272 * is tied to another object (the one that registered it).</p> 1273 * 1274 * @param service Identifies the service to connect to. The Intent may 1275 * specify either an explicit component name, or a logical 1276 * description (action, category, etc) to match an 1277 * {@link IntentFilter} published by a service. 1278 * @param conn Receives information as the service is started and stopped. 1279 * @param flags Operation options for the binding. May be 0, 1280 * {@link #BIND_AUTO_CREATE}, {@link #BIND_DEBUG_UNBIND}, 1281 * {@link #BIND_NOT_FOREGROUND}, {@link #BIND_ABOVE_CLIENT}, 1282 * {@link #BIND_ALLOW_OOM_MANAGEMENT}, or 1283 * {@link #BIND_WAIVE_PRIORITY}. 1284 * @return If you have successfully bound to the service, true is returned; 1285 * false is returned if the connection is not made so you will not 1286 * receive the service object. 1287 * 1288 * @throws SecurityException 1289 * 1290 * @see #unbindService 1291 * @see #startService 1292 * @see #BIND_AUTO_CREATE 1293 * @see #BIND_DEBUG_UNBIND 1294 * @see #BIND_NOT_FOREGROUND 1295 */ 1296 public abstract boolean bindService(Intent service, ServiceConnection conn, 1297 int flags); 1298 1299 /** 1300 * Disconnect from an application service. You will no longer receive 1301 * calls as the service is restarted, and the service is now allowed to 1302 * stop at any time. 1303 * 1304 * @param conn The connection interface previously supplied to 1305 * bindService(). 1306 * 1307 * @see #bindService 1308 */ 1309 public abstract void unbindService(ServiceConnection conn); 1310 1311 /** 1312 * Start executing an {@link android.app.Instrumentation} class. The given 1313 * Instrumentation component will be run by killing its target application 1314 * (if currently running), starting the target process, instantiating the 1315 * instrumentation component, and then letting it drive the application. 1316 * 1317 * <p>This function is not synchronous -- it returns as soon as the 1318 * instrumentation has started and while it is running. 1319 * 1320 * <p>Instrumentation is normally only allowed to run against a package 1321 * that is either unsigned or signed with a signature that the 1322 * the instrumentation package is also signed with (ensuring the target 1323 * trusts the instrumentation). 1324 * 1325 * @param className Name of the Instrumentation component to be run. 1326 * @param profileFile Optional path to write profiling data as the 1327 * instrumentation runs, or null for no profiling. 1328 * @param arguments Additional optional arguments to pass to the 1329 * instrumentation, or null. 1330 * 1331 * @return Returns true if the instrumentation was successfully started, 1332 * else false if it could not be found. 1333 */ 1334 public abstract boolean startInstrumentation(ComponentName className, 1335 String profileFile, Bundle arguments); 1336 1337 /** 1338 * Return the handle to a system-level service by name. The class of the 1339 * returned object varies by the requested name. Currently available names 1340 * are: 1341 * 1342 * <dl> 1343 * <dt> {@link #WINDOW_SERVICE} ("window") 1344 * <dd> The top-level window manager in which you can place custom 1345 * windows. The returned object is a {@link android.view.WindowManager}. 1346 * <dt> {@link #LAYOUT_INFLATER_SERVICE} ("layout_inflater") 1347 * <dd> A {@link android.view.LayoutInflater} for inflating layout resources 1348 * in this context. 1349 * <dt> {@link #ACTIVITY_SERVICE} ("activity") 1350 * <dd> A {@link android.app.ActivityManager} for interacting with the 1351 * global activity state of the system. 1352 * <dt> {@link #POWER_SERVICE} ("power") 1353 * <dd> A {@link android.os.PowerManager} for controlling power 1354 * management. 1355 * <dt> {@link #ALARM_SERVICE} ("alarm") 1356 * <dd> A {@link android.app.AlarmManager} for receiving intents at the 1357 * time of your choosing. 1358 * <dt> {@link #NOTIFICATION_SERVICE} ("notification") 1359 * <dd> A {@link android.app.NotificationManager} for informing the user 1360 * of background events. 1361 * <dt> {@link #KEYGUARD_SERVICE} ("keyguard") 1362 * <dd> A {@link android.app.KeyguardManager} for controlling keyguard. 1363 * <dt> {@link #LOCATION_SERVICE} ("location") 1364 * <dd> A {@link android.location.LocationManager} for controlling location 1365 * (e.g., GPS) updates. 1366 * <dt> {@link #SEARCH_SERVICE} ("search") 1367 * <dd> A {@link android.app.SearchManager} for handling search. 1368 * <dt> {@link #VIBRATOR_SERVICE} ("vibrator") 1369 * <dd> A {@link android.os.Vibrator} for interacting with the vibrator 1370 * hardware. 1371 * <dt> {@link #CONNECTIVITY_SERVICE} ("connection") 1372 * <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for 1373 * handling management of network connections. 1374 * <dt> {@link #WIFI_SERVICE} ("wifi") 1375 * <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of 1376 * Wi-Fi connectivity. 1377 * <dt> {@link #INPUT_METHOD_SERVICE} ("input_method") 1378 * <dd> An {@link android.view.inputmethod.InputMethodManager InputMethodManager} 1379 * for management of input methods. 1380 * <dt> {@link #UI_MODE_SERVICE} ("uimode") 1381 * <dd> An {@link android.app.UiModeManager} for controlling UI modes. 1382 * <dt> {@link #DOWNLOAD_SERVICE} ("download") 1383 * <dd> A {@link android.app.DownloadManager} for requesting HTTP downloads 1384 * </dl> 1385 * 1386 * <p>Note: System services obtained via this API may be closely associated with 1387 * the Context in which they are obtained from. In general, do not share the 1388 * service objects between various different contexts (Activities, Applications, 1389 * Services, Providers, etc.) 1390 * 1391 * @param name The name of the desired service. 1392 * 1393 * @return The service or null if the name does not exist. 1394 * 1395 * @see #WINDOW_SERVICE 1396 * @see android.view.WindowManager 1397 * @see #LAYOUT_INFLATER_SERVICE 1398 * @see android.view.LayoutInflater 1399 * @see #ACTIVITY_SERVICE 1400 * @see android.app.ActivityManager 1401 * @see #POWER_SERVICE 1402 * @see android.os.PowerManager 1403 * @see #ALARM_SERVICE 1404 * @see android.app.AlarmManager 1405 * @see #NOTIFICATION_SERVICE 1406 * @see android.app.NotificationManager 1407 * @see #KEYGUARD_SERVICE 1408 * @see android.app.KeyguardManager 1409 * @see #LOCATION_SERVICE 1410 * @see android.location.LocationManager 1411 * @see #SEARCH_SERVICE 1412 * @see android.app.SearchManager 1413 * @see #SENSOR_SERVICE 1414 * @see android.hardware.SensorManager 1415 * @see #STORAGE_SERVICE 1416 * @see android.os.storage.StorageManager 1417 * @see #VIBRATOR_SERVICE 1418 * @see android.os.Vibrator 1419 * @see #CONNECTIVITY_SERVICE 1420 * @see android.net.ConnectivityManager 1421 * @see #WIFI_SERVICE 1422 * @see android.net.wifi.WifiManager 1423 * @see #AUDIO_SERVICE 1424 * @see android.media.AudioManager 1425 * @see #TELEPHONY_SERVICE 1426 * @see android.telephony.TelephonyManager 1427 * @see #INPUT_METHOD_SERVICE 1428 * @see android.view.inputmethod.InputMethodManager 1429 * @see #UI_MODE_SERVICE 1430 * @see android.app.UiModeManager 1431 * @see #DOWNLOAD_SERVICE 1432 * @see android.app.DownloadManager 1433 */ 1434 public abstract Object getSystemService(String name); 1435 1436 /** 1437 * Use with {@link #getSystemService} to retrieve a 1438 * {@link android.os.PowerManager} for controlling power management, 1439 * including "wake locks," which let you keep the device on while 1440 * you're running long tasks. 1441 */ 1442 public static final String POWER_SERVICE = "power"; 1443 1444 /** 1445 * Use with {@link #getSystemService} to retrieve a 1446 * {@link android.view.WindowManager} for accessing the system's window 1447 * manager. 1448 * 1449 * @see #getSystemService 1450 * @see android.view.WindowManager 1451 */ 1452 public static final String WINDOW_SERVICE = "window"; 1453 1454 /** 1455 * Use with {@link #getSystemService} to retrieve a 1456 * {@link android.view.LayoutInflater} for inflating layout resources in this 1457 * context. 1458 * 1459 * @see #getSystemService 1460 * @see android.view.LayoutInflater 1461 */ 1462 public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater"; 1463 1464 /** 1465 * Use with {@link #getSystemService} to retrieve a 1466 * {@link android.accounts.AccountManager} for receiving intents at a 1467 * time of your choosing. 1468 * 1469 * @see #getSystemService 1470 * @see android.accounts.AccountManager 1471 */ 1472 public static final String ACCOUNT_SERVICE = "account"; 1473 1474 /** 1475 * Use with {@link #getSystemService} to retrieve a 1476 * {@link android.app.ActivityManager} for interacting with the global 1477 * system state. 1478 * 1479 * @see #getSystemService 1480 * @see android.app.ActivityManager 1481 */ 1482 public static final String ACTIVITY_SERVICE = "activity"; 1483 1484 /** 1485 * Use with {@link #getSystemService} to retrieve a 1486 * {@link android.app.AlarmManager} for receiving intents at a 1487 * time of your choosing. 1488 * 1489 * @see #getSystemService 1490 * @see android.app.AlarmManager 1491 */ 1492 public static final String ALARM_SERVICE = "alarm"; 1493 1494 /** 1495 * Use with {@link #getSystemService} to retrieve a 1496 * {@link android.app.NotificationManager} for informing the user of 1497 * background events. 1498 * 1499 * @see #getSystemService 1500 * @see android.app.NotificationManager 1501 */ 1502 public static final String NOTIFICATION_SERVICE = "notification"; 1503 1504 /** 1505 * Use with {@link #getSystemService} to retrieve a 1506 * {@link android.view.accessibility.AccessibilityManager} for giving the user 1507 * feedback for UI events through the registered event listeners. 1508 * 1509 * @see #getSystemService 1510 * @see android.view.accessibility.AccessibilityManager 1511 */ 1512 public static final String ACCESSIBILITY_SERVICE = "accessibility"; 1513 1514 /** 1515 * Use with {@link #getSystemService} to retrieve a 1516 * {@link android.app.NotificationManager} for controlling keyguard. 1517 * 1518 * @see #getSystemService 1519 * @see android.app.KeyguardManager 1520 */ 1521 public static final String KEYGUARD_SERVICE = "keyguard"; 1522 1523 /** 1524 * Use with {@link #getSystemService} to retrieve a {@link 1525 * android.location.LocationManager} for controlling location 1526 * updates. 1527 * 1528 * @see #getSystemService 1529 * @see android.location.LocationManager 1530 */ 1531 public static final String LOCATION_SERVICE = "location"; 1532 1533 /** 1534 * Use with {@link #getSystemService} to retrieve a 1535 * {@link android.location.CountryDetector} for detecting the country that 1536 * the user is in. 1537 * 1538 * @hide 1539 */ 1540 public static final String COUNTRY_DETECTOR = "country_detector"; 1541 1542 /** 1543 * Use with {@link #getSystemService} to retrieve a {@link 1544 * android.app.SearchManager} for handling searches. 1545 * 1546 * @see #getSystemService 1547 * @see android.app.SearchManager 1548 */ 1549 public static final String SEARCH_SERVICE = "search"; 1550 1551 /** 1552 * Use with {@link #getSystemService} to retrieve a {@link 1553 * android.hardware.SensorManager} for accessing sensors. 1554 * 1555 * @see #getSystemService 1556 * @see android.hardware.SensorManager 1557 */ 1558 public static final String SENSOR_SERVICE = "sensor"; 1559 1560 /** 1561 * Use with {@link #getSystemService} to retrieve a {@link 1562 * android.os.storage.StorageManager} for accessing system storage 1563 * functions. 1564 * 1565 * @see #getSystemService 1566 * @see android.os.storage.StorageManager 1567 */ 1568 public static final String STORAGE_SERVICE = "storage"; 1569 1570 /** 1571 * Use with {@link #getSystemService} to retrieve a 1572 * com.android.server.WallpaperService for accessing wallpapers. 1573 * 1574 * @see #getSystemService 1575 */ 1576 public static final String WALLPAPER_SERVICE = "wallpaper"; 1577 1578 /** 1579 * Use with {@link #getSystemService} to retrieve a {@link 1580 * android.os.Vibrator} for interacting with the vibration hardware. 1581 * 1582 * @see #getSystemService 1583 * @see android.os.Vibrator 1584 */ 1585 public static final String VIBRATOR_SERVICE = "vibrator"; 1586 1587 /** 1588 * Use with {@link #getSystemService} to retrieve a {@link 1589 * android.app.StatusBarManager} for interacting with the status bar. 1590 * 1591 * @see #getSystemService 1592 * @see android.app.StatusBarManager 1593 * @hide 1594 */ 1595 public static final String STATUS_BAR_SERVICE = "statusbar"; 1596 1597 /** 1598 * Use with {@link #getSystemService} to retrieve a {@link 1599 * android.net.ConnectivityManager} for handling management of 1600 * network connections. 1601 * 1602 * @see #getSystemService 1603 * @see android.net.ConnectivityManager 1604 */ 1605 public static final String CONNECTIVITY_SERVICE = "connectivity"; 1606 1607 /** 1608 * Use with {@link #getSystemService} to retrieve a {@link 1609 * android.net.ThrottleManager} for handling management of 1610 * throttling. 1611 * 1612 * @hide 1613 * @see #getSystemService 1614 * @see android.net.ThrottleManager 1615 */ 1616 public static final String THROTTLE_SERVICE = "throttle"; 1617 1618 /** 1619 * Use with {@link #getSystemService} to retrieve a {@link 1620 * android.net.NetworkManagementService} for handling management of 1621 * system network services 1622 * 1623 * @hide 1624 * @see #getSystemService 1625 * @see android.net.NetworkManagementService 1626 */ 1627 public static final String NETWORKMANAGEMENT_SERVICE = "network_management"; 1628 1629 /** {@hide} */ 1630 public static final String NETWORK_STATS_SERVICE = "netstats"; 1631 /** {@hide} */ 1632 public static final String NETWORK_POLICY_SERVICE = "netpolicy"; 1633 1634 /** 1635 * Use with {@link #getSystemService} to retrieve a {@link 1636 * android.net.wifi.WifiManager} for handling management of 1637 * Wi-Fi access. 1638 * 1639 * @see #getSystemService 1640 * @see android.net.wifi.WifiManager 1641 */ 1642 public static final String WIFI_SERVICE = "wifi"; 1643 1644 /** 1645 * Use with {@link #getSystemService} to retrieve a {@link 1646 * android.net.wifi.p2p.WifiP2pManager} for handling management of 1647 * Wi-Fi p2p. 1648 * 1649 * @see #getSystemService 1650 * @see android.net.wifi.p2p.WifiP2pManager 1651 * @hide 1652 */ 1653 public static final String WIFI_P2P_SERVICE = "wifip2p"; 1654 1655 /** 1656 * Use with {@link #getSystemService} to retrieve a 1657 * {@link android.media.AudioManager} for handling management of volume, 1658 * ringer modes and audio routing. 1659 * 1660 * @see #getSystemService 1661 * @see android.media.AudioManager 1662 */ 1663 public static final String AUDIO_SERVICE = "audio"; 1664 1665 /** 1666 * Use with {@link #getSystemService} to retrieve a 1667 * {@link android.telephony.TelephonyManager} for handling management the 1668 * telephony features of the device. 1669 * 1670 * @see #getSystemService 1671 * @see android.telephony.TelephonyManager 1672 */ 1673 public static final String TELEPHONY_SERVICE = "phone"; 1674 1675 /** 1676 * Use with {@link #getSystemService} to retrieve a 1677 * {@link android.text.ClipboardManager} for accessing and modifying 1678 * the contents of the global clipboard. 1679 * 1680 * @see #getSystemService 1681 * @see android.text.ClipboardManager 1682 */ 1683 public static final String CLIPBOARD_SERVICE = "clipboard"; 1684 1685 /** 1686 * Use with {@link #getSystemService} to retrieve a 1687 * {@link android.view.inputmethod.InputMethodManager} for accessing input 1688 * methods. 1689 * 1690 * @see #getSystemService 1691 */ 1692 public static final String INPUT_METHOD_SERVICE = "input_method"; 1693 1694 /** 1695 * Use with {@link #getSystemService} to retrieve a 1696 * {@link android.view.textservice.TextServicesManager} for accessing 1697 * text services. 1698 * 1699 * @see #getSystemService 1700 */ 1701 public static final String TEXT_SERVICES_MANAGER_SERVICE = "textservices"; 1702 1703 /** 1704 * Use with {@link #getSystemService} to retrieve a 1705 * {@link android.appwidget.AppWidgetManager} for accessing AppWidgets. 1706 * 1707 * @hide 1708 * @see #getSystemService 1709 */ 1710 public static final String APPWIDGET_SERVICE = "appwidget"; 1711 1712 /** 1713 * Use with {@link #getSystemService} to retrieve an 1714 * {@link android.app.backup.IBackupManager IBackupManager} for communicating 1715 * with the backup mechanism. 1716 * @hide 1717 * 1718 * @see #getSystemService 1719 */ 1720 public static final String BACKUP_SERVICE = "backup"; 1721 1722 /** 1723 * Use with {@link #getSystemService} to retrieve a 1724 * {@link android.os.DropBoxManager} instance for recording 1725 * diagnostic logs. 1726 * @see #getSystemService 1727 */ 1728 public static final String DROPBOX_SERVICE = "dropbox"; 1729 1730 /** 1731 * Use with {@link #getSystemService} to retrieve a 1732 * {@link android.app.admin.DevicePolicyManager} for working with global 1733 * device policy management. 1734 * 1735 * @see #getSystemService 1736 */ 1737 public static final String DEVICE_POLICY_SERVICE = "device_policy"; 1738 1739 /** 1740 * Use with {@link #getSystemService} to retrieve a 1741 * {@link android.app.UiModeManager} for controlling UI modes. 1742 * 1743 * @see #getSystemService 1744 */ 1745 public static final String UI_MODE_SERVICE = "uimode"; 1746 1747 /** 1748 * Use with {@link #getSystemService} to retrieve a 1749 * {@link android.app.DownloadManager} for requesting HTTP downloads. 1750 * 1751 * @see #getSystemService 1752 */ 1753 public static final String DOWNLOAD_SERVICE = "download"; 1754 1755 /** 1756 * Use with {@link #getSystemService} to retrieve a 1757 * {@link android.nfc.NfcManager} for using NFC. 1758 * 1759 * @see #getSystemService 1760 */ 1761 public static final String NFC_SERVICE = "nfc"; 1762 1763 /** 1764 * Use with {@link #getSystemService} to retrieve a 1765 * {@link android.net.sip.SipManager} for accessing the SIP related service. 1766 * 1767 * @see #getSystemService 1768 */ 1769 /** @hide */ 1770 public static final String SIP_SERVICE = "sip"; 1771 1772 /** 1773 * Use with {@link #getSystemService} to retrieve a {@link 1774 * android.hardware.usb.UsbManager} for access to USB devices (as a USB host) 1775 * and for controlling this device's behavior as a USB device. 1776 * 1777 * @see #getSystemService 1778 * @see android.harware.usb.UsbManager 1779 */ 1780 public static final String USB_SERVICE = "usb"; 1781 1782 /** 1783 * Determine whether the given permission is allowed for a particular 1784 * process and user ID running in the system. 1785 * 1786 * @param permission The name of the permission being checked. 1787 * @param pid The process ID being checked against. Must be > 0. 1788 * @param uid The user ID being checked against. A uid of 0 is the root 1789 * user, which will pass every permission check. 1790 * 1791 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the given 1792 * pid/uid is allowed that permission, or 1793 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1794 * 1795 * @see PackageManager#checkPermission(String, String) 1796 * @see #checkCallingPermission 1797 */ 1798 public abstract int checkPermission(String permission, int pid, int uid); 1799 1800 /** 1801 * Determine whether the calling process of an IPC you are handling has been 1802 * granted a particular permission. This is basically the same as calling 1803 * {@link #checkPermission(String, int, int)} with the pid and uid returned 1804 * by {@link android.os.Binder#getCallingPid} and 1805 * {@link android.os.Binder#getCallingUid}. One important difference 1806 * is that if you are not currently processing an IPC, this function 1807 * will always fail. This is done to protect against accidentally 1808 * leaking permissions; you can use {@link #checkCallingOrSelfPermission} 1809 * to avoid this protection. 1810 * 1811 * @param permission The name of the permission being checked. 1812 * 1813 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the calling 1814 * pid/uid is allowed that permission, or 1815 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1816 * 1817 * @see PackageManager#checkPermission(String, String) 1818 * @see #checkPermission 1819 * @see #checkCallingOrSelfPermission 1820 */ 1821 public abstract int checkCallingPermission(String permission); 1822 1823 /** 1824 * Determine whether the calling process of an IPC <em>or you</em> have been 1825 * granted a particular permission. This is the same as 1826 * {@link #checkCallingPermission}, except it grants your own permissions 1827 * if you are not currently processing an IPC. Use with care! 1828 * 1829 * @param permission The name of the permission being checked. 1830 * 1831 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the calling 1832 * pid/uid is allowed that permission, or 1833 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1834 * 1835 * @see PackageManager#checkPermission(String, String) 1836 * @see #checkPermission 1837 * @see #checkCallingPermission 1838 */ 1839 public abstract int checkCallingOrSelfPermission(String permission); 1840 1841 /** 1842 * If the given permission is not allowed for a particular process 1843 * and user ID running in the system, throw a {@link SecurityException}. 1844 * 1845 * @param permission The name of the permission being checked. 1846 * @param pid The process ID being checked against. Must be > 0. 1847 * @param uid The user ID being checked against. A uid of 0 is the root 1848 * user, which will pass every permission check. 1849 * @param message A message to include in the exception if it is thrown. 1850 * 1851 * @see #checkPermission(String, int, int) 1852 */ 1853 public abstract void enforcePermission( 1854 String permission, int pid, int uid, String message); 1855 1856 /** 1857 * If the calling process of an IPC you are handling has not been 1858 * granted a particular permission, throw a {@link 1859 * SecurityException}. This is basically the same as calling 1860 * {@link #enforcePermission(String, int, int, String)} with the 1861 * pid and uid returned by {@link android.os.Binder#getCallingPid} 1862 * and {@link android.os.Binder#getCallingUid}. One important 1863 * difference is that if you are not currently processing an IPC, 1864 * this function will always throw the SecurityException. This is 1865 * done to protect against accidentally leaking permissions; you 1866 * can use {@link #enforceCallingOrSelfPermission} to avoid this 1867 * protection. 1868 * 1869 * @param permission The name of the permission being checked. 1870 * @param message A message to include in the exception if it is thrown. 1871 * 1872 * @see #checkCallingPermission(String) 1873 */ 1874 public abstract void enforceCallingPermission( 1875 String permission, String message); 1876 1877 /** 1878 * If neither you nor the calling process of an IPC you are 1879 * handling has been granted a particular permission, throw a 1880 * {@link SecurityException}. This is the same as {@link 1881 * #enforceCallingPermission}, except it grants your own 1882 * permissions if you are not currently processing an IPC. Use 1883 * with care! 1884 * 1885 * @param permission The name of the permission being checked. 1886 * @param message A message to include in the exception if it is thrown. 1887 * 1888 * @see #checkCallingOrSelfPermission(String) 1889 */ 1890 public abstract void enforceCallingOrSelfPermission( 1891 String permission, String message); 1892 1893 /** 1894 * Grant permission to access a specific Uri to another package, regardless 1895 * of whether that package has general permission to access the Uri's 1896 * content provider. This can be used to grant specific, temporary 1897 * permissions, typically in response to user interaction (such as the 1898 * user opening an attachment that you would like someone else to 1899 * display). 1900 * 1901 * <p>Normally you should use {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1902 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1903 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1904 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} with the Intent being used to 1905 * start an activity instead of this function directly. If you use this 1906 * function directly, you should be sure to call 1907 * {@link #revokeUriPermission} when the target should no longer be allowed 1908 * to access it. 1909 * 1910 * <p>To succeed, the content provider owning the Uri must have set the 1911 * {@link android.R.styleable#AndroidManifestProvider_grantUriPermissions 1912 * grantUriPermissions} attribute in its manifest or included the 1913 * {@link android.R.styleable#AndroidManifestGrantUriPermission 1914 * <grant-uri-permissions>} tag. 1915 * 1916 * @param toPackage The package you would like to allow to access the Uri. 1917 * @param uri The Uri you would like to grant access to. 1918 * @param modeFlags The desired access modes. Any combination of 1919 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1920 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1921 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1922 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1923 * 1924 * @see #revokeUriPermission 1925 */ 1926 public abstract void grantUriPermission(String toPackage, Uri uri, 1927 int modeFlags); 1928 1929 /** 1930 * Remove all permissions to access a particular content provider Uri 1931 * that were previously added with {@link #grantUriPermission}. The given 1932 * Uri will match all previously granted Uris that are the same or a 1933 * sub-path of the given Uri. That is, revoking "content://foo/one" will 1934 * revoke both "content://foo/target" and "content://foo/target/sub", but not 1935 * "content://foo". 1936 * 1937 * @param uri The Uri you would like to revoke access to. 1938 * @param modeFlags The desired access modes. Any combination of 1939 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1940 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1941 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1942 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1943 * 1944 * @see #grantUriPermission 1945 */ 1946 public abstract void revokeUriPermission(Uri uri, int modeFlags); 1947 1948 /** 1949 * Determine whether a particular process and user ID has been granted 1950 * permission to access a specific URI. This only checks for permissions 1951 * that have been explicitly granted -- if the given process/uid has 1952 * more general access to the URI's content provider then this check will 1953 * always fail. 1954 * 1955 * @param uri The uri that is being checked. 1956 * @param pid The process ID being checked against. Must be > 0. 1957 * @param uid The user ID being checked against. A uid of 0 is the root 1958 * user, which will pass every permission check. 1959 * @param modeFlags The type of access to grant. May be one or both of 1960 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1961 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1962 * 1963 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the given 1964 * pid/uid is allowed to access that uri, or 1965 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1966 * 1967 * @see #checkCallingUriPermission 1968 */ 1969 public abstract int checkUriPermission(Uri uri, int pid, int uid, int modeFlags); 1970 1971 /** 1972 * Determine whether the calling process and user ID has been 1973 * granted permission to access a specific URI. This is basically 1974 * the same as calling {@link #checkUriPermission(Uri, int, int, 1975 * int)} with the pid and uid returned by {@link 1976 * android.os.Binder#getCallingPid} and {@link 1977 * android.os.Binder#getCallingUid}. One important difference is 1978 * that if you are not currently processing an IPC, this function 1979 * will always fail. 1980 * 1981 * @param uri The uri that is being checked. 1982 * @param modeFlags The type of access to grant. May be one or both of 1983 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1984 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1985 * 1986 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 1987 * is allowed to access that uri, or 1988 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1989 * 1990 * @see #checkUriPermission(Uri, int, int, int) 1991 */ 1992 public abstract int checkCallingUriPermission(Uri uri, int modeFlags); 1993 1994 /** 1995 * Determine whether the calling process of an IPC <em>or you</em> has been granted 1996 * permission to access a specific URI. This is the same as 1997 * {@link #checkCallingUriPermission}, except it grants your own permissions 1998 * if you are not currently processing an IPC. Use with care! 1999 * 2000 * @param uri The uri that is being checked. 2001 * @param modeFlags The type of access to grant. May be one or both of 2002 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2003 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2004 * 2005 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 2006 * is allowed to access that uri, or 2007 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2008 * 2009 * @see #checkCallingUriPermission 2010 */ 2011 public abstract int checkCallingOrSelfUriPermission(Uri uri, int modeFlags); 2012 2013 /** 2014 * Check both a Uri and normal permission. This allows you to perform 2015 * both {@link #checkPermission} and {@link #checkUriPermission} in one 2016 * call. 2017 * 2018 * @param uri The Uri whose permission is to be checked, or null to not 2019 * do this check. 2020 * @param readPermission The permission that provides overall read access, 2021 * or null to not do this check. 2022 * @param writePermission The permission that provides overall write 2023 * acess, or null to not do this check. 2024 * @param pid The process ID being checked against. Must be > 0. 2025 * @param uid The user ID being checked against. A uid of 0 is the root 2026 * user, which will pass every permission check. 2027 * @param modeFlags The type of access to grant. May be one or both of 2028 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2029 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2030 * 2031 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 2032 * is allowed to access that uri or holds one of the given permissions, or 2033 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2034 */ 2035 public abstract int checkUriPermission(Uri uri, String readPermission, 2036 String writePermission, int pid, int uid, int modeFlags); 2037 2038 /** 2039 * If a particular process and user ID has not been granted 2040 * permission to access a specific URI, throw {@link 2041 * SecurityException}. This only checks for permissions that have 2042 * been explicitly granted -- if the given process/uid has more 2043 * general access to the URI's content provider then this check 2044 * will always fail. 2045 * 2046 * @param uri The uri that is being checked. 2047 * @param pid The process ID being checked against. Must be > 0. 2048 * @param uid The user ID being checked against. A uid of 0 is the root 2049 * user, which will pass every permission check. 2050 * @param modeFlags The type of access to grant. May be one or both of 2051 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2052 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2053 * @param message A message to include in the exception if it is thrown. 2054 * 2055 * @see #checkUriPermission(Uri, int, int, int) 2056 */ 2057 public abstract void enforceUriPermission( 2058 Uri uri, int pid, int uid, int modeFlags, String message); 2059 2060 /** 2061 * If the calling process and user ID has not been granted 2062 * permission to access a specific URI, throw {@link 2063 * SecurityException}. This is basically the same as calling 2064 * {@link #enforceUriPermission(Uri, int, int, int, String)} with 2065 * the pid and uid returned by {@link 2066 * android.os.Binder#getCallingPid} and {@link 2067 * android.os.Binder#getCallingUid}. One important difference is 2068 * that if you are not currently processing an IPC, this function 2069 * will always throw a SecurityException. 2070 * 2071 * @param uri The uri that is being checked. 2072 * @param modeFlags The type of access to grant. May be one or both of 2073 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2074 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2075 * @param message A message to include in the exception if it is thrown. 2076 * 2077 * @see #checkCallingUriPermission(Uri, int) 2078 */ 2079 public abstract void enforceCallingUriPermission( 2080 Uri uri, int modeFlags, String message); 2081 2082 /** 2083 * If the calling process of an IPC <em>or you</em> has not been 2084 * granted permission to access a specific URI, throw {@link 2085 * SecurityException}. This is the same as {@link 2086 * #enforceCallingUriPermission}, except it grants your own 2087 * permissions if you are not currently processing an IPC. Use 2088 * with care! 2089 * 2090 * @param uri The uri that is being checked. 2091 * @param modeFlags The type of access to grant. May be one or both of 2092 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2093 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2094 * @param message A message to include in the exception if it is thrown. 2095 * 2096 * @see #checkCallingOrSelfUriPermission(Uri, int) 2097 */ 2098 public abstract void enforceCallingOrSelfUriPermission( 2099 Uri uri, int modeFlags, String message); 2100 2101 /** 2102 * Enforce both a Uri and normal permission. This allows you to perform 2103 * both {@link #enforcePermission} and {@link #enforceUriPermission} in one 2104 * call. 2105 * 2106 * @param uri The Uri whose permission is to be checked, or null to not 2107 * do this check. 2108 * @param readPermission The permission that provides overall read access, 2109 * or null to not do this check. 2110 * @param writePermission The permission that provides overall write 2111 * acess, or null to not do this check. 2112 * @param pid The process ID being checked against. Must be > 0. 2113 * @param uid The user ID being checked against. A uid of 0 is the root 2114 * user, which will pass every permission check. 2115 * @param modeFlags The type of access to grant. May be one or both of 2116 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2117 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2118 * @param message A message to include in the exception if it is thrown. 2119 * 2120 * @see #checkUriPermission(Uri, String, String, int, int, int) 2121 */ 2122 public abstract void enforceUriPermission( 2123 Uri uri, String readPermission, String writePermission, 2124 int pid, int uid, int modeFlags, String message); 2125 2126 /** 2127 * Flag for use with {@link #createPackageContext}: include the application 2128 * code with the context. This means loading code into the caller's 2129 * process, so that {@link #getClassLoader()} can be used to instantiate 2130 * the application's classes. Setting this flags imposes security 2131 * restrictions on what application context you can access; if the 2132 * requested application can not be safely loaded into your process, 2133 * java.lang.SecurityException will be thrown. If this flag is not set, 2134 * there will be no restrictions on the packages that can be loaded, 2135 * but {@link #getClassLoader} will always return the default system 2136 * class loader. 2137 */ 2138 public static final int CONTEXT_INCLUDE_CODE = 0x00000001; 2139 2140 /** 2141 * Flag for use with {@link #createPackageContext}: ignore any security 2142 * restrictions on the Context being requested, allowing it to always 2143 * be loaded. For use with {@link #CONTEXT_INCLUDE_CODE} to allow code 2144 * to be loaded into a process even when it isn't safe to do so. Use 2145 * with extreme care! 2146 */ 2147 public static final int CONTEXT_IGNORE_SECURITY = 0x00000002; 2148 2149 /** 2150 * Flag for use with {@link #createPackageContext}: a restricted context may 2151 * disable specific features. For instance, a View associated with a restricted 2152 * context would ignore particular XML attributes. 2153 */ 2154 public static final int CONTEXT_RESTRICTED = 0x00000004; 2155 2156 /** 2157 * Return a new Context object for the given application name. This 2158 * Context is the same as what the named application gets when it is 2159 * launched, containing the same resources and class loader. Each call to 2160 * this method returns a new instance of a Context object; Context objects 2161 * are not shared, however they share common state (Resources, ClassLoader, 2162 * etc) so the Context instance itself is fairly lightweight. 2163 * 2164 * <p>Throws {@link PackageManager.NameNotFoundException} if there is no 2165 * application with the given package name. 2166 * 2167 * <p>Throws {@link java.lang.SecurityException} if the Context requested 2168 * can not be loaded into the caller's process for security reasons (see 2169 * {@link #CONTEXT_INCLUDE_CODE} for more information}. 2170 * 2171 * @param packageName Name of the application's package. 2172 * @param flags Option flags, one of {@link #CONTEXT_INCLUDE_CODE} 2173 * or {@link #CONTEXT_IGNORE_SECURITY}. 2174 * 2175 * @return A Context for the application. 2176 * 2177 * @throws java.lang.SecurityException 2178 * @throws PackageManager.NameNotFoundException if there is no application with 2179 * the given package name 2180 */ 2181 public abstract Context createPackageContext(String packageName, 2182 int flags) throws PackageManager.NameNotFoundException; 2183 2184 /** 2185 * Indicates whether this Context is restricted. 2186 * 2187 * @return True if this Context is restricted, false otherwise. 2188 * 2189 * @see #CONTEXT_RESTRICTED 2190 */ 2191 public boolean isRestricted() { 2192 return false; 2193 } 2194} 2195