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