blob: 32ace144ddd822522ef2059ebe5d42b738f72e91 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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.app;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.BroadcastReceiver;
20import android.content.ComponentName;
Nicolas Prevotd85fc722014-04-16 19:52:08 +010021import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.ContextWrapper;
25import android.content.IContentProvider;
Jeff Brown6e539312015-02-24 18:53:21 -080026import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Intent;
28import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070029import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.ReceiverCallNotAllowedException;
31import android.content.ServiceConnection;
32import android.content.SharedPreferences;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070036import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070038import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070039import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070041import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.database.sqlite.SQLiteDatabase;
43import android.database.sqlite.SQLiteDatabase.CursorFactory;
44import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080048import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070050import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080051import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.FileUtils;
53import android.os.Handler;
54import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070059import android.os.UserHandle;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070060import android.os.storage.IMountService;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -070061import android.system.ErrnoException;
62import android.system.Os;
63import android.system.OsConstants;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070065import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070067import android.util.Slog;
Jeff Brown98365d72012-08-19 20:30:52 -070068import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080069import android.view.DisplayAdjustments;
Dan Egnor95240272009-10-27 18:23:39 -070070
Jeff Sharkey35871f22016-01-29 17:13:29 -070071import com.android.internal.annotations.GuardedBy;
72import com.android.internal.util.Preconditions;
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.io.File;
75import java.io.FileInputStream;
76import java.io.FileNotFoundException;
77import java.io.FileOutputStream;
Jeff Sharkey35871f22016-01-29 17:13:29 -070078import java.io.FilenameFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.io.IOException;
80import java.io.InputStream;
Jeff Sharkey7a30a302015-12-08 14:20:06 -070081import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083class ReceiverRestrictedContext extends ContextWrapper {
84 ReceiverRestrictedContext(Context base) {
85 super(base);
86 }
87
88 @Override
89 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
90 return registerReceiver(receiver, filter, null, null);
91 }
92
93 @Override
94 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
95 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070096 if (receiver == null) {
97 // Allow retrieving current sticky broadcast; this is safe since we
98 // aren't actually registering a receiver.
99 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
100 } else {
101 throw new ReceiverCallNotAllowedException(
102 "BroadcastReceiver components are not allowed to register to receive intents");
103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 }
105
106 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700107 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
108 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700109 if (receiver == null) {
110 // Allow retrieving current sticky broadcast; this is safe since we
111 // aren't actually registering a receiver.
112 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
113 } else {
114 throw new ReceiverCallNotAllowedException(
115 "BroadcastReceiver components are not allowed to register to receive intents");
116 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700117 }
118
119 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
121 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700122 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
124}
125
126/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800127 * Common implementation of Context API, which provides the base
128 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800130class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800131 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700132 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700134 /**
135 * Map from package name, to preference name, to cached preferences.
136 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700137 @GuardedBy("ContextImpl.class")
138 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
139
140 /**
141 * Map from preference name to generated path.
142 */
143 @GuardedBy("ContextImpl.class")
144 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Jeff Browndefd4a62014-03-10 21:24:37 -0700146 final ActivityThread mMainThread;
147 final LoadedApk mPackageInfo;
148
149 private final IBinder mActivityToken;
150
151 private final UserHandle mUser;
152
153 private final ApplicationContentResolver mContentResolver;
154
155 private final String mBasePackageName;
156 private final String mOpPackageName;
157
158 private final ResourcesManager mResourcesManager;
159 private final Resources mResources;
160 private final Display mDisplay; // may be null if default display
161 private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments();
Jeff Browndefd4a62014-03-10 21:24:37 -0700162
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700163 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 private int mThemeResource = 0;
167 private Resources.Theme mTheme = null;
168 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
171 private final Object mSync = new Object();
172
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700173 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700175 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700177 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700179 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700180 private File mNoBackupFilesDir;
181 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700183 @GuardedBy("mSync")
184 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700185
186 @GuardedBy("mSync")
187 private File[] mExternalObbDirs;
188 @GuardedBy("mSync")
189 private File[] mExternalFilesDirs;
190 @GuardedBy("mSync")
191 private File[] mExternalCacheDirs;
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700192 @GuardedBy("mSync")
193 private File[] mExternalMediaDirs;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200194
Jeff Brown6e539312015-02-24 18:53:21 -0800195 // The system service cache for the system services that are cached per-ContextImpl.
196 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800197
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700198 static ContextImpl getImpl(Context context) {
199 Context nextContext;
200 while ((context instanceof ContextWrapper) &&
201 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
202 context = nextContext;
203 }
204 return (ContextImpl)context;
205 }
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 @Override
208 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700209 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211
212 @Override
213 public Resources getResources() {
214 return mResources;
215 }
216
217 @Override
218 public PackageManager getPackageManager() {
219 if (mPackageManager != null) {
220 return mPackageManager;
221 }
222
223 IPackageManager pm = ActivityThread.getPackageManager();
224 if (pm != null) {
225 // Doesn't matter if we make more than one instance.
226 return (mPackageManager = new ApplicationPackageManager(this, pm));
227 }
228
229 return null;
230 }
231
232 @Override
233 public ContentResolver getContentResolver() {
234 return mContentResolver;
235 }
236
237 @Override
238 public Looper getMainLooper() {
239 return mMainThread.getLooper();
240 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 @Override
243 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700244 return (mPackageInfo != null) ?
245 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700249 public void setTheme(int resId) {
250 if (mThemeResource != resId) {
251 mThemeResource = resId;
252 initializeTheme();
253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800257 public int getThemeResId() {
258 return mThemeResource;
259 }
260
261 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 public Resources.Theme getTheme() {
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700263 if (mTheme != null) {
264 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700266
267 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
268 getOuterContext().getApplicationInfo().targetSdkVersion);
269 initializeTheme();
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 return mTheme;
272 }
273
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700274 private void initializeTheme() {
275 if (mTheme == null) {
276 mTheme = mResources.newTheme();
277 }
278 mTheme.applyStyle(mThemeResource, true);
279 }
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 @Override
282 public ClassLoader getClassLoader() {
283 return mPackageInfo != null ?
284 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
285 }
286
287 @Override
288 public String getPackageName() {
289 if (mPackageInfo != null) {
290 return mPackageInfo.getPackageName();
291 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800292 // No mPackageInfo means this is a Context for the system itself,
293 // and this here is its name.
294 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800297 /** @hide */
298 @Override
299 public String getBasePackageName() {
300 return mBasePackageName != null ? mBasePackageName : getPackageName();
301 }
302
Dianne Hackborn95d78532013-09-11 09:51:14 -0700303 /** @hide */
304 @Override
305 public String getOpPackageName() {
306 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
307 }
308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700310 public ApplicationInfo getApplicationInfo() {
311 if (mPackageInfo != null) {
312 return mPackageInfo.getApplicationInfo();
313 }
314 throw new RuntimeException("Not supported in system context");
315 }
316
317 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 public String getPackageResourcePath() {
319 if (mPackageInfo != null) {
320 return mPackageInfo.getResDir();
321 }
322 throw new RuntimeException("Not supported in system context");
323 }
324
325 @Override
326 public String getPackageCodePath() {
327 if (mPackageInfo != null) {
328 return mPackageInfo.getAppDir();
329 }
330 throw new RuntimeException("Not supported in system context");
331 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200332
Jeff Brown6e539312015-02-24 18:53:21 -0800333 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700335 // At least one application in the world actually passes in a null
336 // name. This happened to work because when we generated the file name
337 // we would stringify it to "null.xml". Nice.
338 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
339 Build.VERSION_CODES.KITKAT) {
340 if (name == null) {
341 name = "null";
342 }
343 }
344
Jeff Sharkeybe782582016-02-15 18:35:57 -0700345 File file;
346 synchronized (ContextImpl.class) {
347 if (mSharedPrefsPaths == null) {
348 mSharedPrefsPaths = new ArrayMap<>();
349 }
350 file = mSharedPrefsPaths.get(name);
351 if (file == null) {
352 file = getSharedPreferencesPath(name);
353 mSharedPrefsPaths.put(name, file);
354 }
355 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700356 return getSharedPreferences(file, mode);
357 }
358
359 @Override
360 public SharedPreferences getSharedPreferences(File file, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700361 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700363 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700364 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
365 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700366 if (sp == null) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700367 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700368 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700369 return sp;
370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800372 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
373 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
374 // If somebody else (some other process) changed the prefs
375 // file behind our back, we reload it. This has been the
376 // historical (if undocumented) behavior.
377 sp.startReloadIfChangedUnexpectedly();
378 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700379 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 }
381
Jeff Sharkey35871f22016-01-29 17:13:29 -0700382 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700383 if (sSharedPrefsCache == null) {
384 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700385 }
386
387 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700388 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700389 if (packagePrefs == null) {
390 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700391 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700392 }
393
394 return packagePrefs;
395 }
396
397 /**
398 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700399 * requested prefix.
400 *
401 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700402 */
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700403 private static int migrateFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700404 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
405 @Override
406 public boolean accept(File dir, String name) {
407 return name.startsWith(prefix);
408 }
409 });
410
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700411 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700412 for (File sourceFile : sourceFiles) {
413 final File targetFile = new File(targetDir, sourceFile.getName());
414 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
415 try {
416 FileUtils.copyFileOrThrow(sourceFile, targetFile);
417 FileUtils.copyPermissions(sourceFile, targetFile);
418 if (!sourceFile.delete()) {
419 throw new IOException("Failed to clean up " + sourceFile);
420 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700421 if (res != -1) {
422 res++;
423 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700424 } catch (IOException e) {
425 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700426 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700427 }
428 }
429 return res;
430 }
431
432 @Override
433 public boolean migrateSharedPreferencesFrom(Context sourceContext, String name) {
434 synchronized (ContextImpl.class) {
435 final File source = sourceContext.getSharedPreferencesPath(name);
436 final File target = getSharedPreferencesPath(name);
437
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700438 final int res = migrateFiles(source.getParentFile(), target.getParentFile(),
439 source.getName());
440 if (res > 0) {
441 // We moved at least one file, so evict any in-memory caches for
442 // either location
443 final ArrayMap<File, SharedPreferencesImpl> cache =
444 getSharedPreferencesCacheLocked();
445 cache.remove(source);
446 cache.remove(target);
447 }
448 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700449 }
450 }
451
452 @Override
453 public boolean deleteSharedPreferences(String name) {
454 synchronized (ContextImpl.class) {
455 final File prefs = getSharedPreferencesPath(name);
456 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
457
458 // Evict any in-memory caches
459 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
460 cache.remove(prefs);
461
462 prefs.delete();
463 prefsBackup.delete();
464
465 // We failed if files are still lingering
466 return !(prefs.exists() || prefsBackup.exists());
467 }
468 }
469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 private File getPreferencesDir() {
471 synchronized (mSync) {
472 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700473 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700475 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477 }
478
479 @Override
480 public FileInputStream openFileInput(String name)
481 throws FileNotFoundException {
482 File f = makeFilename(getFilesDir(), name);
483 return new FileInputStream(f);
484 }
485
486 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700487 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
488 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 final boolean append = (mode&MODE_APPEND) != 0;
490 File f = makeFilename(getFilesDir(), name);
491 try {
492 FileOutputStream fos = new FileOutputStream(f, append);
493 setFilePermissionsFromMode(f.getPath(), mode, 0);
494 return fos;
495 } catch (FileNotFoundException e) {
496 }
497
498 File parent = f.getParentFile();
499 parent.mkdir();
500 FileUtils.setPermissions(
501 parent.getPath(),
502 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
503 -1, -1);
504 FileOutputStream fos = new FileOutputStream(f, append);
505 setFilePermissionsFromMode(f.getPath(), mode, 0);
506 return fos;
507 }
508
509 @Override
510 public boolean deleteFile(String name) {
511 File f = makeFilename(getFilesDir(), name);
512 return f.delete();
513 }
514
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700515 /**
516 * Common-path handling of app data dir creation
517 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700518 private static File ensurePrivateDirExists(File file) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700519 if (!file.exists()) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700520 try {
521 Os.mkdir(file.getAbsolutePath(), 0771);
Jeff Sharkey46ed6f42016-02-15 14:16:08 -0700522 Os.chmod(file.getAbsolutePath(), 0771);
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700523 } catch (ErrnoException e) {
524 if (e.errno == OsConstants.EEXIST) {
525 // We must have raced with someone; that's okay
526 } else {
527 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700528 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700529 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700530 }
531 return file;
532 }
533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 @Override
535 public File getFilesDir() {
536 synchronized (mSync) {
537 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700538 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700540 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700541 }
542 }
543
544 @Override
545 public File getNoBackupFilesDir() {
546 synchronized (mSync) {
547 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700548 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700550 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800555 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700556 // Operates on primary external storage
557 return getExternalFilesDirs(type)[0];
558 }
559
560 @Override
561 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800562 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700563 if (mExternalFilesDirs == null) {
564 mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800565 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700566
567 // Splice in requested type, if any
568 File[] dirs = mExternalFilesDirs;
569 if (type != null) {
570 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800571 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700572
573 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700574 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800575 }
576 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200577
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800578 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800579 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700580 // Operates on primary external storage
581 return getObbDirs()[0];
582 }
583
584 @Override
585 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800586 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700587 if (mExternalObbDirs == null) {
588 mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800589 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700590
591 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700592 return ensureExternalDirsExistOrFilter(mExternalObbDirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800593 }
594 }
595
596 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 public File getCacheDir() {
598 synchronized (mSync) {
599 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700600 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700602 return ensurePrivateDirExists(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200605
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800606 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700607 public File getCodeCacheDir() {
608 synchronized (mSync) {
609 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700610 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700611 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700612 return ensurePrivateDirExists(mCodeCacheDir);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700613 }
614 }
615
616 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800617 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700618 // Operates on primary external storage
619 return getExternalCacheDirs()[0];
620 }
621
622 @Override
623 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800624 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700625 if (mExternalCacheDirs == null) {
626 mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800627 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700628
629 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700630 return ensureExternalDirsExistOrFilter(mExternalCacheDirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800631 }
632 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700635 public File[] getExternalMediaDirs() {
636 synchronized (mSync) {
637 if (mExternalMediaDirs == null) {
638 mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
639 }
640
641 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700642 return ensureExternalDirsExistOrFilter(mExternalMediaDirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700643 }
644 }
645
646 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 public File getFileStreamPath(String name) {
648 return makeFilename(getFilesDir(), name);
649 }
650
651 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700652 public File getSharedPreferencesPath(String name) {
653 return makeFilename(getPreferencesDir(), name + ".xml");
654 }
655
656 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700658 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660
661 @Override
662 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700663 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665
666 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700667 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
668 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700669 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700670 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700671 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
672 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
673 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
674 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800675 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
676 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
677 }
Jeff Brown47847f32012-03-22 19:13:11 -0700678 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700679 setFilePermissionsFromMode(f.getPath(), mode, 0);
680 return db;
681 }
682
683 @Override
Jeff Sharkey35871f22016-01-29 17:13:29 -0700684 public boolean migrateDatabaseFrom(Context sourceContext, String name) {
685 synchronized (ContextImpl.class) {
686 final File source = sourceContext.getDatabasePath(name);
687 final File target = getDatabasePath(name);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700688 return migrateFiles(source.getParentFile(), target.getParentFile(),
689 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700690 }
691 }
692
693 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 public boolean deleteDatabase(String name) {
695 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700696 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800697 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 } catch (Exception e) {
699 }
700 return false;
701 }
702
703 @Override
704 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700705 File dir;
706 File f;
707
708 if (name.charAt(0) == File.separatorChar) {
709 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
710 dir = new File(dirPath);
711 name = name.substring(name.lastIndexOf(File.separatorChar));
712 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700713
714 if (!dir.isDirectory() && dir.mkdir()) {
715 FileUtils.setPermissions(dir.getPath(),
716 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
717 -1, -1);
718 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700719 } else {
720 dir = getDatabasesDir();
721 f = makeFilename(dir, name);
722 }
723
Jeff Sharkey35871f22016-01-29 17:13:29 -0700724 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726
727 @Override
728 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700729 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 private File getDatabasesDir() {
733 synchronized (mSync) {
734 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700735 if ("android".equals(getPackageName())) {
736 mDatabasesDir = new File("/data/system");
737 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700738 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700741 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
743 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800746 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700748 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750
751 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800752 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700753 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700754 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756
757 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800758 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700760 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762
763 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800764 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700766 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
768
769 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800770 @Deprecated
771 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700772 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
774
775 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800776 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700778 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780
781 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800782 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700784 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786
Jeff Brown6e539312015-02-24 18:53:21 -0800787 private WallpaperManager getWallpaperManager() {
788 return getSystemService(WallpaperManager.class);
789 }
790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 @Override
792 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700793 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700794 startActivity(intent, null);
795 }
796
Amith Yamasani82644082012-08-03 13:09:11 -0700797 /** @hide */
798 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700799 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700800 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700801 }
802
Dianne Hackborna4972e92012-03-14 10:38:05 -0700803 @Override
804 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700805 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
807 throw new AndroidRuntimeException(
808 "Calling startActivity() from outside of an Activity "
809 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
810 + " Is this really what you want?");
811 }
812 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700813 getOuterContext(), mMainThread.getApplicationThread(), null,
814 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
816
Amith Yamasani258848d2012-08-10 17:06:33 -0700817 /** @hide */
818 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700819 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700820 try {
821 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800822 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700823 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700824 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700825 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700826 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700827 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700828 }
829 }
830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800832 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700833 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700834 startActivities(intents, null);
835 }
836
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700837 /** @hide */
838 @Override
839 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
840 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
841 throw new AndroidRuntimeException(
842 "Calling startActivities() from outside of an Activity "
843 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
844 + " Is this really what you want?");
845 }
846 mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700847 getOuterContext(), mMainThread.getApplicationThread(), null,
848 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700849 }
850
Dianne Hackborna4972e92012-03-14 10:38:05 -0700851 @Override
852 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700853 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800854 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
855 throw new AndroidRuntimeException(
856 "Calling startActivities() from outside of an Activity "
857 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
858 + " Is this really what you want?");
859 }
860 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700861 getOuterContext(), mMainThread.getApplicationThread(), null,
862 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800863 }
864
865 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700866 public void startIntentSender(IntentSender intent,
867 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
868 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700869 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
870 }
871
872 @Override
873 public void startIntentSender(IntentSender intent, Intent fillInIntent,
874 int flagsMask, int flagsValues, int extraFlags, Bundle options)
875 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700876 try {
877 String resolvedType = null;
878 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700879 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -0700880 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700881 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
882 }
883 int result = ActivityManagerNative.getDefault()
884 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
885 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700886 0, flagsMask, flagsValues, options);
887 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700888 throw new IntentSender.SendIntentException();
889 }
890 Instrumentation.checkStartActivityResult(result, null);
891 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700892 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700893 }
894 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200895
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700896 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700898 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
900 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700901 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700903 mMainThread.getApplicationThread(), intent, resolvedType, null,
904 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
905 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700907 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
909 }
910
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800911 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700913 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700915 String[] receiverPermissions = receiverPermission == null ? null
916 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700918 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700920 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700921 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
922 null, false, false, getUserId());
923 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700924 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700925 }
926 }
927
928 @Override
929 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
930 warnIfCallingFromSystemProcess();
931 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
932 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700933 intent.prepareToLeaveProcess(this);
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700934 ActivityManagerNative.getDefault().broadcastIntent(
935 mMainThread.getApplicationThread(), intent, resolvedType, null,
936 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700937 null, false, false, getUserId());
938 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700939 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -0700940 }
941 }
942
943 @Override
944 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
945 warnIfCallingFromSystemProcess();
946 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700947 String[] receiverPermissions = receiverPermission == null ? null
948 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -0700949 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700950 intent.prepareToLeaveProcess(this);
Dianne Hackborna750a632015-06-16 17:18:23 -0700951 ActivityManagerNative.getDefault().broadcastIntent(
952 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700953 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700954 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800955 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700956 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800957 }
958 }
959
960 @Override
961 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
962 warnIfCallingFromSystemProcess();
963 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700964 String[] receiverPermissions = receiverPermission == null ? null
965 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800966 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700967 intent.prepareToLeaveProcess(this);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800968 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700969 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700970 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -0700971 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700973 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
975 }
976
977 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -0700978 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700979 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700981 String[] receiverPermissions = receiverPermission == null ? null
982 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700984 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700986 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700987 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700988 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700990 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 }
992 }
993
994 @Override
995 public void sendOrderedBroadcast(Intent intent,
996 String receiverPermission, BroadcastReceiver resultReceiver,
997 Handler scheduler, int initialCode, String initialData,
998 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800999 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001000 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1001 }
1002
1003 @Override
1004 public void sendOrderedBroadcast(Intent intent,
1005 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
1006 Handler scheduler, int initialCode, String initialData,
1007 Bundle initialExtras) {
1008 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1009 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001010 }
1011
1012 @Override
1013 public void sendOrderedBroadcast(Intent intent,
1014 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1015 Handler scheduler, int initialCode, String initialData,
1016 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001017 sendOrderedBroadcast(intent, receiverPermission, appOp,
1018 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1019 }
1020
1021 void sendOrderedBroadcast(Intent intent,
1022 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1023 Handler scheduler, int initialCode, String initialData,
1024 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001025 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 IIntentReceiver rd = null;
1027 if (resultReceiver != null) {
1028 if (mPackageInfo != null) {
1029 if (scheduler == null) {
1030 scheduler = mMainThread.getHandler();
1031 }
1032 rd = mPackageInfo.getReceiverDispatcher(
1033 resultReceiver, getOuterContext(), scheduler,
1034 mMainThread.getInstrumentation(), false);
1035 } else {
1036 if (scheduler == null) {
1037 scheduler = mMainThread.getHandler();
1038 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001039 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1041 }
1042 }
1043 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001044 String[] receiverPermissions = receiverPermission == null ? null
1045 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001047 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 ActivityManagerNative.getDefault().broadcastIntent(
1049 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001050 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001051 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001053 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055 }
1056
1057 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001058 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001059 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1060 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001061 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001062 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001063 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001064 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001065 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001066 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001067 }
1068 }
1069
1070 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001071 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1072 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001073 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1074 }
1075
1076 @Override
1077 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1078 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001079 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001080 String[] receiverPermissions = receiverPermission == null ? null
1081 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001082 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001083 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001084 ActivityManagerNative.getDefault().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001085 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001086 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001087 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001088 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001089 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001090 }
1091 }
1092
1093 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001094 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001095 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001096 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001097 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001098 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001099 }
1100
1101 @Override
1102 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1103 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001104 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1105 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1106 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1107 }
1108
1109 @Override
1110 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1111 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1112 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001113 IIntentReceiver rd = null;
1114 if (resultReceiver != null) {
1115 if (mPackageInfo != null) {
1116 if (scheduler == null) {
1117 scheduler = mMainThread.getHandler();
1118 }
1119 rd = mPackageInfo.getReceiverDispatcher(
1120 resultReceiver, getOuterContext(), scheduler,
1121 mMainThread.getInstrumentation(), false);
1122 } else {
1123 if (scheduler == null) {
1124 scheduler = mMainThread.getHandler();
1125 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001126 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1127 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001128 }
1129 }
1130 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001131 String[] receiverPermissions = receiverPermission == null ? null
1132 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001133 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001134 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001135 ActivityManagerNative.getDefault().broadcastIntent(
1136 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001137 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001138 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001139 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001140 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001141 }
1142 }
1143
1144 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001145 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001147 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1149 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001150 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 ActivityManagerNative.getDefault().broadcastIntent(
1152 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001153 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001154 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001156 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158 }
1159
1160 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001161 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001162 public void sendStickyOrderedBroadcast(Intent intent,
1163 BroadcastReceiver resultReceiver,
1164 Handler scheduler, int initialCode, String initialData,
1165 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001166 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001167 IIntentReceiver rd = null;
1168 if (resultReceiver != null) {
1169 if (mPackageInfo != null) {
1170 if (scheduler == null) {
1171 scheduler = mMainThread.getHandler();
1172 }
1173 rd = mPackageInfo.getReceiverDispatcher(
1174 resultReceiver, getOuterContext(), scheduler,
1175 mMainThread.getInstrumentation(), false);
1176 } else {
1177 if (scheduler == null) {
1178 scheduler = mMainThread.getHandler();
1179 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001180 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001181 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1182 }
1183 }
1184 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1185 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001186 intent.prepareToLeaveProcess(this);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001187 ActivityManagerNative.getDefault().broadcastIntent(
1188 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1189 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001190 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001191 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001192 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001193 }
1194 }
1195
1196 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001197 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 public void removeStickyBroadcast(Intent intent) {
1199 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1200 if (resolvedType != null) {
1201 intent = new Intent(intent);
1202 intent.setDataAndType(intent.getData(), resolvedType);
1203 }
1204 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001205 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001207 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001209 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 }
1212
1213 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001214 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001215 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1216 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1217 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001218 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001219 ActivityManagerNative.getDefault().broadcastIntent(
1220 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001221 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1222 user.getIdentifier());
1223 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001224 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001225 }
1226 }
1227
1228 @Override
1229 @Deprecated
1230 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1231 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1232 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001233 intent.prepareToLeaveProcess(this);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001234 ActivityManagerNative.getDefault().broadcastIntent(
1235 mMainThread.getApplicationThread(), intent, resolvedType, null,
1236 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1237 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001238 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001239 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001240 }
1241 }
1242
1243 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001244 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001245 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1246 UserHandle user, BroadcastReceiver resultReceiver,
1247 Handler scheduler, int initialCode, String initialData,
1248 Bundle initialExtras) {
1249 IIntentReceiver rd = null;
1250 if (resultReceiver != null) {
1251 if (mPackageInfo != null) {
1252 if (scheduler == null) {
1253 scheduler = mMainThread.getHandler();
1254 }
1255 rd = mPackageInfo.getReceiverDispatcher(
1256 resultReceiver, getOuterContext(), scheduler,
1257 mMainThread.getInstrumentation(), false);
1258 } else {
1259 if (scheduler == null) {
1260 scheduler = mMainThread.getHandler();
1261 }
1262 rd = new LoadedApk.ReceiverDispatcher(
1263 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1264 }
1265 }
1266 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1267 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001268 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001269 ActivityManagerNative.getDefault().broadcastIntent(
1270 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1271 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001272 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001273 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001274 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001275 }
1276 }
1277
1278 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001279 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001280 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1281 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1282 if (resolvedType != null) {
1283 intent = new Intent(intent);
1284 intent.setDataAndType(intent.getData(), resolvedType);
1285 }
1286 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001287 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001288 ActivityManagerNative.getDefault().unbroadcastIntent(
1289 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1290 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001291 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001292 }
1293 }
1294
1295 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1297 return registerReceiver(receiver, filter, null, null);
1298 }
1299
1300 @Override
1301 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1302 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001303 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001304 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306
Dianne Hackborn20e80982012-08-31 19:00:44 -07001307 @Override
1308 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1309 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1310 return registerReceiverInternal(receiver, user.getIdentifier(),
1311 filter, broadcastPermission, scheduler, getOuterContext());
1312 }
1313
1314 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 IntentFilter filter, String broadcastPermission,
1316 Handler scheduler, Context context) {
1317 IIntentReceiver rd = null;
1318 if (receiver != null) {
1319 if (mPackageInfo != null && context != null) {
1320 if (scheduler == null) {
1321 scheduler = mMainThread.getHandler();
1322 }
1323 rd = mPackageInfo.getReceiverDispatcher(
1324 receiver, context, scheduler,
1325 mMainThread.getInstrumentation(), true);
1326 } else {
1327 if (scheduler == null) {
1328 scheduler = mMainThread.getHandler();
1329 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001330 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001331 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333 }
1334 try {
1335 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001336 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001337 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001339 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
1342
1343 @Override
1344 public void unregisterReceiver(BroadcastReceiver receiver) {
1345 if (mPackageInfo != null) {
1346 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1347 getOuterContext(), receiver);
1348 try {
1349 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1350 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001351 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 } else {
1354 throw new RuntimeException("Not supported in system context");
1355 }
1356 }
1357
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001358 private void validateServiceIntent(Intent service) {
1359 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001360 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001361 IllegalArgumentException ex = new IllegalArgumentException(
1362 "Service Intent must be explicit: " + service);
1363 throw ex;
1364 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001365 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1366 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001367 }
1368 }
1369 }
1370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 @Override
1372 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001373 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001374 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001375 }
1376
1377 @Override
1378 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001379 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001380 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001381 }
1382
1383 @Override
1384 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001385 return startServiceCommon(service, user);
1386 }
1387
1388 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001390 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001391 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 ComponentName cn = ActivityManagerNative.getDefault().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001393 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
1394 getContentResolver()), getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001395 if (cn != null) {
1396 if (cn.getPackageName().equals("!")) {
1397 throw new SecurityException(
1398 "Not allowed to start service " + service
1399 + " without permission " + cn.getClassName());
1400 } else if (cn.getPackageName().equals("!!")) {
1401 throw new SecurityException(
1402 "Unable to start service " + service
1403 + ": " + cn.getClassName());
1404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
1406 return cn;
1407 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001408 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410 }
1411
1412 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001413 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001414 return stopServiceCommon(service, user);
1415 }
1416
1417 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001419 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001420 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 int res = ActivityManagerNative.getDefault().stopService(
1422 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001423 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 if (res < 0) {
1425 throw new SecurityException(
1426 "Not allowed to stop service " + service);
1427 }
1428 return res != 0;
1429 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001430 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432 }
1433
1434 @Override
1435 public boolean bindService(Intent service, ServiceConnection conn,
1436 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001437 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001438 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1439 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001440 }
1441
1442 /** @hide */
1443 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001444 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1445 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001446 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001447 }
1448
Adrian Roos691546e2016-02-09 10:13:41 -08001449 /** @hide */
1450 @Override
1451 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1452 Handler handler, UserHandle user) {
1453 if (handler == null) {
1454 throw new IllegalArgumentException("handler must not be null.");
1455 }
1456 return bindServiceCommon(service, conn, flags, handler, user);
1457 }
1458
1459 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1460 handler, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001462 if (conn == null) {
1463 throw new IllegalArgumentException("connection is null");
1464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 if (mPackageInfo != null) {
Adrian Roos691546e2016-02-09 10:13:41 -08001466 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 } else {
1468 throw new RuntimeException("Not supported in system context");
1469 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001470 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001472 IBinder token = getActivityToken();
1473 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1474 && mPackageInfo.getApplicationInfo().targetSdkVersion
1475 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1476 flags |= BIND_WAIVE_PRIORITY;
1477 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001478 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 int res = ActivityManagerNative.getDefault().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001480 mMainThread.getApplicationThread(), getActivityToken(), service,
1481 service.resolveTypeIfNeeded(getContentResolver()),
1482 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 if (res < 0) {
1484 throw new SecurityException(
1485 "Not allowed to bind to service " + service);
1486 }
1487 return res != 0;
1488 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001489 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 }
1492
1493 @Override
1494 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001495 if (conn == null) {
1496 throw new IllegalArgumentException("connection is null");
1497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 if (mPackageInfo != null) {
1499 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1500 getOuterContext(), conn);
1501 try {
1502 ActivityManagerNative.getDefault().unbindService(sd);
1503 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001504 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506 } else {
1507 throw new RuntimeException("Not supported in system context");
1508 }
1509 }
1510
1511 @Override
1512 public boolean startInstrumentation(ComponentName className,
1513 String profileFile, Bundle arguments) {
1514 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001515 if (arguments != null) {
1516 arguments.setAllowFds(false);
1517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001519 className, profileFile, 0, arguments, null, null, getUserId(),
1520 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001522 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525
1526 @Override
1527 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001528 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
1530
Jeff Brown6e539312015-02-24 18:53:21 -08001531 @Override
1532 public String getSystemServiceName(Class<?> serviceClass) {
1533 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001534 }
1535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 @Override
1537 public int checkPermission(String permission, int pid, int uid) {
1538 if (permission == null) {
1539 throw new IllegalArgumentException("permission is null");
1540 }
1541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 try {
1543 return ActivityManagerNative.getDefault().checkPermission(
1544 permission, pid, uid);
1545 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001546 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548 }
1549
Dianne Hackbornff170242014-11-19 10:59:01 -08001550 /** @hide */
1551 @Override
1552 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1553 if (permission == null) {
1554 throw new IllegalArgumentException("permission is null");
1555 }
1556
1557 try {
1558 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1559 permission, pid, uid, callerToken);
1560 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001561 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001562 }
1563 }
1564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 @Override
1566 public int checkCallingPermission(String permission) {
1567 if (permission == null) {
1568 throw new IllegalArgumentException("permission is null");
1569 }
1570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 int pid = Binder.getCallingPid();
1572 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001573 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575 return PackageManager.PERMISSION_DENIED;
1576 }
1577
1578 @Override
1579 public int checkCallingOrSelfPermission(String permission) {
1580 if (permission == null) {
1581 throw new IllegalArgumentException("permission is null");
1582 }
1583
1584 return checkPermission(permission, Binder.getCallingPid(),
1585 Binder.getCallingUid());
1586 }
1587
Svetoslavc6d1c342015-02-26 14:44:43 -08001588 @Override
1589 public int checkSelfPermission(String permission) {
1590 if (permission == null) {
1591 throw new IllegalArgumentException("permission is null");
1592 }
1593
1594 return checkPermission(permission, Process.myPid(), Process.myUid());
1595 }
1596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 private void enforce(
1598 String permission, int resultOfCheck,
1599 boolean selfToo, int uid, String message) {
1600 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1601 throw new SecurityException(
1602 (message != null ? (message + ": ") : "") +
1603 (selfToo
1604 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001605 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 permission +
1607 ".");
1608 }
1609 }
1610
Jeff Brown6e539312015-02-24 18:53:21 -08001611 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 public void enforcePermission(
1613 String permission, int pid, int uid, String message) {
1614 enforce(permission,
1615 checkPermission(permission, pid, uid),
1616 false,
1617 uid,
1618 message);
1619 }
1620
Jeff Brown6e539312015-02-24 18:53:21 -08001621 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 public void enforceCallingPermission(String permission, String message) {
1623 enforce(permission,
1624 checkCallingPermission(permission),
1625 false,
1626 Binder.getCallingUid(),
1627 message);
1628 }
1629
Jeff Brown6e539312015-02-24 18:53:21 -08001630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 public void enforceCallingOrSelfPermission(
1632 String permission, String message) {
1633 enforce(permission,
1634 checkCallingOrSelfPermission(permission),
1635 true,
1636 Binder.getCallingUid(),
1637 message);
1638 }
1639
1640 @Override
1641 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1642 try {
1643 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001644 mMainThread.getApplicationThread(), toPackage,
1645 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001647 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 }
1649 }
1650
1651 @Override
1652 public void revokeUriPermission(Uri uri, int modeFlags) {
1653 try {
1654 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001655 mMainThread.getApplicationThread(),
1656 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001658 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660 }
1661
1662 @Override
1663 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 try {
1665 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001666 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001667 resolveUserId(uri), null);
1668 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001669 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001670 }
1671 }
1672
1673 /** @hide */
1674 @Override
1675 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1676 try {
1677 return ActivityManagerNative.getDefault().checkUriPermission(
1678 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1679 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001681 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 }
1684
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001685 private int resolveUserId(Uri uri) {
1686 return ContentProvider.getUserIdFromUri(uri, getUserId());
1687 }
1688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 @Override
1690 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 int pid = Binder.getCallingPid();
1692 if (pid != Process.myPid()) {
1693 return checkUriPermission(uri, pid,
1694 Binder.getCallingUid(), modeFlags);
1695 }
1696 return PackageManager.PERMISSION_DENIED;
1697 }
1698
1699 @Override
1700 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1701 return checkUriPermission(uri, Binder.getCallingPid(),
1702 Binder.getCallingUid(), modeFlags);
1703 }
1704
1705 @Override
1706 public int checkUriPermission(Uri uri, String readPermission,
1707 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001708 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1710 + readPermission + " writePermission=" + writePermission
1711 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1712 }
1713 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1714 if (readPermission == null
1715 || checkPermission(readPermission, pid, uid)
1716 == PackageManager.PERMISSION_GRANTED) {
1717 return PackageManager.PERMISSION_GRANTED;
1718 }
1719 }
1720 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1721 if (writePermission == null
1722 || checkPermission(writePermission, pid, uid)
1723 == PackageManager.PERMISSION_GRANTED) {
1724 return PackageManager.PERMISSION_GRANTED;
1725 }
1726 }
1727 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1728 : PackageManager.PERMISSION_DENIED;
1729 }
1730
1731 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001732 StringBuilder builder = new StringBuilder();
1733 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1734 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001736 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1737 builder.append("write and ");
1738 }
1739 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1740 builder.append("persistable and ");
1741 }
1742 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1743 builder.append("prefix and ");
1744 }
1745
1746 if (builder.length() > 5) {
1747 builder.setLength(builder.length() - 5);
1748 return builder.toString();
1749 } else {
1750 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
1753
1754 private void enforceForUri(
1755 int modeFlags, int resultOfCheck, boolean selfToo,
1756 int uid, Uri uri, String message) {
1757 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1758 throw new SecurityException(
1759 (message != null ? (message + ": ") : "") +
1760 (selfToo
1761 ? "Neither user " + uid + " nor current process has "
1762 : "User " + uid + " does not have ") +
1763 uriModeFlagToString(modeFlags) +
1764 " permission on " +
1765 uri +
1766 ".");
1767 }
1768 }
1769
Jeff Brown6e539312015-02-24 18:53:21 -08001770 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 public void enforceUriPermission(
1772 Uri uri, int pid, int uid, int modeFlags, String message) {
1773 enforceForUri(
1774 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1775 false, uid, uri, message);
1776 }
1777
Jeff Brown6e539312015-02-24 18:53:21 -08001778 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 public void enforceCallingUriPermission(
1780 Uri uri, int modeFlags, String message) {
1781 enforceForUri(
1782 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001783 false,
1784 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
1786
Jeff Brown6e539312015-02-24 18:53:21 -08001787 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 public void enforceCallingOrSelfUriPermission(
1789 Uri uri, int modeFlags, String message) {
1790 enforceForUri(
1791 modeFlags,
1792 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1793 Binder.getCallingUid(), uri, message);
1794 }
1795
Jeff Brown6e539312015-02-24 18:53:21 -08001796 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 public void enforceUriPermission(
1798 Uri uri, String readPermission, String writePermission,
1799 int pid, int uid, int modeFlags, String message) {
1800 enforceForUri(modeFlags,
1801 checkUriPermission(
1802 uri, readPermission, writePermission, pid, uid,
1803 modeFlags),
1804 false,
1805 uid,
1806 uri,
1807 message);
1808 }
1809
Tom O'Neill365632e2013-09-09 09:34:58 -07001810 /**
1811 * Logs a warning if the system process directly called a method such as
1812 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1813 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1814 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001815 private void warnIfCallingFromSystemProcess() {
1816 if (Process.myUid() == Process.SYSTEM_UID) {
1817 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001818 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001819 }
1820 }
1821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001823 public Context createApplicationContext(ApplicationInfo application, int flags)
1824 throws NameNotFoundException {
1825 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1826 flags | CONTEXT_REGISTER_PACKAGE);
1827 if (pi != null) {
Svetoslav976e8bd2014-07-16 15:12:03 -07001828 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001829 new UserHandle(UserHandle.getUserId(application.uid)), flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001830 mDisplay, null, Display.INVALID_DISPLAY);
Svetoslav976e8bd2014-07-16 15:12:03 -07001831 if (c.mResources != null) {
1832 return c;
1833 }
1834 }
1835
1836 throw new PackageManager.NameNotFoundException(
1837 "Application package " + application.packageName + " not found");
1838 }
1839
1840 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001842 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001843 return createPackageContextAsUser(packageName, flags,
1844 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001845 }
1846
1847 @Override
1848 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1849 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00001850 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001851 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001852 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854
Jeff Browndefd4a62014-03-10 21:24:37 -07001855 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001856 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 if (pi != null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001858 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001859 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 if (c.mResources != null) {
1861 return c;
1862 }
1863 }
1864
1865 // Should be a better exception.
1866 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001867 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 }
1869
Romain Guy870e09f2009-07-06 16:35:25 -07001870 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001871 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001872 if (overrideConfiguration == null) {
1873 throw new IllegalArgumentException("overrideConfiguration must not be null");
1874 }
1875
Jeff Browndefd4a62014-03-10 21:24:37 -07001876 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001877 mUser, mFlags, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001878 }
1879
1880 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001881 public Context createDisplayContext(Display display) {
1882 if (display == null) {
1883 throw new IllegalArgumentException("display must not be null");
1884 }
1885
Jeff Browndefd4a62014-03-10 21:24:37 -07001886 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001887 mUser, mFlags, display, null, Display.INVALID_DISPLAY);
Jeff Browna492c3a2012-08-23 19:48:44 -07001888 }
1889
Jeff Brown6e539312015-02-24 18:53:21 -08001890 Display getDisplay() {
1891 if (mDisplay != null) {
1892 return mDisplay;
1893 }
Wale Ogunwale26698512015-06-05 16:55:33 -07001894 return ResourcesManager.getInstance().getAdjustedDisplay(
1895 Display.DEFAULT_DISPLAY, mDisplayAdjustments);
Jeff Brown6e539312015-02-24 18:53:21 -08001896 }
1897
Jeff Browna492c3a2012-08-23 19:48:44 -07001898 private int getDisplayId() {
1899 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1900 }
1901
1902 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001903 public Context createDeviceEncryptedStorageContext() {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001904 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE)
1905 | Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001906 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1907 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1908 }
1909
1910 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001911 public Context createCredentialEncryptedStorageContext() {
1912 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE)
1913 | Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001914 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1915 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1916 }
1917
1918 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001919 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001920 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
1921 }
1922
1923 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001924 public boolean isDeviceEncryptedStorage() {
1925 return (mFlags & Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001926 }
1927
1928 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001929 public boolean isCredentialEncryptedStorage() {
1930 return (mFlags & Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07001931 }
1932
Jeff Brown98365d72012-08-19 20:30:52 -07001933 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001934 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1935 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -07001936 }
1937
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001938 @Override
1939 public File getDataDir() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001941 File res = null;
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001942 if (isCredentialEncryptedStorage()) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001943 res = mPackageInfo.getCredentialEncryptedDataDirFile();
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001944 } else if (isDeviceEncryptedStorage()) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001945 res = mPackageInfo.getDeviceEncryptedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001946 } else {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001947 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001948 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001949
1950 if (res != null) {
1951 return res;
1952 } else {
1953 throw new RuntimeException(
1954 "No data directory found for package " + getPackageName());
1955 }
1956 } else {
1957 throw new RuntimeException(
1958 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 }
1961
1962 @Override
1963 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07001964 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001966 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 if (!file.exists()) {
1968 file.mkdir();
1969 setFilePermissionsFromMode(file.getPath(), mode,
1970 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1971 }
1972 return file;
1973 }
1974
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001975 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08001976 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001977 public int getUserId() {
1978 return mUser.getIdentifier();
1979 }
1980
Dianne Hackborn21556372010-02-04 16:34:40 -08001981 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001982 LoadedApk packageInfo = new LoadedApk(mainThread);
1983 ContextImpl context = new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001984 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001985 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Wale Ogunwale7c726682015-02-06 17:34:28 -08001986 context.mResourcesManager.getDisplayMetricsLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 return context;
1988 }
1989
Jeff Browndefd4a62014-03-10 21:24:37 -07001990 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
1991 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
1992 return new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001993 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001994 }
1995
1996 static ContextImpl createActivityContext(ActivityThread mainThread,
Adam Lesinski082614c2016-03-04 14:33:47 -08001997 LoadedApk packageInfo, IBinder activityToken, int displayId,
1998 Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001999 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski082614c2016-03-04 14:33:47 -08002000 return new ContextImpl(null, mainThread, packageInfo, activityToken, null, 0,
Wale Ogunwale26698512015-06-05 16:55:33 -07002001 null, overrideConfiguration, displayId);
Jeff Browndefd4a62014-03-10 21:24:37 -07002002 }
2003
2004 private ContextImpl(ContextImpl container, ActivityThread mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002005 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07002006 Display display, Configuration overrideConfiguration, int createDisplayWithId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002009 // If creator didn't specify which storage to use, use the default
2010 // location for application.
Jeff Sharkeye13529a2015-12-09 14:15:27 -07002011 if ((flags & (Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE
2012 | Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE)) == 0) {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002013 final File dataDir = packageInfo.getDataDirFile();
2014 if (Objects.equals(dataDir, packageInfo.getCredentialEncryptedDataDirFile())) {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07002015 flags |= Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002016 } else if (Objects.equals(dataDir, packageInfo.getDeviceEncryptedDataDirFile())) {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07002017 flags |= Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002018 }
2019 }
2020
Jeff Browndefd4a62014-03-10 21:24:37 -07002021 mMainThread = mainThread;
2022 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002023 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024
Jeff Browndefd4a62014-03-10 21:24:37 -07002025 if (user == null) {
2026 user = Process.myUserHandle();
2027 }
2028 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07002031 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002032
Wale Ogunwale26698512015-06-05 16:55:33 -07002033 final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
Wale Ogunwalecac3dc62015-06-09 15:35:02 -07002034 ? createDisplayWithId
2035 : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
Wale Ogunwale26698512015-06-05 16:55:33 -07002036
Jeff Browndefd4a62014-03-10 21:24:37 -07002037 CompatibilityInfo compatInfo = null;
2038 if (container != null) {
2039 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
2040 }
Wale Ogunwale9cf99542015-02-18 16:31:34 -08002041 if (compatInfo == null) {
2042 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2043 ? packageInfo.getCompatibilityInfo()
2044 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Jeff Browndefd4a62014-03-10 21:24:37 -07002045 }
2046 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002047 mDisplayAdjustments.setConfiguration(overrideConfiguration);
Jeff Browndefd4a62014-03-10 21:24:37 -07002048
Wale Ogunwale26698512015-06-05 16:55:33 -07002049 mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
2050 : ResourcesManager.getInstance().getAdjustedDisplay(displayId, mDisplayAdjustments);
2051
Jeff Browndefd4a62014-03-10 21:24:37 -07002052 Resources resources = packageInfo.getResources(mainThread);
2053 if (resources != null) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002054 if (displayId != Display.DEFAULT_DISPLAY
Jeff Browndefd4a62014-03-10 21:24:37 -07002055 || overrideConfiguration != null
2056 || (compatInfo != null && compatInfo.applicationScale
2057 != resources.getCompatibilityInfo().applicationScale)) {
Adam Lesinski082614c2016-03-04 14:33:47 -08002058 resources = mResourcesManager.getResources(
2059 activityToken,
2060 packageInfo.getResDir(),
2061 packageInfo.getSplitResDirs(),
2062 packageInfo.getOverlayDirs(),
2063 packageInfo.getApplicationInfo().sharedLibraryFiles,
2064 displayId,
2065 overrideConfiguration,
2066 compatInfo,
2067 packageInfo.getClassLoader());
Jeff Browndefd4a62014-03-10 21:24:37 -07002068 }
2069 }
2070 mResources = resources;
2071
2072 if (container != null) {
2073 mBasePackageName = container.mBasePackageName;
2074 mOpPackageName = container.mOpPackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002075 } else {
2076 mBasePackageName = packageInfo.mPackageName;
2077 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2078 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2079 // Special case: system components allow themselves to be loaded in to other
2080 // processes. For purposes of app ops, we must then consider the context as
2081 // belonging to the package of this process, not the system itself, otherwise
2082 // the package+uid verifications in app ops will fail.
2083 mOpPackageName = ActivityThread.currentPackageName();
2084 } else {
2085 mOpPackageName = mBasePackageName;
2086 }
2087 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002088
2089 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091
Narayan Kamath29564cd2014-08-07 10:57:40 +01002092 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2093 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
2095
2096 final void scheduleFinalCleanup(String who, String what) {
2097 mMainThread.scheduleContextCleanup(this, who, what);
2098 }
2099
2100 final void performFinalCleanup(String who, String what) {
2101 //Log.i(TAG, "Cleanup up context: " + this);
2102 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2103 }
2104
2105 final Context getReceiverRestrictedContext() {
2106 if (mReceiverRestrictedContext != null) {
2107 return mReceiverRestrictedContext;
2108 }
2109 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2110 }
2111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 final void setOuterContext(Context context) {
2113 mOuterContext = context;
2114 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 final Context getOuterContext() {
2117 return mOuterContext;
2118 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 final IBinder getActivityToken() {
2121 return mActivityToken;
2122 }
2123
Jeff Sharkey634dc422016-01-30 17:44:15 -07002124 private void checkMode(int mode) {
2125 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2126 if ((mode & MODE_WORLD_READABLE) != 0) {
2127 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2128 }
2129 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2130 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2131 }
2132 }
2133 }
2134
Jeff Brown6e539312015-02-24 18:53:21 -08002135 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002136 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 int extraPermissions) {
2138 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2139 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2140 |extraPermissions;
2141 if ((mode&MODE_WORLD_READABLE) != 0) {
2142 perms |= FileUtils.S_IROTH;
2143 }
2144 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2145 perms |= FileUtils.S_IWOTH;
2146 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002147 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2149 + ", perms=0x" + Integer.toHexString(perms));
2150 }
2151 FileUtils.setPermissions(name, perms, -1, -1);
2152 }
2153
2154 private File makeFilename(File base, String name) {
2155 if (name.indexOf(File.separatorChar) < 0) {
2156 return new File(base, name);
2157 }
2158 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002159 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
2161
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002162 /**
2163 * Ensure that given directories exist, trying to create them if missing. If
2164 * unable to create, they are filtered by replacing with {@code null}.
2165 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002166 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002167 File[] result = new File[dirs.length];
2168 for (int i = 0; i < dirs.length; i++) {
2169 File dir = dirs[i];
2170 if (!dir.exists()) {
2171 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002172 // recheck existence in case of cross-process race
2173 if (!dir.exists()) {
2174 // Failing to mkdir() may be okay, since we might not have
2175 // enough permissions; ask vold to create on our behalf.
2176 final IMountService mount = IMountService.Stub.asInterface(
2177 ServiceManager.getService("mount"));
Christopher Tatecc866da2013-10-02 18:11:01 -07002178 try {
Jeff Sharkey983294592015-07-13 10:25:31 -07002179 final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
2180 if (res != 0) {
2181 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2182 dir = null;
2183 }
2184 } catch (Exception e) {
2185 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002186 dir = null;
2187 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002188 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002189 }
2190 }
2191 result[i] = dir;
2192 }
2193 return result;
2194 }
2195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 // ----------------------------------------------------------------------
2197 // ----------------------------------------------------------------------
2198 // ----------------------------------------------------------------------
2199
2200 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002201 private final ActivityThread mMainThread;
2202 private final UserHandle mUser;
2203
2204 public ApplicationContentResolver(
2205 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002207 mMainThread = Preconditions.checkNotNull(mainThread);
2208 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 }
2210
2211 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002212 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002213 return mMainThread.acquireProvider(context,
2214 ContentProvider.getAuthorityWithoutUserId(auth),
2215 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
2217
2218 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002219 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002220 return mMainThread.acquireExistingProvider(context,
2221 ContentProvider.getAuthorityWithoutUserId(auth),
2222 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002223 }
2224
2225 @Override
2226 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002227 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002229
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002230 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002231 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002232 return mMainThread.acquireProvider(c,
2233 ContentProvider.getAuthorityWithoutUserId(auth),
2234 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002235 }
2236
2237 @Override
2238 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002239 return mMainThread.releaseProvider(icp, false);
2240 }
2241
2242 @Override
2243 public void unstableProviderDied(IContentProvider icp) {
2244 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002245 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002246
2247 @Override
2248 public void appNotRespondingViaProvider(IContentProvider icp) {
2249 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2250 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002251
2252 /** @hide */
2253 protected int resolveUserIdFromAuthority(String auth) {
2254 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257}