Context.java revision 82142c21dd333307682d5f4bb09de3ab3ccfa06c
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.annotation.IntDef; 20import android.annotation.NonNull; 21import android.annotation.Nullable; 22import android.annotation.StringDef; 23import android.content.pm.ApplicationInfo; 24import android.content.pm.PackageManager; 25import android.content.res.AssetManager; 26import android.content.res.Configuration; 27import android.content.res.Resources; 28import android.content.res.TypedArray; 29import android.database.DatabaseErrorHandler; 30import android.database.sqlite.SQLiteDatabase; 31import android.database.sqlite.SQLiteDatabase.CursorFactory; 32import android.graphics.Bitmap; 33import android.graphics.drawable.Drawable; 34import android.media.MediaScannerConnection.OnScanCompletedListener; 35import android.net.Uri; 36import android.os.Bundle; 37import android.os.Environment; 38import android.os.Handler; 39import android.os.Looper; 40import android.os.StatFs; 41import android.os.UserHandle; 42import android.os.UserManager; 43import android.util.AttributeSet; 44import android.view.DisplayAdjustments; 45import android.view.Display; 46import android.view.WindowManager; 47 48import java.io.File; 49import java.io.FileInputStream; 50import java.io.FileNotFoundException; 51import java.io.FileOutputStream; 52import java.io.IOException; 53import java.io.InputStream; 54import java.lang.annotation.Retention; 55import java.lang.annotation.RetentionPolicy; 56 57/** 58 * Interface to global information about an application environment. This is 59 * an abstract class whose implementation is provided by 60 * the Android system. It 61 * allows access to application-specific resources and classes, as well as 62 * up-calls for application-level operations such as launching activities, 63 * broadcasting and receiving intents, etc. 64 */ 65public abstract class Context { 66 /** 67 * File creation mode: the default mode, where the created file can only 68 * be accessed by the calling application (or all applications sharing the 69 * same user ID). 70 * @see #MODE_WORLD_READABLE 71 * @see #MODE_WORLD_WRITEABLE 72 */ 73 public static final int MODE_PRIVATE = 0x0000; 74 /** 75 * @deprecated Creating world-readable files is very dangerous, and likely 76 * to cause security holes in applications. It is strongly discouraged; 77 * instead, applications should use more formal mechanism for interactions 78 * such as {@link ContentProvider}, {@link BroadcastReceiver}, and 79 * {@link android.app.Service}. There are no guarantees that this 80 * access mode will remain on a file, such as when it goes through a 81 * backup and restore. 82 * File creation mode: allow all other applications to have read access 83 * to the created file. 84 * @see #MODE_PRIVATE 85 * @see #MODE_WORLD_WRITEABLE 86 */ 87 @Deprecated 88 public static final int MODE_WORLD_READABLE = 0x0001; 89 /** 90 * @deprecated Creating world-writable files is very dangerous, and likely 91 * to cause security holes in applications. It is strongly discouraged; 92 * instead, applications should use more formal mechanism for interactions 93 * such as {@link ContentProvider}, {@link BroadcastReceiver}, and 94 * {@link android.app.Service}. There are no guarantees that this 95 * access mode will remain on a file, such as when it goes through a 96 * backup and restore. 97 * File creation mode: allow all other applications to have write access 98 * to the created file. 99 * @see #MODE_PRIVATE 100 * @see #MODE_WORLD_READABLE 101 */ 102 @Deprecated 103 public static final int MODE_WORLD_WRITEABLE = 0x0002; 104 /** 105 * File creation mode: for use with {@link #openFileOutput}, if the file 106 * already exists then write data to the end of the existing file 107 * instead of erasing it. 108 * @see #openFileOutput 109 */ 110 public static final int MODE_APPEND = 0x8000; 111 112 /** 113 * SharedPreference loading flag: when set, the file on disk will 114 * be checked for modification even if the shared preferences 115 * instance is already loaded in this process. This behavior is 116 * sometimes desired in cases where the application has multiple 117 * processes, all writing to the same SharedPreferences file. 118 * Generally there are better forms of communication between 119 * processes, though. 120 * 121 * <p>This was the legacy (but undocumented) behavior in and 122 * before Gingerbread (Android 2.3) and this flag is implied when 123 * targetting such releases. For applications targetting SDK 124 * versions <em>greater than</em> Android 2.3, this flag must be 125 * explicitly set if desired. 126 * 127 * @see #getSharedPreferences 128 */ 129 public static final int MODE_MULTI_PROCESS = 0x0004; 130 131 /** 132 * Database open flag: when set, the database is opened with write-ahead 133 * logging enabled by default. 134 * 135 * @see #openOrCreateDatabase(String, int, CursorFactory) 136 * @see #openOrCreateDatabase(String, int, CursorFactory, DatabaseErrorHandler) 137 * @see SQLiteDatabase#enableWriteAheadLogging 138 */ 139 public static final int MODE_ENABLE_WRITE_AHEAD_LOGGING = 0x0008; 140 141 /** @hide */ 142 @IntDef(flag = true, 143 value = { 144 BIND_AUTO_CREATE, 145 BIND_AUTO_CREATE, 146 BIND_DEBUG_UNBIND, 147 BIND_NOT_FOREGROUND, 148 BIND_ABOVE_CLIENT, 149 BIND_ALLOW_OOM_MANAGEMENT, 150 BIND_WAIVE_PRIORITY 151 }) 152 @Retention(RetentionPolicy.SOURCE) 153 public @interface BindServiceFlags {} 154 155 /** 156 * Flag for {@link #bindService}: automatically create the service as long 157 * as the binding exists. Note that while this will create the service, 158 * its {@link android.app.Service#onStartCommand} 159 * method will still only be called due to an 160 * explicit call to {@link #startService}. Even without that, though, 161 * this still provides you with access to the service object while the 162 * service is created. 163 * 164 * <p>Note that prior to {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, 165 * not supplying this flag would also impact how important the system 166 * consider's the target service's process to be. When set, the only way 167 * for it to be raised was by binding from a service in which case it will 168 * only be important when that activity is in the foreground. Now to 169 * achieve this behavior you must explicitly supply the new flag 170 * {@link #BIND_ADJUST_WITH_ACTIVITY}. For compatibility, old applications 171 * that don't specify {@link #BIND_AUTO_CREATE} will automatically have 172 * the flags {@link #BIND_WAIVE_PRIORITY} and 173 * {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve 174 * the same result. 175 */ 176 public static final int BIND_AUTO_CREATE = 0x0001; 177 178 /** 179 * Flag for {@link #bindService}: include debugging help for mismatched 180 * calls to unbind. When this flag is set, the callstack of the following 181 * {@link #unbindService} call is retained, to be printed if a later 182 * incorrect unbind call is made. Note that doing this requires retaining 183 * information about the binding that was made for the lifetime of the app, 184 * resulting in a leak -- this should only be used for debugging. 185 */ 186 public static final int BIND_DEBUG_UNBIND = 0x0002; 187 188 /** 189 * Flag for {@link #bindService}: don't allow this binding to raise 190 * the target service's process to the foreground scheduling priority. 191 * It will still be raised to at least the same memory priority 192 * as the client (so that its process will not be killable in any 193 * situation where the client is not killable), but for CPU scheduling 194 * purposes it may be left in the background. This only has an impact 195 * in the situation where the binding client is a foreground process 196 * and the target service is in a background process. 197 */ 198 public static final int BIND_NOT_FOREGROUND = 0x0004; 199 200 /** 201 * Flag for {@link #bindService}: indicates that the client application 202 * binding to this service considers the service to be more important than 203 * the app itself. When set, the platform will try to have the out of 204 * memory killer kill the app before it kills the service it is bound to, though 205 * this is not guaranteed to be the case. 206 */ 207 public static final int BIND_ABOVE_CLIENT = 0x0008; 208 209 /** 210 * Flag for {@link #bindService}: allow the process hosting the bound 211 * service to go through its normal memory management. It will be 212 * treated more like a running service, allowing the system to 213 * (temporarily) expunge the process if low on memory or for some other 214 * whim it may have, and being more aggressive about making it a candidate 215 * to be killed (and restarted) if running for a long time. 216 */ 217 public static final int BIND_ALLOW_OOM_MANAGEMENT = 0x0010; 218 219 /** 220 * Flag for {@link #bindService}: don't impact the scheduling or 221 * memory management priority of the target service's hosting process. 222 * Allows the service's process to be managed on the background LRU list 223 * just like a regular application process in the background. 224 */ 225 public static final int BIND_WAIVE_PRIORITY = 0x0020; 226 227 /** 228 * Flag for {@link #bindService}: this service is very important to 229 * the client, so should be brought to the foreground process level 230 * when the client is. Normally a process can only be raised to the 231 * visibility level by a client, even if that client is in the foreground. 232 */ 233 public static final int BIND_IMPORTANT = 0x0040; 234 235 /** 236 * Flag for {@link #bindService}: If binding from an activity, allow the 237 * target service's process importance to be raised based on whether the 238 * activity is visible to the user, regardless whether another flag is 239 * used to reduce the amount that the client process's overall importance 240 * is used to impact it. 241 */ 242 public static final int BIND_ADJUST_WITH_ACTIVITY = 0x0080; 243 244 /** 245 * @hide Flag for {@link #bindService}: Treat the binding as hosting 246 * an activity, an unbinding as the activity going in the background. 247 * That is, when unbinding, the process when empty will go on the activity 248 * LRU list instead of the regular one, keeping it around more aggressively 249 * than it otherwise would be. This is intended for use with IMEs to try 250 * to keep IME processes around for faster keyboard switching. 251 */ 252 public static final int BIND_TREAT_LIKE_ACTIVITY = 0x08000000; 253 254 /** 255 * @hide An idea that is not yet implemented. 256 * Flag for {@link #bindService}: If binding from an activity, consider 257 * this service to be visible like the binding activity is. That is, 258 * it will be treated as something more important to keep around than 259 * invisible background activities. This will impact the number of 260 * recent activities the user can switch between without having them 261 * restart. There is no guarantee this will be respected, as the system 262 * tries to balance such requests from one app vs. the importantance of 263 * keeping other apps around. 264 */ 265 public static final int BIND_VISIBLE = 0x10000000; 266 267 /** 268 * @hide 269 * Flag for {@link #bindService}: Consider this binding to be causing the target 270 * process to be showing UI, so it will be do a UI_HIDDEN memory trim when it goes 271 * away. 272 */ 273 public static final int BIND_SHOWING_UI = 0x20000000; 274 275 /** 276 * Flag for {@link #bindService}: Don't consider the bound service to be 277 * visible, even if the caller is visible. 278 * @hide 279 */ 280 public static final int BIND_NOT_VISIBLE = 0x40000000; 281 282 /** Return an AssetManager instance for your application's package. */ 283 public abstract AssetManager getAssets(); 284 285 /** Return a Resources instance for your application's package. */ 286 public abstract Resources getResources(); 287 288 /** Return PackageManager instance to find global package information. */ 289 public abstract PackageManager getPackageManager(); 290 291 /** Return a ContentResolver instance for your application's package. */ 292 public abstract ContentResolver getContentResolver(); 293 294 /** 295 * Return the Looper for the main thread of the current process. This is 296 * the thread used to dispatch calls to application components (activities, 297 * services, etc). 298 * <p> 299 * By definition, this method returns the same result as would be obtained 300 * by calling {@link Looper#getMainLooper() Looper.getMainLooper()}. 301 * </p> 302 * 303 * @return The main looper. 304 */ 305 public abstract Looper getMainLooper(); 306 307 /** 308 * Return the context of the single, global Application object of the 309 * current process. This generally should only be used if you need a 310 * Context whose lifecycle is separate from the current context, that is 311 * tied to the lifetime of the process rather than the current component. 312 * 313 * <p>Consider for example how this interacts with 314 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)}: 315 * <ul> 316 * <li> <p>If used from an Activity context, the receiver is being registered 317 * within that activity. This means that you are expected to unregister 318 * before the activity is done being destroyed; in fact if you do not do 319 * so, the framework will clean up your leaked registration as it removes 320 * the activity and log an error. Thus, if you use the Activity context 321 * to register a receiver that is static (global to the process, not 322 * associated with an Activity instance) then that registration will be 323 * removed on you at whatever point the activity you used is destroyed. 324 * <li> <p>If used from the Context returned here, the receiver is being 325 * registered with the global state associated with your application. Thus 326 * it will never be unregistered for you. This is necessary if the receiver 327 * is associated with static data, not a particular component. However 328 * using the ApplicationContext elsewhere can easily lead to serious leaks 329 * if you forget to unregister, unbind, etc. 330 * </ul> 331 */ 332 public abstract Context getApplicationContext(); 333 334 /** 335 * Add a new {@link ComponentCallbacks} to the base application of the 336 * Context, which will be called at the same times as the ComponentCallbacks 337 * methods of activities and other components are called. Note that you 338 * <em>must</em> be sure to use {@link #unregisterComponentCallbacks} when 339 * appropriate in the future; this will not be removed for you. 340 * 341 * @param callback The interface to call. This can be either a 342 * {@link ComponentCallbacks} or {@link ComponentCallbacks2} interface. 343 */ 344 public void registerComponentCallbacks(ComponentCallbacks callback) { 345 getApplicationContext().registerComponentCallbacks(callback); 346 } 347 348 /** 349 * Remove a {@link ComponentCallbacks} object that was previously registered 350 * with {@link #registerComponentCallbacks(ComponentCallbacks)}. 351 */ 352 public void unregisterComponentCallbacks(ComponentCallbacks callback) { 353 getApplicationContext().unregisterComponentCallbacks(callback); 354 } 355 356 /** 357 * Return a localized, styled CharSequence from the application's package's 358 * default string table. 359 * 360 * @param resId Resource id for the CharSequence text 361 */ 362 public final CharSequence getText(int resId) { 363 return getResources().getText(resId); 364 } 365 366 /** 367 * Return a localized string from the application's package's 368 * default string table. 369 * 370 * @param resId Resource id for the string 371 */ 372 public final String getString(int resId) { 373 return getResources().getString(resId); 374 } 375 376 /** 377 * Return a localized formatted string from the application's package's 378 * default string table, substituting the format arguments as defined in 379 * {@link java.util.Formatter} and {@link java.lang.String#format}. 380 * 381 * @param resId Resource id for the format string 382 * @param formatArgs The format arguments that will be used for substitution. 383 */ 384 385 public final String getString(int resId, Object... formatArgs) { 386 return getResources().getString(resId, formatArgs); 387 } 388 389 /** 390 * Return a drawable object associated with a particular resource ID and 391 * styled for the current theme. 392 * 393 * @param id The desired resource identifier, as generated by the aapt 394 * tool. This integer encodes the package, type, and resource 395 * entry. The value 0 is an invalid identifier. 396 * @return Drawable An object that can be used to draw this resource. 397 */ 398 public final Drawable getDrawable(int id) { 399 return getResources().getDrawable(id, getTheme()); 400 } 401 402 /** 403 * Set the base theme for this context. Note that this should be called 404 * before any views are instantiated in the Context (for example before 405 * calling {@link android.app.Activity#setContentView} or 406 * {@link android.view.LayoutInflater#inflate}). 407 * 408 * @param resid The style resource describing the theme. 409 */ 410 public abstract void setTheme(int resid); 411 412 /** @hide Needed for some internal implementation... not public because 413 * you can't assume this actually means anything. */ 414 public int getThemeResId() { 415 return 0; 416 } 417 418 /** 419 * Return the Theme object associated with this Context. 420 */ 421 public abstract Resources.Theme getTheme(); 422 423 /** 424 * Retrieve styled attribute information in this Context's theme. See 425 * {@link Resources.Theme#obtainStyledAttributes(int[])} 426 * for more information. 427 * 428 * @see Resources.Theme#obtainStyledAttributes(int[]) 429 */ 430 public final TypedArray obtainStyledAttributes( 431 int[] attrs) { 432 return getTheme().obtainStyledAttributes(attrs); 433 } 434 435 /** 436 * Retrieve styled attribute information in this Context's theme. See 437 * {@link Resources.Theme#obtainStyledAttributes(int, int[])} 438 * for more information. 439 * 440 * @see Resources.Theme#obtainStyledAttributes(int, int[]) 441 */ 442 public final TypedArray obtainStyledAttributes( 443 int resid, int[] attrs) throws Resources.NotFoundException { 444 return getTheme().obtainStyledAttributes(resid, attrs); 445 } 446 447 /** 448 * Retrieve styled attribute information in this Context's theme. See 449 * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} 450 * for more information. 451 * 452 * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 453 */ 454 public final TypedArray obtainStyledAttributes( 455 AttributeSet set, int[] attrs) { 456 return getTheme().obtainStyledAttributes(set, attrs, 0, 0); 457 } 458 459 /** 460 * Retrieve styled attribute information in this Context's theme. See 461 * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} 462 * for more information. 463 * 464 * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 465 */ 466 public final TypedArray obtainStyledAttributes( 467 AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) { 468 return getTheme().obtainStyledAttributes( 469 set, attrs, defStyleAttr, defStyleRes); 470 } 471 472 /** 473 * Return a class loader you can use to retrieve classes in this package. 474 */ 475 public abstract ClassLoader getClassLoader(); 476 477 /** Return the name of this application's package. */ 478 public abstract String getPackageName(); 479 480 /** @hide Return the name of the base context this context is derived from. */ 481 public abstract String getBasePackageName(); 482 483 /** @hide Return the package name that should be used for app ops calls from 484 * this context. This is the same as {@link #getBasePackageName()} except in 485 * cases where system components are loaded into other app processes, in which 486 * case this will be the name of the primary package in that process (so that app 487 * ops uid verification will work with the name). */ 488 public abstract String getOpPackageName(); 489 490 /** Return the full application info for this context's package. */ 491 public abstract ApplicationInfo getApplicationInfo(); 492 493 /** 494 * Return the full path to this context's primary Android package. 495 * The Android package is a ZIP file which contains the application's 496 * primary resources. 497 * 498 * <p>Note: this is not generally useful for applications, since they should 499 * not be directly accessing the file system. 500 * 501 * @return String Path to the resources. 502 */ 503 public abstract String getPackageResourcePath(); 504 505 /** 506 * Return the full path to this context's primary Android package. 507 * The Android package is a ZIP file which contains application's 508 * primary code and assets. 509 * 510 * <p>Note: this is not generally useful for applications, since they should 511 * not be directly accessing the file system. 512 * 513 * @return String Path to the code and assets. 514 */ 515 public abstract String getPackageCodePath(); 516 517 /** 518 * {@hide} 519 * Return the full path to the shared prefs file for the given prefs group name. 520 * 521 * <p>Note: this is not generally useful for applications, since they should 522 * not be directly accessing the file system. 523 */ 524 public abstract File getSharedPrefsFile(String name); 525 526 /** 527 * Retrieve and hold the contents of the preferences file 'name', returning 528 * a SharedPreferences through which you can retrieve and modify its 529 * values. Only one instance of the SharedPreferences object is returned 530 * to any callers for the same name, meaning they will see each other's 531 * edits as soon as they are made. 532 * 533 * @param name Desired preferences file. If a preferences file by this name 534 * does not exist, it will be created when you retrieve an 535 * editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). 536 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 537 * default operation, {@link #MODE_WORLD_READABLE} 538 * and {@link #MODE_WORLD_WRITEABLE} to control permissions. The bit 539 * {@link #MODE_MULTI_PROCESS} can also be used if multiple processes 540 * are mutating the same SharedPreferences file. {@link #MODE_MULTI_PROCESS} 541 * is always on in apps targeting Gingerbread (Android 2.3) and below, and 542 * off by default in later versions. 543 * 544 * @return The single {@link SharedPreferences} instance that can be used 545 * to retrieve and modify the preference values. 546 * 547 * @see #MODE_PRIVATE 548 * @see #MODE_WORLD_READABLE 549 * @see #MODE_WORLD_WRITEABLE 550 * @see #MODE_MULTI_PROCESS 551 */ 552 public abstract SharedPreferences getSharedPreferences(String name, 553 int mode); 554 555 /** 556 * Open a private file associated with this Context's application package 557 * for reading. 558 * 559 * @param name The name of the file to open; can not contain path 560 * separators. 561 * 562 * @return The resulting {@link FileInputStream}. 563 * 564 * @see #openFileOutput 565 * @see #fileList 566 * @see #deleteFile 567 * @see java.io.FileInputStream#FileInputStream(String) 568 */ 569 public abstract FileInputStream openFileInput(String name) 570 throws FileNotFoundException; 571 572 /** 573 * Open a private file associated with this Context's application package 574 * for writing. Creates the file if it doesn't already exist. 575 * 576 * @param name The name of the file to open; can not contain path 577 * separators. 578 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 579 * default operation, {@link #MODE_APPEND} to append to an existing file, 580 * {@link #MODE_WORLD_READABLE} and {@link #MODE_WORLD_WRITEABLE} to control 581 * permissions. 582 * 583 * @return The resulting {@link FileOutputStream}. 584 * 585 * @see #MODE_APPEND 586 * @see #MODE_PRIVATE 587 * @see #MODE_WORLD_READABLE 588 * @see #MODE_WORLD_WRITEABLE 589 * @see #openFileInput 590 * @see #fileList 591 * @see #deleteFile 592 * @see java.io.FileOutputStream#FileOutputStream(String) 593 */ 594 public abstract FileOutputStream openFileOutput(String name, int mode) 595 throws FileNotFoundException; 596 597 /** 598 * Delete the given private file associated with this Context's 599 * application package. 600 * 601 * @param name The name of the file to delete; can not contain path 602 * separators. 603 * 604 * @return {@code true} if the file was successfully deleted; else 605 * {@code false}. 606 * 607 * @see #openFileInput 608 * @see #openFileOutput 609 * @see #fileList 610 * @see java.io.File#delete() 611 */ 612 public abstract boolean deleteFile(String name); 613 614 /** 615 * Returns the absolute path on the filesystem where a file created with 616 * {@link #openFileOutput} is stored. 617 * 618 * @param name The name of the file for which you would like to get 619 * its path. 620 * 621 * @return An absolute path to the given file. 622 * 623 * @see #openFileOutput 624 * @see #getFilesDir 625 * @see #getDir 626 */ 627 public abstract File getFileStreamPath(String name); 628 629 /** 630 * Returns the absolute path to the directory on the filesystem where 631 * files created with {@link #openFileOutput} are stored. 632 * 633 * @return The path of the directory holding application files. 634 * 635 * @see #openFileOutput 636 * @see #getFileStreamPath 637 * @see #getDir 638 */ 639 public abstract File getFilesDir(); 640 641 /** 642 * Returns the absolute path to the directory on the primary external filesystem 643 * (that is somewhere on {@link android.os.Environment#getExternalStorageDirectory() 644 * Environment.getExternalStorageDirectory()}) where the application can 645 * place persistent files it owns. These files are internal to the 646 * applications, and not typically visible to the user as media. 647 * 648 * <p>This is like {@link #getFilesDir()} in that these 649 * files will be deleted when the application is uninstalled, however there 650 * are some important differences: 651 * 652 * <ul> 653 * <li>External files are not always available: they will disappear if the 654 * user mounts the external storage on a computer or removes it. See the 655 * APIs on {@link android.os.Environment} for information in the storage state. 656 * <li>There is no security enforced with these files. For example, any application 657 * holding {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 658 * these files. 659 * </ul> 660 * 661 * <p>Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, no permissions 662 * are required to read or write to the returned path; it's always 663 * accessible to the calling app. This only applies to paths generated for 664 * package name of the calling application. To access paths belonging 665 * to other packages, {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} 666 * and/or {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} are required. 667 * 668 * <p>On devices with multiple users (as described by {@link UserManager}), 669 * each user has their own isolated external storage. Applications only 670 * have access to the external storage for the user they're running as.</p> 671 * 672 * <p>Here is an example of typical code to manipulate a file in 673 * an application's private storage:</p> 674 * 675 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java 676 * private_file} 677 * 678 * <p>If you supply a non-null <var>type</var> to this function, the returned 679 * file will be a path to a sub-directory of the given type. Though these files 680 * are not automatically scanned by the media scanner, you can explicitly 681 * add them to the media database with 682 * {@link android.media.MediaScannerConnection#scanFile(Context, String[], String[], 683 * OnScanCompletedListener) MediaScannerConnection.scanFile}. 684 * Note that this is not the same as 685 * {@link android.os.Environment#getExternalStoragePublicDirectory 686 * Environment.getExternalStoragePublicDirectory()}, which provides 687 * directories of media shared by all applications. The 688 * directories returned here are 689 * owned by the application, and their contents will be removed when the 690 * application is uninstalled. Unlike 691 * {@link android.os.Environment#getExternalStoragePublicDirectory 692 * Environment.getExternalStoragePublicDirectory()}, the directory 693 * returned here will be automatically created for you. 694 * 695 * <p>Here is an example of typical code to manipulate a picture in 696 * an application's private storage and add it to the media database:</p> 697 * 698 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java 699 * private_picture} 700 * 701 * @param type The type of files directory to return. May be null for 702 * the root of the files directory or one of 703 * the following Environment constants for a subdirectory: 704 * {@link android.os.Environment#DIRECTORY_MUSIC}, 705 * {@link android.os.Environment#DIRECTORY_PODCASTS}, 706 * {@link android.os.Environment#DIRECTORY_RINGTONES}, 707 * {@link android.os.Environment#DIRECTORY_ALARMS}, 708 * {@link android.os.Environment#DIRECTORY_NOTIFICATIONS}, 709 * {@link android.os.Environment#DIRECTORY_PICTURES}, or 710 * {@link android.os.Environment#DIRECTORY_MOVIES}. 711 * 712 * @return The path of the directory holding application files 713 * on external storage. Returns null if external storage is not currently 714 * mounted so it could not ensure the path exists; you will need to call 715 * this method again when it is available. 716 * 717 * @see #getFilesDir 718 * @see android.os.Environment#getExternalStoragePublicDirectory 719 */ 720 @Nullable 721 public abstract File getExternalFilesDir(@Nullable String type); 722 723 /** 724 * Returns absolute paths to application-specific directories on all 725 * external storage devices where the application can place persistent files 726 * it owns. These files are internal to the application, and not typically 727 * visible to the user as media. 728 * <p> 729 * This is like {@link #getFilesDir()} in that these files will be deleted when 730 * the application is uninstalled, however there are some important differences: 731 * <ul> 732 * <li>External files are not always available: they will disappear if the 733 * user mounts the external storage on a computer or removes it. 734 * <li>There is no security enforced with these files. 735 * </ul> 736 * <p> 737 * External storage devices returned here are considered a permanent part of 738 * the device, including both emulated external storage and physical media 739 * slots, such as SD cards in a battery compartment. The returned paths do 740 * not include transient devices, such as USB flash drives. 741 * <p> 742 * An application may store data on any or all of the returned devices. For 743 * example, an app may choose to store large files on the device with the 744 * most available space, as measured by {@link StatFs}. 745 * <p> 746 * No permissions are required to read or write to the returned paths; they 747 * are always accessible to the calling app. Write access outside of these 748 * paths on secondary external storage devices is not available. 749 * <p> 750 * The first path returned is the same as {@link #getExternalFilesDir(String)}. 751 * Returned paths may be {@code null} if a storage device is unavailable. 752 * 753 * @see #getExternalFilesDir(String) 754 * @see Environment#getExternalStorageState(File) 755 */ 756 public abstract File[] getExternalFilesDirs(String type); 757 758 /** 759 * Return the primary external storage directory where this application's OBB 760 * files (if there are any) can be found. Note if the application does not have 761 * any OBB files, this directory may not exist. 762 * <p> 763 * This is like {@link #getFilesDir()} in that these files will be deleted when 764 * the application is uninstalled, however there are some important differences: 765 * <ul> 766 * <li>External files are not always available: they will disappear if the 767 * user mounts the external storage on a computer or removes it. 768 * <li>There is no security enforced with these files. For example, any application 769 * holding {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 770 * these files. 771 * </ul> 772 * <p> 773 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, no permissions 774 * are required to read or write to the returned path; it's always 775 * accessible to the calling app. This only applies to paths generated for 776 * package name of the calling application. To access paths belonging 777 * to other packages, {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} 778 * and/or {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} are required. 779 * <p> 780 * On devices with multiple users (as described by {@link UserManager}), 781 * multiple users may share the same OBB storage location. Applications 782 * should ensure that multiple instances running under different users don't 783 * interfere with each other. 784 */ 785 public abstract File getObbDir(); 786 787 /** 788 * Returns absolute paths to application-specific directories on all 789 * external storage devices where the application's OBB files (if there are 790 * any) can be found. Note if the application does not have any OBB files, 791 * these directories may not exist. 792 * <p> 793 * This is like {@link #getFilesDir()} in that these files will be deleted when 794 * the application is uninstalled, however there are some important differences: 795 * <ul> 796 * <li>External files are not always available: they will disappear if the 797 * user mounts the external storage on a computer or removes it. 798 * <li>There is no security enforced with these files. 799 * </ul> 800 * <p> 801 * External storage devices returned here are considered a permanent part of 802 * the device, including both emulated external storage and physical media 803 * slots, such as SD cards in a battery compartment. The returned paths do 804 * not include transient devices, such as USB flash drives. 805 * <p> 806 * An application may store data on any or all of the returned devices. For 807 * example, an app may choose to store large files on the device with the 808 * most available space, as measured by {@link StatFs}. 809 * <p> 810 * No permissions are required to read or write to the returned paths; they 811 * are always accessible to the calling app. Write access outside of these 812 * paths on secondary external storage devices is not available. 813 * <p> 814 * The first path returned is the same as {@link #getObbDir()}. 815 * Returned paths may be {@code null} if a storage device is unavailable. 816 * 817 * @see #getObbDir() 818 * @see Environment#getExternalStorageState(File) 819 */ 820 public abstract File[] getObbDirs(); 821 822 /** 823 * Returns the absolute path to the application specific cache directory 824 * on the filesystem. These files will be ones that get deleted first when the 825 * device runs low on storage. 826 * There is no guarantee when these files will be deleted. 827 * 828 * <strong>Note: you should not <em>rely</em> on the system deleting these 829 * files for you; you should always have a reasonable maximum, such as 1 MB, 830 * for the amount of space you consume with cache files, and prune those 831 * files when exceeding that space.</strong> 832 * 833 * @return The path of the directory holding application cache files. 834 * 835 * @see #openFileOutput 836 * @see #getFileStreamPath 837 * @see #getDir 838 */ 839 public abstract File getCacheDir(); 840 841 /** 842 * Returns the absolute path to the directory on the primary external filesystem 843 * (that is somewhere on {@link android.os.Environment#getExternalStorageDirectory() 844 * Environment.getExternalStorageDirectory()} where the application can 845 * place cache files it owns. These files are internal to the application, and 846 * not typically visible to the user as media. 847 * 848 * <p>This is like {@link #getCacheDir()} in that these 849 * files will be deleted when the application is uninstalled, however there 850 * are some important differences: 851 * 852 * <ul> 853 * <li>The platform does not always monitor the space available in external 854 * storage, and thus may not automatically delete these files. Currently 855 * the only time files here will be deleted by the platform is when running 856 * on {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} or later and 857 * {@link android.os.Environment#isExternalStorageEmulated() 858 * Environment.isExternalStorageEmulated()} returns true. Note that you should 859 * be managing the maximum space you will use for these anyway, just like 860 * with {@link #getCacheDir()}. 861 * <li>External files are not always available: they will disappear if the 862 * user mounts the external storage on a computer or removes it. See the 863 * APIs on {@link android.os.Environment} for information in the storage state. 864 * <li>There is no security enforced with these files. For example, any application 865 * holding {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 866 * these files. 867 * </ul> 868 * 869 * <p>Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, no permissions 870 * are required to read or write to the returned path; it's always 871 * accessible to the calling app. This only applies to paths generated for 872 * package name of the calling application. To access paths belonging 873 * to other packages, {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} 874 * and/or {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} are required. 875 * 876 * <p>On devices with multiple users (as described by {@link UserManager}), 877 * each user has their own isolated external storage. Applications only 878 * have access to the external storage for the user they're running as.</p> 879 * 880 * @return The path of the directory holding application cache files 881 * on external storage. Returns null if external storage is not currently 882 * mounted so it could not ensure the path exists; you will need to call 883 * this method again when it is available. 884 * 885 * @see #getCacheDir 886 */ 887 @Nullable 888 public abstract File getExternalCacheDir(); 889 890 /** 891 * Returns absolute paths to application-specific directories on all 892 * external storage devices where the application can place cache files it 893 * owns. These files are internal to the application, and not typically 894 * visible to the user as media. 895 * <p> 896 * This is like {@link #getCacheDir()} in that these files will be deleted when 897 * the application is uninstalled, however there are some important differences: 898 * <ul> 899 * <li>External files are not always available: they will disappear if the 900 * user mounts the external storage on a computer or removes it. 901 * <li>There is no security enforced with these files. 902 * </ul> 903 * <p> 904 * External storage devices returned here are considered a permanent part of 905 * the device, including both emulated external storage and physical media 906 * slots, such as SD cards in a battery compartment. The returned paths do 907 * not include transient devices, such as USB flash drives. 908 * <p> 909 * An application may store data on any or all of the returned devices. For 910 * example, an app may choose to store large files on the device with the 911 * most available space, as measured by {@link StatFs}. 912 * <p> 913 * No permissions are required to read or write to the returned paths; they 914 * are always accessible to the calling app. Write access outside of these 915 * paths on secondary external storage devices is not available. 916 * <p> 917 * The first path returned is the same as {@link #getExternalCacheDir()}. 918 * Returned paths may be {@code null} if a storage device is unavailable. 919 * 920 * @see #getExternalCacheDir() 921 * @see Environment#getExternalStorageState(File) 922 */ 923 public abstract File[] getExternalCacheDirs(); 924 925 /** 926 * Returns an array of strings naming the private files associated with 927 * this Context's application package. 928 * 929 * @return Array of strings naming the private files. 930 * 931 * @see #openFileInput 932 * @see #openFileOutput 933 * @see #deleteFile 934 */ 935 public abstract String[] fileList(); 936 937 /** 938 * Retrieve, creating if needed, a new directory in which the application 939 * can place its own custom data files. You can use the returned File 940 * object to create and access files in this directory. Note that files 941 * created through a File object will only be accessible by your own 942 * application; you can only set the mode of the entire directory, not 943 * of individual files. 944 * 945 * @param name Name of the directory to retrieve. This is a directory 946 * that is created as part of your application data. 947 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 948 * default operation, {@link #MODE_WORLD_READABLE} and 949 * {@link #MODE_WORLD_WRITEABLE} to control permissions. 950 * 951 * @return A {@link File} object for the requested directory. The directory 952 * will have been created if it does not already exist. 953 * 954 * @see #openFileOutput(String, int) 955 */ 956 public abstract File getDir(String name, int mode); 957 958 /** 959 * Open a new private SQLiteDatabase associated with this Context's 960 * application package. Create the database file if it doesn't exist. 961 * 962 * @param name The name (unique in the application package) of the database. 963 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 964 * default operation, {@link #MODE_WORLD_READABLE} 965 * and {@link #MODE_WORLD_WRITEABLE} to control permissions. 966 * Use {@link #MODE_ENABLE_WRITE_AHEAD_LOGGING} to enable write-ahead logging by default. 967 * @param factory An optional factory class that is called to instantiate a 968 * cursor when query is called. 969 * 970 * @return The contents of a newly created database with the given name. 971 * @throws android.database.sqlite.SQLiteException if the database file could not be opened. 972 * 973 * @see #MODE_PRIVATE 974 * @see #MODE_WORLD_READABLE 975 * @see #MODE_WORLD_WRITEABLE 976 * @see #MODE_ENABLE_WRITE_AHEAD_LOGGING 977 * @see #deleteDatabase 978 */ 979 public abstract SQLiteDatabase openOrCreateDatabase(String name, 980 int mode, CursorFactory factory); 981 982 /** 983 * Open a new private SQLiteDatabase associated with this Context's 984 * application package. Creates the database file if it doesn't exist. 985 * 986 * <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be 987 * used to handle corruption when sqlite reports database corruption.</p> 988 * 989 * @param name The name (unique in the application package) of the database. 990 * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the 991 * default operation, {@link #MODE_WORLD_READABLE} 992 * and {@link #MODE_WORLD_WRITEABLE} to control permissions. 993 * Use {@link #MODE_ENABLE_WRITE_AHEAD_LOGGING} to enable write-ahead logging by default. 994 * @param factory An optional factory class that is called to instantiate a 995 * cursor when query is called. 996 * @param errorHandler the {@link DatabaseErrorHandler} to be used when sqlite reports database 997 * corruption. if null, {@link android.database.DefaultDatabaseErrorHandler} is assumed. 998 * @return The contents of a newly created database with the given name. 999 * @throws android.database.sqlite.SQLiteException if the database file could not be opened. 1000 * 1001 * @see #MODE_PRIVATE 1002 * @see #MODE_WORLD_READABLE 1003 * @see #MODE_WORLD_WRITEABLE 1004 * @see #MODE_ENABLE_WRITE_AHEAD_LOGGING 1005 * @see #deleteDatabase 1006 */ 1007 public abstract SQLiteDatabase openOrCreateDatabase(String name, 1008 int mode, CursorFactory factory, 1009 @Nullable DatabaseErrorHandler errorHandler); 1010 1011 /** 1012 * Delete an existing private SQLiteDatabase associated with this Context's 1013 * application package. 1014 * 1015 * @param name The name (unique in the application package) of the 1016 * database. 1017 * 1018 * @return {@code true} if the database was successfully deleted; else {@code false}. 1019 * 1020 * @see #openOrCreateDatabase 1021 */ 1022 public abstract boolean deleteDatabase(String name); 1023 1024 /** 1025 * Returns the absolute path on the filesystem where a database created with 1026 * {@link #openOrCreateDatabase} is stored. 1027 * 1028 * @param name The name of the database for which you would like to get 1029 * its path. 1030 * 1031 * @return An absolute path to the given database. 1032 * 1033 * @see #openOrCreateDatabase 1034 */ 1035 public abstract File getDatabasePath(String name); 1036 1037 /** 1038 * Returns an array of strings naming the private databases associated with 1039 * this Context's application package. 1040 * 1041 * @return Array of strings naming the private databases. 1042 * 1043 * @see #openOrCreateDatabase 1044 * @see #deleteDatabase 1045 */ 1046 public abstract String[] databaseList(); 1047 1048 /** 1049 * @deprecated Use {@link android.app.WallpaperManager#getDrawable 1050 * WallpaperManager.get()} instead. 1051 */ 1052 @Deprecated 1053 public abstract Drawable getWallpaper(); 1054 1055 /** 1056 * @deprecated Use {@link android.app.WallpaperManager#peekDrawable 1057 * WallpaperManager.peek()} instead. 1058 */ 1059 @Deprecated 1060 public abstract Drawable peekWallpaper(); 1061 1062 /** 1063 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumWidth() 1064 * WallpaperManager.getDesiredMinimumWidth()} instead. 1065 */ 1066 @Deprecated 1067 public abstract int getWallpaperDesiredMinimumWidth(); 1068 1069 /** 1070 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumHeight() 1071 * WallpaperManager.getDesiredMinimumHeight()} instead. 1072 */ 1073 @Deprecated 1074 public abstract int getWallpaperDesiredMinimumHeight(); 1075 1076 /** 1077 * @deprecated Use {@link android.app.WallpaperManager#setBitmap(Bitmap) 1078 * WallpaperManager.set()} instead. 1079 * <p>This method requires the caller to hold the permission 1080 * {@link android.Manifest.permission#SET_WALLPAPER}. 1081 */ 1082 @Deprecated 1083 public abstract void setWallpaper(Bitmap bitmap) throws IOException; 1084 1085 /** 1086 * @deprecated Use {@link android.app.WallpaperManager#setStream(InputStream) 1087 * WallpaperManager.set()} instead. 1088 * <p>This method requires the caller to hold the permission 1089 * {@link android.Manifest.permission#SET_WALLPAPER}. 1090 */ 1091 @Deprecated 1092 public abstract void setWallpaper(InputStream data) throws IOException; 1093 1094 /** 1095 * @deprecated Use {@link android.app.WallpaperManager#clear 1096 * WallpaperManager.clear()} instead. 1097 * <p>This method requires the caller to hold the permission 1098 * {@link android.Manifest.permission#SET_WALLPAPER}. 1099 */ 1100 @Deprecated 1101 public abstract void clearWallpaper() throws IOException; 1102 1103 /** 1104 * Same as {@link #startActivity(Intent, Bundle)} with no options 1105 * specified. 1106 * 1107 * @param intent The description of the activity to start. 1108 * 1109 * @throws ActivityNotFoundException 1110 * 1111 * @see #startActivity(Intent, Bundle) 1112 * @see PackageManager#resolveActivity 1113 */ 1114 public abstract void startActivity(Intent intent); 1115 1116 /** 1117 * Version of {@link #startActivity(Intent)} that allows you to specify the 1118 * user the activity will be started for. This is not available to applications 1119 * that are not pre-installed on the system image. Using it requires holding 1120 * the INTERACT_ACROSS_USERS_FULL permission. 1121 * @param intent The description of the activity to start. 1122 * @param user The UserHandle of the user to start this activity for. 1123 * @throws ActivityNotFoundException 1124 * @hide 1125 */ 1126 public void startActivityAsUser(Intent intent, UserHandle user) { 1127 throw new RuntimeException("Not implemented. Must override in a subclass."); 1128 } 1129 1130 /** 1131 * Launch a new activity. You will not receive any information about when 1132 * the activity exits. 1133 * 1134 * <p>Note that if this method is being called from outside of an 1135 * {@link android.app.Activity} Context, then the Intent must include 1136 * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because, 1137 * without being started from an existing Activity, there is no existing 1138 * task in which to place the new activity and thus it needs to be placed 1139 * in its own separate task. 1140 * 1141 * <p>This method throws {@link ActivityNotFoundException} 1142 * if there was no Activity found to run the given Intent. 1143 * 1144 * @param intent The description of the activity to start. 1145 * @param options Additional options for how the Activity should be started. 1146 * May be null if there are no options. See {@link android.app.ActivityOptions} 1147 * for how to build the Bundle supplied here; there are no supported definitions 1148 * for building it manually. 1149 * 1150 * @throws ActivityNotFoundException 1151 * 1152 * @see #startActivity(Intent) 1153 * @see PackageManager#resolveActivity 1154 */ 1155 public abstract void startActivity(Intent intent, @Nullable Bundle options); 1156 1157 /** 1158 * Version of {@link #startActivity(Intent, Bundle)} that allows you to specify the 1159 * user the activity will be started for. This is not available to applications 1160 * that are not pre-installed on the system image. Using it requires holding 1161 * the INTERACT_ACROSS_USERS_FULL permission. 1162 * @param intent The description of the activity to start. 1163 * @param options Additional options for how the Activity should be started. 1164 * May be null if there are no options. See {@link android.app.ActivityOptions} 1165 * for how to build the Bundle supplied here; there are no supported definitions 1166 * for building it manually. 1167 * @param userId The UserHandle of the user to start this activity for. 1168 * @throws ActivityNotFoundException 1169 * @hide 1170 */ 1171 public void startActivityAsUser(Intent intent, @Nullable Bundle options, UserHandle userId) { 1172 throw new RuntimeException("Not implemented. Must override in a subclass."); 1173 } 1174 1175 /** 1176 * Same as {@link #startActivities(Intent[], Bundle)} with no options 1177 * specified. 1178 * 1179 * @param intents An array of Intents to be started. 1180 * 1181 * @throws ActivityNotFoundException 1182 * 1183 * @see #startActivities(Intent[], Bundle) 1184 * @see PackageManager#resolveActivity 1185 */ 1186 public abstract void startActivities(Intent[] intents); 1187 1188 /** 1189 * Launch multiple new activities. This is generally the same as calling 1190 * {@link #startActivity(Intent)} for the first Intent in the array, 1191 * that activity during its creation calling {@link #startActivity(Intent)} 1192 * for the second entry, etc. Note that unlike that approach, generally 1193 * none of the activities except the last in the array will be created 1194 * at this point, but rather will be created when the user first visits 1195 * them (due to pressing back from the activity on top). 1196 * 1197 * <p>This method throws {@link ActivityNotFoundException} 1198 * if there was no Activity found for <em>any</em> given Intent. In this 1199 * case the state of the activity stack is undefined (some Intents in the 1200 * list may be on it, some not), so you probably want to avoid such situations. 1201 * 1202 * @param intents An array of Intents to be started. 1203 * @param options Additional options for how the Activity should be started. 1204 * See {@link android.content.Context#startActivity(Intent, Bundle) 1205 * Context.startActivity(Intent, Bundle)} for more details. 1206 * 1207 * @throws ActivityNotFoundException 1208 * 1209 * @see #startActivities(Intent[]) 1210 * @see PackageManager#resolveActivity 1211 */ 1212 public abstract void startActivities(Intent[] intents, Bundle options); 1213 1214 /** 1215 * @hide 1216 * Launch multiple new activities. This is generally the same as calling 1217 * {@link #startActivity(Intent)} for the first Intent in the array, 1218 * that activity during its creation calling {@link #startActivity(Intent)} 1219 * for the second entry, etc. Note that unlike that approach, generally 1220 * none of the activities except the last in the array will be created 1221 * at this point, but rather will be created when the user first visits 1222 * them (due to pressing back from the activity on top). 1223 * 1224 * <p>This method throws {@link ActivityNotFoundException} 1225 * if there was no Activity found for <em>any</em> given Intent. In this 1226 * case the state of the activity stack is undefined (some Intents in the 1227 * list may be on it, some not), so you probably want to avoid such situations. 1228 * 1229 * @param intents An array of Intents to be started. 1230 * @param options Additional options for how the Activity should be started. 1231 * @param userHandle The user for whom to launch the activities 1232 * See {@link android.content.Context#startActivity(Intent, Bundle) 1233 * Context.startActivity(Intent, Bundle)} for more details. 1234 * 1235 * @throws ActivityNotFoundException 1236 * 1237 * @see #startActivities(Intent[]) 1238 * @see PackageManager#resolveActivity 1239 */ 1240 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) { 1241 throw new RuntimeException("Not implemented. Must override in a subclass."); 1242 } 1243 1244 /** 1245 * Same as {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)} 1246 * with no options specified. 1247 * 1248 * @param intent The IntentSender to launch. 1249 * @param fillInIntent If non-null, this will be provided as the 1250 * intent parameter to {@link IntentSender#sendIntent}. 1251 * @param flagsMask Intent flags in the original IntentSender that you 1252 * would like to change. 1253 * @param flagsValues Desired values for any bits set in 1254 * <var>flagsMask</var> 1255 * @param extraFlags Always set to 0. 1256 * 1257 * @see #startActivity(Intent) 1258 * @see #startIntentSender(IntentSender, Intent, int, int, int, Bundle) 1259 */ 1260 public abstract void startIntentSender(IntentSender intent, 1261 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 1262 throws IntentSender.SendIntentException; 1263 1264 /** 1265 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender 1266 * to start. If the IntentSender is for an activity, that activity will be started 1267 * as if you had called the regular {@link #startActivity(Intent)} 1268 * here; otherwise, its associated action will be executed (such as 1269 * sending a broadcast) as if you had called 1270 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it. 1271 * 1272 * @param intent The IntentSender to launch. 1273 * @param fillInIntent If non-null, this will be provided as the 1274 * intent parameter to {@link IntentSender#sendIntent}. 1275 * @param flagsMask Intent flags in the original IntentSender that you 1276 * would like to change. 1277 * @param flagsValues Desired values for any bits set in 1278 * <var>flagsMask</var> 1279 * @param extraFlags Always set to 0. 1280 * @param options Additional options for how the Activity should be started. 1281 * See {@link android.content.Context#startActivity(Intent, Bundle) 1282 * Context.startActivity(Intent, Bundle)} for more details. If options 1283 * have also been supplied by the IntentSender, options given here will 1284 * override any that conflict with those given by the IntentSender. 1285 * 1286 * @see #startActivity(Intent, Bundle) 1287 * @see #startIntentSender(IntentSender, Intent, int, int, int) 1288 */ 1289 public abstract void startIntentSender(IntentSender intent, 1290 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, 1291 Bundle options) throws IntentSender.SendIntentException; 1292 1293 /** 1294 * Broadcast the given intent to all interested BroadcastReceivers. This 1295 * call is asynchronous; it returns immediately, and you will continue 1296 * executing while the receivers are run. No results are propagated from 1297 * receivers and receivers can not abort the broadcast. If you want 1298 * to allow receivers to propagate results or abort the broadcast, you must 1299 * send an ordered broadcast using 1300 * {@link #sendOrderedBroadcast(Intent, String)}. 1301 * 1302 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1303 * 1304 * @param intent The Intent to broadcast; all receivers matching this 1305 * Intent will receive the broadcast. 1306 * 1307 * @see android.content.BroadcastReceiver 1308 * @see #registerReceiver 1309 * @see #sendBroadcast(Intent, String) 1310 * @see #sendOrderedBroadcast(Intent, String) 1311 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1312 */ 1313 public abstract void sendBroadcast(Intent intent); 1314 1315 /** 1316 * Broadcast the given intent to all interested BroadcastReceivers, allowing 1317 * an optional required permission to be enforced. This 1318 * call is asynchronous; it returns immediately, and you will continue 1319 * executing while the receivers are run. No results are propagated from 1320 * receivers and receivers can not abort the broadcast. If you want 1321 * to allow receivers to propagate results or abort the broadcast, you must 1322 * send an ordered broadcast using 1323 * {@link #sendOrderedBroadcast(Intent, String)}. 1324 * 1325 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1326 * 1327 * @param intent The Intent to broadcast; all receivers matching this 1328 * Intent will receive the broadcast. 1329 * @param receiverPermission (optional) String naming a permission that 1330 * a receiver must hold in order to receive your broadcast. 1331 * If null, no permission is required. 1332 * 1333 * @see android.content.BroadcastReceiver 1334 * @see #registerReceiver 1335 * @see #sendBroadcast(Intent) 1336 * @see #sendOrderedBroadcast(Intent, String) 1337 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1338 */ 1339 public abstract void sendBroadcast(Intent intent, 1340 @Nullable String receiverPermission); 1341 1342 /** 1343 * Like {@link #sendBroadcast(Intent, String)}, but also allows specification 1344 * of an associated app op as per {@link android.app.AppOpsManager}. 1345 * @hide 1346 */ 1347 public abstract void sendBroadcast(Intent intent, 1348 String receiverPermission, int appOp); 1349 1350 /** 1351 * Broadcast the given intent to all interested BroadcastReceivers, delivering 1352 * them one at a time to allow more preferred receivers to consume the 1353 * broadcast before it is delivered to less preferred receivers. This 1354 * call is asynchronous; it returns immediately, and you will continue 1355 * executing while the receivers are run. 1356 * 1357 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1358 * 1359 * @param intent The Intent to broadcast; all receivers matching this 1360 * Intent will receive the broadcast. 1361 * @param receiverPermission (optional) String naming a permissions that 1362 * a receiver must hold in order to receive your broadcast. 1363 * If null, no permission is required. 1364 * 1365 * @see android.content.BroadcastReceiver 1366 * @see #registerReceiver 1367 * @see #sendBroadcast(Intent) 1368 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1369 */ 1370 public abstract void sendOrderedBroadcast(Intent intent, 1371 @Nullable String receiverPermission); 1372 1373 /** 1374 * Version of {@link #sendBroadcast(Intent)} that allows you to 1375 * receive data back from the broadcast. This is accomplished by 1376 * supplying your own BroadcastReceiver when calling, which will be 1377 * treated as a final receiver at the end of the broadcast -- its 1378 * {@link BroadcastReceiver#onReceive} method will be called with 1379 * the result values collected from the other receivers. The broadcast will 1380 * be serialized in the same way as calling 1381 * {@link #sendOrderedBroadcast(Intent, String)}. 1382 * 1383 * <p>Like {@link #sendBroadcast(Intent)}, this method is 1384 * asynchronous; it will return before 1385 * resultReceiver.onReceive() is called. 1386 * 1387 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1388 * 1389 * @param intent The Intent to broadcast; all receivers matching this 1390 * Intent will receive the broadcast. 1391 * @param receiverPermission String naming a permissions that 1392 * a receiver must hold in order to receive your broadcast. 1393 * If null, no permission is required. 1394 * @param resultReceiver Your own BroadcastReceiver to treat as the final 1395 * receiver of the broadcast. 1396 * @param scheduler A custom Handler with which to schedule the 1397 * resultReceiver callback; if null it will be 1398 * scheduled in the Context's main thread. 1399 * @param initialCode An initial value for the result code. Often 1400 * Activity.RESULT_OK. 1401 * @param initialData An initial value for the result data. Often 1402 * null. 1403 * @param initialExtras An initial value for the result extras. Often 1404 * null. 1405 * 1406 * @see #sendBroadcast(Intent) 1407 * @see #sendBroadcast(Intent, String) 1408 * @see #sendOrderedBroadcast(Intent, String) 1409 * @see #sendStickyBroadcast(Intent) 1410 * @see #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle) 1411 * @see android.content.BroadcastReceiver 1412 * @see #registerReceiver 1413 * @see android.app.Activity#RESULT_OK 1414 */ 1415 public abstract void sendOrderedBroadcast(@NonNull Intent intent, 1416 @Nullable String receiverPermission, BroadcastReceiver resultReceiver, 1417 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 1418 @Nullable Bundle initialExtras); 1419 1420 /** 1421 * Like {@link #sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, 1422 * int, String, android.os.Bundle)}, but also allows specification 1423 * of an associated app op as per {@link android.app.AppOpsManager}. 1424 * @hide 1425 */ 1426 public abstract void sendOrderedBroadcast(Intent intent, 1427 String receiverPermission, int appOp, BroadcastReceiver resultReceiver, 1428 Handler scheduler, int initialCode, String initialData, 1429 Bundle initialExtras); 1430 1431 /** 1432 * Version of {@link #sendBroadcast(Intent)} that allows you to specify the 1433 * user the broadcast will be sent to. This is not available to applications 1434 * that are not pre-installed on the system image. Using it requires holding 1435 * the INTERACT_ACROSS_USERS permission. 1436 * @param intent The intent to broadcast 1437 * @param user UserHandle to send the intent to. 1438 * @see #sendBroadcast(Intent) 1439 */ 1440 public abstract void sendBroadcastAsUser(Intent intent, UserHandle user); 1441 1442 /** 1443 * Version of {@link #sendBroadcast(Intent, String)} that allows you to specify the 1444 * user the broadcast will be sent to. This is not available to applications 1445 * that are not pre-installed on the system image. Using it requires holding 1446 * the INTERACT_ACROSS_USERS permission. 1447 * 1448 * @param intent The Intent to broadcast; all receivers matching this 1449 * Intent will receive the broadcast. 1450 * @param user UserHandle to send the intent to. 1451 * @param receiverPermission (optional) String naming a permission that 1452 * a receiver must hold in order to receive your broadcast. 1453 * If null, no permission is required. 1454 * 1455 * @see #sendBroadcast(Intent, String) 1456 */ 1457 public abstract void sendBroadcastAsUser(Intent intent, UserHandle user, 1458 @Nullable String receiverPermission); 1459 1460 /** 1461 * Version of 1462 * {@link #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle)} 1463 * that allows you to specify the 1464 * user the broadcast will be sent to. This is not available to applications 1465 * that are not pre-installed on the system image. Using it requires holding 1466 * the INTERACT_ACROSS_USERS permission. 1467 * 1468 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1469 * 1470 * @param intent The Intent to broadcast; all receivers matching this 1471 * Intent will receive the broadcast. 1472 * @param user UserHandle to send the intent to. 1473 * @param receiverPermission String naming a permissions that 1474 * a receiver must hold in order to receive your broadcast. 1475 * If null, no permission is required. 1476 * @param resultReceiver Your own BroadcastReceiver to treat as the final 1477 * receiver of the broadcast. 1478 * @param scheduler A custom Handler with which to schedule the 1479 * resultReceiver callback; if null it will be 1480 * scheduled in the Context's main thread. 1481 * @param initialCode An initial value for the result code. Often 1482 * Activity.RESULT_OK. 1483 * @param initialData An initial value for the result data. Often 1484 * null. 1485 * @param initialExtras An initial value for the result extras. Often 1486 * null. 1487 * 1488 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1489 */ 1490 public abstract void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 1491 @Nullable String receiverPermission, BroadcastReceiver resultReceiver, 1492 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 1493 @Nullable Bundle initialExtras); 1494 1495 /** 1496 * Perform a {@link #sendBroadcast(Intent)} that is "sticky," meaning the 1497 * Intent you are sending stays around after the broadcast is complete, 1498 * so that others can quickly retrieve that data through the return 1499 * value of {@link #registerReceiver(BroadcastReceiver, IntentFilter)}. In 1500 * all other ways, this behaves the same as 1501 * {@link #sendBroadcast(Intent)}. 1502 * 1503 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 1504 * permission in order to use this API. If you do not hold that 1505 * permission, {@link SecurityException} will be thrown. 1506 * 1507 * @param intent The Intent to broadcast; all receivers matching this 1508 * Intent will receive the broadcast, and the Intent will be held to 1509 * be re-broadcast to future receivers. 1510 * 1511 * @see #sendBroadcast(Intent) 1512 * @see #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle) 1513 */ 1514 public abstract void sendStickyBroadcast(Intent intent); 1515 1516 /** 1517 * Version of {@link #sendStickyBroadcast} that allows you to 1518 * receive data back from the broadcast. This is accomplished by 1519 * supplying your own BroadcastReceiver when calling, which will be 1520 * treated as a final receiver at the end of the broadcast -- its 1521 * {@link BroadcastReceiver#onReceive} method will be called with 1522 * the result values collected from the other receivers. The broadcast will 1523 * be serialized in the same way as calling 1524 * {@link #sendOrderedBroadcast(Intent, String)}. 1525 * 1526 * <p>Like {@link #sendBroadcast(Intent)}, this method is 1527 * asynchronous; it will return before 1528 * resultReceiver.onReceive() is called. Note that the sticky data 1529 * stored is only the data you initially supply to the broadcast, not 1530 * the result of any changes made by the receivers. 1531 * 1532 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1533 * 1534 * @param intent The Intent to broadcast; all receivers matching this 1535 * Intent will receive the broadcast. 1536 * @param resultReceiver Your own BroadcastReceiver to treat as the final 1537 * receiver of the broadcast. 1538 * @param scheduler A custom Handler with which to schedule the 1539 * resultReceiver callback; if null it will be 1540 * scheduled in the Context's main thread. 1541 * @param initialCode An initial value for the result code. Often 1542 * Activity.RESULT_OK. 1543 * @param initialData An initial value for the result data. Often 1544 * null. 1545 * @param initialExtras An initial value for the result extras. Often 1546 * null. 1547 * 1548 * @see #sendBroadcast(Intent) 1549 * @see #sendBroadcast(Intent, String) 1550 * @see #sendOrderedBroadcast(Intent, String) 1551 * @see #sendStickyBroadcast(Intent) 1552 * @see android.content.BroadcastReceiver 1553 * @see #registerReceiver 1554 * @see android.app.Activity#RESULT_OK 1555 */ 1556 public abstract void sendStickyOrderedBroadcast(Intent intent, 1557 BroadcastReceiver resultReceiver, 1558 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 1559 @Nullable Bundle initialExtras); 1560 1561 /** 1562 * Remove the data previously sent with {@link #sendStickyBroadcast}, 1563 * so that it is as if the sticky broadcast had never happened. 1564 * 1565 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 1566 * permission in order to use this API. If you do not hold that 1567 * permission, {@link SecurityException} will be thrown. 1568 * 1569 * @param intent The Intent that was previously broadcast. 1570 * 1571 * @see #sendStickyBroadcast 1572 */ 1573 public abstract void removeStickyBroadcast(Intent intent); 1574 1575 /** 1576 * Version of {@link #sendStickyBroadcast(Intent)} that allows you to specify the 1577 * user the broadcast will be sent to. This is not available to applications 1578 * that are not pre-installed on the system image. Using it requires holding 1579 * the INTERACT_ACROSS_USERS permission. 1580 * 1581 * @param intent The Intent to broadcast; all receivers matching this 1582 * Intent will receive the broadcast, and the Intent will be held to 1583 * be re-broadcast to future receivers. 1584 * @param user UserHandle to send the intent to. 1585 * 1586 * @see #sendBroadcast(Intent) 1587 */ 1588 public abstract void sendStickyBroadcastAsUser(Intent intent, UserHandle user); 1589 1590 /** 1591 * Version of 1592 * {@link #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle)} 1593 * that allows you to specify the 1594 * user the broadcast will be sent to. This is not available to applications 1595 * that are not pre-installed on the system image. Using it requires holding 1596 * the INTERACT_ACROSS_USERS permission. 1597 * 1598 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1599 * 1600 * @param intent The Intent to broadcast; all receivers matching this 1601 * Intent will receive the broadcast. 1602 * @param user UserHandle to send the intent to. 1603 * @param resultReceiver Your own BroadcastReceiver to treat as the final 1604 * receiver of the broadcast. 1605 * @param scheduler A custom Handler with which to schedule the 1606 * resultReceiver callback; if null it will be 1607 * scheduled in the Context's main thread. 1608 * @param initialCode An initial value for the result code. Often 1609 * Activity.RESULT_OK. 1610 * @param initialData An initial value for the result data. Often 1611 * null. 1612 * @param initialExtras An initial value for the result extras. Often 1613 * null. 1614 * 1615 * @see #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle) 1616 */ 1617 public abstract void sendStickyOrderedBroadcastAsUser(Intent intent, 1618 UserHandle user, BroadcastReceiver resultReceiver, 1619 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 1620 @Nullable Bundle initialExtras); 1621 1622 /** 1623 * Version of {@link #removeStickyBroadcast(Intent)} that allows you to specify the 1624 * user the broadcast will be sent to. This is not available to applications 1625 * that are not pre-installed on the system image. Using it requires holding 1626 * the INTERACT_ACROSS_USERS permission. 1627 * 1628 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 1629 * permission in order to use this API. If you do not hold that 1630 * permission, {@link SecurityException} will be thrown. 1631 * 1632 * @param intent The Intent that was previously broadcast. 1633 * @param user UserHandle to remove the sticky broadcast from. 1634 * 1635 * @see #sendStickyBroadcastAsUser 1636 */ 1637 public abstract void removeStickyBroadcastAsUser(Intent intent, UserHandle user); 1638 1639 /** 1640 * Register a BroadcastReceiver to be run in the main activity thread. The 1641 * <var>receiver</var> will be called with any broadcast Intent that 1642 * matches <var>filter</var>, in the main application thread. 1643 * 1644 * <p>The system may broadcast Intents that are "sticky" -- these stay 1645 * around after the broadcast as finished, to be sent to any later 1646 * registrations. If your IntentFilter matches one of these sticky 1647 * Intents, that Intent will be returned by this function 1648 * <strong>and</strong> sent to your <var>receiver</var> as if it had just 1649 * been broadcast. 1650 * 1651 * <p>There may be multiple sticky Intents that match <var>filter</var>, 1652 * in which case each of these will be sent to <var>receiver</var>. In 1653 * this case, only one of these can be returned directly by the function; 1654 * which of these that is returned is arbitrarily decided by the system. 1655 * 1656 * <p>If you know the Intent your are registering for is sticky, you can 1657 * supply null for your <var>receiver</var>. In this case, no receiver is 1658 * registered -- the function simply returns the sticky Intent that 1659 * matches <var>filter</var>. In the case of multiple matches, the same 1660 * rules as described above apply. 1661 * 1662 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1663 * 1664 * <p>As of {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, receivers 1665 * registered with this method will correctly respect the 1666 * {@link Intent#setPackage(String)} specified for an Intent being broadcast. 1667 * Prior to that, it would be ignored and delivered to all matching registered 1668 * receivers. Be careful if using this for security.</p> 1669 * 1670 * <p class="note">Note: this method <em>cannot be called from a 1671 * {@link BroadcastReceiver} component;</em> that is, from a BroadcastReceiver 1672 * that is declared in an application's manifest. It is okay, however, to call 1673 * this method from another BroadcastReceiver that has itself been registered 1674 * at run time with {@link #registerReceiver}, since the lifetime of such a 1675 * registered BroadcastReceiver is tied to the object that registered it.</p> 1676 * 1677 * @param receiver The BroadcastReceiver to handle the broadcast. 1678 * @param filter Selects the Intent broadcasts to be received. 1679 * 1680 * @return The first sticky intent found that matches <var>filter</var>, 1681 * or null if there are none. 1682 * 1683 * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 1684 * @see #sendBroadcast 1685 * @see #unregisterReceiver 1686 */ 1687 @Nullable 1688 public abstract Intent registerReceiver(@Nullable BroadcastReceiver receiver, 1689 IntentFilter filter); 1690 1691 /** 1692 * Register to receive intent broadcasts, to run in the context of 1693 * <var>scheduler</var>. See 1694 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)} for more 1695 * information. This allows you to enforce permissions on who can 1696 * broadcast intents to your receiver, or have the receiver run in 1697 * a different thread than the main application thread. 1698 * 1699 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1700 * 1701 * <p>As of {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, receivers 1702 * registered with this method will correctly respect the 1703 * {@link Intent#setPackage(String)} specified for an Intent being broadcast. 1704 * Prior to that, it would be ignored and delivered to all matching registered 1705 * receivers. Be careful if using this for security.</p> 1706 * 1707 * @param receiver The BroadcastReceiver to handle the broadcast. 1708 * @param filter Selects the Intent broadcasts to be received. 1709 * @param broadcastPermission String naming a permissions that a 1710 * broadcaster must hold in order to send an Intent to you. If null, 1711 * no permission is required. 1712 * @param scheduler Handler identifying the thread that will receive 1713 * the Intent. If null, the main thread of the process will be used. 1714 * 1715 * @return The first sticky intent found that matches <var>filter</var>, 1716 * or null if there are none. 1717 * 1718 * @see #registerReceiver(BroadcastReceiver, IntentFilter) 1719 * @see #sendBroadcast 1720 * @see #unregisterReceiver 1721 */ 1722 @Nullable 1723 public abstract Intent registerReceiver(BroadcastReceiver receiver, 1724 IntentFilter filter, @Nullable String broadcastPermission, 1725 @Nullable Handler scheduler); 1726 1727 /** 1728 * @hide 1729 * Same as {@link #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 1730 * but for a specific user. This receiver will receiver broadcasts that 1731 * are sent to the requested user. It 1732 * requires holding the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} 1733 * permission. 1734 * 1735 * @param receiver The BroadcastReceiver to handle the broadcast. 1736 * @param user UserHandle to send the intent to. 1737 * @param filter Selects the Intent broadcasts to be received. 1738 * @param broadcastPermission String naming a permissions that a 1739 * broadcaster must hold in order to send an Intent to you. If null, 1740 * no permission is required. 1741 * @param scheduler Handler identifying the thread that will receive 1742 * the Intent. If null, the main thread of the process will be used. 1743 * 1744 * @return The first sticky intent found that matches <var>filter</var>, 1745 * or null if there are none. 1746 * 1747 * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler 1748 * @see #sendBroadcast 1749 * @see #unregisterReceiver 1750 */ 1751 @Nullable 1752 public abstract Intent registerReceiverAsUser(BroadcastReceiver receiver, 1753 UserHandle user, IntentFilter filter, @Nullable String broadcastPermission, 1754 @Nullable Handler scheduler); 1755 1756 /** 1757 * Unregister a previously registered BroadcastReceiver. <em>All</em> 1758 * filters that have been registered for this BroadcastReceiver will be 1759 * removed. 1760 * 1761 * @param receiver The BroadcastReceiver to unregister. 1762 * 1763 * @see #registerReceiver 1764 */ 1765 public abstract void unregisterReceiver(BroadcastReceiver receiver); 1766 1767 /** 1768 * Request that a given application service be started. The Intent 1769 * should contain either contain the complete class name of a specific service 1770 * implementation to start or a specific package name to target. If the 1771 * Intent is less specified, it log a warning about this and which of the 1772 * multiple matching services it finds and uses will be undefined. If this service 1773 * is not already running, it will be instantiated and started (creating a 1774 * process for it if needed); if it is running then it remains running. 1775 * 1776 * <p>Every call to this method will result in a corresponding call to 1777 * the target service's {@link android.app.Service#onStartCommand} method, 1778 * with the <var>intent</var> given here. This provides a convenient way 1779 * to submit jobs to a service without having to bind and call on to its 1780 * interface. 1781 * 1782 * <p>Using startService() overrides the default service lifetime that is 1783 * managed by {@link #bindService}: it requires the service to remain 1784 * running until {@link #stopService} is called, regardless of whether 1785 * any clients are connected to it. Note that calls to startService() 1786 * are not nesting: no matter how many times you call startService(), 1787 * a single call to {@link #stopService} will stop it. 1788 * 1789 * <p>The system attempts to keep running services around as much as 1790 * possible. The only time they should be stopped is if the current 1791 * foreground application is using so many resources that the service needs 1792 * to be killed. If any errors happen in the service's process, it will 1793 * automatically be restarted. 1794 * 1795 * <p>This function will throw {@link SecurityException} if you do not 1796 * have permission to start the given service. 1797 * 1798 * @param service Identifies the service to be started. The Intent must be either 1799 * fully explicit (supplying a component name) or specify a specific package 1800 * name it is targetted to. Additional values 1801 * may be included in the Intent extras to supply arguments along with 1802 * this specific start call. 1803 * 1804 * @return If the service is being started or is already running, the 1805 * {@link ComponentName} of the actual service that was started is 1806 * returned; else if the service does not exist null is returned. 1807 * 1808 * @throws SecurityException 1809 * 1810 * @see #stopService 1811 * @see #bindService 1812 */ 1813 @Nullable 1814 public abstract ComponentName startService(Intent service); 1815 1816 /** 1817 * Request that a given application service be stopped. If the service is 1818 * not running, nothing happens. Otherwise it is stopped. Note that calls 1819 * to startService() are not counted -- this stops the service no matter 1820 * how many times it was started. 1821 * 1822 * <p>Note that if a stopped service still has {@link ServiceConnection} 1823 * objects bound to it with the {@link #BIND_AUTO_CREATE} set, it will 1824 * not be destroyed until all of these bindings are removed. See 1825 * the {@link android.app.Service} documentation for more details on a 1826 * service's lifecycle. 1827 * 1828 * <p>This function will throw {@link SecurityException} if you do not 1829 * have permission to stop the given service. 1830 * 1831 * @param service Description of the service to be stopped. The Intent must be either 1832 * fully explicit (supplying a component name) or specify a specific package 1833 * name it is targetted to. 1834 * 1835 * @return If there is a service matching the given Intent that is already 1836 * running, then it is stopped and {@code true} is returned; else {@code false} is returned. 1837 * 1838 * @throws SecurityException 1839 * 1840 * @see #startService 1841 */ 1842 public abstract boolean stopService(Intent service); 1843 1844 /** 1845 * @hide like {@link #startService(Intent)} but for a specific user. 1846 */ 1847 public abstract ComponentName startServiceAsUser(Intent service, UserHandle user); 1848 1849 /** 1850 * @hide like {@link #stopService(Intent)} but for a specific user. 1851 */ 1852 public abstract boolean stopServiceAsUser(Intent service, UserHandle user); 1853 1854 /** 1855 * Connect to an application service, creating it if needed. This defines 1856 * a dependency between your application and the service. The given 1857 * <var>conn</var> will receive the service object when it is created and be 1858 * told if it dies and restarts. The service will be considered required 1859 * by the system only for as long as the calling context exists. For 1860 * example, if this Context is an Activity that is stopped, the service will 1861 * not be required to continue running until the Activity is resumed. 1862 * 1863 * <p>This function will throw {@link SecurityException} if you do not 1864 * have permission to bind to the given service. 1865 * 1866 * <p class="note">Note: this method <em>can not be called from a 1867 * {@link BroadcastReceiver} component</em>. A pattern you can use to 1868 * communicate from a BroadcastReceiver to a Service is to call 1869 * {@link #startService} with the arguments containing the command to be 1870 * sent, with the service calling its 1871 * {@link android.app.Service#stopSelf(int)} method when done executing 1872 * that command. See the API demo App/Service/Service Start Arguments 1873 * Controller for an illustration of this. It is okay, however, to use 1874 * this method from a BroadcastReceiver that has been registered with 1875 * {@link #registerReceiver}, since the lifetime of this BroadcastReceiver 1876 * is tied to another object (the one that registered it).</p> 1877 * 1878 * @param service Identifies the service to connect to. The Intent may 1879 * specify either an explicit component name, or a logical 1880 * description (action, category, etc) to match an 1881 * {@link IntentFilter} published by a service. 1882 * @param conn Receives information as the service is started and stopped. 1883 * This must be a valid ServiceConnection object; it must not be null. 1884 * @param flags Operation options for the binding. May be 0, 1885 * {@link #BIND_AUTO_CREATE}, {@link #BIND_DEBUG_UNBIND}, 1886 * {@link #BIND_NOT_FOREGROUND}, {@link #BIND_ABOVE_CLIENT}, 1887 * {@link #BIND_ALLOW_OOM_MANAGEMENT}, or 1888 * {@link #BIND_WAIVE_PRIORITY}. 1889 * @return If you have successfully bound to the service, {@code true} is returned; 1890 * {@code false} is returned if the connection is not made so you will not 1891 * receive the service object. 1892 * 1893 * @throws SecurityException 1894 * 1895 * @see #unbindService 1896 * @see #startService 1897 * @see #BIND_AUTO_CREATE 1898 * @see #BIND_DEBUG_UNBIND 1899 * @see #BIND_NOT_FOREGROUND 1900 */ 1901 public abstract boolean bindService(Intent service, @NonNull ServiceConnection conn, 1902 @BindServiceFlags int flags); 1903 1904 /** 1905 * Same as {@link #bindService(Intent, ServiceConnection, int)}, but with an explicit userHandle 1906 * argument for use by system server and other multi-user aware code. 1907 * @hide 1908 */ 1909 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, UserHandle user) { 1910 throw new RuntimeException("Not implemented. Must override in a subclass."); 1911 } 1912 1913 /** 1914 * Disconnect from an application service. You will no longer receive 1915 * calls as the service is restarted, and the service is now allowed to 1916 * stop at any time. 1917 * 1918 * @param conn The connection interface previously supplied to 1919 * bindService(). This parameter must not be null. 1920 * 1921 * @see #bindService 1922 */ 1923 public abstract void unbindService(@NonNull ServiceConnection conn); 1924 1925 /** 1926 * Start executing an {@link android.app.Instrumentation} class. The given 1927 * Instrumentation component will be run by killing its target application 1928 * (if currently running), starting the target process, instantiating the 1929 * instrumentation component, and then letting it drive the application. 1930 * 1931 * <p>This function is not synchronous -- it returns as soon as the 1932 * instrumentation has started and while it is running. 1933 * 1934 * <p>Instrumentation is normally only allowed to run against a package 1935 * that is either unsigned or signed with a signature that the 1936 * the instrumentation package is also signed with (ensuring the target 1937 * trusts the instrumentation). 1938 * 1939 * @param className Name of the Instrumentation component to be run. 1940 * @param profileFile Optional path to write profiling data as the 1941 * instrumentation runs, or null for no profiling. 1942 * @param arguments Additional optional arguments to pass to the 1943 * instrumentation, or null. 1944 * 1945 * @return {@code true} if the instrumentation was successfully started, 1946 * else {@code false} if it could not be found. 1947 */ 1948 public abstract boolean startInstrumentation(@NonNull ComponentName className, 1949 @Nullable String profileFile, @Nullable Bundle arguments); 1950 1951 /** @hide */ 1952 @StringDef({ 1953 POWER_SERVICE, 1954 WINDOW_SERVICE, 1955 LAYOUT_INFLATER_SERVICE, 1956 ACCOUNT_SERVICE, 1957 ACTIVITY_SERVICE, 1958 ALARM_SERVICE, 1959 NOTIFICATION_SERVICE, 1960 ACCESSIBILITY_SERVICE, 1961 CAPTIONING_SERVICE, 1962 KEYGUARD_SERVICE, 1963 LOCATION_SERVICE, 1964 //@hide: COUNTRY_DETECTOR, 1965 SEARCH_SERVICE, 1966 SENSOR_SERVICE, 1967 STORAGE_SERVICE, 1968 WALLPAPER_SERVICE, 1969 VIBRATOR_SERVICE, 1970 //@hide: STATUS_BAR_SERVICE, 1971 CONNECTIVITY_SERVICE, 1972 //@hide: UPDATE_LOCK_SERVICE, 1973 //@hide: NETWORKMANAGEMENT_SERVICE, 1974 //@hide: NETWORK_STATS_SERVICE, 1975 //@hide: NETWORK_POLICY_SERVICE, 1976 WIFI_SERVICE, 1977 WIFI_HOTSPOT_SERVICE, 1978 WIFI_P2P_SERVICE, 1979 NSD_SERVICE, 1980 AUDIO_SERVICE, 1981 MEDIA_ROUTER_SERVICE, 1982 TELEPHONY_SERVICE, 1983 CLIPBOARD_SERVICE, 1984 INPUT_METHOD_SERVICE, 1985 TEXT_SERVICES_MANAGER_SERVICE, 1986 //@hide: APPWIDGET_SERVICE, 1987 //@hide: BACKUP_SERVICE, 1988 DROPBOX_SERVICE, 1989 DEVICE_POLICY_SERVICE, 1990 UI_MODE_SERVICE, 1991 DOWNLOAD_SERVICE, 1992 NFC_SERVICE, 1993 BLUETOOTH_SERVICE, 1994 //@hide: SIP_SERVICE, 1995 USB_SERVICE, 1996 //@hide: SERIAL_SERVICE, 1997 INPUT_SERVICE, 1998 DISPLAY_SERVICE, 1999 //@hide: SCHEDULING_POLICY_SERVICE, 2000 USER_SERVICE, 2001 //@hide: APP_OPS_SERVICE 2002 CAMERA_SERVICE, 2003 PRINT_SERVICE, 2004 MEDIA_SESSION_SERVICE, 2005 }) 2006 @Retention(RetentionPolicy.SOURCE) 2007 public @interface ServiceName {} 2008 2009 /** 2010 * Return the handle to a system-level service by name. The class of the 2011 * returned object varies by the requested name. Currently available names 2012 * are: 2013 * 2014 * <dl> 2015 * <dt> {@link #WINDOW_SERVICE} ("window") 2016 * <dd> The top-level window manager in which you can place custom 2017 * windows. The returned object is a {@link android.view.WindowManager}. 2018 * <dt> {@link #LAYOUT_INFLATER_SERVICE} ("layout_inflater") 2019 * <dd> A {@link android.view.LayoutInflater} for inflating layout resources 2020 * in this context. 2021 * <dt> {@link #ACTIVITY_SERVICE} ("activity") 2022 * <dd> A {@link android.app.ActivityManager} for interacting with the 2023 * global activity state of the system. 2024 * <dt> {@link #POWER_SERVICE} ("power") 2025 * <dd> A {@link android.os.PowerManager} for controlling power 2026 * management. 2027 * <dt> {@link #ALARM_SERVICE} ("alarm") 2028 * <dd> A {@link android.app.AlarmManager} for receiving intents at the 2029 * time of your choosing. 2030 * <dt> {@link #NOTIFICATION_SERVICE} ("notification") 2031 * <dd> A {@link android.app.NotificationManager} for informing the user 2032 * of background events. 2033 * <dt> {@link #KEYGUARD_SERVICE} ("keyguard") 2034 * <dd> A {@link android.app.KeyguardManager} for controlling keyguard. 2035 * <dt> {@link #LOCATION_SERVICE} ("location") 2036 * <dd> A {@link android.location.LocationManager} for controlling location 2037 * (e.g., GPS) updates. 2038 * <dt> {@link #SEARCH_SERVICE} ("search") 2039 * <dd> A {@link android.app.SearchManager} for handling search. 2040 * <dt> {@link #VIBRATOR_SERVICE} ("vibrator") 2041 * <dd> A {@link android.os.Vibrator} for interacting with the vibrator 2042 * hardware. 2043 * <dt> {@link #CONNECTIVITY_SERVICE} ("connection") 2044 * <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for 2045 * handling management of network connections. 2046 * <dt> {@link #WIFI_SERVICE} ("wifi") 2047 * <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of 2048 * Wi-Fi connectivity. 2049 * <dt> {@link #INPUT_METHOD_SERVICE} ("input_method") 2050 * <dd> An {@link android.view.inputmethod.InputMethodManager InputMethodManager} 2051 * for management of input methods. 2052 * <dt> {@link #UI_MODE_SERVICE} ("uimode") 2053 * <dd> An {@link android.app.UiModeManager} for controlling UI modes. 2054 * <dt> {@link #DOWNLOAD_SERVICE} ("download") 2055 * <dd> A {@link android.app.DownloadManager} for requesting HTTP downloads 2056 * </dl> 2057 * 2058 * <p>Note: System services obtained via this API may be closely associated with 2059 * the Context in which they are obtained from. In general, do not share the 2060 * service objects between various different contexts (Activities, Applications, 2061 * Services, Providers, etc.) 2062 * 2063 * @param name The name of the desired service. 2064 * 2065 * @return The service or null if the name does not exist. 2066 * 2067 * @see #WINDOW_SERVICE 2068 * @see android.view.WindowManager 2069 * @see #LAYOUT_INFLATER_SERVICE 2070 * @see android.view.LayoutInflater 2071 * @see #ACTIVITY_SERVICE 2072 * @see android.app.ActivityManager 2073 * @see #POWER_SERVICE 2074 * @see android.os.PowerManager 2075 * @see #ALARM_SERVICE 2076 * @see android.app.AlarmManager 2077 * @see #NOTIFICATION_SERVICE 2078 * @see android.app.NotificationManager 2079 * @see #KEYGUARD_SERVICE 2080 * @see android.app.KeyguardManager 2081 * @see #LOCATION_SERVICE 2082 * @see android.location.LocationManager 2083 * @see #SEARCH_SERVICE 2084 * @see android.app.SearchManager 2085 * @see #SENSOR_SERVICE 2086 * @see android.hardware.SensorManager 2087 * @see #HDMI_CEC_SERVICE 2088 * @see android.hardware.hdmi.HdmiCecManager 2089 * @see #STORAGE_SERVICE 2090 * @see android.os.storage.StorageManager 2091 * @see #VIBRATOR_SERVICE 2092 * @see android.os.Vibrator 2093 * @see #CONNECTIVITY_SERVICE 2094 * @see android.net.ConnectivityManager 2095 * @see #WIFI_SERVICE 2096 * @see android.net.wifi.WifiManager 2097 * @see #AUDIO_SERVICE 2098 * @see android.media.AudioManager 2099 * @see #MEDIA_ROUTER_SERVICE 2100 * @see android.media.MediaRouter 2101 * @see #TELEPHONY_SERVICE 2102 * @see android.telephony.TelephonyManager 2103 * @see #INPUT_METHOD_SERVICE 2104 * @see android.view.inputmethod.InputMethodManager 2105 * @see #UI_MODE_SERVICE 2106 * @see android.app.UiModeManager 2107 * @see #DOWNLOAD_SERVICE 2108 * @see android.app.DownloadManager 2109 */ 2110 public abstract Object getSystemService(@ServiceName @NonNull String name); 2111 2112 /** 2113 * Use with {@link #getSystemService} to retrieve a 2114 * {@link android.os.PowerManager} for controlling power management, 2115 * including "wake locks," which let you keep the device on while 2116 * you're running long tasks. 2117 */ 2118 public static final String POWER_SERVICE = "power"; 2119 2120 /** 2121 * Use with {@link #getSystemService} to retrieve a 2122 * {@link android.view.WindowManager} for accessing the system's window 2123 * manager. 2124 * 2125 * @see #getSystemService 2126 * @see android.view.WindowManager 2127 */ 2128 public static final String WINDOW_SERVICE = "window"; 2129 2130 /** 2131 * Use with {@link #getSystemService} to retrieve a 2132 * {@link android.view.LayoutInflater} for inflating layout resources in this 2133 * context. 2134 * 2135 * @see #getSystemService 2136 * @see android.view.LayoutInflater 2137 */ 2138 public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater"; 2139 2140 /** 2141 * Use with {@link #getSystemService} to retrieve a 2142 * {@link android.accounts.AccountManager} for receiving intents at a 2143 * time of your choosing. 2144 * 2145 * @see #getSystemService 2146 * @see android.accounts.AccountManager 2147 */ 2148 public static final String ACCOUNT_SERVICE = "account"; 2149 2150 /** 2151 * Use with {@link #getSystemService} to retrieve a 2152 * {@link android.app.ActivityManager} for interacting with the global 2153 * system state. 2154 * 2155 * @see #getSystemService 2156 * @see android.app.ActivityManager 2157 */ 2158 public static final String ACTIVITY_SERVICE = "activity"; 2159 2160 /** 2161 * Use with {@link #getSystemService} to retrieve a 2162 * {@link android.app.AlarmManager} for receiving intents at a 2163 * time of your choosing. 2164 * 2165 * @see #getSystemService 2166 * @see android.app.AlarmManager 2167 */ 2168 public static final String ALARM_SERVICE = "alarm"; 2169 2170 /** 2171 * Use with {@link #getSystemService} to retrieve a 2172 * {@link android.app.NotificationManager} for informing the user of 2173 * background events. 2174 * 2175 * @see #getSystemService 2176 * @see android.app.NotificationManager 2177 */ 2178 public static final String NOTIFICATION_SERVICE = "notification"; 2179 2180 /** 2181 * Use with {@link #getSystemService} to retrieve a 2182 * {@link android.view.accessibility.AccessibilityManager} for giving the user 2183 * feedback for UI events through the registered event listeners. 2184 * 2185 * @see #getSystemService 2186 * @see android.view.accessibility.AccessibilityManager 2187 */ 2188 public static final String ACCESSIBILITY_SERVICE = "accessibility"; 2189 2190 /** 2191 * Use with {@link #getSystemService} to retrieve a 2192 * {@link android.view.accessibility.CaptioningManager} for obtaining 2193 * captioning properties and listening for changes in captioning 2194 * preferences. 2195 * 2196 * @see #getSystemService 2197 * @see android.view.accessibility.CaptioningManager 2198 */ 2199 public static final String CAPTIONING_SERVICE = "captioning"; 2200 2201 /** 2202 * Use with {@link #getSystemService} to retrieve a 2203 * {@link android.app.NotificationManager} for controlling keyguard. 2204 * 2205 * @see #getSystemService 2206 * @see android.app.KeyguardManager 2207 */ 2208 public static final String KEYGUARD_SERVICE = "keyguard"; 2209 2210 /** 2211 * Use with {@link #getSystemService} to retrieve a {@link 2212 * android.location.LocationManager} for controlling location 2213 * updates. 2214 * 2215 * @see #getSystemService 2216 * @see android.location.LocationManager 2217 */ 2218 public static final String LOCATION_SERVICE = "location"; 2219 2220 /** 2221 * Use with {@link #getSystemService} to retrieve a 2222 * {@link android.location.CountryDetector} for detecting the country that 2223 * the user is in. 2224 * 2225 * @hide 2226 */ 2227 public static final String COUNTRY_DETECTOR = "country_detector"; 2228 2229 /** 2230 * Use with {@link #getSystemService} to retrieve a {@link 2231 * android.app.SearchManager} for handling searches. 2232 * 2233 * @see #getSystemService 2234 * @see android.app.SearchManager 2235 */ 2236 public static final String SEARCH_SERVICE = "search"; 2237 2238 /** 2239 * Use with {@link #getSystemService} to retrieve a {@link 2240 * android.hardware.SensorManager} for accessing sensors. 2241 * 2242 * @see #getSystemService 2243 * @see android.hardware.SensorManager 2244 */ 2245 public static final String SENSOR_SERVICE = "sensor"; 2246 2247 /** 2248 * Use with {@link #getSystemService} to retrieve a {@link 2249 * android.os.storage.StorageManager} for accessing system storage 2250 * functions. 2251 * 2252 * @see #getSystemService 2253 * @see android.os.storage.StorageManager 2254 */ 2255 public static final String STORAGE_SERVICE = "storage"; 2256 2257 /** 2258 * Use with {@link #getSystemService} to retrieve a 2259 * com.android.server.WallpaperService for accessing wallpapers. 2260 * 2261 * @see #getSystemService 2262 */ 2263 public static final String WALLPAPER_SERVICE = "wallpaper"; 2264 2265 /** 2266 * Use with {@link #getSystemService} to retrieve a {@link 2267 * android.os.Vibrator} for interacting with the vibration hardware. 2268 * 2269 * @see #getSystemService 2270 * @see android.os.Vibrator 2271 */ 2272 public static final String VIBRATOR_SERVICE = "vibrator"; 2273 2274 /** 2275 * Use with {@link #getSystemService} to retrieve a {@link 2276 * android.app.StatusBarManager} for interacting with the status bar. 2277 * 2278 * @see #getSystemService 2279 * @see android.app.StatusBarManager 2280 * @hide 2281 */ 2282 public static final String STATUS_BAR_SERVICE = "statusbar"; 2283 2284 /** 2285 * Use with {@link #getSystemService} to retrieve a {@link 2286 * android.net.ConnectivityManager} for handling management of 2287 * network connections. 2288 * 2289 * @see #getSystemService 2290 * @see android.net.ConnectivityManager 2291 */ 2292 public static final String CONNECTIVITY_SERVICE = "connectivity"; 2293 2294 /** 2295 * Use with {@link #getSystemService} to retrieve a {@link 2296 * android.os.IUpdateLock} for managing runtime sequences that 2297 * must not be interrupted by headless OTA application or similar. 2298 * 2299 * @hide 2300 * @see #getSystemService 2301 * @see android.os.UpdateLock 2302 */ 2303 public static final String UPDATE_LOCK_SERVICE = "updatelock"; 2304 2305 /** 2306 * Constant for the internal network management service, not really a Context service. 2307 * @hide 2308 */ 2309 public static final String NETWORKMANAGEMENT_SERVICE = "network_management"; 2310 2311 /** {@hide} */ 2312 public static final String NETWORK_STATS_SERVICE = "netstats"; 2313 /** {@hide} */ 2314 public static final String NETWORK_POLICY_SERVICE = "netpolicy"; 2315 2316 /** 2317 * Use with {@link #getSystemService} to retrieve a {@link 2318 * android.net.wifi.WifiManager} for handling management of 2319 * Wi-Fi access. 2320 * 2321 * @see #getSystemService 2322 * @see android.net.wifi.WifiManager 2323 */ 2324 public static final String WIFI_SERVICE = "wifi"; 2325 2326 /** 2327 * Use with {@link #getSystemService} to retrieve a {@link 2328 * android.net.wifi.hotspot.WifiHotspotManager} for handling management of 2329 * Wi-Fi hotspot access. 2330 * 2331 * @see #getSystemService 2332 * @see android.net.wifi.hotspot.WifiHotspotManager 2333 */ 2334 public static final String WIFI_HOTSPOT_SERVICE = "wifihotspot"; 2335 2336 /** 2337 * Use with {@link #getSystemService} to retrieve a {@link 2338 * android.net.wifi.p2p.WifiP2pManager} for handling management of 2339 * Wi-Fi peer-to-peer connections. 2340 * 2341 * @see #getSystemService 2342 * @see android.net.wifi.p2p.WifiP2pManager 2343 */ 2344 public static final String WIFI_P2P_SERVICE = "wifip2p"; 2345 2346 /** 2347 * Use with {@link #getSystemService} to retrieve a {@link 2348 * android.net.nsd.NsdManager} for handling management of network service 2349 * discovery 2350 * 2351 * @see #getSystemService 2352 * @see android.net.nsd.NsdManager 2353 */ 2354 public static final String NSD_SERVICE = "servicediscovery"; 2355 2356 /** 2357 * Use with {@link #getSystemService} to retrieve a 2358 * {@link android.media.AudioManager} for handling management of volume, 2359 * ringer modes and audio routing. 2360 * 2361 * @see #getSystemService 2362 * @see android.media.AudioManager 2363 */ 2364 public static final String AUDIO_SERVICE = "audio"; 2365 2366 /** 2367 * Use with {@link #getSystemService} to retrieve a 2368 * {@link android.media.MediaRouter} for controlling and managing 2369 * routing of media. 2370 * 2371 * @see #getSystemService 2372 * @see android.media.MediaRouter 2373 */ 2374 public static final String MEDIA_ROUTER_SERVICE = "media_router"; 2375 2376 /** 2377 * Use with {@link #getSystemService} to retrieve a 2378 * {@link android.media.session.MediaSessionManager} for managing media Sessions. 2379 * 2380 * @see #getSystemService 2381 * @see android.media.session.MediaSessionManager 2382 */ 2383 public static final String MEDIA_SESSION_SERVICE = "media_session"; 2384 2385 /** 2386 * Use with {@link #getSystemService} to retrieve a 2387 * {@link android.telephony.TelephonyManager} for handling management the 2388 * telephony features of the device. 2389 * 2390 * @see #getSystemService 2391 * @see android.telephony.TelephonyManager 2392 */ 2393 public static final String TELEPHONY_SERVICE = "phone"; 2394 2395 /** 2396 * Use with {@link #getSystemService} to retrieve a 2397 * {@link android.text.ClipboardManager} for accessing and modifying 2398 * the contents of the global clipboard. 2399 * 2400 * @see #getSystemService 2401 * @see android.text.ClipboardManager 2402 */ 2403 public static final String CLIPBOARD_SERVICE = "clipboard"; 2404 2405 /** 2406 * Use with {@link #getSystemService} to retrieve a 2407 * {@link android.view.inputmethod.InputMethodManager} for accessing input 2408 * methods. 2409 * 2410 * @see #getSystemService 2411 */ 2412 public static final String INPUT_METHOD_SERVICE = "input_method"; 2413 2414 /** 2415 * Use with {@link #getSystemService} to retrieve a 2416 * {@link android.view.textservice.TextServicesManager} for accessing 2417 * text services. 2418 * 2419 * @see #getSystemService 2420 */ 2421 public static final String TEXT_SERVICES_MANAGER_SERVICE = "textservices"; 2422 2423 /** 2424 * Use with {@link #getSystemService} to retrieve a 2425 * {@link android.appwidget.AppWidgetManager} for accessing AppWidgets. 2426 * 2427 * @hide 2428 * @see #getSystemService 2429 */ 2430 public static final String APPWIDGET_SERVICE = "appwidget"; 2431 2432 /** 2433 * Use with {@link #getSystemService} to retrieve an 2434 * {@link android.app.backup.IBackupManager IBackupManager} for communicating 2435 * with the backup mechanism. 2436 * @hide 2437 * 2438 * @see #getSystemService 2439 */ 2440 public static final String BACKUP_SERVICE = "backup"; 2441 2442 /** 2443 * Use with {@link #getSystemService} to retrieve a 2444 * {@link android.os.DropBoxManager} instance for recording 2445 * diagnostic logs. 2446 * @see #getSystemService 2447 */ 2448 public static final String DROPBOX_SERVICE = "dropbox"; 2449 2450 /** 2451 * Use with {@link #getSystemService} to retrieve a 2452 * {@link android.app.admin.DevicePolicyManager} for working with global 2453 * device policy management. 2454 * 2455 * @see #getSystemService 2456 */ 2457 public static final String DEVICE_POLICY_SERVICE = "device_policy"; 2458 2459 /** 2460 * Use with {@link #getSystemService} to retrieve a 2461 * {@link android.app.UiModeManager} for controlling UI modes. 2462 * 2463 * @see #getSystemService 2464 */ 2465 public static final String UI_MODE_SERVICE = "uimode"; 2466 2467 /** 2468 * Use with {@link #getSystemService} to retrieve a 2469 * {@link android.app.DownloadManager} for requesting HTTP downloads. 2470 * 2471 * @see #getSystemService 2472 */ 2473 public static final String DOWNLOAD_SERVICE = "download"; 2474 2475 /** 2476 * Use with {@link #getSystemService} to retrieve a 2477 * {@link android.nfc.NfcManager} for using NFC. 2478 * 2479 * @see #getSystemService 2480 */ 2481 public static final String NFC_SERVICE = "nfc"; 2482 2483 /** 2484 * Use with {@link #getSystemService} to retrieve a 2485 * {@link android.bluetooth.BluetoothAdapter} for using Bluetooth. 2486 * 2487 * @see #getSystemService 2488 */ 2489 public static final String BLUETOOTH_SERVICE = "bluetooth"; 2490 2491 /** 2492 * Use with {@link #getSystemService} to retrieve a 2493 * {@link android.net.sip.SipManager} for accessing the SIP related service. 2494 * 2495 * @see #getSystemService 2496 */ 2497 /** @hide */ 2498 public static final String SIP_SERVICE = "sip"; 2499 2500 /** 2501 * Use with {@link #getSystemService} to retrieve a {@link 2502 * android.hardware.usb.UsbManager} for access to USB devices (as a USB host) 2503 * and for controlling this device's behavior as a USB device. 2504 * 2505 * @see #getSystemService 2506 * @see android.hardware.usb.UsbManager 2507 */ 2508 public static final String USB_SERVICE = "usb"; 2509 2510 /** 2511 * Use with {@link #getSystemService} to retrieve a {@link 2512 * android.hardware.SerialManager} for access to serial ports. 2513 * 2514 * @see #getSystemService 2515 * @see android.hardware.SerialManager 2516 * 2517 * @hide 2518 */ 2519 public static final String SERIAL_SERVICE = "serial"; 2520 2521 /** 2522 * Use with {@link #getSystemService} to retrieve a 2523 * {@link android.hardware.hdmi.HdmiCecManager for controlling and managing 2524 * HDMI-CEC protocol. 2525 * 2526 * @see #getSystemService 2527 * @see android.hardware.hdmi.HdmiCecManager 2528 */ 2529 public static final String HDMI_CEC_SERVICE = "hdmi_cec"; 2530 2531 /** 2532 * Use with {@link #getSystemService} to retrieve a 2533 * {@link android.hardware.input.InputManager} for interacting with input devices. 2534 * 2535 * @see #getSystemService 2536 * @see android.hardware.input.InputManager 2537 */ 2538 public static final String INPUT_SERVICE = "input"; 2539 2540 /** 2541 * Use with {@link #getSystemService} to retrieve a 2542 * {@link android.hardware.display.DisplayManager} for interacting with display devices. 2543 * 2544 * @see #getSystemService 2545 * @see android.hardware.display.DisplayManager 2546 */ 2547 public static final String DISPLAY_SERVICE = "display"; 2548 2549 /** 2550 * Use with {@link #getSystemService} to retrieve a 2551 * {@link android.os.UserManager} for managing users on devices that support multiple users. 2552 * 2553 * @see #getSystemService 2554 * @see android.os.UserManager 2555 */ 2556 public static final String USER_SERVICE = "user"; 2557 2558 /** 2559 * Use with {@link #getSystemService} to retrieve a 2560 * {@link android.app.AppOpsManager} for tracking application operations 2561 * on the device. 2562 * 2563 * @see #getSystemService 2564 * @see android.app.AppOpsManager 2565 */ 2566 public static final String APP_OPS_SERVICE = "appops"; 2567 2568 /** 2569 * Use with {@link #getSystemService} to retrieve a 2570 * {@link android.hardware.camera2.CameraManager} for interacting with 2571 * camera devices. 2572 * 2573 * @see #getSystemService 2574 * @see android.hardware.camera2.CameraManager 2575 */ 2576 public static final String CAMERA_SERVICE = "camera"; 2577 2578 /** 2579 * {@link android.print.PrintManager} for printing and managing 2580 * printers and print tasks. 2581 * 2582 * @see #getSystemService 2583 * @see android.print.PrintManager 2584 */ 2585 public static final String PRINT_SERVICE = "print"; 2586 2587 /** 2588 * Use with {@link #getSystemService} to retrieve a 2589 * {@link android.hardware.ConsumerIrManager} for transmitting infrared 2590 * signals from the device. 2591 * 2592 * @see #getSystemService 2593 * @see android.hardware.ConsumerIrManager 2594 */ 2595 public static final String CONSUMER_IR_SERVICE = "consumer_ir"; 2596 2597 /** 2598 * {@link android.app.trust.TrustManager} for managing trust agents. 2599 * @see #getSystemService 2600 * @see android.app.trust.TrustManager 2601 * @hide 2602 */ 2603 public static final String TRUST_SERVICE = "trust"; 2604 2605 /** 2606 * Determine whether the given permission is allowed for a particular 2607 * process and user ID running in the system. 2608 * 2609 * @param permission The name of the permission being checked. 2610 * @param pid The process ID being checked against. Must be > 0. 2611 * @param uid The user ID being checked against. A uid of 0 is the root 2612 * user, which will pass every permission check. 2613 * 2614 * @return {@link PackageManager#PERMISSION_GRANTED} if the given 2615 * pid/uid is allowed that permission, or 2616 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2617 * 2618 * @see PackageManager#checkPermission(String, String) 2619 * @see #checkCallingPermission 2620 */ 2621 @PackageManager.PermissionResult 2622 public abstract int checkPermission(@NonNull String permission, int pid, int uid); 2623 2624 /** 2625 * Determine whether the calling process of an IPC you are handling has been 2626 * granted a particular permission. This is basically the same as calling 2627 * {@link #checkPermission(String, int, int)} with the pid and uid returned 2628 * by {@link android.os.Binder#getCallingPid} and 2629 * {@link android.os.Binder#getCallingUid}. One important difference 2630 * is that if you are not currently processing an IPC, this function 2631 * will always fail. This is done to protect against accidentally 2632 * leaking permissions; you can use {@link #checkCallingOrSelfPermission} 2633 * to avoid this protection. 2634 * 2635 * @param permission The name of the permission being checked. 2636 * 2637 * @return {@link PackageManager#PERMISSION_GRANTED} if the calling 2638 * pid/uid is allowed that permission, or 2639 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2640 * 2641 * @see PackageManager#checkPermission(String, String) 2642 * @see #checkPermission 2643 * @see #checkCallingOrSelfPermission 2644 */ 2645 @PackageManager.PermissionResult 2646 public abstract int checkCallingPermission(@NonNull String permission); 2647 2648 /** 2649 * Determine whether the calling process of an IPC <em>or you</em> have been 2650 * granted a particular permission. This is the same as 2651 * {@link #checkCallingPermission}, except it grants your own permissions 2652 * if you are not currently processing an IPC. Use with care! 2653 * 2654 * @param permission The name of the permission being checked. 2655 * 2656 * @return {@link PackageManager#PERMISSION_GRANTED} if the calling 2657 * pid/uid is allowed that permission, or 2658 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2659 * 2660 * @see PackageManager#checkPermission(String, String) 2661 * @see #checkPermission 2662 * @see #checkCallingPermission 2663 */ 2664 @PackageManager.PermissionResult 2665 public abstract int checkCallingOrSelfPermission(@NonNull String permission); 2666 2667 /** 2668 * If the given permission is not allowed for a particular process 2669 * and user ID running in the system, throw a {@link SecurityException}. 2670 * 2671 * @param permission The name of the permission being checked. 2672 * @param pid The process ID being checked against. Must be > 0. 2673 * @param uid The user ID being checked against. A uid of 0 is the root 2674 * user, which will pass every permission check. 2675 * @param message A message to include in the exception if it is thrown. 2676 * 2677 * @see #checkPermission(String, int, int) 2678 */ 2679 public abstract void enforcePermission( 2680 @NonNull String permission, int pid, int uid, @Nullable String message); 2681 2682 /** 2683 * If the calling process of an IPC you are handling has not been 2684 * granted a particular permission, throw a {@link 2685 * SecurityException}. This is basically the same as calling 2686 * {@link #enforcePermission(String, int, int, String)} with the 2687 * pid and uid returned by {@link android.os.Binder#getCallingPid} 2688 * and {@link android.os.Binder#getCallingUid}. One important 2689 * difference is that if you are not currently processing an IPC, 2690 * this function will always throw the SecurityException. This is 2691 * done to protect against accidentally leaking permissions; you 2692 * can use {@link #enforceCallingOrSelfPermission} to avoid this 2693 * protection. 2694 * 2695 * @param permission The name of the permission being checked. 2696 * @param message A message to include in the exception if it is thrown. 2697 * 2698 * @see #checkCallingPermission(String) 2699 */ 2700 public abstract void enforceCallingPermission( 2701 @NonNull String permission, @Nullable String message); 2702 2703 /** 2704 * If neither you nor the calling process of an IPC you are 2705 * handling has been granted a particular permission, throw a 2706 * {@link SecurityException}. This is the same as {@link 2707 * #enforceCallingPermission}, except it grants your own 2708 * permissions if you are not currently processing an IPC. Use 2709 * with care! 2710 * 2711 * @param permission The name of the permission being checked. 2712 * @param message A message to include in the exception if it is thrown. 2713 * 2714 * @see #checkCallingOrSelfPermission(String) 2715 */ 2716 public abstract void enforceCallingOrSelfPermission( 2717 @NonNull String permission, @Nullable String message); 2718 2719 /** 2720 * Grant permission to access a specific Uri to another package, regardless 2721 * of whether that package has general permission to access the Uri's 2722 * content provider. This can be used to grant specific, temporary 2723 * permissions, typically in response to user interaction (such as the 2724 * user opening an attachment that you would like someone else to 2725 * display). 2726 * 2727 * <p>Normally you should use {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 2728 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2729 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 2730 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} with the Intent being used to 2731 * start an activity instead of this function directly. If you use this 2732 * function directly, you should be sure to call 2733 * {@link #revokeUriPermission} when the target should no longer be allowed 2734 * to access it. 2735 * 2736 * <p>To succeed, the content provider owning the Uri must have set the 2737 * {@link android.R.styleable#AndroidManifestProvider_grantUriPermissions 2738 * grantUriPermissions} attribute in its manifest or included the 2739 * {@link android.R.styleable#AndroidManifestGrantUriPermission 2740 * <grant-uri-permissions>} tag. 2741 * 2742 * @param toPackage The package you would like to allow to access the Uri. 2743 * @param uri The Uri you would like to grant access to. 2744 * @param modeFlags The desired access modes. Any combination of 2745 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 2746 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2747 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 2748 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2749 * 2750 * @see #revokeUriPermission 2751 */ 2752 public abstract void grantUriPermission(String toPackage, Uri uri, 2753 @Intent.GrantUriMode int modeFlags); 2754 2755 /** 2756 * Remove all permissions to access a particular content provider Uri 2757 * that were previously added with {@link #grantUriPermission}. The given 2758 * Uri will match all previously granted Uris that are the same or a 2759 * sub-path of the given Uri. That is, revoking "content://foo/target" will 2760 * revoke both "content://foo/target" and "content://foo/target/sub", but not 2761 * "content://foo". 2762 * 2763 * @param uri The Uri you would like to revoke access to. 2764 * @param modeFlags The desired access modes. Any combination of 2765 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 2766 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2767 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 2768 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2769 * 2770 * @see #grantUriPermission 2771 */ 2772 public abstract void revokeUriPermission(Uri uri, @Intent.GrantUriMode int modeFlags); 2773 2774 /** 2775 * Determine whether a particular process and user ID has been granted 2776 * permission to access a specific URI. This only checks for permissions 2777 * that have been explicitly granted -- if the given process/uid has 2778 * more general access to the URI's content provider then this check will 2779 * always fail. 2780 * 2781 * @param uri The uri that is being checked. 2782 * @param pid The process ID being checked against. Must be > 0. 2783 * @param uid The user ID being checked against. A uid of 0 is the root 2784 * user, which will pass every permission check. 2785 * @param modeFlags The type of access to grant. May be one or both of 2786 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2787 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2788 * 2789 * @return {@link PackageManager#PERMISSION_GRANTED} if the given 2790 * pid/uid is allowed to access that uri, or 2791 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2792 * 2793 * @see #checkCallingUriPermission 2794 */ 2795 public abstract int checkUriPermission(Uri uri, int pid, int uid, 2796 @Intent.GrantUriMode int modeFlags); 2797 2798 /** 2799 * Determine whether the calling process and user ID has been 2800 * granted permission to access a specific URI. This is basically 2801 * the same as calling {@link #checkUriPermission(Uri, int, int, 2802 * int)} with the pid and uid returned by {@link 2803 * android.os.Binder#getCallingPid} and {@link 2804 * android.os.Binder#getCallingUid}. One important difference is 2805 * that if you are not currently processing an IPC, this function 2806 * will always fail. 2807 * 2808 * @param uri The uri that is being checked. 2809 * @param modeFlags The type of access to grant. May be one or both of 2810 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2811 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2812 * 2813 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 2814 * is allowed to access that uri, or 2815 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2816 * 2817 * @see #checkUriPermission(Uri, int, int, int) 2818 */ 2819 public abstract int checkCallingUriPermission(Uri uri, @Intent.GrantUriMode int modeFlags); 2820 2821 /** 2822 * Determine whether the calling process of an IPC <em>or you</em> has been granted 2823 * permission to access a specific URI. This is the same as 2824 * {@link #checkCallingUriPermission}, except it grants your own permissions 2825 * if you are not currently processing an IPC. Use with care! 2826 * 2827 * @param uri The uri that is being checked. 2828 * @param modeFlags The type of access to grant. May be one or both of 2829 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2830 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2831 * 2832 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 2833 * is allowed to access that uri, or 2834 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2835 * 2836 * @see #checkCallingUriPermission 2837 */ 2838 public abstract int checkCallingOrSelfUriPermission(Uri uri, 2839 @Intent.GrantUriMode int modeFlags); 2840 2841 /** 2842 * Check both a Uri and normal permission. This allows you to perform 2843 * both {@link #checkPermission} and {@link #checkUriPermission} in one 2844 * call. 2845 * 2846 * @param uri The Uri whose permission is to be checked, or null to not 2847 * do this check. 2848 * @param readPermission The permission that provides overall read access, 2849 * or null to not do this check. 2850 * @param writePermission The permission that provides overall write 2851 * access, or null to not do this check. 2852 * @param pid The process ID being checked against. Must be > 0. 2853 * @param uid The user ID being checked against. A uid of 0 is the root 2854 * user, which will pass every permission check. 2855 * @param modeFlags The type of access to grant. May be one or both of 2856 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2857 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2858 * 2859 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 2860 * is allowed to access that uri or holds one of the given permissions, or 2861 * {@link PackageManager#PERMISSION_DENIED} if it is not. 2862 */ 2863 public abstract int checkUriPermission(@Nullable Uri uri, @Nullable String readPermission, 2864 @Nullable String writePermission, int pid, int uid, 2865 @Intent.GrantUriMode int modeFlags); 2866 2867 /** 2868 * If a particular process and user ID has not been granted 2869 * permission to access a specific URI, throw {@link 2870 * SecurityException}. This only checks for permissions that have 2871 * been explicitly granted -- if the given process/uid has more 2872 * general access to the URI's content provider then this check 2873 * will always fail. 2874 * 2875 * @param uri The uri that is being checked. 2876 * @param pid The process ID being checked against. Must be > 0. 2877 * @param uid The user ID being checked against. A uid of 0 is the root 2878 * user, which will pass every permission check. 2879 * @param modeFlags The type of access to grant. May be one or both of 2880 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2881 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2882 * @param message A message to include in the exception if it is thrown. 2883 * 2884 * @see #checkUriPermission(Uri, int, int, int) 2885 */ 2886 public abstract void enforceUriPermission( 2887 Uri uri, int pid, int uid, @Intent.GrantUriMode int modeFlags, String message); 2888 2889 /** 2890 * If the calling process and user ID has not been granted 2891 * permission to access a specific URI, throw {@link 2892 * SecurityException}. This is basically the same as calling 2893 * {@link #enforceUriPermission(Uri, int, int, int, String)} with 2894 * the pid and uid returned by {@link 2895 * android.os.Binder#getCallingPid} and {@link 2896 * android.os.Binder#getCallingUid}. One important difference is 2897 * that if you are not currently processing an IPC, this function 2898 * will always throw a SecurityException. 2899 * 2900 * @param uri The uri that is being checked. 2901 * @param modeFlags The type of access to grant. May be one or both of 2902 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2903 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2904 * @param message A message to include in the exception if it is thrown. 2905 * 2906 * @see #checkCallingUriPermission(Uri, int) 2907 */ 2908 public abstract void enforceCallingUriPermission( 2909 Uri uri, @Intent.GrantUriMode int modeFlags, String message); 2910 2911 /** 2912 * If the calling process of an IPC <em>or you</em> has not been 2913 * granted permission to access a specific URI, throw {@link 2914 * SecurityException}. This is the same as {@link 2915 * #enforceCallingUriPermission}, except it grants your own 2916 * permissions if you are not currently processing an IPC. Use 2917 * with care! 2918 * 2919 * @param uri The uri that is being checked. 2920 * @param modeFlags The type of access to grant. May be one or both of 2921 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2922 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2923 * @param message A message to include in the exception if it is thrown. 2924 * 2925 * @see #checkCallingOrSelfUriPermission(Uri, int) 2926 */ 2927 public abstract void enforceCallingOrSelfUriPermission( 2928 Uri uri, @Intent.GrantUriMode int modeFlags, String message); 2929 2930 /** 2931 * Enforce both a Uri and normal permission. This allows you to perform 2932 * both {@link #enforcePermission} and {@link #enforceUriPermission} in one 2933 * call. 2934 * 2935 * @param uri The Uri whose permission is to be checked, or null to not 2936 * do this check. 2937 * @param readPermission The permission that provides overall read access, 2938 * or null to not do this check. 2939 * @param writePermission The permission that provides overall write 2940 * access, or null to not do this check. 2941 * @param pid The process ID being checked against. Must be > 0. 2942 * @param uid The user ID being checked against. A uid of 0 is the root 2943 * user, which will pass every permission check. 2944 * @param modeFlags The type of access to grant. May be one or both of 2945 * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or 2946 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}. 2947 * @param message A message to include in the exception if it is thrown. 2948 * 2949 * @see #checkUriPermission(Uri, String, String, int, int, int) 2950 */ 2951 public abstract void enforceUriPermission( 2952 @Nullable Uri uri, @Nullable String readPermission, 2953 @Nullable String writePermission, int pid, int uid, @Intent.GrantUriMode int modeFlags, 2954 @Nullable String message); 2955 2956 /** @hide */ 2957 @IntDef(flag = true, 2958 value = {CONTEXT_INCLUDE_CODE, CONTEXT_IGNORE_SECURITY, CONTEXT_RESTRICTED}) 2959 @Retention(RetentionPolicy.SOURCE) 2960 public @interface CreatePackageOptions {} 2961 2962 /** 2963 * Flag for use with {@link #createPackageContext}: include the application 2964 * code with the context. This means loading code into the caller's 2965 * process, so that {@link #getClassLoader()} can be used to instantiate 2966 * the application's classes. Setting this flags imposes security 2967 * restrictions on what application context you can access; if the 2968 * requested application can not be safely loaded into your process, 2969 * java.lang.SecurityException will be thrown. If this flag is not set, 2970 * there will be no restrictions on the packages that can be loaded, 2971 * but {@link #getClassLoader} will always return the default system 2972 * class loader. 2973 */ 2974 public static final int CONTEXT_INCLUDE_CODE = 0x00000001; 2975 2976 /** 2977 * Flag for use with {@link #createPackageContext}: ignore any security 2978 * restrictions on the Context being requested, allowing it to always 2979 * be loaded. For use with {@link #CONTEXT_INCLUDE_CODE} to allow code 2980 * to be loaded into a process even when it isn't safe to do so. Use 2981 * with extreme care! 2982 */ 2983 public static final int CONTEXT_IGNORE_SECURITY = 0x00000002; 2984 2985 /** 2986 * Flag for use with {@link #createPackageContext}: a restricted context may 2987 * disable specific features. For instance, a View associated with a restricted 2988 * context would ignore particular XML attributes. 2989 */ 2990 public static final int CONTEXT_RESTRICTED = 0x00000004; 2991 2992 /** 2993 * Return a new Context object for the given application name. This 2994 * Context is the same as what the named application gets when it is 2995 * launched, containing the same resources and class loader. Each call to 2996 * this method returns a new instance of a Context object; Context objects 2997 * are not shared, however they share common state (Resources, ClassLoader, 2998 * etc) so the Context instance itself is fairly lightweight. 2999 * 3000 * <p>Throws {@link PackageManager.NameNotFoundException} if there is no 3001 * application with the given package name. 3002 * 3003 * <p>Throws {@link java.lang.SecurityException} if the Context requested 3004 * can not be loaded into the caller's process for security reasons (see 3005 * {@link #CONTEXT_INCLUDE_CODE} for more information}. 3006 * 3007 * @param packageName Name of the application's package. 3008 * @param flags Option flags, one of {@link #CONTEXT_INCLUDE_CODE} 3009 * or {@link #CONTEXT_IGNORE_SECURITY}. 3010 * 3011 * @return A {@link Context} for the application. 3012 * 3013 * @throws SecurityException 3014 * @throws PackageManager.NameNotFoundException if there is no application with 3015 * the given package name. 3016 */ 3017 public abstract Context createPackageContext(String packageName, 3018 @CreatePackageOptions int flags) throws PackageManager.NameNotFoundException; 3019 3020 /** 3021 * Similar to {@link #createPackageContext(String, int)}, but with a 3022 * different {@link UserHandle}. For example, {@link #getContentResolver()} 3023 * will open any {@link Uri} as the given user. 3024 * 3025 * @hide 3026 */ 3027 public abstract Context createPackageContextAsUser( 3028 String packageName, int flags, UserHandle user) 3029 throws PackageManager.NameNotFoundException; 3030 3031 /** 3032 * Get the userId associated with this context 3033 * @return user id 3034 * 3035 * @hide 3036 */ 3037 public abstract int getUserId(); 3038 3039 /** 3040 * Return a new Context object for the current Context but whose resources 3041 * are adjusted to match the given Configuration. Each call to this method 3042 * returns a new instance of a Context object; Context objects are not 3043 * shared, however common state (ClassLoader, other Resources for the 3044 * same configuration) may be so the Context itself can be fairly lightweight. 3045 * 3046 * @param overrideConfiguration A {@link Configuration} specifying what 3047 * values to modify in the base Configuration of the original Context's 3048 * resources. If the base configuration changes (such as due to an 3049 * orientation change), the resources of this context will also change except 3050 * for those that have been explicitly overridden with a value here. 3051 * 3052 * @return A {@link Context} with the given configuration override. 3053 */ 3054 public abstract Context createConfigurationContext( 3055 @NonNull Configuration overrideConfiguration); 3056 3057 /** 3058 * Return a new Context object for the current Context but whose resources 3059 * are adjusted to match the metrics of the given Display. Each call to this method 3060 * returns a new instance of a Context object; Context objects are not 3061 * shared, however common state (ClassLoader, other Resources for the 3062 * same configuration) may be so the Context itself can be fairly lightweight. 3063 * 3064 * The returned display Context provides a {@link WindowManager} 3065 * (see {@link #getSystemService(String)}) that is configured to show windows 3066 * on the given display. The WindowManager's {@link WindowManager#getDefaultDisplay} 3067 * method can be used to retrieve the Display from the returned Context. 3068 * 3069 * @param display A {@link Display} object specifying the display 3070 * for whose metrics the Context's resources should be tailored and upon which 3071 * new windows should be shown. 3072 * 3073 * @return A {@link Context} for the display. 3074 */ 3075 public abstract Context createDisplayContext(@NonNull Display display); 3076 3077 /** 3078 * Gets the display adjustments holder for this context. This information 3079 * is provided on a per-application or activity basis and is used to simulate lower density 3080 * display metrics for legacy applications and restricted screen sizes. 3081 * 3082 * @param displayId The display id for which to get compatibility info. 3083 * @return The compatibility info holder, or null if not required by the application. 3084 * @hide 3085 */ 3086 public abstract DisplayAdjustments getDisplayAdjustments(int displayId); 3087 3088 /** 3089 * Indicates whether this Context is restricted. 3090 * 3091 * @return {@code true} if this Context is restricted, {@code false} otherwise. 3092 * 3093 * @see #CONTEXT_RESTRICTED 3094 */ 3095 public boolean isRestricted() { 3096 return false; 3097 } 3098} 3099