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