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