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