Context.java revision 5c1207be90fdf296c1b83034b7c68915e1749284
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.sqlite.SQLiteDatabase; 25import android.database.sqlite.SQLiteDatabase.CursorFactory; 26import android.graphics.Bitmap; 27import android.graphics.drawable.Drawable; 28import android.net.Uri; 29import android.os.Bundle; 30import android.os.Handler; 31import android.os.Looper; 32import android.util.AttributeSet; 33 34import java.io.File; 35import java.io.FileInputStream; 36import java.io.FileNotFoundException; 37import java.io.FileOutputStream; 38import java.io.IOException; 39import java.io.InputStream; 40 41/** 42 * Interface to global information about an application environment. This is 43 * an abstract class whose implementation is provided by 44 * the Android system. It 45 * allows access to application-specific resources and classes, as well as 46 * up-calls for application-level operations such as launching activities, 47 * broadcasting and receiving intents, etc. 48 */ 49public abstract class Context { 50 /** 51 * File creation mode: the default mode, where the created file can only 52 * be accessed by the calling application (or all applications sharing the 53 * same user ID). 54 * @see #MODE_WORLD_READABLE 55 * @see #MODE_WORLD_WRITEABLE 56 */ 57 public static final int MODE_PRIVATE = 0x0000; 58 /** 59 * File creation mode: allow all other applications to have read access 60 * to the created file. 61 * @see #MODE_PRIVATE 62 * @see #MODE_WORLD_WRITEABLE 63 */ 64 public static final int MODE_WORLD_READABLE = 0x0001; 65 /** 66 * File creation mode: allow all other applications to have write access 67 * to the created file. 68 * @see #MODE_PRIVATE 69 * @see #MODE_WORLD_READABLE 70 */ 71 public static final int MODE_WORLD_WRITEABLE = 0x0002; 72 /** 73 * File creation mode: for use with {@link #openFileOutput}, if the file 74 * already exists then write data to the end of the existing file 75 * instead of erasing it. 76 * @see #openFileOutput 77 */ 78 public static final int MODE_APPEND = 0x8000; 79 80 /** 81 * Flag for {@link #bindService}: automatically create the service as long 82 * as the binding exists. Note that while this will create the service, 83 * its {@link android.app.Service#onStart} method will still only be called due to an 84 * explicit call to {@link #startService}. Even without that, though, 85 * this still provides you with access to the service object while the 86 * service is created. 87 * 88 * <p>Specifying this flag also tells the system to treat the service 89 * as being as important as your own process -- that is, when deciding 90 * which process should be killed to free memory, the service will only 91 * be considered a candidate as long as the processes of any such bindings 92 * is also a candidate to be killed. This is to avoid situations where 93 * the service is being continually created and killed due to low memory. 94 */ 95 public static final int BIND_AUTO_CREATE = 0x0001; 96 97 /** 98 * Flag for {@link #bindService}: include debugging help for mismatched 99 * calls to unbind. When this flag is set, the callstack of the following 100 * {@link #unbindService} call is retained, to be printed if a later 101 * incorrect unbind call is made. Note that doing this requires retaining 102 * information about the binding that was made for the lifetime of the app, 103 * resulting in a leak -- this should only be used for debugging. 104 */ 105 public static final int BIND_DEBUG_UNBIND = 0x0002; 106 107 /** Return an AssetManager instance for your application's package. */ 108 public abstract AssetManager getAssets(); 109 110 /** Return a Resources instance for your application's package. */ 111 public abstract Resources getResources(); 112 113 /** Return PackageManager instance to find global package information. */ 114 public abstract PackageManager getPackageManager(); 115 116 /** Return a ContentResolver instance for your application's package. */ 117 public abstract ContentResolver getContentResolver(); 118 119 /** 120 * Return the Looper for the main thread of the current process. This is 121 * the thread used to dispatch calls to application components (activities, 122 * services, etc). 123 */ 124 public abstract Looper getMainLooper(); 125 126 /** 127 * Return the context of the single, global Application object of the 128 * current process. 129 */ 130 public abstract Context getApplicationContext(); 131 132 /** 133 * Return a localized, styled CharSequence from the application's package's 134 * default string table. 135 * 136 * @param resId Resource id for the CharSequence text 137 */ 138 public final CharSequence getText(int resId) { 139 return getResources().getText(resId); 140 } 141 142 /** 143 * Return a localized string from the application's package's 144 * default string table. 145 * 146 * @param resId Resource id for the string 147 */ 148 public final String getString(int resId) { 149 return getResources().getString(resId); 150 } 151 152 /** 153 * Return a localized formatted string from the application's package's 154 * default string table, substituting the format arguments as defined in 155 * {@link java.util.Formatter} and {@link java.lang.String#format}. 156 * 157 * @param resId Resource id for the format string 158 * @param formatArgs The format arguments that will be used for substitution. 159 */ 160 161 public final String getString(int resId, Object... formatArgs) { 162 return getResources().getString(resId, formatArgs); 163 } 164 165 /** 166 * Set the base theme for this context. Note that this should be called 167 * before any views are instantiated in the Context (for example before 168 * calling {@link android.app.Activity#setContentView} or 169 * {@link android.view.LayoutInflater#inflate}). 170 * 171 * @param resid The style resource describing the theme. 172 */ 173 public abstract void setTheme(int resid); 174 175 /** 176 * Return the Theme object associated with this Context. 177 */ 178 public abstract Resources.Theme getTheme(); 179 180 /** 181 * Retrieve styled attribute information in this Context's theme. See 182 * {@link Resources.Theme#obtainStyledAttributes(int[])} 183 * for more information. 184 * 185 * @see Resources.Theme#obtainStyledAttributes(int[]) 186 */ 187 public final TypedArray obtainStyledAttributes( 188 int[] attrs) { 189 return getTheme().obtainStyledAttributes(attrs); 190 } 191 192 /** 193 * Retrieve styled attribute information in this Context's theme. See 194 * {@link Resources.Theme#obtainStyledAttributes(int, int[])} 195 * for more information. 196 * 197 * @see Resources.Theme#obtainStyledAttributes(int, int[]) 198 */ 199 public final TypedArray obtainStyledAttributes( 200 int resid, int[] attrs) throws Resources.NotFoundException { 201 return getTheme().obtainStyledAttributes(resid, attrs); 202 } 203 204 /** 205 * Retrieve styled attribute information in this Context's theme. See 206 * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} 207 * for more information. 208 * 209 * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 210 */ 211 public final TypedArray obtainStyledAttributes( 212 AttributeSet set, int[] attrs) { 213 return getTheme().obtainStyledAttributes(set, attrs, 0, 0); 214 } 215 216 /** 217 * Retrieve styled attribute information in this Context's theme. See 218 * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} 219 * for more information. 220 * 221 * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 222 */ 223 public final TypedArray obtainStyledAttributes( 224 AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) { 225 return getTheme().obtainStyledAttributes( 226 set, attrs, defStyleAttr, defStyleRes); 227 } 228 229 /** 230 * Return a class loader you can use to retrieve classes in this package. 231 */ 232 public abstract ClassLoader getClassLoader(); 233 234 /** Return the name of this application's package. */ 235 public abstract String getPackageName(); 236 237 /** Return the full application info for this context's package. */ 238 public abstract ApplicationInfo getApplicationInfo(); 239 240 /** 241 * {@hide} 242 * Return the full path to this context's resource files. This is the ZIP files 243 * containing the application's resources. 244 * 245 * <p>Note: this is not generally useful for applications, since they should 246 * not be directly accessing the file system. 247 * 248 * 249 * @return String Path to the resources. 250 */ 251 public abstract String getPackageResourcePath(); 252 253 /** 254 * {@hide} 255 * Return the full path to this context's code and asset files. This is the ZIP files 256 * containing the application's code and assets. 257 * 258 * <p>Note: this is not generally useful for applications, since they should 259 * not be directly accessing the file system. 260 * 261 * @return String Path to the code and assets. 262 */ 263 public abstract String getPackageCodePath(); 264 265 /** 266 * {@hide} 267 * Return the full path to the shared prefs file for the given prefs group name. 268 * 269 * <p>Note: this is not generally useful for applications, since they should 270 * not be directly accessing the file system. 271 */ 272 public abstract File getSharedPrefsFile(String name); 273 274 /** 275 * Retrieve and hold the contents of the preferences file 'name', returning 276 * a SharedPreferences through which you can retrieve and modify its 277 * values. Only one instance of the SharedPreferences object is returned 278 * to any callers for the same name, meaning they will see each other's 279 * edits as soon as they are made. 280 * 281 * @param name Desired preferences file. If a preferences file by this name 282 * does not exist, it will be created when you retrieve an 283 * editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). 284 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 285 * default operation, {@link #MODE_WORLD_READABLE} 286 * and {@link #MODE_WORLD_WRITEABLE} to control permissions. 287 * 288 * @return Returns the single SharedPreferences instance that can be used 289 * to retrieve and modify the preference values. 290 * 291 * @see #MODE_PRIVATE 292 * @see #MODE_WORLD_READABLE 293 * @see #MODE_WORLD_WRITEABLE 294 */ 295 public abstract SharedPreferences getSharedPreferences(String name, 296 int mode); 297 298 /** 299 * Open a private file associated with this Context's application package 300 * for reading. 301 * 302 * @param name The name of the file to open; can not contain path 303 * separators. 304 * 305 * @return FileInputStream Resulting input stream. 306 * 307 * @see #openFileOutput 308 * @see #fileList 309 * @see #deleteFile 310 * @see java.io.FileInputStream#FileInputStream(String) 311 */ 312 public abstract FileInputStream openFileInput(String name) 313 throws FileNotFoundException; 314 315 /** 316 * Open a private file associated with this Context's application package 317 * for writing. Creates the file if it doesn't already exist. 318 * 319 * @param name The name of the file to open; can not contain path 320 * separators. 321 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 322 * default operation, {@link #MODE_APPEND} to append to an existing file, 323 * {@link #MODE_WORLD_READABLE} and {@link #MODE_WORLD_WRITEABLE} to control 324 * permissions. 325 * 326 * @return FileOutputStream Resulting output stream. 327 * 328 * @see #MODE_APPEND 329 * @see #MODE_PRIVATE 330 * @see #MODE_WORLD_READABLE 331 * @see #MODE_WORLD_WRITEABLE 332 * @see #openFileInput 333 * @see #fileList 334 * @see #deleteFile 335 * @see java.io.FileOutputStream#FileOutputStream(String) 336 */ 337 public abstract FileOutputStream openFileOutput(String name, int mode) 338 throws FileNotFoundException; 339 340 /** 341 * Delete the given private file associated with this Context's 342 * application package. 343 * 344 * @param name The name of the file to delete; can not contain path 345 * separators. 346 * 347 * @return True if the file was successfully deleted; else 348 * false. 349 * 350 * @see #openFileInput 351 * @see #openFileOutput 352 * @see #fileList 353 * @see java.io.File#delete() 354 */ 355 public abstract boolean deleteFile(String name); 356 357 /** 358 * Returns the absolute path on the filesystem where a file created with 359 * {@link #openFileOutput} is stored. 360 * 361 * @param name The name of the file for which you would like to get 362 * its path. 363 * 364 * @return Returns an absolute path to the given file. 365 * 366 * @see #openFileOutput 367 * @see #getFilesDir 368 * @see #getDir 369 */ 370 public abstract File getFileStreamPath(String name); 371 372 /** 373 * Returns the absolute path to the directory on the filesystem where 374 * files created with {@link #openFileOutput} are stored. 375 * 376 * @return Returns the path of the directory holding application files. 377 * 378 * @see #openFileOutput 379 * @see #getFileStreamPath 380 * @see #getDir 381 */ 382 public abstract File getFilesDir(); 383 384 /** 385 * Returns the absolute path to the application specific cache directory 386 * on the filesystem. These files will be ones that get deleted first when the 387 * device runs low on storage 388 * There is no guarantee when these files will be deleted. 389 * 390 * @return Returns the path of the directory holding application cache files. 391 * 392 * @see #openFileOutput 393 * @see #getFileStreamPath 394 * @see #getDir 395 */ 396 public abstract File getCacheDir(); 397 398 /** 399 * Returns an array of strings naming the private files associated with 400 * this Context's application package. 401 * 402 * @return Array of strings naming the private files. 403 * 404 * @see #openFileInput 405 * @see #openFileOutput 406 * @see #deleteFile 407 */ 408 public abstract String[] fileList(); 409 410 /** 411 * Retrieve, creating if needed, a new directory in which the application 412 * can place its own custom data files. You can use the returned File 413 * object to create and access files in this directory. Note that files 414 * created through a File object will only be accessible by your own 415 * application; you can only set the mode of the entire directory, not 416 * of individual files. 417 * 418 * @param name Name of the directory to retrieve. This is a directory 419 * that is created as part of your application data. 420 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 421 * default operation, {@link #MODE_WORLD_READABLE} and 422 * {@link #MODE_WORLD_WRITEABLE} to control permissions. 423 * 424 * @return Returns a File object for the requested directory. The directory 425 * will have been created if it does not already exist. 426 * 427 * @see #openFileOutput(String, int) 428 */ 429 public abstract File getDir(String name, int mode); 430 431 /** 432 * Open a new private SQLiteDatabase associated with this Context's 433 * application package. Create the database file if it doesn't exist. 434 * 435 * @param name The name (unique in the application package) of the database. 436 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 437 * default operation, {@link #MODE_WORLD_READABLE} 438 * and {@link #MODE_WORLD_WRITEABLE} to control permissions. 439 * @param factory An optional factory class that is called to instantiate a 440 * cursor when query is called. 441 * 442 * @return The contents of a newly created database with the given name. 443 * @throws android.database.sqlite.SQLiteException if the database file could not be opened. 444 * 445 * @see #MODE_PRIVATE 446 * @see #MODE_WORLD_READABLE 447 * @see #MODE_WORLD_WRITEABLE 448 * @see #deleteDatabase 449 */ 450 public abstract SQLiteDatabase openOrCreateDatabase(String name, 451 int mode, CursorFactory factory); 452 453 /** 454 * Delete an existing private SQLiteDatabase associated with this Context's 455 * application package. 456 * 457 * @param name The name (unique in the application package) of the 458 * database. 459 * 460 * @return True if the database was successfully deleted; else false. 461 * 462 * @see #openOrCreateDatabase 463 */ 464 public abstract boolean deleteDatabase(String name); 465 466 /** 467 * Returns the absolute path on the filesystem where a database created with 468 * {@link #openOrCreateDatabase} is stored. 469 * 470 * @param name The name of the database for which you would like to get 471 * its path. 472 * 473 * @return Returns an absolute path to the given database. 474 * 475 * @see #openOrCreateDatabase 476 */ 477 public abstract File getDatabasePath(String name); 478 479 /** 480 * Returns an array of strings naming the private databases associated with 481 * this Context's application package. 482 * 483 * @return Array of strings naming the private databases. 484 * 485 * @see #openOrCreateDatabase 486 * @see #deleteDatabase 487 */ 488 public abstract String[] databaseList(); 489 490 /** 491 * Like {@link #peekWallpaper}, but always returns a valid Drawable. If 492 * no wallpaper is set, the system default wallpaper is returned. 493 * 494 * @return Returns a Drawable object that will draw the wallpaper. 495 */ 496 public abstract Drawable getWallpaper(); 497 498 /** 499 * Retrieve the current system wallpaper. This is returned as an 500 * abstract Drawable that you can install in a View to display whatever 501 * wallpaper the user has currently set. If there is no wallpaper set, 502 * a null pointer is returned. 503 * 504 * @return Returns a Drawable object that will draw the wallpaper or a 505 * null pointer if these is none. 506 */ 507 public abstract Drawable peekWallpaper(); 508 509 /** 510 * Returns the desired minimum width for the wallpaper. Callers of 511 * {@link #setWallpaper(android.graphics.Bitmap)} or 512 * {@link #setWallpaper(java.io.InputStream)} should check this value 513 * beforehand to make sure the supplied wallpaper respects the desired 514 * minimum width. 515 * 516 * If the returned value is <= 0, the caller should use the width of 517 * the default display instead. 518 * 519 * @return The desired minimum width for the wallpaper. This value should 520 * be honored by applications that set the wallpaper but it is not 521 * mandatory. 522 */ 523 public abstract int getWallpaperDesiredMinimumWidth(); 524 525 /** 526 * Returns the desired minimum height for the wallpaper. Callers of 527 * {@link #setWallpaper(android.graphics.Bitmap)} or 528 * {@link #setWallpaper(java.io.InputStream)} should check this value 529 * beforehand to make sure the supplied wallpaper respects the desired 530 * minimum height. 531 * 532 * If the returned value is <= 0, the caller should use the height of 533 * the default display instead. 534 * 535 * @return The desired minimum height for the wallpaper. This value should 536 * be honored by applications that set the wallpaper but it is not 537 * mandatory. 538 */ 539 public abstract int getWallpaperDesiredMinimumHeight(); 540 541 /** 542 * Change the current system wallpaper to a bitmap. The given bitmap is 543 * converted to a PNG and stored as the wallpaper. On success, the intent 544 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. 545 * 546 * @param bitmap The bitmap to save. 547 * 548 * @throws IOException If an error occurs reverting to the default 549 * wallpaper. 550 */ 551 public abstract void setWallpaper(Bitmap bitmap) throws IOException; 552 553 /** 554 * Change the current system wallpaper to a specific byte stream. The 555 * give InputStream is copied into persistent storage and will now be 556 * used as the wallpaper. Currently it must be either a JPEG or PNG 557 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} 558 * is broadcast. 559 * 560 * @param data A stream containing the raw data to install as a wallpaper. 561 * 562 * @throws IOException If an error occurs reverting to the default 563 * wallpaper. 564 */ 565 public abstract void setWallpaper(InputStream data) throws IOException; 566 567 /** 568 * Remove any currently set wallpaper, reverting to the system's default 569 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} 570 * is broadcast. 571 * 572 * @throws IOException If an error occurs reverting to the default 573 * wallpaper. 574 */ 575 public abstract void clearWallpaper() throws IOException; 576 577 /** 578 * Launch a new activity. You will not receive any information about when 579 * the activity exits. 580 * 581 * <p>Note that if this method is being called from outside of an 582 * {@link android.app.Activity} Context, then the Intent must include 583 * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because, 584 * without being started from an existing Activity, there is no existing 585 * task in which to place the new activity and thus it needs to be placed 586 * in its own separate task. 587 * 588 * <p>This method throws {@link ActivityNotFoundException} 589 * if there was no Activity found to run the given Intent. 590 * 591 * @param intent The description of the activity to start. 592 * 593 * @throws ActivityNotFoundException 594 * 595 * @see PackageManager#resolveActivity 596 */ 597 public abstract void startActivity(Intent intent); 598 599 /** 600 * Broadcast the given intent to all interested BroadcastReceivers. This 601 * call is asynchronous; it returns immediately, and you will continue 602 * executing while the receivers are run. No results are propagated from 603 * receivers and receivers can not abort the broadcast. If you want 604 * to allow receivers to propagate results or abort the broadcast, you must 605 * send an ordered broadcast using 606 * {@link #sendOrderedBroadcast(Intent, String)}. 607 * 608 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 609 * 610 * @param intent The Intent to broadcast; all receivers matching this 611 * Intent will receive the broadcast. 612 * 613 * @see android.content.BroadcastReceiver 614 * @see #registerReceiver 615 * @see #sendBroadcast(Intent, String) 616 * @see #sendOrderedBroadcast(Intent, String) 617 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 618 */ 619 public abstract void sendBroadcast(Intent intent); 620 621 /** 622 * Broadcast the given intent to all interested BroadcastReceivers, allowing 623 * an optional required permission to be enforced. This 624 * call is asynchronous; it returns immediately, and you will continue 625 * executing while the receivers are run. No results are propagated from 626 * receivers and receivers can not abort the broadcast. If you want 627 * to allow receivers to propagate results or abort the broadcast, you must 628 * send an ordered broadcast using 629 * {@link #sendOrderedBroadcast(Intent, String)}. 630 * 631 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 632 * 633 * @param intent The Intent to broadcast; all receivers matching this 634 * Intent will receive the broadcast. 635 * @param receiverPermission (optional) String naming a permissions that 636 * a receiver must hold in order to receive your broadcast. 637 * If null, no permission is required. 638 * 639 * @see android.content.BroadcastReceiver 640 * @see #registerReceiver 641 * @see #sendBroadcast(Intent) 642 * @see #sendOrderedBroadcast(Intent, String) 643 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 644 */ 645 public abstract void sendBroadcast(Intent intent, 646 String receiverPermission); 647 648 /** 649 * Broadcast the given intent to all interested BroadcastReceivers, delivering 650 * them one at a time to allow more preferred receivers to consume the 651 * broadcast before it is delivered to less preferred receivers. This 652 * call is asynchronous; it returns immediately, and you will continue 653 * executing while the receivers are run. 654 * 655 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 656 * 657 * @param intent The Intent to broadcast; all receivers matching this 658 * Intent will receive the broadcast. 659 * @param receiverPermission (optional) String naming a permissions that 660 * a receiver must hold in order to receive your broadcast. 661 * If null, no permission is required. 662 * 663 * @see android.content.BroadcastReceiver 664 * @see #registerReceiver 665 * @see #sendBroadcast(Intent) 666 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 667 */ 668 public abstract void sendOrderedBroadcast(Intent intent, 669 String receiverPermission); 670 671 /** 672 * Version of {@link #sendBroadcast(Intent)} that allows you to 673 * receive data back from the broadcast. This is accomplished by 674 * supplying your own BroadcastReceiver when calling, which will be 675 * treated as a final receiver at the end of the broadcast -- its 676 * {@link BroadcastReceiver#onReceive} method will be called with 677 * the result values collected from the other receivers. If you use 678 * an <var>resultReceiver</var> with this method, then the broadcast will 679 * be serialized in the same way as calling 680 * {@link #sendOrderedBroadcast(Intent, String)}. 681 * 682 * <p>Like {@link #sendBroadcast(Intent)}, this method is 683 * asynchronous; it will return before 684 * resultReceiver.onReceive() is called. 685 * 686 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 687 * 688 * @param intent The Intent to broadcast; all receivers matching this 689 * Intent will receive the broadcast. 690 * @param receiverPermission String naming a permissions that 691 * a receiver must hold in order to receive your broadcast. 692 * If null, no permission is required. 693 * @param resultReceiver Your own BroadcastReceiver to treat as the final 694 * receiver of the broadcast. 695 * @param scheduler A custom Handler with which to schedule the 696 * resultReceiver callback; if null it will be 697 * scheduled in the Context's main thread. 698 * @param initialCode An initial value for the result code. Often 699 * Activity.RESULT_OK. 700 * @param initialData An initial value for the result data. Often 701 * null. 702 * @param initialExtras An initial value for the result extras. Often 703 * null. 704 * 705 * @see #sendBroadcast(Intent) 706 * @see #sendBroadcast(Intent, String) 707 * @see #sendOrderedBroadcast(Intent, String) 708 * @see #sendStickyBroadcast(Intent) 709 * @see android.content.BroadcastReceiver 710 * @see #registerReceiver 711 * @see android.app.Activity#RESULT_OK 712 */ 713 public abstract void sendOrderedBroadcast(Intent intent, 714 String receiverPermission, BroadcastReceiver resultReceiver, 715 Handler scheduler, int initialCode, String initialData, 716 Bundle initialExtras); 717 718 /** 719 * Perform a {@link #sendBroadcast(Intent)} that is "sticky," meaning the 720 * Intent you are sending stays around after the broadcast is complete, 721 * so that others can quickly retrieve that data through the return 722 * value of {@link #registerReceiver(BroadcastReceiver, IntentFilter)}. In 723 * all other ways, this behaves the same as 724 * {@link #sendBroadcast(Intent)}. 725 * 726 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 727 * permission in order to use this API. If you do not hold that 728 * permission, {@link SecurityException} will be thrown. 729 * 730 * @param intent The Intent to broadcast; all receivers matching this 731 * Intent will receive the broadcast, and the Intent will be held to 732 * be re-broadcast to future receivers. 733 * 734 * @see #sendBroadcast(Intent) 735 */ 736 public abstract void sendStickyBroadcast(Intent intent); 737 738 /** 739 * Remove the data previously sent with {@link #sendStickyBroadcast}, 740 * so that it is as if the sticky broadcast had never happened. 741 * 742 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 743 * permission in order to use this API. If you do not hold that 744 * permission, {@link SecurityException} will be thrown. 745 * 746 * @param intent The Intent that was previously broadcast. 747 * 748 * @see #sendStickyBroadcast 749 */ 750 public abstract void removeStickyBroadcast(Intent intent); 751 752 /** 753 * Register a BroadcastReceiver to be run in the main activity thread. The 754 * <var>receiver</var> will be called with any broadcast Intent that 755 * matches <var>filter</var>, in the main application thread. 756 * 757 * <p>The system may broadcast Intents that are "sticky" -- these stay 758 * around after the broadcast as finished, to be sent to any later 759 * registrations. If your IntentFilter matches one of these sticky 760 * Intents, that Intent will be returned by this function 761 * <strong>and</strong> sent to your <var>receiver</var> as if it had just 762 * been broadcast. 763 * 764 * <p>There may be multiple sticky Intents that match <var>filter</var>, 765 * in which case each of these will be sent to <var>receiver</var>. In 766 * this case, only one of these can be returned directly by the function; 767 * which of these that is returned is arbitrarily decided by the system. 768 * 769 * <p>If you know the Intent your are registering for is sticky, you can 770 * supply null for your <var>receiver</var>. In this case, no receiver is 771 * registered -- the function simply returns the sticky Intent that 772 * matches <var>filter</var>. In the case of multiple matches, the same 773 * rules as described above apply. 774 * 775 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 776 * 777 * <p class="note">Note: this method <em>cannot be called from a 778 * {@link BroadcastReceiver} component;</em> that is, from a BroadcastReceiver 779 * that is declared in an application's manifest. It is okay, however, to call 780 * this method from another BroadcastReceiver that has itself been registered 781 * at run time with {@link #registerReceiver}, since the lifetime of such a 782 * registered BroadcastReceiver is tied to the object that registered it.</p> 783 * 784 * @param receiver The BroadcastReceiver to handle the broadcast. 785 * @param filter Selects the Intent broadcasts to be received. 786 * 787 * @return The first sticky intent found that matches <var>filter</var>, 788 * or null if there are none. 789 * 790 * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 791 * @see #sendBroadcast 792 * @see #unregisterReceiver 793 */ 794 public abstract Intent registerReceiver(BroadcastReceiver receiver, 795 IntentFilter filter); 796 797 /** 798 * Register to receive intent broadcasts, to run in the context of 799 * <var>scheduler</var>. See 800 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)} for more 801 * information. This allows you to enforce permissions on who can 802 * broadcast intents to your receiver, or have the receiver run in 803 * a different thread than the main application thread. 804 * 805 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 806 * 807 * @param receiver The BroadcastReceiver to handle the broadcast. 808 * @param filter Selects the Intent broadcasts to be received. 809 * @param broadcastPermission String naming a permissions that a 810 * broadcaster must hold in order to send an Intent to you. If null, 811 * no permission is required. 812 * @param scheduler Handler identifying the thread that will receive 813 * the Intent. If null, the main thread of the process will be used. 814 * 815 * @return The first sticky intent found that matches <var>filter</var>, 816 * or null if there are none. 817 * 818 * @see #registerReceiver(BroadcastReceiver, IntentFilter) 819 * @see #sendBroadcast 820 * @see #unregisterReceiver 821 */ 822 public abstract Intent registerReceiver(BroadcastReceiver receiver, 823 IntentFilter filter, 824 String broadcastPermission, 825 Handler scheduler); 826 827 /** 828 * Unregister a previously registered BroadcastReceiver. <em>All</em> 829 * filters that have been registered for this BroadcastReceiver will be 830 * removed. 831 * 832 * @param receiver The BroadcastReceiver to unregister. 833 * 834 * @see #registerReceiver 835 */ 836 public abstract void unregisterReceiver(BroadcastReceiver receiver); 837 838 /** 839 * Request that a given application service be started. The Intent 840 * can either contain the complete class name of a specific service 841 * implementation to start, or an abstract definition through the 842 * action and other fields of the kind of service to start. If this service 843 * is not already running, it will be instantiated and started (creating a 844 * process for it if needed); if it is running then it remains running. 845 * 846 * <p>Every call to this method will result in a corresponding call to 847 * the target service's {@link android.app.Service#onStart} method, 848 * with the <var>intent</var> given here. This provides a convenient way 849 * to submit jobs to a service without having to bind and call on to its 850 * interface. 851 * 852 * <p>Using startService() overrides the default service lifetime that is 853 * managed by {@link #bindService}: it requires the service to remain 854 * running until {@link #stopService} is called, regardless of whether 855 * any clients are connected to it. Note that calls to startService() 856 * are not nesting: no matter how many times you call startService(), 857 * a single call to {@link #stopService} will stop it. 858 * 859 * <p>The system attempts to keep running services around as much as 860 * possible. The only time they should be stopped is if the current 861 * foreground application is using so many resources that the service needs 862 * to be killed. If any errors happen in the service's process, it will 863 * automatically be restarted. 864 * 865 * <p>This function will throw {@link SecurityException} if you do not 866 * have permission to start the given service. 867 * 868 * @param service Identifies the service to be started. The Intent may 869 * specify either an explicit component name to start, or a logical 870 * description (action, category, etc) to match an 871 * {@link IntentFilter} published by a service. Additional values 872 * may be included in the Intent extras to supply arguments along with 873 * this specific start call. 874 * 875 * @return If the service is being started or is already running, the 876 * {@link ComponentName} of the actual service that was started is 877 * returned; else if the service does not exist null is returned. 878 * 879 * @throws SecurityException 880 * 881 * @see #stopService 882 * @see #bindService 883 */ 884 public abstract ComponentName startService(Intent service); 885 886 /** 887 * Request that a given application service be stopped. If the service is 888 * not running, nothing happens. Otherwise it is stopped. Note that calls 889 * to startService() are not counted -- this stops the service no matter 890 * how many times it was started. 891 * 892 * <p>Note that if a stopped service still has {@link ServiceConnection} 893 * objects bound to it with the {@link #BIND_AUTO_CREATE} set, it will 894 * not be destroyed until all of these bindings are removed. See 895 * the {@link android.app.Service} documentation for more details on a 896 * service's lifecycle. 897 * 898 * <p>This function will throw {@link SecurityException} if you do not 899 * have permission to stop the given service. 900 * 901 * @param service Description of the service to be stopped. The Intent may 902 * specify either an explicit component name to start, or a logical 903 * description (action, category, etc) to match an 904 * {@link IntentFilter} published by a service. 905 * 906 * @return If there is a service matching the given Intent that is already 907 * running, then it is stopped and true is returned; else false is returned. 908 * 909 * @throws SecurityException 910 * 911 * @see #startService 912 */ 913 public abstract boolean stopService(Intent service); 914 915 /** 916 * Connect to an application service, creating it if needed. This defines 917 * a dependency between your application and the service. The given 918 * <var>conn</var> will receive the service object when its created and be 919 * told if it dies and restarts. The service will be considered required 920 * by the system only for as long as the calling context exists. For 921 * example, if this Context is an Activity that is stopped, the service will 922 * not be required to continue running until the Activity is resumed. 923 * 924 * <p>This function will throw {@link SecurityException} if you do not 925 * have permission to bind to the given service. 926 * 927 * <p class="note">Note: this method <em>can not be called from an 928 * {@link BroadcastReceiver} component</em>. A pattern you can use to 929 * communicate from an BroadcastReceiver to a Service is to call 930 * {@link #startService} with the arguments containing the command to be 931 * sent, with the service calling its 932 * {@link android.app.Service#stopSelf(int)} method when done executing 933 * that command. See the API demo App/Service/Service Start Arguments 934 * Controller for an illustration of this. It is okay, however, to use 935 * this method from an BroadcastReceiver that has been registered with 936 * {@link #registerReceiver}, since the lifetime of this BroadcastReceiver 937 * is tied to another object (the one that registered it).</p> 938 * 939 * @param service Identifies the service to connect to. The Intent may 940 * specify either an explicit component name, or a logical 941 * description (action, category, etc) to match an 942 * {@link IntentFilter} published by a service. 943 * @param conn Receives information as the service is started and stopped. 944 * @param flags Operation options for the binding. May be 0 or 945 * {@link #BIND_AUTO_CREATE}. 946 * @return If you have successfully bound to the service, true is returned; 947 * false is returned if the connection is not made so you will not 948 * receive the service object. 949 * 950 * @throws SecurityException 951 * 952 * @see #unbindService 953 * @see #startService 954 * @see #BIND_AUTO_CREATE 955 */ 956 public abstract boolean bindService(Intent service, ServiceConnection conn, 957 int flags); 958 959 /** 960 * Disconnect from an application service. You will no longer receive 961 * calls as the service is restarted, and the service is now allowed to 962 * stop at any time. 963 * 964 * @param conn The connection interface previously supplied to 965 * bindService(). 966 * 967 * @see #bindService 968 */ 969 public abstract void unbindService(ServiceConnection conn); 970 971 /** 972 * Start executing an {@link android.app.Instrumentation} class. The given 973 * Instrumentation component will be run by killing its target application 974 * (if currently running), starting the target process, instantiating the 975 * instrumentation component, and then letting it drive the application. 976 * 977 * <p>This function is not synchronous -- it returns as soon as the 978 * instrumentation has started and while it is running. 979 * 980 * <p>Instrumentation is normally only allowed to run against a package 981 * that is either unsigned or signed with a signature that the 982 * the instrumentation package is also signed with (ensuring the target 983 * trusts the instrumentation). 984 * 985 * @param className Name of the Instrumentation component to be run. 986 * @param profileFile Optional path to write profiling data as the 987 * instrumentation runs, or null for no profiling. 988 * @param arguments Additional optional arguments to pass to the 989 * instrumentation, or null. 990 * 991 * @return Returns true if the instrumentation was successfully started, 992 * else false if it could not be found. 993 */ 994 public abstract boolean startInstrumentation(ComponentName className, 995 String profileFile, Bundle arguments); 996 997 /** 998 * Return the handle to a system-level service by name. The class of the 999 * returned object varies by the requested name. Currently available names 1000 * are: 1001 * 1002 * <dl> 1003 * <dt> {@link #WINDOW_SERVICE} ("window") 1004 * <dd> The top-level window manager in which you can place custom 1005 * windows. The returned object is a {@link android.view.WindowManager}. 1006 * <dt> {@link #LAYOUT_INFLATER_SERVICE} ("layout_inflater") 1007 * <dd> A {@link android.view.LayoutInflater} for inflating layout resources 1008 * in this context. 1009 * <dt> {@link #ACTIVITY_SERVICE} ("activity") 1010 * <dd> A {@link android.app.ActivityManager} for interacting with the 1011 * global activity state of the system. 1012 * <dt> {@link #POWER_SERVICE} ("power") 1013 * <dd> A {@link android.os.PowerManager} for controlling power 1014 * management. 1015 * <dt> {@link #ALARM_SERVICE} ("alarm") 1016 * <dd> A {@link android.app.AlarmManager} for receiving intents at the 1017 * time of your choosing. 1018 * <dt> {@link #NOTIFICATION_SERVICE} ("notification") 1019 * <dd> A {@link android.app.NotificationManager} for informing the user 1020 * of background events. 1021 * <dt> {@link #KEYGUARD_SERVICE} ("keyguard") 1022 * <dd> A {@link android.app.KeyguardManager} for controlling keyguard. 1023 * <dt> {@link #LOCATION_SERVICE} ("location") 1024 * <dd> A {@link android.location.LocationManager} for controlling location 1025 * (e.g., GPS) updates. 1026 * <dt> {@link #SEARCH_SERVICE} ("search") 1027 * <dd> A {@link android.app.SearchManager} for handling search. 1028 * <dt> {@link #VIBRATOR_SERVICE} ("vibrator") 1029 * <dd> A {@link android.os.Vibrator} for interacting with the vibrator 1030 * hardware. 1031 * <dt> {@link #CONNECTIVITY_SERVICE} ("connection") 1032 * <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for 1033 * handling management of network connections. 1034 * <dt> {@link #WIFI_SERVICE} ("wifi") 1035 * <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of 1036 * Wi-Fi connectivity. 1037 * <dt> {@link #INPUT_METHOD_SERVICE} ("input_method") 1038 * <dd> An {@link android.view.inputmethod.InputMethodManager InputMethodManager} 1039 * for management of input methods. 1040 * </dl> 1041 * 1042 * <p>Note: System services obtained via this API may be closely associated with 1043 * the Context in which they are obtained from. In general, do not share the 1044 * service objects between various different contexts (Activities, Applications, 1045 * Services, Providers, etc.) 1046 * 1047 * @param name The name of the desired service. 1048 * 1049 * @return The service or null if the name does not exist. 1050 * 1051 * @see #WINDOW_SERVICE 1052 * @see android.view.WindowManager 1053 * @see #LAYOUT_INFLATER_SERVICE 1054 * @see android.view.LayoutInflater 1055 * @see #ACTIVITY_SERVICE 1056 * @see android.app.ActivityManager 1057 * @see #POWER_SERVICE 1058 * @see android.os.PowerManager 1059 * @see #ALARM_SERVICE 1060 * @see android.app.AlarmManager 1061 * @see #NOTIFICATION_SERVICE 1062 * @see android.app.NotificationManager 1063 * @see #KEYGUARD_SERVICE 1064 * @see android.app.KeyguardManager 1065 * @see #LOCATION_SERVICE 1066 * @see android.location.LocationManager 1067 * @see #SEARCH_SERVICE 1068 * @see android.app.SearchManager 1069 * @see #SENSOR_SERVICE 1070 * @see android.hardware.SensorManager 1071 * @see #VIBRATOR_SERVICE 1072 * @see android.os.Vibrator 1073 * @see #CONNECTIVITY_SERVICE 1074 * @see android.net.ConnectivityManager 1075 * @see #WIFI_SERVICE 1076 * @see android.net.wifi.WifiManager 1077 * @see #AUDIO_SERVICE 1078 * @see android.media.AudioManager 1079 * @see #TELEPHONY_SERVICE 1080 * @see android.telephony.TelephonyManager 1081 * @see #INPUT_METHOD_SERVICE 1082 * @see android.view.inputmethod.InputMethodManager 1083 */ 1084 public abstract Object getSystemService(String name); 1085 1086 /** 1087 * Use with {@link #getSystemService} to retrieve a 1088 * {@link android.os.PowerManager} for controlling power management, 1089 * including "wake locks," which let you keep the device on while 1090 * you're running long tasks. 1091 */ 1092 public static final String POWER_SERVICE = "power"; 1093 /** 1094 * Use with {@link #getSystemService} to retrieve a 1095 * {@link android.view.WindowManager} for accessing the system's window 1096 * manager. 1097 * 1098 * @see #getSystemService 1099 * @see android.view.WindowManager 1100 */ 1101 public static final String WINDOW_SERVICE = "window"; 1102 /** 1103 * Use with {@link #getSystemService} to retrieve a 1104 * {@link android.view.LayoutInflater} for inflating layout resources in this 1105 * context. 1106 * 1107 * @see #getSystemService 1108 * @see android.view.LayoutInflater 1109 */ 1110 public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater"; 1111 /** 1112 * Use with {@link #getSystemService} to retrieve a 1113 * {@link android.app.ActivityManager} for interacting with the global 1114 * system state. 1115 * 1116 * @see #getSystemService 1117 * @see android.app.ActivityManager 1118 */ 1119 public static final String ACTIVITY_SERVICE = "activity"; 1120 /** 1121 * Use with {@link #getSystemService} to retrieve a 1122 * {@link android.app.AlarmManager} for receiving intents at a 1123 * time of your choosing. 1124 * 1125 * @see #getSystemService 1126 * @see android.app.AlarmManager 1127 */ 1128 public static final String ALARM_SERVICE = "alarm"; 1129 /** 1130 * Use with {@link #getSystemService} to retrieve a 1131 * {@link android.app.NotificationManager} for informing the user of 1132 * background events. 1133 * 1134 * @see #getSystemService 1135 * @see android.app.NotificationManager 1136 */ 1137 public static final String NOTIFICATION_SERVICE = "notification"; 1138 /** 1139 * Use with {@link #getSystemService} to retrieve a 1140 * {@link android.view.accessibility.AccessibilityManager} for giving the user 1141 * feedback for UI events through the registered event listeners. 1142 * 1143 * @see #getSystemService 1144 * @see android.view.accessibility.AccessibilityManager 1145 */ 1146 public static final String ACCESSIBILITY_SERVICE = "accessibility"; 1147 /** 1148 * Use with {@link #getSystemService} to retrieve a 1149 * {@link android.app.NotificationManager} for controlling keyguard. 1150 * 1151 * @see #getSystemService 1152 * @see android.app.KeyguardManager 1153 */ 1154 public static final String KEYGUARD_SERVICE = "keyguard"; 1155 /** 1156 * Use with {@link #getSystemService} to retrieve a {@link 1157 * android.location.LocationManager} for controlling location 1158 * updates. 1159 * 1160 * @see #getSystemService 1161 * @see android.location.LocationManager 1162 */ 1163 public static final String LOCATION_SERVICE = "location"; 1164 /** 1165 * Use with {@link #getSystemService} to retrieve a {@link 1166 * android.app.SearchManager} for handling searches. 1167 * 1168 * @see #getSystemService 1169 * @see android.app.SearchManager 1170 */ 1171 public static final String SEARCH_SERVICE = "search"; 1172 /** 1173 * Use with {@link #getSystemService} to retrieve a {@link 1174 * android.hardware.SensorManager} for accessing sensors. 1175 * 1176 * @see #getSystemService 1177 * @see android.hardware.SensorManager 1178 */ 1179 public static final String SENSOR_SERVICE = "sensor"; 1180 /** 1181 * Use with {@link #getSystemService} to retrieve a {@link 1182 * android.bluetooth.BluetoothDevice} for interacting with Bluetooth. 1183 * 1184 * @see #getSystemService 1185 * @see android.bluetooth.BluetoothDevice 1186 * @hide 1187 */ 1188 public static final String BLUETOOTH_SERVICE = "bluetooth"; 1189 /** 1190 * Use with {@link #getSystemService} to retrieve a 1191 * com.android.server.WallpaperService for accessing wallpapers. 1192 * 1193 * @see #getSystemService 1194 */ 1195 public static final String WALLPAPER_SERVICE = "wallpaper"; 1196 /** 1197 * Use with {@link #getSystemService} to retrieve a {@link 1198 * android.os.Vibrator} for interacting with the vibration hardware. 1199 * 1200 * @see #getSystemService 1201 * @see android.os.Vibrator 1202 */ 1203 public static final String VIBRATOR_SERVICE = "vibrator"; 1204 /** 1205 * Use with {@link #getSystemService} to retrieve a {@link 1206 * android.app.StatusBarManager} for interacting with the status bar. 1207 * 1208 * @see #getSystemService 1209 * @see android.app.StatusBarManager 1210 * @hide 1211 */ 1212 public static final String STATUS_BAR_SERVICE = "statusbar"; 1213 1214 /** 1215 * Use with {@link #getSystemService} to retrieve a {@link 1216 * android.net.ConnectivityManager} for handling management of 1217 * network connections. 1218 * 1219 * @see #getSystemService 1220 * @see android.net.ConnectivityManager 1221 */ 1222 public static final String CONNECTIVITY_SERVICE = "connectivity"; 1223 1224 /** 1225 * Use with {@link #getSystemService} to retrieve a {@link 1226 * android.net.wifi.WifiManager} for handling management of 1227 * Wi-Fi access. 1228 * 1229 * @see #getSystemService 1230 * @see android.net.wifi.WifiManager 1231 */ 1232 public static final String WIFI_SERVICE = "wifi"; 1233 1234 /** 1235 * Use with {@link #getSystemService} to retrieve a 1236 * {@link android.media.AudioManager} for handling management of volume, 1237 * ringer modes and audio routing. 1238 * 1239 * @see #getSystemService 1240 * @see android.media.AudioManager 1241 */ 1242 public static final String AUDIO_SERVICE = "audio"; 1243 1244 /** 1245 * Use with {@link #getSystemService} to retrieve a 1246 * {@link android.telephony.TelephonyManager} for handling management the 1247 * telephony features of the device. 1248 * 1249 * @see #getSystemService 1250 * @see android.telephony.TelephonyManager 1251 */ 1252 public static final String TELEPHONY_SERVICE = "phone"; 1253 1254 /** 1255 * Use with {@link #getSystemService} to retrieve a 1256 * {@link android.text.ClipboardManager} for accessing and modifying 1257 * the contents of the global clipboard. 1258 * 1259 * @see #getSystemService 1260 * @see android.text.ClipboardManager 1261 */ 1262 public static final String CLIPBOARD_SERVICE = "clipboard"; 1263 1264 /** 1265 * Use with {@link #getSystemService} to retrieve a 1266 * {@link android.view.inputmethod.InputMethodManager} for accessing input 1267 * methods. 1268 * 1269 * @see #getSystemService 1270 */ 1271 public static final String INPUT_METHOD_SERVICE = "input_method"; 1272 1273 /** 1274 * Use with {@link #getSystemService} to retrieve a 1275 * {@blink android.appwidget.AppWidgetManager} for accessing AppWidgets. 1276 * 1277 * @hide 1278 * @see #getSystemService 1279 */ 1280 public static final String APPWIDGET_SERVICE = "appwidget"; 1281 1282 /** 1283 * Use with {@link #getSystemService} to retrieve an 1284 * {@blink android.backup.IBackupManager IBackupManager} for communicating 1285 * with the backup mechanism. 1286 * @hide 1287 * 1288 * @see #getSystemService 1289 */ 1290 public static final String BACKUP_SERVICE = "backup"; 1291 1292 /** 1293 * Determine whether the given permission is allowed for a particular 1294 * process and user ID running in the system. 1295 * 1296 * @param permission The name of the permission being checked. 1297 * @param pid The process ID being checked against. Must be > 0. 1298 * @param uid The user ID being checked against. A uid of 0 is the root 1299 * user, which will pass every permission check. 1300 * 1301 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the given 1302 * pid/uid is allowed that permission, or 1303 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1304 * 1305 * @see PackageManager#checkPermission(String, String) 1306 * @see #checkCallingPermission 1307 */ 1308 public abstract int checkPermission(String permission, int pid, int uid); 1309 1310 /** 1311 * Determine whether the calling process of an IPC you are handling has been 1312 * granted a particular permission. This is basically the same as calling 1313 * {@link #checkPermission(String, int, int)} with the pid and uid returned 1314 * by {@link android.os.Binder#getCallingPid} and 1315 * {@link android.os.Binder#getCallingUid}. One important difference 1316 * is that if you are not currently processing an IPC, this function 1317 * will always fail. This is done to protect against accidentally 1318 * leaking permissions; you can use {@link #checkCallingOrSelfPermission} 1319 * to avoid this protection. 1320 * 1321 * @param permission The name of the permission being checked. 1322 * 1323 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the calling 1324 * pid/uid is allowed that permission, or 1325 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1326 * 1327 * @see PackageManager#checkPermission(String, String) 1328 * @see #checkPermission 1329 * @see #checkCallingOrSelfPermission 1330 */ 1331 public abstract int checkCallingPermission(String permission); 1332 1333 /** 1334 * Determine whether the calling process of an IPC <em>or you</em> have been 1335 * granted a particular permission. This is the same as 1336 * {@link #checkCallingPermission}, except it grants your own permissions 1337 * if you are not currently processing an IPC. Use with care! 1338 * 1339 * @param permission The name of the permission being checked. 1340 * 1341 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the calling 1342 * pid/uid is allowed that permission, or 1343 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1344 * 1345 * @see PackageManager#checkPermission(String, String) 1346 * @see #checkPermission 1347 * @see #checkCallingPermission 1348 */ 1349 public abstract int checkCallingOrSelfPermission(String permission); 1350 1351 /** 1352 * If the given permission is not allowed for a particular process 1353 * and user ID running in the system, throw a {@link SecurityException}. 1354 * 1355 * @param permission The name of the permission being checked. 1356 * @param pid The process ID being checked against. Must be > 0. 1357 * @param uid The user ID being checked against. A uid of 0 is the root 1358 * user, which will pass every permission check. 1359 * @param message A message to include in the exception if it is thrown. 1360 * 1361 * @see #checkPermission(String, int, int) 1362 */ 1363 public abstract void enforcePermission( 1364 String permission, int pid, int uid, String message); 1365 1366 /** 1367 * If the calling process of an IPC you are handling has not been 1368 * granted a particular permission, throw a {@link 1369 * SecurityException}. This is basically the same as calling 1370 * {@link #enforcePermission(String, int, int, String)} with the 1371 * pid and uid returned by {@link android.os.Binder#getCallingPid} 1372 * and {@link android.os.Binder#getCallingUid}. One important 1373 * difference is that if you are not currently processing an IPC, 1374 * this function will always throw the SecurityException. This is 1375 * done to protect against accidentally leaking permissions; you 1376 * can use {@link #enforceCallingOrSelfPermission} to avoid this 1377 * protection. 1378 * 1379 * @param permission The name of the permission being checked. 1380 * @param message A message to include in the exception if it is thrown. 1381 * 1382 * @see #checkCallingPermission(String) 1383 */ 1384 public abstract void enforceCallingPermission( 1385 String permission, String message); 1386 1387 /** 1388 * If neither you nor the calling process of an IPC you are 1389 * handling has been granted a particular permission, throw a 1390 * {@link SecurityException}. This is the same as {@link 1391 * #enforceCallingPermission}, except it grants your own 1392 * permissions if you are not currently processing an IPC. Use 1393 * with care! 1394 * 1395 * @param permission The name of the permission being checked. 1396 * @param message A message to include in the exception if it is thrown. 1397 * 1398 * @see #checkCallingOrSelfPermission(String) 1399 */ 1400 public abstract void enforceCallingOrSelfPermission( 1401 String permission, String message); 1402 1403 /** 1404 * Grant permission to access a specific Uri to another package, regardless 1405 * of whether that package has general permission to access the Uri's 1406 * content provider. This can be used to grant specific, temporary 1407 * permissions, typically in response to user interaction (such as the 1408 * user opening an attachment that you would like someone else to 1409 * display). 1410 * 1411 * <p>Normally you should use {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1412 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1413 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1414 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} with the Intent being used to 1415 * start an activity instead of this function directly. If you use this 1416 * function directly, you should be sure to call 1417 * {@link #revokeUriPermission} when the target should no longer be allowed 1418 * to access it. 1419 * 1420 * <p>To succeed, the content provider owning the Uri must have set the 1421 * {@link android.R.styleable#AndroidManifestProvider_grantUriPermissions 1422 * grantUriPermissions} attribute in its manifest or included the 1423 * {@link android.R.styleable#AndroidManifestGrantUriPermission 1424 * <grant-uri-permissions>} tag. 1425 * 1426 * @param toPackage The package you would like to allow to access the Uri. 1427 * @param uri The Uri you would like to grant access to. 1428 * @param modeFlags The desired access modes. Any combination of 1429 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1430 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1431 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1432 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1433 * 1434 * @see #revokeUriPermission 1435 */ 1436 public abstract void grantUriPermission(String toPackage, Uri uri, 1437 int modeFlags); 1438 1439 /** 1440 * Remove all permissions to access a particular content provider Uri 1441 * that were previously added with {@link #grantUriPermission}. The given 1442 * Uri will match all previously granted Uris that are the same or a 1443 * sub-path of the given Uri. That is, revoking "content://foo/one" will 1444 * revoke both "content://foo/target" and "content://foo/target/sub", but not 1445 * "content://foo". 1446 * 1447 * @param uri The Uri you would like to revoke access to. 1448 * @param modeFlags The desired access modes. Any combination of 1449 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1450 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1451 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1452 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1453 * 1454 * @see #grantUriPermission 1455 */ 1456 public abstract void revokeUriPermission(Uri uri, int modeFlags); 1457 1458 /** 1459 * Determine whether a particular process and user ID has been granted 1460 * permission to access a specific URI. This only checks for permissions 1461 * that have been explicitly granted -- if the given process/uid has 1462 * more general access to the URI's content provider then this check will 1463 * always fail. 1464 * 1465 * @param uri The uri that is being checked. 1466 * @param pid The process ID being checked against. Must be > 0. 1467 * @param uid The user ID being checked against. A uid of 0 is the root 1468 * user, which will pass every permission check. 1469 * @param modeFlags The type of access to grant. May be one or both of 1470 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1471 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1472 * 1473 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the given 1474 * pid/uid is allowed to access that uri, or 1475 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1476 * 1477 * @see #checkCallingUriPermission 1478 */ 1479 public abstract int checkUriPermission(Uri uri, int pid, int uid, int modeFlags); 1480 1481 /** 1482 * Determine whether the calling process and user ID has been 1483 * granted permission to access a specific URI. This is basically 1484 * the same as calling {@link #checkUriPermission(Uri, int, int, 1485 * int)} with the pid and uid returned by {@link 1486 * android.os.Binder#getCallingPid} and {@link 1487 * android.os.Binder#getCallingUid}. One important difference is 1488 * that if you are not currently processing an IPC, this function 1489 * will always fail. 1490 * 1491 * @param uri The uri that is being checked. 1492 * @param modeFlags The type of access to grant. May be one or both of 1493 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1494 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1495 * 1496 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 1497 * is allowed to access that uri, or 1498 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1499 * 1500 * @see #checkUriPermission(Uri, int, int, int) 1501 */ 1502 public abstract int checkCallingUriPermission(Uri uri, int modeFlags); 1503 1504 /** 1505 * Determine whether the calling process of an IPC <em>or you</em> has been granted 1506 * permission to access a specific URI. This is the same as 1507 * {@link #checkCallingUriPermission}, except it grants your own permissions 1508 * if you are not currently processing an IPC. Use with care! 1509 * 1510 * @param uri The uri that is being checked. 1511 * @param modeFlags The type of access to grant. May be one or both of 1512 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1513 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1514 * 1515 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 1516 * is allowed to access that uri, or 1517 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1518 * 1519 * @see #checkCallingUriPermission 1520 */ 1521 public abstract int checkCallingOrSelfUriPermission(Uri uri, int modeFlags); 1522 1523 /** 1524 * Check both a Uri and normal permission. This allows you to perform 1525 * both {@link #checkPermission} and {@link #checkUriPermission} in one 1526 * call. 1527 * 1528 * @param uri The Uri whose permission is to be checked, or null to not 1529 * do this check. 1530 * @param readPermission The permission that provides overall read access, 1531 * or null to not do this check. 1532 * @param writePermission The permission that provides overall write 1533 * acess, or null to not do this check. 1534 * @param pid The process ID being checked against. Must be > 0. 1535 * @param uid The user ID being checked against. A uid of 0 is the root 1536 * user, which will pass every permission check. 1537 * @param modeFlags The type of access to grant. May be one or both of 1538 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1539 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1540 * 1541 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 1542 * is allowed to access that uri or holds one of the given permissions, or 1543 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1544 */ 1545 public abstract int checkUriPermission(Uri uri, String readPermission, 1546 String writePermission, int pid, int uid, int modeFlags); 1547 1548 /** 1549 * If a particular process and user ID has not been granted 1550 * permission to access a specific URI, throw {@link 1551 * SecurityException}. This only checks for permissions that have 1552 * been explicitly granted -- if the given process/uid has more 1553 * general access to the URI's content provider then this check 1554 * will always fail. 1555 * 1556 * @param uri The uri that is being checked. 1557 * @param pid The process ID being checked against. Must be > 0. 1558 * @param uid The user ID being checked against. A uid of 0 is the root 1559 * user, which will pass every permission check. 1560 * @param modeFlags The type of access to grant. May be one or both of 1561 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1562 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1563 * @param message A message to include in the exception if it is thrown. 1564 * 1565 * @see #checkUriPermission(Uri, int, int, int) 1566 */ 1567 public abstract void enforceUriPermission( 1568 Uri uri, int pid, int uid, int modeFlags, String message); 1569 1570 /** 1571 * If the calling process and user ID has not been granted 1572 * permission to access a specific URI, throw {@link 1573 * SecurityException}. This is basically the same as calling 1574 * {@link #enforceUriPermission(Uri, int, int, int, String)} with 1575 * the pid and uid returned by {@link 1576 * android.os.Binder#getCallingPid} and {@link 1577 * android.os.Binder#getCallingUid}. One important difference is 1578 * that if you are not currently processing an IPC, this function 1579 * will always throw a SecurityException. 1580 * 1581 * @param uri The uri that is being checked. 1582 * @param modeFlags The type of access to grant. May be one or both of 1583 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1584 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1585 * @param message A message to include in the exception if it is thrown. 1586 * 1587 * @see #checkCallingUriPermission(Uri, int) 1588 */ 1589 public abstract void enforceCallingUriPermission( 1590 Uri uri, int modeFlags, String message); 1591 1592 /** 1593 * If the calling process of an IPC <em>or you</em> has not been 1594 * granted permission to access a specific URI, throw {@link 1595 * SecurityException}. This is the same as {@link 1596 * #enforceCallingUriPermission}, except it grants your own 1597 * permissions if you are not currently processing an IPC. Use 1598 * with care! 1599 * 1600 * @param uri The uri that is being checked. 1601 * @param modeFlags The type of access to grant. May be one or both of 1602 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1603 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1604 * @param message A message to include in the exception if it is thrown. 1605 * 1606 * @see #checkCallingOrSelfUriPermission(Uri, int) 1607 */ 1608 public abstract void enforceCallingOrSelfUriPermission( 1609 Uri uri, int modeFlags, String message); 1610 1611 /** 1612 * Enforce both a Uri and normal permission. This allows you to perform 1613 * both {@link #enforcePermission} and {@link #enforceUriPermission} in one 1614 * call. 1615 * 1616 * @param uri The Uri whose permission is to be checked, or null to not 1617 * do this check. 1618 * @param readPermission The permission that provides overall read access, 1619 * or null to not do this check. 1620 * @param writePermission The permission that provides overall write 1621 * acess, or null to not do this check. 1622 * @param pid The process ID being checked against. Must be > 0. 1623 * @param uid The user ID being checked against. A uid of 0 is the root 1624 * user, which will pass every permission check. 1625 * @param modeFlags The type of access to grant. May be one or both of 1626 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1627 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1628 * @param message A message to include in the exception if it is thrown. 1629 * 1630 * @see #checkUriPermission(Uri, String, String, int, int, int) 1631 */ 1632 public abstract void enforceUriPermission( 1633 Uri uri, String readPermission, String writePermission, 1634 int pid, int uid, int modeFlags, String message); 1635 1636 /** 1637 * Flag for use with {@link #createPackageContext}: include the application 1638 * code with the context. This means loading code into the caller's 1639 * process, so that {@link #getClassLoader()} can be used to instantiate 1640 * the application's classes. Setting this flags imposes security 1641 * restrictions on what application context you can access; if the 1642 * requested application can not be safely loaded into your process, 1643 * java.lang.SecurityException will be thrown. If this flag is not set, 1644 * there will be no restrictions on the packages that can be loaded, 1645 * but {@link #getClassLoader} will always return the default system 1646 * class loader. 1647 */ 1648 public static final int CONTEXT_INCLUDE_CODE = 0x00000001; 1649 1650 /** 1651 * Flag for use with {@link #createPackageContext}: ignore any security 1652 * restrictions on the Context being requested, allowing it to always 1653 * be loaded. For use with {@link #CONTEXT_INCLUDE_CODE} to allow code 1654 * to be loaded into a process even when it isn't safe to do so. Use 1655 * with extreme care! 1656 */ 1657 public static final int CONTEXT_IGNORE_SECURITY = 0x00000002; 1658 1659 /** 1660 * Flag for use with {@link #createPackageContext}: a restricted context may 1661 * disable specific features. For instance, a View associated with a restricted 1662 * context would ignore particular XML attributes. 1663 */ 1664 public static final int CONTEXT_RESTRICTED = 0x00000004; 1665 1666 /** 1667 * Return a new Context object for the given application name. This 1668 * Context is the same as what the named application gets when it is 1669 * launched, containing the same resources and class loader. Each call to 1670 * this method returns a new instance of a Context object; Context objects 1671 * are not shared, however they share common state (Resources, ClassLoader, 1672 * etc) so the Context instance itself is fairly lightweight. 1673 * 1674 * <p>Throws {@link PackageManager.NameNotFoundException} if there is no 1675 * application with the given package name. 1676 * 1677 * <p>Throws {@link java.lang.SecurityException} if the Context requested 1678 * can not be loaded into the caller's process for security reasons (see 1679 * {@link #CONTEXT_INCLUDE_CODE} for more information}. 1680 * 1681 * @param packageName Name of the application's package. 1682 * @param flags Option flags, one of {@link #CONTEXT_INCLUDE_CODE} 1683 * or {@link #CONTEXT_IGNORE_SECURITY}. 1684 * 1685 * @return A Context for the application. 1686 * 1687 * @throws java.lang.SecurityException 1688 * @throws PackageManager.NameNotFoundException if there is no application with 1689 * the given package name 1690 */ 1691 public abstract Context createPackageContext(String packageName, 1692 int flags) throws PackageManager.NameNotFoundException; 1693 1694 /** 1695 * Indicates whether this Context is restricted. 1696 * 1697 * @return True if this Context is restricted, false otherwise. 1698 * 1699 * @see #CONTEXT_RESTRICTED 1700 */ 1701 public boolean isRestricted() { 1702 return false; 1703 } 1704} 1705