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