Context.java revision d999d225a8ebd5662b61d7f67bb402d8e5cb965b
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.AttrRes; 20import android.annotation.CheckResult; 21import android.annotation.ColorInt; 22import android.annotation.ColorRes; 23import android.annotation.DrawableRes; 24import android.annotation.IntDef; 25import android.annotation.NonNull; 26import android.annotation.Nullable; 27import android.annotation.RequiresPermission; 28import android.annotation.StringDef; 29import android.annotation.StringRes; 30import android.annotation.StyleRes; 31import android.annotation.StyleableRes; 32import android.annotation.SystemApi; 33import android.annotation.TestApi; 34import android.annotation.UserIdInt; 35import android.app.IApplicationThread; 36import android.app.IServiceConnection; 37import android.app.Notification; 38import android.app.VrManager; 39import android.content.pm.ApplicationInfo; 40import android.content.pm.PackageManager; 41import android.content.res.AssetManager; 42import android.content.res.ColorStateList; 43import android.content.res.Configuration; 44import android.content.res.Resources; 45import android.content.res.TypedArray; 46import android.database.DatabaseErrorHandler; 47import android.database.sqlite.SQLiteDatabase; 48import android.database.sqlite.SQLiteDatabase.CursorFactory; 49import android.graphics.Bitmap; 50import android.graphics.drawable.Drawable; 51import android.net.Uri; 52import android.os.Bundle; 53import android.os.Environment; 54import android.os.Handler; 55import android.os.IBinder; 56import android.os.Looper; 57import android.os.StatFs; 58import android.os.UserHandle; 59import android.os.UserManager; 60import android.os.storage.StorageManager; 61import android.provider.MediaStore; 62import android.util.AttributeSet; 63import android.view.Display; 64import android.view.DisplayAdjustments; 65import android.view.View; 66import android.view.ViewDebug; 67import android.view.WindowManager; 68import android.view.textclassifier.TextClassificationManager; 69 70import java.io.File; 71import java.io.FileInputStream; 72import java.io.FileNotFoundException; 73import java.io.FileOutputStream; 74import java.io.IOException; 75import java.io.InputStream; 76import java.lang.annotation.Retention; 77import java.lang.annotation.RetentionPolicy; 78 79/** 80 * Interface to global information about an application environment. This is 81 * an abstract class whose implementation is provided by 82 * the Android system. It 83 * allows access to application-specific resources and classes, as well as 84 * up-calls for application-level operations such as launching activities, 85 * broadcasting and receiving intents, etc. 86 */ 87public abstract class Context { 88 /** @hide */ 89 @IntDef(flag = true, prefix = { "MODE_" }, value = { 90 MODE_PRIVATE, 91 MODE_WORLD_READABLE, 92 MODE_WORLD_WRITEABLE, 93 MODE_APPEND, 94 }) 95 @Retention(RetentionPolicy.SOURCE) 96 public @interface FileMode {} 97 98 /** @hide */ 99 @IntDef(flag = true, prefix = { "MODE_" }, value = { 100 MODE_PRIVATE, 101 MODE_WORLD_READABLE, 102 MODE_WORLD_WRITEABLE, 103 MODE_MULTI_PROCESS, 104 }) 105 @Retention(RetentionPolicy.SOURCE) 106 public @interface PreferencesMode {} 107 108 /** @hide */ 109 @IntDef(flag = true, prefix = { "MODE_" }, value = { 110 MODE_PRIVATE, 111 MODE_WORLD_READABLE, 112 MODE_WORLD_WRITEABLE, 113 MODE_ENABLE_WRITE_AHEAD_LOGGING, 114 MODE_NO_LOCALIZED_COLLATORS, 115 }) 116 @Retention(RetentionPolicy.SOURCE) 117 public @interface DatabaseMode {} 118 119 /** 120 * File creation mode: the default mode, where the created file can only 121 * be accessed by the calling application (or all applications sharing the 122 * same user ID). 123 */ 124 public static final int MODE_PRIVATE = 0x0000; 125 126 /** 127 * File creation mode: allow all other applications to have read access to 128 * the created file. 129 * <p> 130 * As of {@link android.os.Build.VERSION_CODES#N} attempting to use this 131 * mode will throw a {@link SecurityException}. 132 * 133 * @deprecated Creating world-readable files is very dangerous, and likely 134 * to cause security holes in applications. It is strongly 135 * discouraged; instead, applications should use more formal 136 * mechanism for interactions such as {@link ContentProvider}, 137 * {@link BroadcastReceiver}, and {@link android.app.Service}. 138 * There are no guarantees that this access mode will remain on 139 * a file, such as when it goes through a backup and restore. 140 * @see android.support.v4.content.FileProvider 141 * @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION 142 */ 143 @Deprecated 144 public static final int MODE_WORLD_READABLE = 0x0001; 145 146 /** 147 * File creation mode: allow all other applications to have write access to 148 * the created file. 149 * <p> 150 * As of {@link android.os.Build.VERSION_CODES#N} attempting to use this 151 * mode will throw a {@link SecurityException}. 152 * 153 * @deprecated Creating world-writable files is very dangerous, and likely 154 * to cause security holes in applications. It is strongly 155 * discouraged; instead, applications should use more formal 156 * mechanism for interactions such as {@link ContentProvider}, 157 * {@link BroadcastReceiver}, and {@link android.app.Service}. 158 * There are no guarantees that this access mode will remain on 159 * a file, such as when it goes through a backup and restore. 160 * @see android.support.v4.content.FileProvider 161 * @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION 162 */ 163 @Deprecated 164 public static final int MODE_WORLD_WRITEABLE = 0x0002; 165 166 /** 167 * File creation mode: for use with {@link #openFileOutput}, if the file 168 * already exists then write data to the end of the existing file 169 * instead of erasing it. 170 * @see #openFileOutput 171 */ 172 public static final int MODE_APPEND = 0x8000; 173 174 /** 175 * SharedPreference loading flag: when set, the file on disk will 176 * be checked for modification even if the shared preferences 177 * instance is already loaded in this process. This behavior is 178 * sometimes desired in cases where the application has multiple 179 * processes, all writing to the same SharedPreferences file. 180 * Generally there are better forms of communication between 181 * processes, though. 182 * 183 * <p>This was the legacy (but undocumented) behavior in and 184 * before Gingerbread (Android 2.3) and this flag is implied when 185 * targetting such releases. For applications targetting SDK 186 * versions <em>greater than</em> Android 2.3, this flag must be 187 * explicitly set if desired. 188 * 189 * @see #getSharedPreferences 190 * 191 * @deprecated MODE_MULTI_PROCESS does not work reliably in 192 * some versions of Android, and furthermore does not provide any 193 * mechanism for reconciling concurrent modifications across 194 * processes. Applications should not attempt to use it. Instead, 195 * they should use an explicit cross-process data management 196 * approach such as {@link android.content.ContentProvider ContentProvider}. 197 */ 198 @Deprecated 199 public static final int MODE_MULTI_PROCESS = 0x0004; 200 201 /** 202 * Database open flag: when set, the database is opened with write-ahead 203 * logging enabled by default. 204 * 205 * @see #openOrCreateDatabase(String, int, CursorFactory) 206 * @see #openOrCreateDatabase(String, int, CursorFactory, DatabaseErrorHandler) 207 * @see SQLiteDatabase#enableWriteAheadLogging 208 */ 209 public static final int MODE_ENABLE_WRITE_AHEAD_LOGGING = 0x0008; 210 211 /** 212 * Database open flag: when set, the database is opened without support for 213 * localized collators. 214 * 215 * @see #openOrCreateDatabase(String, int, CursorFactory) 216 * @see #openOrCreateDatabase(String, int, CursorFactory, DatabaseErrorHandler) 217 * @see SQLiteDatabase#NO_LOCALIZED_COLLATORS 218 */ 219 public static final int MODE_NO_LOCALIZED_COLLATORS = 0x0010; 220 221 /** @hide */ 222 @IntDef(flag = true, 223 value = { 224 BIND_AUTO_CREATE, 225 BIND_DEBUG_UNBIND, 226 BIND_NOT_FOREGROUND, 227 BIND_ABOVE_CLIENT, 228 BIND_ALLOW_OOM_MANAGEMENT, 229 BIND_WAIVE_PRIORITY, 230 BIND_IMPORTANT, 231 BIND_ADJUST_WITH_ACTIVITY 232 }) 233 @Retention(RetentionPolicy.SOURCE) 234 public @interface BindServiceFlags {} 235 236 /** 237 * Flag for {@link #bindService}: automatically create the service as long 238 * as the binding exists. Note that while this will create the service, 239 * its {@link android.app.Service#onStartCommand} 240 * method will still only be called due to an 241 * explicit call to {@link #startService}. Even without that, though, 242 * this still provides you with access to the service object while the 243 * service is created. 244 * 245 * <p>Note that prior to {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, 246 * not supplying this flag would also impact how important the system 247 * consider's the target service's process to be. When set, the only way 248 * for it to be raised was by binding from a service in which case it will 249 * only be important when that activity is in the foreground. Now to 250 * achieve this behavior you must explicitly supply the new flag 251 * {@link #BIND_ADJUST_WITH_ACTIVITY}. For compatibility, old applications 252 * that don't specify {@link #BIND_AUTO_CREATE} will automatically have 253 * the flags {@link #BIND_WAIVE_PRIORITY} and 254 * {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve 255 * the same result. 256 */ 257 public static final int BIND_AUTO_CREATE = 0x0001; 258 259 /** 260 * Flag for {@link #bindService}: include debugging help for mismatched 261 * calls to unbind. When this flag is set, the callstack of the following 262 * {@link #unbindService} call is retained, to be printed if a later 263 * incorrect unbind call is made. Note that doing this requires retaining 264 * information about the binding that was made for the lifetime of the app, 265 * resulting in a leak -- this should only be used for debugging. 266 */ 267 public static final int BIND_DEBUG_UNBIND = 0x0002; 268 269 /** 270 * Flag for {@link #bindService}: don't allow this binding to raise 271 * the target service's process to the foreground scheduling priority. 272 * It will still be raised to at least the same memory priority 273 * as the client (so that its process will not be killable in any 274 * situation where the client is not killable), but for CPU scheduling 275 * purposes it may be left in the background. This only has an impact 276 * in the situation where the binding client is a foreground process 277 * and the target service is in a background process. 278 */ 279 public static final int BIND_NOT_FOREGROUND = 0x0004; 280 281 /** 282 * Flag for {@link #bindService}: indicates that the client application 283 * binding to this service considers the service to be more important than 284 * the app itself. When set, the platform will try to have the out of 285 * memory killer kill the app before it kills the service it is bound to, though 286 * this is not guaranteed to be the case. 287 */ 288 public static final int BIND_ABOVE_CLIENT = 0x0008; 289 290 /** 291 * Flag for {@link #bindService}: allow the process hosting the bound 292 * service to go through its normal memory management. It will be 293 * treated more like a running service, allowing the system to 294 * (temporarily) expunge the process if low on memory or for some other 295 * whim it may have, and being more aggressive about making it a candidate 296 * to be killed (and restarted) if running for a long time. 297 */ 298 public static final int BIND_ALLOW_OOM_MANAGEMENT = 0x0010; 299 300 /** 301 * Flag for {@link #bindService}: don't impact the scheduling or 302 * memory management priority of the target service's hosting process. 303 * Allows the service's process to be managed on the background LRU list 304 * just like a regular application process in the background. 305 */ 306 public static final int BIND_WAIVE_PRIORITY = 0x0020; 307 308 /** 309 * Flag for {@link #bindService}: this service is very important to 310 * the client, so should be brought to the foreground process level 311 * when the client is. Normally a process can only be raised to the 312 * visibility level by a client, even if that client is in the foreground. 313 */ 314 public static final int BIND_IMPORTANT = 0x0040; 315 316 /** 317 * Flag for {@link #bindService}: If binding from an activity, allow the 318 * target service's process importance to be raised based on whether the 319 * activity is visible to the user, regardless whether another flag is 320 * used to reduce the amount that the client process's overall importance 321 * is used to impact it. 322 */ 323 public static final int BIND_ADJUST_WITH_ACTIVITY = 0x0080; 324 325 /** 326 * @hide Flag for {@link #bindService}: like {@link #BIND_NOT_FOREGROUND}, but puts it 327 * up in to the important background state (instead of transient). 328 */ 329 public static final int BIND_IMPORTANT_BACKGROUND = 0x00800000; 330 331 /** 332 * @hide Flag for {@link #bindService}: allows application hosting service to manage whitelists 333 * such as temporary allowing a {@code PendingIntent} to bypass Power Save mode. 334 */ 335 public static final int BIND_ALLOW_WHITELIST_MANAGEMENT = 0x01000000; 336 337 /** 338 * @hide Flag for {@link #bindService}: Like {@link #BIND_FOREGROUND_SERVICE}, 339 * but only applies while the device is awake. 340 */ 341 public static final int BIND_FOREGROUND_SERVICE_WHILE_AWAKE = 0x02000000; 342 343 /** 344 * @hide Flag for {@link #bindService}: For only the case where the binding 345 * is coming from the system, set the process state to FOREGROUND_SERVICE 346 * instead of the normal maximum of IMPORTANT_FOREGROUND. That is, this is 347 * saying that the process shouldn't participate in the normal power reduction 348 * modes (removing network access etc). 349 */ 350 public static final int BIND_FOREGROUND_SERVICE = 0x04000000; 351 352 /** 353 * @hide Flag for {@link #bindService}: Treat the binding as hosting 354 * an activity, an unbinding as the activity going in the background. 355 * That is, when unbinding, the process when empty will go on the activity 356 * LRU list instead of the regular one, keeping it around more aggressively 357 * than it otherwise would be. This is intended for use with IMEs to try 358 * to keep IME processes around for faster keyboard switching. 359 */ 360 public static final int BIND_TREAT_LIKE_ACTIVITY = 0x08000000; 361 362 /** 363 * @hide An idea that is not yet implemented. 364 * Flag for {@link #bindService}: If binding from an activity, consider 365 * this service to be visible like the binding activity is. That is, 366 * it will be treated as something more important to keep around than 367 * invisible background activities. This will impact the number of 368 * recent activities the user can switch between without having them 369 * restart. There is no guarantee this will be respected, as the system 370 * tries to balance such requests from one app vs. the importantance of 371 * keeping other apps around. 372 */ 373 public static final int BIND_VISIBLE = 0x10000000; 374 375 /** 376 * @hide 377 * Flag for {@link #bindService}: Consider this binding to be causing the target 378 * process to be showing UI, so it will be do a UI_HIDDEN memory trim when it goes 379 * away. 380 */ 381 public static final int BIND_SHOWING_UI = 0x20000000; 382 383 /** 384 * Flag for {@link #bindService}: Don't consider the bound service to be 385 * visible, even if the caller is visible. 386 * @hide 387 */ 388 public static final int BIND_NOT_VISIBLE = 0x40000000; 389 390 /** 391 * Flag for {@link #bindService}: The service being bound is an 392 * {@link android.R.attr#isolatedProcess isolated}, 393 * {@link android.R.attr#externalService external} service. This binds the service into the 394 * calling application's package, rather than the package in which the service is declared. 395 * <p> 396 * When using this flag, the code for the service being bound will execute under the calling 397 * application's package name and user ID. Because the service must be an isolated process, 398 * it will not have direct access to the application's data, though. 399 * 400 * The purpose of this flag is to allow applications to provide services that are attributed 401 * to the app using the service, rather than the application providing the service. 402 * </p> 403 */ 404 public static final int BIND_EXTERNAL_SERVICE = 0x80000000; 405 406 /** @hide */ 407 @IntDef(flag = true, 408 value = { 409 RECEIVER_VISIBLE_TO_INSTANT_APPS 410 }) 411 @Retention(RetentionPolicy.SOURCE) 412 public @interface RegisterReceiverFlags {} 413 414 /** 415 * Flag for {@link #registerReceiver}: The receiver can receive broadcasts from Instant Apps. 416 */ 417 public static final int RECEIVER_VISIBLE_TO_INSTANT_APPS = 0x1; 418 419 /** 420 * Returns an AssetManager instance for the application's package. 421 * <p> 422 * <strong>Note:</strong> Implementations of this method should return 423 * an AssetManager instance that is consistent with the Resources instance 424 * returned by {@link #getResources()}. For example, they should share the 425 * same {@link Configuration} object. 426 * 427 * @return an AssetManager instance for the application's package 428 * @see #getResources() 429 */ 430 public abstract AssetManager getAssets(); 431 432 /** 433 * Returns a Resources instance for the application's package. 434 * <p> 435 * <strong>Note:</strong> Implementations of this method should return 436 * a Resources instance that is consistent with the AssetManager instance 437 * returned by {@link #getAssets()}. For example, they should share the 438 * same {@link Configuration} object. 439 * 440 * @return a Resources instance for the application's package 441 * @see #getAssets() 442 */ 443 public abstract Resources getResources(); 444 445 /** Return PackageManager instance to find global package information. */ 446 public abstract PackageManager getPackageManager(); 447 448 /** Return a ContentResolver instance for your application's package. */ 449 public abstract ContentResolver getContentResolver(); 450 451 /** 452 * Return the Looper for the main thread of the current process. This is 453 * the thread used to dispatch calls to application components (activities, 454 * services, etc). 455 * <p> 456 * By definition, this method returns the same result as would be obtained 457 * by calling {@link Looper#getMainLooper() Looper.getMainLooper()}. 458 * </p> 459 * 460 * @return The main looper. 461 */ 462 public abstract Looper getMainLooper(); 463 464 /** 465 * Return the context of the single, global Application object of the 466 * current process. This generally should only be used if you need a 467 * Context whose lifecycle is separate from the current context, that is 468 * tied to the lifetime of the process rather than the current component. 469 * 470 * <p>Consider for example how this interacts with 471 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)}: 472 * <ul> 473 * <li> <p>If used from an Activity context, the receiver is being registered 474 * within that activity. This means that you are expected to unregister 475 * before the activity is done being destroyed; in fact if you do not do 476 * so, the framework will clean up your leaked registration as it removes 477 * the activity and log an error. Thus, if you use the Activity context 478 * to register a receiver that is static (global to the process, not 479 * associated with an Activity instance) then that registration will be 480 * removed on you at whatever point the activity you used is destroyed. 481 * <li> <p>If used from the Context returned here, the receiver is being 482 * registered with the global state associated with your application. Thus 483 * it will never be unregistered for you. This is necessary if the receiver 484 * is associated with static data, not a particular component. However 485 * using the ApplicationContext elsewhere can easily lead to serious leaks 486 * if you forget to unregister, unbind, etc. 487 * </ul> 488 */ 489 public abstract Context getApplicationContext(); 490 491 /** Non-activity related autofill ids are unique in the app */ 492 private static int sLastAutofillId = View.NO_ID; 493 494 /** 495 * Gets the next autofill ID. 496 * 497 * <p>All IDs will be smaller or the same as {@link View#LAST_APP_AUTOFILL_ID}. All IDs 498 * returned will be unique. 499 * 500 * @return A ID that is unique in the process 501 * 502 * {@hide} 503 */ 504 public int getNextAutofillId() { 505 if (sLastAutofillId == View.LAST_APP_AUTOFILL_ID - 1) { 506 sLastAutofillId = View.NO_ID; 507 } 508 509 sLastAutofillId++; 510 511 return sLastAutofillId; 512 } 513 514 /** 515 * Add a new {@link ComponentCallbacks} to the base application of the 516 * Context, which will be called at the same times as the ComponentCallbacks 517 * methods of activities and other components are called. Note that you 518 * <em>must</em> be sure to use {@link #unregisterComponentCallbacks} when 519 * appropriate in the future; this will not be removed for you. 520 * 521 * @param callback The interface to call. This can be either a 522 * {@link ComponentCallbacks} or {@link ComponentCallbacks2} interface. 523 */ 524 public void registerComponentCallbacks(ComponentCallbacks callback) { 525 getApplicationContext().registerComponentCallbacks(callback); 526 } 527 528 /** 529 * Remove a {@link ComponentCallbacks} object that was previously registered 530 * with {@link #registerComponentCallbacks(ComponentCallbacks)}. 531 */ 532 public void unregisterComponentCallbacks(ComponentCallbacks callback) { 533 getApplicationContext().unregisterComponentCallbacks(callback); 534 } 535 536 /** 537 * Return a localized, styled CharSequence from the application's package's 538 * default string table. 539 * 540 * @param resId Resource id for the CharSequence text 541 */ 542 public final CharSequence getText(@StringRes int resId) { 543 return getResources().getText(resId); 544 } 545 546 /** 547 * Returns a localized string from the application's package's 548 * default string table. 549 * 550 * @param resId Resource id for the string 551 * @return The string data associated with the resource, stripped of styled 552 * text information. 553 */ 554 @NonNull 555 public final String getString(@StringRes int resId) { 556 return getResources().getString(resId); 557 } 558 559 /** 560 * Returns a localized formatted string from the application's package's 561 * default string table, substituting the format arguments as defined in 562 * {@link java.util.Formatter} and {@link java.lang.String#format}. 563 * 564 * @param resId Resource id for the format string 565 * @param formatArgs The format arguments that will be used for 566 * substitution. 567 * @return The string data associated with the resource, formatted and 568 * stripped of styled text information. 569 */ 570 @NonNull 571 public final String getString(@StringRes int resId, Object... formatArgs) { 572 return getResources().getString(resId, formatArgs); 573 } 574 575 /** 576 * Returns a color associated with a particular resource ID and styled for 577 * the current theme. 578 * 579 * @param id The desired resource identifier, as generated by the aapt 580 * tool. This integer encodes the package, type, and resource 581 * entry. The value 0 is an invalid identifier. 582 * @return A single color value in the form 0xAARRGGBB. 583 * @throws android.content.res.Resources.NotFoundException if the given ID 584 * does not exist. 585 */ 586 @ColorInt 587 public final int getColor(@ColorRes int id) { 588 return getResources().getColor(id, getTheme()); 589 } 590 591 /** 592 * Returns a drawable object associated with a particular resource ID and 593 * styled for the current theme. 594 * 595 * @param id The desired resource identifier, as generated by the aapt 596 * tool. This integer encodes the package, type, and resource 597 * entry. The value 0 is an invalid identifier. 598 * @return An object that can be used to draw this resource, or 599 * {@code null} if the resource could not be resolved. 600 * @throws android.content.res.Resources.NotFoundException if the given ID 601 * does not exist. 602 */ 603 @Nullable 604 public final Drawable getDrawable(@DrawableRes int id) { 605 return getResources().getDrawable(id, getTheme()); 606 } 607 608 /** 609 * Returns a color state list associated with a particular resource ID and 610 * styled for the current theme. 611 * 612 * @param id The desired resource identifier, as generated by the aapt 613 * tool. This integer encodes the package, type, and resource 614 * entry. The value 0 is an invalid identifier. 615 * @return A color state list, or {@code null} if the resource could not be 616 * resolved. 617 * @throws android.content.res.Resources.NotFoundException if the given ID 618 * does not exist. 619 */ 620 @Nullable 621 public final ColorStateList getColorStateList(@ColorRes int id) { 622 return getResources().getColorStateList(id, getTheme()); 623 } 624 625 /** 626 * Set the base theme for this context. Note that this should be called 627 * before any views are instantiated in the Context (for example before 628 * calling {@link android.app.Activity#setContentView} or 629 * {@link android.view.LayoutInflater#inflate}). 630 * 631 * @param resid The style resource describing the theme. 632 */ 633 public abstract void setTheme(@StyleRes int resid); 634 635 /** @hide Needed for some internal implementation... not public because 636 * you can't assume this actually means anything. */ 637 public int getThemeResId() { 638 return 0; 639 } 640 641 /** 642 * Return the Theme object associated with this Context. 643 */ 644 @ViewDebug.ExportedProperty(deepExport = true) 645 public abstract Resources.Theme getTheme(); 646 647 /** 648 * Retrieve styled attribute information in this Context's theme. See 649 * {@link android.content.res.Resources.Theme#obtainStyledAttributes(int[])} 650 * for more information. 651 * 652 * @see android.content.res.Resources.Theme#obtainStyledAttributes(int[]) 653 */ 654 public final TypedArray obtainStyledAttributes(@StyleableRes int[] attrs) { 655 return getTheme().obtainStyledAttributes(attrs); 656 } 657 658 /** 659 * Retrieve styled attribute information in this Context's theme. See 660 * {@link android.content.res.Resources.Theme#obtainStyledAttributes(int, int[])} 661 * for more information. 662 * 663 * @see android.content.res.Resources.Theme#obtainStyledAttributes(int, int[]) 664 */ 665 public final TypedArray obtainStyledAttributes( 666 @StyleRes int resid, @StyleableRes int[] attrs) throws Resources.NotFoundException { 667 return getTheme().obtainStyledAttributes(resid, attrs); 668 } 669 670 /** 671 * Retrieve styled attribute information in this Context's theme. See 672 * {@link android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} 673 * for more information. 674 * 675 * @see android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 676 */ 677 public final TypedArray obtainStyledAttributes( 678 AttributeSet set, @StyleableRes int[] attrs) { 679 return getTheme().obtainStyledAttributes(set, attrs, 0, 0); 680 } 681 682 /** 683 * Retrieve styled attribute information in this Context's theme. See 684 * {@link android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} 685 * for more information. 686 * 687 * @see android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) 688 */ 689 public final TypedArray obtainStyledAttributes( 690 AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr, 691 @StyleRes int defStyleRes) { 692 return getTheme().obtainStyledAttributes( 693 set, attrs, defStyleAttr, defStyleRes); 694 } 695 696 /** 697 * Return a class loader you can use to retrieve classes in this package. 698 */ 699 public abstract ClassLoader getClassLoader(); 700 701 /** Return the name of this application's package. */ 702 public abstract String getPackageName(); 703 704 /** @hide Return the name of the base context this context is derived from. */ 705 public abstract String getBasePackageName(); 706 707 /** @hide Return the package name that should be used for app ops calls from 708 * this context. This is the same as {@link #getBasePackageName()} except in 709 * cases where system components are loaded into other app processes, in which 710 * case this will be the name of the primary package in that process (so that app 711 * ops uid verification will work with the name). */ 712 public abstract String getOpPackageName(); 713 714 /** Return the full application info for this context's package. */ 715 public abstract ApplicationInfo getApplicationInfo(); 716 717 /** 718 * Return the full path to this context's primary Android package. 719 * The Android package is a ZIP file which contains the application's 720 * primary resources. 721 * 722 * <p>Note: this is not generally useful for applications, since they should 723 * not be directly accessing the file system. 724 * 725 * @return String Path to the resources. 726 */ 727 public abstract String getPackageResourcePath(); 728 729 /** 730 * Return the full path to this context's primary Android package. 731 * The Android package is a ZIP file which contains application's 732 * primary code and assets. 733 * 734 * <p>Note: this is not generally useful for applications, since they should 735 * not be directly accessing the file system. 736 * 737 * @return String Path to the code and assets. 738 */ 739 public abstract String getPackageCodePath(); 740 741 /** 742 * @hide 743 * @deprecated use {@link #getSharedPreferencesPath(String)} 744 */ 745 @Deprecated 746 public File getSharedPrefsFile(String name) { 747 return getSharedPreferencesPath(name); 748 } 749 750 /** 751 * Retrieve and hold the contents of the preferences file 'name', returning 752 * a SharedPreferences through which you can retrieve and modify its 753 * values. Only one instance of the SharedPreferences object is returned 754 * to any callers for the same name, meaning they will see each other's 755 * edits as soon as they are made. 756 * 757 * @param name Desired preferences file. If a preferences file by this name 758 * does not exist, it will be created when you retrieve an 759 * editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). 760 * @param mode Operating mode. 761 * 762 * @return The single {@link SharedPreferences} instance that can be used 763 * to retrieve and modify the preference values. 764 * 765 * @see #MODE_PRIVATE 766 */ 767 public abstract SharedPreferences getSharedPreferences(String name, @PreferencesMode int mode); 768 769 /** 770 * Retrieve and hold the contents of the preferences file, returning 771 * a SharedPreferences through which you can retrieve and modify its 772 * values. Only one instance of the SharedPreferences object is returned 773 * to any callers for the same name, meaning they will see each other's 774 * edits as soon as they are made. 775 * 776 * @param file Desired preferences file. If a preferences file by this name 777 * does not exist, it will be created when you retrieve an 778 * editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). 779 * @param mode Operating mode. 780 * 781 * @return The single {@link SharedPreferences} instance that can be used 782 * to retrieve and modify the preference values. 783 * 784 * @see #getSharedPreferencesPath(String) 785 * @see #MODE_PRIVATE 786 * @removed 787 */ 788 public abstract SharedPreferences getSharedPreferences(File file, @PreferencesMode int mode); 789 790 /** 791 * Move an existing shared preferences file from the given source storage 792 * context to this context. This is typically used to migrate data between 793 * storage locations after an upgrade, such as moving to device protected 794 * storage. 795 * 796 * @param sourceContext The source context which contains the existing 797 * shared preferences to move. 798 * @param name The name of the shared preferences file. 799 * @return {@code true} if the move was successful or if the shared 800 * preferences didn't exist in the source context, otherwise 801 * {@code false}. 802 * @see #createDeviceProtectedStorageContext() 803 */ 804 public abstract boolean moveSharedPreferencesFrom(Context sourceContext, String name); 805 806 /** 807 * Delete an existing shared preferences file. 808 * 809 * @param name The name (unique in the application package) of the shared 810 * preferences file. 811 * @return {@code true} if the shared preferences file was successfully 812 * deleted; else {@code false}. 813 * @see #getSharedPreferences(String, int) 814 */ 815 public abstract boolean deleteSharedPreferences(String name); 816 817 /** 818 * Open a private file associated with this Context's application package 819 * for reading. 820 * 821 * @param name The name of the file to open; can not contain path 822 * separators. 823 * 824 * @return The resulting {@link FileInputStream}. 825 * 826 * @see #openFileOutput 827 * @see #fileList 828 * @see #deleteFile 829 * @see java.io.FileInputStream#FileInputStream(String) 830 */ 831 public abstract FileInputStream openFileInput(String name) 832 throws FileNotFoundException; 833 834 /** 835 * Open a private file associated with this Context's application package 836 * for writing. Creates the file if it doesn't already exist. 837 * <p> 838 * No additional permissions are required for the calling app to read or 839 * write the returned file. 840 * 841 * @param name The name of the file to open; can not contain path 842 * separators. 843 * @param mode Operating mode. 844 * @return The resulting {@link FileOutputStream}. 845 * @see #MODE_APPEND 846 * @see #MODE_PRIVATE 847 * @see #openFileInput 848 * @see #fileList 849 * @see #deleteFile 850 * @see java.io.FileOutputStream#FileOutputStream(String) 851 */ 852 public abstract FileOutputStream openFileOutput(String name, @FileMode int mode) 853 throws FileNotFoundException; 854 855 /** 856 * Delete the given private file associated with this Context's 857 * application package. 858 * 859 * @param name The name of the file to delete; can not contain path 860 * separators. 861 * 862 * @return {@code true} if the file was successfully deleted; else 863 * {@code false}. 864 * 865 * @see #openFileInput 866 * @see #openFileOutput 867 * @see #fileList 868 * @see java.io.File#delete() 869 */ 870 public abstract boolean deleteFile(String name); 871 872 /** 873 * Returns the absolute path on the filesystem where a file created with 874 * {@link #openFileOutput} is stored. 875 * <p> 876 * The returned path may change over time if the calling app is moved to an 877 * adopted storage device, so only relative paths should be persisted. 878 * 879 * @param name The name of the file for which you would like to get 880 * its path. 881 * 882 * @return An absolute path to the given file. 883 * 884 * @see #openFileOutput 885 * @see #getFilesDir 886 * @see #getDir 887 */ 888 public abstract File getFileStreamPath(String name); 889 890 /** 891 * Returns the absolute path on the filesystem where a file created with 892 * {@link #getSharedPreferences(String, int)} is stored. 893 * <p> 894 * The returned path may change over time if the calling app is moved to an 895 * adopted storage device, so only relative paths should be persisted. 896 * 897 * @param name The name of the shared preferences for which you would like 898 * to get a path. 899 * @return An absolute path to the given file. 900 * @see #getSharedPreferences(String, int) 901 * @removed 902 */ 903 public abstract File getSharedPreferencesPath(String name); 904 905 /** 906 * Returns the absolute path to the directory on the filesystem where all 907 * private files belonging to this app are stored. Apps should not use this 908 * path directly; they should instead use {@link #getFilesDir()}, 909 * {@link #getCacheDir()}, {@link #getDir(String, int)}, or other storage 910 * APIs on this class. 911 * <p> 912 * The returned path may change over time if the calling app is moved to an 913 * adopted storage device, so only relative paths should be persisted. 914 * <p> 915 * No additional permissions are required for the calling app to read or 916 * write files under the returned path. 917 * 918 * @see ApplicationInfo#dataDir 919 */ 920 public abstract File getDataDir(); 921 922 /** 923 * Returns the absolute path to the directory on the filesystem where files 924 * created with {@link #openFileOutput} are stored. 925 * <p> 926 * The returned path may change over time if the calling app is moved to an 927 * adopted storage device, so only relative paths should be persisted. 928 * <p> 929 * No additional permissions are required for the calling app to read or 930 * write files under the returned path. 931 * 932 * @return The path of the directory holding application files. 933 * @see #openFileOutput 934 * @see #getFileStreamPath 935 * @see #getDir 936 */ 937 public abstract File getFilesDir(); 938 939 /** 940 * Returns the absolute path to the directory on the filesystem similar to 941 * {@link #getFilesDir()}. The difference is that files placed under this 942 * directory will be excluded from automatic backup to remote storage. See 943 * {@link android.app.backup.BackupAgent BackupAgent} for a full discussion 944 * of the automatic backup mechanism in Android. 945 * <p> 946 * The returned path may change over time if the calling app is moved to an 947 * adopted storage device, so only relative paths should be persisted. 948 * <p> 949 * No additional permissions are required for the calling app to read or 950 * write files under the returned path. 951 * 952 * @return The path of the directory holding application files that will not 953 * be automatically backed up to remote storage. 954 * @see #openFileOutput 955 * @see #getFileStreamPath 956 * @see #getDir 957 * @see android.app.backup.BackupAgent 958 */ 959 public abstract File getNoBackupFilesDir(); 960 961 /** 962 * Returns the absolute path to the directory on the primary shared/external 963 * storage device where the application can place persistent files it owns. 964 * These files are internal to the applications, and not typically visible 965 * to the user as media. 966 * <p> 967 * This is like {@link #getFilesDir()} in that these files will be deleted 968 * when the application is uninstalled, however there are some important 969 * differences: 970 * <ul> 971 * <li>Shared storage may not always be available, since removable media can 972 * be ejected by the user. Media state can be checked using 973 * {@link Environment#getExternalStorageState(File)}. 974 * <li>There is no security enforced with these files. For example, any 975 * application holding 976 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 977 * these files. 978 * </ul> 979 * <p> 980 * If a shared storage device is emulated (as determined by 981 * {@link Environment#isExternalStorageEmulated(File)}), it's contents are 982 * backed by a private user data partition, which means there is little 983 * benefit to storing data here instead of the private directories returned 984 * by {@link #getFilesDir()}, etc. 985 * <p> 986 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, no permissions 987 * are required to read or write to the returned path; it's always 988 * accessible to the calling app. This only applies to paths generated for 989 * package name of the calling application. To access paths belonging to 990 * other packages, 991 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} and/or 992 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} are required. 993 * <p> 994 * On devices with multiple users (as described by {@link UserManager}), 995 * each user has their own isolated shared storage. Applications only have 996 * access to the shared storage for the user they're running as. 997 * <p> 998 * The returned path may change over time if different shared storage media 999 * is inserted, so only relative paths should be persisted. 1000 * <p> 1001 * Here is an example of typical code to manipulate a file in an 1002 * application's shared storage: 1003 * </p> 1004 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java 1005 * private_file} 1006 * <p> 1007 * If you supply a non-null <var>type</var> to this function, the returned 1008 * file will be a path to a sub-directory of the given type. Though these 1009 * files are not automatically scanned by the media scanner, you can 1010 * explicitly add them to the media database with 1011 * {@link android.media.MediaScannerConnection#scanFile(Context, String[], String[], android.media.MediaScannerConnection.OnScanCompletedListener) 1012 * MediaScannerConnection.scanFile}. Note that this is not the same as 1013 * {@link android.os.Environment#getExternalStoragePublicDirectory 1014 * Environment.getExternalStoragePublicDirectory()}, which provides 1015 * directories of media shared by all applications. The directories returned 1016 * here are owned by the application, and their contents will be removed 1017 * when the application is uninstalled. Unlike 1018 * {@link android.os.Environment#getExternalStoragePublicDirectory 1019 * Environment.getExternalStoragePublicDirectory()}, the directory returned 1020 * here will be automatically created for you. 1021 * <p> 1022 * Here is an example of typical code to manipulate a picture in an 1023 * application's shared storage and add it to the media database: 1024 * </p> 1025 * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java 1026 * private_picture} 1027 * 1028 * @param type The type of files directory to return. May be {@code null} 1029 * for the root of the files directory or one of the following 1030 * constants for a subdirectory: 1031 * {@link android.os.Environment#DIRECTORY_MUSIC}, 1032 * {@link android.os.Environment#DIRECTORY_PODCASTS}, 1033 * {@link android.os.Environment#DIRECTORY_RINGTONES}, 1034 * {@link android.os.Environment#DIRECTORY_ALARMS}, 1035 * {@link android.os.Environment#DIRECTORY_NOTIFICATIONS}, 1036 * {@link android.os.Environment#DIRECTORY_PICTURES}, or 1037 * {@link android.os.Environment#DIRECTORY_MOVIES}. 1038 * @return the absolute path to application-specific directory. May return 1039 * {@code null} if shared storage is not currently available. 1040 * @see #getFilesDir 1041 * @see #getExternalFilesDirs(String) 1042 * @see Environment#getExternalStorageState(File) 1043 * @see Environment#isExternalStorageEmulated(File) 1044 * @see Environment#isExternalStorageRemovable(File) 1045 */ 1046 @Nullable 1047 public abstract File getExternalFilesDir(@Nullable String type); 1048 1049 /** 1050 * Returns absolute paths to application-specific directories on all 1051 * shared/external storage devices where the application can place 1052 * persistent files it owns. These files are internal to the application, 1053 * and not typically visible to the user as media. 1054 * <p> 1055 * This is like {@link #getFilesDir()} in that these files will be deleted 1056 * when the application is uninstalled, however there are some important 1057 * differences: 1058 * <ul> 1059 * <li>Shared storage may not always be available, since removable media can 1060 * be ejected by the user. Media state can be checked using 1061 * {@link Environment#getExternalStorageState(File)}. 1062 * <li>There is no security enforced with these files. For example, any 1063 * application holding 1064 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 1065 * these files. 1066 * </ul> 1067 * <p> 1068 * If a shared storage device is emulated (as determined by 1069 * {@link Environment#isExternalStorageEmulated(File)}), it's contents are 1070 * backed by a private user data partition, which means there is little 1071 * benefit to storing data here instead of the private directories returned 1072 * by {@link #getFilesDir()}, etc. 1073 * <p> 1074 * Shared storage devices returned here are considered a stable part of the 1075 * device, including physical media slots under a protective cover. The 1076 * returned paths do not include transient devices, such as USB flash drives 1077 * connected to handheld devices. 1078 * <p> 1079 * An application may store data on any or all of the returned devices. For 1080 * example, an app may choose to store large files on the device with the 1081 * most available space, as measured by {@link StatFs}. 1082 * <p> 1083 * No additional permissions are required for the calling app to read or 1084 * write files under the returned path. Write access outside of these paths 1085 * on secondary external storage devices is not available. 1086 * <p> 1087 * The returned path may change over time if different shared storage media 1088 * is inserted, so only relative paths should be persisted. 1089 * 1090 * @param type The type of files directory to return. May be {@code null} 1091 * for the root of the files directory or one of the following 1092 * constants for a subdirectory: 1093 * {@link android.os.Environment#DIRECTORY_MUSIC}, 1094 * {@link android.os.Environment#DIRECTORY_PODCASTS}, 1095 * {@link android.os.Environment#DIRECTORY_RINGTONES}, 1096 * {@link android.os.Environment#DIRECTORY_ALARMS}, 1097 * {@link android.os.Environment#DIRECTORY_NOTIFICATIONS}, 1098 * {@link android.os.Environment#DIRECTORY_PICTURES}, or 1099 * {@link android.os.Environment#DIRECTORY_MOVIES}. 1100 * @return the absolute paths to application-specific directories. Some 1101 * individual paths may be {@code null} if that shared storage is 1102 * not currently available. The first path returned is the same as 1103 * {@link #getExternalFilesDir(String)}. 1104 * @see #getExternalFilesDir(String) 1105 * @see Environment#getExternalStorageState(File) 1106 * @see Environment#isExternalStorageEmulated(File) 1107 * @see Environment#isExternalStorageRemovable(File) 1108 */ 1109 public abstract File[] getExternalFilesDirs(String type); 1110 1111 /** 1112 * Return the primary shared/external storage directory where this 1113 * application's OBB files (if there are any) can be found. Note if the 1114 * application does not have any OBB files, this directory may not exist. 1115 * <p> 1116 * This is like {@link #getFilesDir()} in that these files will be deleted 1117 * when the application is uninstalled, however there are some important 1118 * differences: 1119 * <ul> 1120 * <li>Shared storage may not always be available, since removable media can 1121 * be ejected by the user. Media state can be checked using 1122 * {@link Environment#getExternalStorageState(File)}. 1123 * <li>There is no security enforced with these files. For example, any 1124 * application holding 1125 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 1126 * these files. 1127 * </ul> 1128 * <p> 1129 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, no permissions 1130 * are required to read or write to the returned path; it's always 1131 * accessible to the calling app. This only applies to paths generated for 1132 * package name of the calling application. To access paths belonging to 1133 * other packages, 1134 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} and/or 1135 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} are required. 1136 * <p> 1137 * On devices with multiple users (as described by {@link UserManager}), 1138 * multiple users may share the same OBB storage location. Applications 1139 * should ensure that multiple instances running under different users don't 1140 * interfere with each other. 1141 * 1142 * @return the absolute path to application-specific directory. May return 1143 * {@code null} if shared storage is not currently available. 1144 * @see #getObbDirs() 1145 * @see Environment#getExternalStorageState(File) 1146 * @see Environment#isExternalStorageEmulated(File) 1147 * @see Environment#isExternalStorageRemovable(File) 1148 */ 1149 public abstract File getObbDir(); 1150 1151 /** 1152 * Returns absolute paths to application-specific directories on all 1153 * shared/external storage devices where the application's OBB files (if 1154 * there are any) can be found. Note if the application does not have any 1155 * OBB files, these directories may not exist. 1156 * <p> 1157 * This is like {@link #getFilesDir()} in that these files will be deleted 1158 * when the application is uninstalled, however there are some important 1159 * differences: 1160 * <ul> 1161 * <li>Shared storage may not always be available, since removable media can 1162 * be ejected by the user. Media state can be checked using 1163 * {@link Environment#getExternalStorageState(File)}. 1164 * <li>There is no security enforced with these files. For example, any 1165 * application holding 1166 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 1167 * these files. 1168 * </ul> 1169 * <p> 1170 * Shared storage devices returned here are considered a stable part of the 1171 * device, including physical media slots under a protective cover. The 1172 * returned paths do not include transient devices, such as USB flash drives 1173 * connected to handheld devices. 1174 * <p> 1175 * An application may store data on any or all of the returned devices. For 1176 * example, an app may choose to store large files on the device with the 1177 * most available space, as measured by {@link StatFs}. 1178 * <p> 1179 * No additional permissions are required for the calling app to read or 1180 * write files under the returned path. Write access outside of these paths 1181 * on secondary external storage devices is not available. 1182 * 1183 * @return the absolute paths to application-specific directories. Some 1184 * individual paths may be {@code null} if that shared storage is 1185 * not currently available. The first path returned is the same as 1186 * {@link #getObbDir()} 1187 * @see #getObbDir() 1188 * @see Environment#getExternalStorageState(File) 1189 * @see Environment#isExternalStorageEmulated(File) 1190 * @see Environment#isExternalStorageRemovable(File) 1191 */ 1192 public abstract File[] getObbDirs(); 1193 1194 /** 1195 * Returns the absolute path to the application specific cache directory on 1196 * the filesystem. 1197 * <p> 1198 * The system will automatically delete files in this directory as disk 1199 * space is needed elsewhere on the device. The system will always delete 1200 * older files first, as reported by {@link File#lastModified()}. If 1201 * desired, you can exert more control over how files are deleted using 1202 * {@link StorageManager#setCacheBehaviorGroup(File, boolean)} and 1203 * {@link StorageManager#setCacheBehaviorTombstone(File, boolean)}. 1204 * <p> 1205 * Apps are strongly encouraged to keep their usage of cache space below the 1206 * quota returned by 1207 * {@link StorageManager#getCacheQuotaBytes(java.util.UUID)}. If your app 1208 * goes above this quota, your cached files will be some of the first to be 1209 * deleted when additional disk space is needed. Conversely, if your app 1210 * stays under this quota, your cached files will be some of the last to be 1211 * deleted when additional disk space is needed. 1212 * <p> 1213 * Note that your cache quota will change over time depending on how 1214 * frequently the user interacts with your app, and depending on how much 1215 * system-wide disk space is used. 1216 * <p> 1217 * The returned path may change over time if the calling app is moved to an 1218 * adopted storage device, so only relative paths should be persisted. 1219 * <p> 1220 * Apps require no extra permissions to read or write to the returned path, 1221 * since this path lives in their private storage. 1222 * 1223 * @return The path of the directory holding application cache files. 1224 * @see #openFileOutput 1225 * @see #getFileStreamPath 1226 * @see #getDir 1227 * @see #getExternalCacheDir 1228 */ 1229 public abstract File getCacheDir(); 1230 1231 /** 1232 * Returns the absolute path to the application specific cache directory on 1233 * the filesystem designed for storing cached code. 1234 * <p> 1235 * The system will delete any files stored in this location both when your 1236 * specific application is upgraded, and when the entire platform is 1237 * upgraded. 1238 * <p> 1239 * This location is optimal for storing compiled or optimized code generated 1240 * by your application at runtime. 1241 * <p> 1242 * The returned path may change over time if the calling app is moved to an 1243 * adopted storage device, so only relative paths should be persisted. 1244 * <p> 1245 * Apps require no extra permissions to read or write to the returned path, 1246 * since this path lives in their private storage. 1247 * 1248 * @return The path of the directory holding application code cache files. 1249 */ 1250 public abstract File getCodeCacheDir(); 1251 1252 /** 1253 * Returns absolute path to application-specific directory on the primary 1254 * shared/external storage device where the application can place cache 1255 * files it owns. These files are internal to the application, and not 1256 * typically visible to the user as media. 1257 * <p> 1258 * This is like {@link #getCacheDir()} in that these files will be deleted 1259 * when the application is uninstalled, however there are some important 1260 * differences: 1261 * <ul> 1262 * <li>The platform does not always monitor the space available in shared 1263 * storage, and thus may not automatically delete these files. Apps should 1264 * always manage the maximum space used in this location. Currently the only 1265 * time files here will be deleted by the platform is when running on 1266 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} or later and 1267 * {@link Environment#isExternalStorageEmulated(File)} returns true. 1268 * <li>Shared storage may not always be available, since removable media can 1269 * be ejected by the user. Media state can be checked using 1270 * {@link Environment#getExternalStorageState(File)}. 1271 * <li>There is no security enforced with these files. For example, any 1272 * application holding 1273 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 1274 * these files. 1275 * </ul> 1276 * <p> 1277 * If a shared storage device is emulated (as determined by 1278 * {@link Environment#isExternalStorageEmulated(File)}), its contents are 1279 * backed by a private user data partition, which means there is little 1280 * benefit to storing data here instead of the private directory returned by 1281 * {@link #getCacheDir()}. 1282 * <p> 1283 * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, no permissions 1284 * are required to read or write to the returned path; it's always 1285 * accessible to the calling app. This only applies to paths generated for 1286 * package name of the calling application. To access paths belonging to 1287 * other packages, 1288 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} and/or 1289 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} are required. 1290 * <p> 1291 * On devices with multiple users (as described by {@link UserManager}), 1292 * each user has their own isolated shared storage. Applications only have 1293 * access to the shared storage for the user they're running as. 1294 * <p> 1295 * The returned path may change over time if different shared storage media 1296 * is inserted, so only relative paths should be persisted. 1297 * 1298 * @return the absolute path to application-specific directory. May return 1299 * {@code null} if shared storage is not currently available. 1300 * @see #getCacheDir 1301 * @see #getExternalCacheDirs() 1302 * @see Environment#getExternalStorageState(File) 1303 * @see Environment#isExternalStorageEmulated(File) 1304 * @see Environment#isExternalStorageRemovable(File) 1305 */ 1306 @Nullable 1307 public abstract File getExternalCacheDir(); 1308 1309 /** 1310 * Returns absolute path to application-specific directory in the preloaded cache. 1311 * <p>Files stored in the cache directory can be deleted when the device runs low on storage. 1312 * There is no guarantee when these files will be deleted. 1313 * @hide 1314 */ 1315 @Nullable 1316 @SystemApi 1317 public abstract File getPreloadsFileCache(); 1318 1319 /** 1320 * Returns absolute paths to application-specific directories on all 1321 * shared/external storage devices where the application can place cache 1322 * files it owns. These files are internal to the application, and not 1323 * typically visible to the user as media. 1324 * <p> 1325 * This is like {@link #getCacheDir()} in that these files will be deleted 1326 * when the application is uninstalled, however there are some important 1327 * differences: 1328 * <ul> 1329 * <li>The platform does not always monitor the space available in shared 1330 * storage, and thus may not automatically delete these files. Apps should 1331 * always manage the maximum space used in this location. Currently the only 1332 * time files here will be deleted by the platform is when running on 1333 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} or later and 1334 * {@link Environment#isExternalStorageEmulated(File)} returns true. 1335 * <li>Shared storage may not always be available, since removable media can 1336 * be ejected by the user. Media state can be checked using 1337 * {@link Environment#getExternalStorageState(File)}. 1338 * <li>There is no security enforced with these files. For example, any 1339 * application holding 1340 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 1341 * these files. 1342 * </ul> 1343 * <p> 1344 * If a shared storage device is emulated (as determined by 1345 * {@link Environment#isExternalStorageEmulated(File)}), it's contents are 1346 * backed by a private user data partition, which means there is little 1347 * benefit to storing data here instead of the private directory returned by 1348 * {@link #getCacheDir()}. 1349 * <p> 1350 * Shared storage devices returned here are considered a stable part of the 1351 * device, including physical media slots under a protective cover. The 1352 * returned paths do not include transient devices, such as USB flash drives 1353 * connected to handheld devices. 1354 * <p> 1355 * An application may store data on any or all of the returned devices. For 1356 * example, an app may choose to store large files on the device with the 1357 * most available space, as measured by {@link StatFs}. 1358 * <p> 1359 * No additional permissions are required for the calling app to read or 1360 * write files under the returned path. Write access outside of these paths 1361 * on secondary external storage devices is not available. 1362 * <p> 1363 * The returned paths may change over time if different shared storage media 1364 * is inserted, so only relative paths should be persisted. 1365 * 1366 * @return the absolute paths to application-specific directories. Some 1367 * individual paths may be {@code null} if that shared storage is 1368 * not currently available. The first path returned is the same as 1369 * {@link #getExternalCacheDir()}. 1370 * @see #getExternalCacheDir() 1371 * @see Environment#getExternalStorageState(File) 1372 * @see Environment#isExternalStorageEmulated(File) 1373 * @see Environment#isExternalStorageRemovable(File) 1374 */ 1375 public abstract File[] getExternalCacheDirs(); 1376 1377 /** 1378 * Returns absolute paths to application-specific directories on all 1379 * shared/external storage devices where the application can place media 1380 * files. These files are scanned and made available to other apps through 1381 * {@link MediaStore}. 1382 * <p> 1383 * This is like {@link #getExternalFilesDirs} in that these files will be 1384 * deleted when the application is uninstalled, however there are some 1385 * important differences: 1386 * <ul> 1387 * <li>Shared storage may not always be available, since removable media can 1388 * be ejected by the user. Media state can be checked using 1389 * {@link Environment#getExternalStorageState(File)}. 1390 * <li>There is no security enforced with these files. For example, any 1391 * application holding 1392 * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} can write to 1393 * these files. 1394 * </ul> 1395 * <p> 1396 * Shared storage devices returned here are considered a stable part of the 1397 * device, including physical media slots under a protective cover. The 1398 * returned paths do not include transient devices, such as USB flash drives 1399 * connected to handheld devices. 1400 * <p> 1401 * An application may store data on any or all of the returned devices. For 1402 * example, an app may choose to store large files on the device with the 1403 * most available space, as measured by {@link StatFs}. 1404 * <p> 1405 * No additional permissions are required for the calling app to read or 1406 * write files under the returned path. Write access outside of these paths 1407 * on secondary external storage devices is not available. 1408 * <p> 1409 * The returned paths may change over time if different shared storage media 1410 * is inserted, so only relative paths should be persisted. 1411 * 1412 * @return the absolute paths to application-specific directories. Some 1413 * individual paths may be {@code null} if that shared storage is 1414 * not currently available. 1415 * @see Environment#getExternalStorageState(File) 1416 * @see Environment#isExternalStorageEmulated(File) 1417 * @see Environment#isExternalStorageRemovable(File) 1418 */ 1419 public abstract File[] getExternalMediaDirs(); 1420 1421 /** 1422 * Returns an array of strings naming the private files associated with 1423 * this Context's application package. 1424 * 1425 * @return Array of strings naming the private files. 1426 * 1427 * @see #openFileInput 1428 * @see #openFileOutput 1429 * @see #deleteFile 1430 */ 1431 public abstract String[] fileList(); 1432 1433 /** 1434 * Retrieve, creating if needed, a new directory in which the application 1435 * can place its own custom data files. You can use the returned File 1436 * object to create and access files in this directory. Note that files 1437 * created through a File object will only be accessible by your own 1438 * application; you can only set the mode of the entire directory, not 1439 * of individual files. 1440 * <p> 1441 * The returned path may change over time if the calling app is moved to an 1442 * adopted storage device, so only relative paths should be persisted. 1443 * <p> 1444 * Apps require no extra permissions to read or write to the returned path, 1445 * since this path lives in their private storage. 1446 * 1447 * @param name Name of the directory to retrieve. This is a directory 1448 * that is created as part of your application data. 1449 * @param mode Operating mode. 1450 * 1451 * @return A {@link File} object for the requested directory. The directory 1452 * will have been created if it does not already exist. 1453 * 1454 * @see #openFileOutput(String, int) 1455 */ 1456 public abstract File getDir(String name, @FileMode int mode); 1457 1458 /** 1459 * Open a new private SQLiteDatabase associated with this Context's 1460 * application package. Create the database file if it doesn't exist. 1461 * 1462 * @param name The name (unique in the application package) of the database. 1463 * @param mode Operating mode. 1464 * @param factory An optional factory class that is called to instantiate a 1465 * cursor when query is called. 1466 * @return The contents of a newly created database with the given name. 1467 * @throws android.database.sqlite.SQLiteException if the database file 1468 * could not be opened. 1469 * @see #MODE_PRIVATE 1470 * @see #MODE_ENABLE_WRITE_AHEAD_LOGGING 1471 * @see #MODE_NO_LOCALIZED_COLLATORS 1472 * @see #deleteDatabase 1473 */ 1474 public abstract SQLiteDatabase openOrCreateDatabase(String name, 1475 @DatabaseMode int mode, CursorFactory factory); 1476 1477 /** 1478 * Open a new private SQLiteDatabase associated with this Context's 1479 * application package. Creates the database file if it doesn't exist. 1480 * <p> 1481 * Accepts input param: a concrete instance of {@link DatabaseErrorHandler} 1482 * to be used to handle corruption when sqlite reports database corruption. 1483 * </p> 1484 * 1485 * @param name The name (unique in the application package) of the database. 1486 * @param mode Operating mode. 1487 * @param factory An optional factory class that is called to instantiate a 1488 * cursor when query is called. 1489 * @param errorHandler the {@link DatabaseErrorHandler} to be used when 1490 * sqlite reports database corruption. if null, 1491 * {@link android.database.DefaultDatabaseErrorHandler} is 1492 * assumed. 1493 * @return The contents of a newly created database with the given name. 1494 * @throws android.database.sqlite.SQLiteException if the database file 1495 * could not be opened. 1496 * @see #MODE_PRIVATE 1497 * @see #MODE_ENABLE_WRITE_AHEAD_LOGGING 1498 * @see #MODE_NO_LOCALIZED_COLLATORS 1499 * @see #deleteDatabase 1500 */ 1501 public abstract SQLiteDatabase openOrCreateDatabase(String name, 1502 @DatabaseMode int mode, CursorFactory factory, 1503 @Nullable DatabaseErrorHandler errorHandler); 1504 1505 /** 1506 * Move an existing database file from the given source storage context to 1507 * this context. This is typically used to migrate data between storage 1508 * locations after an upgrade, such as migrating to device protected 1509 * storage. 1510 * <p> 1511 * The database must be closed before being moved. 1512 * 1513 * @param sourceContext The source context which contains the existing 1514 * database to move. 1515 * @param name The name of the database file. 1516 * @return {@code true} if the move was successful or if the database didn't 1517 * exist in the source context, otherwise {@code false}. 1518 * @see #createDeviceProtectedStorageContext() 1519 */ 1520 public abstract boolean moveDatabaseFrom(Context sourceContext, String name); 1521 1522 /** 1523 * Delete an existing private SQLiteDatabase associated with this Context's 1524 * application package. 1525 * 1526 * @param name The name (unique in the application package) of the 1527 * database. 1528 * 1529 * @return {@code true} if the database was successfully deleted; else {@code false}. 1530 * 1531 * @see #openOrCreateDatabase 1532 */ 1533 public abstract boolean deleteDatabase(String name); 1534 1535 /** 1536 * Returns the absolute path on the filesystem where a database created with 1537 * {@link #openOrCreateDatabase} is stored. 1538 * <p> 1539 * The returned path may change over time if the calling app is moved to an 1540 * adopted storage device, so only relative paths should be persisted. 1541 * 1542 * @param name The name of the database for which you would like to get 1543 * its path. 1544 * 1545 * @return An absolute path to the given database. 1546 * 1547 * @see #openOrCreateDatabase 1548 */ 1549 public abstract File getDatabasePath(String name); 1550 1551 /** 1552 * Returns an array of strings naming the private databases associated with 1553 * this Context's application package. 1554 * 1555 * @return Array of strings naming the private databases. 1556 * 1557 * @see #openOrCreateDatabase 1558 * @see #deleteDatabase 1559 */ 1560 public abstract String[] databaseList(); 1561 1562 /** 1563 * @deprecated Use {@link android.app.WallpaperManager#getDrawable 1564 * WallpaperManager.get()} instead. 1565 */ 1566 @Deprecated 1567 public abstract Drawable getWallpaper(); 1568 1569 /** 1570 * @deprecated Use {@link android.app.WallpaperManager#peekDrawable 1571 * WallpaperManager.peek()} instead. 1572 */ 1573 @Deprecated 1574 public abstract Drawable peekWallpaper(); 1575 1576 /** 1577 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumWidth() 1578 * WallpaperManager.getDesiredMinimumWidth()} instead. 1579 */ 1580 @Deprecated 1581 public abstract int getWallpaperDesiredMinimumWidth(); 1582 1583 /** 1584 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumHeight() 1585 * WallpaperManager.getDesiredMinimumHeight()} instead. 1586 */ 1587 @Deprecated 1588 public abstract int getWallpaperDesiredMinimumHeight(); 1589 1590 /** 1591 * @deprecated Use {@link android.app.WallpaperManager#setBitmap(Bitmap) 1592 * WallpaperManager.set()} instead. 1593 * <p>This method requires the caller to hold the permission 1594 * {@link android.Manifest.permission#SET_WALLPAPER}. 1595 */ 1596 @Deprecated 1597 public abstract void setWallpaper(Bitmap bitmap) throws IOException; 1598 1599 /** 1600 * @deprecated Use {@link android.app.WallpaperManager#setStream(InputStream) 1601 * WallpaperManager.set()} instead. 1602 * <p>This method requires the caller to hold the permission 1603 * {@link android.Manifest.permission#SET_WALLPAPER}. 1604 */ 1605 @Deprecated 1606 public abstract void setWallpaper(InputStream data) throws IOException; 1607 1608 /** 1609 * @deprecated Use {@link android.app.WallpaperManager#clear 1610 * WallpaperManager.clear()} instead. 1611 * <p>This method requires the caller to hold the permission 1612 * {@link android.Manifest.permission#SET_WALLPAPER}. 1613 */ 1614 @Deprecated 1615 public abstract void clearWallpaper() throws IOException; 1616 1617 /** 1618 * Same as {@link #startActivity(Intent, Bundle)} with no options 1619 * specified. 1620 * 1621 * @param intent The description of the activity to start. 1622 * 1623 * @throws ActivityNotFoundException 1624 *` 1625 * @see #startActivity(Intent, Bundle) 1626 * @see PackageManager#resolveActivity 1627 */ 1628 public abstract void startActivity(@RequiresPermission Intent intent); 1629 1630 /** 1631 * Version of {@link #startActivity(Intent)} that allows you to specify the 1632 * user the activity will be started for. This is not available to applications 1633 * that are not pre-installed on the system image. 1634 * @param intent The description of the activity to start. 1635 * @param user The UserHandle of the user to start this activity for. 1636 * @throws ActivityNotFoundException 1637 * @hide 1638 */ 1639 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) 1640 public void startActivityAsUser(@RequiresPermission Intent intent, UserHandle user) { 1641 throw new RuntimeException("Not implemented. Must override in a subclass."); 1642 } 1643 1644 /** 1645 * Launch a new activity. You will not receive any information about when 1646 * the activity exits. 1647 * 1648 * <p>Note that if this method is being called from outside of an 1649 * {@link android.app.Activity} Context, then the Intent must include 1650 * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because, 1651 * without being started from an existing Activity, there is no existing 1652 * task in which to place the new activity and thus it needs to be placed 1653 * in its own separate task. 1654 * 1655 * <p>This method throws {@link ActivityNotFoundException} 1656 * if there was no Activity found to run the given Intent. 1657 * 1658 * @param intent The description of the activity to start. 1659 * @param options Additional options for how the Activity should be started. 1660 * May be null if there are no options. See {@link android.app.ActivityOptions} 1661 * for how to build the Bundle supplied here; there are no supported definitions 1662 * for building it manually. 1663 * 1664 * @throws ActivityNotFoundException 1665 * 1666 * @see #startActivity(Intent) 1667 * @see PackageManager#resolveActivity 1668 */ 1669 public abstract void startActivity(@RequiresPermission Intent intent, 1670 @Nullable Bundle options); 1671 1672 /** 1673 * Version of {@link #startActivity(Intent, Bundle)} that allows you to specify the 1674 * user the activity will be started for. This is not available to applications 1675 * that are not pre-installed on the system image. 1676 * @param intent The description of the activity to start. 1677 * @param options Additional options for how the Activity should be started. 1678 * May be null if there are no options. See {@link android.app.ActivityOptions} 1679 * for how to build the Bundle supplied here; there are no supported definitions 1680 * for building it manually. 1681 * @param userId The UserHandle of the user to start this activity for. 1682 * @throws ActivityNotFoundException 1683 * @hide 1684 */ 1685 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) 1686 public void startActivityAsUser(@RequiresPermission Intent intent, @Nullable Bundle options, 1687 UserHandle userId) { 1688 throw new RuntimeException("Not implemented. Must override in a subclass."); 1689 } 1690 1691 /** 1692 * Version of {@link #startActivity(Intent, Bundle)} that returns a result to the caller. This 1693 * is only supported for Views and Fragments. 1694 * @param who The identifier for the calling element that will receive the result. 1695 * @param intent The intent to start. 1696 * @param requestCode The code that will be returned with onActivityResult() identifying this 1697 * request. 1698 * @param options Additional options for how the Activity should be started. 1699 * May be null if there are no options. See {@link android.app.ActivityOptions} 1700 * for how to build the Bundle supplied here; there are no supported definitions 1701 * for building it manually. 1702 * @hide 1703 */ 1704 public void startActivityForResult( 1705 @NonNull String who, Intent intent, int requestCode, @Nullable Bundle options) { 1706 throw new RuntimeException("This method is only implemented for Activity-based Contexts. " 1707 + "Check canStartActivityForResult() before calling."); 1708 } 1709 1710 /** 1711 * Identifies whether this Context instance will be able to process calls to 1712 * {@link #startActivityForResult(String, Intent, int, Bundle)}. 1713 * @hide 1714 */ 1715 public boolean canStartActivityForResult() { 1716 return false; 1717 } 1718 1719 /** 1720 * Same as {@link #startActivities(Intent[], Bundle)} with no options 1721 * specified. 1722 * 1723 * @param intents An array of Intents to be started. 1724 * 1725 * @throws ActivityNotFoundException 1726 * 1727 * @see #startActivities(Intent[], Bundle) 1728 * @see PackageManager#resolveActivity 1729 */ 1730 public abstract void startActivities(@RequiresPermission Intent[] intents); 1731 1732 /** 1733 * Launch multiple new activities. This is generally the same as calling 1734 * {@link #startActivity(Intent)} for the first Intent in the array, 1735 * that activity during its creation calling {@link #startActivity(Intent)} 1736 * for the second entry, etc. Note that unlike that approach, generally 1737 * none of the activities except the last in the array will be created 1738 * at this point, but rather will be created when the user first visits 1739 * them (due to pressing back from the activity on top). 1740 * 1741 * <p>This method throws {@link ActivityNotFoundException} 1742 * if there was no Activity found for <em>any</em> given Intent. In this 1743 * case the state of the activity stack is undefined (some Intents in the 1744 * list may be on it, some not), so you probably want to avoid such situations. 1745 * 1746 * @param intents An array of Intents to be started. 1747 * @param options Additional options for how the Activity should be started. 1748 * See {@link android.content.Context#startActivity(Intent, Bundle)} 1749 * Context.startActivity(Intent, Bundle)} for more details. 1750 * 1751 * @throws ActivityNotFoundException 1752 * 1753 * @see #startActivities(Intent[]) 1754 * @see PackageManager#resolveActivity 1755 */ 1756 public abstract void startActivities(@RequiresPermission Intent[] intents, Bundle options); 1757 1758 /** 1759 * @hide 1760 * Launch multiple new activities. This is generally the same as calling 1761 * {@link #startActivity(Intent)} for the first Intent in the array, 1762 * that activity during its creation calling {@link #startActivity(Intent)} 1763 * for the second entry, etc. Note that unlike that approach, generally 1764 * none of the activities except the last in the array will be created 1765 * at this point, but rather will be created when the user first visits 1766 * them (due to pressing back from the activity on top). 1767 * 1768 * <p>This method throws {@link ActivityNotFoundException} 1769 * if there was no Activity found for <em>any</em> given Intent. In this 1770 * case the state of the activity stack is undefined (some Intents in the 1771 * list may be on it, some not), so you probably want to avoid such situations. 1772 * 1773 * @param intents An array of Intents to be started. 1774 * @param options Additional options for how the Activity should be started. 1775 * @param userHandle The user for whom to launch the activities 1776 * See {@link android.content.Context#startActivity(Intent, Bundle)} 1777 * Context.startActivity(Intent, Bundle)} for more details. 1778 * 1779 * @throws ActivityNotFoundException 1780 * 1781 * @see #startActivities(Intent[]) 1782 * @see PackageManager#resolveActivity 1783 */ 1784 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) 1785 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) { 1786 throw new RuntimeException("Not implemented. Must override in a subclass."); 1787 } 1788 1789 /** 1790 * Same as {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)} 1791 * with no options specified. 1792 * 1793 * @param intent The IntentSender to launch. 1794 * @param fillInIntent If non-null, this will be provided as the 1795 * intent parameter to {@link IntentSender#sendIntent}. 1796 * @param flagsMask Intent flags in the original IntentSender that you 1797 * would like to change. 1798 * @param flagsValues Desired values for any bits set in 1799 * <var>flagsMask</var> 1800 * @param extraFlags Always set to 0. 1801 * 1802 * @see #startActivity(Intent) 1803 * @see #startIntentSender(IntentSender, Intent, int, int, int, Bundle) 1804 */ 1805 public abstract void startIntentSender(IntentSender intent, @Nullable Intent fillInIntent, 1806 @Intent.MutableFlags int flagsMask, @Intent.MutableFlags int flagsValues, 1807 int extraFlags) throws IntentSender.SendIntentException; 1808 1809 /** 1810 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender 1811 * to start. If the IntentSender is for an activity, that activity will be started 1812 * as if you had called the regular {@link #startActivity(Intent)} 1813 * here; otherwise, its associated action will be executed (such as 1814 * sending a broadcast) as if you had called 1815 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it. 1816 * 1817 * @param intent The IntentSender to launch. 1818 * @param fillInIntent If non-null, this will be provided as the 1819 * intent parameter to {@link IntentSender#sendIntent}. 1820 * @param flagsMask Intent flags in the original IntentSender that you 1821 * would like to change. 1822 * @param flagsValues Desired values for any bits set in 1823 * <var>flagsMask</var> 1824 * @param extraFlags Always set to 0. 1825 * @param options Additional options for how the Activity should be started. 1826 * See {@link android.content.Context#startActivity(Intent, Bundle)} 1827 * Context.startActivity(Intent, Bundle)} for more details. If options 1828 * have also been supplied by the IntentSender, options given here will 1829 * override any that conflict with those given by the IntentSender. 1830 * 1831 * @see #startActivity(Intent, Bundle) 1832 * @see #startIntentSender(IntentSender, Intent, int, int, int) 1833 */ 1834 public abstract void startIntentSender(IntentSender intent, @Nullable Intent fillInIntent, 1835 @Intent.MutableFlags int flagsMask, @Intent.MutableFlags int flagsValues, 1836 int extraFlags, @Nullable Bundle options) throws IntentSender.SendIntentException; 1837 1838 /** 1839 * Broadcast the given intent to all interested BroadcastReceivers. This 1840 * call is asynchronous; it returns immediately, and you will continue 1841 * executing while the receivers are run. No results are propagated from 1842 * receivers and receivers can not abort the broadcast. If you want 1843 * to allow receivers to propagate results or abort the broadcast, you must 1844 * send an ordered broadcast using 1845 * {@link #sendOrderedBroadcast(Intent, String)}. 1846 * 1847 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1848 * 1849 * @param intent The Intent to broadcast; all receivers matching this 1850 * Intent will receive the broadcast. 1851 * 1852 * @see android.content.BroadcastReceiver 1853 * @see #registerReceiver 1854 * @see #sendBroadcast(Intent, String) 1855 * @see #sendOrderedBroadcast(Intent, String) 1856 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1857 */ 1858 public abstract void sendBroadcast(@RequiresPermission Intent intent); 1859 1860 /** 1861 * Broadcast the given intent to all interested BroadcastReceivers, allowing 1862 * an optional required permission to be enforced. This 1863 * call is asynchronous; it returns immediately, and you will continue 1864 * executing while the receivers are run. No results are propagated from 1865 * receivers and receivers can not abort the broadcast. If you want 1866 * to allow receivers to propagate results or abort the broadcast, you must 1867 * send an ordered broadcast using 1868 * {@link #sendOrderedBroadcast(Intent, String)}. 1869 * 1870 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1871 * 1872 * @param intent The Intent to broadcast; all receivers matching this 1873 * Intent will receive the broadcast. 1874 * @param receiverPermission (optional) String naming a permission that 1875 * a receiver must hold in order to receive your broadcast. 1876 * If null, no permission is required. 1877 * 1878 * @see android.content.BroadcastReceiver 1879 * @see #registerReceiver 1880 * @see #sendBroadcast(Intent) 1881 * @see #sendOrderedBroadcast(Intent, String) 1882 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1883 */ 1884 public abstract void sendBroadcast(@RequiresPermission Intent intent, 1885 @Nullable String receiverPermission); 1886 1887 1888 /** 1889 * Broadcast the given intent to all interested BroadcastReceivers, allowing 1890 * an array of required permissions to be enforced. This call is asynchronous; it returns 1891 * immediately, and you will continue executing while the receivers are run. No results are 1892 * propagated from receivers and receivers can not abort the broadcast. If you want to allow 1893 * receivers to propagate results or abort the broadcast, you must send an ordered broadcast 1894 * using {@link #sendOrderedBroadcast(Intent, String)}. 1895 * 1896 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1897 * 1898 * @param intent The Intent to broadcast; all receivers matching this 1899 * Intent will receive the broadcast. 1900 * @param receiverPermissions Array of names of permissions that a receiver must hold 1901 * in order to receive your broadcast. 1902 * If null or empty, no permissions are required. 1903 * 1904 * @see android.content.BroadcastReceiver 1905 * @see #registerReceiver 1906 * @see #sendBroadcast(Intent) 1907 * @see #sendOrderedBroadcast(Intent, String) 1908 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1909 * @hide 1910 */ 1911 public abstract void sendBroadcastMultiplePermissions(Intent intent, 1912 String[] receiverPermissions); 1913 1914 /** 1915 * Broadcast the given intent to all interested BroadcastReceivers, allowing 1916 * an optional required permission to be enforced. This 1917 * call is asynchronous; it returns immediately, and you will continue 1918 * executing while the receivers are run. No results are propagated from 1919 * receivers and receivers can not abort the broadcast. If you want 1920 * to allow receivers to propagate results or abort the broadcast, you must 1921 * send an ordered broadcast using 1922 * {@link #sendOrderedBroadcast(Intent, String)}. 1923 * 1924 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1925 * 1926 * @param intent The Intent to broadcast; all receivers matching this 1927 * Intent will receive the broadcast. 1928 * @param receiverPermission (optional) String naming a permission that 1929 * a receiver must hold in order to receive your broadcast. 1930 * If null, no permission is required. 1931 * @param options (optional) Additional sending options, generated from a 1932 * {@link android.app.BroadcastOptions}. 1933 * 1934 * @see android.content.BroadcastReceiver 1935 * @see #registerReceiver 1936 * @see #sendBroadcast(Intent) 1937 * @see #sendOrderedBroadcast(Intent, String) 1938 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1939 * @hide 1940 */ 1941 @SystemApi 1942 public abstract void sendBroadcast(Intent intent, 1943 @Nullable String receiverPermission, 1944 @Nullable Bundle options); 1945 1946 /** 1947 * Like {@link #sendBroadcast(Intent, String)}, but also allows specification 1948 * of an associated app op as per {@link android.app.AppOpsManager}. 1949 * @hide 1950 */ 1951 public abstract void sendBroadcast(Intent intent, 1952 String receiverPermission, int appOp); 1953 1954 /** 1955 * Broadcast the given intent to all interested BroadcastReceivers, delivering 1956 * them one at a time to allow more preferred receivers to consume the 1957 * broadcast before it is delivered to less preferred receivers. This 1958 * call is asynchronous; it returns immediately, and you will continue 1959 * executing while the receivers are run. 1960 * 1961 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1962 * 1963 * @param intent The Intent to broadcast; all receivers matching this 1964 * Intent will receive the broadcast. 1965 * @param receiverPermission (optional) String naming a permissions that 1966 * a receiver must hold in order to receive your broadcast. 1967 * If null, no permission is required. 1968 * 1969 * @see android.content.BroadcastReceiver 1970 * @see #registerReceiver 1971 * @see #sendBroadcast(Intent) 1972 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 1973 */ 1974 public abstract void sendOrderedBroadcast(@RequiresPermission Intent intent, 1975 @Nullable String receiverPermission); 1976 1977 /** 1978 * Version of {@link #sendBroadcast(Intent)} that allows you to 1979 * receive data back from the broadcast. This is accomplished by 1980 * supplying your own BroadcastReceiver when calling, which will be 1981 * treated as a final receiver at the end of the broadcast -- its 1982 * {@link BroadcastReceiver#onReceive} method will be called with 1983 * the result values collected from the other receivers. The broadcast will 1984 * be serialized in the same way as calling 1985 * {@link #sendOrderedBroadcast(Intent, String)}. 1986 * 1987 * <p>Like {@link #sendBroadcast(Intent)}, this method is 1988 * asynchronous; it will return before 1989 * resultReceiver.onReceive() is called. 1990 * 1991 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 1992 * 1993 * @param intent The Intent to broadcast; all receivers matching this 1994 * Intent will receive the broadcast. 1995 * @param receiverPermission String naming a permissions that 1996 * a receiver must hold in order to receive your broadcast. 1997 * If null, no permission is required. 1998 * @param resultReceiver Your own BroadcastReceiver to treat as the final 1999 * receiver of the broadcast. 2000 * @param scheduler A custom Handler with which to schedule the 2001 * resultReceiver callback; if null it will be 2002 * scheduled in the Context's main thread. 2003 * @param initialCode An initial value for the result code. Often 2004 * Activity.RESULT_OK. 2005 * @param initialData An initial value for the result data. Often 2006 * null. 2007 * @param initialExtras An initial value for the result extras. Often 2008 * null. 2009 * 2010 * @see #sendBroadcast(Intent) 2011 * @see #sendBroadcast(Intent, String) 2012 * @see #sendOrderedBroadcast(Intent, String) 2013 * @see android.content.BroadcastReceiver 2014 * @see #registerReceiver 2015 * @see android.app.Activity#RESULT_OK 2016 */ 2017 public abstract void sendOrderedBroadcast(@RequiresPermission @NonNull Intent intent, 2018 @Nullable String receiverPermission, @Nullable BroadcastReceiver resultReceiver, 2019 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 2020 @Nullable Bundle initialExtras); 2021 2022 /** 2023 * Version of {@link #sendBroadcast(Intent)} that allows you to 2024 * receive data back from the broadcast. This is accomplished by 2025 * supplying your own BroadcastReceiver when calling, which will be 2026 * treated as a final receiver at the end of the broadcast -- its 2027 * {@link BroadcastReceiver#onReceive} method will be called with 2028 * the result values collected from the other receivers. The broadcast will 2029 * be serialized in the same way as calling 2030 * {@link #sendOrderedBroadcast(Intent, String)}. 2031 * 2032 * <p>Like {@link #sendBroadcast(Intent)}, this method is 2033 * asynchronous; it will return before 2034 * resultReceiver.onReceive() is called. 2035 * 2036 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 2037 * 2038 * 2039 * @param intent The Intent to broadcast; all receivers matching this 2040 * Intent will receive the broadcast. 2041 * @param receiverPermission String naming a permissions that 2042 * a receiver must hold in order to receive your broadcast. 2043 * If null, no permission is required. 2044 * @param options (optional) Additional sending options, generated from a 2045 * {@link android.app.BroadcastOptions}. 2046 * @param resultReceiver Your own BroadcastReceiver to treat as the final 2047 * receiver of the broadcast. 2048 * @param scheduler A custom Handler with which to schedule the 2049 * resultReceiver callback; if null it will be 2050 * scheduled in the Context's main thread. 2051 * @param initialCode An initial value for the result code. Often 2052 * Activity.RESULT_OK. 2053 * @param initialData An initial value for the result data. Often 2054 * null. 2055 * @param initialExtras An initial value for the result extras. Often 2056 * null. 2057 * @see #sendBroadcast(Intent) 2058 * @see #sendBroadcast(Intent, String) 2059 * @see #sendOrderedBroadcast(Intent, String) 2060 * @see android.content.BroadcastReceiver 2061 * @see #registerReceiver 2062 * @see android.app.Activity#RESULT_OK 2063 * @hide 2064 */ 2065 @SystemApi 2066 public abstract void sendOrderedBroadcast(@NonNull Intent intent, 2067 @Nullable String receiverPermission, @Nullable Bundle options, 2068 @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler, 2069 int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras); 2070 2071 /** 2072 * Like {@link #sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, 2073 * int, String, android.os.Bundle)}, but also allows specification 2074 * of an associated app op as per {@link android.app.AppOpsManager}. 2075 * @hide 2076 */ 2077 public abstract void sendOrderedBroadcast(Intent intent, 2078 String receiverPermission, int appOp, BroadcastReceiver resultReceiver, 2079 Handler scheduler, int initialCode, String initialData, 2080 Bundle initialExtras); 2081 2082 /** 2083 * Version of {@link #sendBroadcast(Intent)} that allows you to specify the 2084 * user the broadcast will be sent to. This is not available to applications 2085 * that are not pre-installed on the system image. 2086 * @param intent The intent to broadcast 2087 * @param user UserHandle to send the intent to. 2088 * @see #sendBroadcast(Intent) 2089 */ 2090 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2091 public abstract void sendBroadcastAsUser(@RequiresPermission Intent intent, 2092 UserHandle user); 2093 2094 /** 2095 * Version of {@link #sendBroadcast(Intent, String)} that allows you to specify the 2096 * user the broadcast will be sent to. This is not available to applications 2097 * that are not pre-installed on the system image. 2098 * 2099 * @param intent The Intent to broadcast; all receivers matching this 2100 * Intent will receive the broadcast. 2101 * @param user UserHandle to send the intent to. 2102 * @param receiverPermission (optional) String naming a permission that 2103 * a receiver must hold in order to receive your broadcast. 2104 * If null, no permission is required. 2105 * 2106 * @see #sendBroadcast(Intent, String) 2107 */ 2108 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2109 public abstract void sendBroadcastAsUser(@RequiresPermission Intent intent, 2110 UserHandle user, @Nullable String receiverPermission); 2111 2112 /** 2113 * Version of {@link #sendBroadcast(Intent, String, Bundle)} that allows you to specify the 2114 * user the broadcast will be sent to. This is not available to applications 2115 * that are not pre-installed on the system image. 2116 * 2117 * @param intent The Intent to broadcast; all receivers matching this 2118 * Intent will receive the broadcast. 2119 * @param user UserHandle to send the intent to. 2120 * @param receiverPermission (optional) String naming a permission that 2121 * a receiver must hold in order to receive your broadcast. 2122 * If null, no permission is required. 2123 * @param options (optional) Additional sending options, generated from a 2124 * {@link android.app.BroadcastOptions}. 2125 * 2126 * @see #sendBroadcast(Intent, String, Bundle) 2127 * @hide 2128 */ 2129 @SystemApi 2130 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2131 public abstract void sendBroadcastAsUser(@RequiresPermission Intent intent, 2132 UserHandle user, @Nullable String receiverPermission, @Nullable Bundle options); 2133 2134 /** 2135 * Version of {@link #sendBroadcast(Intent, String)} that allows you to specify the 2136 * user the broadcast will be sent to. This is not available to applications 2137 * that are not pre-installed on the system image. 2138 * 2139 * @param intent The Intent to broadcast; all receivers matching this 2140 * Intent will receive the broadcast. 2141 * @param user UserHandle to send the intent to. 2142 * @param receiverPermission (optional) String naming a permission that 2143 * a receiver must hold in order to receive your broadcast. 2144 * If null, no permission is required. 2145 * @param appOp The app op associated with the broadcast. 2146 * 2147 * @see #sendBroadcast(Intent, String) 2148 * 2149 * @hide 2150 */ 2151 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2152 public abstract void sendBroadcastAsUser(@RequiresPermission Intent intent, 2153 UserHandle user, @Nullable String receiverPermission, int appOp); 2154 2155 /** 2156 * Version of 2157 * {@link #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle)} 2158 * that allows you to specify the 2159 * user the broadcast will be sent to. This is not available to applications 2160 * that are not pre-installed on the system image. 2161 * 2162 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 2163 * 2164 * @param intent The Intent to broadcast; all receivers matching this 2165 * Intent will receive the broadcast. 2166 * @param user UserHandle to send the intent to. 2167 * @param receiverPermission String naming a permissions that 2168 * a receiver must hold in order to receive your broadcast. 2169 * If null, no permission is required. 2170 * @param resultReceiver Your own BroadcastReceiver to treat as the final 2171 * receiver of the broadcast. 2172 * @param scheduler A custom Handler with which to schedule the 2173 * resultReceiver callback; if null it will be 2174 * scheduled in the Context's main thread. 2175 * @param initialCode An initial value for the result code. Often 2176 * Activity.RESULT_OK. 2177 * @param initialData An initial value for the result data. Often 2178 * null. 2179 * @param initialExtras An initial value for the result extras. Often 2180 * null. 2181 * 2182 * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 2183 */ 2184 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2185 public abstract void sendOrderedBroadcastAsUser(@RequiresPermission Intent intent, 2186 UserHandle user, @Nullable String receiverPermission, BroadcastReceiver resultReceiver, 2187 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 2188 @Nullable Bundle initialExtras); 2189 2190 /** 2191 * Similar to above but takes an appOp as well, to enforce restrictions. 2192 * @see #sendOrderedBroadcastAsUser(Intent, UserHandle, String, 2193 * BroadcastReceiver, Handler, int, String, Bundle) 2194 * @hide 2195 */ 2196 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2197 public abstract void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 2198 @Nullable String receiverPermission, int appOp, BroadcastReceiver resultReceiver, 2199 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 2200 @Nullable Bundle initialExtras); 2201 2202 /** 2203 * Similar to above but takes an appOp as well, to enforce restrictions, and an options Bundle. 2204 * @see #sendOrderedBroadcastAsUser(Intent, UserHandle, String, 2205 * BroadcastReceiver, Handler, int, String, Bundle) 2206 * @hide 2207 */ 2208 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2209 public abstract void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 2210 @Nullable String receiverPermission, int appOp, @Nullable Bundle options, 2211 BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, 2212 @Nullable String initialData, @Nullable Bundle initialExtras); 2213 2214 /** 2215 * <p>Perform a {@link #sendBroadcast(Intent)} that is "sticky," meaning the 2216 * Intent you are sending stays around after the broadcast is complete, 2217 * so that others can quickly retrieve that data through the return 2218 * value of {@link #registerReceiver(BroadcastReceiver, IntentFilter)}. In 2219 * all other ways, this behaves the same as 2220 * {@link #sendBroadcast(Intent)}. 2221 * 2222 * @deprecated Sticky broadcasts should not be used. They provide no security (anyone 2223 * can access them), no protection (anyone can modify them), and many other problems. 2224 * The recommended pattern is to use a non-sticky broadcast to report that <em>something</em> 2225 * has changed, with another mechanism for apps to retrieve the current value whenever 2226 * desired. 2227 * 2228 * @param intent The Intent to broadcast; all receivers matching this 2229 * Intent will receive the broadcast, and the Intent will be held to 2230 * be re-broadcast to future receivers. 2231 * 2232 * @see #sendBroadcast(Intent) 2233 * @see #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle) 2234 */ 2235 @Deprecated 2236 @RequiresPermission(android.Manifest.permission.BROADCAST_STICKY) 2237 public abstract void sendStickyBroadcast(@RequiresPermission Intent intent); 2238 2239 /** 2240 * <p>Version of {@link #sendStickyBroadcast} that allows you to 2241 * receive data back from the broadcast. This is accomplished by 2242 * supplying your own BroadcastReceiver when calling, which will be 2243 * treated as a final receiver at the end of the broadcast -- its 2244 * {@link BroadcastReceiver#onReceive} method will be called with 2245 * the result values collected from the other receivers. The broadcast will 2246 * be serialized in the same way as calling 2247 * {@link #sendOrderedBroadcast(Intent, String)}. 2248 * 2249 * <p>Like {@link #sendBroadcast(Intent)}, this method is 2250 * asynchronous; it will return before 2251 * resultReceiver.onReceive() is called. Note that the sticky data 2252 * stored is only the data you initially supply to the broadcast, not 2253 * the result of any changes made by the receivers. 2254 * 2255 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 2256 * 2257 * @deprecated Sticky broadcasts should not be used. They provide no security (anyone 2258 * can access them), no protection (anyone can modify them), and many other problems. 2259 * The recommended pattern is to use a non-sticky broadcast to report that <em>something</em> 2260 * has changed, with another mechanism for apps to retrieve the current value whenever 2261 * desired. 2262 * 2263 * @param intent The Intent to broadcast; all receivers matching this 2264 * Intent will receive the broadcast. 2265 * @param resultReceiver Your own BroadcastReceiver to treat as the final 2266 * receiver of the broadcast. 2267 * @param scheduler A custom Handler with which to schedule the 2268 * resultReceiver callback; if null it will be 2269 * scheduled in the Context's main thread. 2270 * @param initialCode An initial value for the result code. Often 2271 * Activity.RESULT_OK. 2272 * @param initialData An initial value for the result data. Often 2273 * null. 2274 * @param initialExtras An initial value for the result extras. Often 2275 * null. 2276 * 2277 * @see #sendBroadcast(Intent) 2278 * @see #sendBroadcast(Intent, String) 2279 * @see #sendOrderedBroadcast(Intent, String) 2280 * @see #sendStickyBroadcast(Intent) 2281 * @see android.content.BroadcastReceiver 2282 * @see #registerReceiver 2283 * @see android.app.Activity#RESULT_OK 2284 */ 2285 @Deprecated 2286 @RequiresPermission(android.Manifest.permission.BROADCAST_STICKY) 2287 public abstract void sendStickyOrderedBroadcast(@RequiresPermission Intent intent, 2288 BroadcastReceiver resultReceiver, 2289 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 2290 @Nullable Bundle initialExtras); 2291 2292 /** 2293 * <p>Remove the data previously sent with {@link #sendStickyBroadcast}, 2294 * so that it is as if the sticky broadcast had never happened. 2295 * 2296 * @deprecated Sticky broadcasts should not be used. They provide no security (anyone 2297 * can access them), no protection (anyone can modify them), and many other problems. 2298 * The recommended pattern is to use a non-sticky broadcast to report that <em>something</em> 2299 * has changed, with another mechanism for apps to retrieve the current value whenever 2300 * desired. 2301 * 2302 * @param intent The Intent that was previously broadcast. 2303 * 2304 * @see #sendStickyBroadcast 2305 */ 2306 @Deprecated 2307 @RequiresPermission(android.Manifest.permission.BROADCAST_STICKY) 2308 public abstract void removeStickyBroadcast(@RequiresPermission Intent intent); 2309 2310 /** 2311 * <p>Version of {@link #sendStickyBroadcast(Intent)} that allows you to specify the 2312 * user the broadcast will be sent to. This is not available to applications 2313 * that are not pre-installed on the system image. 2314 * 2315 * @deprecated Sticky broadcasts should not be used. They provide no security (anyone 2316 * can access them), no protection (anyone can modify them), and many other problems. 2317 * The recommended pattern is to use a non-sticky broadcast to report that <em>something</em> 2318 * has changed, with another mechanism for apps to retrieve the current value whenever 2319 * desired. 2320 * 2321 * @param intent The Intent to broadcast; all receivers matching this 2322 * Intent will receive the broadcast, and the Intent will be held to 2323 * be re-broadcast to future receivers. 2324 * @param user UserHandle to send the intent to. 2325 * 2326 * @see #sendBroadcast(Intent) 2327 */ 2328 @Deprecated 2329 @RequiresPermission(allOf = { 2330 android.Manifest.permission.INTERACT_ACROSS_USERS, 2331 android.Manifest.permission.BROADCAST_STICKY 2332 }) 2333 public abstract void sendStickyBroadcastAsUser(@RequiresPermission Intent intent, 2334 UserHandle user); 2335 2336 /** 2337 * @hide 2338 * This is just here for sending CONNECTIVITY_ACTION. 2339 */ 2340 @Deprecated 2341 @RequiresPermission(allOf = { 2342 android.Manifest.permission.INTERACT_ACROSS_USERS, 2343 android.Manifest.permission.BROADCAST_STICKY 2344 }) 2345 public abstract void sendStickyBroadcastAsUser(@RequiresPermission Intent intent, 2346 UserHandle user, Bundle options); 2347 2348 /** 2349 * <p>Version of 2350 * {@link #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle)} 2351 * that allows you to specify the 2352 * user the broadcast will be sent to. This is not available to applications 2353 * that are not pre-installed on the system image. 2354 * 2355 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 2356 * 2357 * @deprecated Sticky broadcasts should not be used. They provide no security (anyone 2358 * can access them), no protection (anyone can modify them), and many other problems. 2359 * The recommended pattern is to use a non-sticky broadcast to report that <em>something</em> 2360 * has changed, with another mechanism for apps to retrieve the current value whenever 2361 * desired. 2362 * 2363 * @param intent The Intent to broadcast; all receivers matching this 2364 * Intent will receive the broadcast. 2365 * @param user UserHandle to send the intent to. 2366 * @param resultReceiver Your own BroadcastReceiver to treat as the final 2367 * receiver of the broadcast. 2368 * @param scheduler A custom Handler with which to schedule the 2369 * resultReceiver callback; if null it will be 2370 * scheduled in the Context's main thread. 2371 * @param initialCode An initial value for the result code. Often 2372 * Activity.RESULT_OK. 2373 * @param initialData An initial value for the result data. Often 2374 * null. 2375 * @param initialExtras An initial value for the result extras. Often 2376 * null. 2377 * 2378 * @see #sendStickyOrderedBroadcast(Intent, BroadcastReceiver, Handler, int, String, Bundle) 2379 */ 2380 @Deprecated 2381 @RequiresPermission(allOf = { 2382 android.Manifest.permission.INTERACT_ACROSS_USERS, 2383 android.Manifest.permission.BROADCAST_STICKY 2384 }) 2385 public abstract void sendStickyOrderedBroadcastAsUser(@RequiresPermission Intent intent, 2386 UserHandle user, BroadcastReceiver resultReceiver, 2387 @Nullable Handler scheduler, int initialCode, @Nullable String initialData, 2388 @Nullable Bundle initialExtras); 2389 2390 /** 2391 * <p>Version of {@link #removeStickyBroadcast(Intent)} that allows you to specify the 2392 * user the broadcast will be sent to. This is not available to applications 2393 * that are not pre-installed on the system image. 2394 * 2395 * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} 2396 * permission in order to use this API. If you do not hold that 2397 * permission, {@link SecurityException} will be thrown. 2398 * 2399 * @deprecated Sticky broadcasts should not be used. They provide no security (anyone 2400 * can access them), no protection (anyone can modify them), and many other problems. 2401 * The recommended pattern is to use a non-sticky broadcast to report that <em>something</em> 2402 * has changed, with another mechanism for apps to retrieve the current value whenever 2403 * desired. 2404 * 2405 * @param intent The Intent that was previously broadcast. 2406 * @param user UserHandle to remove the sticky broadcast from. 2407 * 2408 * @see #sendStickyBroadcastAsUser 2409 */ 2410 @Deprecated 2411 @RequiresPermission(allOf = { 2412 android.Manifest.permission.INTERACT_ACROSS_USERS, 2413 android.Manifest.permission.BROADCAST_STICKY 2414 }) 2415 public abstract void removeStickyBroadcastAsUser(@RequiresPermission Intent intent, 2416 UserHandle user); 2417 2418 /** 2419 * Register a BroadcastReceiver to be run in the main activity thread. The 2420 * <var>receiver</var> will be called with any broadcast Intent that 2421 * matches <var>filter</var>, in the main application thread. 2422 * 2423 * <p>The system may broadcast Intents that are "sticky" -- these stay 2424 * around after the broadcast has finished, to be sent to any later 2425 * registrations. If your IntentFilter matches one of these sticky 2426 * Intents, that Intent will be returned by this function 2427 * <strong>and</strong> sent to your <var>receiver</var> as if it had just 2428 * been broadcast. 2429 * 2430 * <p>There may be multiple sticky Intents that match <var>filter</var>, 2431 * in which case each of these will be sent to <var>receiver</var>. In 2432 * this case, only one of these can be returned directly by the function; 2433 * which of these that is returned is arbitrarily decided by the system. 2434 * 2435 * <p>If you know the Intent your are registering for is sticky, you can 2436 * supply null for your <var>receiver</var>. In this case, no receiver is 2437 * registered -- the function simply returns the sticky Intent that 2438 * matches <var>filter</var>. In the case of multiple matches, the same 2439 * rules as described above apply. 2440 * 2441 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 2442 * 2443 * <p>As of {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, receivers 2444 * registered with this method will correctly respect the 2445 * {@link Intent#setPackage(String)} specified for an Intent being broadcast. 2446 * Prior to that, it would be ignored and delivered to all matching registered 2447 * receivers. Be careful if using this for security.</p> 2448 * 2449 * <p class="note">Note: this method <em>cannot be called from a 2450 * {@link BroadcastReceiver} component;</em> that is, from a BroadcastReceiver 2451 * that is declared in an application's manifest. It is okay, however, to call 2452 * this method from another BroadcastReceiver that has itself been registered 2453 * at run time with {@link #registerReceiver}, since the lifetime of such a 2454 * registered BroadcastReceiver is tied to the object that registered it.</p> 2455 * 2456 * @param receiver The BroadcastReceiver to handle the broadcast. 2457 * @param filter Selects the Intent broadcasts to be received. 2458 * 2459 * @return The first sticky intent found that matches <var>filter</var>, 2460 * or null if there are none. 2461 * 2462 * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 2463 * @see #sendBroadcast 2464 * @see #unregisterReceiver 2465 */ 2466 @Nullable 2467 public abstract Intent registerReceiver(@Nullable BroadcastReceiver receiver, 2468 IntentFilter filter); 2469 2470 /** 2471 * Register to receive intent broadcasts, with the receiver optionally being 2472 * exposed to Instant Apps. See 2473 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)} for more 2474 * information. By default Instant Apps cannot interact with receivers in other 2475 * applications, this allows you to expose a receiver that Instant Apps can 2476 * interact with. 2477 * 2478 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 2479 * 2480 * <p>As of {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, receivers 2481 * registered with this method will correctly respect the 2482 * {@link Intent#setPackage(String)} specified for an Intent being broadcast. 2483 * Prior to that, it would be ignored and delivered to all matching registered 2484 * receivers. Be careful if using this for security.</p> 2485 * 2486 * @param receiver The BroadcastReceiver to handle the broadcast. 2487 * @param filter Selects the Intent broadcasts to be received. 2488 * @param flags Additional options for the receiver. May be 0 or 2489 * {@link #RECEIVER_VISIBLE_TO_INSTANT_APPS}. 2490 * 2491 * @return The first sticky intent found that matches <var>filter</var>, 2492 * or null if there are none. 2493 * 2494 * @see #registerReceiver(BroadcastReceiver, IntentFilter) 2495 * @see #sendBroadcast 2496 * @see #unregisterReceiver 2497 */ 2498 @Nullable 2499 public abstract Intent registerReceiver(@Nullable BroadcastReceiver receiver, 2500 IntentFilter filter, 2501 @RegisterReceiverFlags int flags); 2502 2503 /** 2504 * Register to receive intent broadcasts, to run in the context of 2505 * <var>scheduler</var>. See 2506 * {@link #registerReceiver(BroadcastReceiver, IntentFilter)} for more 2507 * information. This allows you to enforce permissions on who can 2508 * broadcast intents to your receiver, or have the receiver run in 2509 * a different thread than the main application thread. 2510 * 2511 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 2512 * 2513 * <p>As of {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, receivers 2514 * registered with this method will correctly respect the 2515 * {@link Intent#setPackage(String)} specified for an Intent being broadcast. 2516 * Prior to that, it would be ignored and delivered to all matching registered 2517 * receivers. Be careful if using this for security.</p> 2518 * 2519 * @param receiver The BroadcastReceiver to handle the broadcast. 2520 * @param filter Selects the Intent broadcasts to be received. 2521 * @param broadcastPermission String naming a permissions that a 2522 * broadcaster must hold in order to send an Intent to you. If null, 2523 * no permission is required. 2524 * @param scheduler Handler identifying the thread that will receive 2525 * the Intent. If null, the main thread of the process will be used. 2526 * 2527 * @return The first sticky intent found that matches <var>filter</var>, 2528 * or null if there are none. 2529 * 2530 * @see #registerReceiver(BroadcastReceiver, IntentFilter) 2531 * @see #sendBroadcast 2532 * @see #unregisterReceiver 2533 */ 2534 @Nullable 2535 public abstract Intent registerReceiver(BroadcastReceiver receiver, 2536 IntentFilter filter, @Nullable String broadcastPermission, 2537 @Nullable Handler scheduler); 2538 2539 /** 2540 * Register to receive intent broadcasts, to run in the context of 2541 * <var>scheduler</var>. See 2542 * {@link #registerReceiver(BroadcastReceiver, IntentFilter, int)} and 2543 * {@link #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler)} 2544 * for more information. 2545 * 2546 * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts. 2547 * 2548 * <p>As of {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, receivers 2549 * registered with this method will correctly respect the 2550 * {@link Intent#setPackage(String)} specified for an Intent being broadcast. 2551 * Prior to that, it would be ignored and delivered to all matching registered 2552 * receivers. Be careful if using this for security.</p> 2553 * 2554 * @param receiver The BroadcastReceiver to handle the broadcast. 2555 * @param filter Selects the Intent broadcasts to be received. 2556 * @param broadcastPermission String naming a permissions that a 2557 * broadcaster must hold in order to send an Intent to you. If null, 2558 * no permission is required. 2559 * @param scheduler Handler identifying the thread that will receive 2560 * the Intent. If null, the main thread of the process will be used. 2561 * @param flags Additional options for the receiver. May be 0 or 2562 * {@link #RECEIVER_VISIBLE_TO_INSTANT_APPS}. 2563 * 2564 * @return The first sticky intent found that matches <var>filter</var>, 2565 * or null if there are none. 2566 * 2567 * @see #registerReceiver(BroadcastReceiver, IntentFilter, int) 2568 * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 2569 * @see #sendBroadcast 2570 * @see #unregisterReceiver 2571 */ 2572 @Nullable 2573 public abstract Intent registerReceiver(BroadcastReceiver receiver, 2574 IntentFilter filter, @Nullable String broadcastPermission, 2575 @Nullable Handler scheduler, @RegisterReceiverFlags int flags); 2576 2577 /** 2578 * @hide 2579 * Same as {@link #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 2580 * but for a specific user. This receiver will receiver broadcasts that 2581 * are sent to the requested user. 2582 * 2583 * @param receiver The BroadcastReceiver to handle the broadcast. 2584 * @param user UserHandle to send the intent to. 2585 * @param filter Selects the Intent broadcasts to be received. 2586 * @param broadcastPermission String naming a permissions that a 2587 * broadcaster must hold in order to send an Intent to you. If null, 2588 * no permission is required. 2589 * @param scheduler Handler identifying the thread that will receive 2590 * the Intent. If null, the main thread of the process will be used. 2591 * 2592 * @return The first sticky intent found that matches <var>filter</var>, 2593 * or null if there are none. 2594 * 2595 * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 2596 * @see #sendBroadcast 2597 * @see #unregisterReceiver 2598 */ 2599 @Nullable 2600 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) 2601 public abstract Intent registerReceiverAsUser(BroadcastReceiver receiver, 2602 UserHandle user, IntentFilter filter, @Nullable String broadcastPermission, 2603 @Nullable Handler scheduler); 2604 2605 /** 2606 * Unregister a previously registered BroadcastReceiver. <em>All</em> 2607 * filters that have been registered for this BroadcastReceiver will be 2608 * removed. 2609 * 2610 * @param receiver The BroadcastReceiver to unregister. 2611 * 2612 * @see #registerReceiver 2613 */ 2614 public abstract void unregisterReceiver(BroadcastReceiver receiver); 2615 2616 /** 2617 * Request that a given application service be started. The Intent 2618 * should either contain the complete class name of a specific service 2619 * implementation to start, or a specific package name to target. If the 2620 * Intent is less specified, it logs a warning about this. In this case any of the 2621 * multiple matching services may be used. If this service 2622 * is not already running, it will be instantiated and started (creating a 2623 * process for it if needed); if it is running then it remains running. 2624 * 2625 * <p>Every call to this method will result in a corresponding call to 2626 * the target service's {@link android.app.Service#onStartCommand} method, 2627 * with the <var>intent</var> given here. This provides a convenient way 2628 * to submit jobs to a service without having to bind and call on to its 2629 * interface. 2630 * 2631 * <p>Using startService() overrides the default service lifetime that is 2632 * managed by {@link #bindService}: it requires the service to remain 2633 * running until {@link #stopService} is called, regardless of whether 2634 * any clients are connected to it. Note that calls to startService() 2635 * do not nest: no matter how many times you call startService(), 2636 * a single call to {@link #stopService} will stop it. 2637 * 2638 * <p>The system attempts to keep running services around as much as 2639 * possible. The only time they should be stopped is if the current 2640 * foreground application is using so many resources that the service needs 2641 * to be killed. If any errors happen in the service's process, it will 2642 * automatically be restarted. 2643 * 2644 * <p>This function will throw {@link SecurityException} if you do not 2645 * have permission to start the given service. 2646 * 2647 * <p class="note"><strong>Note:</strong> Each call to startService() 2648 * results in significant work done by the system to manage service 2649 * lifecycle surrounding the processing of the intent, which can take 2650 * multiple milliseconds of CPU time. Due to this cost, startService() 2651 * should not be used for frequent intent delivery to a service, and only 2652 * for scheduling significant work. Use {@link #bindService bound services} 2653 * for high frequency calls. 2654 * </p> 2655 * 2656 * @param service Identifies the service to be started. The Intent must be 2657 * fully explicit (supplying a component name). Additional values 2658 * may be included in the Intent extras to supply arguments along with 2659 * this specific start call. 2660 * 2661 * @return If the service is being started or is already running, the 2662 * {@link ComponentName} of the actual service that was started is 2663 * returned; else if the service does not exist null is returned. 2664 * 2665 * @throws SecurityException If the caller does not have permission to access the service 2666 * or the service can not be found. 2667 * @throws IllegalStateException If the application is in a state where the service 2668 * can not be started (such as not in the foreground in a state when services are allowed). 2669 * 2670 * @see #stopService 2671 * @see #bindService 2672 */ 2673 @Nullable 2674 public abstract ComponentName startService(Intent service); 2675 2676 /** 2677 * Similar to {@link #startService(Intent)}, but with an implicit promise that the 2678 * Service will call {@link android.app.Service#startForeground(int, Notification) 2679 * startForeground(int, Notification)} once it begins running. The service is given 2680 * an amount of time comparable to the ANR interval to do this, otherwise the system 2681 * will automatically stop the service and declare the app ANR. 2682 * 2683 * <p>Unlike the ordinary {@link #startService(Intent)}, this method can be used 2684 * at any time, regardless of whether the app hosting the service is in a foreground 2685 * state. 2686 * 2687 * @param service Identifies the service to be started. The Intent must be 2688 * fully explicit (supplying a component name). Additional values 2689 * may be included in the Intent extras to supply arguments along with 2690 * this specific start call. 2691 * 2692 * @return If the service is being started or is already running, the 2693 * {@link ComponentName} of the actual service that was started is 2694 * returned; else if the service does not exist null is returned. 2695 * 2696 * @throws SecurityException If the caller does not have permission to access the service 2697 * or the service can not be found. 2698 * 2699 * @see #stopService 2700 * @see android.app.Service#startForeground(int, Notification) 2701 */ 2702 @Nullable 2703 public abstract ComponentName startForegroundService(Intent service); 2704 2705 /** 2706 * @hide like {@link #startForegroundService(Intent)} but for a specific user. 2707 */ 2708 @Nullable 2709 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2710 public abstract ComponentName startForegroundServiceAsUser(Intent service, UserHandle user); 2711 2712 /** 2713 * Request that a given application service be stopped. If the service is 2714 * not running, nothing happens. Otherwise it is stopped. Note that calls 2715 * to startService() are not counted -- this stops the service no matter 2716 * how many times it was started. 2717 * 2718 * <p>Note that if a stopped service still has {@link ServiceConnection} 2719 * objects bound to it with the {@link #BIND_AUTO_CREATE} set, it will 2720 * not be destroyed until all of these bindings are removed. See 2721 * the {@link android.app.Service} documentation for more details on a 2722 * service's lifecycle. 2723 * 2724 * <p>This function will throw {@link SecurityException} if you do not 2725 * have permission to stop the given service. 2726 * 2727 * @param service Description of the service to be stopped. The Intent must be either 2728 * fully explicit (supplying a component name) or specify a specific package 2729 * name it is targetted to. 2730 * 2731 * @return If there is a service matching the given Intent that is already 2732 * running, then it is stopped and {@code true} is returned; else {@code false} is returned. 2733 * 2734 * @throws SecurityException If the caller does not have permission to access the service 2735 * or the service can not be found. 2736 * @throws IllegalStateException If the application is in a state where the service 2737 * can not be started (such as not in the foreground in a state when services are allowed). 2738 * 2739 * @see #startService 2740 */ 2741 public abstract boolean stopService(Intent service); 2742 2743 /** 2744 * @hide like {@link #startService(Intent)} but for a specific user. 2745 */ 2746 @Nullable 2747 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2748 public abstract ComponentName startServiceAsUser(Intent service, UserHandle user); 2749 2750 /** 2751 * @hide like {@link #stopService(Intent)} but for a specific user. 2752 */ 2753 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2754 public abstract boolean stopServiceAsUser(Intent service, UserHandle user); 2755 2756 /** 2757 * Connect to an application service, creating it if needed. This defines 2758 * a dependency between your application and the service. The given 2759 * <var>conn</var> will receive the service object when it is created and be 2760 * told if it dies and restarts. The service will be considered required 2761 * by the system only for as long as the calling context exists. For 2762 * example, if this Context is an Activity that is stopped, the service will 2763 * not be required to continue running until the Activity is resumed. 2764 * 2765 * <p>This function will throw {@link SecurityException} if you do not 2766 * have permission to bind to the given service. 2767 * 2768 * <p class="note">Note: this method <em>can not be called from a 2769 * {@link BroadcastReceiver} component</em>. A pattern you can use to 2770 * communicate from a BroadcastReceiver to a Service is to call 2771 * {@link #startService} with the arguments containing the command to be 2772 * sent, with the service calling its 2773 * {@link android.app.Service#stopSelf(int)} method when done executing 2774 * that command. See the API demo App/Service/Service Start Arguments 2775 * Controller for an illustration of this. It is okay, however, to use 2776 * this method from a BroadcastReceiver that has been registered with 2777 * {@link #registerReceiver}, since the lifetime of this BroadcastReceiver 2778 * is tied to another object (the one that registered it).</p> 2779 * 2780 * @param service Identifies the service to connect to. The Intent must 2781 * specify an explicit component name. 2782 * @param conn Receives information as the service is started and stopped. 2783 * This must be a valid ServiceConnection object; it must not be null. 2784 * @param flags Operation options for the binding. May be 0, 2785 * {@link #BIND_AUTO_CREATE}, {@link #BIND_DEBUG_UNBIND}, 2786 * {@link #BIND_NOT_FOREGROUND}, {@link #BIND_ABOVE_CLIENT}, 2787 * {@link #BIND_ALLOW_OOM_MANAGEMENT}, or 2788 * {@link #BIND_WAIVE_PRIORITY}. 2789 * @return If you have successfully bound to the service, {@code true} is returned; 2790 * {@code false} is returned if the connection is not made so you will not 2791 * receive the service object. However, you should still call 2792 * {@link #unbindService} to release the connection. 2793 * 2794 * @throws SecurityException If the caller does not have permission to access the service 2795 * or the service can not be found. 2796 * 2797 * @see #unbindService 2798 * @see #startService 2799 * @see #BIND_AUTO_CREATE 2800 * @see #BIND_DEBUG_UNBIND 2801 * @see #BIND_NOT_FOREGROUND 2802 */ 2803 public abstract boolean bindService(@RequiresPermission Intent service, 2804 @NonNull ServiceConnection conn, @BindServiceFlags int flags); 2805 2806 /** 2807 * Same as {@link #bindService(Intent, ServiceConnection, int)}, but with an explicit userHandle 2808 * argument for use by system server and other multi-user aware code. 2809 * @hide 2810 */ 2811 @SystemApi 2812 @SuppressWarnings("unused") 2813 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2814 public boolean bindServiceAsUser(@RequiresPermission Intent service, ServiceConnection conn, 2815 int flags, UserHandle user) { 2816 throw new RuntimeException("Not implemented. Must override in a subclass."); 2817 } 2818 2819 /** 2820 * Same as {@link #bindService(Intent, ServiceConnection, int, UserHandle)}, but with an 2821 * explicit non-null Handler to run the ServiceConnection callbacks on. 2822 * 2823 * @hide 2824 */ 2825 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) 2826 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, 2827 Handler handler, UserHandle user) { 2828 throw new RuntimeException("Not implemented. Must override in a subclass."); 2829 } 2830 2831 /** 2832 * Disconnect from an application service. You will no longer receive 2833 * calls as the service is restarted, and the service is now allowed to 2834 * stop at any time. 2835 * 2836 * @param conn The connection interface previously supplied to 2837 * bindService(). This parameter must not be null. 2838 * 2839 * @see #bindService 2840 */ 2841 public abstract void unbindService(@NonNull ServiceConnection conn); 2842 2843 /** 2844 * Start executing an {@link android.app.Instrumentation} class. The given 2845 * Instrumentation component will be run by killing its target application 2846 * (if currently running), starting the target process, instantiating the 2847 * instrumentation component, and then letting it drive the application. 2848 * 2849 * <p>This function is not synchronous -- it returns as soon as the 2850 * instrumentation has started and while it is running. 2851 * 2852 * <p>Instrumentation is normally only allowed to run against a package 2853 * that is either unsigned or signed with a signature that the 2854 * the instrumentation package is also signed with (ensuring the target 2855 * trusts the instrumentation). 2856 * 2857 * @param className Name of the Instrumentation component to be run. 2858 * @param profileFile Optional path to write profiling data as the 2859 * instrumentation runs, or null for no profiling. 2860 * @param arguments Additional optional arguments to pass to the 2861 * instrumentation, or null. 2862 * 2863 * @return {@code true} if the instrumentation was successfully started, 2864 * else {@code false} if it could not be found. 2865 */ 2866 public abstract boolean startInstrumentation(@NonNull ComponentName className, 2867 @Nullable String profileFile, @Nullable Bundle arguments); 2868 2869 /** @hide */ 2870 @StringDef({ 2871 POWER_SERVICE, 2872 WINDOW_SERVICE, 2873 LAYOUT_INFLATER_SERVICE, 2874 ACCOUNT_SERVICE, 2875 ACTIVITY_SERVICE, 2876 ALARM_SERVICE, 2877 NOTIFICATION_SERVICE, 2878 ACCESSIBILITY_SERVICE, 2879 CAPTIONING_SERVICE, 2880 KEYGUARD_SERVICE, 2881 LOCATION_SERVICE, 2882 //@hide: COUNTRY_DETECTOR, 2883 SEARCH_SERVICE, 2884 SENSOR_SERVICE, 2885 STORAGE_SERVICE, 2886 STORAGE_STATS_SERVICE, 2887 WALLPAPER_SERVICE, 2888 TIME_ZONE_RULES_MANAGER_SERVICE, 2889 VIBRATOR_SERVICE, 2890 //@hide: STATUS_BAR_SERVICE, 2891 CONNECTIVITY_SERVICE, 2892 IPSEC_SERVICE, 2893 //@hide: UPDATE_LOCK_SERVICE, 2894 //@hide: NETWORKMANAGEMENT_SERVICE, 2895 NETWORK_STATS_SERVICE, 2896 //@hide: NETWORK_POLICY_SERVICE, 2897 WIFI_SERVICE, 2898 WIFI_AWARE_SERVICE, 2899 WIFI_P2P_SERVICE, 2900 WIFI_SCANNING_SERVICE, 2901 //@hide: WIFI_RTT_SERVICE, 2902 //@hide: ETHERNET_SERVICE, 2903 WIFI_RTT_SERVICE, 2904 NSD_SERVICE, 2905 AUDIO_SERVICE, 2906 FINGERPRINT_SERVICE, 2907 MEDIA_ROUTER_SERVICE, 2908 TELEPHONY_SERVICE, 2909 TELEPHONY_SUBSCRIPTION_SERVICE, 2910 CARRIER_CONFIG_SERVICE, 2911 TELECOM_SERVICE, 2912 CLIPBOARD_SERVICE, 2913 INPUT_METHOD_SERVICE, 2914 TEXT_SERVICES_MANAGER_SERVICE, 2915 TEXT_CLASSIFICATION_SERVICE, 2916 APPWIDGET_SERVICE, 2917 //@hide: VOICE_INTERACTION_MANAGER_SERVICE, 2918 //@hide: BACKUP_SERVICE, 2919 DROPBOX_SERVICE, 2920 //@hide: DEVICE_IDLE_CONTROLLER, 2921 DEVICE_POLICY_SERVICE, 2922 UI_MODE_SERVICE, 2923 DOWNLOAD_SERVICE, 2924 NFC_SERVICE, 2925 BLUETOOTH_SERVICE, 2926 //@hide: SIP_SERVICE, 2927 USB_SERVICE, 2928 LAUNCHER_APPS_SERVICE, 2929 //@hide: SERIAL_SERVICE, 2930 //@hide: HDMI_CONTROL_SERVICE, 2931 INPUT_SERVICE, 2932 DISPLAY_SERVICE, 2933 USER_SERVICE, 2934 RESTRICTIONS_SERVICE, 2935 APP_OPS_SERVICE, 2936 CAMERA_SERVICE, 2937 PRINT_SERVICE, 2938 CONSUMER_IR_SERVICE, 2939 //@hide: TRUST_SERVICE, 2940 TV_INPUT_SERVICE, 2941 //@hide: NETWORK_SCORE_SERVICE, 2942 USAGE_STATS_SERVICE, 2943 MEDIA_SESSION_SERVICE, 2944 BATTERY_SERVICE, 2945 JOB_SCHEDULER_SERVICE, 2946 //@hide: PERSISTENT_DATA_BLOCK_SERVICE, 2947 //@hide: OEM_LOCK_SERVICE, 2948 MEDIA_PROJECTION_SERVICE, 2949 MIDI_SERVICE, 2950 RADIO_SERVICE, 2951 HARDWARE_PROPERTIES_SERVICE, 2952 //@hide: SOUND_TRIGGER_SERVICE, 2953 SHORTCUT_SERVICE, 2954 //@hide: CONTEXTHUB_SERVICE, 2955 SYSTEM_HEALTH_SERVICE, 2956 //@hide: INCIDENT_SERVICE 2957 }) 2958 @Retention(RetentionPolicy.SOURCE) 2959 public @interface ServiceName {} 2960 2961 /** 2962 * Return the handle to a system-level service by name. The class of the 2963 * returned object varies by the requested name. Currently available names 2964 * are: 2965 * 2966 * <dl> 2967 * <dt> {@link #WINDOW_SERVICE} ("window") 2968 * <dd> The top-level window manager in which you can place custom 2969 * windows. The returned object is a {@link android.view.WindowManager}. 2970 * <dt> {@link #LAYOUT_INFLATER_SERVICE} ("layout_inflater") 2971 * <dd> A {@link android.view.LayoutInflater} for inflating layout resources 2972 * in this context. 2973 * <dt> {@link #ACTIVITY_SERVICE} ("activity") 2974 * <dd> A {@link android.app.ActivityManager} for interacting with the 2975 * global activity state of the system. 2976 * <dt> {@link #POWER_SERVICE} ("power") 2977 * <dd> A {@link android.os.PowerManager} for controlling power 2978 * management. 2979 * <dt> {@link #ALARM_SERVICE} ("alarm") 2980 * <dd> A {@link android.app.AlarmManager} for receiving intents at the 2981 * time of your choosing. 2982 * <dt> {@link #NOTIFICATION_SERVICE} ("notification") 2983 * <dd> A {@link android.app.NotificationManager} for informing the user 2984 * of background events. 2985 * <dt> {@link #KEYGUARD_SERVICE} ("keyguard") 2986 * <dd> A {@link android.app.KeyguardManager} for controlling keyguard. 2987 * <dt> {@link #LOCATION_SERVICE} ("location") 2988 * <dd> A {@link android.location.LocationManager} for controlling location 2989 * (e.g., GPS) updates. 2990 * <dt> {@link #SEARCH_SERVICE} ("search") 2991 * <dd> A {@link android.app.SearchManager} for handling search. 2992 * <dt> {@link #VIBRATOR_SERVICE} ("vibrator") 2993 * <dd> A {@link android.os.Vibrator} for interacting with the vibrator 2994 * hardware. 2995 * <dt> {@link #CONNECTIVITY_SERVICE} ("connection") 2996 * <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for 2997 * handling management of network connections. 2998 * <dt> {@link #IPSEC_SERVICE} ("ipsec") 2999 * <dd> A {@link android.net.IpSecManager IpSecManager} for managing IPSec on 3000 * sockets and networks. 3001 * <dt> {@link #WIFI_SERVICE} ("wifi") 3002 * <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of Wi-Fi 3003 * connectivity. On releases before NYC, it should only be obtained from an application 3004 * context, and not from any other derived context to avoid memory leaks within the calling 3005 * process. 3006 * <dt> {@link #WIFI_AWARE_SERVICE} ("wifiaware") 3007 * <dd> A {@link android.net.wifi.aware.WifiAwareManager WifiAwareManager} for management of 3008 * Wi-Fi Aware discovery and connectivity. 3009 * <dt> {@link #WIFI_P2P_SERVICE} ("wifip2p") 3010 * <dd> A {@link android.net.wifi.p2p.WifiP2pManager WifiP2pManager} for management of 3011 * Wi-Fi Direct connectivity. 3012 * <dt> {@link #INPUT_METHOD_SERVICE} ("input_method") 3013 * <dd> An {@link android.view.inputmethod.InputMethodManager InputMethodManager} 3014 * for management of input methods. 3015 * <dt> {@link #UI_MODE_SERVICE} ("uimode") 3016 * <dd> An {@link android.app.UiModeManager} for controlling UI modes. 3017 * <dt> {@link #DOWNLOAD_SERVICE} ("download") 3018 * <dd> A {@link android.app.DownloadManager} for requesting HTTP downloads 3019 * <dt> {@link #BATTERY_SERVICE} ("batterymanager") 3020 * <dd> A {@link android.os.BatteryManager} for managing battery state 3021 * <dt> {@link #JOB_SCHEDULER_SERVICE} ("taskmanager") 3022 * <dd> A {@link android.app.job.JobScheduler} for managing scheduled tasks 3023 * <dt> {@link #NETWORK_STATS_SERVICE} ("netstats") 3024 * <dd> A {@link android.app.usage.NetworkStatsManager NetworkStatsManager} for querying network 3025 * usage statistics. 3026 * <dt> {@link #HARDWARE_PROPERTIES_SERVICE} ("hardware_properties") 3027 * <dd> A {@link android.os.HardwarePropertiesManager} for accessing hardware properties. 3028 * </dl> 3029 * 3030 * <p>Note: System services obtained via this API may be closely associated with 3031 * the Context in which they are obtained from. In general, do not share the 3032 * service objects between various different contexts (Activities, Applications, 3033 * Services, Providers, etc.) 3034 * 3035 * @param name The name of the desired service. 3036 * 3037 * @return The service or null if the name does not exist. 3038 * 3039 * @see #WINDOW_SERVICE 3040 * @see android.view.WindowManager 3041 * @see #LAYOUT_INFLATER_SERVICE 3042 * @see android.view.LayoutInflater 3043 * @see #ACTIVITY_SERVICE 3044 * @see android.app.ActivityManager 3045 * @see #POWER_SERVICE 3046 * @see android.os.PowerManager 3047 * @see #ALARM_SERVICE 3048 * @see android.app.AlarmManager 3049 * @see #NOTIFICATION_SERVICE 3050 * @see android.app.NotificationManager 3051 * @see #KEYGUARD_SERVICE 3052 * @see android.app.KeyguardManager 3053 * @see #LOCATION_SERVICE 3054 * @see android.location.LocationManager 3055 * @see #SEARCH_SERVICE 3056 * @see android.app.SearchManager 3057 * @see #SENSOR_SERVICE 3058 * @see android.hardware.SensorManager 3059 * @see #STORAGE_SERVICE 3060 * @see android.os.storage.StorageManager 3061 * @see #VIBRATOR_SERVICE 3062 * @see android.os.Vibrator 3063 * @see #CONNECTIVITY_SERVICE 3064 * @see android.net.ConnectivityManager 3065 * @see #WIFI_SERVICE 3066 * @see android.net.wifi.WifiManager 3067 * @see #AUDIO_SERVICE 3068 * @see android.media.AudioManager 3069 * @see #MEDIA_ROUTER_SERVICE 3070 * @see android.media.MediaRouter 3071 * @see #TELEPHONY_SERVICE 3072 * @see android.telephony.TelephonyManager 3073 * @see #TELEPHONY_SUBSCRIPTION_SERVICE 3074 * @see android.telephony.SubscriptionManager 3075 * @see #CARRIER_CONFIG_SERVICE 3076 * @see android.telephony.CarrierConfigManager 3077 * @see #INPUT_METHOD_SERVICE 3078 * @see android.view.inputmethod.InputMethodManager 3079 * @see #UI_MODE_SERVICE 3080 * @see android.app.UiModeManager 3081 * @see #DOWNLOAD_SERVICE 3082 * @see android.app.DownloadManager 3083 * @see #BATTERY_SERVICE 3084 * @see android.os.BatteryManager 3085 * @see #JOB_SCHEDULER_SERVICE 3086 * @see android.app.job.JobScheduler 3087 * @see #NETWORK_STATS_SERVICE 3088 * @see android.app.usage.NetworkStatsManager 3089 * @see android.os.HardwarePropertiesManager 3090 * @see #HARDWARE_PROPERTIES_SERVICE 3091 */ 3092 public abstract @Nullable Object getSystemService(@ServiceName @NonNull String name); 3093 3094 /** 3095 * Return the handle to a system-level service by class. 3096 * <p> 3097 * Currently available classes are: 3098 * {@link android.view.WindowManager}, {@link android.view.LayoutInflater}, 3099 * {@link android.app.ActivityManager}, {@link android.os.PowerManager}, 3100 * {@link android.app.AlarmManager}, {@link android.app.NotificationManager}, 3101 * {@link android.app.KeyguardManager}, {@link android.location.LocationManager}, 3102 * {@link android.app.SearchManager}, {@link android.os.Vibrator}, 3103 * {@link android.net.ConnectivityManager}, 3104 * {@link android.net.wifi.WifiManager}, 3105 * {@link android.media.AudioManager}, {@link android.media.MediaRouter}, 3106 * {@link android.telephony.TelephonyManager}, {@link android.telephony.SubscriptionManager}, 3107 * {@link android.view.inputmethod.InputMethodManager}, 3108 * {@link android.app.UiModeManager}, {@link android.app.DownloadManager}, 3109 * {@link android.os.BatteryManager}, {@link android.app.job.JobScheduler}, 3110 * {@link android.app.usage.NetworkStatsManager}. 3111 * </p><p> 3112 * Note: System services obtained via this API may be closely associated with 3113 * the Context in which they are obtained from. In general, do not share the 3114 * service objects between various different contexts (Activities, Applications, 3115 * Services, Providers, etc.) 3116 * </p> 3117 * 3118 * @param serviceClass The class of the desired service. 3119 * @return The service or null if the class is not a supported system service. 3120 */ 3121 @SuppressWarnings("unchecked") 3122 public final @Nullable <T> T getSystemService(@NonNull Class<T> serviceClass) { 3123 // Because subclasses may override getSystemService(String) we cannot 3124 // perform a lookup by class alone. We must first map the class to its 3125 // service name then invoke the string-based method. 3126 String serviceName = getSystemServiceName(serviceClass); 3127 return serviceName != null ? (T)getSystemService(serviceName) : null; 3128 } 3129 3130 /** 3131 * Gets the name of the system-level service that is represented by the specified class. 3132 * 3133 * @param serviceClass The class of the desired service. 3134 * @return The service name or null if the class is not a supported system service. 3135 */ 3136 public abstract @Nullable String getSystemServiceName(@NonNull Class<?> serviceClass); 3137 3138 /** 3139 * Use with {@link #getSystemService} to retrieve a 3140 * {@link android.os.PowerManager} for controlling power management, 3141 * including "wake locks," which let you keep the device on while 3142 * you're running long tasks. 3143 */ 3144 public static final String POWER_SERVICE = "power"; 3145 3146 /** 3147 * Use with {@link #getSystemService} to retrieve a 3148 * {@link android.os.RecoverySystem} for accessing the recovery system 3149 * service. 3150 * 3151 * @see #getSystemService 3152 * @hide 3153 */ 3154 public static final String RECOVERY_SERVICE = "recovery"; 3155 3156 /** 3157 * Use with {@link #getSystemService} to retrieve a 3158 * {@link android.view.WindowManager} for accessing the system's window 3159 * manager. 3160 * 3161 * @see #getSystemService 3162 * @see android.view.WindowManager 3163 */ 3164 public static final String WINDOW_SERVICE = "window"; 3165 3166 /** 3167 * Use with {@link #getSystemService} to retrieve a 3168 * {@link android.view.LayoutInflater} for inflating layout resources in this 3169 * context. 3170 * 3171 * @see #getSystemService 3172 * @see android.view.LayoutInflater 3173 */ 3174 public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater"; 3175 3176 /** 3177 * Use with {@link #getSystemService} to retrieve a 3178 * {@link android.accounts.AccountManager} for receiving intents at a 3179 * time of your choosing. 3180 * 3181 * @see #getSystemService 3182 * @see android.accounts.AccountManager 3183 */ 3184 public static final String ACCOUNT_SERVICE = "account"; 3185 3186 /** 3187 * Use with {@link #getSystemService} to retrieve a 3188 * {@link android.app.ActivityManager} for interacting with the global 3189 * system state. 3190 * 3191 * @see #getSystemService 3192 * @see android.app.ActivityManager 3193 */ 3194 public static final String ACTIVITY_SERVICE = "activity"; 3195 3196 /** 3197 * Use with {@link #getSystemService} to retrieve a 3198 * {@link android.app.AlarmManager} for receiving intents at a 3199 * time of your choosing. 3200 * 3201 * @see #getSystemService 3202 * @see android.app.AlarmManager 3203 */ 3204 public static final String ALARM_SERVICE = "alarm"; 3205 3206 /** 3207 * Use with {@link #getSystemService} to retrieve a 3208 * {@link android.app.NotificationManager} for informing the user of 3209 * background events. 3210 * 3211 * @see #getSystemService 3212 * @see android.app.NotificationManager 3213 */ 3214 public static final String NOTIFICATION_SERVICE = "notification"; 3215 3216 /** 3217 * Use with {@link #getSystemService} to retrieve a 3218 * {@link android.view.accessibility.AccessibilityManager} for giving the user 3219 * feedback for UI events through the registered event listeners. 3220 * 3221 * @see #getSystemService 3222 * @see android.view.accessibility.AccessibilityManager 3223 */ 3224 public static final String ACCESSIBILITY_SERVICE = "accessibility"; 3225 3226 /** 3227 * Use with {@link #getSystemService} to retrieve a 3228 * {@link android.view.accessibility.CaptioningManager} for obtaining 3229 * captioning properties and listening for changes in captioning 3230 * preferences. 3231 * 3232 * @see #getSystemService 3233 * @see android.view.accessibility.CaptioningManager 3234 */ 3235 public static final String CAPTIONING_SERVICE = "captioning"; 3236 3237 /** 3238 * Use with {@link #getSystemService} to retrieve a 3239 * {@link android.app.NotificationManager} for controlling keyguard. 3240 * 3241 * @see #getSystemService 3242 * @see android.app.KeyguardManager 3243 */ 3244 public static final String KEYGUARD_SERVICE = "keyguard"; 3245 3246 /** 3247 * Use with {@link #getSystemService} to retrieve a {@link 3248 * android.location.LocationManager} for controlling location 3249 * updates. 3250 * 3251 * @see #getSystemService 3252 * @see android.location.LocationManager 3253 */ 3254 public static final String LOCATION_SERVICE = "location"; 3255 3256 /** 3257 * Use with {@link #getSystemService} to retrieve a 3258 * {@link android.location.CountryDetector} for detecting the country that 3259 * the user is in. 3260 * 3261 * @hide 3262 */ 3263 public static final String COUNTRY_DETECTOR = "country_detector"; 3264 3265 /** 3266 * Use with {@link #getSystemService} to retrieve a {@link 3267 * android.app.SearchManager} for handling searches. 3268 * 3269 * @see #getSystemService 3270 * @see android.app.SearchManager 3271 */ 3272 public static final String SEARCH_SERVICE = "search"; 3273 3274 /** 3275 * Use with {@link #getSystemService} to retrieve a {@link 3276 * android.hardware.SensorManager} for accessing sensors. 3277 * 3278 * @see #getSystemService 3279 * @see android.hardware.SensorManager 3280 */ 3281 public static final String SENSOR_SERVICE = "sensor"; 3282 3283 /** 3284 * Use with {@link #getSystemService} to retrieve a {@link 3285 * android.os.storage.StorageManager} for accessing system storage 3286 * functions. 3287 * 3288 * @see #getSystemService 3289 * @see android.os.storage.StorageManager 3290 */ 3291 public static final String STORAGE_SERVICE = "storage"; 3292 3293 /** 3294 * Use with {@link #getSystemService} to retrieve a {@link 3295 * android.app.usage.StorageStatsManager} for accessing system storage 3296 * statistics. 3297 * 3298 * @see #getSystemService 3299 * @see android.app.usage.StorageStatsManager 3300 */ 3301 public static final String STORAGE_STATS_SERVICE = "storagestats"; 3302 3303 /** 3304 * Use with {@link #getSystemService} to retrieve a 3305 * com.android.server.WallpaperService for accessing wallpapers. 3306 * 3307 * @see #getSystemService 3308 */ 3309 public static final String WALLPAPER_SERVICE = "wallpaper"; 3310 3311 /** 3312 * Use with {@link #getSystemService} to retrieve a {@link 3313 * android.os.Vibrator} for interacting with the vibration hardware. 3314 * 3315 * @see #getSystemService 3316 * @see android.os.Vibrator 3317 */ 3318 public static final String VIBRATOR_SERVICE = "vibrator"; 3319 3320 /** 3321 * Use with {@link #getSystemService} to retrieve a {@link 3322 * android.app.StatusBarManager} for interacting with the status bar. 3323 * 3324 * @see #getSystemService 3325 * @see android.app.StatusBarManager 3326 * @hide 3327 */ 3328 public static final String STATUS_BAR_SERVICE = "statusbar"; 3329 3330 /** 3331 * Use with {@link #getSystemService} to retrieve a {@link 3332 * android.net.ConnectivityManager} for handling management of 3333 * network connections. 3334 * 3335 * @see #getSystemService 3336 * @see android.net.ConnectivityManager 3337 */ 3338 public static final String CONNECTIVITY_SERVICE = "connectivity"; 3339 3340 /** 3341 * Use with {@link #getSystemService} to retrieve a 3342 * {@link android.net.IpSecManager} for encrypting Sockets or Networks with 3343 * IPSec. 3344 * 3345 * @see #getSystemService 3346 */ 3347 public static final String IPSEC_SERVICE = "ipsec"; 3348 3349 /** 3350 * Use with {@link #getSystemService} to retrieve a {@link 3351 * android.os.IUpdateLock} for managing runtime sequences that 3352 * must not be interrupted by headless OTA application or similar. 3353 * 3354 * @hide 3355 * @see #getSystemService 3356 * @see android.os.UpdateLock 3357 */ 3358 public static final String UPDATE_LOCK_SERVICE = "updatelock"; 3359 3360 /** 3361 * Constant for the internal network management service, not really a Context service. 3362 * @hide 3363 */ 3364 public static final String NETWORKMANAGEMENT_SERVICE = "network_management"; 3365 3366 /** 3367 * Use with {@link #getSystemService} to retrieve a {@link 3368 * android.app.usage.NetworkStatsManager} for querying network usage stats. 3369 * 3370 * @see #getSystemService 3371 * @see android.app.usage.NetworkStatsManager 3372 */ 3373 public static final String NETWORK_STATS_SERVICE = "netstats"; 3374 /** {@hide} */ 3375 public static final String NETWORK_POLICY_SERVICE = "netpolicy"; 3376 3377 /** 3378 * Use with {@link #getSystemService} to retrieve a {@link 3379 * android.net.wifi.WifiManager} for handling management of 3380 * Wi-Fi access. 3381 * 3382 * @see #getSystemService 3383 * @see android.net.wifi.WifiManager 3384 */ 3385 public static final String WIFI_SERVICE = "wifi"; 3386 3387 /** 3388 * Use with {@link #getSystemService} to retrieve a {@link 3389 * android.net.wifi.p2p.WifiP2pManager} for handling management of 3390 * Wi-Fi peer-to-peer connections. 3391 * 3392 * @see #getSystemService 3393 * @see android.net.wifi.p2p.WifiP2pManager 3394 */ 3395 public static final String WIFI_P2P_SERVICE = "wifip2p"; 3396 3397 /** 3398 * Use with {@link #getSystemService} to retrieve a 3399 * {@link android.net.wifi.aware.WifiAwareManager} for handling management of 3400 * Wi-Fi Aware. 3401 * 3402 * @see #getSystemService 3403 * @see android.net.wifi.aware.WifiAwareManager 3404 */ 3405 public static final String WIFI_AWARE_SERVICE = "wifiaware"; 3406 3407 /** 3408 * Use with {@link #getSystemService} to retrieve a {@link 3409 * android.net.wifi.WifiScanner} for scanning the wifi universe 3410 * 3411 * @see #getSystemService 3412 * @see android.net.wifi.WifiScanner 3413 * @hide 3414 */ 3415 @SystemApi 3416 public static final String WIFI_SCANNING_SERVICE = "wifiscanner"; 3417 3418 /** 3419 * Use with {@link #getSystemService} to retrieve a {@link 3420 * android.net.wifi.RttManager} for ranging devices with wifi 3421 * 3422 * @see #getSystemService 3423 * @see android.net.wifi.RttManager 3424 * @hide 3425 */ 3426 @SystemApi 3427 public static final String WIFI_RTT_SERVICE = "rttmanager"; 3428 3429 /** 3430 * Use with {@link #getSystemService} to retrieve a {@link 3431 * android.net.EthernetManager} for handling management of 3432 * Ethernet access. 3433 * 3434 * @see #getSystemService 3435 * @see android.net.EthernetManager 3436 * 3437 * @hide 3438 */ 3439 public static final String ETHERNET_SERVICE = "ethernet"; 3440 3441 /** 3442 * Use with {@link #getSystemService} to retrieve a {@link 3443 * android.net.nsd.NsdManager} for handling management of network service 3444 * discovery 3445 * 3446 * @see #getSystemService 3447 * @see android.net.nsd.NsdManager 3448 */ 3449 public static final String NSD_SERVICE = "servicediscovery"; 3450 3451 /** 3452 * Use with {@link #getSystemService} to retrieve a 3453 * {@link android.media.AudioManager} for handling management of volume, 3454 * ringer modes and audio routing. 3455 * 3456 * @see #getSystemService 3457 * @see android.media.AudioManager 3458 */ 3459 public static final String AUDIO_SERVICE = "audio"; 3460 3461 /** 3462 * Use with {@link #getSystemService} to retrieve a 3463 * {@link android.hardware.fingerprint.FingerprintManager} for handling management 3464 * of fingerprints. 3465 * 3466 * @see #getSystemService 3467 * @see android.hardware.fingerprint.FingerprintManager 3468 */ 3469 public static final String FINGERPRINT_SERVICE = "fingerprint"; 3470 3471 /** 3472 * Use with {@link #getSystemService} to retrieve a 3473 * {@link android.media.MediaRouter} for controlling and managing 3474 * routing of media. 3475 * 3476 * @see #getSystemService 3477 * @see android.media.MediaRouter 3478 */ 3479 public static final String MEDIA_ROUTER_SERVICE = "media_router"; 3480 3481 /** 3482 * Use with {@link #getSystemService} to retrieve a 3483 * {@link android.media.session.MediaSessionManager} for managing media Sessions. 3484 * 3485 * @see #getSystemService 3486 * @see android.media.session.MediaSessionManager 3487 */ 3488 public static final String MEDIA_SESSION_SERVICE = "media_session"; 3489 3490 /** 3491 * Use with {@link #getSystemService} to retrieve a 3492 * {@link android.telephony.TelephonyManager} for handling management the 3493 * telephony features of the device. 3494 * 3495 * @see #getSystemService 3496 * @see android.telephony.TelephonyManager 3497 */ 3498 public static final String TELEPHONY_SERVICE = "phone"; 3499 3500 /** 3501 * Use with {@link #getSystemService} to retrieve a 3502 * {@link android.telephony.SubscriptionManager} for handling management the 3503 * telephony subscriptions of the device. 3504 * 3505 * @see #getSystemService 3506 * @see android.telephony.SubscriptionManager 3507 */ 3508 public static final String TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service"; 3509 3510 /** 3511 * Use with {@link #getSystemService} to retrieve a 3512 * {@link android.telecom.TelecomManager} to manage telecom-related features 3513 * of the device. 3514 * 3515 * @see #getSystemService 3516 * @see android.telecom.TelecomManager 3517 */ 3518 public static final String TELECOM_SERVICE = "telecom"; 3519 3520 /** 3521 * Use with {@link #getSystemService} to retrieve a 3522 * {@link android.telephony.CarrierConfigManager} for reading carrier configuration values. 3523 * 3524 * @see #getSystemService 3525 * @see android.telephony.CarrierConfigManager 3526 */ 3527 public static final String CARRIER_CONFIG_SERVICE = "carrier_config"; 3528 3529 /** 3530 * Use with {@link #getSystemService} to retrieve a 3531 * {@link android.text.ClipboardManager} for accessing and modifying 3532 * {@link android.content.ClipboardManager} for accessing and modifying 3533 * the contents of the global clipboard. 3534 * 3535 * @see #getSystemService 3536 * @see android.content.ClipboardManager 3537 */ 3538 public static final String CLIPBOARD_SERVICE = "clipboard"; 3539 3540 /** 3541 * Use with {@link #getSystemService} to retrieve a 3542 * {@link TextClassificationManager} for text classification services. 3543 * 3544 * @see #getSystemService 3545 * @see TextClassificationManager 3546 */ 3547 public static final String TEXT_CLASSIFICATION_SERVICE = "textclassification"; 3548 3549 /** 3550 * Use with {@link #getSystemService} to retrieve a 3551 * {@link android.view.inputmethod.InputMethodManager} for accessing input 3552 * methods. 3553 * 3554 * @see #getSystemService 3555 */ 3556 public static final String INPUT_METHOD_SERVICE = "input_method"; 3557 3558 /** 3559 * Use with {@link #getSystemService} to retrieve a 3560 * {@link android.view.textservice.TextServicesManager} for accessing 3561 * text services. 3562 * 3563 * @see #getSystemService 3564 */ 3565 public static final String TEXT_SERVICES_MANAGER_SERVICE = "textservices"; 3566 3567 /** 3568 * Use with {@link #getSystemService} to retrieve a 3569 * {@link android.appwidget.AppWidgetManager} for accessing AppWidgets. 3570 * 3571 * @see #getSystemService 3572 */ 3573 public static final String APPWIDGET_SERVICE = "appwidget"; 3574 3575 /** 3576 * Official published name of the (internal) voice interaction manager service. 3577 * 3578 * @hide 3579 * @see #getSystemService 3580 */ 3581 public static final String VOICE_INTERACTION_MANAGER_SERVICE = "voiceinteraction"; 3582 3583 /** 3584 * Official published name of the (internal) autofill service. 3585 * 3586 * @hide 3587 * @see #getSystemService 3588 */ 3589 public static final String AUTOFILL_MANAGER_SERVICE = "autofill"; 3590 3591 /** 3592 * Use with {@link #getSystemService} to access the 3593 * {@link com.android.server.voiceinteraction.SoundTriggerService}. 3594 * 3595 * @hide 3596 * @see #getSystemService 3597 */ 3598 public static final String SOUND_TRIGGER_SERVICE = "soundtrigger"; 3599 3600 3601 /** 3602 * Use with {@link #getSystemService} to retrieve an 3603 * {@link android.app.backup.IBackupManager IBackupManager} for communicating 3604 * with the backup mechanism. 3605 * @hide 3606 * 3607 * @see #getSystemService 3608 */ 3609 @SystemApi 3610 public static final String BACKUP_SERVICE = "backup"; 3611 3612 /** 3613 * Use with {@link #getSystemService} to retrieve a 3614 * {@link android.os.DropBoxManager} instance for recording 3615 * diagnostic logs. 3616 * @see #getSystemService 3617 */ 3618 public static final String DROPBOX_SERVICE = "dropbox"; 3619 3620 /** 3621 * System service name for the DeviceIdleController. There is no Java API for this. 3622 * @see #getSystemService 3623 * @hide 3624 */ 3625 public static final String DEVICE_IDLE_CONTROLLER = "deviceidle"; 3626 3627 /** 3628 * Use with {@link #getSystemService} to retrieve a 3629 * {@link android.app.admin.DevicePolicyManager} for working with global 3630 * device policy management. 3631 * 3632 * @see #getSystemService 3633 */ 3634 public static final String DEVICE_POLICY_SERVICE = "device_policy"; 3635 3636 /** 3637 * Use with {@link #getSystemService} to retrieve a 3638 * {@link android.app.UiModeManager} for controlling UI modes. 3639 * 3640 * @see #getSystemService 3641 */ 3642 public static final String UI_MODE_SERVICE = "uimode"; 3643 3644 /** 3645 * Use with {@link #getSystemService} to retrieve a 3646 * {@link android.app.DownloadManager} for requesting HTTP downloads. 3647 * 3648 * @see #getSystemService 3649 */ 3650 public static final String DOWNLOAD_SERVICE = "download"; 3651 3652 /** 3653 * Use with {@link #getSystemService} to retrieve a 3654 * {@link android.os.BatteryManager} for managing battery state. 3655 * 3656 * @see #getSystemService 3657 */ 3658 public static final String BATTERY_SERVICE = "batterymanager"; 3659 3660 /** 3661 * Use with {@link #getSystemService} to retrieve a 3662 * {@link android.nfc.NfcManager} for using NFC. 3663 * 3664 * @see #getSystemService 3665 */ 3666 public static final String NFC_SERVICE = "nfc"; 3667 3668 /** 3669 * Use with {@link #getSystemService} to retrieve a 3670 * {@link android.bluetooth.BluetoothManager} for using Bluetooth. 3671 * 3672 * @see #getSystemService 3673 */ 3674 public static final String BLUETOOTH_SERVICE = "bluetooth"; 3675 3676 /** 3677 * Use with {@link #getSystemService} to retrieve a 3678 * {@link android.net.sip.SipManager} for accessing the SIP related service. 3679 * 3680 * @see #getSystemService 3681 */ 3682 /** @hide */ 3683 public static final String SIP_SERVICE = "sip"; 3684 3685 /** 3686 * Use with {@link #getSystemService} to retrieve a {@link 3687 * android.hardware.usb.UsbManager} for access to USB devices (as a USB host) 3688 * and for controlling this device's behavior as a USB device. 3689 * 3690 * @see #getSystemService 3691 * @see android.hardware.usb.UsbManager 3692 */ 3693 public static final String USB_SERVICE = "usb"; 3694 3695 /** 3696 * Use with {@link #getSystemService} to retrieve a {@link 3697 * android.hardware.SerialManager} for access to serial ports. 3698 * 3699 * @see #getSystemService 3700 * @see android.hardware.SerialManager 3701 * 3702 * @hide 3703 */ 3704 public static final String SERIAL_SERVICE = "serial"; 3705 3706 /** 3707 * Use with {@link #getSystemService} to retrieve a 3708 * {@link android.hardware.hdmi.HdmiControlManager} for controlling and managing 3709 * HDMI-CEC protocol. 3710 * 3711 * @see #getSystemService 3712 * @see android.hardware.hdmi.HdmiControlManager 3713 * @hide 3714 */ 3715 @SystemApi 3716 public static final String HDMI_CONTROL_SERVICE = "hdmi_control"; 3717 3718 /** 3719 * Use with {@link #getSystemService} to retrieve a 3720 * {@link android.hardware.input.InputManager} for interacting with input devices. 3721 * 3722 * @see #getSystemService 3723 * @see android.hardware.input.InputManager 3724 */ 3725 public static final String INPUT_SERVICE = "input"; 3726 3727 /** 3728 * Use with {@link #getSystemService} to retrieve a 3729 * {@link android.hardware.display.DisplayManager} for interacting with display devices. 3730 * 3731 * @see #getSystemService 3732 * @see android.hardware.display.DisplayManager 3733 */ 3734 public static final String DISPLAY_SERVICE = "display"; 3735 3736 /** 3737 * Use with {@link #getSystemService} to retrieve a 3738 * {@link android.os.UserManager} for managing users on devices that support multiple users. 3739 * 3740 * @see #getSystemService 3741 * @see android.os.UserManager 3742 */ 3743 public static final String USER_SERVICE = "user"; 3744 3745 /** 3746 * Use with {@link #getSystemService} to retrieve a 3747 * {@link android.content.pm.LauncherApps} for querying and monitoring launchable apps across 3748 * profiles of a user. 3749 * 3750 * @see #getSystemService 3751 * @see android.content.pm.LauncherApps 3752 */ 3753 public static final String LAUNCHER_APPS_SERVICE = "launcherapps"; 3754 3755 /** 3756 * Use with {@link #getSystemService} to retrieve a 3757 * {@link android.content.RestrictionsManager} for retrieving application restrictions 3758 * and requesting permissions for restricted operations. 3759 * @see #getSystemService 3760 * @see android.content.RestrictionsManager 3761 */ 3762 public static final String RESTRICTIONS_SERVICE = "restrictions"; 3763 3764 /** 3765 * Use with {@link #getSystemService} to retrieve a 3766 * {@link android.app.AppOpsManager} for tracking application operations 3767 * on the device. 3768 * 3769 * @see #getSystemService 3770 * @see android.app.AppOpsManager 3771 */ 3772 public static final String APP_OPS_SERVICE = "appops"; 3773 3774 /** 3775 * Use with {@link #getSystemService} to retrieve a 3776 * {@link android.hardware.camera2.CameraManager} for interacting with 3777 * camera devices. 3778 * 3779 * @see #getSystemService 3780 * @see android.hardware.camera2.CameraManager 3781 */ 3782 public static final String CAMERA_SERVICE = "camera"; 3783 3784 /** 3785 * {@link android.print.PrintManager} for printing and managing 3786 * printers and print tasks. 3787 * 3788 * @see #getSystemService 3789 * @see android.print.PrintManager 3790 */ 3791 public static final String PRINT_SERVICE = "print"; 3792 3793 /** 3794 * Use with {@link #getSystemService} to retrieve a 3795 * {@link android.companion.CompanionDeviceManager} for managing companion devices 3796 * 3797 * @see #getSystemService 3798 * @see android.companion.CompanionDeviceManager 3799 */ 3800 public static final String COMPANION_DEVICE_SERVICE = "companiondevice"; 3801 3802 /** 3803 * Use with {@link #getSystemService} to retrieve a 3804 * {@link android.hardware.ConsumerIrManager} for transmitting infrared 3805 * signals from the device. 3806 * 3807 * @see #getSystemService 3808 * @see android.hardware.ConsumerIrManager 3809 */ 3810 public static final String CONSUMER_IR_SERVICE = "consumer_ir"; 3811 3812 /** 3813 * {@link android.app.trust.TrustManager} for managing trust agents. 3814 * @see #getSystemService 3815 * @see android.app.trust.TrustManager 3816 * @hide 3817 */ 3818 public static final String TRUST_SERVICE = "trust"; 3819 3820 /** 3821 * Use with {@link #getSystemService} to retrieve a 3822 * {@link android.media.tv.TvInputManager} for interacting with TV inputs 3823 * on the device. 3824 * 3825 * @see #getSystemService 3826 * @see android.media.tv.TvInputManager 3827 */ 3828 public static final String TV_INPUT_SERVICE = "tv_input"; 3829 3830 /** 3831 * {@link android.net.NetworkScoreManager} for managing network scoring. 3832 * @see #getSystemService 3833 * @see android.net.NetworkScoreManager 3834 * @hide 3835 */ 3836 @SystemApi 3837 public static final String NETWORK_SCORE_SERVICE = "network_score"; 3838 3839 /** 3840 * Use with {@link #getSystemService} to retrieve a {@link 3841 * android.app.usage.UsageStatsManager} for querying device usage stats. 3842 * 3843 * @see #getSystemService 3844 * @see android.app.usage.UsageStatsManager 3845 */ 3846 public static final String USAGE_STATS_SERVICE = "usagestats"; 3847 3848 /** 3849 * Use with {@link #getSystemService} to retrieve a {@link 3850 * android.app.job.JobScheduler} instance for managing occasional 3851 * background tasks. 3852 * @see #getSystemService 3853 * @see android.app.job.JobScheduler 3854 */ 3855 public static final String JOB_SCHEDULER_SERVICE = "jobscheduler"; 3856 3857 /** 3858 * Use with {@link #getSystemService} to retrieve a {@link 3859 * android.service.persistentdata.PersistentDataBlockManager} instance 3860 * for interacting with a storage device that lives across factory resets. 3861 * 3862 * @see #getSystemService 3863 * @see android.service.persistentdata.PersistentDataBlockManager 3864 * @hide 3865 */ 3866 @SystemApi 3867 public static final String PERSISTENT_DATA_BLOCK_SERVICE = "persistent_data_block"; 3868 3869 /** 3870 * Use with {@link #getSystemService} to retrieve a {@link 3871 * android.service.oemlock.OemLockManager} instance for managing the OEM lock. 3872 * 3873 * @see #getSystemService 3874 * @see android.service.oemlock.OemLockManager 3875 * @hide 3876 */ 3877 @SystemApi 3878 public static final String OEM_LOCK_SERVICE = "oem_lock"; 3879 3880 /** 3881 * Use with {@link #getSystemService} to retrieve a {@link 3882 * android.media.projection.MediaProjectionManager} instance for managing 3883 * media projection sessions. 3884 * @see #getSystemService 3885 * @see android.media.projection.MediaProjectionManager 3886 */ 3887 public static final String MEDIA_PROJECTION_SERVICE = "media_projection"; 3888 3889 /** 3890 * Use with {@link #getSystemService} to retrieve a 3891 * {@link android.media.midi.MidiManager} for accessing the MIDI service. 3892 * 3893 * @see #getSystemService 3894 */ 3895 public static final String MIDI_SERVICE = "midi"; 3896 3897 3898 /** 3899 * Use with {@link #getSystemService} to retrieve a 3900 * {@link android.hardware.radio.RadioManager} for accessing the broadcast radio service. 3901 * 3902 * @see #getSystemService 3903 * @hide 3904 */ 3905 public static final String RADIO_SERVICE = "radio"; 3906 3907 /** 3908 * Use with {@link #getSystemService} to retrieve a 3909 * {@link android.os.HardwarePropertiesManager} for accessing the hardware properties service. 3910 * 3911 * @see #getSystemService 3912 */ 3913 public static final String HARDWARE_PROPERTIES_SERVICE = "hardware_properties"; 3914 3915 /** 3916 * Use with {@link #getSystemService} to retrieve a 3917 * {@link android.content.pm.ShortcutManager} for accessing the launcher shortcut service. 3918 * 3919 * @see #getSystemService 3920 * @see android.content.pm.ShortcutManager 3921 */ 3922 public static final String SHORTCUT_SERVICE = "shortcut"; 3923 3924 /** 3925 * Use with {@link #getSystemService} to retrieve a {@link 3926 * android.hardware.location.ContextHubManager} for accessing context hubs. 3927 * 3928 * @see #getSystemService 3929 * @see android.hardware.location.ContextHubManager 3930 * 3931 * @hide 3932 */ 3933 @SystemApi 3934 public static final String CONTEXTHUB_SERVICE = "contexthub"; 3935 3936 /** 3937 * Use with {@link #getSystemService} to retrieve a 3938 * {@link android.os.health.SystemHealthManager} for accessing system health (battery, power, 3939 * memory, etc) metrics. 3940 * 3941 * @see #getSystemService 3942 */ 3943 public static final String SYSTEM_HEALTH_SERVICE = "systemhealth"; 3944 3945 /** 3946 * Gatekeeper Service. 3947 * @hide 3948 */ 3949 public static final String GATEKEEPER_SERVICE = "android.service.gatekeeper.IGateKeeperService"; 3950 3951 /** 3952 * Service defining the policy for access to device identifiers. 3953 * @hide 3954 */ 3955 public static final String DEVICE_IDENTIFIERS_SERVICE = "device_identifiers"; 3956 3957 /** 3958 * Service to report a system health "incident" 3959 * @hide 3960 */ 3961 public static final String INCIDENT_SERVICE = "incident"; 3962 3963 /** 3964 * Use with {@link #getSystemService} to retrieve a {@link 3965 * android.content.om.OverlayManager} for managing overlay packages. 3966 * 3967 * @see #getSystemService 3968 * @see android.content.om.OverlayManager 3969 * @hide 3970 */ 3971 public static final String OVERLAY_SERVICE = "overlay"; 3972 3973 /** 3974 * Use with {@link #getSystemService} to retrieve a 3975 * {@link VrManager} for accessing the VR service. 3976 * 3977 * @see #getSystemService 3978 * @hide 3979 */ 3980 @SystemApi 3981 public static final String VR_SERVICE = "vrmanager"; 3982 3983 /** 3984 * Use with {@link #getSystemService} to retrieve an 3985 * {@link android.app.timezone.ITimeZoneRulesManager}. 3986 * @hide 3987 * 3988 * @see #getSystemService 3989 */ 3990 public static final String TIME_ZONE_RULES_MANAGER_SERVICE = "timezone"; 3991 3992 /** 3993 * Determine whether the given permission is allowed for a particular 3994 * process and user ID running in the system. 3995 * 3996 * @param permission The name of the permission being checked. 3997 * @param pid The process ID being checked against. Must be > 0. 3998 * @param uid The user ID being checked against. A uid of 0 is the root 3999 * user, which will pass every permission check. 4000 * 4001 * @return {@link PackageManager#PERMISSION_GRANTED} if the given 4002 * pid/uid is allowed that permission, or 4003 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4004 * 4005 * @see PackageManager#checkPermission(String, String) 4006 * @see #checkCallingPermission 4007 */ 4008 @CheckResult(suggest="#enforcePermission(String,int,int,String)") 4009 @PackageManager.PermissionResult 4010 public abstract int checkPermission(@NonNull String permission, int pid, int uid); 4011 4012 /** @hide */ 4013 @PackageManager.PermissionResult 4014 public abstract int checkPermission(@NonNull String permission, int pid, int uid, 4015 IBinder callerToken); 4016 4017 /** 4018 * Determine whether the calling process of an IPC you are handling has been 4019 * granted a particular permission. This is basically the same as calling 4020 * {@link #checkPermission(String, int, int)} with the pid and uid returned 4021 * by {@link android.os.Binder#getCallingPid} and 4022 * {@link android.os.Binder#getCallingUid}. One important difference 4023 * is that if you are not currently processing an IPC, this function 4024 * will always fail. This is done to protect against accidentally 4025 * leaking permissions; you can use {@link #checkCallingOrSelfPermission} 4026 * to avoid this protection. 4027 * 4028 * @param permission The name of the permission being checked. 4029 * 4030 * @return {@link PackageManager#PERMISSION_GRANTED} if the calling 4031 * pid/uid is allowed that permission, or 4032 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4033 * 4034 * @see PackageManager#checkPermission(String, String) 4035 * @see #checkPermission 4036 * @see #checkCallingOrSelfPermission 4037 */ 4038 @CheckResult(suggest="#enforceCallingPermission(String,String)") 4039 @PackageManager.PermissionResult 4040 public abstract int checkCallingPermission(@NonNull String permission); 4041 4042 /** 4043 * Determine whether the calling process of an IPC <em>or you</em> have been 4044 * granted a particular permission. This is the same as 4045 * {@link #checkCallingPermission}, except it grants your own permissions 4046 * if you are not currently processing an IPC. Use with care! 4047 * 4048 * @param permission The name of the permission being checked. 4049 * 4050 * @return {@link PackageManager#PERMISSION_GRANTED} if the calling 4051 * pid/uid is allowed that permission, or 4052 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4053 * 4054 * @see PackageManager#checkPermission(String, String) 4055 * @see #checkPermission 4056 * @see #checkCallingPermission 4057 */ 4058 @CheckResult(suggest="#enforceCallingOrSelfPermission(String,String)") 4059 @PackageManager.PermissionResult 4060 public abstract int checkCallingOrSelfPermission(@NonNull String permission); 4061 4062 /** 4063 * Determine whether <em>you</em> have been granted a particular permission. 4064 * 4065 * @param permission The name of the permission being checked. 4066 * 4067 * @return {@link PackageManager#PERMISSION_GRANTED} if you have the 4068 * permission, or {@link PackageManager#PERMISSION_DENIED} if not. 4069 * 4070 * @see PackageManager#checkPermission(String, String) 4071 * @see #checkCallingPermission(String) 4072 */ 4073 @PackageManager.PermissionResult 4074 public abstract int checkSelfPermission(@NonNull String permission); 4075 4076 /** 4077 * If the given permission is not allowed for a particular process 4078 * and user ID running in the system, throw a {@link SecurityException}. 4079 * 4080 * @param permission The name of the permission being checked. 4081 * @param pid The process ID being checked against. Must be > 0. 4082 * @param uid The user ID being checked against. A uid of 0 is the root 4083 * user, which will pass every permission check. 4084 * @param message A message to include in the exception if it is thrown. 4085 * 4086 * @see #checkPermission(String, int, int) 4087 */ 4088 public abstract void enforcePermission( 4089 @NonNull String permission, int pid, int uid, @Nullable String message); 4090 4091 /** 4092 * If the calling process of an IPC you are handling has not been 4093 * granted a particular permission, throw a {@link 4094 * SecurityException}. This is basically the same as calling 4095 * {@link #enforcePermission(String, int, int, String)} with the 4096 * pid and uid returned by {@link android.os.Binder#getCallingPid} 4097 * and {@link android.os.Binder#getCallingUid}. One important 4098 * difference is that if you are not currently processing an IPC, 4099 * this function will always throw the SecurityException. This is 4100 * done to protect against accidentally leaking permissions; you 4101 * can use {@link #enforceCallingOrSelfPermission} to avoid this 4102 * protection. 4103 * 4104 * @param permission The name of the permission being checked. 4105 * @param message A message to include in the exception if it is thrown. 4106 * 4107 * @see #checkCallingPermission(String) 4108 */ 4109 public abstract void enforceCallingPermission( 4110 @NonNull String permission, @Nullable String message); 4111 4112 /** 4113 * If neither you nor the calling process of an IPC you are 4114 * handling has been granted a particular permission, throw a 4115 * {@link SecurityException}. This is the same as {@link 4116 * #enforceCallingPermission}, except it grants your own 4117 * permissions if you are not currently processing an IPC. Use 4118 * with care! 4119 * 4120 * @param permission The name of the permission being checked. 4121 * @param message A message to include in the exception if it is thrown. 4122 * 4123 * @see #checkCallingOrSelfPermission(String) 4124 */ 4125 public abstract void enforceCallingOrSelfPermission( 4126 @NonNull String permission, @Nullable String message); 4127 4128 /** 4129 * Grant permission to access a specific Uri to another package, regardless 4130 * of whether that package has general permission to access the Uri's 4131 * content provider. This can be used to grant specific, temporary 4132 * permissions, typically in response to user interaction (such as the 4133 * user opening an attachment that you would like someone else to 4134 * display). 4135 * 4136 * <p>Normally you should use {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 4137 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 4138 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 4139 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} with the Intent being used to 4140 * start an activity instead of this function directly. If you use this 4141 * function directly, you should be sure to call 4142 * {@link #revokeUriPermission} when the target should no longer be allowed 4143 * to access it. 4144 * 4145 * <p>To succeed, the content provider owning the Uri must have set the 4146 * {@link android.R.styleable#AndroidManifestProvider_grantUriPermissions 4147 * grantUriPermissions} attribute in its manifest or included the 4148 * {@link android.R.styleable#AndroidManifestGrantUriPermission 4149 * <grant-uri-permissions>} tag. 4150 * 4151 * @param toPackage The package you would like to allow to access the Uri. 4152 * @param uri The Uri you would like to grant access to. 4153 * @param modeFlags The desired access modes. 4154 * 4155 * @see #revokeUriPermission 4156 */ 4157 public abstract void grantUriPermission(String toPackage, Uri uri, 4158 @Intent.GrantUriMode int modeFlags); 4159 4160 /** 4161 * Remove all permissions to access a particular content provider Uri 4162 * that were previously added with {@link #grantUriPermission} or <em>any other</em> mechanism. 4163 * The given Uri will match all previously granted Uris that are the same or a 4164 * sub-path of the given Uri. That is, revoking "content://foo/target" will 4165 * revoke both "content://foo/target" and "content://foo/target/sub", but not 4166 * "content://foo". It will not remove any prefix grants that exist at a 4167 * higher level. 4168 * 4169 * <p>Prior to {@link android.os.Build.VERSION_CODES#LOLLIPOP}, if you did not have 4170 * regular permission access to a Uri, but had received access to it through 4171 * a specific Uri permission grant, you could not revoke that grant with this 4172 * function and a {@link SecurityException} would be thrown. As of 4173 * {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this function will not throw a security 4174 * exception, but will remove whatever permission grants to the Uri had been given to the app 4175 * (or none).</p> 4176 * 4177 * <p>Unlike {@link #revokeUriPermission(String, Uri, int)}, this method impacts all permission 4178 * grants matching the given Uri, for any package they had been granted to, through any 4179 * mechanism this had happened (such as indirectly through the clipboard, activity launch, 4180 * service start, etc). That means this can be potentially dangerous to use, as it can 4181 * revoke grants that another app could be strongly expecting to stick around.</p> 4182 * 4183 * @param uri The Uri you would like to revoke access to. 4184 * @param modeFlags The access modes to revoke. 4185 * 4186 * @see #grantUriPermission 4187 */ 4188 public abstract void revokeUriPermission(Uri uri, @Intent.AccessUriMode int modeFlags); 4189 4190 /** 4191 * Remove permissions to access a particular content provider Uri 4192 * that were previously added with {@link #grantUriPermission} for a specific target 4193 * package. The given Uri will match all previously granted Uris that are the same or a 4194 * sub-path of the given Uri. That is, revoking "content://foo/target" will 4195 * revoke both "content://foo/target" and "content://foo/target/sub", but not 4196 * "content://foo". It will not remove any prefix grants that exist at a 4197 * higher level. 4198 * 4199 * <p>Unlike {@link #revokeUriPermission(Uri, int)}, this method will <em>only</em> 4200 * revoke permissions that had been explicitly granted through {@link #grantUriPermission} 4201 * and only for the package specified. Any matching grants that have happened through 4202 * other mechanisms (clipboard, activity launching, service starting, etc) will not be 4203 * removed.</p> 4204 * 4205 * @param toPackage The package you had previously granted access to. 4206 * @param uri The Uri you would like to revoke access to. 4207 * @param modeFlags The access modes to revoke. 4208 * 4209 * @see #grantUriPermission 4210 */ 4211 public abstract void revokeUriPermission(String toPackage, Uri uri, 4212 @Intent.AccessUriMode int modeFlags); 4213 4214 /** 4215 * Determine whether a particular process and user ID has been granted 4216 * permission to access a specific URI. This only checks for permissions 4217 * that have been explicitly granted -- if the given process/uid has 4218 * more general access to the URI's content provider then this check will 4219 * always fail. 4220 * 4221 * @param uri The uri that is being checked. 4222 * @param pid The process ID being checked against. Must be > 0. 4223 * @param uid The user ID being checked against. A uid of 0 is the root 4224 * user, which will pass every permission check. 4225 * @param modeFlags The access modes to check. 4226 * 4227 * @return {@link PackageManager#PERMISSION_GRANTED} if the given 4228 * pid/uid is allowed to access that uri, or 4229 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4230 * 4231 * @see #checkCallingUriPermission 4232 */ 4233 @CheckResult(suggest="#enforceUriPermission(Uri,int,int,String)") 4234 @PackageManager.PermissionResult 4235 public abstract int checkUriPermission(Uri uri, int pid, int uid, 4236 @Intent.AccessUriMode int modeFlags); 4237 4238 /** @hide */ 4239 @PackageManager.PermissionResult 4240 public abstract int checkUriPermission(Uri uri, int pid, int uid, 4241 @Intent.AccessUriMode int modeFlags, IBinder callerToken); 4242 4243 /** 4244 * Determine whether the calling process and user ID has been 4245 * granted permission to access a specific URI. This is basically 4246 * the same as calling {@link #checkUriPermission(Uri, int, int, 4247 * int)} with the pid and uid returned by {@link 4248 * android.os.Binder#getCallingPid} and {@link 4249 * android.os.Binder#getCallingUid}. One important difference is 4250 * that if you are not currently processing an IPC, this function 4251 * will always fail. 4252 * 4253 * @param uri The uri that is being checked. 4254 * @param modeFlags The access modes to check. 4255 * 4256 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 4257 * is allowed to access that uri, or 4258 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4259 * 4260 * @see #checkUriPermission(Uri, int, int, int) 4261 */ 4262 @CheckResult(suggest="#enforceCallingUriPermission(Uri,int,String)") 4263 @PackageManager.PermissionResult 4264 public abstract int checkCallingUriPermission(Uri uri, @Intent.AccessUriMode int modeFlags); 4265 4266 /** 4267 * Determine whether the calling process of an IPC <em>or you</em> has been granted 4268 * permission to access a specific URI. This is the same as 4269 * {@link #checkCallingUriPermission}, except it grants your own permissions 4270 * if you are not currently processing an IPC. Use with care! 4271 * 4272 * @param uri The uri that is being checked. 4273 * @param modeFlags The access modes to check. 4274 * 4275 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 4276 * is allowed to access that uri, or 4277 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4278 * 4279 * @see #checkCallingUriPermission 4280 */ 4281 @CheckResult(suggest="#enforceCallingOrSelfUriPermission(Uri,int,String)") 4282 @PackageManager.PermissionResult 4283 public abstract int checkCallingOrSelfUriPermission(Uri uri, 4284 @Intent.AccessUriMode int modeFlags); 4285 4286 /** 4287 * Check both a Uri and normal permission. This allows you to perform 4288 * both {@link #checkPermission} and {@link #checkUriPermission} in one 4289 * call. 4290 * 4291 * @param uri The Uri whose permission is to be checked, or null to not 4292 * do this check. 4293 * @param readPermission The permission that provides overall read access, 4294 * or null to not do this check. 4295 * @param writePermission The permission that provides overall write 4296 * access, or null to not do this check. 4297 * @param pid The process ID being checked against. Must be > 0. 4298 * @param uid The user ID being checked against. A uid of 0 is the root 4299 * user, which will pass every permission check. 4300 * @param modeFlags The access modes to check. 4301 * 4302 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 4303 * is allowed to access that uri or holds one of the given permissions, or 4304 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4305 */ 4306 @CheckResult(suggest="#enforceUriPermission(Uri,String,String,int,int,int,String)") 4307 @PackageManager.PermissionResult 4308 public abstract int checkUriPermission(@Nullable Uri uri, @Nullable String readPermission, 4309 @Nullable String writePermission, int pid, int uid, 4310 @Intent.AccessUriMode int modeFlags); 4311 4312 /** 4313 * If a particular process and user ID has not been granted 4314 * permission to access a specific URI, throw {@link 4315 * SecurityException}. This only checks for permissions that have 4316 * been explicitly granted -- if the given process/uid has more 4317 * general access to the URI's content provider then this check 4318 * will always fail. 4319 * 4320 * @param uri The uri that is being checked. 4321 * @param pid The process ID being checked against. Must be > 0. 4322 * @param uid The user ID being checked against. A uid of 0 is the root 4323 * user, which will pass every permission check. 4324 * @param modeFlags The access modes to enforce. 4325 * @param message A message to include in the exception if it is thrown. 4326 * 4327 * @see #checkUriPermission(Uri, int, int, int) 4328 */ 4329 public abstract void enforceUriPermission( 4330 Uri uri, int pid, int uid, @Intent.AccessUriMode int modeFlags, String message); 4331 4332 /** 4333 * If the calling process and user ID has not been granted 4334 * permission to access a specific URI, throw {@link 4335 * SecurityException}. This is basically the same as calling 4336 * {@link #enforceUriPermission(Uri, int, int, int, String)} with 4337 * the pid and uid returned by {@link 4338 * android.os.Binder#getCallingPid} and {@link 4339 * android.os.Binder#getCallingUid}. One important difference is 4340 * that if you are not currently processing an IPC, this function 4341 * will always throw a SecurityException. 4342 * 4343 * @param uri The uri that is being checked. 4344 * @param modeFlags The access modes to enforce. 4345 * @param message A message to include in the exception if it is thrown. 4346 * 4347 * @see #checkCallingUriPermission(Uri, int) 4348 */ 4349 public abstract void enforceCallingUriPermission( 4350 Uri uri, @Intent.AccessUriMode int modeFlags, String message); 4351 4352 /** 4353 * If the calling process of an IPC <em>or you</em> has not been 4354 * granted permission to access a specific URI, throw {@link 4355 * SecurityException}. This is the same as {@link 4356 * #enforceCallingUriPermission}, except it grants your own 4357 * permissions if you are not currently processing an IPC. Use 4358 * with care! 4359 * 4360 * @param uri The uri that is being checked. 4361 * @param modeFlags The access modes to enforce. 4362 * @param message A message to include in the exception if it is thrown. 4363 * 4364 * @see #checkCallingOrSelfUriPermission(Uri, int) 4365 */ 4366 public abstract void enforceCallingOrSelfUriPermission( 4367 Uri uri, @Intent.AccessUriMode int modeFlags, String message); 4368 4369 /** 4370 * Enforce both a Uri and normal permission. This allows you to perform 4371 * both {@link #enforcePermission} and {@link #enforceUriPermission} in one 4372 * call. 4373 * 4374 * @param uri The Uri whose permission is to be checked, or null to not 4375 * do this check. 4376 * @param readPermission The permission that provides overall read access, 4377 * or null to not do this check. 4378 * @param writePermission The permission that provides overall write 4379 * access, or null to not do this check. 4380 * @param pid The process ID being checked against. Must be > 0. 4381 * @param uid The user ID being checked against. A uid of 0 is the root 4382 * user, which will pass every permission check. 4383 * @param modeFlags The access modes to enforce. 4384 * @param message A message to include in the exception if it is thrown. 4385 * 4386 * @see #checkUriPermission(Uri, String, String, int, int, int) 4387 */ 4388 public abstract void enforceUriPermission( 4389 @Nullable Uri uri, @Nullable String readPermission, 4390 @Nullable String writePermission, int pid, int uid, @Intent.AccessUriMode int modeFlags, 4391 @Nullable String message); 4392 4393 /** @hide */ 4394 @IntDef(flag = true, prefix = { "CONTEXT_" }, value = { 4395 CONTEXT_INCLUDE_CODE, 4396 CONTEXT_IGNORE_SECURITY, 4397 CONTEXT_RESTRICTED, 4398 CONTEXT_DEVICE_PROTECTED_STORAGE, 4399 CONTEXT_CREDENTIAL_PROTECTED_STORAGE, 4400 CONTEXT_REGISTER_PACKAGE, 4401 }) 4402 @Retention(RetentionPolicy.SOURCE) 4403 public @interface CreatePackageOptions {} 4404 4405 /** 4406 * Flag for use with {@link #createPackageContext}: include the application 4407 * code with the context. This means loading code into the caller's 4408 * process, so that {@link #getClassLoader()} can be used to instantiate 4409 * the application's classes. Setting this flags imposes security 4410 * restrictions on what application context you can access; if the 4411 * requested application can not be safely loaded into your process, 4412 * java.lang.SecurityException will be thrown. If this flag is not set, 4413 * there will be no restrictions on the packages that can be loaded, 4414 * but {@link #getClassLoader} will always return the default system 4415 * class loader. 4416 */ 4417 public static final int CONTEXT_INCLUDE_CODE = 0x00000001; 4418 4419 /** 4420 * Flag for use with {@link #createPackageContext}: ignore any security 4421 * restrictions on the Context being requested, allowing it to always 4422 * be loaded. For use with {@link #CONTEXT_INCLUDE_CODE} to allow code 4423 * to be loaded into a process even when it isn't safe to do so. Use 4424 * with extreme care! 4425 */ 4426 public static final int CONTEXT_IGNORE_SECURITY = 0x00000002; 4427 4428 /** 4429 * Flag for use with {@link #createPackageContext}: a restricted context may 4430 * disable specific features. For instance, a View associated with a restricted 4431 * context would ignore particular XML attributes. 4432 */ 4433 public static final int CONTEXT_RESTRICTED = 0x00000004; 4434 4435 /** 4436 * Flag for use with {@link #createPackageContext}: point all file APIs at 4437 * device-protected storage. 4438 * 4439 * @hide 4440 */ 4441 public static final int CONTEXT_DEVICE_PROTECTED_STORAGE = 0x00000008; 4442 4443 /** 4444 * Flag for use with {@link #createPackageContext}: point all file APIs at 4445 * credential-protected storage. 4446 * 4447 * @hide 4448 */ 4449 public static final int CONTEXT_CREDENTIAL_PROTECTED_STORAGE = 0x00000010; 4450 4451 /** 4452 * @hide Used to indicate we should tell the activity manager about the process 4453 * loading this code. 4454 */ 4455 public static final int CONTEXT_REGISTER_PACKAGE = 0x40000000; 4456 4457 /** 4458 * Return a new Context object for the given application name. This 4459 * Context is the same as what the named application gets when it is 4460 * launched, containing the same resources and class loader. Each call to 4461 * this method returns a new instance of a Context object; Context objects 4462 * are not shared, however they share common state (Resources, ClassLoader, 4463 * etc) so the Context instance itself is fairly lightweight. 4464 * 4465 * <p>Throws {@link android.content.pm.PackageManager.NameNotFoundException} if there is no 4466 * application with the given package name. 4467 * 4468 * <p>Throws {@link java.lang.SecurityException} if the Context requested 4469 * can not be loaded into the caller's process for security reasons (see 4470 * {@link #CONTEXT_INCLUDE_CODE} for more information}. 4471 * 4472 * @param packageName Name of the application's package. 4473 * @param flags Option flags. 4474 * 4475 * @return A {@link Context} for the application. 4476 * 4477 * @throws SecurityException 4478 * @throws PackageManager.NameNotFoundException if there is no application with 4479 * the given package name. 4480 */ 4481 public abstract Context createPackageContext(String packageName, 4482 @CreatePackageOptions int flags) throws PackageManager.NameNotFoundException; 4483 4484 /** 4485 * Similar to {@link #createPackageContext(String, int)}, but with a 4486 * different {@link UserHandle}. For example, {@link #getContentResolver()} 4487 * will open any {@link Uri} as the given user. 4488 * 4489 * @hide 4490 */ 4491 public abstract Context createPackageContextAsUser( 4492 String packageName, @CreatePackageOptions int flags, UserHandle user) 4493 throws PackageManager.NameNotFoundException; 4494 4495 /** 4496 * Creates a context given an {@link android.content.pm.ApplicationInfo}. 4497 * 4498 * @hide 4499 */ 4500 public abstract Context createApplicationContext(ApplicationInfo application, 4501 @CreatePackageOptions int flags) throws PackageManager.NameNotFoundException; 4502 4503 /** 4504 * Return a new Context object for the given split name. The new Context has a ClassLoader and 4505 * Resources object that can access the split's and all of its dependencies' code/resources. 4506 * Each call to this method returns a new instance of a Context object; 4507 * Context objects are not shared, however common state (ClassLoader, other Resources for 4508 * the same split) may be so the Context itself can be fairly lightweight. 4509 * 4510 * @param splitName The name of the split to include, as declared in the split's 4511 * <code>AndroidManifest.xml</code>. 4512 * @return A {@link Context} with the given split's code and/or resources loaded. 4513 */ 4514 public abstract Context createContextForSplit(String splitName) 4515 throws PackageManager.NameNotFoundException; 4516 4517 /** 4518 * Get the userId associated with this context 4519 * @return user id 4520 * 4521 * @hide 4522 */ 4523 @TestApi 4524 public abstract @UserIdInt int getUserId(); 4525 4526 /** 4527 * Return a new Context object for the current Context but whose resources 4528 * are adjusted to match the given Configuration. Each call to this method 4529 * returns a new instance of a Context object; Context objects are not 4530 * shared, however common state (ClassLoader, other Resources for the 4531 * same configuration) may be so the Context itself can be fairly lightweight. 4532 * 4533 * @param overrideConfiguration A {@link Configuration} specifying what 4534 * values to modify in the base Configuration of the original Context's 4535 * resources. If the base configuration changes (such as due to an 4536 * orientation change), the resources of this context will also change except 4537 * for those that have been explicitly overridden with a value here. 4538 * 4539 * @return A {@link Context} with the given configuration override. 4540 */ 4541 public abstract Context createConfigurationContext( 4542 @NonNull Configuration overrideConfiguration); 4543 4544 /** 4545 * Return a new Context object for the current Context but whose resources 4546 * are adjusted to match the metrics of the given Display. Each call to this method 4547 * returns a new instance of a Context object; Context objects are not 4548 * shared, however common state (ClassLoader, other Resources for the 4549 * same configuration) may be so the Context itself can be fairly lightweight. 4550 * 4551 * The returned display Context provides a {@link WindowManager} 4552 * (see {@link #getSystemService(String)}) that is configured to show windows 4553 * on the given display. The WindowManager's {@link WindowManager#getDefaultDisplay} 4554 * method can be used to retrieve the Display from the returned Context. 4555 * 4556 * @param display A {@link Display} object specifying the display 4557 * for whose metrics the Context's resources should be tailored and upon which 4558 * new windows should be shown. 4559 * 4560 * @return A {@link Context} for the display. 4561 */ 4562 public abstract Context createDisplayContext(@NonNull Display display); 4563 4564 /** 4565 * Return a new Context object for the current Context but whose storage 4566 * APIs are backed by device-protected storage. 4567 * <p> 4568 * On devices with direct boot, data stored in this location is encrypted 4569 * with a key tied to the physical device, and it can be accessed 4570 * immediately after the device has booted successfully, both 4571 * <em>before and after</em> the user has authenticated with their 4572 * credentials (such as a lock pattern or PIN). 4573 * <p> 4574 * Because device-protected data is available without user authentication, 4575 * you should carefully limit the data you store using this Context. For 4576 * example, storing sensitive authentication tokens or passwords in the 4577 * device-protected area is strongly discouraged. 4578 * <p> 4579 * If the underlying device does not have the ability to store 4580 * device-protected and credential-protected data using different keys, then 4581 * both storage areas will become available at the same time. They remain as 4582 * two distinct storage locations on disk, and only the window of 4583 * availability changes. 4584 * <p> 4585 * Each call to this method returns a new instance of a Context object; 4586 * Context objects are not shared, however common state (ClassLoader, other 4587 * Resources for the same configuration) may be so the Context itself can be 4588 * fairly lightweight. 4589 * 4590 * @see #isDeviceProtectedStorage() 4591 */ 4592 public abstract Context createDeviceProtectedStorageContext(); 4593 4594 /** 4595 * Return a new Context object for the current Context but whose storage 4596 * APIs are backed by credential-protected storage. This is the default 4597 * storage area for apps unless 4598 * {@link android.R.attr#defaultToDeviceProtectedStorage} was requested. 4599 * <p> 4600 * On devices with direct boot, data stored in this location is encrypted 4601 * with a key tied to user credentials, which can be accessed 4602 * <em>only after</em> the user has entered their credentials (such as a 4603 * lock pattern or PIN). 4604 * <p> 4605 * If the underlying device does not have the ability to store 4606 * device-protected and credential-protected data using different keys, then 4607 * both storage areas will become available at the same time. They remain as 4608 * two distinct storage locations on disk, and only the window of 4609 * availability changes. 4610 * <p> 4611 * Each call to this method returns a new instance of a Context object; 4612 * Context objects are not shared, however common state (ClassLoader, other 4613 * Resources for the same configuration) may be so the Context itself can be 4614 * fairly lightweight. 4615 * 4616 * @see #isCredentialProtectedStorage() 4617 * @hide 4618 */ 4619 @SystemApi 4620 public abstract Context createCredentialProtectedStorageContext(); 4621 4622 /** 4623 * Gets the display adjustments holder for this context. This information 4624 * is provided on a per-application or activity basis and is used to simulate lower density 4625 * display metrics for legacy applications and restricted screen sizes. 4626 * 4627 * @param displayId The display id for which to get compatibility info. 4628 * @return The compatibility info holder, or null if not required by the application. 4629 * @hide 4630 */ 4631 public abstract DisplayAdjustments getDisplayAdjustments(int displayId); 4632 4633 /** 4634 * @hide 4635 */ 4636 public abstract Display getDisplay(); 4637 4638 /** 4639 * @hide 4640 */ 4641 public abstract void updateDisplay(int displayId); 4642 4643 /** 4644 * Indicates whether this Context is restricted. 4645 * 4646 * @return {@code true} if this Context is restricted, {@code false} otherwise. 4647 * 4648 * @see #CONTEXT_RESTRICTED 4649 */ 4650 public boolean isRestricted() { 4651 return false; 4652 } 4653 4654 /** 4655 * Indicates if the storage APIs of this Context are backed by 4656 * device-protected storage. 4657 * 4658 * @see #createDeviceProtectedStorageContext() 4659 */ 4660 public abstract boolean isDeviceProtectedStorage(); 4661 4662 /** 4663 * Indicates if the storage APIs of this Context are backed by 4664 * credential-protected storage. 4665 * 4666 * @see #createCredentialProtectedStorageContext() 4667 * @hide 4668 */ 4669 @SystemApi 4670 public abstract boolean isCredentialProtectedStorage(); 4671 4672 /** 4673 * Returns true if the context can load unsafe resources, e.g. fonts. 4674 * @hide 4675 */ 4676 public abstract boolean canLoadUnsafeResources(); 4677 4678 /** 4679 * @hide 4680 */ 4681 public IBinder getActivityToken() { 4682 throw new RuntimeException("Not implemented. Must override in a subclass."); 4683 } 4684 4685 /** 4686 * @hide 4687 */ 4688 @Nullable 4689 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler, 4690 int flags) { 4691 throw new RuntimeException("Not implemented. Must override in a subclass."); 4692 } 4693 4694 /** 4695 * @hide 4696 */ 4697 public IApplicationThread getIApplicationThread() { 4698 throw new RuntimeException("Not implemented. Must override in a subclass."); 4699 } 4700 4701 /** 4702 * @hide 4703 */ 4704 public Handler getMainThreadHandler() { 4705 throw new RuntimeException("Not implemented. Must override in a subclass."); 4706 } 4707 4708 /** 4709 * Throws an exception if the Context is using system resources, 4710 * which are non-runtime-overlay-themable and may show inconsistent UI. 4711 * @hide 4712 */ 4713 public void assertRuntimeOverlayThemable() { 4714 // Resources.getSystem() is a singleton and the only Resources not managed by 4715 // ResourcesManager; therefore Resources.getSystem() is not themable. 4716 if (getResources() == Resources.getSystem()) { 4717 throw new IllegalArgumentException("Non-UI context used to display UI; " 4718 + "get a UI context from ActivityThread#getSystemUiContext()"); 4719 } 4720 } 4721} 4722