2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.BroadcastReceiver;
22 import android.content.ComponentName;
23 import android.content.ContentProvider;
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.content.ContextWrapper;
27 import android.content.IContentProvider;
28 import android.content.IIntentReceiver;
29 import android.content.Intent;
30 import android.content.IntentFilter;
31 import android.content.IntentSender;
32 import android.content.ReceiverCallNotAllowedException;
33 import android.content.ServiceConnection;
34 import android.content.SharedPreferences;
35 import android.content.pm.ActivityInfo;
36 import android.content.pm.ApplicationInfo;
37 import android.content.pm.IPackageManager;
38 import android.content.pm.PackageManager;
39 import android.content.pm.PackageManager.NameNotFoundException;
40 import android.content.res.AssetManager;
41 import android.content.res.CompatibilityInfo;
42 import android.content.res.Configuration;
43 import android.content.res.Resources;
44 import android.database.DatabaseErrorHandler;
45 import android.database.sqlite.SQLiteDatabase;
46 import android.database.sqlite.SQLiteDatabase.CursorFactory;
47 import android.graphics.Bitmap;
48 import android.graphics.drawable.Drawable;
49 import android.net.Uri;
50 import android.os.Binder;
51 import android.os.Build;
52 import android.os.Bundle;
53 import android.os.Debug;
54 import android.os.Environment;
55 import android.os.FileUtils;
56 import android.os.Handler;
57 import android.os.IBinder;
58 import android.os.Looper;
59 import android.os.Process;
60 import android.os.RemoteException;
61 import android.os.ServiceManager;
62 import android.os.UserHandle;
63 import android.os.storage.IMountService;
64 import android.system.ErrnoException;
65 import android.system.Os;
66 import android.system.OsConstants;
67 import android.util.AndroidRuntimeException;
68 import android.util.ArrayMap;
69 import android.util.Log;
70 import android.util.Slog;
71 import android.view.Display;
72 import android.view.DisplayAdjustments;
74 import com.android.internal.annotations.GuardedBy;
75 import com.android.internal.util.Preconditions;
78 import java.io.FileInputStream;
79 import java.io.FileNotFoundException;
80 import java.io.FileOutputStream;
81 import java.io.FilenameFilter;
82 import java.io.IOException;
83 import java.io.InputStream;
84 import java.util.Objects;
86 class ReceiverRestrictedContext extends ContextWrapper {
87 ReceiverRestrictedContext(Context base) {
92 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
93 return registerReceiver(receiver, filter, null, null);
97 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
98 String broadcastPermission, Handler scheduler) {
99 if (receiver == null) {
100 // Allow retrieving current sticky broadcast; this is safe since we
101 // aren't actually registering a receiver.
102 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
104 throw new ReceiverCallNotAllowedException(
105 "BroadcastReceiver components are not allowed to register to receive intents");
110 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
111 IntentFilter filter, String broadcastPermission, Handler scheduler) {
112 if (receiver == null) {
113 // Allow retrieving current sticky broadcast; this is safe since we
114 // aren't actually registering a receiver.
115 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
117 throw new ReceiverCallNotAllowedException(
118 "BroadcastReceiver components are not allowed to register to receive intents");
123 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
124 throw new ReceiverCallNotAllowedException(
125 "BroadcastReceiver components are not allowed to bind to services");
130 * Common implementation of Context API, which provides the base
131 * context object for Activity and other application components.
133 class ContextImpl extends Context {
134 private final static String TAG = "ContextImpl";
135 private final static boolean DEBUG = false;
138 * Map from package name, to preference name, to cached preferences.
140 @GuardedBy("ContextImpl.class")
141 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
144 * Map from preference name to generated path.
146 @GuardedBy("ContextImpl.class")
147 private ArrayMap<String, File> mSharedPrefsPaths;
149 final ActivityThread mMainThread;
150 final LoadedApk mPackageInfo;
152 private final IBinder mActivityToken;
154 private final UserHandle mUser;
156 private final ApplicationContentResolver mContentResolver;
158 private final String mBasePackageName;
159 private final String mOpPackageName;
161 private final @NonNull ResourcesManager mResourcesManager;
162 private final @NonNull Resources mResources;
163 private @Nullable Display mDisplay; // may be null if default display
165 private final int mFlags;
167 private Context mOuterContext;
168 private int mThemeResource = 0;
169 private Resources.Theme mTheme = null;
170 private PackageManager mPackageManager;
171 private Context mReceiverRestrictedContext = null;
173 private final Object mSync = new Object();
176 private File mDatabasesDir;
178 private File mPreferencesDir;
180 private File mFilesDir;
182 private File mNoBackupFilesDir;
184 private File mCacheDir;
186 private File mCodeCacheDir;
189 private File[] mExternalObbDirs;
191 private File[] mExternalFilesDirs;
193 private File[] mExternalCacheDirs;
195 private File[] mExternalMediaDirs;
197 // The system service cache for the system services that are cached per-ContextImpl.
198 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
200 static ContextImpl getImpl(Context context) {
202 while ((context instanceof ContextWrapper) &&
203 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
204 context = nextContext;
206 return (ContextImpl)context;
210 public AssetManager getAssets() {
211 return getResources().getAssets();
215 public Resources getResources() {
220 public PackageManager getPackageManager() {
221 if (mPackageManager != null) {
222 return mPackageManager;
225 IPackageManager pm = ActivityThread.getPackageManager();
227 // Doesn't matter if we make more than one instance.
228 return (mPackageManager = new ApplicationPackageManager(this, pm));
235 public ContentResolver getContentResolver() {
236 return mContentResolver;
240 public Looper getMainLooper() {
241 return mMainThread.getLooper();
245 public Context getApplicationContext() {
246 return (mPackageInfo != null) ?
247 mPackageInfo.getApplication() : mMainThread.getApplication();
251 public void setTheme(int resId) {
252 if (mThemeResource != resId) {
253 mThemeResource = resId;
259 public int getThemeResId() {
260 return mThemeResource;
264 public Resources.Theme getTheme() {
265 if (mTheme != null) {
269 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
270 getOuterContext().getApplicationInfo().targetSdkVersion);
276 private void initializeTheme() {
277 if (mTheme == null) {
278 mTheme = mResources.newTheme();
280 mTheme.applyStyle(mThemeResource, true);
284 public ClassLoader getClassLoader() {
285 return mPackageInfo != null ?
286 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
290 public String getPackageName() {
291 if (mPackageInfo != null) {
292 return mPackageInfo.getPackageName();
294 // No mPackageInfo means this is a Context for the system itself,
295 // and this here is its name.
301 public String getBasePackageName() {
302 return mBasePackageName != null ? mBasePackageName : getPackageName();
307 public String getOpPackageName() {
308 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
312 public ApplicationInfo getApplicationInfo() {
313 if (mPackageInfo != null) {
314 return mPackageInfo.getApplicationInfo();
316 throw new RuntimeException("Not supported in system context");
320 public String getPackageResourcePath() {
321 if (mPackageInfo != null) {
322 return mPackageInfo.getResDir();
324 throw new RuntimeException("Not supported in system context");
328 public String getPackageCodePath() {
329 if (mPackageInfo != null) {
330 return mPackageInfo.getAppDir();
332 throw new RuntimeException("Not supported in system context");
336 public SharedPreferences getSharedPreferences(String name, int mode) {
337 // At least one application in the world actually passes in a null
338 // name. This happened to work because when we generated the file name
339 // we would stringify it to "null.xml". Nice.
340 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
341 Build.VERSION_CODES.KITKAT) {
348 synchronized (ContextImpl.class) {
349 if (mSharedPrefsPaths == null) {
350 mSharedPrefsPaths = new ArrayMap<>();
352 file = mSharedPrefsPaths.get(name);
354 file = getSharedPreferencesPath(name);
355 mSharedPrefsPaths.put(name, file);
358 return getSharedPreferences(file, mode);
362 public SharedPreferences getSharedPreferences(File file, int mode) {
364 SharedPreferencesImpl sp;
365 synchronized (ContextImpl.class) {
366 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
367 sp = cache.get(file);
369 sp = new SharedPreferencesImpl(file, mode);
374 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
375 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
376 // If somebody else (some other process) changed the prefs
377 // file behind our back, we reload it. This has been the
378 // historical (if undocumented) behavior.
379 sp.startReloadIfChangedUnexpectedly();
384 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
385 if (sSharedPrefsCache == null) {
386 sSharedPrefsCache = new ArrayMap<>();
389 final String packageName = getPackageName();
390 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
391 if (packagePrefs == null) {
392 packagePrefs = new ArrayMap<>();
393 sSharedPrefsCache.put(packageName, packagePrefs);
400 * Try our best to migrate all files from source to target that match
403 * @return the number of files moved, or -1 if there was trouble.
405 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
406 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
408 public boolean accept(File dir, String name) {
409 return name.startsWith(prefix);
414 for (File sourceFile : sourceFiles) {
415 final File targetFile = new File(targetDir, sourceFile.getName());
416 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
418 FileUtils.copyFileOrThrow(sourceFile, targetFile);
419 FileUtils.copyPermissions(sourceFile, targetFile);
420 if (!sourceFile.delete()) {
421 throw new IOException("Failed to clean up " + sourceFile);
426 } catch (IOException e) {
427 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
435 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
436 synchronized (ContextImpl.class) {
437 final File source = sourceContext.getSharedPreferencesPath(name);
438 final File target = getSharedPreferencesPath(name);
440 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
443 // We moved at least one file, so evict any in-memory caches for
445 final ArrayMap<File, SharedPreferencesImpl> cache =
446 getSharedPreferencesCacheLocked();
447 cache.remove(source);
448 cache.remove(target);
455 public boolean deleteSharedPreferences(String name) {
456 synchronized (ContextImpl.class) {
457 final File prefs = getSharedPreferencesPath(name);
458 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
460 // Evict any in-memory caches
461 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
465 prefsBackup.delete();
467 // We failed if files are still lingering
468 return !(prefs.exists() || prefsBackup.exists());
472 private File getPreferencesDir() {
473 synchronized (mSync) {
474 if (mPreferencesDir == null) {
475 mPreferencesDir = new File(getDataDir(), "shared_prefs");
477 return ensurePrivateDirExists(mPreferencesDir);
482 public FileInputStream openFileInput(String name)
483 throws FileNotFoundException {
484 File f = makeFilename(getFilesDir(), name);
485 return new FileInputStream(f);
489 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
491 final boolean append = (mode&MODE_APPEND) != 0;
492 File f = makeFilename(getFilesDir(), name);
494 FileOutputStream fos = new FileOutputStream(f, append);
495 setFilePermissionsFromMode(f.getPath(), mode, 0);
497 } catch (FileNotFoundException e) {
500 File parent = f.getParentFile();
502 FileUtils.setPermissions(
504 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
506 FileOutputStream fos = new FileOutputStream(f, append);
507 setFilePermissionsFromMode(f.getPath(), mode, 0);
512 public boolean deleteFile(String name) {
513 File f = makeFilename(getFilesDir(), name);
518 * Common-path handling of app data dir creation
520 private static File ensurePrivateDirExists(File file) {
521 if (!file.exists()) {
523 Os.mkdir(file.getAbsolutePath(), 0771);
524 Os.chmod(file.getAbsolutePath(), 0771);
525 } catch (ErrnoException e) {
526 if (e.errno == OsConstants.EEXIST) {
527 // We must have raced with someone; that's okay
529 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
537 public File getFilesDir() {
538 synchronized (mSync) {
539 if (mFilesDir == null) {
540 mFilesDir = new File(getDataDir(), "files");
542 return ensurePrivateDirExists(mFilesDir);
547 public File getNoBackupFilesDir() {
548 synchronized (mSync) {
549 if (mNoBackupFilesDir == null) {
550 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
552 return ensurePrivateDirExists(mNoBackupFilesDir);
557 public File getExternalFilesDir(String type) {
558 // Operates on primary external storage
559 return getExternalFilesDirs(type)[0];
563 public File[] getExternalFilesDirs(String type) {
564 synchronized (mSync) {
565 if (mExternalFilesDirs == null) {
566 mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
569 // Splice in requested type, if any
570 File[] dirs = mExternalFilesDirs;
572 dirs = Environment.buildPaths(dirs, type);
575 // Create dirs if needed
576 return ensureExternalDirsExistOrFilter(dirs);
581 public File getObbDir() {
582 // Operates on primary external storage
583 return getObbDirs()[0];
587 public File[] getObbDirs() {
588 synchronized (mSync) {
589 if (mExternalObbDirs == null) {
590 mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
593 // Create dirs if needed
594 return ensureExternalDirsExistOrFilter(mExternalObbDirs);
599 public File getCacheDir() {
600 synchronized (mSync) {
601 if (mCacheDir == null) {
602 mCacheDir = new File(getDataDir(), "cache");
604 return ensurePrivateDirExists(mCacheDir);
609 public File getCodeCacheDir() {
610 synchronized (mSync) {
611 if (mCodeCacheDir == null) {
612 mCodeCacheDir = new File(getDataDir(), "code_cache");
614 return ensurePrivateDirExists(mCodeCacheDir);
619 public File getExternalCacheDir() {
620 // Operates on primary external storage
621 return getExternalCacheDirs()[0];
625 public File[] getExternalCacheDirs() {
626 synchronized (mSync) {
627 if (mExternalCacheDirs == null) {
628 mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
631 // Create dirs if needed
632 return ensureExternalDirsExistOrFilter(mExternalCacheDirs);
637 public File[] getExternalMediaDirs() {
638 synchronized (mSync) {
639 if (mExternalMediaDirs == null) {
640 mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
643 // Create dirs if needed
644 return ensureExternalDirsExistOrFilter(mExternalMediaDirs);
649 public File getFileStreamPath(String name) {
650 return makeFilename(getFilesDir(), name);
654 public File getSharedPreferencesPath(String name) {
655 return makeFilename(getPreferencesDir(), name + ".xml");
659 public String[] fileList() {
660 return FileUtils.listOrEmpty(getFilesDir());
664 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
665 return openOrCreateDatabase(name, mode, factory, null);
669 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
670 DatabaseErrorHandler errorHandler) {
672 File f = getDatabasePath(name);
673 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
674 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
675 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
677 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
678 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
680 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
681 setFilePermissionsFromMode(f.getPath(), mode, 0);
686 public boolean moveDatabaseFrom(Context sourceContext, String name) {
687 synchronized (ContextImpl.class) {
688 final File source = sourceContext.getDatabasePath(name);
689 final File target = getDatabasePath(name);
690 return moveFiles(source.getParentFile(), target.getParentFile(),
691 source.getName()) != -1;
696 public boolean deleteDatabase(String name) {
698 File f = getDatabasePath(name);
699 return SQLiteDatabase.deleteDatabase(f);
700 } catch (Exception e) {
706 public File getDatabasePath(String name) {
710 if (name.charAt(0) == File.separatorChar) {
711 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
712 dir = new File(dirPath);
713 name = name.substring(name.lastIndexOf(File.separatorChar));
714 f = new File(dir, name);
716 if (!dir.isDirectory() && dir.mkdir()) {
717 FileUtils.setPermissions(dir.getPath(),
718 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
722 dir = getDatabasesDir();
723 f = makeFilename(dir, name);
730 public String[] databaseList() {
731 return FileUtils.listOrEmpty(getDatabasesDir());
734 private File getDatabasesDir() {
735 synchronized (mSync) {
736 if (mDatabasesDir == null) {
737 if ("android".equals(getPackageName())) {
738 mDatabasesDir = new File("/data/system");
740 mDatabasesDir = new File(getDataDir(), "databases");
743 return ensurePrivateDirExists(mDatabasesDir);
749 public Drawable getWallpaper() {
750 return getWallpaperManager().getDrawable();
755 public Drawable peekWallpaper() {
756 return getWallpaperManager().peekDrawable();
761 public int getWallpaperDesiredMinimumWidth() {
762 return getWallpaperManager().getDesiredMinimumWidth();
767 public int getWallpaperDesiredMinimumHeight() {
768 return getWallpaperManager().getDesiredMinimumHeight();
773 public void setWallpaper(Bitmap bitmap) throws IOException {
774 getWallpaperManager().setBitmap(bitmap);
779 public void setWallpaper(InputStream data) throws IOException {
780 getWallpaperManager().setStream(data);
785 public void clearWallpaper() throws IOException {
786 getWallpaperManager().clear();
789 private WallpaperManager getWallpaperManager() {
790 return getSystemService(WallpaperManager.class);
794 public void startActivity(Intent intent) {
795 warnIfCallingFromSystemProcess();
796 startActivity(intent, null);
801 public void startActivityAsUser(Intent intent, UserHandle user) {
802 startActivityAsUser(intent, null, user);
806 public void startActivity(Intent intent, Bundle options) {
807 warnIfCallingFromSystemProcess();
809 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
810 // generally not allowed, except if the caller specifies the task id the activity should
812 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
813 && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
814 throw new AndroidRuntimeException(
815 "Calling startActivity() from outside of an Activity "
816 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
817 + " Is this really what you want?");
819 mMainThread.getInstrumentation().execStartActivity(
820 getOuterContext(), mMainThread.getApplicationThread(), null,
821 (Activity) null, intent, -1, options);
826 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
828 ActivityManagerNative.getDefault().startActivityAsUser(
829 mMainThread.getApplicationThread(), getBasePackageName(), intent,
830 intent.resolveTypeIfNeeded(getContentResolver()),
831 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
832 user.getIdentifier());
833 } catch (RemoteException e) {
834 throw e.rethrowFromSystemServer();
839 public void startActivities(Intent[] intents) {
840 warnIfCallingFromSystemProcess();
841 startActivities(intents, null);
846 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
847 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
848 throw new AndroidRuntimeException(
849 "Calling startActivities() from outside of an Activity "
850 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
851 + " Is this really what you want?");
853 mMainThread.getInstrumentation().execStartActivitiesAsUser(
854 getOuterContext(), mMainThread.getApplicationThread(), null,
855 (Activity) null, intents, options, userHandle.getIdentifier());
859 public void startActivities(Intent[] intents, Bundle options) {
860 warnIfCallingFromSystemProcess();
861 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
862 throw new AndroidRuntimeException(
863 "Calling startActivities() from outside of an Activity "
864 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
865 + " Is this really what you want?");
867 mMainThread.getInstrumentation().execStartActivities(
868 getOuterContext(), mMainThread.getApplicationThread(), null,
869 (Activity) null, intents, options);
873 public void startIntentSender(IntentSender intent,
874 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
875 throws IntentSender.SendIntentException {
876 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
880 public void startIntentSender(IntentSender intent, Intent fillInIntent,
881 int flagsMask, int flagsValues, int extraFlags, Bundle options)
882 throws IntentSender.SendIntentException {
884 String resolvedType = null;
885 if (fillInIntent != null) {
886 fillInIntent.migrateExtraStreamToClipData();
887 fillInIntent.prepareToLeaveProcess(this);
888 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
890 int result = ActivityManagerNative.getDefault()
891 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
892 fillInIntent, resolvedType, null, null,
893 0, flagsMask, flagsValues, options);
894 if (result == ActivityManager.START_CANCELED) {
895 throw new IntentSender.SendIntentException();
897 Instrumentation.checkStartActivityResult(result, null);
898 } catch (RemoteException e) {
899 throw e.rethrowFromSystemServer();
904 public void sendBroadcast(Intent intent) {
905 warnIfCallingFromSystemProcess();
906 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
908 intent.prepareToLeaveProcess(this);
909 ActivityManagerNative.getDefault().broadcastIntent(
910 mMainThread.getApplicationThread(), intent, resolvedType, null,
911 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
913 } catch (RemoteException e) {
914 throw e.rethrowFromSystemServer();
919 public void sendBroadcast(Intent intent, String receiverPermission) {
920 warnIfCallingFromSystemProcess();
921 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
922 String[] receiverPermissions = receiverPermission == null ? null
923 : new String[] {receiverPermission};
925 intent.prepareToLeaveProcess(this);
926 ActivityManagerNative.getDefault().broadcastIntent(
927 mMainThread.getApplicationThread(), intent, resolvedType, null,
928 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
929 null, false, false, getUserId());
930 } catch (RemoteException e) {
931 throw e.rethrowFromSystemServer();
936 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
937 warnIfCallingFromSystemProcess();
938 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
940 intent.prepareToLeaveProcess(this);
941 ActivityManagerNative.getDefault().broadcastIntent(
942 mMainThread.getApplicationThread(), intent, resolvedType, null,
943 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
944 null, false, false, getUserId());
945 } catch (RemoteException e) {
946 throw e.rethrowFromSystemServer();
951 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
952 warnIfCallingFromSystemProcess();
953 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
954 String[] receiverPermissions = receiverPermission == null ? null
955 : new String[] {receiverPermission};
957 intent.prepareToLeaveProcess(this);
958 ActivityManagerNative.getDefault().broadcastIntent(
959 mMainThread.getApplicationThread(), intent, resolvedType, null,
960 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
961 options, false, false, getUserId());
962 } catch (RemoteException e) {
963 throw e.rethrowFromSystemServer();
968 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
969 warnIfCallingFromSystemProcess();
970 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
971 String[] receiverPermissions = receiverPermission == null ? null
972 : new String[] {receiverPermission};
974 intent.prepareToLeaveProcess(this);
975 ActivityManagerNative.getDefault().broadcastIntent(
976 mMainThread.getApplicationThread(), intent, resolvedType, null,
977 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
979 } catch (RemoteException e) {
980 throw e.rethrowFromSystemServer();
985 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
986 warnIfCallingFromSystemProcess();
987 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
988 String[] receiverPermissions = receiverPermission == null ? null
989 : new String[] {receiverPermission};
991 intent.prepareToLeaveProcess(this);
992 ActivityManagerNative.getDefault().broadcastIntent(
993 mMainThread.getApplicationThread(), intent, resolvedType, null,
994 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
995 null, true, false, getUserId());
996 } catch (RemoteException e) {
997 throw e.rethrowFromSystemServer();
1002 public void sendOrderedBroadcast(Intent intent,
1003 String receiverPermission, BroadcastReceiver resultReceiver,
1004 Handler scheduler, int initialCode, String initialData,
1005 Bundle initialExtras) {
1006 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1007 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1011 public void sendOrderedBroadcast(Intent intent,
1012 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
1013 Handler scheduler, int initialCode, String initialData,
1014 Bundle initialExtras) {
1015 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1016 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
1020 public void sendOrderedBroadcast(Intent intent,
1021 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1022 Handler scheduler, int initialCode, String initialData,
1023 Bundle initialExtras) {
1024 sendOrderedBroadcast(intent, receiverPermission, appOp,
1025 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1028 void sendOrderedBroadcast(Intent intent,
1029 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1030 Handler scheduler, int initialCode, String initialData,
1031 Bundle initialExtras, Bundle options) {
1032 warnIfCallingFromSystemProcess();
1033 IIntentReceiver rd = null;
1034 if (resultReceiver != null) {
1035 if (mPackageInfo != null) {
1036 if (scheduler == null) {
1037 scheduler = mMainThread.getHandler();
1039 rd = mPackageInfo.getReceiverDispatcher(
1040 resultReceiver, getOuterContext(), scheduler,
1041 mMainThread.getInstrumentation(), false);
1043 if (scheduler == null) {
1044 scheduler = mMainThread.getHandler();
1046 rd = new LoadedApk.ReceiverDispatcher(
1047 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1050 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1051 String[] receiverPermissions = receiverPermission == null ? null
1052 : new String[] {receiverPermission};
1054 intent.prepareToLeaveProcess(this);
1055 ActivityManagerNative.getDefault().broadcastIntent(
1056 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1057 initialCode, initialData, initialExtras, receiverPermissions, appOp,
1058 options, true, false, getUserId());
1059 } catch (RemoteException e) {
1060 throw e.rethrowFromSystemServer();
1065 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
1066 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1068 intent.prepareToLeaveProcess(this);
1069 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
1070 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
1071 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
1072 } catch (RemoteException e) {
1073 throw e.rethrowFromSystemServer();
1078 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1079 String receiverPermission) {
1080 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1084 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1085 String receiverPermission, int appOp) {
1086 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1087 String[] receiverPermissions = receiverPermission == null ? null
1088 : new String[] {receiverPermission};
1090 intent.prepareToLeaveProcess(this);
1091 ActivityManagerNative.getDefault().broadcastIntent(
1092 mMainThread.getApplicationThread(), intent, resolvedType, null,
1093 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
1094 user.getIdentifier());
1095 } catch (RemoteException e) {
1096 throw e.rethrowFromSystemServer();
1101 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1102 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
1103 int initialCode, String initialData, Bundle initialExtras) {
1104 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
1105 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1109 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1110 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1111 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1112 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1113 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1117 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1118 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1119 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1120 IIntentReceiver rd = null;
1121 if (resultReceiver != null) {
1122 if (mPackageInfo != null) {
1123 if (scheduler == null) {
1124 scheduler = mMainThread.getHandler();
1126 rd = mPackageInfo.getReceiverDispatcher(
1127 resultReceiver, getOuterContext(), scheduler,
1128 mMainThread.getInstrumentation(), false);
1130 if (scheduler == null) {
1131 scheduler = mMainThread.getHandler();
1133 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1134 scheduler, null, false).getIIntentReceiver();
1137 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1138 String[] receiverPermissions = receiverPermission == null ? null
1139 : new String[] {receiverPermission};
1141 intent.prepareToLeaveProcess(this);
1142 ActivityManagerNative.getDefault().broadcastIntent(
1143 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1144 initialCode, initialData, initialExtras, receiverPermissions,
1145 appOp, options, true, false, user.getIdentifier());
1146 } catch (RemoteException e) {
1147 throw e.rethrowFromSystemServer();
1153 public void sendStickyBroadcast(Intent intent) {
1154 warnIfCallingFromSystemProcess();
1155 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1157 intent.prepareToLeaveProcess(this);
1158 ActivityManagerNative.getDefault().broadcastIntent(
1159 mMainThread.getApplicationThread(), intent, resolvedType, null,
1160 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1162 } catch (RemoteException e) {
1163 throw e.rethrowFromSystemServer();
1169 public void sendStickyOrderedBroadcast(Intent intent,
1170 BroadcastReceiver resultReceiver,
1171 Handler scheduler, int initialCode, String initialData,
1172 Bundle initialExtras) {
1173 warnIfCallingFromSystemProcess();
1174 IIntentReceiver rd = null;
1175 if (resultReceiver != null) {
1176 if (mPackageInfo != null) {
1177 if (scheduler == null) {
1178 scheduler = mMainThread.getHandler();
1180 rd = mPackageInfo.getReceiverDispatcher(
1181 resultReceiver, getOuterContext(), scheduler,
1182 mMainThread.getInstrumentation(), false);
1184 if (scheduler == null) {
1185 scheduler = mMainThread.getHandler();
1187 rd = new LoadedApk.ReceiverDispatcher(
1188 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1191 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1193 intent.prepareToLeaveProcess(this);
1194 ActivityManagerNative.getDefault().broadcastIntent(
1195 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1196 initialCode, initialData, initialExtras, null,
1197 AppOpsManager.OP_NONE, null, true, true, getUserId());
1198 } catch (RemoteException e) {
1199 throw e.rethrowFromSystemServer();
1205 public void removeStickyBroadcast(Intent intent) {
1206 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1207 if (resolvedType != null) {
1208 intent = new Intent(intent);
1209 intent.setDataAndType(intent.getData(), resolvedType);
1212 intent.prepareToLeaveProcess(this);
1213 ActivityManagerNative.getDefault().unbroadcastIntent(
1214 mMainThread.getApplicationThread(), intent, getUserId());
1215 } catch (RemoteException e) {
1216 throw e.rethrowFromSystemServer();
1222 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1223 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1225 intent.prepareToLeaveProcess(this);
1226 ActivityManagerNative.getDefault().broadcastIntent(
1227 mMainThread.getApplicationThread(), intent, resolvedType, null,
1228 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1229 user.getIdentifier());
1230 } catch (RemoteException e) {
1231 throw e.rethrowFromSystemServer();
1237 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1238 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1240 intent.prepareToLeaveProcess(this);
1241 ActivityManagerNative.getDefault().broadcastIntent(
1242 mMainThread.getApplicationThread(), intent, resolvedType, null,
1243 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1244 user.getIdentifier());
1245 } catch (RemoteException e) {
1246 throw e.rethrowFromSystemServer();
1252 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1253 UserHandle user, BroadcastReceiver resultReceiver,
1254 Handler scheduler, int initialCode, String initialData,
1255 Bundle initialExtras) {
1256 IIntentReceiver rd = null;
1257 if (resultReceiver != null) {
1258 if (mPackageInfo != null) {
1259 if (scheduler == null) {
1260 scheduler = mMainThread.getHandler();
1262 rd = mPackageInfo.getReceiverDispatcher(
1263 resultReceiver, getOuterContext(), scheduler,
1264 mMainThread.getInstrumentation(), false);
1266 if (scheduler == null) {
1267 scheduler = mMainThread.getHandler();
1269 rd = new LoadedApk.ReceiverDispatcher(
1270 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1273 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1275 intent.prepareToLeaveProcess(this);
1276 ActivityManagerNative.getDefault().broadcastIntent(
1277 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1278 initialCode, initialData, initialExtras, null,
1279 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
1280 } catch (RemoteException e) {
1281 throw e.rethrowFromSystemServer();
1287 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1288 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1289 if (resolvedType != null) {
1290 intent = new Intent(intent);
1291 intent.setDataAndType(intent.getData(), resolvedType);
1294 intent.prepareToLeaveProcess(this);
1295 ActivityManagerNative.getDefault().unbroadcastIntent(
1296 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1297 } catch (RemoteException e) {
1298 throw e.rethrowFromSystemServer();
1303 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1304 return registerReceiver(receiver, filter, null, null);
1308 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1309 String broadcastPermission, Handler scheduler) {
1310 return registerReceiverInternal(receiver, getUserId(),
1311 filter, broadcastPermission, scheduler, getOuterContext());
1315 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1316 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1317 return registerReceiverInternal(receiver, user.getIdentifier(),
1318 filter, broadcastPermission, scheduler, getOuterContext());
1321 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
1322 IntentFilter filter, String broadcastPermission,
1323 Handler scheduler, Context context) {
1324 IIntentReceiver rd = null;
1325 if (receiver != null) {
1326 if (mPackageInfo != null && context != null) {
1327 if (scheduler == null) {
1328 scheduler = mMainThread.getHandler();
1330 rd = mPackageInfo.getReceiverDispatcher(
1331 receiver, context, scheduler,
1332 mMainThread.getInstrumentation(), true);
1334 if (scheduler == null) {
1335 scheduler = mMainThread.getHandler();
1337 rd = new LoadedApk.ReceiverDispatcher(
1338 receiver, context, scheduler, null, true).getIIntentReceiver();
1342 final Intent intent = ActivityManagerNative.getDefault().registerReceiver(
1343 mMainThread.getApplicationThread(), mBasePackageName,
1344 rd, filter, broadcastPermission, userId);
1345 if (intent != null) {
1346 intent.setExtrasClassLoader(getClassLoader());
1347 intent.prepareToEnterProcess();
1350 } catch (RemoteException e) {
1351 throw e.rethrowFromSystemServer();
1356 public void unregisterReceiver(BroadcastReceiver receiver) {
1357 if (mPackageInfo != null) {
1358 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1359 getOuterContext(), receiver);
1361 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1362 } catch (RemoteException e) {
1363 throw e.rethrowFromSystemServer();
1366 throw new RuntimeException("Not supported in system context");
1370 private void validateServiceIntent(Intent service) {
1371 if (service.getComponent() == null && service.getPackage() == null) {
1372 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
1373 IllegalArgumentException ex = new IllegalArgumentException(
1374 "Service Intent must be explicit: " + service);
1377 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1378 + " " + Debug.getCallers(2, 3));
1384 public ComponentName startService(Intent service) {
1385 warnIfCallingFromSystemProcess();
1386 return startServiceCommon(service, mUser);
1390 public boolean stopService(Intent service) {
1391 warnIfCallingFromSystemProcess();
1392 return stopServiceCommon(service, mUser);
1396 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
1397 return startServiceCommon(service, user);
1400 private ComponentName startServiceCommon(Intent service, UserHandle user) {
1402 validateServiceIntent(service);
1403 service.prepareToLeaveProcess(this);
1404 ComponentName cn = ActivityManagerNative.getDefault().startService(
1405 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
1406 getContentResolver()), getOpPackageName(), user.getIdentifier());
1408 if (cn.getPackageName().equals("!")) {
1409 throw new SecurityException(
1410 "Not allowed to start service " + service
1411 + " without permission " + cn.getClassName());
1412 } else if (cn.getPackageName().equals("!!")) {
1413 throw new SecurityException(
1414 "Unable to start service " + service
1415 + ": " + cn.getClassName());
1419 } catch (RemoteException e) {
1420 throw e.rethrowFromSystemServer();
1425 public boolean stopServiceAsUser(Intent service, UserHandle user) {
1426 return stopServiceCommon(service, user);
1429 private boolean stopServiceCommon(Intent service, UserHandle user) {
1431 validateServiceIntent(service);
1432 service.prepareToLeaveProcess(this);
1433 int res = ActivityManagerNative.getDefault().stopService(
1434 mMainThread.getApplicationThread(), service,
1435 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
1437 throw new SecurityException(
1438 "Not allowed to stop service " + service);
1441 } catch (RemoteException e) {
1442 throw e.rethrowFromSystemServer();
1447 public boolean bindService(Intent service, ServiceConnection conn,
1449 warnIfCallingFromSystemProcess();
1450 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1451 Process.myUserHandle());
1456 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1458 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
1463 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1464 Handler handler, UserHandle user) {
1465 if (handler == null) {
1466 throw new IllegalArgumentException("handler must not be null.");
1468 return bindServiceCommon(service, conn, flags, handler, user);
1471 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1472 handler, UserHandle user) {
1473 IServiceConnection sd;
1475 throw new IllegalArgumentException("connection is null");
1477 if (mPackageInfo != null) {
1478 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
1480 throw new RuntimeException("Not supported in system context");
1482 validateServiceIntent(service);
1484 IBinder token = getActivityToken();
1485 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1486 && mPackageInfo.getApplicationInfo().targetSdkVersion
1487 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1488 flags |= BIND_WAIVE_PRIORITY;
1490 service.prepareToLeaveProcess(this);
1491 int res = ActivityManagerNative.getDefault().bindService(
1492 mMainThread.getApplicationThread(), getActivityToken(), service,
1493 service.resolveTypeIfNeeded(getContentResolver()),
1494 sd, flags, getOpPackageName(), user.getIdentifier());
1496 throw new SecurityException(
1497 "Not allowed to bind to service " + service);
1500 } catch (RemoteException e) {
1501 throw e.rethrowFromSystemServer();
1506 public void unbindService(ServiceConnection conn) {
1508 throw new IllegalArgumentException("connection is null");
1510 if (mPackageInfo != null) {
1511 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1512 getOuterContext(), conn);
1514 ActivityManagerNative.getDefault().unbindService(sd);
1515 } catch (RemoteException e) {
1516 throw e.rethrowFromSystemServer();
1519 throw new RuntimeException("Not supported in system context");
1524 public boolean startInstrumentation(ComponentName className,
1525 String profileFile, Bundle arguments) {
1527 if (arguments != null) {
1528 arguments.setAllowFds(false);
1530 return ActivityManagerNative.getDefault().startInstrumentation(
1531 className, profileFile, 0, arguments, null, null, getUserId(),
1532 null /* ABI override */);
1533 } catch (RemoteException e) {
1534 throw e.rethrowFromSystemServer();
1539 public Object getSystemService(String name) {
1540 return SystemServiceRegistry.getSystemService(this, name);
1544 public String getSystemServiceName(Class<?> serviceClass) {
1545 return SystemServiceRegistry.getSystemServiceName(serviceClass);
1549 public int checkPermission(String permission, int pid, int uid) {
1550 if (permission == null) {
1551 throw new IllegalArgumentException("permission is null");
1555 return ActivityManagerNative.getDefault().checkPermission(
1556 permission, pid, uid);
1557 } catch (RemoteException e) {
1558 throw e.rethrowFromSystemServer();
1564 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1565 if (permission == null) {
1566 throw new IllegalArgumentException("permission is null");
1570 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1571 permission, pid, uid, callerToken);
1572 } catch (RemoteException e) {
1573 throw e.rethrowFromSystemServer();
1578 public int checkCallingPermission(String permission) {
1579 if (permission == null) {
1580 throw new IllegalArgumentException("permission is null");
1583 int pid = Binder.getCallingPid();
1584 if (pid != Process.myPid()) {
1585 return checkPermission(permission, pid, Binder.getCallingUid());
1587 return PackageManager.PERMISSION_DENIED;
1591 public int checkCallingOrSelfPermission(String permission) {
1592 if (permission == null) {
1593 throw new IllegalArgumentException("permission is null");
1596 return checkPermission(permission, Binder.getCallingPid(),
1597 Binder.getCallingUid());
1601 public int checkSelfPermission(String permission) {
1602 if (permission == null) {
1603 throw new IllegalArgumentException("permission is null");
1606 return checkPermission(permission, Process.myPid(), Process.myUid());
1609 private void enforce(
1610 String permission, int resultOfCheck,
1611 boolean selfToo, int uid, String message) {
1612 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1613 throw new SecurityException(
1614 (message != null ? (message + ": ") : "") +
1616 ? "Neither user " + uid + " nor current process has "
1617 : "uid " + uid + " does not have ") +
1624 public void enforcePermission(
1625 String permission, int pid, int uid, String message) {
1627 checkPermission(permission, pid, uid),
1634 public void enforceCallingPermission(String permission, String message) {
1636 checkCallingPermission(permission),
1638 Binder.getCallingUid(),
1643 public void enforceCallingOrSelfPermission(
1644 String permission, String message) {
1646 checkCallingOrSelfPermission(permission),
1648 Binder.getCallingUid(),
1653 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1655 ActivityManagerNative.getDefault().grantUriPermission(
1656 mMainThread.getApplicationThread(), toPackage,
1657 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
1658 } catch (RemoteException e) {
1659 throw e.rethrowFromSystemServer();
1664 public void revokeUriPermission(Uri uri, int modeFlags) {
1666 ActivityManagerNative.getDefault().revokeUriPermission(
1667 mMainThread.getApplicationThread(),
1668 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
1669 } catch (RemoteException e) {
1670 throw e.rethrowFromSystemServer();
1675 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
1677 return ActivityManagerNative.getDefault().checkUriPermission(
1678 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1679 resolveUserId(uri), null);
1680 } catch (RemoteException e) {
1681 throw e.rethrowFromSystemServer();
1687 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1689 return ActivityManagerNative.getDefault().checkUriPermission(
1690 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1691 resolveUserId(uri), callerToken);
1692 } catch (RemoteException e) {
1693 throw e.rethrowFromSystemServer();
1697 private int resolveUserId(Uri uri) {
1698 return ContentProvider.getUserIdFromUri(uri, getUserId());
1702 public int checkCallingUriPermission(Uri uri, int modeFlags) {
1703 int pid = Binder.getCallingPid();
1704 if (pid != Process.myPid()) {
1705 return checkUriPermission(uri, pid,
1706 Binder.getCallingUid(), modeFlags);
1708 return PackageManager.PERMISSION_DENIED;
1712 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1713 return checkUriPermission(uri, Binder.getCallingPid(),
1714 Binder.getCallingUid(), modeFlags);
1718 public int checkUriPermission(Uri uri, String readPermission,
1719 String writePermission, int pid, int uid, int modeFlags) {
1721 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1722 + readPermission + " writePermission=" + writePermission
1723 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1725 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1726 if (readPermission == null
1727 || checkPermission(readPermission, pid, uid)
1728 == PackageManager.PERMISSION_GRANTED) {
1729 return PackageManager.PERMISSION_GRANTED;
1732 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1733 if (writePermission == null
1734 || checkPermission(writePermission, pid, uid)
1735 == PackageManager.PERMISSION_GRANTED) {
1736 return PackageManager.PERMISSION_GRANTED;
1739 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1740 : PackageManager.PERMISSION_DENIED;
1743 private String uriModeFlagToString(int uriModeFlags) {
1744 StringBuilder builder = new StringBuilder();
1745 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1746 builder.append("read and ");
1748 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1749 builder.append("write and ");
1751 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1752 builder.append("persistable and ");
1754 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1755 builder.append("prefix and ");
1758 if (builder.length() > 5) {
1759 builder.setLength(builder.length() - 5);
1760 return builder.toString();
1762 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1766 private void enforceForUri(
1767 int modeFlags, int resultOfCheck, boolean selfToo,
1768 int uid, Uri uri, String message) {
1769 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1770 throw new SecurityException(
1771 (message != null ? (message + ": ") : "") +
1773 ? "Neither user " + uid + " nor current process has "
1774 : "User " + uid + " does not have ") +
1775 uriModeFlagToString(modeFlags) +
1783 public void enforceUriPermission(
1784 Uri uri, int pid, int uid, int modeFlags, String message) {
1786 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1787 false, uid, uri, message);
1791 public void enforceCallingUriPermission(
1792 Uri uri, int modeFlags, String message) {
1794 modeFlags, checkCallingUriPermission(uri, modeFlags),
1796 Binder.getCallingUid(), uri, message);
1800 public void enforceCallingOrSelfUriPermission(
1801 Uri uri, int modeFlags, String message) {
1804 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1805 Binder.getCallingUid(), uri, message);
1809 public void enforceUriPermission(
1810 Uri uri, String readPermission, String writePermission,
1811 int pid, int uid, int modeFlags, String message) {
1812 enforceForUri(modeFlags,
1814 uri, readPermission, writePermission, pid, uid,
1823 * Logs a warning if the system process directly called a method such as
1824 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1825 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1827 private void warnIfCallingFromSystemProcess() {
1828 if (Process.myUid() == Process.SYSTEM_UID) {
1829 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
1830 + Debug.getCallers(5));
1835 public Context createApplicationContext(ApplicationInfo application, int flags)
1836 throws NameNotFoundException {
1837 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1838 flags | CONTEXT_REGISTER_PACKAGE);
1840 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
1841 new UserHandle(UserHandle.getUserId(application.uid)), flags,
1842 mDisplay, null, Display.INVALID_DISPLAY);
1843 if (c.mResources != null) {
1848 throw new PackageManager.NameNotFoundException(
1849 "Application package " + application.packageName + " not found");
1853 public Context createPackageContext(String packageName, int flags)
1854 throws NameNotFoundException {
1855 return createPackageContextAsUser(packageName, flags,
1856 mUser != null ? mUser : Process.myUserHandle());
1860 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1861 throws NameNotFoundException {
1862 if (packageName.equals("system") || packageName.equals("android")) {
1863 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1864 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
1867 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
1868 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
1870 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
1871 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
1872 if (c.mResources != null) {
1877 // Should be a better exception.
1878 throw new PackageManager.NameNotFoundException(
1879 "Application package " + packageName + " not found");
1883 public Context createConfigurationContext(Configuration overrideConfiguration) {
1884 if (overrideConfiguration == null) {
1885 throw new IllegalArgumentException("overrideConfiguration must not be null");
1888 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1889 mUser, mFlags, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY);
1893 public Context createDisplayContext(Display display) {
1894 if (display == null) {
1895 throw new IllegalArgumentException("display must not be null");
1898 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1899 mUser, mFlags, display, null, Display.INVALID_DISPLAY);
1903 public Context createDeviceProtectedStorageContext() {
1904 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
1905 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
1906 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1907 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1911 public Context createCredentialProtectedStorageContext() {
1912 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
1913 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
1914 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1915 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1919 public boolean isRestricted() {
1920 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
1924 public boolean isDeviceProtectedStorage() {
1925 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
1929 public boolean isCredentialProtectedStorage() {
1930 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
1934 public Display getDisplay() {
1935 final DisplayAdjustments displayAdjustments = mResources.getDisplayAdjustments();
1936 if (mDisplay == null) {
1937 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
1938 displayAdjustments);
1941 if (!mDisplay.getDisplayAdjustments().equals(displayAdjustments)) {
1942 mDisplay = mResourcesManager.getAdjustedDisplay(mDisplay.getDisplayId(),
1943 displayAdjustments);
1949 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1950 return mResources.getDisplayAdjustments();
1954 public File getDataDir() {
1955 if (mPackageInfo != null) {
1957 if (isCredentialProtectedStorage()) {
1958 res = mPackageInfo.getCredentialProtectedDataDirFile();
1959 } else if (isDeviceProtectedStorage()) {
1960 res = mPackageInfo.getDeviceProtectedDataDirFile();
1962 res = mPackageInfo.getDataDirFile();
1966 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
1967 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
1972 throw new RuntimeException(
1973 "No data directory found for package " + getPackageName());
1976 throw new RuntimeException(
1977 "No package details found for package " + getPackageName());
1982 public File getDir(String name, int mode) {
1984 name = "app_" + name;
1985 File file = makeFilename(getDataDir(), name);
1986 if (!file.exists()) {
1988 setFilePermissionsFromMode(file.getPath(), mode,
1989 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1996 public int getUserId() {
1997 return mUser.getIdentifier();
2000 static ContextImpl createSystemContext(ActivityThread mainThread) {
2001 LoadedApk packageInfo = new LoadedApk(mainThread);
2002 ContextImpl context = new ContextImpl(null, mainThread,
2003 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
2004 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
2005 context.mResourcesManager.getDisplayMetrics());
2009 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
2010 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
2011 return new ContextImpl(null, mainThread,
2012 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
2015 static ContextImpl createActivityContext(ActivityThread mainThread,
2016 LoadedApk packageInfo, IBinder activityToken, int displayId,
2017 Configuration overrideConfiguration) {
2018 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
2019 return new ContextImpl(null, mainThread, packageInfo, activityToken, null, 0,
2020 null, overrideConfiguration, displayId);
2023 private ContextImpl(ContextImpl container, ActivityThread mainThread,
2024 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,
2025 Display display, Configuration overrideConfiguration, int createDisplayWithId) {
2026 mOuterContext = this;
2028 // If creator didn't specify which storage to use, use the default
2029 // location for application.
2030 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2031 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
2032 final File dataDir = packageInfo.getDataDirFile();
2033 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
2034 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
2035 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
2036 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
2040 mMainThread = mainThread;
2041 mActivityToken = activityToken;
2045 user = Process.myUserHandle();
2049 mPackageInfo = packageInfo;
2050 mResourcesManager = ResourcesManager.getInstance();
2052 final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
2053 ? createDisplayWithId
2054 : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
2056 CompatibilityInfo compatInfo = null;
2057 if (container != null) {
2058 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
2060 if (compatInfo == null) {
2061 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2062 ? packageInfo.getCompatibilityInfo()
2063 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
2066 Resources resources = packageInfo.getResources(mainThread);
2067 if (resources != null) {
2068 if (displayId != Display.DEFAULT_DISPLAY
2069 || overrideConfiguration != null
2070 || (compatInfo != null && compatInfo.applicationScale
2071 != resources.getCompatibilityInfo().applicationScale)) {
2073 if (container != null) {
2074 // This is a nested Context, so it can't be a base Activity context.
2075 // Just create a regular Resources object associated with the Activity.
2076 resources = mResourcesManager.getResources(
2078 packageInfo.getResDir(),
2079 packageInfo.getSplitResDirs(),
2080 packageInfo.getOverlayDirs(),
2081 packageInfo.getApplicationInfo().sharedLibraryFiles,
2083 overrideConfiguration,
2085 packageInfo.getClassLoader());
2087 // This is not a nested Context, so it must be the root Activity context.
2088 // All other nested Contexts will inherit the configuration set here.
2089 resources = mResourcesManager.createBaseActivityResources(
2091 packageInfo.getResDir(),
2092 packageInfo.getSplitResDirs(),
2093 packageInfo.getOverlayDirs(),
2094 packageInfo.getApplicationInfo().sharedLibraryFiles,
2096 overrideConfiguration,
2098 packageInfo.getClassLoader());
2102 mResources = resources;
2104 mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
2105 : mResourcesManager.getAdjustedDisplay(displayId, mResources.getDisplayAdjustments());
2107 if (container != null) {
2108 mBasePackageName = container.mBasePackageName;
2109 mOpPackageName = container.mOpPackageName;
2111 mBasePackageName = packageInfo.mPackageName;
2112 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2113 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2114 // Special case: system components allow themselves to be loaded in to other
2115 // processes. For purposes of app ops, we must then consider the context as
2116 // belonging to the package of this process, not the system itself, otherwise
2117 // the package+uid verifications in app ops will fail.
2118 mOpPackageName = ActivityThread.currentPackageName();
2120 mOpPackageName = mBasePackageName;
2124 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
2127 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2128 mPackageInfo.installSystemApplicationInfo(info, classLoader);
2131 final void scheduleFinalCleanup(String who, String what) {
2132 mMainThread.scheduleContextCleanup(this, who, what);
2135 final void performFinalCleanup(String who, String what) {
2136 //Log.i(TAG, "Cleanup up context: " + this);
2137 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2140 final Context getReceiverRestrictedContext() {
2141 if (mReceiverRestrictedContext != null) {
2142 return mReceiverRestrictedContext;
2144 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2147 final void setOuterContext(Context context) {
2148 mOuterContext = context;
2151 final Context getOuterContext() {
2152 return mOuterContext;
2155 final IBinder getActivityToken() {
2156 return mActivityToken;
2159 private void checkMode(int mode) {
2160 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2161 if ((mode & MODE_WORLD_READABLE) != 0) {
2162 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2164 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2165 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2170 @SuppressWarnings("deprecation")
2171 static void setFilePermissionsFromMode(String name, int mode,
2172 int extraPermissions) {
2173 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2174 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2176 if ((mode&MODE_WORLD_READABLE) != 0) {
2177 perms |= FileUtils.S_IROTH;
2179 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2180 perms |= FileUtils.S_IWOTH;
2183 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2184 + ", perms=0x" + Integer.toHexString(perms));
2186 FileUtils.setPermissions(name, perms, -1, -1);
2189 private File makeFilename(File base, String name) {
2190 if (name.indexOf(File.separatorChar) < 0) {
2191 return new File(base, name);
2193 throw new IllegalArgumentException(
2194 "File " + name + " contains a path separator");
2198 * Ensure that given directories exist, trying to create them if missing. If
2199 * unable to create, they are filtered by replacing with {@code null}.
2201 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
2202 File[] result = new File[dirs.length];
2203 for (int i = 0; i < dirs.length; i++) {
2205 if (!dir.exists()) {
2206 if (!dir.mkdirs()) {
2207 // recheck existence in case of cross-process race
2208 if (!dir.exists()) {
2209 // Failing to mkdir() may be okay, since we might not have
2210 // enough permissions; ask vold to create on our behalf.
2211 final IMountService mount = IMountService.Stub.asInterface(
2212 ServiceManager.getService("mount"));
2214 final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
2216 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2219 } catch (Exception e) {
2220 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
2231 // ----------------------------------------------------------------------
2232 // ----------------------------------------------------------------------
2233 // ----------------------------------------------------------------------
2235 private static final class ApplicationContentResolver extends ContentResolver {
2236 private final ActivityThread mMainThread;
2237 private final UserHandle mUser;
2239 public ApplicationContentResolver(
2240 Context context, ActivityThread mainThread, UserHandle user) {
2242 mMainThread = Preconditions.checkNotNull(mainThread);
2243 mUser = Preconditions.checkNotNull(user);
2247 protected IContentProvider acquireProvider(Context context, String auth) {
2248 return mMainThread.acquireProvider(context,
2249 ContentProvider.getAuthorityWithoutUserId(auth),
2250 resolveUserIdFromAuthority(auth), true);
2254 protected IContentProvider acquireExistingProvider(Context context, String auth) {
2255 return mMainThread.acquireExistingProvider(context,
2256 ContentProvider.getAuthorityWithoutUserId(auth),
2257 resolveUserIdFromAuthority(auth), true);
2261 public boolean releaseProvider(IContentProvider provider) {
2262 return mMainThread.releaseProvider(provider, true);
2266 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
2267 return mMainThread.acquireProvider(c,
2268 ContentProvider.getAuthorityWithoutUserId(auth),
2269 resolveUserIdFromAuthority(auth), false);
2273 public boolean releaseUnstableProvider(IContentProvider icp) {
2274 return mMainThread.releaseProvider(icp, false);
2278 public void unstableProviderDied(IContentProvider icp) {
2279 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
2283 public void appNotRespondingViaProvider(IContentProvider icp) {
2284 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2288 protected int resolveUserIdFromAuthority(String auth) {
2289 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());