Context.java revision 45e2704ff512d41e22af2801d76e96955469ce8d
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 * @deprecated Use {@link android.app.WallpaperManager#getDrawable 492 * WallpaperManager.get()} instead. 493 */ 494 public abstract Drawable getWallpaper(); 495 496 /** 497 * @deprecated Use {@link android.app.WallpaperManager#peekDrawable 498 * WallpaperManager.peek()} instead. 499 */ 500 public abstract Drawable peekWallpaper(); 501 502 /** 503 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumWidth() 504 * WallpaperManager.getDesiredMinimumWidth()} instead. 505 */ 506 public abstract int getWallpaperDesiredMinimumWidth(); 507 508 /** 509 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumHeight() 510 * WallpaperManager.getDesiredMinimumHeight()} instead. 511 */ 512 public abstract int getWallpaperDesiredMinimumHeight(); 513 514 /** 515 * @deprecated Use {@link android.app.WallpaperManager#setBitmap(Bitmap) 516 * WallpaperManager.set()} instead. 517 */ 518 public abstract void setWallpaper(Bitmap bitmap) throws IOException; 519 520 /** 521 * @deprecated Use {@link android.app.WallpaperManager#setStream(InputStream) 522 * WallpaperManager.set()} instead. 523 */ 524 public abstract void setWallpaper(InputStream data) throws IOException; 525 526 /** 527 * @deprecated Use {@link android.app.WallpaperManager#clear 528 * WallpaperManager.clear()} instead. 529 */ 530 public abstract void clearWallpaper() throws IOException; 531 532 /** 533 * Launch a new activity. You will not receive any information about when 534 * the activity exits. 535 * 536 * <p>Note that if this method is being called from outside of an 537 * {@link android.app.Activity} Context, then the Intent must include 538 * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because, 539 * without being started from an existing Activity, there is no existing 540 * task in which to place the new activity and thus it needs to be placed 541 * in its own separate task. 542 * 543 * <p>This method throws {@link ActivityNotFoundException} 544 * if there was no Activity found to run the given Intent. 545 * 546 * @param intent The description of the activity to start. 547 * 548 * @throws ActivityNotFoundException 549 * 550 * @see PackageManager#resolveActivity 551 */ 552 public abstract void startActivity(Intent intent); 553 554 /** 555 * Broadcast the given intent to all interested BroadcastReceivers. This 556 * call is asynchronous; it returns immediately, and you will continue 557 * executing while the receivers are run. No results are propagated from 558 * receivers and receivers can not abort the broadcast. If you want 559 * to allow receivers to propagate results or abort the broadcast, you must 560 * send an ordered broadcast using 561 * {@link #sendOrderedBroadcast(Intent, String)}. 562 * 563 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 564 * 565 * @param intent The Intent to broadcast; all receivers matching this 566 * Intent will receive the broadcast. 567 * 568 * @see android.content.BroadcastReceiver 569 * @see #registerReceiver 570 * @see #sendBroadcast(Intent, String) 571 * @see #sendOrderedBroadcast(Intent, String) 572 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 573 */ 574 public abstract void sendBroadcast(Intent intent); 575 576 /** 577 * Broadcast the given intent to all interested BroadcastReceivers, allowing 578 * an optional required permission to be enforced. This 579 * call is asynchronous; it returns immediately, and you will continue 580 * executing while the receivers are run. No results are propagated from 581 * receivers and receivers can not abort the broadcast. If you want 582 * to allow receivers to propagate results or abort the broadcast, you must 583 * send an ordered broadcast using 584 * {@link #sendOrderedBroadcast(Intent, String)}. 585 * 586 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 587 * 588 * @param intent The Intent to broadcast; all receivers matching this 589 * Intent will receive the broadcast. 590 * @param receiverPermission (optional) String naming a permissions that 591 * a receiver must hold in order to receive your broadcast. 592 * If null, no permission is required. 593 * 594 * @see android.content.BroadcastReceiver 595 * @see #registerReceiver 596 * @see #sendBroadcast(Intent) 597 * @see #sendOrderedBroadcast(Intent, String) 598 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 599 */ 600 public abstract void sendBroadcast(Intent intent, 601 String receiverPermission); 602 603 /** 604 * Broadcast the given intent to all interested BroadcastReceivers, delivering 605 * them one at a time to allow more preferred receivers to consume the 606 * broadcast before it is delivered to less preferred receivers. This 607 * call is asynchronous; it returns immediately, and you will continue 608 * executing while the receivers are run. 609 * 610 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 611 * 612 * @param intent The Intent to broadcast; all receivers matching this 613 * Intent will receive the broadcast. 614 * @param receiverPermission (optional) String naming a permissions that 615 * a receiver must hold in order to receive your broadcast. 616 * If null, no permission is required. 617 * 618 * @see android.content.BroadcastReceiver 619 * @see #registerReceiver 620 * @see #sendBroadcast(Intent) 621 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 622 */ 623 public abstract void sendOrderedBroadcast(Intent intent, 624 String receiverPermission); 625 626 /** 627 * Version of {@link #sendBroadcast(Intent)} that allows you to 628 * receive data back from the broadcast. This is accomplished by 629 * supplying your own BroadcastReceiver when calling, which will be 630 * treated as a final receiver at the end of the broadcast -- its 631 * {@link BroadcastReceiver#onReceive} method will be called with 632 * the result values collected from the other receivers. If you use 633 * an <var>resultReceiver</var> with this method, then the broadcast will 634 * be serialized in the same way as calling 635 * {@link #sendOrderedBroadcast(Intent, String)}. 636 * 637 * <p>Like {@link #sendBroadcast(Intent)}, this method is 638 * asynchronous; it will return before 639 * resultReceiver.onReceive() is called. 640 * 641 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 642 * 643 * @param intent The Intent to broadcast; all receivers matching this 644 * Intent will receive the broadcast. 645 * @param receiverPermission String naming a permissions that 646 * a receiver must hold in order to receive your broadcast. 647 * If null, no permission is required. 648 * @param resultReceiver Your own BroadcastReceiver to treat as the final 649 * receiver of the broadcast. 650 * @param scheduler A custom Handler with which to schedule the 651 * resultReceiver callback; if null it will be 652 * scheduled in the Context's main thread. 653 * @param initialCode An initial value for the result code. Often 654 * Activity.RESULT_OK. 655 * @param initialData An initial value for the result data. Often 656 * null. 657 * @param initialExtras An initial value for the result extras. Often 658 * null. 659 * 660 * @see #sendBroadcast(Intent) 661 * @see #sendBroadcast(Intent, String) 662 * @see #sendOrderedBroadcast(Intent, String) 663 * @see #sendStickyBroadcast(Intent) 664 * @see android.content.BroadcastReceiver 665 * @see #registerReceiver 666 * @see android.app.Activity#RESULT_OK 667 */ 668 public abstract void sendOrderedBroadcast(Intent intent, 669 String receiverPermission, BroadcastReceiver resultReceiver, 670 Handler scheduler, int initialCode, String initialData, 671 Bundle initialExtras); 672 673 /** 674 * Perform a {@link #sendBroadcast(Intent)} that is "sticky," meaning the 675 * Intent you are sending stays around after the broadcast is complete, 676 * so that others can quickly retrieve that data through the return 677 * value of {@link #registerReceiver(BroadcastReceiver, IntentFilter)}. In 678 * all other ways, this behaves the same as 679 * {@link #sendBroadcast(Intent)}. 680 * 681 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 682 * permission in order to use this API. If you do not hold that 683 * permission, {@link SecurityException} will be thrown. 684 * 685 * @param intent The Intent to broadcast; all receivers matching this 686 * Intent will receive the broadcast, and the Intent will be held to 687 * be re-broadcast to future receivers. 688 * 689 * @see #sendBroadcast(Intent) 690 */ 691 public abstract void sendStickyBroadcast(Intent intent); 692 693 /** 694 * Remove the data previously sent with {@link #sendStickyBroadcast}, 695 * so that it is as if the sticky broadcast had never happened. 696 * 697 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 698 * permission in order to use this API. If you do not hold that 699 * permission, {@link SecurityException} will be thrown. 700 * 701 * @param intent The Intent that was previously broadcast. 702 * 703 * @see #sendStickyBroadcast 704 */ 705 public abstract void removeStickyBroadcast(Intent intent); 706 707 /** 708 * Register a BroadcastReceiver to be run in the main activity thread. The 709 * <var>receiver</var> will be called with any broadcast Intent that 710 * matches <var>filter</var>, in the main application thread. 711 * 712 * <p>The system may broadcast Intents that are "sticky" -- these stay 713 * around after the broadcast as finished, to be sent to any later 714 * registrations. If your IntentFilter matches one of these sticky 715 * Intents, that Intent will be returned by this function 716 * <strong>and</strong> sent to your <var>receiver</var> as if it had just 717 * been broadcast. 718 * 719 * <p>There may be multiple sticky Intents that match <var>filter</var>, 720 * in which case each of these will be sent to <var>receiver</var>. In 721 * this case, only one of these can be returned directly by the function; 722 * which of these that is returned is arbitrarily decided by the system. 723 * 724 * <p>If you know the Intent your are registering for is sticky, you can 725 * supply null for your <var>receiver</var>. In this case, no receiver is 726 * registered -- the function simply returns the sticky Intent that 727 * matches <var>filter</var>. In the case of multiple matches, the same 728 * rules as described above apply. 729 * 730 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 731 * 732 * <p class="note">Note: this method <em>cannot be called from a 733 * {@link BroadcastReceiver} component;</em> that is, from a BroadcastReceiver 734 * that is declared in an application's manifest. It is okay, however, to call 735 * this method from another BroadcastReceiver that has itself been registered 736 * at run time with {@link #registerReceiver}, since the lifetime of such a 737 * registered BroadcastReceiver is tied to the object that registered it.</p> 738 * 739 * @param receiver The BroadcastReceiver to handle the broadcast. 740 * @param filter Selects the Intent broadcasts to be received. 741 * 742 * @return The first sticky intent found that matches <var>filter</var>, 743 * or null if there are none. 744 * 745 * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 746 * @see #sendBroadcast 747 * @see #unregisterReceiver 748 */ 749 public abstract Intent registerReceiver(BroadcastReceiver receiver, 750 IntentFilter filter); 751 752 /** 753 * Register to receive intent broadcasts, to run in the context of 754 * <var>scheduler</var>. See 755 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)} for more 756 * information. This allows you to enforce permissions on who can 757 * broadcast intents to your receiver, or have the receiver run in 758 * a different thread than the main application thread. 759 * 760 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 761 * 762 * @param receiver The BroadcastReceiver to handle the broadcast. 763 * @param filter Selects the Intent broadcasts to be received. 764 * @param broadcastPermission String naming a permissions that a 765 * broadcaster must hold in order to send an Intent to you. If null, 766 * no permission is required. 767 * @param scheduler Handler identifying the thread that will receive 768 * the Intent. If null, the main thread of the process will be used. 769 * 770 * @return The first sticky intent found that matches <var>filter</var>, 771 * or null if there are none. 772 * 773 * @see #registerReceiver(BroadcastReceiver, IntentFilter) 774 * @see #sendBroadcast 775 * @see #unregisterReceiver 776 */ 777 public abstract Intent registerReceiver(BroadcastReceiver receiver, 778 IntentFilter filter, 779 String broadcastPermission, 780 Handler scheduler); 781 782 /** 783 * Unregister a previously registered BroadcastReceiver. <em>All</em> 784 * filters that have been registered for this BroadcastReceiver will be 785 * removed. 786 * 787 * @param receiver The BroadcastReceiver to unregister. 788 * 789 * @see #registerReceiver 790 */ 791 public abstract void unregisterReceiver(BroadcastReceiver receiver); 792 793 /** 794 * Request that a given application service be started. The Intent 795 * can either contain the complete class name of a specific service 796 * implementation to start, or an abstract definition through the 797 * action and other fields of the kind of service to start. If this service 798 * is not already running, it will be instantiated and started (creating a 799 * process for it if needed); if it is running then it remains running. 800 * 801 * <p>Every call to this method will result in a corresponding call to 802 * the target service's {@link android.app.Service#onStart} method, 803 * with the <var>intent</var> given here. This provides a convenient way 804 * to submit jobs to a service without having to bind and call on to its 805 * interface. 806 * 807 * <p>Using startService() overrides the default service lifetime that is 808 * managed by {@link #bindService}: it requires the service to remain 809 * running until {@link #stopService} is called, regardless of whether 810 * any clients are connected to it. Note that calls to startService() 811 * are not nesting: no matter how many times you call startService(), 812 * a single call to {@link #stopService} will stop it. 813 * 814 * <p>The system attempts to keep running services around as much as 815 * possible. The only time they should be stopped is if the current 816 * foreground application is using so many resources that the service needs 817 * to be killed. If any errors happen in the service's process, it will 818 * automatically be restarted. 819 * 820 * <p>This function will throw {@link SecurityException} if you do not 821 * have permission to start the given service. 822 * 823 * @param service Identifies the service to be started. The Intent may 824 * specify either an explicit component name to start, or a logical 825 * description (action, category, etc) to match an 826 * {@link IntentFilter} published by a service. Additional values 827 * may be included in the Intent extras to supply arguments along with 828 * this specific start call. 829 * 830 * @return If the service is being started or is already running, the 831 * {@link ComponentName} of the actual service that was started is 832 * returned; else if the service does not exist null is returned. 833 * 834 * @throws SecurityException 835 * 836 * @see #stopService 837 * @see #bindService 838 */ 839 public abstract ComponentName startService(Intent service); 840 841 /** 842 * Request that a given application service be stopped. If the service is 843 * not running, nothing happens. Otherwise it is stopped. Note that calls 844 * to startService() are not counted -- this stops the service no matter 845 * how many times it was started. 846 * 847 * <p>Note that if a stopped service still has {@link ServiceConnection} 848 * objects bound to it with the {@link #BIND_AUTO_CREATE} set, it will 849 * not be destroyed until all of these bindings are removed. See 850 * the {@link android.app.Service} documentation for more details on a 851 * service's lifecycle. 852 * 853 * <p>This function will throw {@link SecurityException} if you do not 854 * have permission to stop the given service. 855 * 856 * @param service Description of the service to be stopped. The Intent may 857 * specify either an explicit component name to start, or a logical 858 * description (action, category, etc) to match an 859 * {@link IntentFilter} published by a service. 860 * 861 * @return If there is a service matching the given Intent that is already 862 * running, then it is stopped and true is returned; else false is returned. 863 * 864 * @throws SecurityException 865 * 866 * @see #startService 867 */ 868 public abstract boolean stopService(Intent service); 869 870 /** 871 * Connect to an application service, creating it if needed. This defines 872 * a dependency between your application and the service. The given 873 * <var>conn</var> will receive the service object when its created and be 874 * told if it dies and restarts. The service will be considered required 875 * by the system only for as long as the calling context exists. For 876 * example, if this Context is an Activity that is stopped, the service will 877 * not be required to continue running until the Activity is resumed. 878 * 879 * <p>This function will throw {@link SecurityException} if you do not 880 * have permission to bind to the given service. 881 * 882 * <p class="note">Note: this method <em>can not be called from an 883 * {@link BroadcastReceiver} component</em>. A pattern you can use to 884 * communicate from an BroadcastReceiver to a Service is to call 885 * {@link #startService} with the arguments containing the command to be 886 * sent, with the service calling its 887 * {@link android.app.Service#stopSelf(int)} method when done executing 888 * that command. See the API demo App/Service/Service Start Arguments 889 * Controller for an illustration of this. It is okay, however, to use 890 * this method from an BroadcastReceiver that has been registered with 891 * {@link #registerReceiver}, since the lifetime of this BroadcastReceiver 892 * is tied to another object (the one that registered it).</p> 893 * 894 * @param service Identifies the service to connect to. The Intent may 895 * specify either an explicit component name, or a logical 896 * description (action, category, etc) to match an 897 * {@link IntentFilter} published by a service. 898 * @param conn Receives information as the service is started and stopped. 899 * @param flags Operation options for the binding. May be 0 or 900 * {@link #BIND_AUTO_CREATE}. 901 * @return If you have successfully bound to the service, true is returned; 902 * false is returned if the connection is not made so you will not 903 * receive the service object. 904 * 905 * @throws SecurityException 906 * 907 * @see #unbindService 908 * @see #startService 909 * @see #BIND_AUTO_CREATE 910 */ 911 public abstract boolean bindService(Intent service, ServiceConnection conn, 912 int flags); 913 914 /** 915 * Disconnect from an application service. You will no longer receive 916 * calls as the service is restarted, and the service is now allowed to 917 * stop at any time. 918 * 919 * @param conn The connection interface previously supplied to 920 * bindService(). 921 * 922 * @see #bindService 923 */ 924 public abstract void unbindService(ServiceConnection conn); 925 926 /** 927 * Start executing an {@link android.app.Instrumentation} class. The given 928 * Instrumentation component will be run by killing its target application 929 * (if currently running), starting the target process, instantiating the 930 * instrumentation component, and then letting it drive the application. 931 * 932 * <p>This function is not synchronous -- it returns as soon as the 933 * instrumentation has started and while it is running. 934 * 935 * <p>Instrumentation is normally only allowed to run against a package 936 * that is either unsigned or signed with a signature that the 937 * the instrumentation package is also signed with (ensuring the target 938 * trusts the instrumentation). 939 * 940 * @param className Name of the Instrumentation component to be run. 941 * @param profileFile Optional path to write profiling data as the 942 * instrumentation runs, or null for no profiling. 943 * @param arguments Additional optional arguments to pass to the 944 * instrumentation, or null. 945 * 946 * @return Returns true if the instrumentation was successfully started, 947 * else false if it could not be found. 948 */ 949 public abstract boolean startInstrumentation(ComponentName className, 950 String profileFile, Bundle arguments); 951 952 /** 953 * Return the handle to a system-level service by name. The class of the 954 * returned object varies by the requested name. Currently available names 955 * are: 956 * 957 * <dl> 958 * <dt> {@link #WINDOW_SERVICE} ("window") 959 * <dd> The top-level window manager in which you can place custom 960 * windows. The returned object is a {@link android.view.WindowManager}. 961 * <dt> {@link #LAYOUT_INFLATER_SERVICE} ("layout_inflater") 962 * <dd> A {@link android.view.LayoutInflater} for inflating layout resources 963 * in this context. 964 * <dt> {@link #ACTIVITY_SERVICE} ("activity") 965 * <dd> A {@link android.app.ActivityManager} for interacting with the 966 * global activity state of the system. 967 * <dt> {@link #POWER_SERVICE} ("power") 968 * <dd> A {@link android.os.PowerManager} for controlling power 969 * management. 970 * <dt> {@link #ALARM_SERVICE} ("alarm") 971 * <dd> A {@link android.app.AlarmManager} for receiving intents at the 972 * time of your choosing. 973 * <dt> {@link #NOTIFICATION_SERVICE} ("notification") 974 * <dd> A {@link android.app.NotificationManager} for informing the user 975 * of background events. 976 * <dt> {@link #KEYGUARD_SERVICE} ("keyguard") 977 * <dd> A {@link android.app.KeyguardManager} for controlling keyguard. 978 * <dt> {@link #LOCATION_SERVICE} ("location") 979 * <dd> A {@link android.location.LocationManager} for controlling location 980 * (e.g., GPS) updates. 981 * <dt> {@link #SEARCH_SERVICE} ("search") 982 * <dd> A {@link android.app.SearchManager} for handling search. 983 * <dt> {@link #VIBRATOR_SERVICE} ("vibrator") 984 * <dd> A {@link android.os.Vibrator} for interacting with the vibrator 985 * hardware. 986 * <dt> {@link #CONNECTIVITY_SERVICE} ("connection") 987 * <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for 988 * handling management of network connections. 989 * <dt> {@link #WIFI_SERVICE} ("wifi") 990 * <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of 991 * Wi-Fi connectivity. 992 * <dt> {@link #INPUT_METHOD_SERVICE} ("input_method") 993 * <dd> An {@link android.view.inputmethod.InputMethodManager InputMethodManager} 994 * for management of input methods. 995 * </dl> 996 * 997 * <p>Note: System services obtained via this API may be closely associated with 998 * the Context in which they are obtained from. In general, do not share the 999 * service objects between various different contexts (Activities, Applications, 1000 * Services, Providers, etc.) 1001 * 1002 * @param name The name of the desired service. 1003 * 1004 * @return The service or null if the name does not exist. 1005 * 1006 * @see #WINDOW_SERVICE 1007 * @see android.view.WindowManager 1008 * @see #LAYOUT_INFLATER_SERVICE 1009 * @see android.view.LayoutInflater 1010 * @see #ACTIVITY_SERVICE 1011 * @see android.app.ActivityManager 1012 * @see #POWER_SERVICE 1013 * @see android.os.PowerManager 1014 * @see #ALARM_SERVICE 1015 * @see android.app.AlarmManager 1016 * @see #NOTIFICATION_SERVICE 1017 * @see android.app.NotificationManager 1018 * @see #KEYGUARD_SERVICE 1019 * @see android.app.KeyguardManager 1020 * @see #LOCATION_SERVICE 1021 * @see android.location.LocationManager 1022 * @see #SEARCH_SERVICE 1023 * @see android.app.SearchManager 1024 * @see #SENSOR_SERVICE 1025 * @see android.hardware.SensorManager 1026 * @see #VIBRATOR_SERVICE 1027 * @see android.os.Vibrator 1028 * @see #CONNECTIVITY_SERVICE 1029 * @see android.net.ConnectivityManager 1030 * @see #WIFI_SERVICE 1031 * @see android.net.wifi.WifiManager 1032 * @see #AUDIO_SERVICE 1033 * @see android.media.AudioManager 1034 * @see #TELEPHONY_SERVICE 1035 * @see android.telephony.TelephonyManager 1036 * @see #INPUT_METHOD_SERVICE 1037 * @see android.view.inputmethod.InputMethodManager 1038 */ 1039 public abstract Object getSystemService(String name); 1040 1041 /** 1042 * Use with {@link #getSystemService} to retrieve a 1043 * {@link android.os.PowerManager} for controlling power management, 1044 * including "wake locks," which let you keep the device on while 1045 * you're running long tasks. 1046 */ 1047 public static final String POWER_SERVICE = "power"; 1048 /** 1049 * Use with {@link #getSystemService} to retrieve a 1050 * {@link android.view.WindowManager} for accessing the system's window 1051 * manager. 1052 * 1053 * @see #getSystemService 1054 * @see android.view.WindowManager 1055 */ 1056 public static final String WINDOW_SERVICE = "window"; 1057 /** 1058 * Use with {@link #getSystemService} to retrieve a 1059 * {@link android.view.LayoutInflater} for inflating layout resources in this 1060 * context. 1061 * 1062 * @see #getSystemService 1063 * @see android.view.LayoutInflater 1064 */ 1065 public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater"; 1066 /** 1067 * Use with {@link #getSystemService} to retrieve a 1068 * {@link android.accounts.AccountManager} for receiving intents at a 1069 * time of your choosing. 1070 * TODO STOPSHIP perform a final review of the the account apis before shipping 1071 * 1072 * @see #getSystemService 1073 * @see android.accounts.AccountManager 1074 */ 1075 public static final String ACCOUNT_SERVICE = "account"; 1076 /** 1077 * Use with {@link #getSystemService} to retrieve a 1078 * {@link android.app.ActivityManager} for interacting with the global 1079 * system state. 1080 * 1081 * @see #getSystemService 1082 * @see android.app.ActivityManager 1083 */ 1084 public static final String ACTIVITY_SERVICE = "activity"; 1085 /** 1086 * Use with {@link #getSystemService} to retrieve a 1087 * {@link android.app.AlarmManager} for receiving intents at a 1088 * time of your choosing. 1089 * 1090 * @see #getSystemService 1091 * @see android.app.AlarmManager 1092 */ 1093 public static final String ALARM_SERVICE = "alarm"; 1094 /** 1095 * Use with {@link #getSystemService} to retrieve a 1096 * {@link android.app.NotificationManager} for informing the user of 1097 * background events. 1098 * 1099 * @see #getSystemService 1100 * @see android.app.NotificationManager 1101 */ 1102 public static final String NOTIFICATION_SERVICE = "notification"; 1103 /** 1104 * Use with {@link #getSystemService} to retrieve a 1105 * {@link android.view.accessibility.AccessibilityManager} for giving the user 1106 * feedback for UI events through the registered event listeners. 1107 * 1108 * @see #getSystemService 1109 * @see android.view.accessibility.AccessibilityManager 1110 */ 1111 public static final String ACCESSIBILITY_SERVICE = "accessibility"; 1112 /** 1113 * Use with {@link #getSystemService} to retrieve a 1114 * {@link android.app.NotificationManager} for controlling keyguard. 1115 * 1116 * @see #getSystemService 1117 * @see android.app.KeyguardManager 1118 */ 1119 public static final String KEYGUARD_SERVICE = "keyguard"; 1120 /** 1121 * Use with {@link #getSystemService} to retrieve a {@link 1122 * android.location.LocationManager} for controlling location 1123 * updates. 1124 * 1125 * @see #getSystemService 1126 * @see android.location.LocationManager 1127 */ 1128 public static final String LOCATION_SERVICE = "location"; 1129 /** 1130 * Use with {@link #getSystemService} to retrieve a {@link 1131 * android.app.SearchManager} for handling searches. 1132 * 1133 * @see #getSystemService 1134 * @see android.app.SearchManager 1135 */ 1136 public static final String SEARCH_SERVICE = "search"; 1137 /** 1138 * Use with {@link #getSystemService} to retrieve a {@link 1139 * android.hardware.SensorManager} for accessing sensors. 1140 * 1141 * @see #getSystemService 1142 * @see android.hardware.SensorManager 1143 */ 1144 public static final String SENSOR_SERVICE = "sensor"; 1145 /** 1146 * Use with {@link #getSystemService} to retrieve a {@link 1147 * android.bluetooth.BluetoothAdapter} for using Bluetooth. 1148 * 1149 * @see #getSystemService 1150 * @see android.bluetooth.BluetoothAdapter 1151 */ 1152 public static final String BLUETOOTH_SERVICE = "bluetooth"; 1153 /** 1154 * Use with {@link #getSystemService} to retrieve a 1155 * com.android.server.WallpaperService for accessing wallpapers. 1156 * 1157 * @see #getSystemService 1158 */ 1159 public static final String WALLPAPER_SERVICE = "wallpaper"; 1160 /** 1161 * Use with {@link #getSystemService} to retrieve a {@link 1162 * android.os.Vibrator} for interacting with the vibration hardware. 1163 * 1164 * @see #getSystemService 1165 * @see android.os.Vibrator 1166 */ 1167 public static final String VIBRATOR_SERVICE = "vibrator"; 1168 /** 1169 * Use with {@link #getSystemService} to retrieve a {@link 1170 * android.app.StatusBarManager} for interacting with the status bar. 1171 * 1172 * @see #getSystemService 1173 * @see android.app.StatusBarManager 1174 * @hide 1175 */ 1176 public static final String STATUS_BAR_SERVICE = "statusbar"; 1177 1178 /** 1179 * Use with {@link #getSystemService} to retrieve a {@link 1180 * android.net.ConnectivityManager} for handling management of 1181 * network connections. 1182 * 1183 * @see #getSystemService 1184 * @see android.net.ConnectivityManager 1185 */ 1186 public static final String CONNECTIVITY_SERVICE = "connectivity"; 1187 1188 /** 1189 * Use with {@link #getSystemService} to retrieve a {@link 1190 * android.net.wifi.WifiManager} for handling management of 1191 * Wi-Fi access. 1192 * 1193 * @see #getSystemService 1194 * @see android.net.wifi.WifiManager 1195 */ 1196 public static final String WIFI_SERVICE = "wifi"; 1197 1198 /** 1199 * Use with {@link #getSystemService} to retrieve a 1200 * {@link android.media.AudioManager} for handling management of volume, 1201 * ringer modes and audio routing. 1202 * 1203 * @see #getSystemService 1204 * @see android.media.AudioManager 1205 */ 1206 public static final String AUDIO_SERVICE = "audio"; 1207 1208 /** 1209 * Use with {@link #getSystemService} to retrieve a 1210 * {@link android.telephony.TelephonyManager} for handling management the 1211 * telephony features of the device. 1212 * 1213 * @see #getSystemService 1214 * @see android.telephony.TelephonyManager 1215 */ 1216 public static final String TELEPHONY_SERVICE = "phone"; 1217 1218 /** 1219 * Use with {@link #getSystemService} to retrieve a 1220 * {@link android.text.ClipboardManager} for accessing and modifying 1221 * the contents of the global clipboard. 1222 * 1223 * @see #getSystemService 1224 * @see android.text.ClipboardManager 1225 */ 1226 public static final String CLIPBOARD_SERVICE = "clipboard"; 1227 1228 /** 1229 * Use with {@link #getSystemService} to retrieve a 1230 * {@link android.view.inputmethod.InputMethodManager} for accessing input 1231 * methods. 1232 * 1233 * @see #getSystemService 1234 */ 1235 public static final String INPUT_METHOD_SERVICE = "input_method"; 1236 1237 /** 1238 * Use with {@link #getSystemService} to retrieve a 1239 * {@blink android.appwidget.AppWidgetManager} for accessing AppWidgets. 1240 * 1241 * @hide 1242 * @see #getSystemService 1243 */ 1244 public static final String APPWIDGET_SERVICE = "appwidget"; 1245 1246 /** 1247 * Use with {@link #getSystemService} to retrieve an 1248 * {@blink android.backup.IBackupManager IBackupManager} for communicating 1249 * with the backup mechanism. 1250 * @hide 1251 * 1252 * @see #getSystemService 1253 */ 1254 public static final String BACKUP_SERVICE = "backup"; 1255 1256 /** 1257 * Determine whether the given permission is allowed for a particular 1258 * process and user ID running in the system. 1259 * 1260 * @param permission The name of the permission being checked. 1261 * @param pid The process ID being checked against. Must be > 0. 1262 * @param uid The user ID being checked against. A uid of 0 is the root 1263 * user, which will pass every permission check. 1264 * 1265 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the given 1266 * pid/uid is allowed that permission, or 1267 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1268 * 1269 * @see PackageManager#checkPermission(String, String) 1270 * @see #checkCallingPermission 1271 */ 1272 public abstract int checkPermission(String permission, int pid, int uid); 1273 1274 /** 1275 * Determine whether the calling process of an IPC you are handling has been 1276 * granted a particular permission. This is basically the same as calling 1277 * {@link #checkPermission(String, int, int)} with the pid and uid returned 1278 * by {@link android.os.Binder#getCallingPid} and 1279 * {@link android.os.Binder#getCallingUid}. One important difference 1280 * is that if you are not currently processing an IPC, this function 1281 * will always fail. This is done to protect against accidentally 1282 * leaking permissions; you can use {@link #checkCallingOrSelfPermission} 1283 * to avoid this protection. 1284 * 1285 * @param permission The name of the permission being checked. 1286 * 1287 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the calling 1288 * pid/uid is allowed that permission, or 1289 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1290 * 1291 * @see PackageManager#checkPermission(String, String) 1292 * @see #checkPermission 1293 * @see #checkCallingOrSelfPermission 1294 */ 1295 public abstract int checkCallingPermission(String permission); 1296 1297 /** 1298 * Determine whether the calling process of an IPC <em>or you</em> have been 1299 * granted a particular permission. This is the same as 1300 * {@link #checkCallingPermission}, except it grants your own permissions 1301 * if you are not currently processing an IPC. Use with care! 1302 * 1303 * @param permission The name of the permission being checked. 1304 * 1305 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the calling 1306 * pid/uid is allowed that permission, or 1307 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1308 * 1309 * @see PackageManager#checkPermission(String, String) 1310 * @see #checkPermission 1311 * @see #checkCallingPermission 1312 */ 1313 public abstract int checkCallingOrSelfPermission(String permission); 1314 1315 /** 1316 * If the given permission is not allowed for a particular process 1317 * and user ID running in the system, throw a {@link SecurityException}. 1318 * 1319 * @param permission The name of the permission being checked. 1320 * @param pid The process ID being checked against. Must be > 0. 1321 * @param uid The user ID being checked against. A uid of 0 is the root 1322 * user, which will pass every permission check. 1323 * @param message A message to include in the exception if it is thrown. 1324 * 1325 * @see #checkPermission(String, int, int) 1326 */ 1327 public abstract void enforcePermission( 1328 String permission, int pid, int uid, String message); 1329 1330 /** 1331 * If the calling process of an IPC you are handling has not been 1332 * granted a particular permission, throw a {@link 1333 * SecurityException}. This is basically the same as calling 1334 * {@link #enforcePermission(String, int, int, String)} with the 1335 * pid and uid returned by {@link android.os.Binder#getCallingPid} 1336 * and {@link android.os.Binder#getCallingUid}. One important 1337 * difference is that if you are not currently processing an IPC, 1338 * this function will always throw the SecurityException. This is 1339 * done to protect against accidentally leaking permissions; you 1340 * can use {@link #enforceCallingOrSelfPermission} to avoid this 1341 * protection. 1342 * 1343 * @param permission The name of the permission being checked. 1344 * @param message A message to include in the exception if it is thrown. 1345 * 1346 * @see #checkCallingPermission(String) 1347 */ 1348 public abstract void enforceCallingPermission( 1349 String permission, String message); 1350 1351 /** 1352 * If neither you nor the calling process of an IPC you are 1353 * handling has been granted a particular permission, throw a 1354 * {@link SecurityException}. This is the same as {@link 1355 * #enforceCallingPermission}, except it grants your own 1356 * permissions if you are not currently processing an IPC. Use 1357 * with care! 1358 * 1359 * @param permission The name of the permission being checked. 1360 * @param message A message to include in the exception if it is thrown. 1361 * 1362 * @see #checkCallingOrSelfPermission(String) 1363 */ 1364 public abstract void enforceCallingOrSelfPermission( 1365 String permission, String message); 1366 1367 /** 1368 * Grant permission to access a specific Uri to another package, regardless 1369 * of whether that package has general permission to access the Uri's 1370 * content provider. This can be used to grant specific, temporary 1371 * permissions, typically in response to user interaction (such as the 1372 * user opening an attachment that you would like someone else to 1373 * display). 1374 * 1375 * <p>Normally you should use {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1376 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1377 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1378 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} with the Intent being used to 1379 * start an activity instead of this function directly. If you use this 1380 * function directly, you should be sure to call 1381 * {@link #revokeUriPermission} when the target should no longer be allowed 1382 * to access it. 1383 * 1384 * <p>To succeed, the content provider owning the Uri must have set the 1385 * {@link android.R.styleable#AndroidManifestProvider_grantUriPermissions 1386 * grantUriPermissions} attribute in its manifest or included the 1387 * {@link android.R.styleable#AndroidManifestGrantUriPermission 1388 * <grant-uri-permissions>} tag. 1389 * 1390 * @param toPackage The package you would like to allow to access the Uri. 1391 * @param uri The Uri you would like to grant access to. 1392 * @param modeFlags The desired access modes. Any combination of 1393 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1394 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1395 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1396 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1397 * 1398 * @see #revokeUriPermission 1399 */ 1400 public abstract void grantUriPermission(String toPackage, Uri uri, 1401 int modeFlags); 1402 1403 /** 1404 * Remove all permissions to access a particular content provider Uri 1405 * that were previously added with {@link #grantUriPermission}. The given 1406 * Uri will match all previously granted Uris that are the same or a 1407 * sub-path of the given Uri. That is, revoking "content://foo/one" will 1408 * revoke both "content://foo/target" and "content://foo/target/sub", but not 1409 * "content://foo". 1410 * 1411 * @param uri The Uri you would like to revoke access to. 1412 * @param modeFlags The desired access modes. Any combination of 1413 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 1414 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1415 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 1416 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1417 * 1418 * @see #grantUriPermission 1419 */ 1420 public abstract void revokeUriPermission(Uri uri, int modeFlags); 1421 1422 /** 1423 * Determine whether a particular process and user ID has been granted 1424 * permission to access a specific URI. This only checks for permissions 1425 * that have been explicitly granted -- if the given process/uid has 1426 * more general access to the URI's content provider then this check will 1427 * always fail. 1428 * 1429 * @param uri The uri that is being checked. 1430 * @param pid The process ID being checked against. Must be > 0. 1431 * @param uid The user ID being checked against. A uid of 0 is the root 1432 * user, which will pass every permission check. 1433 * @param modeFlags The type of access to grant. May be one or both of 1434 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1435 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1436 * 1437 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the given 1438 * pid/uid is allowed to access that uri, or 1439 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1440 * 1441 * @see #checkCallingUriPermission 1442 */ 1443 public abstract int checkUriPermission(Uri uri, int pid, int uid, int modeFlags); 1444 1445 /** 1446 * Determine whether the calling process and user ID has been 1447 * granted permission to access a specific URI. This is basically 1448 * the same as calling {@link #checkUriPermission(Uri, int, int, 1449 * int)} with the pid and uid returned by {@link 1450 * android.os.Binder#getCallingPid} and {@link 1451 * android.os.Binder#getCallingUid}. One important difference is 1452 * that if you are not currently processing an IPC, this function 1453 * will always fail. 1454 * 1455 * @param uri The uri that is being checked. 1456 * @param modeFlags The type of access to grant. May be one or both of 1457 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1458 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1459 * 1460 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 1461 * is allowed to access that uri, or 1462 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1463 * 1464 * @see #checkUriPermission(Uri, int, int, int) 1465 */ 1466 public abstract int checkCallingUriPermission(Uri uri, int modeFlags); 1467 1468 /** 1469 * Determine whether the calling process of an IPC <em>or you</em> has been granted 1470 * permission to access a specific URI. This is the same as 1471 * {@link #checkCallingUriPermission}, except it grants your own permissions 1472 * if you are not currently processing an IPC. Use with care! 1473 * 1474 * @param uri The uri that is being checked. 1475 * @param modeFlags The type of access to grant. May be one or both of 1476 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1477 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1478 * 1479 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 1480 * is allowed to access that uri, or 1481 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1482 * 1483 * @see #checkCallingUriPermission 1484 */ 1485 public abstract int checkCallingOrSelfUriPermission(Uri uri, int modeFlags); 1486 1487 /** 1488 * Check both a Uri and normal permission. This allows you to perform 1489 * both {@link #checkPermission} and {@link #checkUriPermission} in one 1490 * call. 1491 * 1492 * @param uri The Uri whose permission is to be checked, or null to not 1493 * do this check. 1494 * @param readPermission The permission that provides overall read access, 1495 * or null to not do this check. 1496 * @param writePermission The permission that provides overall write 1497 * acess, or null to not do this check. 1498 * @param pid The process ID being checked against. Must be > 0. 1499 * @param uid The user ID being checked against. A uid of 0 is the root 1500 * user, which will pass every permission check. 1501 * @param modeFlags The type of access to grant. May be one or both of 1502 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1503 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1504 * 1505 * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller 1506 * is allowed to access that uri or holds one of the given permissions, or 1507 * {@link PackageManager#PERMISSION_DENIED} if it is not. 1508 */ 1509 public abstract int checkUriPermission(Uri uri, String readPermission, 1510 String writePermission, int pid, int uid, int modeFlags); 1511 1512 /** 1513 * If a particular process and user ID has not been granted 1514 * permission to access a specific URI, throw {@link 1515 * SecurityException}. This only checks for permissions that have 1516 * been explicitly granted -- if the given process/uid has more 1517 * general access to the URI's content provider then this check 1518 * will always fail. 1519 * 1520 * @param uri The uri that is being checked. 1521 * @param pid The process ID being checked against. Must be > 0. 1522 * @param uid The user ID being checked against. A uid of 0 is the root 1523 * user, which will pass every permission check. 1524 * @param modeFlags The type of access to grant. May be one or both of 1525 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1526 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1527 * @param message A message to include in the exception if it is thrown. 1528 * 1529 * @see #checkUriPermission(Uri, int, int, int) 1530 */ 1531 public abstract void enforceUriPermission( 1532 Uri uri, int pid, int uid, int modeFlags, String message); 1533 1534 /** 1535 * If the calling process and user ID has not been granted 1536 * permission to access a specific URI, throw {@link 1537 * SecurityException}. This is basically the same as calling 1538 * {@link #enforceUriPermission(Uri, int, int, int, String)} with 1539 * the pid and uid returned by {@link 1540 * android.os.Binder#getCallingPid} and {@link 1541 * android.os.Binder#getCallingUid}. One important difference is 1542 * that if you are not currently processing an IPC, this function 1543 * will always throw a SecurityException. 1544 * 1545 * @param uri The uri that is being checked. 1546 * @param modeFlags The type of access to grant. May be one or both of 1547 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1548 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1549 * @param message A message to include in the exception if it is thrown. 1550 * 1551 * @see #checkCallingUriPermission(Uri, int) 1552 */ 1553 public abstract void enforceCallingUriPermission( 1554 Uri uri, int modeFlags, String message); 1555 1556 /** 1557 * If the calling process of an IPC <em>or you</em> has not been 1558 * granted permission to access a specific URI, throw {@link 1559 * SecurityException}. This is the same as {@link 1560 * #enforceCallingUriPermission}, except it grants your own 1561 * permissions if you are not currently processing an IPC. Use 1562 * with care! 1563 * 1564 * @param uri The uri that is being checked. 1565 * @param modeFlags The type of access to grant. May be one or both of 1566 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1567 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1568 * @param message A message to include in the exception if it is thrown. 1569 * 1570 * @see #checkCallingOrSelfUriPermission(Uri, int) 1571 */ 1572 public abstract void enforceCallingOrSelfUriPermission( 1573 Uri uri, int modeFlags, String message); 1574 1575 /** 1576 * Enforce both a Uri and normal permission. This allows you to perform 1577 * both {@link #enforcePermission} and {@link #enforceUriPermission} in one 1578 * call. 1579 * 1580 * @param uri The Uri whose permission is to be checked, or null to not 1581 * do this check. 1582 * @param readPermission The permission that provides overall read access, 1583 * or null to not do this check. 1584 * @param writePermission The permission that provides overall write 1585 * acess, or null to not do this check. 1586 * @param pid The process ID being checked against. Must be > 0. 1587 * @param uid The user ID being checked against. A uid of 0 is the root 1588 * user, which will pass every permission check. 1589 * @param modeFlags The type of access to grant. May be one or both of 1590 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 1591 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 1592 * @param message A message to include in the exception if it is thrown. 1593 * 1594 * @see #checkUriPermission(Uri, String, String, int, int, int) 1595 */ 1596 public abstract void enforceUriPermission( 1597 Uri uri, String readPermission, String writePermission, 1598 int pid, int uid, int modeFlags, String message); 1599 1600 /** 1601 * Flag for use with {@link #createPackageContext}: include the application 1602 * code with the context. This means loading code into the caller's 1603 * process, so that {@link #getClassLoader()} can be used to instantiate 1604 * the application's classes. Setting this flags imposes security 1605 * restrictions on what application context you can access; if the 1606 * requested application can not be safely loaded into your process, 1607 * java.lang.SecurityException will be thrown. If this flag is not set, 1608 * there will be no restrictions on the packages that can be loaded, 1609 * but {@link #getClassLoader} will always return the default system 1610 * class loader. 1611 */ 1612 public static final int CONTEXT_INCLUDE_CODE = 0x00000001; 1613 1614 /** 1615 * Flag for use with {@link #createPackageContext}: ignore any security 1616 * restrictions on the Context being requested, allowing it to always 1617 * be loaded. For use with {@link #CONTEXT_INCLUDE_CODE} to allow code 1618 * to be loaded into a process even when it isn't safe to do so. Use 1619 * with extreme care! 1620 */ 1621 public static final int CONTEXT_IGNORE_SECURITY = 0x00000002; 1622 1623 /** 1624 * Flag for use with {@link #createPackageContext}: a restricted context may 1625 * disable specific features. For instance, a View associated with a restricted 1626 * context would ignore particular XML attributes. 1627 */ 1628 public static final int CONTEXT_RESTRICTED = 0x00000004; 1629 1630 /** 1631 * Return a new Context object for the given application name. This 1632 * Context is the same as what the named application gets when it is 1633 * launched, containing the same resources and class loader. Each call to 1634 * this method returns a new instance of a Context object; Context objects 1635 * are not shared, however they share common state (Resources, ClassLoader, 1636 * etc) so the Context instance itself is fairly lightweight. 1637 * 1638 * <p>Throws {@link PackageManager.NameNotFoundException} if there is no 1639 * application with the given package name. 1640 * 1641 * <p>Throws {@link java.lang.SecurityException} if the Context requested 1642 * can not be loaded into the caller's process for security reasons (see 1643 * {@link #CONTEXT_INCLUDE_CODE} for more information}. 1644 * 1645 * @param packageName Name of the application's package. 1646 * @param flags Option flags, one of {@link #CONTEXT_INCLUDE_CODE} 1647 * or {@link #CONTEXT_IGNORE_SECURITY}. 1648 * 1649 * @return A Context for the application. 1650 * 1651 * @throws java.lang.SecurityException 1652 * @throws PackageManager.NameNotFoundException if there is no application with 1653 * the given package name 1654 */ 1655 public abstract Context createPackageContext(String packageName, 1656 int flags) throws PackageManager.NameNotFoundException; 1657 1658 /** 1659 * Indicates whether this Context is restricted. 1660 * 1661 * @return True if this Context is restricted, false otherwise. 1662 * 1663 * @see #CONTEXT_RESTRICTED 1664 */ 1665 public boolean isRestricted() { 1666 return false; 1667 } 1668} 1669