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