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 17 package android.content; 18 19 import android.annotation.AttrRes; 20 import android.annotation.CheckResult; 21 import android.annotation.ColorInt; 22 import android.annotation.ColorRes; 23 import android.annotation.DrawableRes; 24 import android.annotation.IntDef; 25 import android.annotation.NonNull; 26 import android.annotation.Nullable; 27 import android.annotation.RequiresPermission; 28 import android.annotation.StringDef; 29 import android.annotation.StringRes; 30 import android.annotation.StyleRes; 31 import android.annotation.StyleableRes; 32 import android.annotation.SystemApi; 33 import android.annotation.TestApi; 34 import android.annotation.UserIdInt; 35 import android.app.IApplicationThread; 36 import android.app.IServiceConnection; 37 import android.app.Notification; 38 import android.app.VrManager; 39 import android.content.pm.ApplicationInfo; 40 import android.content.pm.PackageManager; 41 import android.content.res.AssetManager; 42 import android.content.res.ColorStateList; 43 import android.content.res.Configuration; 44 import android.content.res.Resources; 45 import android.content.res.TypedArray; 46 import android.database.DatabaseErrorHandler; 47 import android.database.sqlite.SQLiteDatabase; 48 import android.database.sqlite.SQLiteDatabase.CursorFactory; 49 import android.graphics.Bitmap; 50 import android.graphics.drawable.Drawable; 51 import android.net.Uri; 52 import android.os.Bundle; 53 import android.os.Environment; 54 import android.os.Handler; 55 import android.os.IBinder; 56 import android.os.Looper; 57 import android.os.StatFs; 58 import android.os.UserHandle; 59 import android.os.UserManager; 60 import android.os.storage.StorageManager; 61 import android.provider.MediaStore; 62 import android.util.AttributeSet; 63 import android.view.Display; 64 import android.view.DisplayAdjustments; 65 import android.view.View; 66 import android.view.ViewDebug; 67 import android.view.WindowManager; 68 import android.view.textclassifier.TextClassificationManager; 69 70 import java.io.File; 71 import java.io.FileInputStream; 72 import java.io.FileNotFoundException; 73 import java.io.FileOutputStream; 74 import java.io.IOException; 75 import java.io.InputStream; 76 import java.lang.annotation.Retention; 77 import 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 */ 87 public 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 */ getAssets()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 */ getResources()443 public abstract Resources getResources(); 444 445 /** Return PackageManager instance to find global package information. */ getPackageManager()446 public abstract PackageManager getPackageManager(); 447 448 /** Return a ContentResolver instance for your application's package. */ getContentResolver()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 */ getMainLooper()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 */ getApplicationContext()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 */ getNextAutofillId()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 */ registerComponentCallbacks(ComponentCallbacks callback)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 */ unregisterComponentCallbacks(ComponentCallbacks callback)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 */ getText(@tringRes int resId)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 getString(@tringRes int resId)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 getString(@tringRes int resId, Object... formatArgs)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 getColor(@olorRes int id)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 getDrawable(@rawableRes int id)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 getColorStateList(@olorRes int id)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 */ setTheme(@tyleRes int resid)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. */ getThemeResId()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) getTheme()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 */ obtainStyledAttributes(@tyleableRes int[] attrs)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 */ obtainStyledAttributes( @tyleRes int resid, @StyleableRes int[] attrs)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 */ obtainStyledAttributes( AttributeSet set, @StyleableRes int[] attrs)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 */ obtainStyledAttributes( AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes)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 */ getClassLoader()699 public abstract ClassLoader getClassLoader(); 700 701 /** Return the name of this application's package. */ getPackageName()702 public abstract String getPackageName(); 703 704 /** @hide Return the name of the base context this context is derived from. */ getBasePackageName()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). */ getOpPackageName()712 public abstract String getOpPackageName(); 713 714 /** Return the full application info for this context's package. */ getApplicationInfo()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 */ getPackageResourcePath()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 */ getPackageCodePath()739 public abstract String getPackageCodePath(); 740 741 /** 742 * @hide 743 * @deprecated use {@link #getSharedPreferencesPath(String)} 744 */ 745 @Deprecated getSharedPrefsFile(String name)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 */ getSharedPreferences(String name, @PreferencesMode int mode)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 */ getSharedPreferences(File file, @PreferencesMode int mode)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 */ moveSharedPreferencesFrom(Context sourceContext, String name)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 */ deleteSharedPreferences(String name)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 */ openFileInput(String name)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 */ openFileOutput(String name, @FileMode int mode)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 */ deleteFile(String name)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 */ getFileStreamPath(String name)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 */ getSharedPreferencesPath(String name)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 */ getDataDir()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 */ getFilesDir()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 */ getNoBackupFilesDir()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 getExternalFilesDir(@ullable String type)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 */ getExternalFilesDirs(String type)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 */ getObbDir()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 */ getObbDirs()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 */ getCacheDir()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 */ getCodeCacheDir()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 getExternalCacheDir()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 getPreloadsFileCache()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 */ getExternalCacheDirs()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 */ getExternalMediaDirs()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 */ fileList()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 */ getDir(String name, @FileMode int mode)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 */ openOrCreateDatabase(String name, @DatabaseMode int mode, CursorFactory factory)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 */ openOrCreateDatabase(String name, @DatabaseMode int mode, CursorFactory factory, @Nullable DatabaseErrorHandler errorHandler)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 */ moveDatabaseFrom(Context sourceContext, String name)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 */ deleteDatabase(String name)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 */ getDatabasePath(String name)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 */ databaseList()1560 public abstract String[] databaseList(); 1561 1562 /** 1563 * @deprecated Use {@link android.app.WallpaperManager#getDrawable 1564 * WallpaperManager.get()} instead. 1565 */ 1566 @Deprecated getWallpaper()1567 public abstract Drawable getWallpaper(); 1568 1569 /** 1570 * @deprecated Use {@link android.app.WallpaperManager#peekDrawable 1571 * WallpaperManager.peek()} instead. 1572 */ 1573 @Deprecated peekWallpaper()1574 public abstract Drawable peekWallpaper(); 1575 1576 /** 1577 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumWidth() 1578 * WallpaperManager.getDesiredMinimumWidth()} instead. 1579 */ 1580 @Deprecated getWallpaperDesiredMinimumWidth()1581 public abstract int getWallpaperDesiredMinimumWidth(); 1582 1583 /** 1584 * @deprecated Use {@link android.app.WallpaperManager#getDesiredMinimumHeight() 1585 * WallpaperManager.getDesiredMinimumHeight()} instead. 1586 */ 1587 @Deprecated getWallpaperDesiredMinimumHeight()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 setWallpaper(Bitmap bitmap)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 setWallpaper(InputStream data)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 clearWallpaper()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 */ startActivity(@equiresPermission Intent intent)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) startActivityAsUser(@equiresPermission Intent intent, UserHandle user)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 */ startActivity(@equiresPermission Intent intent, @Nullable Bundle options)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) startActivityAsUser(@equiresPermission Intent intent, @Nullable Bundle options, UserHandle userId)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 */ startActivityForResult( @onNull String who, Intent intent, int requestCode, @Nullable Bundle options)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 */ canStartActivityForResult()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 */ startActivities(@equiresPermission Intent[] intents)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 */ startActivities(@equiresPermission Intent[] intents, Bundle options)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) startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle)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 */ startIntentSender(IntentSender intent, @Nullable Intent fillInIntent, @Intent.MutableFlags int flagsMask, @Intent.MutableFlags int flagsValues, int extraFlags)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 */ startIntentSender(IntentSender intent, @Nullable Intent fillInIntent, @Intent.MutableFlags int flagsMask, @Intent.MutableFlags int flagsValues, int extraFlags, @Nullable Bundle options)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 */ sendBroadcast(@equiresPermission Intent intent)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 */ sendBroadcast(@equiresPermission Intent intent, @Nullable String receiverPermission)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 */ sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions)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 sendBroadcast(Intent intent, @Nullable String receiverPermission, @Nullable Bundle options)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 */ sendBroadcast(Intent intent, String receiverPermission, int appOp)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 */ sendOrderedBroadcast(@equiresPermission Intent intent, @Nullable String receiverPermission)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 */ sendOrderedBroadcast(@equiresPermission @onNull Intent intent, @Nullable String receiverPermission, @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras)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 sendOrderedBroadcast(@onNull Intent intent, @Nullable String receiverPermission, @Nullable Bundle options, @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras)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 */ sendOrderedBroadcast(Intent intent, String receiverPermission, int appOp, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)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) sendBroadcastAsUser(@equiresPermission Intent intent, UserHandle user)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) sendBroadcastAsUser(@equiresPermission Intent intent, UserHandle user, @Nullable String receiverPermission)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) sendBroadcastAsUser(@equiresPermission Intent intent, UserHandle user, @Nullable String receiverPermission, @Nullable Bundle options)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) sendBroadcastAsUser(@equiresPermission Intent intent, UserHandle user, @Nullable String receiverPermission, int appOp)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) sendOrderedBroadcastAsUser(@equiresPermission Intent intent, UserHandle user, @Nullable String receiverPermission, BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras)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) sendOrderedBroadcastAsUser(Intent intent, UserHandle user, @Nullable String receiverPermission, int appOp, BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras)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) sendOrderedBroadcastAsUser(Intent intent, UserHandle user, @Nullable String receiverPermission, int appOp, @Nullable Bundle options, BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras)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) sendStickyBroadcast(@equiresPermission Intent intent)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) sendStickyOrderedBroadcast(@equiresPermission Intent intent, BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras)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) removeStickyBroadcast(@equiresPermission Intent intent)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 }) sendStickyBroadcastAsUser(@equiresPermission Intent intent, UserHandle user)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 }) sendStickyBroadcastAsUser(@equiresPermission Intent intent, UserHandle user, Bundle options)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 }) sendStickyOrderedBroadcastAsUser(@equiresPermission Intent intent, UserHandle user, BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras)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 }) removeStickyBroadcastAsUser(@equiresPermission Intent intent, UserHandle user)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 registerReceiver(@ullable BroadcastReceiver receiver, IntentFilter filter)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 registerReceiver(@ullable BroadcastReceiver receiver, IntentFilter filter, @RegisterReceiverFlags int flags)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 registerReceiver(BroadcastReceiver receiver, IntentFilter filter, @Nullable String broadcastPermission, @Nullable Handler scheduler)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 registerReceiver(BroadcastReceiver receiver, IntentFilter filter, @Nullable String broadcastPermission, @Nullable Handler scheduler, @RegisterReceiverFlags int flags)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) registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, IntentFilter filter, @Nullable String broadcastPermission, @Nullable Handler scheduler)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 */ unregisterReceiver(BroadcastReceiver receiver)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 startService(Intent service)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 startForegroundService(Intent service)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) startForegroundServiceAsUser(Intent service, UserHandle user)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 */ stopService(Intent service)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) startServiceAsUser(Intent service, UserHandle user)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) stopServiceAsUser(Intent service, UserHandle user)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 */ bindService(@equiresPermission Intent service, @NonNull ServiceConnection conn, @BindServiceFlags int flags)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) bindServiceAsUser(@equiresPermission Intent service, ServiceConnection conn, int flags, UserHandle user)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) bindServiceAsUser(Intent service, ServiceConnection conn, int flags, Handler handler, UserHandle user)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 */ unbindService(@onNull ServiceConnection conn)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 */ startInstrumentation(@onNull ComponentName className, @Nullable String profileFile, @Nullable Bundle arguments)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 VIBRATOR_SERVICE, 2889 //@hide: STATUS_BAR_SERVICE, 2890 CONNECTIVITY_SERVICE, 2891 IPSEC_SERVICE, 2892 //@hide: UPDATE_LOCK_SERVICE, 2893 //@hide: NETWORKMANAGEMENT_SERVICE, 2894 NETWORK_STATS_SERVICE, 2895 //@hide: NETWORK_POLICY_SERVICE, 2896 WIFI_SERVICE, 2897 WIFI_AWARE_SERVICE, 2898 WIFI_P2P_SERVICE, 2899 WIFI_SCANNING_SERVICE, 2900 //@hide: WIFI_RTT_SERVICE, 2901 //@hide: ETHERNET_SERVICE, 2902 WIFI_RTT_SERVICE, 2903 NSD_SERVICE, 2904 AUDIO_SERVICE, 2905 FINGERPRINT_SERVICE, 2906 MEDIA_ROUTER_SERVICE, 2907 TELEPHONY_SERVICE, 2908 TELEPHONY_SUBSCRIPTION_SERVICE, 2909 CARRIER_CONFIG_SERVICE, 2910 TELECOM_SERVICE, 2911 CLIPBOARD_SERVICE, 2912 INPUT_METHOD_SERVICE, 2913 TEXT_SERVICES_MANAGER_SERVICE, 2914 TEXT_CLASSIFICATION_SERVICE, 2915 APPWIDGET_SERVICE, 2916 //@hide: VOICE_INTERACTION_MANAGER_SERVICE, 2917 //@hide: BACKUP_SERVICE, 2918 DROPBOX_SERVICE, 2919 //@hide: DEVICE_IDLE_CONTROLLER, 2920 DEVICE_POLICY_SERVICE, 2921 UI_MODE_SERVICE, 2922 DOWNLOAD_SERVICE, 2923 NFC_SERVICE, 2924 BLUETOOTH_SERVICE, 2925 //@hide: SIP_SERVICE, 2926 USB_SERVICE, 2927 LAUNCHER_APPS_SERVICE, 2928 //@hide: SERIAL_SERVICE, 2929 //@hide: HDMI_CONTROL_SERVICE, 2930 INPUT_SERVICE, 2931 DISPLAY_SERVICE, 2932 USER_SERVICE, 2933 RESTRICTIONS_SERVICE, 2934 APP_OPS_SERVICE, 2935 CAMERA_SERVICE, 2936 PRINT_SERVICE, 2937 CONSUMER_IR_SERVICE, 2938 //@hide: TRUST_SERVICE, 2939 TV_INPUT_SERVICE, 2940 //@hide: NETWORK_SCORE_SERVICE, 2941 USAGE_STATS_SERVICE, 2942 MEDIA_SESSION_SERVICE, 2943 BATTERY_SERVICE, 2944 JOB_SCHEDULER_SERVICE, 2945 //@hide: PERSISTENT_DATA_BLOCK_SERVICE, 2946 //@hide: OEM_LOCK_SERVICE, 2947 MEDIA_PROJECTION_SERVICE, 2948 MIDI_SERVICE, 2949 RADIO_SERVICE, 2950 HARDWARE_PROPERTIES_SERVICE, 2951 //@hide: SOUND_TRIGGER_SERVICE, 2952 SHORTCUT_SERVICE, 2953 //@hide: CONTEXTHUB_SERVICE, 2954 SYSTEM_HEALTH_SERVICE, 2955 //@hide: INCIDENT_SERVICE 2956 }) 2957 @Retention(RetentionPolicy.SOURCE) 2958 public @interface ServiceName {} 2959 2960 /** 2961 * Return the handle to a system-level service by name. The class of the 2962 * returned object varies by the requested name. Currently available names 2963 * are: 2964 * 2965 * <dl> 2966 * <dt> {@link #WINDOW_SERVICE} ("window") 2967 * <dd> The top-level window manager in which you can place custom 2968 * windows. The returned object is a {@link android.view.WindowManager}. 2969 * <dt> {@link #LAYOUT_INFLATER_SERVICE} ("layout_inflater") 2970 * <dd> A {@link android.view.LayoutInflater} for inflating layout resources 2971 * in this context. 2972 * <dt> {@link #ACTIVITY_SERVICE} ("activity") 2973 * <dd> A {@link android.app.ActivityManager} for interacting with the 2974 * global activity state of the system. 2975 * <dt> {@link #POWER_SERVICE} ("power") 2976 * <dd> A {@link android.os.PowerManager} for controlling power 2977 * management. 2978 * <dt> {@link #ALARM_SERVICE} ("alarm") 2979 * <dd> A {@link android.app.AlarmManager} for receiving intents at the 2980 * time of your choosing. 2981 * <dt> {@link #NOTIFICATION_SERVICE} ("notification") 2982 * <dd> A {@link android.app.NotificationManager} for informing the user 2983 * of background events. 2984 * <dt> {@link #KEYGUARD_SERVICE} ("keyguard") 2985 * <dd> A {@link android.app.KeyguardManager} for controlling keyguard. 2986 * <dt> {@link #LOCATION_SERVICE} ("location") 2987 * <dd> A {@link android.location.LocationManager} for controlling location 2988 * (e.g., GPS) updates. 2989 * <dt> {@link #SEARCH_SERVICE} ("search") 2990 * <dd> A {@link android.app.SearchManager} for handling search. 2991 * <dt> {@link #VIBRATOR_SERVICE} ("vibrator") 2992 * <dd> A {@link android.os.Vibrator} for interacting with the vibrator 2993 * hardware. 2994 * <dt> {@link #CONNECTIVITY_SERVICE} ("connection") 2995 * <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for 2996 * handling management of network connections. 2997 * <dt> {@link #WIFI_SERVICE} ("wifi") 2998 * <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of Wi-Fi 2999 * connectivity. On releases before NYC, it should only be obtained from an application 3000 * context, and not from any other derived context to avoid memory leaks within the calling 3001 * process. 3002 * <dt> {@link #WIFI_AWARE_SERVICE} ("wifiaware") 3003 * <dd> A {@link android.net.wifi.aware.WifiAwareManager WifiAwareManager} for management of 3004 * Wi-Fi Aware discovery and connectivity. 3005 * <dt> {@link #WIFI_P2P_SERVICE} ("wifip2p") 3006 * <dd> A {@link android.net.wifi.p2p.WifiP2pManager WifiP2pManager} for management of 3007 * Wi-Fi Direct connectivity. 3008 * <dt> {@link #INPUT_METHOD_SERVICE} ("input_method") 3009 * <dd> An {@link android.view.inputmethod.InputMethodManager InputMethodManager} 3010 * for management of input methods. 3011 * <dt> {@link #UI_MODE_SERVICE} ("uimode") 3012 * <dd> An {@link android.app.UiModeManager} for controlling UI modes. 3013 * <dt> {@link #DOWNLOAD_SERVICE} ("download") 3014 * <dd> A {@link android.app.DownloadManager} for requesting HTTP downloads 3015 * <dt> {@link #BATTERY_SERVICE} ("batterymanager") 3016 * <dd> A {@link android.os.BatteryManager} for managing battery state 3017 * <dt> {@link #JOB_SCHEDULER_SERVICE} ("taskmanager") 3018 * <dd> A {@link android.app.job.JobScheduler} for managing scheduled tasks 3019 * <dt> {@link #NETWORK_STATS_SERVICE} ("netstats") 3020 * <dd> A {@link android.app.usage.NetworkStatsManager NetworkStatsManager} for querying network 3021 * usage statistics. 3022 * <dt> {@link #HARDWARE_PROPERTIES_SERVICE} ("hardware_properties") 3023 * <dd> A {@link android.os.HardwarePropertiesManager} for accessing hardware properties. 3024 * </dl> 3025 * 3026 * <p>Note: System services obtained via this API may be closely associated with 3027 * the Context in which they are obtained from. In general, do not share the 3028 * service objects between various different contexts (Activities, Applications, 3029 * Services, Providers, etc.) 3030 * 3031 * @param name The name of the desired service. 3032 * 3033 * @return The service or null if the name does not exist. 3034 * 3035 * @see #WINDOW_SERVICE 3036 * @see android.view.WindowManager 3037 * @see #LAYOUT_INFLATER_SERVICE 3038 * @see android.view.LayoutInflater 3039 * @see #ACTIVITY_SERVICE 3040 * @see android.app.ActivityManager 3041 * @see #POWER_SERVICE 3042 * @see android.os.PowerManager 3043 * @see #ALARM_SERVICE 3044 * @see android.app.AlarmManager 3045 * @see #NOTIFICATION_SERVICE 3046 * @see android.app.NotificationManager 3047 * @see #KEYGUARD_SERVICE 3048 * @see android.app.KeyguardManager 3049 * @see #LOCATION_SERVICE 3050 * @see android.location.LocationManager 3051 * @see #SEARCH_SERVICE 3052 * @see android.app.SearchManager 3053 * @see #SENSOR_SERVICE 3054 * @see android.hardware.SensorManager 3055 * @see #STORAGE_SERVICE 3056 * @see android.os.storage.StorageManager 3057 * @see #VIBRATOR_SERVICE 3058 * @see android.os.Vibrator 3059 * @see #CONNECTIVITY_SERVICE 3060 * @see android.net.ConnectivityManager 3061 * @see #WIFI_SERVICE 3062 * @see android.net.wifi.WifiManager 3063 * @see #AUDIO_SERVICE 3064 * @see android.media.AudioManager 3065 * @see #MEDIA_ROUTER_SERVICE 3066 * @see android.media.MediaRouter 3067 * @see #TELEPHONY_SERVICE 3068 * @see android.telephony.TelephonyManager 3069 * @see #TELEPHONY_SUBSCRIPTION_SERVICE 3070 * @see android.telephony.SubscriptionManager 3071 * @see #CARRIER_CONFIG_SERVICE 3072 * @see android.telephony.CarrierConfigManager 3073 * @see #INPUT_METHOD_SERVICE 3074 * @see android.view.inputmethod.InputMethodManager 3075 * @see #UI_MODE_SERVICE 3076 * @see android.app.UiModeManager 3077 * @see #DOWNLOAD_SERVICE 3078 * @see android.app.DownloadManager 3079 * @see #BATTERY_SERVICE 3080 * @see android.os.BatteryManager 3081 * @see #JOB_SCHEDULER_SERVICE 3082 * @see android.app.job.JobScheduler 3083 * @see #NETWORK_STATS_SERVICE 3084 * @see android.app.usage.NetworkStatsManager 3085 * @see android.os.HardwarePropertiesManager 3086 * @see #HARDWARE_PROPERTIES_SERVICE 3087 */ getSystemService(@erviceName @onNull String name)3088 public abstract @Nullable Object getSystemService(@ServiceName @NonNull String name); 3089 3090 /** 3091 * Return the handle to a system-level service by class. 3092 * <p> 3093 * Currently available classes are: 3094 * {@link android.view.WindowManager}, {@link android.view.LayoutInflater}, 3095 * {@link android.app.ActivityManager}, {@link android.os.PowerManager}, 3096 * {@link android.app.AlarmManager}, {@link android.app.NotificationManager}, 3097 * {@link android.app.KeyguardManager}, {@link android.location.LocationManager}, 3098 * {@link android.app.SearchManager}, {@link android.os.Vibrator}, 3099 * {@link android.net.ConnectivityManager}, 3100 * {@link android.net.wifi.WifiManager}, 3101 * {@link android.media.AudioManager}, {@link android.media.MediaRouter}, 3102 * {@link android.telephony.TelephonyManager}, {@link android.telephony.SubscriptionManager}, 3103 * {@link android.view.inputmethod.InputMethodManager}, 3104 * {@link android.app.UiModeManager}, {@link android.app.DownloadManager}, 3105 * {@link android.os.BatteryManager}, {@link android.app.job.JobScheduler}, 3106 * {@link android.app.usage.NetworkStatsManager}. 3107 * </p><p> 3108 * Note: System services obtained via this API may be closely associated with 3109 * the Context in which they are obtained from. In general, do not share the 3110 * service objects between various different contexts (Activities, Applications, 3111 * Services, Providers, etc.) 3112 * </p> 3113 * 3114 * @param serviceClass The class of the desired service. 3115 * @return The service or null if the class is not a supported system service. 3116 */ 3117 @SuppressWarnings("unchecked") getSystemService(@onNull Class<T> serviceClass)3118 public final @Nullable <T> T getSystemService(@NonNull Class<T> serviceClass) { 3119 // Because subclasses may override getSystemService(String) we cannot 3120 // perform a lookup by class alone. We must first map the class to its 3121 // service name then invoke the string-based method. 3122 String serviceName = getSystemServiceName(serviceClass); 3123 return serviceName != null ? (T)getSystemService(serviceName) : null; 3124 } 3125 3126 /** 3127 * Gets the name of the system-level service that is represented by the specified class. 3128 * 3129 * @param serviceClass The class of the desired service. 3130 * @return The service name or null if the class is not a supported system service. 3131 */ getSystemServiceName(@onNull Class<?> serviceClass)3132 public abstract @Nullable String getSystemServiceName(@NonNull Class<?> serviceClass); 3133 3134 /** 3135 * Use with {@link #getSystemService} to retrieve a 3136 * {@link android.os.PowerManager} for controlling power management, 3137 * including "wake locks," which let you keep the device on while 3138 * you're running long tasks. 3139 */ 3140 public static final String POWER_SERVICE = "power"; 3141 3142 /** 3143 * Use with {@link #getSystemService} to retrieve a 3144 * {@link android.os.RecoverySystem} for accessing the recovery system 3145 * service. 3146 * 3147 * @see #getSystemService 3148 * @hide 3149 */ 3150 public static final String RECOVERY_SERVICE = "recovery"; 3151 3152 /** 3153 * Use with {@link #getSystemService} to retrieve a 3154 * {@link android.view.WindowManager} for accessing the system's window 3155 * manager. 3156 * 3157 * @see #getSystemService 3158 * @see android.view.WindowManager 3159 */ 3160 public static final String WINDOW_SERVICE = "window"; 3161 3162 /** 3163 * Use with {@link #getSystemService} to retrieve a 3164 * {@link android.view.LayoutInflater} for inflating layout resources in this 3165 * context. 3166 * 3167 * @see #getSystemService 3168 * @see android.view.LayoutInflater 3169 */ 3170 public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater"; 3171 3172 /** 3173 * Use with {@link #getSystemService} to retrieve a 3174 * {@link android.accounts.AccountManager} for receiving intents at a 3175 * time of your choosing. 3176 * 3177 * @see #getSystemService 3178 * @see android.accounts.AccountManager 3179 */ 3180 public static final String ACCOUNT_SERVICE = "account"; 3181 3182 /** 3183 * Use with {@link #getSystemService} to retrieve a 3184 * {@link android.app.ActivityManager} for interacting with the global 3185 * system state. 3186 * 3187 * @see #getSystemService 3188 * @see android.app.ActivityManager 3189 */ 3190 public static final String ACTIVITY_SERVICE = "activity"; 3191 3192 /** 3193 * Use with {@link #getSystemService} to retrieve a 3194 * {@link android.app.AlarmManager} for receiving intents at a 3195 * time of your choosing. 3196 * 3197 * @see #getSystemService 3198 * @see android.app.AlarmManager 3199 */ 3200 public static final String ALARM_SERVICE = "alarm"; 3201 3202 /** 3203 * Use with {@link #getSystemService} to retrieve a 3204 * {@link android.app.NotificationManager} for informing the user of 3205 * background events. 3206 * 3207 * @see #getSystemService 3208 * @see android.app.NotificationManager 3209 */ 3210 public static final String NOTIFICATION_SERVICE = "notification"; 3211 3212 /** 3213 * Use with {@link #getSystemService} to retrieve a 3214 * {@link android.view.accessibility.AccessibilityManager} for giving the user 3215 * feedback for UI events through the registered event listeners. 3216 * 3217 * @see #getSystemService 3218 * @see android.view.accessibility.AccessibilityManager 3219 */ 3220 public static final String ACCESSIBILITY_SERVICE = "accessibility"; 3221 3222 /** 3223 * Use with {@link #getSystemService} to retrieve a 3224 * {@link android.view.accessibility.CaptioningManager} for obtaining 3225 * captioning properties and listening for changes in captioning 3226 * preferences. 3227 * 3228 * @see #getSystemService 3229 * @see android.view.accessibility.CaptioningManager 3230 */ 3231 public static final String CAPTIONING_SERVICE = "captioning"; 3232 3233 /** 3234 * Use with {@link #getSystemService} to retrieve a 3235 * {@link android.app.NotificationManager} for controlling keyguard. 3236 * 3237 * @see #getSystemService 3238 * @see android.app.KeyguardManager 3239 */ 3240 public static final String KEYGUARD_SERVICE = "keyguard"; 3241 3242 /** 3243 * Use with {@link #getSystemService} to retrieve a {@link 3244 * android.location.LocationManager} for controlling location 3245 * updates. 3246 * 3247 * @see #getSystemService 3248 * @see android.location.LocationManager 3249 */ 3250 public static final String LOCATION_SERVICE = "location"; 3251 3252 /** 3253 * Use with {@link #getSystemService} to retrieve a 3254 * {@link android.location.CountryDetector} for detecting the country that 3255 * the user is in. 3256 * 3257 * @hide 3258 */ 3259 public static final String COUNTRY_DETECTOR = "country_detector"; 3260 3261 /** 3262 * Use with {@link #getSystemService} to retrieve a {@link 3263 * android.app.SearchManager} for handling searches. 3264 * 3265 * @see #getSystemService 3266 * @see android.app.SearchManager 3267 */ 3268 public static final String SEARCH_SERVICE = "search"; 3269 3270 /** 3271 * Use with {@link #getSystemService} to retrieve a {@link 3272 * android.hardware.SensorManager} for accessing sensors. 3273 * 3274 * @see #getSystemService 3275 * @see android.hardware.SensorManager 3276 */ 3277 public static final String SENSOR_SERVICE = "sensor"; 3278 3279 /** 3280 * Use with {@link #getSystemService} to retrieve a {@link 3281 * android.os.storage.StorageManager} for accessing system storage 3282 * functions. 3283 * 3284 * @see #getSystemService 3285 * @see android.os.storage.StorageManager 3286 */ 3287 public static final String STORAGE_SERVICE = "storage"; 3288 3289 /** 3290 * Use with {@link #getSystemService} to retrieve a {@link 3291 * android.app.usage.StorageStatsManager} for accessing system storage 3292 * statistics. 3293 * 3294 * @see #getSystemService 3295 * @see android.app.usage.StorageStatsManager 3296 */ 3297 public static final String STORAGE_STATS_SERVICE = "storagestats"; 3298 3299 /** 3300 * Use with {@link #getSystemService} to retrieve a 3301 * com.android.server.WallpaperService for accessing wallpapers. 3302 * 3303 * @see #getSystemService 3304 */ 3305 public static final String WALLPAPER_SERVICE = "wallpaper"; 3306 3307 /** 3308 * Use with {@link #getSystemService} to retrieve a {@link 3309 * android.os.Vibrator} for interacting with the vibration hardware. 3310 * 3311 * @see #getSystemService 3312 * @see android.os.Vibrator 3313 */ 3314 public static final String VIBRATOR_SERVICE = "vibrator"; 3315 3316 /** 3317 * Use with {@link #getSystemService} to retrieve a {@link 3318 * android.app.StatusBarManager} for interacting with the status bar. 3319 * 3320 * @see #getSystemService 3321 * @see android.app.StatusBarManager 3322 * @hide 3323 */ 3324 public static final String STATUS_BAR_SERVICE = "statusbar"; 3325 3326 /** 3327 * Use with {@link #getSystemService} to retrieve a {@link 3328 * android.net.ConnectivityManager} for handling management of 3329 * network connections. 3330 * 3331 * @see #getSystemService 3332 * @see android.net.ConnectivityManager 3333 */ 3334 public static final String CONNECTIVITY_SERVICE = "connectivity"; 3335 3336 /** 3337 * Use with {@link #getSystemService} to retrieve a 3338 * {@link android.net.IpSecManager} for encrypting Sockets or Networks with 3339 * IPSec. 3340 * 3341 * @hide 3342 * @see #getSystemService 3343 */ 3344 public static final String IPSEC_SERVICE = "ipsec"; 3345 3346 /** 3347 * Use with {@link #getSystemService} to retrieve a {@link 3348 * android.os.IUpdateLock} for managing runtime sequences that 3349 * must not be interrupted by headless OTA application or similar. 3350 * 3351 * @hide 3352 * @see #getSystemService 3353 * @see android.os.UpdateLock 3354 */ 3355 public static final String UPDATE_LOCK_SERVICE = "updatelock"; 3356 3357 /** 3358 * Constant for the internal network management service, not really a Context service. 3359 * @hide 3360 */ 3361 public static final String NETWORKMANAGEMENT_SERVICE = "network_management"; 3362 3363 /** 3364 * Use with {@link #getSystemService} to retrieve a {@link 3365 * android.app.usage.NetworkStatsManager} for querying network usage stats. 3366 * 3367 * @see #getSystemService 3368 * @see android.app.usage.NetworkStatsManager 3369 */ 3370 public static final String NETWORK_STATS_SERVICE = "netstats"; 3371 /** {@hide} */ 3372 public static final String NETWORK_POLICY_SERVICE = "netpolicy"; 3373 3374 /** 3375 * Use with {@link #getSystemService} to retrieve a {@link 3376 * android.net.wifi.WifiManager} for handling management of 3377 * Wi-Fi access. 3378 * 3379 * @see #getSystemService 3380 * @see android.net.wifi.WifiManager 3381 */ 3382 public static final String WIFI_SERVICE = "wifi"; 3383 3384 /** 3385 * Use with {@link #getSystemService} to retrieve a {@link 3386 * android.net.wifi.p2p.WifiP2pManager} for handling management of 3387 * Wi-Fi peer-to-peer connections. 3388 * 3389 * @see #getSystemService 3390 * @see android.net.wifi.p2p.WifiP2pManager 3391 */ 3392 public static final String WIFI_P2P_SERVICE = "wifip2p"; 3393 3394 /** 3395 * Use with {@link #getSystemService} to retrieve a 3396 * {@link android.net.wifi.aware.WifiAwareManager} for handling management of 3397 * Wi-Fi Aware. 3398 * 3399 * @see #getSystemService 3400 * @see android.net.wifi.aware.WifiAwareManager 3401 */ 3402 public static final String WIFI_AWARE_SERVICE = "wifiaware"; 3403 3404 /** 3405 * Use with {@link #getSystemService} to retrieve a {@link 3406 * android.net.wifi.WifiScanner} for scanning the wifi universe 3407 * 3408 * @see #getSystemService 3409 * @see android.net.wifi.WifiScanner 3410 * @hide 3411 */ 3412 @SystemApi 3413 public static final String WIFI_SCANNING_SERVICE = "wifiscanner"; 3414 3415 /** 3416 * Use with {@link #getSystemService} to retrieve a {@link 3417 * android.net.wifi.RttManager} for ranging devices with wifi 3418 * 3419 * @see #getSystemService 3420 * @see android.net.wifi.RttManager 3421 * @hide 3422 */ 3423 @SystemApi 3424 public static final String WIFI_RTT_SERVICE = "rttmanager"; 3425 3426 /** 3427 * Use with {@link #getSystemService} to retrieve a {@link 3428 * android.net.EthernetManager} for handling management of 3429 * Ethernet access. 3430 * 3431 * @see #getSystemService 3432 * @see android.net.EthernetManager 3433 * 3434 * @hide 3435 */ 3436 public static final String ETHERNET_SERVICE = "ethernet"; 3437 3438 /** 3439 * Use with {@link #getSystemService} to retrieve a {@link 3440 * android.net.nsd.NsdManager} for handling management of network service 3441 * discovery 3442 * 3443 * @see #getSystemService 3444 * @see android.net.nsd.NsdManager 3445 */ 3446 public static final String NSD_SERVICE = "servicediscovery"; 3447 3448 /** 3449 * Use with {@link #getSystemService} to retrieve a 3450 * {@link android.media.AudioManager} for handling management of volume, 3451 * ringer modes and audio routing. 3452 * 3453 * @see #getSystemService 3454 * @see android.media.AudioManager 3455 */ 3456 public static final String AUDIO_SERVICE = "audio"; 3457 3458 /** 3459 * Use with {@link #getSystemService} to retrieve a 3460 * {@link android.hardware.fingerprint.FingerprintManager} for handling management 3461 * of fingerprints. 3462 * 3463 * @see #getSystemService 3464 * @see android.hardware.fingerprint.FingerprintManager 3465 */ 3466 public static final String FINGERPRINT_SERVICE = "fingerprint"; 3467 3468 /** 3469 * Use with {@link #getSystemService} to retrieve a 3470 * {@link android.media.MediaRouter} for controlling and managing 3471 * routing of media. 3472 * 3473 * @see #getSystemService 3474 * @see android.media.MediaRouter 3475 */ 3476 public static final String MEDIA_ROUTER_SERVICE = "media_router"; 3477 3478 /** 3479 * Use with {@link #getSystemService} to retrieve a 3480 * {@link android.media.session.MediaSessionManager} for managing media Sessions. 3481 * 3482 * @see #getSystemService 3483 * @see android.media.session.MediaSessionManager 3484 */ 3485 public static final String MEDIA_SESSION_SERVICE = "media_session"; 3486 3487 /** 3488 * Use with {@link #getSystemService} to retrieve a 3489 * {@link android.telephony.TelephonyManager} for handling management the 3490 * telephony features of the device. 3491 * 3492 * @see #getSystemService 3493 * @see android.telephony.TelephonyManager 3494 */ 3495 public static final String TELEPHONY_SERVICE = "phone"; 3496 3497 /** 3498 * Use with {@link #getSystemService} to retrieve a 3499 * {@link android.telephony.SubscriptionManager} for handling management the 3500 * telephony subscriptions of the device. 3501 * 3502 * @see #getSystemService 3503 * @see android.telephony.SubscriptionManager 3504 */ 3505 public static final String TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service"; 3506 3507 /** 3508 * Use with {@link #getSystemService} to retrieve a 3509 * {@link android.telecom.TelecomManager} to manage telecom-related features 3510 * of the device. 3511 * 3512 * @see #getSystemService 3513 * @see android.telecom.TelecomManager 3514 */ 3515 public static final String TELECOM_SERVICE = "telecom"; 3516 3517 /** 3518 * Use with {@link #getSystemService} to retrieve a 3519 * {@link android.telephony.CarrierConfigManager} for reading carrier configuration values. 3520 * 3521 * @see #getSystemService 3522 * @see android.telephony.CarrierConfigManager 3523 */ 3524 public static final String CARRIER_CONFIG_SERVICE = "carrier_config"; 3525 3526 /** 3527 * Use with {@link #getSystemService} to retrieve a 3528 * {@link android.text.ClipboardManager} for accessing and modifying 3529 * {@link android.content.ClipboardManager} for accessing and modifying 3530 * the contents of the global clipboard. 3531 * 3532 * @see #getSystemService 3533 * @see android.content.ClipboardManager 3534 */ 3535 public static final String CLIPBOARD_SERVICE = "clipboard"; 3536 3537 /** 3538 * Use with {@link #getSystemService} to retrieve a 3539 * {@link TextClassificationManager} for text classification services. 3540 * 3541 * @see #getSystemService 3542 * @see TextClassificationManager 3543 */ 3544 public static final String TEXT_CLASSIFICATION_SERVICE = "textclassification"; 3545 3546 /** 3547 * Use with {@link #getSystemService} to retrieve a 3548 * {@link android.view.inputmethod.InputMethodManager} for accessing input 3549 * methods. 3550 * 3551 * @see #getSystemService 3552 */ 3553 public static final String INPUT_METHOD_SERVICE = "input_method"; 3554 3555 /** 3556 * Use with {@link #getSystemService} to retrieve a 3557 * {@link android.view.textservice.TextServicesManager} for accessing 3558 * text services. 3559 * 3560 * @see #getSystemService 3561 */ 3562 public static final String TEXT_SERVICES_MANAGER_SERVICE = "textservices"; 3563 3564 /** 3565 * Use with {@link #getSystemService} to retrieve a 3566 * {@link android.appwidget.AppWidgetManager} for accessing AppWidgets. 3567 * 3568 * @see #getSystemService 3569 */ 3570 public static final String APPWIDGET_SERVICE = "appwidget"; 3571 3572 /** 3573 * Official published name of the (internal) voice interaction manager service. 3574 * 3575 * @hide 3576 * @see #getSystemService 3577 */ 3578 public static final String VOICE_INTERACTION_MANAGER_SERVICE = "voiceinteraction"; 3579 3580 /** 3581 * Official published name of the (internal) autofill service. 3582 * 3583 * @hide 3584 * @see #getSystemService 3585 */ 3586 public static final String AUTOFILL_MANAGER_SERVICE = "autofill"; 3587 3588 /** 3589 * Use with {@link #getSystemService} to access the 3590 * {@link com.android.server.voiceinteraction.SoundTriggerService}. 3591 * 3592 * @hide 3593 * @see #getSystemService 3594 */ 3595 public static final String SOUND_TRIGGER_SERVICE = "soundtrigger"; 3596 3597 3598 /** 3599 * Use with {@link #getSystemService} to retrieve an 3600 * {@link android.app.backup.IBackupManager IBackupManager} for communicating 3601 * with the backup mechanism. 3602 * @hide 3603 * 3604 * @see #getSystemService 3605 */ 3606 @SystemApi 3607 public static final String BACKUP_SERVICE = "backup"; 3608 3609 /** 3610 * Use with {@link #getSystemService} to retrieve a 3611 * {@link android.os.DropBoxManager} instance for recording 3612 * diagnostic logs. 3613 * @see #getSystemService 3614 */ 3615 public static final String DROPBOX_SERVICE = "dropbox"; 3616 3617 /** 3618 * System service name for the DeviceIdleController. There is no Java API for this. 3619 * @see #getSystemService 3620 * @hide 3621 */ 3622 public static final String DEVICE_IDLE_CONTROLLER = "deviceidle"; 3623 3624 /** 3625 * Use with {@link #getSystemService} to retrieve a 3626 * {@link android.app.admin.DevicePolicyManager} for working with global 3627 * device policy management. 3628 * 3629 * @see #getSystemService 3630 */ 3631 public static final String DEVICE_POLICY_SERVICE = "device_policy"; 3632 3633 /** 3634 * Use with {@link #getSystemService} to retrieve a 3635 * {@link android.app.UiModeManager} for controlling UI modes. 3636 * 3637 * @see #getSystemService 3638 */ 3639 public static final String UI_MODE_SERVICE = "uimode"; 3640 3641 /** 3642 * Use with {@link #getSystemService} to retrieve a 3643 * {@link android.app.DownloadManager} for requesting HTTP downloads. 3644 * 3645 * @see #getSystemService 3646 */ 3647 public static final String DOWNLOAD_SERVICE = "download"; 3648 3649 /** 3650 * Use with {@link #getSystemService} to retrieve a 3651 * {@link android.os.BatteryManager} for managing battery state. 3652 * 3653 * @see #getSystemService 3654 */ 3655 public static final String BATTERY_SERVICE = "batterymanager"; 3656 3657 /** 3658 * Use with {@link #getSystemService} to retrieve a 3659 * {@link android.nfc.NfcManager} for using NFC. 3660 * 3661 * @see #getSystemService 3662 */ 3663 public static final String NFC_SERVICE = "nfc"; 3664 3665 /** 3666 * Use with {@link #getSystemService} to retrieve a 3667 * {@link android.bluetooth.BluetoothManager} for using Bluetooth. 3668 * 3669 * @see #getSystemService 3670 */ 3671 public static final String BLUETOOTH_SERVICE = "bluetooth"; 3672 3673 /** 3674 * Use with {@link #getSystemService} to retrieve a 3675 * {@link android.net.sip.SipManager} for accessing the SIP related service. 3676 * 3677 * @see #getSystemService 3678 */ 3679 /** @hide */ 3680 public static final String SIP_SERVICE = "sip"; 3681 3682 /** 3683 * Use with {@link #getSystemService} to retrieve a {@link 3684 * android.hardware.usb.UsbManager} for access to USB devices (as a USB host) 3685 * and for controlling this device's behavior as a USB device. 3686 * 3687 * @see #getSystemService 3688 * @see android.hardware.usb.UsbManager 3689 */ 3690 public static final String USB_SERVICE = "usb"; 3691 3692 /** 3693 * Use with {@link #getSystemService} to retrieve a {@link 3694 * android.hardware.SerialManager} for access to serial ports. 3695 * 3696 * @see #getSystemService 3697 * @see android.hardware.SerialManager 3698 * 3699 * @hide 3700 */ 3701 public static final String SERIAL_SERVICE = "serial"; 3702 3703 /** 3704 * Use with {@link #getSystemService} to retrieve a 3705 * {@link android.hardware.hdmi.HdmiControlManager} for controlling and managing 3706 * HDMI-CEC protocol. 3707 * 3708 * @see #getSystemService 3709 * @see android.hardware.hdmi.HdmiControlManager 3710 * @hide 3711 */ 3712 @SystemApi 3713 public static final String HDMI_CONTROL_SERVICE = "hdmi_control"; 3714 3715 /** 3716 * Use with {@link #getSystemService} to retrieve a 3717 * {@link android.hardware.input.InputManager} for interacting with input devices. 3718 * 3719 * @see #getSystemService 3720 * @see android.hardware.input.InputManager 3721 */ 3722 public static final String INPUT_SERVICE = "input"; 3723 3724 /** 3725 * Use with {@link #getSystemService} to retrieve a 3726 * {@link android.hardware.display.DisplayManager} for interacting with display devices. 3727 * 3728 * @see #getSystemService 3729 * @see android.hardware.display.DisplayManager 3730 */ 3731 public static final String DISPLAY_SERVICE = "display"; 3732 3733 /** 3734 * Use with {@link #getSystemService} to retrieve a 3735 * {@link android.os.UserManager} for managing users on devices that support multiple users. 3736 * 3737 * @see #getSystemService 3738 * @see android.os.UserManager 3739 */ 3740 public static final String USER_SERVICE = "user"; 3741 3742 /** 3743 * Use with {@link #getSystemService} to retrieve a 3744 * {@link android.content.pm.LauncherApps} for querying and monitoring launchable apps across 3745 * profiles of a user. 3746 * 3747 * @see #getSystemService 3748 * @see android.content.pm.LauncherApps 3749 */ 3750 public static final String LAUNCHER_APPS_SERVICE = "launcherapps"; 3751 3752 /** 3753 * Use with {@link #getSystemService} to retrieve a 3754 * {@link android.content.RestrictionsManager} for retrieving application restrictions 3755 * and requesting permissions for restricted operations. 3756 * @see #getSystemService 3757 * @see android.content.RestrictionsManager 3758 */ 3759 public static final String RESTRICTIONS_SERVICE = "restrictions"; 3760 3761 /** 3762 * Use with {@link #getSystemService} to retrieve a 3763 * {@link android.app.AppOpsManager} for tracking application operations 3764 * on the device. 3765 * 3766 * @see #getSystemService 3767 * @see android.app.AppOpsManager 3768 */ 3769 public static final String APP_OPS_SERVICE = "appops"; 3770 3771 /** 3772 * Use with {@link #getSystemService} to retrieve a 3773 * {@link android.hardware.camera2.CameraManager} for interacting with 3774 * camera devices. 3775 * 3776 * @see #getSystemService 3777 * @see android.hardware.camera2.CameraManager 3778 */ 3779 public static final String CAMERA_SERVICE = "camera"; 3780 3781 /** 3782 * {@link android.print.PrintManager} for printing and managing 3783 * printers and print tasks. 3784 * 3785 * @see #getSystemService 3786 * @see android.print.PrintManager 3787 */ 3788 public static final String PRINT_SERVICE = "print"; 3789 3790 /** 3791 * Use with {@link #getSystemService} to retrieve a 3792 * {@link android.companion.CompanionDeviceManager} for managing companion devices 3793 * 3794 * @see #getSystemService 3795 * @see android.companion.CompanionDeviceManager 3796 */ 3797 public static final String COMPANION_DEVICE_SERVICE = "companiondevice"; 3798 3799 /** 3800 * Use with {@link #getSystemService} to retrieve a 3801 * {@link android.hardware.ConsumerIrManager} for transmitting infrared 3802 * signals from the device. 3803 * 3804 * @see #getSystemService 3805 * @see android.hardware.ConsumerIrManager 3806 */ 3807 public static final String CONSUMER_IR_SERVICE = "consumer_ir"; 3808 3809 /** 3810 * {@link android.app.trust.TrustManager} for managing trust agents. 3811 * @see #getSystemService 3812 * @see android.app.trust.TrustManager 3813 * @hide 3814 */ 3815 public static final String TRUST_SERVICE = "trust"; 3816 3817 /** 3818 * Use with {@link #getSystemService} to retrieve a 3819 * {@link android.media.tv.TvInputManager} for interacting with TV inputs 3820 * on the device. 3821 * 3822 * @see #getSystemService 3823 * @see android.media.tv.TvInputManager 3824 */ 3825 public static final String TV_INPUT_SERVICE = "tv_input"; 3826 3827 /** 3828 * {@link android.net.NetworkScoreManager} for managing network scoring. 3829 * @see #getSystemService 3830 * @see android.net.NetworkScoreManager 3831 * @hide 3832 */ 3833 @SystemApi 3834 public static final String NETWORK_SCORE_SERVICE = "network_score"; 3835 3836 /** 3837 * Use with {@link #getSystemService} to retrieve a {@link 3838 * android.app.usage.UsageStatsManager} for querying device usage stats. 3839 * 3840 * @see #getSystemService 3841 * @see android.app.usage.UsageStatsManager 3842 */ 3843 public static final String USAGE_STATS_SERVICE = "usagestats"; 3844 3845 /** 3846 * Use with {@link #getSystemService} to retrieve a {@link 3847 * android.app.job.JobScheduler} instance for managing occasional 3848 * background tasks. 3849 * @see #getSystemService 3850 * @see android.app.job.JobScheduler 3851 */ 3852 public static final String JOB_SCHEDULER_SERVICE = "jobscheduler"; 3853 3854 /** 3855 * Use with {@link #getSystemService} to retrieve a {@link 3856 * android.service.persistentdata.PersistentDataBlockManager} instance 3857 * for interacting with a storage device that lives across factory resets. 3858 * 3859 * @see #getSystemService 3860 * @see android.service.persistentdata.PersistentDataBlockManager 3861 * @hide 3862 */ 3863 @SystemApi 3864 public static final String PERSISTENT_DATA_BLOCK_SERVICE = "persistent_data_block"; 3865 3866 /** 3867 * Use with {@link #getSystemService} to retrieve a {@link 3868 * android.service.oemlock.OemLockManager} instance for managing the OEM lock. 3869 * 3870 * @see #getSystemService 3871 * @see android.service.oemlock.OemLockManager 3872 * @hide 3873 */ 3874 @SystemApi 3875 public static final String OEM_LOCK_SERVICE = "oem_lock"; 3876 3877 /** 3878 * Use with {@link #getSystemService} to retrieve a {@link 3879 * android.media.projection.MediaProjectionManager} instance for managing 3880 * media projection sessions. 3881 * @see #getSystemService 3882 * @see android.media.projection.MediaProjectionManager 3883 */ 3884 public static final String MEDIA_PROJECTION_SERVICE = "media_projection"; 3885 3886 /** 3887 * Use with {@link #getSystemService} to retrieve a 3888 * {@link android.media.midi.MidiManager} for accessing the MIDI service. 3889 * 3890 * @see #getSystemService 3891 */ 3892 public static final String MIDI_SERVICE = "midi"; 3893 3894 3895 /** 3896 * Use with {@link #getSystemService} to retrieve a 3897 * {@link android.hardware.radio.RadioManager} for accessing the broadcast radio service. 3898 * 3899 * @see #getSystemService 3900 * @hide 3901 */ 3902 public static final String RADIO_SERVICE = "radio"; 3903 3904 /** 3905 * Use with {@link #getSystemService} to retrieve a 3906 * {@link android.os.HardwarePropertiesManager} for accessing the hardware properties service. 3907 * 3908 * @see #getSystemService 3909 */ 3910 public static final String HARDWARE_PROPERTIES_SERVICE = "hardware_properties"; 3911 3912 /** 3913 * Use with {@link #getSystemService} to retrieve a 3914 * {@link android.content.pm.ShortcutManager} for accessing the launcher shortcut service. 3915 * 3916 * @see #getSystemService 3917 * @see android.content.pm.ShortcutManager 3918 */ 3919 public static final String SHORTCUT_SERVICE = "shortcut"; 3920 3921 /** 3922 * Use with {@link #getSystemService} to retrieve a {@link 3923 * android.hardware.location.ContextHubManager} for accessing context hubs. 3924 * 3925 * @see #getSystemService 3926 * @see android.hardware.location.ContextHubManager 3927 * 3928 * @hide 3929 */ 3930 @SystemApi 3931 public static final String CONTEXTHUB_SERVICE = "contexthub"; 3932 3933 /** 3934 * Use with {@link #getSystemService} to retrieve a 3935 * {@link android.os.health.SystemHealthManager} for accessing system health (battery, power, 3936 * memory, etc) metrics. 3937 * 3938 * @see #getSystemService 3939 */ 3940 public static final String SYSTEM_HEALTH_SERVICE = "systemhealth"; 3941 3942 /** 3943 * Gatekeeper Service. 3944 * @hide 3945 */ 3946 public static final String GATEKEEPER_SERVICE = "android.service.gatekeeper.IGateKeeperService"; 3947 3948 /** 3949 * Service defining the policy for access to device identifiers. 3950 * @hide 3951 */ 3952 public static final String DEVICE_IDENTIFIERS_SERVICE = "device_identifiers"; 3953 3954 /** 3955 * Service to report a system health "incident" 3956 * @hide 3957 */ 3958 public static final String INCIDENT_SERVICE = "incident"; 3959 3960 /** 3961 * Use with {@link #getSystemService} to retrieve a {@link 3962 * android.content.om.OverlayManager} for managing overlay packages. 3963 * 3964 * @see #getSystemService 3965 * @see android.content.om.OverlayManager 3966 * @hide 3967 */ 3968 public static final String OVERLAY_SERVICE = "overlay"; 3969 3970 /** 3971 * Use with {@link #getSystemService} to retrieve a 3972 * {@link VrManager} for accessing the VR service. 3973 * 3974 * @see #getSystemService 3975 * @hide 3976 */ 3977 @SystemApi 3978 public static final String VR_SERVICE = "vrmanager"; 3979 3980 /** 3981 * Determine whether the given permission is allowed for a particular 3982 * process and user ID running in the system. 3983 * 3984 * @param permission The name of the permission being checked. 3985 * @param pid The process ID being checked against. Must be > 0. 3986 * @param uid The user ID being checked against. A uid of 0 is the root 3987 * user, which will pass every permission check. 3988 * 3989 * @return {@link PackageManager#PERMISSION_GRANTED} if the given 3990 * pid/uid is allowed that permission, or 3991 * {@link PackageManager#PERMISSION_DENIED} if it is not. 3992 * 3993 * @see PackageManager#checkPermission(String, String) 3994 * @see #checkCallingPermission 3995 */ 3996 @CheckResult(suggest="#enforcePermission(String,int,int,String)") 3997 @PackageManager.PermissionResult checkPermission(@onNull String permission, int pid, int uid)3998 public abstract int checkPermission(@NonNull String permission, int pid, int uid); 3999 4000 /** @hide */ 4001 @PackageManager.PermissionResult checkPermission(@onNull String permission, int pid, int uid, IBinder callerToken)4002 public abstract int checkPermission(@NonNull String permission, int pid, int uid, 4003 IBinder callerToken); 4004 4005 /** 4006 * Determine whether the calling process of an IPC you are handling has been 4007 * granted a particular permission. This is basically the same as calling 4008 * {@link #checkPermission(String, int, int)} with the pid and uid returned 4009 * by {@link android.os.Binder#getCallingPid} and 4010 * {@link android.os.Binder#getCallingUid}. One important difference 4011 * is that if you are not currently processing an IPC, this function 4012 * will always fail. This is done to protect against accidentally 4013 * leaking permissions; you can use {@link #checkCallingOrSelfPermission} 4014 * to avoid this protection. 4015 * 4016 * @param permission The name of the permission being checked. 4017 * 4018 * @return {@link PackageManager#PERMISSION_GRANTED} if the calling 4019 * pid/uid is allowed that permission, or 4020 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4021 * 4022 * @see PackageManager#checkPermission(String, String) 4023 * @see #checkPermission 4024 * @see #checkCallingOrSelfPermission 4025 */ 4026 @CheckResult(suggest="#enforceCallingPermission(String,String)") 4027 @PackageManager.PermissionResult checkCallingPermission(@onNull String permission)4028 public abstract int checkCallingPermission(@NonNull String permission); 4029 4030 /** 4031 * Determine whether the calling process of an IPC <em>or you</em> have been 4032 * granted a particular permission. This is the same as 4033 * {@link #checkCallingPermission}, except it grants your own permissions 4034 * if you are not currently processing an IPC. Use with care! 4035 * 4036 * @param permission The name of the permission being checked. 4037 * 4038 * @return {@link PackageManager#PERMISSION_GRANTED} if the calling 4039 * pid/uid is allowed that permission, or 4040 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4041 * 4042 * @see PackageManager#checkPermission(String, String) 4043 * @see #checkPermission 4044 * @see #checkCallingPermission 4045 */ 4046 @CheckResult(suggest="#enforceCallingOrSelfPermission(String,String)") 4047 @PackageManager.PermissionResult checkCallingOrSelfPermission(@onNull String permission)4048 public abstract int checkCallingOrSelfPermission(@NonNull String permission); 4049 4050 /** 4051 * Determine whether <em>you</em> have been granted a particular permission. 4052 * 4053 * @param permission The name of the permission being checked. 4054 * 4055 * @return {@link PackageManager#PERMISSION_GRANTED} if you have the 4056 * permission, or {@link PackageManager#PERMISSION_DENIED} if not. 4057 * 4058 * @see PackageManager#checkPermission(String, String) 4059 * @see #checkCallingPermission(String) 4060 */ 4061 @PackageManager.PermissionResult checkSelfPermission(@onNull String permission)4062 public abstract int checkSelfPermission(@NonNull String permission); 4063 4064 /** 4065 * If the given permission is not allowed for a particular process 4066 * and user ID running in the system, throw a {@link SecurityException}. 4067 * 4068 * @param permission The name of the permission being checked. 4069 * @param pid The process ID being checked against. Must be > 0. 4070 * @param uid The user ID being checked against. A uid of 0 is the root 4071 * user, which will pass every permission check. 4072 * @param message A message to include in the exception if it is thrown. 4073 * 4074 * @see #checkPermission(String, int, int) 4075 */ enforcePermission( @onNull String permission, int pid, int uid, @Nullable String message)4076 public abstract void enforcePermission( 4077 @NonNull String permission, int pid, int uid, @Nullable String message); 4078 4079 /** 4080 * If the calling process of an IPC you are handling has not been 4081 * granted a particular permission, throw a {@link 4082 * SecurityException}. This is basically the same as calling 4083 * {@link #enforcePermission(String, int, int, String)} with the 4084 * pid and uid returned by {@link android.os.Binder#getCallingPid} 4085 * and {@link android.os.Binder#getCallingUid}. One important 4086 * difference is that if you are not currently processing an IPC, 4087 * this function will always throw the SecurityException. This is 4088 * done to protect against accidentally leaking permissions; you 4089 * can use {@link #enforceCallingOrSelfPermission} to avoid this 4090 * protection. 4091 * 4092 * @param permission The name of the permission being checked. 4093 * @param message A message to include in the exception if it is thrown. 4094 * 4095 * @see #checkCallingPermission(String) 4096 */ enforceCallingPermission( @onNull String permission, @Nullable String message)4097 public abstract void enforceCallingPermission( 4098 @NonNull String permission, @Nullable String message); 4099 4100 /** 4101 * If neither you nor the calling process of an IPC you are 4102 * handling has been granted a particular permission, throw a 4103 * {@link SecurityException}. This is the same as {@link 4104 * #enforceCallingPermission}, except it grants your own 4105 * permissions if you are not currently processing an IPC. Use 4106 * with care! 4107 * 4108 * @param permission The name of the permission being checked. 4109 * @param message A message to include in the exception if it is thrown. 4110 * 4111 * @see #checkCallingOrSelfPermission(String) 4112 */ enforceCallingOrSelfPermission( @onNull String permission, @Nullable String message)4113 public abstract void enforceCallingOrSelfPermission( 4114 @NonNull String permission, @Nullable String message); 4115 4116 /** 4117 * Grant permission to access a specific Uri to another package, regardless 4118 * of whether that package has general permission to access the Uri's 4119 * content provider. This can be used to grant specific, temporary 4120 * permissions, typically in response to user interaction (such as the 4121 * user opening an attachment that you would like someone else to 4122 * display). 4123 * 4124 * <p>Normally you should use {@link Intent#FLAG_GRANT_READ_URI_PERMISSION 4125 * Intent.FLAG_GRANT_READ_URI_PERMISSION} or 4126 * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION 4127 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} with the Intent being used to 4128 * start an activity instead of this function directly. If you use this 4129 * function directly, you should be sure to call 4130 * {@link #revokeUriPermission} when the target should no longer be allowed 4131 * to access it. 4132 * 4133 * <p>To succeed, the content provider owning the Uri must have set the 4134 * {@link android.R.styleable#AndroidManifestProvider_grantUriPermissions 4135 * grantUriPermissions} attribute in its manifest or included the 4136 * {@link android.R.styleable#AndroidManifestGrantUriPermission 4137 * <grant-uri-permissions>} tag. 4138 * 4139 * @param toPackage The package you would like to allow to access the Uri. 4140 * @param uri The Uri you would like to grant access to. 4141 * @param modeFlags The desired access modes. 4142 * 4143 * @see #revokeUriPermission 4144 */ grantUriPermission(String toPackage, Uri uri, @Intent.GrantUriMode int modeFlags)4145 public abstract void grantUriPermission(String toPackage, Uri uri, 4146 @Intent.GrantUriMode int modeFlags); 4147 4148 /** 4149 * Remove all permissions to access a particular content provider Uri 4150 * that were previously added with {@link #grantUriPermission} or <em>any other</em> mechanism. 4151 * The given Uri will match all previously granted Uris that are the same or a 4152 * sub-path of the given Uri. That is, revoking "content://foo/target" will 4153 * revoke both "content://foo/target" and "content://foo/target/sub", but not 4154 * "content://foo". It will not remove any prefix grants that exist at a 4155 * higher level. 4156 * 4157 * <p>Prior to {@link android.os.Build.VERSION_CODES#LOLLIPOP}, if you did not have 4158 * regular permission access to a Uri, but had received access to it through 4159 * a specific Uri permission grant, you could not revoke that grant with this 4160 * function and a {@link SecurityException} would be thrown. As of 4161 * {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this function will not throw a security 4162 * exception, but will remove whatever permission grants to the Uri had been given to the app 4163 * (or none).</p> 4164 * 4165 * <p>Unlike {@link #revokeUriPermission(String, Uri, int)}, this method impacts all permission 4166 * grants matching the given Uri, for any package they had been granted to, through any 4167 * mechanism this had happened (such as indirectly through the clipboard, activity launch, 4168 * service start, etc). That means this can be potentially dangerous to use, as it can 4169 * revoke grants that another app could be strongly expecting to stick around.</p> 4170 * 4171 * @param uri The Uri you would like to revoke access to. 4172 * @param modeFlags The access modes to revoke. 4173 * 4174 * @see #grantUriPermission 4175 */ revokeUriPermission(Uri uri, @Intent.AccessUriMode int modeFlags)4176 public abstract void revokeUriPermission(Uri uri, @Intent.AccessUriMode int modeFlags); 4177 4178 /** 4179 * Remove permissions to access a particular content provider Uri 4180 * that were previously added with {@link #grantUriPermission} for a specific target 4181 * package. The given Uri will match all previously granted Uris that are the same or a 4182 * sub-path of the given Uri. That is, revoking "content://foo/target" will 4183 * revoke both "content://foo/target" and "content://foo/target/sub", but not 4184 * "content://foo". It will not remove any prefix grants that exist at a 4185 * higher level. 4186 * 4187 * <p>Unlike {@link #revokeUriPermission(Uri, int)}, this method will <em>only</em> 4188 * revoke permissions that had been explicitly granted through {@link #grantUriPermission} 4189 * and only for the package specified. Any matching grants that have happened through 4190 * other mechanisms (clipboard, activity launching, service starting, etc) will not be 4191 * removed.</p> 4192 * 4193 * @param toPackage The package you had previously granted access to. 4194 * @param uri The Uri you would like to revoke access to. 4195 * @param modeFlags The access modes to revoke. 4196 * 4197 * @see #grantUriPermission 4198 */ revokeUriPermission(String toPackage, Uri uri, @Intent.AccessUriMode int modeFlags)4199 public abstract void revokeUriPermission(String toPackage, Uri uri, 4200 @Intent.AccessUriMode int modeFlags); 4201 4202 /** 4203 * Determine whether a particular process and user ID has been granted 4204 * permission to access a specific URI. This only checks for permissions 4205 * that have been explicitly granted -- if the given process/uid has 4206 * more general access to the URI's content provider then this check will 4207 * always fail. 4208 * 4209 * @param uri The uri that is being checked. 4210 * @param pid The process ID being checked against. Must be > 0. 4211 * @param uid The user ID being checked against. A uid of 0 is the root 4212 * user, which will pass every permission check. 4213 * @param modeFlags The access modes to check. 4214 * 4215 * @return {@link PackageManager#PERMISSION_GRANTED} if the given 4216 * pid/uid is allowed to access that uri, or 4217 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4218 * 4219 * @see #checkCallingUriPermission 4220 */ 4221 @CheckResult(suggest="#enforceUriPermission(Uri,int,int,String)") 4222 @PackageManager.PermissionResult checkUriPermission(Uri uri, int pid, int uid, @Intent.AccessUriMode int modeFlags)4223 public abstract int checkUriPermission(Uri uri, int pid, int uid, 4224 @Intent.AccessUriMode int modeFlags); 4225 4226 /** @hide */ 4227 @PackageManager.PermissionResult checkUriPermission(Uri uri, int pid, int uid, @Intent.AccessUriMode int modeFlags, IBinder callerToken)4228 public abstract int checkUriPermission(Uri uri, int pid, int uid, 4229 @Intent.AccessUriMode int modeFlags, IBinder callerToken); 4230 4231 /** 4232 * Determine whether the calling process and user ID has been 4233 * granted permission to access a specific URI. This is basically 4234 * the same as calling {@link #checkUriPermission(Uri, int, int, 4235 * int)} with the pid and uid returned by {@link 4236 * android.os.Binder#getCallingPid} and {@link 4237 * android.os.Binder#getCallingUid}. One important difference is 4238 * that if you are not currently processing an IPC, this function 4239 * will always fail. 4240 * 4241 * @param uri The uri that is being checked. 4242 * @param modeFlags The access modes to check. 4243 * 4244 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 4245 * is allowed to access that uri, or 4246 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4247 * 4248 * @see #checkUriPermission(Uri, int, int, int) 4249 */ 4250 @CheckResult(suggest="#enforceCallingUriPermission(Uri,int,String)") 4251 @PackageManager.PermissionResult checkCallingUriPermission(Uri uri, @Intent.AccessUriMode int modeFlags)4252 public abstract int checkCallingUriPermission(Uri uri, @Intent.AccessUriMode int modeFlags); 4253 4254 /** 4255 * Determine whether the calling process of an IPC <em>or you</em> has been granted 4256 * permission to access a specific URI. This is the same as 4257 * {@link #checkCallingUriPermission}, except it grants your own permissions 4258 * if you are not currently processing an IPC. Use with care! 4259 * 4260 * @param uri The uri that is being checked. 4261 * @param modeFlags The access modes to check. 4262 * 4263 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 4264 * is allowed to access that uri, or 4265 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4266 * 4267 * @see #checkCallingUriPermission 4268 */ 4269 @CheckResult(suggest="#enforceCallingOrSelfUriPermission(Uri,int,String)") 4270 @PackageManager.PermissionResult checkCallingOrSelfUriPermission(Uri uri, @Intent.AccessUriMode int modeFlags)4271 public abstract int checkCallingOrSelfUriPermission(Uri uri, 4272 @Intent.AccessUriMode int modeFlags); 4273 4274 /** 4275 * Check both a Uri and normal permission. This allows you to perform 4276 * both {@link #checkPermission} and {@link #checkUriPermission} in one 4277 * call. 4278 * 4279 * @param uri The Uri whose permission is to be checked, or null to not 4280 * do this check. 4281 * @param readPermission The permission that provides overall read access, 4282 * or null to not do this check. 4283 * @param writePermission The permission that provides overall write 4284 * access, or null to not do this check. 4285 * @param pid The process ID being checked against. Must be > 0. 4286 * @param uid The user ID being checked against. A uid of 0 is the root 4287 * user, which will pass every permission check. 4288 * @param modeFlags The access modes to check. 4289 * 4290 * @return {@link PackageManager#PERMISSION_GRANTED} if the caller 4291 * is allowed to access that uri or holds one of the given permissions, or 4292 * {@link PackageManager#PERMISSION_DENIED} if it is not. 4293 */ 4294 @CheckResult(suggest="#enforceUriPermission(Uri,String,String,int,int,int,String)") 4295 @PackageManager.PermissionResult checkUriPermission(@ullable Uri uri, @Nullable String readPermission, @Nullable String writePermission, int pid, int uid, @Intent.AccessUriMode int modeFlags)4296 public abstract int checkUriPermission(@Nullable Uri uri, @Nullable String readPermission, 4297 @Nullable String writePermission, int pid, int uid, 4298 @Intent.AccessUriMode int modeFlags); 4299 4300 /** 4301 * If a particular process and user ID has not been granted 4302 * permission to access a specific URI, throw {@link 4303 * SecurityException}. This only checks for permissions that have 4304 * been explicitly granted -- if the given process/uid has more 4305 * general access to the URI's content provider then this check 4306 * will always fail. 4307 * 4308 * @param uri The uri that is being checked. 4309 * @param pid The process ID being checked against. Must be > 0. 4310 * @param uid The user ID being checked against. A uid of 0 is the root 4311 * user, which will pass every permission check. 4312 * @param modeFlags The access modes to enforce. 4313 * @param message A message to include in the exception if it is thrown. 4314 * 4315 * @see #checkUriPermission(Uri, int, int, int) 4316 */ enforceUriPermission( Uri uri, int pid, int uid, @Intent.AccessUriMode int modeFlags, String message)4317 public abstract void enforceUriPermission( 4318 Uri uri, int pid, int uid, @Intent.AccessUriMode int modeFlags, String message); 4319 4320 /** 4321 * If the calling process and user ID has not been granted 4322 * permission to access a specific URI, throw {@link 4323 * SecurityException}. This is basically the same as calling 4324 * {@link #enforceUriPermission(Uri, int, int, int, String)} with 4325 * the pid and uid returned by {@link 4326 * android.os.Binder#getCallingPid} and {@link 4327 * android.os.Binder#getCallingUid}. One important difference is 4328 * that if you are not currently processing an IPC, this function 4329 * will always throw a SecurityException. 4330 * 4331 * @param uri The uri that is being checked. 4332 * @param modeFlags The access modes to enforce. 4333 * @param message A message to include in the exception if it is thrown. 4334 * 4335 * @see #checkCallingUriPermission(Uri, int) 4336 */ enforceCallingUriPermission( Uri uri, @Intent.AccessUriMode int modeFlags, String message)4337 public abstract void enforceCallingUriPermission( 4338 Uri uri, @Intent.AccessUriMode int modeFlags, String message); 4339 4340 /** 4341 * If the calling process of an IPC <em>or you</em> has not been 4342 * granted permission to access a specific URI, throw {@link 4343 * SecurityException}. This is the same as {@link 4344 * #enforceCallingUriPermission}, except it grants your own 4345 * permissions if you are not currently processing an IPC. Use 4346 * with care! 4347 * 4348 * @param uri The uri that is being checked. 4349 * @param modeFlags The access modes to enforce. 4350 * @param message A message to include in the exception if it is thrown. 4351 * 4352 * @see #checkCallingOrSelfUriPermission(Uri, int) 4353 */ enforceCallingOrSelfUriPermission( Uri uri, @Intent.AccessUriMode int modeFlags, String message)4354 public abstract void enforceCallingOrSelfUriPermission( 4355 Uri uri, @Intent.AccessUriMode int modeFlags, String message); 4356 4357 /** 4358 * Enforce both a Uri and normal permission. This allows you to perform 4359 * both {@link #enforcePermission} and {@link #enforceUriPermission} in one 4360 * call. 4361 * 4362 * @param uri The Uri whose permission is to be checked, or null to not 4363 * do this check. 4364 * @param readPermission The permission that provides overall read access, 4365 * or null to not do this check. 4366 * @param writePermission The permission that provides overall write 4367 * access, or null to not do this check. 4368 * @param pid The process ID being checked against. Must be > 0. 4369 * @param uid The user ID being checked against. A uid of 0 is the root 4370 * user, which will pass every permission check. 4371 * @param modeFlags The access modes to enforce. 4372 * @param message A message to include in the exception if it is thrown. 4373 * 4374 * @see #checkUriPermission(Uri, String, String, int, int, int) 4375 */ enforceUriPermission( @ullable Uri uri, @Nullable String readPermission, @Nullable String writePermission, int pid, int uid, @Intent.AccessUriMode int modeFlags, @Nullable String message)4376 public abstract void enforceUriPermission( 4377 @Nullable Uri uri, @Nullable String readPermission, 4378 @Nullable String writePermission, int pid, int uid, @Intent.AccessUriMode int modeFlags, 4379 @Nullable String message); 4380 4381 /** @hide */ 4382 @IntDef(flag = true, prefix = { "CONTEXT_" }, value = { 4383 CONTEXT_INCLUDE_CODE, 4384 CONTEXT_IGNORE_SECURITY, 4385 CONTEXT_RESTRICTED, 4386 CONTEXT_DEVICE_PROTECTED_STORAGE, 4387 CONTEXT_CREDENTIAL_PROTECTED_STORAGE, 4388 CONTEXT_REGISTER_PACKAGE, 4389 }) 4390 @Retention(RetentionPolicy.SOURCE) 4391 public @interface CreatePackageOptions {} 4392 4393 /** 4394 * Flag for use with {@link #createPackageContext}: include the application 4395 * code with the context. This means loading code into the caller's 4396 * process, so that {@link #getClassLoader()} can be used to instantiate 4397 * the application's classes. Setting this flags imposes security 4398 * restrictions on what application context you can access; if the 4399 * requested application can not be safely loaded into your process, 4400 * java.lang.SecurityException will be thrown. If this flag is not set, 4401 * there will be no restrictions on the packages that can be loaded, 4402 * but {@link #getClassLoader} will always return the default system 4403 * class loader. 4404 */ 4405 public static final int CONTEXT_INCLUDE_CODE = 0x00000001; 4406 4407 /** 4408 * Flag for use with {@link #createPackageContext}: ignore any security 4409 * restrictions on the Context being requested, allowing it to always 4410 * be loaded. For use with {@link #CONTEXT_INCLUDE_CODE} to allow code 4411 * to be loaded into a process even when it isn't safe to do so. Use 4412 * with extreme care! 4413 */ 4414 public static final int CONTEXT_IGNORE_SECURITY = 0x00000002; 4415 4416 /** 4417 * Flag for use with {@link #createPackageContext}: a restricted context may 4418 * disable specific features. For instance, a View associated with a restricted 4419 * context would ignore particular XML attributes. 4420 */ 4421 public static final int CONTEXT_RESTRICTED = 0x00000004; 4422 4423 /** 4424 * Flag for use with {@link #createPackageContext}: point all file APIs at 4425 * device-protected storage. 4426 * 4427 * @hide 4428 */ 4429 public static final int CONTEXT_DEVICE_PROTECTED_STORAGE = 0x00000008; 4430 4431 /** 4432 * Flag for use with {@link #createPackageContext}: point all file APIs at 4433 * credential-protected storage. 4434 * 4435 * @hide 4436 */ 4437 public static final int CONTEXT_CREDENTIAL_PROTECTED_STORAGE = 0x00000010; 4438 4439 /** 4440 * @hide Used to indicate we should tell the activity manager about the process 4441 * loading this code. 4442 */ 4443 public static final int CONTEXT_REGISTER_PACKAGE = 0x40000000; 4444 4445 /** 4446 * Return a new Context object for the given application name. This 4447 * Context is the same as what the named application gets when it is 4448 * launched, containing the same resources and class loader. Each call to 4449 * this method returns a new instance of a Context object; Context objects 4450 * are not shared, however they share common state (Resources, ClassLoader, 4451 * etc) so the Context instance itself is fairly lightweight. 4452 * 4453 * <p>Throws {@link android.content.pm.PackageManager.NameNotFoundException} if there is no 4454 * application with the given package name. 4455 * 4456 * <p>Throws {@link java.lang.SecurityException} if the Context requested 4457 * can not be loaded into the caller's process for security reasons (see 4458 * {@link #CONTEXT_INCLUDE_CODE} for more information}. 4459 * 4460 * @param packageName Name of the application's package. 4461 * @param flags Option flags. 4462 * 4463 * @return A {@link Context} for the application. 4464 * 4465 * @throws SecurityException 4466 * @throws PackageManager.NameNotFoundException if there is no application with 4467 * the given package name. 4468 */ createPackageContext(String packageName, @CreatePackageOptions int flags)4469 public abstract Context createPackageContext(String packageName, 4470 @CreatePackageOptions int flags) throws PackageManager.NameNotFoundException; 4471 4472 /** 4473 * Similar to {@link #createPackageContext(String, int)}, but with a 4474 * different {@link UserHandle}. For example, {@link #getContentResolver()} 4475 * will open any {@link Uri} as the given user. 4476 * 4477 * @hide 4478 */ createPackageContextAsUser( String packageName, @CreatePackageOptions int flags, UserHandle user)4479 public abstract Context createPackageContextAsUser( 4480 String packageName, @CreatePackageOptions int flags, UserHandle user) 4481 throws PackageManager.NameNotFoundException; 4482 4483 /** 4484 * Creates a context given an {@link android.content.pm.ApplicationInfo}. 4485 * 4486 * @hide 4487 */ createApplicationContext(ApplicationInfo application, @CreatePackageOptions int flags)4488 public abstract Context createApplicationContext(ApplicationInfo application, 4489 @CreatePackageOptions int flags) throws PackageManager.NameNotFoundException; 4490 4491 /** 4492 * Return a new Context object for the given split name. The new Context has a ClassLoader and 4493 * Resources object that can access the split's and all of its dependencies' code/resources. 4494 * Each call to this method returns a new instance of a Context object; 4495 * Context objects are not shared, however common state (ClassLoader, other Resources for 4496 * the same split) may be so the Context itself can be fairly lightweight. 4497 * 4498 * @param splitName The name of the split to include, as declared in the split's 4499 * <code>AndroidManifest.xml</code>. 4500 * @return A {@link Context} with the given split's code and/or resources loaded. 4501 */ createContextForSplit(String splitName)4502 public abstract Context createContextForSplit(String splitName) 4503 throws PackageManager.NameNotFoundException; 4504 4505 /** 4506 * Get the userId associated with this context 4507 * @return user id 4508 * 4509 * @hide 4510 */ 4511 @TestApi getUserId()4512 public abstract @UserIdInt int getUserId(); 4513 4514 /** 4515 * Return a new Context object for the current Context but whose resources 4516 * are adjusted to match the given Configuration. Each call to this method 4517 * returns a new instance of a Context object; Context objects are not 4518 * shared, however common state (ClassLoader, other Resources for the 4519 * same configuration) may be so the Context itself can be fairly lightweight. 4520 * 4521 * @param overrideConfiguration A {@link Configuration} specifying what 4522 * values to modify in the base Configuration of the original Context's 4523 * resources. If the base configuration changes (such as due to an 4524 * orientation change), the resources of this context will also change except 4525 * for those that have been explicitly overridden with a value here. 4526 * 4527 * @return A {@link Context} with the given configuration override. 4528 */ createConfigurationContext( @onNull Configuration overrideConfiguration)4529 public abstract Context createConfigurationContext( 4530 @NonNull Configuration overrideConfiguration); 4531 4532 /** 4533 * Return a new Context object for the current Context but whose resources 4534 * are adjusted to match the metrics of the given Display. Each call to this method 4535 * returns a new instance of a Context object; Context objects are not 4536 * shared, however common state (ClassLoader, other Resources for the 4537 * same configuration) may be so the Context itself can be fairly lightweight. 4538 * 4539 * The returned display Context provides a {@link WindowManager} 4540 * (see {@link #getSystemService(String)}) that is configured to show windows 4541 * on the given display. The WindowManager's {@link WindowManager#getDefaultDisplay} 4542 * method can be used to retrieve the Display from the returned Context. 4543 * 4544 * @param display A {@link Display} object specifying the display 4545 * for whose metrics the Context's resources should be tailored and upon which 4546 * new windows should be shown. 4547 * 4548 * @return A {@link Context} for the display. 4549 */ createDisplayContext(@onNull Display display)4550 public abstract Context createDisplayContext(@NonNull Display display); 4551 4552 /** 4553 * Return a new Context object for the current Context but whose storage 4554 * APIs are backed by device-protected storage. 4555 * <p> 4556 * On devices with direct boot, data stored in this location is encrypted 4557 * with a key tied to the physical device, and it can be accessed 4558 * immediately after the device has booted successfully, both 4559 * <em>before and after</em> the user has authenticated with their 4560 * credentials (such as a lock pattern or PIN). 4561 * <p> 4562 * Because device-protected data is available without user authentication, 4563 * you should carefully limit the data you store using this Context. For 4564 * example, storing sensitive authentication tokens or passwords in the 4565 * device-protected area is strongly discouraged. 4566 * <p> 4567 * If the underlying device does not have the ability to store 4568 * device-protected and credential-protected data using different keys, then 4569 * both storage areas will become available at the same time. They remain as 4570 * two distinct storage locations on disk, and only the window of 4571 * availability changes. 4572 * <p> 4573 * Each call to this method returns a new instance of a Context object; 4574 * Context objects are not shared, however common state (ClassLoader, other 4575 * Resources for the same configuration) may be so the Context itself can be 4576 * fairly lightweight. 4577 * 4578 * @see #isDeviceProtectedStorage() 4579 */ createDeviceProtectedStorageContext()4580 public abstract Context createDeviceProtectedStorageContext(); 4581 4582 /** 4583 * Return a new Context object for the current Context but whose storage 4584 * APIs are backed by credential-protected storage. This is the default 4585 * storage area for apps unless 4586 * {@link android.R.attr#defaultToDeviceProtectedStorage} was requested. 4587 * <p> 4588 * On devices with direct boot, data stored in this location is encrypted 4589 * with a key tied to user credentials, which can be accessed 4590 * <em>only after</em> the user has entered their credentials (such as a 4591 * lock pattern or PIN). 4592 * <p> 4593 * If the underlying device does not have the ability to store 4594 * device-protected and credential-protected data using different keys, then 4595 * both storage areas will become available at the same time. They remain as 4596 * two distinct storage locations on disk, and only the window of 4597 * availability changes. 4598 * <p> 4599 * Each call to this method returns a new instance of a Context object; 4600 * Context objects are not shared, however common state (ClassLoader, other 4601 * Resources for the same configuration) may be so the Context itself can be 4602 * fairly lightweight. 4603 * 4604 * @see #isCredentialProtectedStorage() 4605 * @hide 4606 */ 4607 @SystemApi createCredentialProtectedStorageContext()4608 public abstract Context createCredentialProtectedStorageContext(); 4609 4610 /** 4611 * Gets the display adjustments holder for this context. This information 4612 * is provided on a per-application or activity basis and is used to simulate lower density 4613 * display metrics for legacy applications and restricted screen sizes. 4614 * 4615 * @param displayId The display id for which to get compatibility info. 4616 * @return The compatibility info holder, or null if not required by the application. 4617 * @hide 4618 */ getDisplayAdjustments(int displayId)4619 public abstract DisplayAdjustments getDisplayAdjustments(int displayId); 4620 4621 /** 4622 * @hide 4623 */ getDisplay()4624 public abstract Display getDisplay(); 4625 4626 /** 4627 * @hide 4628 */ updateDisplay(int displayId)4629 public abstract void updateDisplay(int displayId); 4630 4631 /** 4632 * Indicates whether this Context is restricted. 4633 * 4634 * @return {@code true} if this Context is restricted, {@code false} otherwise. 4635 * 4636 * @see #CONTEXT_RESTRICTED 4637 */ isRestricted()4638 public boolean isRestricted() { 4639 return false; 4640 } 4641 4642 /** 4643 * Indicates if the storage APIs of this Context are backed by 4644 * device-protected storage. 4645 * 4646 * @see #createDeviceProtectedStorageContext() 4647 */ isDeviceProtectedStorage()4648 public abstract boolean isDeviceProtectedStorage(); 4649 4650 /** 4651 * Indicates if the storage APIs of this Context are backed by 4652 * credential-protected storage. 4653 * 4654 * @see #createCredentialProtectedStorageContext() 4655 * @hide 4656 */ 4657 @SystemApi isCredentialProtectedStorage()4658 public abstract boolean isCredentialProtectedStorage(); 4659 4660 /** 4661 * Returns true if the context can load unsafe resources, e.g. fonts. 4662 * @hide 4663 */ canLoadUnsafeResources()4664 public abstract boolean canLoadUnsafeResources(); 4665 4666 /** 4667 * @hide 4668 */ getActivityToken()4669 public IBinder getActivityToken() { 4670 throw new RuntimeException("Not implemented. Must override in a subclass."); 4671 } 4672 4673 /** 4674 * @hide 4675 */ 4676 @Nullable getServiceDispatcher(ServiceConnection conn, Handler handler, int flags)4677 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler, 4678 int flags) { 4679 throw new RuntimeException("Not implemented. Must override in a subclass."); 4680 } 4681 4682 /** 4683 * @hide 4684 */ getIApplicationThread()4685 public IApplicationThread getIApplicationThread() { 4686 throw new RuntimeException("Not implemented. Must override in a subclass."); 4687 } 4688 4689 /** 4690 * @hide 4691 */ getMainThreadHandler()4692 public Handler getMainThreadHandler() { 4693 throw new RuntimeException("Not implemented. Must override in a subclass."); 4694 } 4695 4696 /** 4697 * Throws an exception if the Context is using system resources, 4698 * which are non-runtime-overlay-themable and may show inconsistent UI. 4699 * @hide 4700 */ assertRuntimeOverlayThemable()4701 public void assertRuntimeOverlayThemable() { 4702 // Resources.getSystem() is a singleton and the only Resources not managed by 4703 // ResourcesManager; therefore Resources.getSystem() is not themable. 4704 if (getResources() == Resources.getSystem()) { 4705 throw new IllegalArgumentException("Non-UI context used to display UI; " 4706 + "get a UI context from ActivityThread#getSystemUiContext()"); 4707 } 4708 } 4709 } 4710