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