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