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