Context.java revision bf85d63cb8858432ff845250fdaffdb556d2ef1b
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.content.pm.PackageManager;
20import android.content.res.AssetManager;
21import android.content.res.Resources;
22import android.content.res.TypedArray;
23import android.database.sqlite.SQLiteDatabase;
24import android.database.sqlite.SQLiteDatabase.CursorFactory;
25import android.graphics.Bitmap;
26import android.graphics.drawable.Drawable;
27import android.net.Uri;
28import android.os.Bundle;
29import android.os.Handler;
30import android.os.Looper;
31import android.util.AttributeSet;
32
33import java.io.File;
34import java.io.FileInputStream;
35import java.io.FileNotFoundException;
36import java.io.FileOutputStream;
37import java.io.IOException;
38import java.io.InputStream;
39
40/**
41 * Interface to global information about an application environment.  This is
42 * an abstract class whose implementation is provided by
43 * the Android system.  It
44 * allows access to application-specific resources and classes, as well as
45 * up-calls for application-level operations such as launching activities,
46 * broadcasting and receiving intents, etc.
47 */
48public abstract class Context {
49    /**
50     * File creation mode: the default mode, where the created file can only
51     * be accessed by the calling application (or all applications sharing the
52     * same user ID).
53     * @see #MODE_WORLD_READABLE
54     * @see #MODE_WORLD_WRITEABLE
55     */
56    public static final int MODE_PRIVATE = 0x0000;
57    /**
58     * File creation mode: allow all other applications to have read access
59     * to the created file.
60     * @see #MODE_PRIVATE
61     * @see #MODE_WORLD_WRITEABLE
62     */
63    public static final int MODE_WORLD_READABLE = 0x0001;
64    /**
65     * File creation mode: allow all other applications to have write access
66     * to the created file.
67     * @see #MODE_PRIVATE
68     * @see #MODE_WORLD_READABLE
69     */
70    public static final int MODE_WORLD_WRITEABLE = 0x0002;
71    /**
72     * File creation mode: for use with {@link #openFileOutput}, if the file
73     * already exists then write data to the end of the existing file
74     * instead of erasing it.
75     * @see #openFileOutput
76     */
77    public static final int MODE_APPEND = 0x8000;
78
79    /**
80     * Flag for {@link #bindService}: automatically create the service as long
81     * as the binding exists.  Note that while this will create the service,
82     * its {@link android.app.Service#onStart} method will still only be called due to an
83     * explicit call to {@link #startService}.  Even without that, though,
84     * this still provides you with access to the service object while the
85     * service is created.
86     *
87     * <p>Specifying this flag also tells the system to treat the service
88     * as being as important as your own process -- that is, when deciding
89     * which process should be killed to free memory, the service will only
90     * be considered a candidate as long as the processes of any such bindings
91     * is also a candidate to be killed.  This is to avoid situations where
92     * the service is being continually created and killed due to low memory.
93     */
94    public static final int BIND_AUTO_CREATE = 0x0001;
95
96    /**
97     * Flag for {@link #bindService}: include debugging help for mismatched
98     * calls to unbind.  When this flag is set, the callstack of the following
99     * {@link #unbindService} call is retained, to be printed if a later
100     * incorrect unbind call is made.  Note that doing this requires retaining
101     * information about the binding that was made for the lifetime of the app,
102     * resulting in a leak -- this should only be used for debugging.
103     */
104    public static final int BIND_DEBUG_UNBIND = 0x0002;
105
106    /** Return an AssetManager instance for your application's package. */
107    public abstract AssetManager getAssets();
108
109    /** Return a Resources instance for your application's package. */
110    public abstract Resources getResources();
111
112    /** Return PackageManager instance to find global package information. */
113    public abstract PackageManager getPackageManager();
114
115    /** Return a ContentResolver instance for your application's package. */
116    public abstract ContentResolver getContentResolver();
117
118    /**
119     * Return the Looper for the main thread of the current process.  This is
120     * the thread used to dispatch calls to application components (activities,
121     * services, etc).
122     */
123    public abstract Looper getMainLooper();
124
125    /**
126     * Return the context of the single, global Application object of the
127     * current process.
128     */
129    public abstract Context getApplicationContext();
130
131    /**
132     * Return a localized, styled CharSequence from the application's package's
133     * default string table.
134     *
135     * @param resId Resource id for the CharSequence text
136     */
137    public final CharSequence getText(int resId) {
138        return getResources().getText(resId);
139    }
140
141    /**
142     * Return a localized string from the application's package's
143     * default string table.
144     *
145     * @param resId Resource id for the string
146     */
147    public final String getString(int resId) {
148        return getResources().getString(resId);
149    }
150
151    /**
152     * Return a localized formatted string from the application's package's
153     * default string table, substituting the format arguments as defined in
154     * {@link java.util.Formatter} and {@link java.lang.String#format}.
155     *
156     * @param resId Resource id for the format string
157     * @param formatArgs The format arguments that will be used for substitution.
158     */
159
160    public final String getString(int resId, Object... formatArgs) {
161        return getResources().getString(resId, formatArgs);
162    }
163
164     /**
165     * Set the base theme for this context.  Note that this should be called
166     * before any views are instantiated in the Context (for example before
167     * calling {@link android.app.Activity#setContentView} or
168     * {@link android.view.LayoutInflater#inflate}).
169     *
170     * @param resid The style resource describing the theme.
171     */
172    public abstract void setTheme(int resid);
173
174    /**
175     * Return the Theme object associated with this Context.
176     */
177    public abstract Resources.Theme getTheme();
178
179    /**
180     * Retrieve styled attribute information in this Context's theme.  See
181     * {@link Resources.Theme#obtainStyledAttributes(int[])}
182     * for more information.
183     *
184     * @see Resources.Theme#obtainStyledAttributes(int[])
185     */
186    public final TypedArray obtainStyledAttributes(
187            int[] attrs) {
188        return getTheme().obtainStyledAttributes(attrs);
189    }
190
191    /**
192     * Retrieve styled attribute information in this Context's theme.  See
193     * {@link Resources.Theme#obtainStyledAttributes(int, int[])}
194     * for more information.
195     *
196     * @see Resources.Theme#obtainStyledAttributes(int, int[])
197     */
198    public final TypedArray obtainStyledAttributes(
199            int resid, int[] attrs) throws Resources.NotFoundException {
200        return getTheme().obtainStyledAttributes(resid, attrs);
201    }
202
203    /**
204     * Retrieve styled attribute information in this Context's theme.  See
205     * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}
206     * for more information.
207     *
208     * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)
209     */
210    public final TypedArray obtainStyledAttributes(
211            AttributeSet set, int[] attrs) {
212        return getTheme().obtainStyledAttributes(set, attrs, 0, 0);
213    }
214
215    /**
216     * Retrieve styled attribute information in this Context's theme.  See
217     * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}
218     * for more information.
219     *
220     * @see Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)
221     */
222    public final TypedArray obtainStyledAttributes(
223            AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) {
224        return getTheme().obtainStyledAttributes(
225            set, attrs, defStyleAttr, defStyleRes);
226    }
227
228    /**
229     * Return a class loader you can use to retrieve classes in this package.
230     */
231    public abstract ClassLoader getClassLoader();
232
233    /** Return the name of this application's package. */
234    public abstract String getPackageName();
235
236    /**
237     * {@hide}
238     * Return the full path to this context's resource files.  This is the ZIP files
239     * containing the application's resources.
240     *
241     * <p>Note: this is not generally useful for applications, since they should
242     * not be directly accessing the file system.
243     *
244     *
245     * @return String Path to the resources.
246     */
247    public abstract String getPackageResourcePath();
248
249    /**
250     * {@hide}
251     * Return the full path to this context's code and asset files.  This is the ZIP files
252     * containing the application's code and assets.
253     *
254     * <p>Note: this is not generally useful for applications, since they should
255     * not be directly accessing the file system.
256     *
257     * @return String Path to the code and assets.
258     */
259    public abstract String getPackageCodePath();
260
261    /**
262     * {@hide}
263     * Return the full path to the shared prefs file for the given prefs group name.
264     *
265     * <p>Note: this is not generally useful for applications, since they should
266     * not be directly accessing the file system.
267     */
268    public abstract File getSharedPrefsFile(String name);
269
270    /**
271     * Retrieve and hold the contents of the preferences file 'name', returning
272     * a SharedPreferences through which you can retrieve and modify its
273     * values.  Only one instance of the SharedPreferences object is returned
274     * to any callers for the same name, meaning they will see each other's
275     * edits as soon as they are made.
276     *
277     * @param name Desired preferences file. If a preferences file by this name
278     * does not exist, it will be created when you retrieve an
279     * editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
280     * @param mode Operating mode.  Use 0 or {@link #MODE_PRIVATE} for the
281     * default operation, {@link #MODE_WORLD_READABLE}
282     * and {@link #MODE_WORLD_WRITEABLE} to control permissions.
283     *
284     * @return Returns the single SharedPreferences instance that can be used
285     *         to retrieve and modify the preference values.
286     *
287     * @see #MODE_PRIVATE
288     * @see #MODE_WORLD_READABLE
289     * @see #MODE_WORLD_WRITEABLE
290     */
291    public abstract SharedPreferences getSharedPreferences(String name,
292            int mode);
293
294    /**
295     * Open a private file associated with this Context's application package
296     * for reading.
297     *
298     * @param name The name of the file to open; can not contain path
299     *             separators.
300     *
301     * @return FileInputStream Resulting input stream.
302     *
303     * @see #openFileOutput
304     * @see #fileList
305     * @see #deleteFile
306     * @see java.io.FileInputStream#FileInputStream(String)
307     */
308    public abstract FileInputStream openFileInput(String name)
309        throws FileNotFoundException;
310
311    /**
312     * Open a private file associated with this Context's application package
313     * for writing.  Creates the file if it doesn't already exist.
314     *
315     * @param name The name of the file to open; can not contain path
316     *             separators.
317     * @param mode Operating mode.  Use 0 or {@link #MODE_PRIVATE} for the
318     * default operation, {@link #MODE_APPEND} to append to an existing file,
319     * {@link #MODE_WORLD_READABLE} and {@link #MODE_WORLD_WRITEABLE} to control
320     * permissions.
321     *
322     * @return FileOutputStream Resulting output stream.
323     *
324     * @see #MODE_APPEND
325     * @see #MODE_PRIVATE
326     * @see #MODE_WORLD_READABLE
327     * @see #MODE_WORLD_WRITEABLE
328     * @see #openFileInput
329     * @see #fileList
330     * @see #deleteFile
331     * @see java.io.FileOutputStream#FileOutputStream(String)
332     */
333    public abstract FileOutputStream openFileOutput(String name, int mode)
334        throws FileNotFoundException;
335
336    /**
337     * Delete the given private file associated with this Context's
338     * application package.
339     *
340     * @param name The name of the file to delete; can not contain path
341     *             separators.
342     *
343     * @return True if the file was successfully deleted; else
344     *         false.
345     *
346     * @see #openFileInput
347     * @see #openFileOutput
348     * @see #fileList
349     * @see java.io.File#delete()
350     */
351    public abstract boolean deleteFile(String name);
352
353    /**
354     * Returns the absolute path on the filesystem where a file created with
355     * {@link #openFileOutput} is stored.
356     *
357     * @param name The name of the file for which you would like to get
358     *          its path.
359     *
360     * @return Returns an absolute path to the given file.
361     *
362     * @see #openFileOutput
363     * @see #getFilesDir
364     * @see #getDir
365     */
366    public abstract File getFileStreamPath(String name);
367
368    /**
369     * Returns the absolute path to the directory on the filesystem where
370     * files created with {@link #openFileOutput} are stored.
371     *
372     * @return Returns the path of the directory holding application files.
373     *
374     * @see #openFileOutput
375     * @see #getFileStreamPath
376     * @see #getDir
377     */
378    public abstract File getFilesDir();
379
380    /**
381     * Returns the absolute path to the application specific cache directory
382     * on the filesystem. These files will be ones that get deleted first when the
383     * device runs low on storage
384     * There is no guarantee when these files will be deleted.
385     *
386     * @return Returns the path of the directory holding application cache files.
387     *
388     * @see #openFileOutput
389     * @see #getFileStreamPath
390     * @see #getDir
391     */
392    public abstract File getCacheDir();
393
394    /**
395     * Returns an array of strings naming the private files associated with
396     * this Context's application package.
397     *
398     * @return Array of strings naming the private files.
399     *
400     * @see #openFileInput
401     * @see #openFileOutput
402     * @see #deleteFile
403     */
404    public abstract String[] fileList();
405
406    /**
407     * Retrieve, creating if needed, a new directory in which the application
408     * can place its own custom data files.  You can use the returned File
409     * object to create and access files in this directory.  Note that files
410     * created through a File object will only be accessible by your own
411     * application; you can only set the mode of the entire directory, not
412     * of individual files.
413     *
414     * @param name Name of the directory to retrieve.  This is a directory
415     * that is created as part of your application data.
416     * @param mode Operating mode.  Use 0 or {@link #MODE_PRIVATE} for the
417     * default operation, {@link #MODE_WORLD_READABLE} and
418     * {@link #MODE_WORLD_WRITEABLE} to control permissions.
419     *
420     * @return Returns a File object for the requested directory.  The directory
421     * will have been created if it does not already exist.
422     *
423     * @see #openFileOutput(String, int)
424     */
425    public abstract File getDir(String name, int mode);
426
427    /**
428     * Open a new private SQLiteDatabase associated with this Context's
429     * application package.  Create the database file if it doesn't exist.
430     *
431     * @param name The name (unique in the application package) of the database.
432     * @param mode Operating mode.  Use 0 or {@link #MODE_PRIVATE} for the
433     *     default operation, {@link #MODE_WORLD_READABLE}
434     *     and {@link #MODE_WORLD_WRITEABLE} to control permissions.
435     * @param factory An optional factory class that is called to instantiate a
436     *     cursor when query is called.
437     *
438     * @return The contents of a newly created database with the given name.
439     * @throws android.database.sqlite.SQLiteException if the database file could not be opened.
440     *
441     * @see #MODE_PRIVATE
442     * @see #MODE_WORLD_READABLE
443     * @see #MODE_WORLD_WRITEABLE
444     * @see #deleteDatabase
445     */
446    public abstract SQLiteDatabase openOrCreateDatabase(String name,
447            int mode, CursorFactory factory);
448
449    /**
450     * Delete an existing private SQLiteDatabase associated with this Context's
451     * application package.
452     *
453     * @param name The name (unique in the application package) of the
454     *             database.
455     *
456     * @return True if the database was successfully deleted; else false.
457     *
458     * @see #openOrCreateDatabase
459     */
460    public abstract boolean deleteDatabase(String name);
461
462    /**
463     * Returns the absolute path on the filesystem where a database created with
464     * {@link #openOrCreateDatabase} is stored.
465     *
466     * @param name The name of the database for which you would like to get
467     *          its path.
468     *
469     * @return Returns an absolute path to the given database.
470     *
471     * @see #openOrCreateDatabase
472     */
473    public abstract File getDatabasePath(String name);
474
475    /**
476     * Returns an array of strings naming the private databases associated with
477     * this Context's application package.
478     *
479     * @return Array of strings naming the private databases.
480     *
481     * @see #openOrCreateDatabase
482     * @see #deleteDatabase
483     */
484    public abstract String[] databaseList();
485
486    /**
487     * Like {@link #peekWallpaper}, but always returns a valid Drawable.  If
488     * no wallpaper is set, the system default wallpaper is returned.
489     *
490     * @return Returns a Drawable object that will draw the wallpaper.
491     */
492    public abstract Drawable getWallpaper();
493
494    /**
495     * Retrieve the current system wallpaper.  This is returned as an
496     * abstract Drawable that you can install in a View to display whatever
497     * wallpaper the user has currently set.  If there is no wallpaper set,
498     * a null pointer is returned.
499     *
500     * @return Returns a Drawable object that will draw the wallpaper or a
501     * null pointer if these is none.
502     */
503    public abstract Drawable peekWallpaper();
504
505    /**
506     * Returns the desired minimum width for the wallpaper. Callers of
507     * {@link #setWallpaper(android.graphics.Bitmap)} or
508     * {@link #setWallpaper(java.io.InputStream)} should check this value
509     * beforehand to make sure the supplied wallpaper respects the desired
510     * minimum width.
511     *
512     * If the returned value is <= 0, the caller should use the width of
513     * the default display instead.
514     *
515     * @return The desired minimum width for the wallpaper. This value should
516     * be honored by applications that set the wallpaper but it is not
517     * mandatory.
518     */
519    public abstract int getWallpaperDesiredMinimumWidth();
520
521    /**
522     * Returns the desired minimum height for the wallpaper. Callers of
523     * {@link #setWallpaper(android.graphics.Bitmap)} or
524     * {@link #setWallpaper(java.io.InputStream)} should check this value
525     * beforehand to make sure the supplied wallpaper respects the desired
526     * minimum height.
527     *
528     * If the returned value is <= 0, the caller should use the height of
529     * the default display instead.
530     *
531     * @return The desired minimum height for the wallpaper. This value should
532     * be honored by applications that set the wallpaper but it is not
533     * mandatory.
534     */
535    public abstract int getWallpaperDesiredMinimumHeight();
536
537    /**
538     * Change the current system wallpaper to a bitmap.  The given bitmap is
539     * converted to a PNG and stored as the wallpaper.  On success, the intent
540     * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
541     *
542     * @param bitmap The bitmap to save.
543     *
544     * @throws IOException If an error occurs reverting to the default
545     * wallpaper.
546     */
547    public abstract void setWallpaper(Bitmap bitmap) throws IOException;
548
549    /**
550     * Change the current system wallpaper to a specific byte stream.  The
551     * give InputStream is copied into persistent storage and will now be
552     * used as the wallpaper.  Currently it must be either a JPEG or PNG
553     * image.  On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
554     * is broadcast.
555     *
556     * @param data A stream containing the raw data to install as a wallpaper.
557     *
558     * @throws IOException If an error occurs reverting to the default
559     * wallpaper.
560     */
561    public abstract void setWallpaper(InputStream data) throws IOException;
562
563    /**
564     * Remove any currently set wallpaper, reverting to the system's default
565     * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
566     * is broadcast.
567     *
568     * @throws IOException If an error occurs reverting to the default
569     * wallpaper.
570     */
571    public abstract void clearWallpaper() throws IOException;
572
573    /**
574     * Launch a new activity.  You will not receive any information about when
575     * the activity exits.
576     *
577     * <p>Note that if this method is being called from outside of an
578     * {@link android.app.Activity} Context, then the Intent must include
579     * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag.  This is because,
580     * without being started from an existing Activity, there is no existing
581     * task in which to place the new activity and thus it needs to be placed
582     * in its own separate task.
583     *
584     * <p>This method throws {@link ActivityNotFoundException}
585     * if there was no Activity found to run the given Intent.
586     *
587     * @param intent The description of the activity to start.
588     *
589     * @throws ActivityNotFoundException
590     *
591     * @see PackageManager#resolveActivity
592     */
593    public abstract void startActivity(Intent intent);
594
595    /**
596     * Broadcast the given intent to all interested BroadcastReceivers.  This
597     * call is asynchronous; it returns immediately, and you will continue
598     * executing while the receivers are run.  No results are propagated from
599     * receivers and receivers can not abort the broadcast. If you want
600     * to allow receivers to propagate results or abort the broadcast, you must
601     * send an ordered broadcast using
602     * {@link #sendOrderedBroadcast(Intent, String)}.
603     *
604     * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts.
605     *
606     * @param intent The Intent to broadcast; all receivers matching this
607     *               Intent will receive the broadcast.
608     *
609     * @see android.content.BroadcastReceiver
610     * @see #registerReceiver
611     * @see #sendBroadcast(Intent, String)
612     * @see #sendOrderedBroadcast(Intent, String)
613     * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle)
614     */
615    public abstract void sendBroadcast(Intent intent);
616
617    /**
618     * Broadcast the given intent to all interested BroadcastReceivers, allowing
619     * an optional required permission to be enforced.  This
620     * call is asynchronous; it returns immediately, and you will continue
621     * executing while the receivers are run.  No results are propagated from
622     * receivers and receivers can not abort the broadcast. If you want
623     * to allow receivers to propagate results or abort the broadcast, you must
624     * send an ordered broadcast using
625     * {@link #sendOrderedBroadcast(Intent, String)}.
626     *
627     * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts.
628     *
629     * @param intent The Intent to broadcast; all receivers matching this
630     *               Intent will receive the broadcast.
631     * @param receiverPermission (optional) String naming a permissions that
632     *               a receiver must hold in order to receive your broadcast.
633     *               If null, no permission is required.
634     *
635     * @see android.content.BroadcastReceiver
636     * @see #registerReceiver
637     * @see #sendBroadcast(Intent)
638     * @see #sendOrderedBroadcast(Intent, String)
639     * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle)
640     */
641    public abstract void sendBroadcast(Intent intent,
642            String receiverPermission);
643
644    /**
645     * Broadcast the given intent to all interested BroadcastReceivers, delivering
646     * them one at a time to allow more preferred receivers to consume the
647     * broadcast before it is delivered to less preferred receivers.  This
648     * call is asynchronous; it returns immediately, and you will continue
649     * executing while the receivers are run.
650     *
651     * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts.
652     *
653     * @param intent The Intent to broadcast; all receivers matching this
654     *               Intent will receive the broadcast.
655     * @param receiverPermission (optional) String naming a permissions that
656     *               a receiver must hold in order to receive your broadcast.
657     *               If null, no permission is required.
658     *
659     * @see android.content.BroadcastReceiver
660     * @see #registerReceiver
661     * @see #sendBroadcast(Intent)
662     * @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle)
663     */
664    public abstract void sendOrderedBroadcast(Intent intent,
665            String receiverPermission);
666
667    /**
668     * Version of {@link #sendBroadcast(Intent)} that allows you to
669     * receive data back from the broadcast.  This is accomplished by
670     * supplying your own BroadcastReceiver when calling, which will be
671     * treated as a final receiver at the end of the broadcast -- its
672     * {@link BroadcastReceiver#onReceive} method will be called with
673     * the result values collected from the other receivers.  If you use
674     * an <var>resultReceiver</var> with this method, then the broadcast will
675     * be serialized in the same way as calling
676     * {@link #sendOrderedBroadcast(Intent, String)}.
677     *
678     * <p>Like {@link #sendBroadcast(Intent)}, this method is
679     * asynchronous; it will return before
680     * resultReceiver.onReceive() is called.
681     *
682     * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts.
683     *
684     * @param intent The Intent to broadcast; all receivers matching this
685     *               Intent will receive the broadcast.
686     * @param receiverPermission String naming a permissions that
687     *               a receiver must hold in order to receive your broadcast.
688     *               If null, no permission is required.
689     * @param resultReceiver Your own BroadcastReceiver to treat as the final
690     *                       receiver of the broadcast.
691     * @param scheduler A custom Handler with which to schedule the
692     *                  resultReceiver callback; if null it will be
693     *                  scheduled in the Context's main thread.
694     * @param initialCode An initial value for the result code.  Often
695     *                    Activity.RESULT_OK.
696     * @param initialData An initial value for the result data.  Often
697     *                    null.
698     * @param initialExtras An initial value for the result extras.  Often
699     *                      null.
700     *
701     * @see #sendBroadcast(Intent)
702     * @see #sendBroadcast(Intent, String)
703     * @see #sendOrderedBroadcast(Intent, String)
704     * @see #sendStickyBroadcast(Intent)
705     * @see android.content.BroadcastReceiver
706     * @see #registerReceiver
707     * @see android.app.Activity#RESULT_OK
708     */
709    public abstract void sendOrderedBroadcast(Intent intent,
710            String receiverPermission, BroadcastReceiver resultReceiver,
711            Handler scheduler, int initialCode, String initialData,
712            Bundle initialExtras);
713
714    /**
715     * Perform a {@link #sendBroadcast(Intent)} that is "sticky," meaning the
716     * Intent you are sending stays around after the broadcast is complete,
717     * so that others can quickly retrieve that data through the return
718     * value of {@link #registerReceiver(BroadcastReceiver, IntentFilter)}.  In
719     * all other ways, this behaves the same as
720     * {@link #sendBroadcast(Intent)}.
721     *
722     * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY}
723     * permission in order to use this API.  If you do not hold that
724     * permission, {@link SecurityException} will be thrown.
725     *
726     * @param intent The Intent to broadcast; all receivers matching this
727     * Intent will receive the broadcast, and the Intent will be held to
728     * be re-broadcast to future receivers.
729     *
730     * @see #sendBroadcast(Intent)
731     */
732    public abstract void sendStickyBroadcast(Intent intent);
733
734    /**
735     * Remove the data previously sent with {@link #sendStickyBroadcast},
736     * so that it is as if the sticky broadcast had never happened.
737     *
738     * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY}
739     * permission in order to use this API.  If you do not hold that
740     * permission, {@link SecurityException} will be thrown.
741     *
742     * @param intent The Intent that was previously broadcast.
743     *
744     * @see #sendStickyBroadcast
745     */
746    public abstract void removeStickyBroadcast(Intent intent);
747
748    /**
749     * Register a BroadcastReceiver to be run in the main activity thread.  The
750     * <var>receiver</var> will be called with any broadcast Intent that
751     * matches <var>filter</var>, in the main application thread.
752     *
753     * <p>The system may broadcast Intents that are "sticky" -- these stay
754     * around after the broadcast as finished, to be sent to any later
755     * registrations. If your IntentFilter matches one of these sticky
756     * Intents, that Intent will be returned by this function
757     * <strong>and</strong> sent to your <var>receiver</var> as if it had just
758     * been broadcast.
759     *
760     * <p>There may be multiple sticky Intents that match <var>filter</var>,
761     * in which case each of these will be sent to <var>receiver</var>.  In
762     * this case, only one of these can be returned directly by the function;
763     * which of these that is returned is arbitrarily decided by the system.
764     *
765     * <p>If you know the Intent your are registering for is sticky, you can
766     * supply null for your <var>receiver</var>.  In this case, no receiver is
767     * registered -- the function simply returns the sticky Intent that
768     * matches <var>filter</var>.  In the case of multiple matches, the same
769     * rules as described above apply.
770     *
771     * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts.
772     *
773     * <p class="note">Note: this method <em>cannot be called from a
774     * {@link BroadcastReceiver} component;</em> that is, from a BroadcastReceiver
775     * that is declared in an application's manifest.  It is okay, however, to call
776     * this method from another BroadcastReceiver that has itself been registered
777     * at run time with {@link #registerReceiver}, since the lifetime of such a
778     * registered BroadcastReceiver is tied to the object that registered it.</p>
779     *
780     * @param receiver The BroadcastReceiver to handle the broadcast.
781     * @param filter Selects the Intent broadcasts to be received.
782     *
783     * @return The first sticky intent found that matches <var>filter</var>,
784     *         or null if there are none.
785     *
786     * @see #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler)
787     * @see #sendBroadcast
788     * @see #unregisterReceiver
789     */
790    public abstract Intent registerReceiver(BroadcastReceiver receiver,
791                                            IntentFilter filter);
792
793    /**
794     * Register to receive intent broadcasts, to run in the context of
795     * <var>scheduler</var>.  See
796     * {@link #registerReceiver(BroadcastReceiver, IntentFilter)} for more
797     * information.  This allows you to enforce permissions on who can
798     * broadcast intents to your receiver, or have the receiver run in
799     * a different thread than the main application thread.
800     *
801     * <p>See {@link BroadcastReceiver} for more information on Intent broadcasts.
802     *
803     * @param receiver The BroadcastReceiver to handle the broadcast.
804     * @param filter Selects the Intent broadcasts to be received.
805     * @param broadcastPermission String naming a permissions that a
806     *      broadcaster must hold in order to send an Intent to you.  If null,
807     *      no permission is required.
808     * @param scheduler Handler identifying the thread that will receive
809     *      the Intent.  If null, the main thread of the process will be used.
810     *
811     * @return The first sticky intent found that matches <var>filter</var>,
812     *         or null if there are none.
813     *
814     * @see #registerReceiver(BroadcastReceiver, IntentFilter)
815     * @see #sendBroadcast
816     * @see #unregisterReceiver
817     */
818    public abstract Intent registerReceiver(BroadcastReceiver receiver,
819                                            IntentFilter filter,
820                                            String broadcastPermission,
821                                            Handler scheduler);
822
823    /**
824     * Unregister a previously registered BroadcastReceiver.  <em>All</em>
825     * filters that have been registered for this BroadcastReceiver will be
826     * removed.
827     *
828     * @param receiver The BroadcastReceiver to unregister.
829     *
830     * @see #registerReceiver
831     */
832    public abstract void unregisterReceiver(BroadcastReceiver receiver);
833
834    /**
835     * Request that a given application service be started.  The Intent
836     * can either contain the complete class name of a specific service
837     * implementation to start, or an abstract definition through the
838     * action and other fields of the kind of service to start.  If this service
839     * is not already running, it will be instantiated and started (creating a
840     * process for it if needed); if it is running then it remains running.
841     *
842     * <p>Every call to this method will result in a corresponding call to
843     * the target service's {@link android.app.Service#onStart} method,
844     * with the <var>intent</var> given here.  This provides a convenient way
845     * to submit jobs to a service without having to bind and call on to its
846     * interface.
847     *
848     * <p>Using startService() overrides the default service lifetime that is
849     * managed by {@link #bindService}: it requires the service to remain
850     * running until {@link #stopService} is called, regardless of whether
851     * any clients are connected to it.  Note that calls to startService()
852     * are not nesting: no matter how many times you call startService(),
853     * a single call to {@link #stopService} will stop it.
854     *
855     * <p>The system attempts to keep running services around as much as
856     * possible.  The only time they should be stopped is if the current
857     * foreground application is using so many resources that the service needs
858     * to be killed.  If any errors happen in the service's process, it will
859     * automatically be restarted.
860     *
861     * <p>This function will throw {@link SecurityException} if you do not
862     * have permission to start the given service.
863     *
864     * @param service Identifies the service to be started.  The Intent may
865     *      specify either an explicit component name to start, or a logical
866     *      description (action, category, etc) to match an
867     *      {@link IntentFilter} published by a service.  Additional values
868     *      may be included in the Intent extras to supply arguments along with
869     *      this specific start call.
870     *
871     * @return If the service is being started or is already running, the
872     * {@link ComponentName} of the actual service that was started is
873     * returned; else if the service does not exist null is returned.
874     *
875     * @throws SecurityException
876     *
877     * @see #stopService
878     * @see #bindService
879     */
880    public abstract ComponentName startService(Intent service);
881
882    /**
883     * Request that a given application service be stopped.  If the service is
884     * not running, nothing happens.  Otherwise it is stopped.  Note that calls
885     * to startService() are not counted -- this stops the service no matter
886     * how many times it was started.
887     *
888     * <p>Note that if a stopped service still has {@link ServiceConnection}
889     * objects bound to it with the {@link #BIND_AUTO_CREATE} set, it will
890     * not be destroyed until all of these bindings are removed.  See
891     * the {@link android.app.Service} documentation for more details on a
892     * service's lifecycle.
893     *
894     * <p>This function will throw {@link SecurityException} if you do not
895     * have permission to stop the given service.
896     *
897     * @param service Description of the service to be stopped.  The Intent may
898     *      specify either an explicit component name to start, or a logical
899     *      description (action, category, etc) to match an
900     *      {@link IntentFilter} published by a service.
901     *
902     * @return If there is a service matching the given Intent that is already
903     * running, then it is stopped and true is returned; else false is returned.
904     *
905     * @throws SecurityException
906     *
907     * @see #startService
908     */
909    public abstract boolean stopService(Intent service);
910
911    /**
912     * Connect to an application service, creating it if needed.  This defines
913     * a dependency between your application and the service.  The given
914     * <var>conn</var> will receive the service object when its created and be
915     * told if it dies and restarts.  The service will be considered required
916     * by the system only for as long as the calling context exists.  For
917     * example, if this Context is an Activity that is stopped, the service will
918     * not be required to continue running until the Activity is resumed.
919     *
920     * <p>This function will throw {@link SecurityException} if you do not
921     * have permission to bind to the given service.
922     *
923     * <p class="note">Note: this method <em>can not be called from an
924     * {@link BroadcastReceiver} component</em>.  A pattern you can use to
925     * communicate from an BroadcastReceiver to a Service is to call
926     * {@link #startService} with the arguments containing the command to be
927     * sent, with the service calling its
928     * {@link android.app.Service#stopSelf(int)} method when done executing
929     * that command.  See the API demo App/Service/Service Start Arguments
930     * Controller for an illustration of this.  It is okay, however, to use
931     * this method from an BroadcastReceiver that has been registered with
932     * {@link #registerReceiver}, since the lifetime of this BroadcastReceiver
933     * is tied to another object (the one that registered it).</p>
934     *
935     * @param service Identifies the service to connect to.  The Intent may
936     *      specify either an explicit component name, or a logical
937     *      description (action, category, etc) to match an
938     *      {@link IntentFilter} published by a service.
939     * @param conn Receives information as the service is started and stopped.
940     * @param flags Operation options for the binding.  May be 0 or
941     *          {@link #BIND_AUTO_CREATE}.
942     * @return If you have successfully bound to the service, true is returned;
943     *         false is returned if the connection is not made so you will not
944     *         receive the service object.
945     *
946     * @throws SecurityException
947     *
948     * @see #unbindService
949     * @see #startService
950     * @see #BIND_AUTO_CREATE
951     */
952    public abstract boolean bindService(Intent service, ServiceConnection conn,
953            int flags);
954
955    /**
956     * Disconnect from an application service.  You will no longer receive
957     * calls as the service is restarted, and the service is now allowed to
958     * stop at any time.
959     *
960     * @param conn The connection interface previously supplied to
961     *             bindService().
962     *
963     * @see #bindService
964     */
965    public abstract void unbindService(ServiceConnection conn);
966
967    /**
968     * Start executing an {@link android.app.Instrumentation} class.  The given
969     * Instrumentation component will be run by killing its target application
970     * (if currently running), starting the target process, instantiating the
971     * instrumentation component, and then letting it drive the application.
972     *
973     * <p>This function is not synchronous -- it returns as soon as the
974     * instrumentation has started and while it is running.
975     *
976     * <p>Instrumentation is normally only allowed to run against a package
977     * that is either unsigned or signed with a signature that the
978     * the instrumentation package is also signed with (ensuring the target
979     * trusts the instrumentation).
980     *
981     * @param className Name of the Instrumentation component to be run.
982     * @param profileFile Optional path to write profiling data as the
983     * instrumentation runs, or null for no profiling.
984     * @param arguments Additional optional arguments to pass to the
985     * instrumentation, or null.
986     *
987     * @return Returns true if the instrumentation was successfully started,
988     * else false if it could not be found.
989     */
990    public abstract boolean startInstrumentation(ComponentName className,
991            String profileFile, Bundle arguments);
992
993    /**
994     * Return the handle to a system-level service by name. The class of the
995     * returned object varies by the requested name. Currently available names
996     * are:
997     *
998     * <dl>
999     *  <dt> {@link #WINDOW_SERVICE} ("window")
1000     *  <dd> The top-level window manager in which you can place custom
1001     *  windows.  The returned object is a {@link android.view.WindowManager}.
1002     *  <dt> {@link #LAYOUT_INFLATER_SERVICE} ("layout_inflater")
1003     *  <dd> A {@link android.view.LayoutInflater} for inflating layout resources
1004     *  in this context.
1005     *  <dt> {@link #ACTIVITY_SERVICE} ("activity")
1006     *  <dd> A {@link android.app.ActivityManager} for interacting with the
1007     *  global activity state of the system.
1008     *  <dt> {@link #POWER_SERVICE} ("power")
1009     *  <dd> A {@link android.os.PowerManager} for controlling power
1010     *  management.
1011     *  <dt> {@link #ALARM_SERVICE} ("alarm")
1012     *  <dd> A {@link android.app.AlarmManager} for receiving intents at the
1013     *  time of your choosing.
1014     *  <dt> {@link #NOTIFICATION_SERVICE} ("notification")
1015     *  <dd> A {@link android.app.NotificationManager} for informing the user
1016     *   of background events.
1017     *  <dt> {@link #KEYGUARD_SERVICE} ("keyguard")
1018     *  <dd> A {@link android.app.KeyguardManager} for controlling keyguard.
1019     *  <dt> {@link #LOCATION_SERVICE} ("location")
1020     *  <dd> A {@link android.location.LocationManager} for controlling location
1021     *   (e.g., GPS) updates.
1022     *  <dt> {@link #SEARCH_SERVICE} ("search")
1023     *  <dd> A {@link android.app.SearchManager} for handling search.
1024     *  <dt> {@link #VIBRATOR_SERVICE} ("vibrator")
1025     *  <dd> A {@link android.os.Vibrator} for interacting with the vibrator
1026     *  hardware.
1027     *  <dt> {@link #CONNECTIVITY_SERVICE} ("connection")
1028     *  <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for
1029     *  handling management of network connections.
1030     *  <dt> {@link #WIFI_SERVICE} ("wifi")
1031     *  <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of
1032     * Wi-Fi connectivity.
1033     * <dt> {@link #INPUT_METHOD_SERVICE} ("input_method")
1034     * <dd> An {@link android.view.inputmethod.InputMethodManager InputMethodManager}
1035     * for management of input methods.
1036     * </dl>
1037     *
1038     * <p>Note:  System services obtained via this API may be closely associated with
1039     * the Context in which they are obtained from.  In general, do not share the
1040     * service objects between various different contexts (Activities, Applications,
1041     * Services, Providers, etc.)
1042     *
1043     * @param name The name of the desired service.
1044     *
1045     * @return The service or null if the name does not exist.
1046     *
1047     * @see #WINDOW_SERVICE
1048     * @see android.view.WindowManager
1049     * @see #LAYOUT_INFLATER_SERVICE
1050     * @see android.view.LayoutInflater
1051     * @see #ACTIVITY_SERVICE
1052     * @see android.app.ActivityManager
1053     * @see #POWER_SERVICE
1054     * @see android.os.PowerManager
1055     * @see #ALARM_SERVICE
1056     * @see android.app.AlarmManager
1057     * @see #NOTIFICATION_SERVICE
1058     * @see android.app.NotificationManager
1059     * @see #KEYGUARD_SERVICE
1060     * @see android.app.KeyguardManager
1061     * @see #LOCATION_SERVICE
1062     * @see android.location.LocationManager
1063     * @see #SEARCH_SERVICE
1064     * @see android.app.SearchManager
1065     * @see #SENSOR_SERVICE
1066     * @see android.hardware.SensorManager
1067     * @see #VIBRATOR_SERVICE
1068     * @see android.os.Vibrator
1069     * @see #CONNECTIVITY_SERVICE
1070     * @see android.net.ConnectivityManager
1071     * @see #WIFI_SERVICE
1072     * @see android.net.wifi.WifiManager
1073     * @see #AUDIO_SERVICE
1074     * @see android.media.AudioManager
1075     * @see #TELEPHONY_SERVICE
1076     * @see android.telephony.TelephonyManager
1077     * @see #INPUT_METHOD_SERVICE
1078     * @see android.view.inputmethod.InputMethodManager
1079     */
1080    public abstract Object getSystemService(String name);
1081
1082    /**
1083     * Use with {@link #getSystemService} to retrieve a
1084     * {@link android.os.PowerManager} for controlling power management,
1085     * including "wake locks," which let you keep the device on while
1086     * you're running long tasks.
1087     */
1088    public static final String POWER_SERVICE = "power";
1089    /**
1090     * Use with {@link #getSystemService} to retrieve a
1091     * {@link android.view.WindowManager} for accessing the system's window
1092     * manager.
1093     *
1094     * @see #getSystemService
1095     * @see android.view.WindowManager
1096     */
1097    public static final String WINDOW_SERVICE = "window";
1098    /**
1099     * Use with {@link #getSystemService} to retrieve a
1100     * {@link android.view.LayoutInflater} for inflating layout resources in this
1101     * context.
1102     *
1103     * @see #getSystemService
1104     * @see android.view.LayoutInflater
1105     */
1106    public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater";
1107    /**
1108     * Use with {@link #getSystemService} to retrieve a
1109     * {@link android.accounts.AccountManager} for receiving intents at a
1110     * time of your choosing.
1111     * TODO STOPSHIP perform a final review of the the account apis before shipping
1112     *
1113     * @see #getSystemService
1114     * @see android.accounts.AccountManager
1115     */
1116    public static final String ACCOUNT_SERVICE = "account";
1117    /**
1118     * Use with {@link #getSystemService} to retrieve a
1119     * {@link android.app.ActivityManager} for interacting with the global
1120     * system state.
1121     *
1122     * @see #getSystemService
1123     * @see android.app.ActivityManager
1124     */
1125    public static final String ACTIVITY_SERVICE = "activity";
1126    /**
1127     * Use with {@link #getSystemService} to retrieve a
1128     * {@link android.app.AlarmManager} for receiving intents at a
1129     * time of your choosing.
1130     *
1131     * @see #getSystemService
1132     * @see android.app.AlarmManager
1133     */
1134    public static final String ALARM_SERVICE = "alarm";
1135    /**
1136     * Use with {@link #getSystemService} to retrieve a
1137     * {@link android.app.NotificationManager} for informing the user of
1138     * background events.
1139     *
1140     * @see #getSystemService
1141     * @see android.app.NotificationManager
1142     */
1143    public static final String NOTIFICATION_SERVICE = "notification";
1144    /**
1145     * Use with {@link #getSystemService} to retrieve a
1146     * {@link android.view.accessibility.AccessibilityManager} for giving the user
1147     * feedback for UI events through the registered event listeners.
1148     *
1149     * @see #getSystemService
1150     * @see android.view.accessibility.AccessibilityManager
1151     */
1152    public static final String ACCESSIBILITY_SERVICE = "accessibility";
1153    /**
1154     * Use with {@link #getSystemService} to retrieve a
1155     * {@link android.app.NotificationManager} for controlling keyguard.
1156     *
1157     * @see #getSystemService
1158     * @see android.app.KeyguardManager
1159     */
1160    public static final String KEYGUARD_SERVICE = "keyguard";
1161    /**
1162     * Use with {@link #getSystemService} to retrieve a {@link
1163     * android.location.LocationManager} for controlling location
1164     * updates.
1165     *
1166     * @see #getSystemService
1167     * @see android.location.LocationManager
1168     */
1169    public static final String LOCATION_SERVICE = "location";
1170    /**
1171     * Use with {@link #getSystemService} to retrieve a {@link
1172     * android.app.SearchManager} for handling searches.
1173     *
1174     * @see #getSystemService
1175     * @see android.app.SearchManager
1176     */
1177    public static final String SEARCH_SERVICE = "search";
1178    /**
1179     * Use with {@link #getSystemService} to retrieve a {@link
1180     * android.hardware.SensorManager} for accessing sensors.
1181     *
1182     * @see #getSystemService
1183     * @see android.hardware.SensorManager
1184     */
1185    public static final String SENSOR_SERVICE = "sensor";
1186    /**
1187     * Use with {@link #getSystemService} to retrieve a {@link
1188     * android.bluetooth.BluetoothDevice} for interacting with Bluetooth.
1189     *
1190     * @see #getSystemService
1191     * @see android.bluetooth.BluetoothDevice
1192     * @hide
1193     */
1194    public static final String BLUETOOTH_SERVICE = "bluetooth";
1195    /**
1196     * Use with {@link #getSystemService} to retrieve a
1197     * com.android.server.WallpaperService for accessing wallpapers.
1198     *
1199     * @see #getSystemService
1200     */
1201    public static final String WALLPAPER_SERVICE = "wallpaper";
1202    /**
1203     * Use with {@link #getSystemService} to retrieve a {@link
1204     * android.os.Vibrator} for interacting with the vibration hardware.
1205     *
1206     * @see #getSystemService
1207     * @see android.os.Vibrator
1208     */
1209    public static final String VIBRATOR_SERVICE = "vibrator";
1210    /**
1211     * Use with {@link #getSystemService} to retrieve a {@link
1212     * android.app.StatusBarManager} for interacting with the status bar.
1213     *
1214     * @see #getSystemService
1215     * @see android.app.StatusBarManager
1216     * @hide
1217     */
1218    public static final String STATUS_BAR_SERVICE = "statusbar";
1219
1220    /**
1221     * Use with {@link #getSystemService} to retrieve a {@link
1222     * android.net.ConnectivityManager} for handling management of
1223     * network connections.
1224     *
1225     * @see #getSystemService
1226     * @see android.net.ConnectivityManager
1227     */
1228    public static final String CONNECTIVITY_SERVICE = "connectivity";
1229
1230    /**
1231     * Use with {@link #getSystemService} to retrieve a {@link
1232     * android.net.wifi.WifiManager} for handling management of
1233     * Wi-Fi access.
1234     *
1235     * @see #getSystemService
1236     * @see android.net.wifi.WifiManager
1237     */
1238    public static final String WIFI_SERVICE = "wifi";
1239
1240    /**
1241     * Use with {@link #getSystemService} to retrieve a
1242     * {@link android.media.AudioManager} for handling management of volume,
1243     * ringer modes and audio routing.
1244     *
1245     * @see #getSystemService
1246     * @see android.media.AudioManager
1247     */
1248    public static final String AUDIO_SERVICE = "audio";
1249
1250    /**
1251     * Use with {@link #getSystemService} to retrieve a
1252     * {@link android.telephony.TelephonyManager} for handling management the
1253     * telephony features of the device.
1254     *
1255     * @see #getSystemService
1256     * @see android.telephony.TelephonyManager
1257     */
1258    public static final String TELEPHONY_SERVICE = "phone";
1259
1260    /**
1261     * Use with {@link #getSystemService} to retrieve a
1262     * {@link android.text.ClipboardManager} for accessing and modifying
1263     * the contents of the global clipboard.
1264     *
1265     * @see #getSystemService
1266     * @see android.text.ClipboardManager
1267     */
1268    public static final String CLIPBOARD_SERVICE = "clipboard";
1269
1270    /**
1271     * Use with {@link #getSystemService} to retrieve a
1272     * {@link android.view.inputmethod.InputMethodManager} for accessing input
1273     * methods.
1274     *
1275     * @see #getSystemService
1276     */
1277    public static final String INPUT_METHOD_SERVICE = "input_method";
1278
1279    /**
1280     * Use with {@link #getSystemService} to retrieve a
1281     * {@blink android.appwidget.AppWidgetManager} for accessing AppWidgets.
1282     *
1283     * @hide
1284     * @see #getSystemService
1285     */
1286    public static final String APPWIDGET_SERVICE = "appwidget";
1287
1288    /**
1289     * Use with {@link #getSystemService} to retrieve an
1290     * {@blink android.backup.IBackupManager IBackupManager} for communicating
1291     * with the backup mechanism.
1292     *
1293     * @see #getSystemService
1294     */
1295    public static final String BACKUP_SERVICE = "backup";
1296
1297    /**
1298     * Determine whether the given permission is allowed for a particular
1299     * process and user ID running in the system.
1300     *
1301     * @param permission The name of the permission being checked.
1302     * @param pid The process ID being checked against.  Must be > 0.
1303     * @param uid The user ID being checked against.  A uid of 0 is the root
1304     * user, which will pass every permission check.
1305     *
1306     * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the given
1307     * pid/uid is allowed that permission, or
1308     * {@link PackageManager#PERMISSION_DENIED} if it is not.
1309     *
1310     * @see PackageManager#checkPermission(String, String)
1311     * @see #checkCallingPermission
1312     */
1313    public abstract int checkPermission(String permission, int pid, int uid);
1314
1315    /**
1316     * Determine whether the calling process of an IPC you are handling has been
1317     * granted a particular permission.  This is basically the same as calling
1318     * {@link #checkPermission(String, int, int)} with the pid and uid returned
1319     * by {@link android.os.Binder#getCallingPid} and
1320     * {@link android.os.Binder#getCallingUid}.  One important difference
1321     * is that if you are not currently processing an IPC, this function
1322     * will always fail.  This is done to protect against accidentally
1323     * leaking permissions; you can use {@link #checkCallingOrSelfPermission}
1324     * to avoid this protection.
1325     *
1326     * @param permission The name of the permission being checked.
1327     *
1328     * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the calling
1329     * pid/uid is allowed that permission, or
1330     * {@link PackageManager#PERMISSION_DENIED} if it is not.
1331     *
1332     * @see PackageManager#checkPermission(String, String)
1333     * @see #checkPermission
1334     * @see #checkCallingOrSelfPermission
1335     */
1336    public abstract int checkCallingPermission(String permission);
1337
1338    /**
1339     * Determine whether the calling process of an IPC <em>or you</em> have been
1340     * granted a particular permission.  This is the same as
1341     * {@link #checkCallingPermission}, except it grants your own permissions
1342     * if you are not currently processing an IPC.  Use with care!
1343     *
1344     * @param permission The name of the permission being checked.
1345     *
1346     * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the calling
1347     * pid/uid is allowed that permission, or
1348     * {@link PackageManager#PERMISSION_DENIED} if it is not.
1349     *
1350     * @see PackageManager#checkPermission(String, String)
1351     * @see #checkPermission
1352     * @see #checkCallingPermission
1353     */
1354    public abstract int checkCallingOrSelfPermission(String permission);
1355
1356    /**
1357     * If the given permission is not allowed for a particular process
1358     * and user ID running in the system, throw a {@link SecurityException}.
1359     *
1360     * @param permission The name of the permission being checked.
1361     * @param pid The process ID being checked against.  Must be &gt; 0.
1362     * @param uid The user ID being checked against.  A uid of 0 is the root
1363     * user, which will pass every permission check.
1364     * @param message A message to include in the exception if it is thrown.
1365     *
1366     * @see #checkPermission(String, int, int)
1367     */
1368    public abstract void enforcePermission(
1369            String permission, int pid, int uid, String message);
1370
1371    /**
1372     * If the calling process of an IPC you are handling has not been
1373     * granted a particular permission, throw a {@link
1374     * SecurityException}.  This is basically the same as calling
1375     * {@link #enforcePermission(String, int, int, String)} with the
1376     * pid and uid returned by {@link android.os.Binder#getCallingPid}
1377     * and {@link android.os.Binder#getCallingUid}.  One important
1378     * difference is that if you are not currently processing an IPC,
1379     * this function will always throw the SecurityException.  This is
1380     * done to protect against accidentally leaking permissions; you
1381     * can use {@link #enforceCallingOrSelfPermission} to avoid this
1382     * protection.
1383     *
1384     * @param permission The name of the permission being checked.
1385     * @param message A message to include in the exception if it is thrown.
1386     *
1387     * @see #checkCallingPermission(String)
1388     */
1389    public abstract void enforceCallingPermission(
1390            String permission, String message);
1391
1392    /**
1393     * If neither you nor the calling process of an IPC you are
1394     * handling has been granted a particular permission, throw a
1395     * {@link SecurityException}.  This is the same as {@link
1396     * #enforceCallingPermission}, except it grants your own
1397     * permissions if you are not currently processing an IPC.  Use
1398     * with care!
1399     *
1400     * @param permission The name of the permission being checked.
1401     * @param message A message to include in the exception if it is thrown.
1402     *
1403     * @see #checkCallingOrSelfPermission(String)
1404     */
1405    public abstract void enforceCallingOrSelfPermission(
1406            String permission, String message);
1407
1408    /**
1409     * Grant permission to access a specific Uri to another package, regardless
1410     * of whether that package has general permission to access the Uri's
1411     * content provider.  This can be used to grant specific, temporary
1412     * permissions, typically in response to user interaction (such as the
1413     * user opening an attachment that you would like someone else to
1414     * display).
1415     *
1416     * <p>Normally you should use {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
1417     * Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1418     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
1419     * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} with the Intent being used to
1420     * start an activity instead of this function directly.  If you use this
1421     * function directly, you should be sure to call
1422     * {@link #revokeUriPermission} when the target should no longer be allowed
1423     * to access it.
1424     *
1425     * <p>To succeed, the content provider owning the Uri must have set the
1426     * {@link android.R.styleable#AndroidManifestProvider_grantUriPermissions
1427     * grantUriPermissions} attribute in its manifest or included the
1428     * {@link android.R.styleable#AndroidManifestGrantUriPermission
1429     * &lt;grant-uri-permissions&gt;} tag.
1430     *
1431     * @param toPackage The package you would like to allow to access the Uri.
1432     * @param uri The Uri you would like to grant access to.
1433     * @param modeFlags The desired access modes.  Any combination of
1434     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
1435     * Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1436     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
1437     * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1438     *
1439     * @see #revokeUriPermission
1440     */
1441    public abstract void grantUriPermission(String toPackage, Uri uri,
1442            int modeFlags);
1443
1444    /**
1445     * Remove all permissions to access a particular content provider Uri
1446     * that were previously added with {@link #grantUriPermission}.  The given
1447     * Uri will match all previously granted Uris that are the same or a
1448     * sub-path of the given Uri.  That is, revoking "content://foo/one" will
1449     * revoke both "content://foo/target" and "content://foo/target/sub", but not
1450     * "content://foo".
1451     *
1452     * @param uri The Uri you would like to revoke access to.
1453     * @param modeFlags The desired access modes.  Any combination of
1454     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
1455     * Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1456     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
1457     * Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1458     *
1459     * @see #grantUriPermission
1460     */
1461    public abstract void revokeUriPermission(Uri uri, int modeFlags);
1462
1463    /**
1464     * Determine whether a particular process and user ID has been granted
1465     * permission to access a specific URI.  This only checks for permissions
1466     * that have been explicitly granted -- if the given process/uid has
1467     * more general access to the URI's content provider then this check will
1468     * always fail.
1469     *
1470     * @param uri The uri that is being checked.
1471     * @param pid The process ID being checked against.  Must be &gt; 0.
1472     * @param uid The user ID being checked against.  A uid of 0 is the root
1473     * user, which will pass every permission check.
1474     * @param modeFlags The type of access to grant.  May be one or both of
1475     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1476     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1477     *
1478     * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the given
1479     * pid/uid is allowed to access that uri, or
1480     * {@link PackageManager#PERMISSION_DENIED} if it is not.
1481     *
1482     * @see #checkCallingUriPermission
1483     */
1484    public abstract int checkUriPermission(Uri uri, int pid, int uid, int modeFlags);
1485
1486    /**
1487     * Determine whether the calling process and user ID has been
1488     * granted permission to access a specific URI.  This is basically
1489     * the same as calling {@link #checkUriPermission(Uri, int, int,
1490     * int)} with the pid and uid returned by {@link
1491     * android.os.Binder#getCallingPid} and {@link
1492     * android.os.Binder#getCallingUid}.  One important difference is
1493     * that if you are not currently processing an IPC, this function
1494     * will always fail.
1495     *
1496     * @param uri The uri that is being checked.
1497     * @param modeFlags The type of access to grant.  May be one or both of
1498     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1499     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1500     *
1501     * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller
1502     * is allowed to access that uri, or
1503     * {@link PackageManager#PERMISSION_DENIED} if it is not.
1504     *
1505     * @see #checkUriPermission(Uri, int, int, int)
1506     */
1507    public abstract int checkCallingUriPermission(Uri uri, int modeFlags);
1508
1509    /**
1510     * Determine whether the calling process of an IPC <em>or you</em> has been granted
1511     * permission to access a specific URI.  This is the same as
1512     * {@link #checkCallingUriPermission}, except it grants your own permissions
1513     * if you are not currently processing an IPC.  Use with care!
1514     *
1515     * @param uri The uri that is being checked.
1516     * @param modeFlags The type of access to grant.  May be one or both of
1517     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1518     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1519     *
1520     * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller
1521     * is allowed to access that uri, or
1522     * {@link PackageManager#PERMISSION_DENIED} if it is not.
1523     *
1524     * @see #checkCallingUriPermission
1525     */
1526    public abstract int checkCallingOrSelfUriPermission(Uri uri, int modeFlags);
1527
1528    /**
1529     * Check both a Uri and normal permission.  This allows you to perform
1530     * both {@link #checkPermission} and {@link #checkUriPermission} in one
1531     * call.
1532     *
1533     * @param uri The Uri whose permission is to be checked, or null to not
1534     * do this check.
1535     * @param readPermission The permission that provides overall read access,
1536     * or null to not do this check.
1537     * @param writePermission The permission that provides overall write
1538     * acess, or null to not do this check.
1539     * @param pid The process ID being checked against.  Must be &gt; 0.
1540     * @param uid The user ID being checked against.  A uid of 0 is the root
1541     * user, which will pass every permission check.
1542     * @param modeFlags The type of access to grant.  May be one or both of
1543     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1544     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1545     *
1546     * @return Returns {@link PackageManager#PERMISSION_GRANTED} if the caller
1547     * is allowed to access that uri or holds one of the given permissions, or
1548     * {@link PackageManager#PERMISSION_DENIED} if it is not.
1549     */
1550    public abstract int checkUriPermission(Uri uri, String readPermission,
1551            String writePermission, int pid, int uid, int modeFlags);
1552
1553    /**
1554     * If a particular process and user ID has not been granted
1555     * permission to access a specific URI, throw {@link
1556     * SecurityException}.  This only checks for permissions that have
1557     * been explicitly granted -- if the given process/uid has more
1558     * general access to the URI's content provider then this check
1559     * will always fail.
1560     *
1561     * @param uri The uri that is being checked.
1562     * @param pid The process ID being checked against.  Must be &gt; 0.
1563     * @param uid The user ID being checked against.  A uid of 0 is the root
1564     * user, which will pass every permission check.
1565     * @param modeFlags The type of access to grant.  May be one or both of
1566     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1567     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1568     * @param message A message to include in the exception if it is thrown.
1569     *
1570     * @see #checkUriPermission(Uri, int, int, int)
1571     */
1572    public abstract void enforceUriPermission(
1573            Uri uri, int pid, int uid, int modeFlags, String message);
1574
1575    /**
1576     * If the calling process and user ID has not been granted
1577     * permission to access a specific URI, throw {@link
1578     * SecurityException}.  This is basically the same as calling
1579     * {@link #enforceUriPermission(Uri, int, int, int, String)} with
1580     * the pid and uid returned by {@link
1581     * android.os.Binder#getCallingPid} and {@link
1582     * android.os.Binder#getCallingUid}.  One important difference is
1583     * that if you are not currently processing an IPC, this function
1584     * will always throw a SecurityException.
1585     *
1586     * @param uri The uri that is being checked.
1587     * @param modeFlags The type of access to grant.  May be one or both of
1588     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1589     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1590     * @param message A message to include in the exception if it is thrown.
1591     *
1592     * @see #checkCallingUriPermission(Uri, int)
1593     */
1594    public abstract void enforceCallingUriPermission(
1595            Uri uri, int modeFlags, String message);
1596
1597    /**
1598     * If the calling process of an IPC <em>or you</em> has not been
1599     * granted permission to access a specific URI, throw {@link
1600     * SecurityException}.  This is the same as {@link
1601     * #enforceCallingUriPermission}, except it grants your own
1602     * permissions if you are not currently processing an IPC.  Use
1603     * with care!
1604     *
1605     * @param uri The uri that is being checked.
1606     * @param modeFlags The type of access to grant.  May be one or both of
1607     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1608     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1609     * @param message A message to include in the exception if it is thrown.
1610     *
1611     * @see #checkCallingOrSelfUriPermission(Uri, int)
1612     */
1613    public abstract void enforceCallingOrSelfUriPermission(
1614            Uri uri, int modeFlags, String message);
1615
1616    /**
1617     * Enforce both a Uri and normal permission.  This allows you to perform
1618     * both {@link #enforcePermission} and {@link #enforceUriPermission} in one
1619     * call.
1620     *
1621     * @param uri The Uri whose permission is to be checked, or null to not
1622     * do this check.
1623     * @param readPermission The permission that provides overall read access,
1624     * or null to not do this check.
1625     * @param writePermission The permission that provides overall write
1626     * acess, or null to not do this check.
1627     * @param pid The process ID being checked against.  Must be &gt; 0.
1628     * @param uid The user ID being checked against.  A uid of 0 is the root
1629     * user, which will pass every permission check.
1630     * @param modeFlags The type of access to grant.  May be one or both of
1631     * {@link Intent#FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION} or
1632     * {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION}.
1633     * @param message A message to include in the exception if it is thrown.
1634     *
1635     * @see #checkUriPermission(Uri, String, String, int, int, int)
1636     */
1637    public abstract void enforceUriPermission(
1638            Uri uri, String readPermission, String writePermission,
1639            int pid, int uid, int modeFlags, String message);
1640
1641    /**
1642     * Flag for use with {@link #createPackageContext}: include the application
1643     * code with the context.  This means loading code into the caller's
1644     * process, so that {@link #getClassLoader()} can be used to instantiate
1645     * the application's classes.  Setting this flags imposes security
1646     * restrictions on what application context you can access; if the
1647     * requested application can not be safely loaded into your process,
1648     * java.lang.SecurityException will be thrown.  If this flag is not set,
1649     * there will be no restrictions on the packages that can be loaded,
1650     * but {@link #getClassLoader} will always return the default system
1651     * class loader.
1652     */
1653    public static final int CONTEXT_INCLUDE_CODE = 0x00000001;
1654
1655    /**
1656     * Flag for use with {@link #createPackageContext}: ignore any security
1657     * restrictions on the Context being requested, allowing it to always
1658     * be loaded.  For use with {@link #CONTEXT_INCLUDE_CODE} to allow code
1659     * to be loaded into a process even when it isn't safe to do so.  Use
1660     * with extreme care!
1661     */
1662    public static final int CONTEXT_IGNORE_SECURITY = 0x00000002;
1663
1664    /**
1665     * Return a new Context object for the given application name.  This
1666     * Context is the same as what the named application gets when it is
1667     * launched, containing the same resources and class loader.  Each call to
1668     * this method returns a new instance of a Context object; Context objects
1669     * are not shared, however they share common state (Resources, ClassLoader,
1670     * etc) so the Context instance itself is fairly lightweight.
1671     *
1672     * <p>Throws {@link PackageManager.NameNotFoundException} if there is no
1673     * application with the given package name.
1674     *
1675     * <p>Throws {@link java.lang.SecurityException} if the Context requested
1676     * can not be loaded into the caller's process for security reasons (see
1677     * {@link #CONTEXT_INCLUDE_CODE} for more information}.
1678     *
1679     * @param packageName Name of the application's package.
1680     * @param flags Option flags, one of {@link #CONTEXT_INCLUDE_CODE}
1681     *              or {@link #CONTEXT_IGNORE_SECURITY}.
1682     *
1683     * @return A Context for the application.
1684     *
1685     * @throws java.lang.SecurityException
1686     * @throws PackageManager.NameNotFoundException if there is no application with
1687     * the given package name
1688     */
1689    public abstract Context createPackageContext(String packageName,
1690            int flags) throws PackageManager.NameNotFoundException;
1691}
1692