blob: f1d0e10dd6e32e915e44dea86f9af2fa8be4598f [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
Adam Lesinski4ece3d62016-06-16 18:05:41 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Nicolas Prevotd85fc722014-04-16 19:52:08 +010023import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.ContextWrapper;
27import android.content.IContentProvider;
Jeff Brown6e539312015-02-24 18:53:21 -080028import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Intent;
30import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070031import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.ReceiverCallNotAllowedException;
33import android.content.ServiceConnection;
34import android.content.SharedPreferences;
Adam Lesinski4ece3d62016-06-16 18:05:41 -070035import android.content.pm.ActivityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070039import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070041import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070042import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070044import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.database.sqlite.SQLiteDatabase;
46import android.database.sqlite.SQLiteDatabase.CursorFactory;
47import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080051import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070053import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080054import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.FileUtils;
56import android.os.Handler;
57import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070062import android.os.UserHandle;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070063import android.os.storage.IMountService;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -070064import android.system.ErrnoException;
65import android.system.Os;
66import android.system.OsConstants;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070068import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070070import android.util.Slog;
Jeff Brown98365d72012-08-19 20:30:52 -070071import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080072import android.view.DisplayAdjustments;
Dan Egnor95240272009-10-27 18:23:39 -070073
Jeff Sharkey35871f22016-01-29 17:13:29 -070074import com.android.internal.annotations.GuardedBy;
75import com.android.internal.util.Preconditions;
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.io.File;
78import java.io.FileInputStream;
79import java.io.FileNotFoundException;
80import java.io.FileOutputStream;
Jeff Sharkey35871f22016-01-29 17:13:29 -070081import java.io.FilenameFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.io.IOException;
83import java.io.InputStream;
Jeff Sharkey7a30a302015-12-08 14:20:06 -070084import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086class ReceiverRestrictedContext extends ContextWrapper {
87 ReceiverRestrictedContext(Context base) {
88 super(base);
89 }
90
91 @Override
92 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
93 return registerReceiver(receiver, filter, null, null);
94 }
95
96 @Override
97 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
98 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070099 if (receiver == null) {
100 // Allow retrieving current sticky broadcast; this is safe since we
101 // aren't actually registering a receiver.
102 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
103 } else {
104 throw new ReceiverCallNotAllowedException(
105 "BroadcastReceiver components are not allowed to register to receive intents");
106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 }
108
109 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700110 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
111 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700112 if (receiver == null) {
113 // Allow retrieving current sticky broadcast; this is safe since we
114 // aren't actually registering a receiver.
115 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
116 } else {
117 throw new ReceiverCallNotAllowedException(
118 "BroadcastReceiver components are not allowed to register to receive intents");
119 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700120 }
121
122 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
124 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700125 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 }
127}
128
129/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800130 * Common implementation of Context API, which provides the base
131 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800133class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800134 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700135 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700137 /**
138 * Map from package name, to preference name, to cached preferences.
139 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700140 @GuardedBy("ContextImpl.class")
141 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
142
143 /**
144 * Map from preference name to generated path.
145 */
146 @GuardedBy("ContextImpl.class")
147 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Jeff Browndefd4a62014-03-10 21:24:37 -0700149 final ActivityThread mMainThread;
150 final LoadedApk mPackageInfo;
151
152 private final IBinder mActivityToken;
153
154 private final UserHandle mUser;
155
156 private final ApplicationContentResolver mContentResolver;
157
158 private final String mBasePackageName;
159 private final String mOpPackageName;
160
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700161 private final @NonNull ResourcesManager mResourcesManager;
162 private final @NonNull Resources mResources;
163 private @Nullable Display mDisplay; // may be null if default display
Jeff Browndefd4a62014-03-10 21:24:37 -0700164
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700165 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 private int mThemeResource = 0;
169 private Resources.Theme mTheme = null;
170 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
173 private final Object mSync = new Object();
174
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700175 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700177 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700179 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700181 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700182 private File mNoBackupFilesDir;
183 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700185 @GuardedBy("mSync")
186 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700187
Jeff Brown6e539312015-02-24 18:53:21 -0800188 // The system service cache for the system services that are cached per-ContextImpl.
189 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800190
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700191 static ContextImpl getImpl(Context context) {
192 Context nextContext;
193 while ((context instanceof ContextWrapper) &&
194 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
195 context = nextContext;
196 }
197 return (ContextImpl)context;
198 }
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 @Override
201 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700202 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
204
205 @Override
206 public Resources getResources() {
207 return mResources;
208 }
209
210 @Override
211 public PackageManager getPackageManager() {
212 if (mPackageManager != null) {
213 return mPackageManager;
214 }
215
216 IPackageManager pm = ActivityThread.getPackageManager();
217 if (pm != null) {
218 // Doesn't matter if we make more than one instance.
219 return (mPackageManager = new ApplicationPackageManager(this, pm));
220 }
221
222 return null;
223 }
224
225 @Override
226 public ContentResolver getContentResolver() {
227 return mContentResolver;
228 }
229
230 @Override
231 public Looper getMainLooper() {
232 return mMainThread.getLooper();
233 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 @Override
236 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700237 return (mPackageInfo != null) ?
238 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700242 public void setTheme(int resId) {
243 if (mThemeResource != resId) {
244 mThemeResource = resId;
245 initializeTheme();
246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800250 public int getThemeResId() {
251 return mThemeResource;
252 }
253
254 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 public Resources.Theme getTheme() {
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700256 if (mTheme != null) {
257 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700259
260 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
261 getOuterContext().getApplicationInfo().targetSdkVersion);
262 initializeTheme();
263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 return mTheme;
265 }
266
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700267 private void initializeTheme() {
268 if (mTheme == null) {
269 mTheme = mResources.newTheme();
270 }
271 mTheme.applyStyle(mThemeResource, true);
272 }
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 @Override
275 public ClassLoader getClassLoader() {
276 return mPackageInfo != null ?
277 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
278 }
279
280 @Override
281 public String getPackageName() {
282 if (mPackageInfo != null) {
283 return mPackageInfo.getPackageName();
284 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800285 // No mPackageInfo means this is a Context for the system itself,
286 // and this here is its name.
287 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 }
289
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800290 /** @hide */
291 @Override
292 public String getBasePackageName() {
293 return mBasePackageName != null ? mBasePackageName : getPackageName();
294 }
295
Dianne Hackborn95d78532013-09-11 09:51:14 -0700296 /** @hide */
297 @Override
298 public String getOpPackageName() {
299 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
300 }
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700303 public ApplicationInfo getApplicationInfo() {
304 if (mPackageInfo != null) {
305 return mPackageInfo.getApplicationInfo();
306 }
307 throw new RuntimeException("Not supported in system context");
308 }
309
310 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 public String getPackageResourcePath() {
312 if (mPackageInfo != null) {
313 return mPackageInfo.getResDir();
314 }
315 throw new RuntimeException("Not supported in system context");
316 }
317
318 @Override
319 public String getPackageCodePath() {
320 if (mPackageInfo != null) {
321 return mPackageInfo.getAppDir();
322 }
323 throw new RuntimeException("Not supported in system context");
324 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200325
Jeff Brown6e539312015-02-24 18:53:21 -0800326 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700328 // At least one application in the world actually passes in a null
329 // name. This happened to work because when we generated the file name
330 // we would stringify it to "null.xml". Nice.
331 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
332 Build.VERSION_CODES.KITKAT) {
333 if (name == null) {
334 name = "null";
335 }
336 }
337
Jeff Sharkeybe782582016-02-15 18:35:57 -0700338 File file;
339 synchronized (ContextImpl.class) {
340 if (mSharedPrefsPaths == null) {
341 mSharedPrefsPaths = new ArrayMap<>();
342 }
343 file = mSharedPrefsPaths.get(name);
344 if (file == null) {
345 file = getSharedPreferencesPath(name);
346 mSharedPrefsPaths.put(name, file);
347 }
348 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700349 return getSharedPreferences(file, mode);
350 }
351
352 @Override
353 public SharedPreferences getSharedPreferences(File file, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700354 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700356 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700357 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
358 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700359 if (sp == null) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700360 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700361 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700362 return sp;
363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800365 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
366 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
367 // If somebody else (some other process) changed the prefs
368 // file behind our back, we reload it. This has been the
369 // historical (if undocumented) behavior.
370 sp.startReloadIfChangedUnexpectedly();
371 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700372 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 }
374
Jeff Sharkey35871f22016-01-29 17:13:29 -0700375 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700376 if (sSharedPrefsCache == null) {
377 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700378 }
379
380 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700381 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700382 if (packagePrefs == null) {
383 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700384 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700385 }
386
387 return packagePrefs;
388 }
389
390 /**
391 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700392 * requested prefix.
393 *
394 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700395 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600396 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700397 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
398 @Override
399 public boolean accept(File dir, String name) {
400 return name.startsWith(prefix);
401 }
402 });
403
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700404 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700405 for (File sourceFile : sourceFiles) {
406 final File targetFile = new File(targetDir, sourceFile.getName());
407 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
408 try {
409 FileUtils.copyFileOrThrow(sourceFile, targetFile);
410 FileUtils.copyPermissions(sourceFile, targetFile);
411 if (!sourceFile.delete()) {
412 throw new IOException("Failed to clean up " + sourceFile);
413 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700414 if (res != -1) {
415 res++;
416 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700417 } catch (IOException e) {
418 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700419 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700420 }
421 }
422 return res;
423 }
424
425 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600426 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700427 synchronized (ContextImpl.class) {
428 final File source = sourceContext.getSharedPreferencesPath(name);
429 final File target = getSharedPreferencesPath(name);
430
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600431 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700432 source.getName());
433 if (res > 0) {
434 // We moved at least one file, so evict any in-memory caches for
435 // either location
436 final ArrayMap<File, SharedPreferencesImpl> cache =
437 getSharedPreferencesCacheLocked();
438 cache.remove(source);
439 cache.remove(target);
440 }
441 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700442 }
443 }
444
445 @Override
446 public boolean deleteSharedPreferences(String name) {
447 synchronized (ContextImpl.class) {
448 final File prefs = getSharedPreferencesPath(name);
449 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
450
451 // Evict any in-memory caches
452 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
453 cache.remove(prefs);
454
455 prefs.delete();
456 prefsBackup.delete();
457
458 // We failed if files are still lingering
459 return !(prefs.exists() || prefsBackup.exists());
460 }
461 }
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 private File getPreferencesDir() {
464 synchronized (mSync) {
465 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700466 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700468 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
470 }
471
472 @Override
473 public FileInputStream openFileInput(String name)
474 throws FileNotFoundException {
475 File f = makeFilename(getFilesDir(), name);
476 return new FileInputStream(f);
477 }
478
479 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700480 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
481 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 final boolean append = (mode&MODE_APPEND) != 0;
483 File f = makeFilename(getFilesDir(), name);
484 try {
485 FileOutputStream fos = new FileOutputStream(f, append);
486 setFilePermissionsFromMode(f.getPath(), mode, 0);
487 return fos;
488 } catch (FileNotFoundException e) {
489 }
490
491 File parent = f.getParentFile();
492 parent.mkdir();
493 FileUtils.setPermissions(
494 parent.getPath(),
495 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
496 -1, -1);
497 FileOutputStream fos = new FileOutputStream(f, append);
498 setFilePermissionsFromMode(f.getPath(), mode, 0);
499 return fos;
500 }
501
502 @Override
503 public boolean deleteFile(String name) {
504 File f = makeFilename(getFilesDir(), name);
505 return f.delete();
506 }
507
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700508 /**
509 * Common-path handling of app data dir creation
510 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700511 private static File ensurePrivateDirExists(File file) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700512 if (!file.exists()) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700513 try {
514 Os.mkdir(file.getAbsolutePath(), 0771);
Jeff Sharkey46ed6f42016-02-15 14:16:08 -0700515 Os.chmod(file.getAbsolutePath(), 0771);
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700516 } catch (ErrnoException e) {
517 if (e.errno == OsConstants.EEXIST) {
518 // We must have raced with someone; that's okay
519 } else {
520 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700521 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700522 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700523 }
524 return file;
525 }
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 @Override
528 public File getFilesDir() {
529 synchronized (mSync) {
530 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700531 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700533 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700534 }
535 }
536
537 @Override
538 public File getNoBackupFilesDir() {
539 synchronized (mSync) {
540 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700541 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700543 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
545 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800548 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700549 // Operates on primary external storage
550 return getExternalFilesDirs(type)[0];
551 }
552
553 @Override
554 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800555 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600556 File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700557 if (type != null) {
558 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800559 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700560 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800561 }
562 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200563
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800564 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800565 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700566 // Operates on primary external storage
567 return getObbDirs()[0];
568 }
569
570 @Override
571 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800572 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600573 File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
574 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800575 }
576 }
577
578 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 public File getCacheDir() {
580 synchronized (mSync) {
581 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700582 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700584 return ensurePrivateDirExists(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200587
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800588 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700589 public File getCodeCacheDir() {
590 synchronized (mSync) {
591 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700592 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700593 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700594 return ensurePrivateDirExists(mCodeCacheDir);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700595 }
596 }
597
598 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800599 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700600 // Operates on primary external storage
601 return getExternalCacheDirs()[0];
602 }
603
604 @Override
605 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800606 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600607 File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
608 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800609 }
610 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700613 public File[] getExternalMediaDirs() {
614 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600615 File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
616 return ensureExternalDirsExistOrFilter(dirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700617 }
618 }
619
620 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public File getFileStreamPath(String name) {
622 return makeFilename(getFilesDir(), name);
623 }
624
625 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700626 public File getSharedPreferencesPath(String name) {
627 return makeFilename(getPreferencesDir(), name + ".xml");
628 }
629
630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700632 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634
635 @Override
636 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700637 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639
640 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700641 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
642 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700643 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700644 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700645 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
646 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
647 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
648 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800649 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
650 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
651 }
Jeff Brown47847f32012-03-22 19:13:11 -0700652 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700653 setFilePermissionsFromMode(f.getPath(), mode, 0);
654 return db;
655 }
656
657 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600658 public boolean moveDatabaseFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700659 synchronized (ContextImpl.class) {
660 final File source = sourceContext.getDatabasePath(name);
661 final File target = getDatabasePath(name);
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600662 return moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700663 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700664 }
665 }
666
667 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 public boolean deleteDatabase(String name) {
669 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700670 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800671 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 } catch (Exception e) {
673 }
674 return false;
675 }
676
677 @Override
678 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700679 File dir;
680 File f;
681
682 if (name.charAt(0) == File.separatorChar) {
683 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
684 dir = new File(dirPath);
685 name = name.substring(name.lastIndexOf(File.separatorChar));
686 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700687
688 if (!dir.isDirectory() && dir.mkdir()) {
689 FileUtils.setPermissions(dir.getPath(),
690 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
691 -1, -1);
692 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700693 } else {
694 dir = getDatabasesDir();
695 f = makeFilename(dir, name);
696 }
697
Jeff Sharkey35871f22016-01-29 17:13:29 -0700698 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
700
701 @Override
702 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700703 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 private File getDatabasesDir() {
707 synchronized (mSync) {
708 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700709 if ("android".equals(getPackageName())) {
710 mDatabasesDir = new File("/data/system");
711 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700712 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700715 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800720 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700722 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724
725 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800726 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700727 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700728 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730
731 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800732 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700734 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736
737 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800738 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700740 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800744 @Deprecated
745 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700746 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748
749 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800750 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700752 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 }
754
755 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800756 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700758 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760
Jeff Brown6e539312015-02-24 18:53:21 -0800761 private WallpaperManager getWallpaperManager() {
762 return getSystemService(WallpaperManager.class);
763 }
764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 @Override
766 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700767 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700768 startActivity(intent, null);
769 }
770
Amith Yamasani82644082012-08-03 13:09:11 -0700771 /** @hide */
772 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700773 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700774 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700775 }
776
Dianne Hackborna4972e92012-03-14 10:38:05 -0700777 @Override
778 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700779 warnIfCallingFromSystemProcess();
Jorim Jaggi2adba072016-03-03 13:43:39 +0100780
781 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
782 // generally not allowed, except if the caller specifies the task id the activity should
783 // be launched in.
784 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
785 && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 throw new AndroidRuntimeException(
787 "Calling startActivity() from outside of an Activity "
788 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
789 + " Is this really what you want?");
790 }
791 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700792 getOuterContext(), mMainThread.getApplicationThread(), null,
793 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
795
Amith Yamasani258848d2012-08-10 17:06:33 -0700796 /** @hide */
797 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700798 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700799 try {
800 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800801 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700802 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700803 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700804 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700805 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700806 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700807 }
808 }
809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800811 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700812 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700813 startActivities(intents, null);
814 }
815
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700816 /** @hide */
817 @Override
818 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
819 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
820 throw new AndroidRuntimeException(
821 "Calling startActivities() from outside of an Activity "
822 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
823 + " Is this really what you want?");
824 }
825 mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700826 getOuterContext(), mMainThread.getApplicationThread(), null,
827 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700828 }
829
Dianne Hackborna4972e92012-03-14 10:38:05 -0700830 @Override
831 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700832 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800833 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
834 throw new AndroidRuntimeException(
835 "Calling startActivities() from outside of an Activity "
836 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
837 + " Is this really what you want?");
838 }
839 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700840 getOuterContext(), mMainThread.getApplicationThread(), null,
841 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800842 }
843
844 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700845 public void startIntentSender(IntentSender intent,
846 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
847 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700848 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
849 }
850
851 @Override
852 public void startIntentSender(IntentSender intent, Intent fillInIntent,
853 int flagsMask, int flagsValues, int extraFlags, Bundle options)
854 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700855 try {
856 String resolvedType = null;
857 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700858 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -0700859 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700860 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
861 }
862 int result = ActivityManagerNative.getDefault()
863 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
864 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700865 0, flagsMask, flagsValues, options);
866 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700867 throw new IntentSender.SendIntentException();
868 }
869 Instrumentation.checkStartActivityResult(result, null);
870 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700871 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700872 }
873 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200874
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700875 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700877 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
879 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700880 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700882 mMainThread.getApplicationThread(), intent, resolvedType, null,
883 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
884 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700886 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888 }
889
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800890 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700892 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700894 String[] receiverPermissions = receiverPermission == null ? null
895 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700897 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700899 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700900 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
901 null, false, false, getUserId());
902 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700903 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700904 }
905 }
906
907 @Override
908 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
909 warnIfCallingFromSystemProcess();
910 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
911 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700912 intent.prepareToLeaveProcess(this);
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700913 ActivityManagerNative.getDefault().broadcastIntent(
914 mMainThread.getApplicationThread(), intent, resolvedType, null,
915 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700916 null, false, false, getUserId());
917 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700918 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -0700919 }
920 }
921
922 @Override
923 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
924 warnIfCallingFromSystemProcess();
925 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700926 String[] receiverPermissions = receiverPermission == null ? null
927 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -0700928 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700929 intent.prepareToLeaveProcess(this);
Dianne Hackborna750a632015-06-16 17:18:23 -0700930 ActivityManagerNative.getDefault().broadcastIntent(
931 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700932 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700933 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800934 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700935 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800936 }
937 }
938
939 @Override
940 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
941 warnIfCallingFromSystemProcess();
942 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700943 String[] receiverPermissions = receiverPermission == null ? null
944 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800945 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700946 intent.prepareToLeaveProcess(this);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800947 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700948 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700949 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -0700950 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700952 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954 }
955
956 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -0700957 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700958 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700960 String[] receiverPermissions = receiverPermission == null ? null
961 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700963 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700965 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700966 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700967 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700969 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971 }
972
973 @Override
974 public void sendOrderedBroadcast(Intent intent,
975 String receiverPermission, BroadcastReceiver resultReceiver,
976 Handler scheduler, int initialCode, String initialData,
977 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800978 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700979 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
980 }
981
982 @Override
983 public void sendOrderedBroadcast(Intent intent,
984 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
985 Handler scheduler, int initialCode, String initialData,
986 Bundle initialExtras) {
987 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
988 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800989 }
990
991 @Override
992 public void sendOrderedBroadcast(Intent intent,
993 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
994 Handler scheduler, int initialCode, String initialData,
995 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -0700996 sendOrderedBroadcast(intent, receiverPermission, appOp,
997 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
998 }
999
1000 void sendOrderedBroadcast(Intent intent,
1001 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1002 Handler scheduler, int initialCode, String initialData,
1003 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001004 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 IIntentReceiver rd = null;
1006 if (resultReceiver != null) {
1007 if (mPackageInfo != null) {
1008 if (scheduler == null) {
1009 scheduler = mMainThread.getHandler();
1010 }
1011 rd = mPackageInfo.getReceiverDispatcher(
1012 resultReceiver, getOuterContext(), scheduler,
1013 mMainThread.getInstrumentation(), false);
1014 } else {
1015 if (scheduler == null) {
1016 scheduler = mMainThread.getHandler();
1017 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001018 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1020 }
1021 }
1022 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001023 String[] receiverPermissions = receiverPermission == null ? null
1024 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001026 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 ActivityManagerNative.getDefault().broadcastIntent(
1028 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001029 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001030 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001032 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034 }
1035
1036 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001037 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001038 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1039 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001040 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001041 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001042 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001043 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001044 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001045 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001046 }
1047 }
1048
1049 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001050 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1051 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001052 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1053 }
1054
1055 @Override
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001056 public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission,
1057 Bundle options) {
1058 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1059 String[] receiverPermissions = receiverPermission == null ? null
1060 : new String[] {receiverPermission};
1061 try {
1062 intent.prepareToLeaveProcess(this);
1063 ActivityManagerNative.getDefault().broadcastIntent(
1064 mMainThread.getApplicationThread(), intent, resolvedType, null,
1065 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1066 options, false, false, user.getIdentifier());
1067 } catch (RemoteException e) {
1068 throw e.rethrowFromSystemServer();
1069 }
1070 }
1071
1072 @Override
Svet Ganov16a16892015-04-16 10:32:04 -07001073 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1074 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001075 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001076 String[] receiverPermissions = receiverPermission == null ? null
1077 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001078 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001079 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001080 ActivityManagerNative.getDefault().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001081 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001082 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001083 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001084 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001085 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001086 }
1087 }
1088
1089 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001090 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001091 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001092 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001093 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001094 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001095 }
1096
1097 @Override
1098 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1099 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001100 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1101 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1102 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1103 }
1104
1105 @Override
1106 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1107 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1108 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001109 IIntentReceiver rd = null;
1110 if (resultReceiver != null) {
1111 if (mPackageInfo != null) {
1112 if (scheduler == null) {
1113 scheduler = mMainThread.getHandler();
1114 }
1115 rd = mPackageInfo.getReceiverDispatcher(
1116 resultReceiver, getOuterContext(), scheduler,
1117 mMainThread.getInstrumentation(), false);
1118 } else {
1119 if (scheduler == null) {
1120 scheduler = mMainThread.getHandler();
1121 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001122 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1123 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001124 }
1125 }
1126 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001127 String[] receiverPermissions = receiverPermission == null ? null
1128 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001129 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001130 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001131 ActivityManagerNative.getDefault().broadcastIntent(
1132 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001133 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001134 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001135 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001136 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001137 }
1138 }
1139
1140 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001141 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001143 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1145 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001146 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 ActivityManagerNative.getDefault().broadcastIntent(
1148 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001149 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001150 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001152 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
1154 }
1155
1156 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001157 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001158 public void sendStickyOrderedBroadcast(Intent intent,
1159 BroadcastReceiver resultReceiver,
1160 Handler scheduler, int initialCode, String initialData,
1161 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001162 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001163 IIntentReceiver rd = null;
1164 if (resultReceiver != null) {
1165 if (mPackageInfo != null) {
1166 if (scheduler == null) {
1167 scheduler = mMainThread.getHandler();
1168 }
1169 rd = mPackageInfo.getReceiverDispatcher(
1170 resultReceiver, getOuterContext(), scheduler,
1171 mMainThread.getInstrumentation(), false);
1172 } else {
1173 if (scheduler == null) {
1174 scheduler = mMainThread.getHandler();
1175 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001176 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001177 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1178 }
1179 }
1180 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1181 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001182 intent.prepareToLeaveProcess(this);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001183 ActivityManagerNative.getDefault().broadcastIntent(
1184 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1185 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001186 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001187 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001188 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001189 }
1190 }
1191
1192 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001193 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 public void removeStickyBroadcast(Intent intent) {
1195 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1196 if (resolvedType != null) {
1197 intent = new Intent(intent);
1198 intent.setDataAndType(intent.getData(), resolvedType);
1199 }
1200 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001201 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001203 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001205 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207 }
1208
1209 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001210 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001211 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1212 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1213 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001214 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001215 ActivityManagerNative.getDefault().broadcastIntent(
1216 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001217 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1218 user.getIdentifier());
1219 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001220 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001221 }
1222 }
1223
1224 @Override
1225 @Deprecated
1226 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1227 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1228 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001229 intent.prepareToLeaveProcess(this);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001230 ActivityManagerNative.getDefault().broadcastIntent(
1231 mMainThread.getApplicationThread(), intent, resolvedType, null,
1232 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1233 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001234 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001235 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001236 }
1237 }
1238
1239 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001240 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001241 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1242 UserHandle user, BroadcastReceiver resultReceiver,
1243 Handler scheduler, int initialCode, String initialData,
1244 Bundle initialExtras) {
1245 IIntentReceiver rd = null;
1246 if (resultReceiver != null) {
1247 if (mPackageInfo != null) {
1248 if (scheduler == null) {
1249 scheduler = mMainThread.getHandler();
1250 }
1251 rd = mPackageInfo.getReceiverDispatcher(
1252 resultReceiver, getOuterContext(), scheduler,
1253 mMainThread.getInstrumentation(), false);
1254 } else {
1255 if (scheduler == null) {
1256 scheduler = mMainThread.getHandler();
1257 }
1258 rd = new LoadedApk.ReceiverDispatcher(
1259 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1260 }
1261 }
1262 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1263 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001264 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001265 ActivityManagerNative.getDefault().broadcastIntent(
1266 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1267 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001268 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001269 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001270 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001271 }
1272 }
1273
1274 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001275 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001276 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1277 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1278 if (resolvedType != null) {
1279 intent = new Intent(intent);
1280 intent.setDataAndType(intent.getData(), resolvedType);
1281 }
1282 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001283 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001284 ActivityManagerNative.getDefault().unbroadcastIntent(
1285 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1286 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001287 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001288 }
1289 }
1290
1291 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1293 return registerReceiver(receiver, filter, null, null);
1294 }
1295
1296 @Override
1297 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1298 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001299 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001300 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 }
1302
Dianne Hackborn20e80982012-08-31 19:00:44 -07001303 @Override
1304 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1305 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1306 return registerReceiverInternal(receiver, user.getIdentifier(),
1307 filter, broadcastPermission, scheduler, getOuterContext());
1308 }
1309
1310 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 IntentFilter filter, String broadcastPermission,
1312 Handler scheduler, Context context) {
1313 IIntentReceiver rd = null;
1314 if (receiver != null) {
1315 if (mPackageInfo != null && context != null) {
1316 if (scheduler == null) {
1317 scheduler = mMainThread.getHandler();
1318 }
1319 rd = mPackageInfo.getReceiverDispatcher(
1320 receiver, context, scheduler,
1321 mMainThread.getInstrumentation(), true);
1322 } else {
1323 if (scheduler == null) {
1324 scheduler = mMainThread.getHandler();
1325 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001326 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001327 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 }
1329 }
1330 try {
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001331 final Intent intent = ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001332 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001333 rd, filter, broadcastPermission, userId);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001334 if (intent != null) {
1335 intent.setExtrasClassLoader(getClassLoader());
1336 intent.prepareToEnterProcess();
1337 }
1338 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001340 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 }
1342 }
1343
1344 @Override
1345 public void unregisterReceiver(BroadcastReceiver receiver) {
1346 if (mPackageInfo != null) {
1347 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1348 getOuterContext(), receiver);
1349 try {
1350 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1351 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001352 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 }
1354 } else {
1355 throw new RuntimeException("Not supported in system context");
1356 }
1357 }
1358
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001359 private void validateServiceIntent(Intent service) {
1360 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001361 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001362 IllegalArgumentException ex = new IllegalArgumentException(
1363 "Service Intent must be explicit: " + service);
1364 throw ex;
1365 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001366 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1367 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001368 }
1369 }
1370 }
1371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 @Override
1373 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001374 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001375 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001376 }
1377
1378 @Override
1379 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001380 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001381 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001382 }
1383
1384 @Override
1385 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001386 return startServiceCommon(service, user);
1387 }
1388
1389 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001391 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001392 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 ComponentName cn = ActivityManagerNative.getDefault().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001394 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
1395 getContentResolver()), getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001396 if (cn != null) {
1397 if (cn.getPackageName().equals("!")) {
1398 throw new SecurityException(
1399 "Not allowed to start service " + service
1400 + " without permission " + cn.getClassName());
1401 } else if (cn.getPackageName().equals("!!")) {
1402 throw new SecurityException(
1403 "Unable to start service " + service
1404 + ": " + cn.getClassName());
1405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407 return cn;
1408 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001409 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 }
1411 }
1412
1413 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001414 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001415 return stopServiceCommon(service, user);
1416 }
1417
1418 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001420 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001421 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 int res = ActivityManagerNative.getDefault().stopService(
1423 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001424 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 if (res < 0) {
1426 throw new SecurityException(
1427 "Not allowed to stop service " + service);
1428 }
1429 return res != 0;
1430 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001431 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 }
1433 }
1434
1435 @Override
1436 public boolean bindService(Intent service, ServiceConnection conn,
1437 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001438 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001439 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1440 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001441 }
1442
1443 /** @hide */
1444 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001445 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1446 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001447 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001448 }
1449
Adrian Roos691546e2016-02-09 10:13:41 -08001450 /** @hide */
1451 @Override
1452 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1453 Handler handler, UserHandle user) {
1454 if (handler == null) {
1455 throw new IllegalArgumentException("handler must not be null.");
1456 }
1457 return bindServiceCommon(service, conn, flags, handler, user);
1458 }
1459
1460 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1461 handler, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001463 if (conn == null) {
1464 throw new IllegalArgumentException("connection is null");
1465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 if (mPackageInfo != null) {
Adrian Roos691546e2016-02-09 10:13:41 -08001467 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 } else {
1469 throw new RuntimeException("Not supported in system context");
1470 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001471 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001473 IBinder token = getActivityToken();
1474 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1475 && mPackageInfo.getApplicationInfo().targetSdkVersion
1476 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1477 flags |= BIND_WAIVE_PRIORITY;
1478 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001479 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 int res = ActivityManagerNative.getDefault().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001481 mMainThread.getApplicationThread(), getActivityToken(), service,
1482 service.resolveTypeIfNeeded(getContentResolver()),
1483 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 if (res < 0) {
1485 throw new SecurityException(
1486 "Not allowed to bind to service " + service);
1487 }
1488 return res != 0;
1489 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001490 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
1492 }
1493
1494 @Override
1495 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001496 if (conn == null) {
1497 throw new IllegalArgumentException("connection is null");
1498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 if (mPackageInfo != null) {
1500 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1501 getOuterContext(), conn);
1502 try {
1503 ActivityManagerNative.getDefault().unbindService(sd);
1504 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001505 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
1507 } else {
1508 throw new RuntimeException("Not supported in system context");
1509 }
1510 }
1511
1512 @Override
1513 public boolean startInstrumentation(ComponentName className,
1514 String profileFile, Bundle arguments) {
1515 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001516 if (arguments != null) {
1517 arguments.setAllowFds(false);
1518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001520 className, profileFile, 0, arguments, null, null, getUserId(),
1521 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001523 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
1526
1527 @Override
1528 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001529 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
1531
Jeff Brown6e539312015-02-24 18:53:21 -08001532 @Override
1533 public String getSystemServiceName(Class<?> serviceClass) {
1534 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001535 }
1536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 @Override
1538 public int checkPermission(String permission, int pid, int uid) {
1539 if (permission == null) {
1540 throw new IllegalArgumentException("permission is null");
1541 }
1542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 try {
1544 return ActivityManagerNative.getDefault().checkPermission(
1545 permission, pid, uid);
1546 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001547 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
1549 }
1550
Dianne Hackbornff170242014-11-19 10:59:01 -08001551 /** @hide */
1552 @Override
1553 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1554 if (permission == null) {
1555 throw new IllegalArgumentException("permission is null");
1556 }
1557
1558 try {
1559 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1560 permission, pid, uid, callerToken);
1561 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001562 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001563 }
1564 }
1565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 @Override
1567 public int checkCallingPermission(String permission) {
1568 if (permission == null) {
1569 throw new IllegalArgumentException("permission is null");
1570 }
1571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 int pid = Binder.getCallingPid();
1573 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001574 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576 return PackageManager.PERMISSION_DENIED;
1577 }
1578
1579 @Override
1580 public int checkCallingOrSelfPermission(String permission) {
1581 if (permission == null) {
1582 throw new IllegalArgumentException("permission is null");
1583 }
1584
1585 return checkPermission(permission, Binder.getCallingPid(),
1586 Binder.getCallingUid());
1587 }
1588
Svetoslavc6d1c342015-02-26 14:44:43 -08001589 @Override
1590 public int checkSelfPermission(String permission) {
1591 if (permission == null) {
1592 throw new IllegalArgumentException("permission is null");
1593 }
1594
1595 return checkPermission(permission, Process.myPid(), Process.myUid());
1596 }
1597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 private void enforce(
1599 String permission, int resultOfCheck,
1600 boolean selfToo, int uid, String message) {
1601 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1602 throw new SecurityException(
1603 (message != null ? (message + ": ") : "") +
1604 (selfToo
1605 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001606 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 permission +
1608 ".");
1609 }
1610 }
1611
Jeff Brown6e539312015-02-24 18:53:21 -08001612 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 public void enforcePermission(
1614 String permission, int pid, int uid, String message) {
1615 enforce(permission,
1616 checkPermission(permission, pid, uid),
1617 false,
1618 uid,
1619 message);
1620 }
1621
Jeff Brown6e539312015-02-24 18:53:21 -08001622 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 public void enforceCallingPermission(String permission, String message) {
1624 enforce(permission,
1625 checkCallingPermission(permission),
1626 false,
1627 Binder.getCallingUid(),
1628 message);
1629 }
1630
Jeff Brown6e539312015-02-24 18:53:21 -08001631 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 public void enforceCallingOrSelfPermission(
1633 String permission, String message) {
1634 enforce(permission,
1635 checkCallingOrSelfPermission(permission),
1636 true,
1637 Binder.getCallingUid(),
1638 message);
1639 }
1640
1641 @Override
1642 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1643 try {
1644 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001645 mMainThread.getApplicationThread(), toPackage,
1646 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001648 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 }
1650 }
1651
1652 @Override
1653 public void revokeUriPermission(Uri uri, int modeFlags) {
1654 try {
1655 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001656 mMainThread.getApplicationThread(),
1657 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001659 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 }
1661 }
1662
1663 @Override
1664 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 try {
1666 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001667 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001668 resolveUserId(uri), null);
1669 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001670 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001671 }
1672 }
1673
1674 /** @hide */
1675 @Override
1676 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1677 try {
1678 return ActivityManagerNative.getDefault().checkUriPermission(
1679 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1680 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001682 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
1684 }
1685
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001686 private int resolveUserId(Uri uri) {
1687 return ContentProvider.getUserIdFromUri(uri, getUserId());
1688 }
1689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 @Override
1691 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 int pid = Binder.getCallingPid();
1693 if (pid != Process.myPid()) {
1694 return checkUriPermission(uri, pid,
1695 Binder.getCallingUid(), modeFlags);
1696 }
1697 return PackageManager.PERMISSION_DENIED;
1698 }
1699
1700 @Override
1701 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1702 return checkUriPermission(uri, Binder.getCallingPid(),
1703 Binder.getCallingUid(), modeFlags);
1704 }
1705
1706 @Override
1707 public int checkUriPermission(Uri uri, String readPermission,
1708 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001709 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1711 + readPermission + " writePermission=" + writePermission
1712 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1713 }
1714 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1715 if (readPermission == null
1716 || checkPermission(readPermission, pid, uid)
1717 == PackageManager.PERMISSION_GRANTED) {
1718 return PackageManager.PERMISSION_GRANTED;
1719 }
1720 }
1721 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1722 if (writePermission == null
1723 || checkPermission(writePermission, pid, uid)
1724 == PackageManager.PERMISSION_GRANTED) {
1725 return PackageManager.PERMISSION_GRANTED;
1726 }
1727 }
1728 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1729 : PackageManager.PERMISSION_DENIED;
1730 }
1731
1732 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001733 StringBuilder builder = new StringBuilder();
1734 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1735 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001737 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1738 builder.append("write and ");
1739 }
1740 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1741 builder.append("persistable and ");
1742 }
1743 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1744 builder.append("prefix and ");
1745 }
1746
1747 if (builder.length() > 5) {
1748 builder.setLength(builder.length() - 5);
1749 return builder.toString();
1750 } else {
1751 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
1754
1755 private void enforceForUri(
1756 int modeFlags, int resultOfCheck, boolean selfToo,
1757 int uid, Uri uri, String message) {
1758 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1759 throw new SecurityException(
1760 (message != null ? (message + ": ") : "") +
1761 (selfToo
1762 ? "Neither user " + uid + " nor current process has "
1763 : "User " + uid + " does not have ") +
1764 uriModeFlagToString(modeFlags) +
1765 " permission on " +
1766 uri +
1767 ".");
1768 }
1769 }
1770
Jeff Brown6e539312015-02-24 18:53:21 -08001771 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 public void enforceUriPermission(
1773 Uri uri, int pid, int uid, int modeFlags, String message) {
1774 enforceForUri(
1775 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1776 false, uid, uri, message);
1777 }
1778
Jeff Brown6e539312015-02-24 18:53:21 -08001779 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 public void enforceCallingUriPermission(
1781 Uri uri, int modeFlags, String message) {
1782 enforceForUri(
1783 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001784 false,
1785 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787
Jeff Brown6e539312015-02-24 18:53:21 -08001788 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 public void enforceCallingOrSelfUriPermission(
1790 Uri uri, int modeFlags, String message) {
1791 enforceForUri(
1792 modeFlags,
1793 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1794 Binder.getCallingUid(), uri, message);
1795 }
1796
Jeff Brown6e539312015-02-24 18:53:21 -08001797 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 public void enforceUriPermission(
1799 Uri uri, String readPermission, String writePermission,
1800 int pid, int uid, int modeFlags, String message) {
1801 enforceForUri(modeFlags,
1802 checkUriPermission(
1803 uri, readPermission, writePermission, pid, uid,
1804 modeFlags),
1805 false,
1806 uid,
1807 uri,
1808 message);
1809 }
1810
Tom O'Neill365632e2013-09-09 09:34:58 -07001811 /**
1812 * Logs a warning if the system process directly called a method such as
1813 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1814 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1815 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001816 private void warnIfCallingFromSystemProcess() {
1817 if (Process.myUid() == Process.SYSTEM_UID) {
1818 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001819 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001820 }
1821 }
1822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001824 public Context createApplicationContext(ApplicationInfo application, int flags)
1825 throws NameNotFoundException {
1826 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1827 flags | CONTEXT_REGISTER_PACKAGE);
1828 if (pi != null) {
Svetoslav976e8bd2014-07-16 15:12:03 -07001829 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001830 new UserHandle(UserHandle.getUserId(application.uid)), flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001831 mDisplay, null, Display.INVALID_DISPLAY);
Svetoslav976e8bd2014-07-16 15:12:03 -07001832 if (c.mResources != null) {
1833 return c;
1834 }
1835 }
1836
1837 throw new PackageManager.NameNotFoundException(
1838 "Application package " + application.packageName + " not found");
1839 }
1840
1841 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001843 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001844 return createPackageContextAsUser(packageName, flags,
1845 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001846 }
1847
1848 @Override
1849 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1850 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00001851 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001852 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001853 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855
Jeff Browndefd4a62014-03-10 21:24:37 -07001856 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001857 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 if (pi != null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001859 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001860 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 if (c.mResources != null) {
1862 return c;
1863 }
1864 }
1865
1866 // Should be a better exception.
1867 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001868 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
1870
Romain Guy870e09f2009-07-06 16:35:25 -07001871 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001872 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001873 if (overrideConfiguration == null) {
1874 throw new IllegalArgumentException("overrideConfiguration must not be null");
1875 }
1876
Jeff Browndefd4a62014-03-10 21:24:37 -07001877 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001878 mUser, mFlags, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001879 }
1880
1881 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001882 public Context createDisplayContext(Display display) {
1883 if (display == null) {
1884 throw new IllegalArgumentException("display must not be null");
1885 }
1886
Jeff Browndefd4a62014-03-10 21:24:37 -07001887 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001888 mUser, mFlags, display, null, Display.INVALID_DISPLAY);
Jeff Browna492c3a2012-08-23 19:48:44 -07001889 }
1890
Jeff Browna492c3a2012-08-23 19:48:44 -07001891 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001892 public Context createDeviceProtectedStorageContext() {
1893 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
1894 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001895 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1896 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1897 }
1898
1899 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001900 public Context createCredentialProtectedStorageContext() {
1901 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
1902 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001903 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1904 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1905 }
1906
1907 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001908 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001909 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
1910 }
1911
1912 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001913 public boolean isDeviceProtectedStorage() {
1914 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001915 }
1916
1917 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001918 public boolean isCredentialProtectedStorage() {
1919 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07001920 }
1921
Jeff Brown98365d72012-08-19 20:30:52 -07001922 @Override
Adam Lesinski4ece3d62016-06-16 18:05:41 -07001923 public Display getDisplay() {
1924 final DisplayAdjustments displayAdjustments = mResources.getDisplayAdjustments();
1925 if (mDisplay == null) {
1926 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
1927 displayAdjustments);
1928 }
1929
1930 if (!mDisplay.getDisplayAdjustments().equals(displayAdjustments)) {
1931 mDisplay = mResourcesManager.getAdjustedDisplay(mDisplay.getDisplayId(),
1932 displayAdjustments);
1933 }
1934 return mDisplay;
1935 }
1936
1937 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001938 public DisplayAdjustments getDisplayAdjustments(int displayId) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07001939 return mResources.getDisplayAdjustments();
Jeff Brown98365d72012-08-19 20:30:52 -07001940 }
1941
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001942 @Override
1943 public File getDataDir() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001945 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001946 if (isCredentialProtectedStorage()) {
1947 res = mPackageInfo.getCredentialProtectedDataDirFile();
1948 } else if (isDeviceProtectedStorage()) {
1949 res = mPackageInfo.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001950 } else {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001951 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001952 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001953
1954 if (res != null) {
Jeff Sharkey21f50722016-04-27 12:38:02 -06001955 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Jeff Sharkey24492ae2016-04-25 13:20:25 -06001956 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
1957 new Throwable());
1958 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001959 return res;
1960 } else {
1961 throw new RuntimeException(
1962 "No data directory found for package " + getPackageName());
1963 }
1964 } else {
1965 throw new RuntimeException(
1966 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969
1970 @Override
1971 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07001972 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001974 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 if (!file.exists()) {
1976 file.mkdir();
1977 setFilePermissionsFromMode(file.getPath(), mode,
1978 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1979 }
1980 return file;
1981 }
1982
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001983 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08001984 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001985 public int getUserId() {
1986 return mUser.getIdentifier();
1987 }
1988
Dianne Hackborn21556372010-02-04 16:34:40 -08001989 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001990 LoadedApk packageInfo = new LoadedApk(mainThread);
1991 ContextImpl context = new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001992 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001993 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07001994 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 return context;
1996 }
1997
Jeff Browndefd4a62014-03-10 21:24:37 -07001998 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
1999 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
2000 return new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002001 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07002002 }
2003
2004 static ContextImpl createActivityContext(ActivityThread mainThread,
Adam Lesinski082614c2016-03-04 14:33:47 -08002005 LoadedApk packageInfo, IBinder activityToken, int displayId,
2006 Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002007 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski082614c2016-03-04 14:33:47 -08002008 return new ContextImpl(null, mainThread, packageInfo, activityToken, null, 0,
Wale Ogunwale26698512015-06-05 16:55:33 -07002009 null, overrideConfiguration, displayId);
Jeff Browndefd4a62014-03-10 21:24:37 -07002010 }
2011
2012 private ContextImpl(ContextImpl container, ActivityThread mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002013 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07002014 Display display, Configuration overrideConfiguration, int createDisplayWithId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002017 // If creator didn't specify which storage to use, use the default
2018 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002019 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2020 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002021 final File dataDir = packageInfo.getDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002022 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
2023 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
2024 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
2025 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002026 }
2027 }
2028
Jeff Browndefd4a62014-03-10 21:24:37 -07002029 mMainThread = mainThread;
2030 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002031 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032
Jeff Browndefd4a62014-03-10 21:24:37 -07002033 if (user == null) {
2034 user = Process.myUserHandle();
2035 }
2036 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07002039 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002040
Wale Ogunwale26698512015-06-05 16:55:33 -07002041 final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
Wale Ogunwalecac3dc62015-06-09 15:35:02 -07002042 ? createDisplayWithId
2043 : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
Wale Ogunwale26698512015-06-05 16:55:33 -07002044
Jeff Browndefd4a62014-03-10 21:24:37 -07002045 CompatibilityInfo compatInfo = null;
2046 if (container != null) {
2047 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
2048 }
Wale Ogunwale9cf99542015-02-18 16:31:34 -08002049 if (compatInfo == null) {
2050 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2051 ? packageInfo.getCompatibilityInfo()
2052 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Jeff Browndefd4a62014-03-10 21:24:37 -07002053 }
Wale Ogunwale26698512015-06-05 16:55:33 -07002054
Jeff Browndefd4a62014-03-10 21:24:37 -07002055 Resources resources = packageInfo.getResources(mainThread);
2056 if (resources != null) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002057 if (displayId != Display.DEFAULT_DISPLAY
Jeff Browndefd4a62014-03-10 21:24:37 -07002058 || overrideConfiguration != null
2059 || (compatInfo != null && compatInfo.applicationScale
2060 != resources.getCompatibilityInfo().applicationScale)) {
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002061
2062 if (container != null) {
2063 // This is a nested Context, so it can't be a base Activity context.
2064 // Just create a regular Resources object associated with the Activity.
2065 resources = mResourcesManager.getResources(
2066 activityToken,
2067 packageInfo.getResDir(),
2068 packageInfo.getSplitResDirs(),
2069 packageInfo.getOverlayDirs(),
2070 packageInfo.getApplicationInfo().sharedLibraryFiles,
2071 displayId,
2072 overrideConfiguration,
2073 compatInfo,
2074 packageInfo.getClassLoader());
2075 } else {
2076 // This is not a nested Context, so it must be the root Activity context.
2077 // All other nested Contexts will inherit the configuration set here.
2078 resources = mResourcesManager.createBaseActivityResources(
2079 activityToken,
2080 packageInfo.getResDir(),
2081 packageInfo.getSplitResDirs(),
2082 packageInfo.getOverlayDirs(),
2083 packageInfo.getApplicationInfo().sharedLibraryFiles,
2084 displayId,
2085 overrideConfiguration,
2086 compatInfo,
2087 packageInfo.getClassLoader());
2088 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002089 }
2090 }
2091 mResources = resources;
2092
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002093 mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
2094 : mResourcesManager.getAdjustedDisplay(displayId, mResources.getDisplayAdjustments());
2095
Jeff Browndefd4a62014-03-10 21:24:37 -07002096 if (container != null) {
2097 mBasePackageName = container.mBasePackageName;
2098 mOpPackageName = container.mOpPackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002099 } else {
2100 mBasePackageName = packageInfo.mPackageName;
2101 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2102 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2103 // Special case: system components allow themselves to be loaded in to other
2104 // processes. For purposes of app ops, we must then consider the context as
2105 // belonging to the package of this process, not the system itself, otherwise
2106 // the package+uid verifications in app ops will fail.
2107 mOpPackageName = ActivityThread.currentPackageName();
2108 } else {
2109 mOpPackageName = mBasePackageName;
2110 }
2111 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002112
2113 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
2115
Narayan Kamath29564cd2014-08-07 10:57:40 +01002116 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2117 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
2119
2120 final void scheduleFinalCleanup(String who, String what) {
2121 mMainThread.scheduleContextCleanup(this, who, what);
2122 }
2123
2124 final void performFinalCleanup(String who, String what) {
2125 //Log.i(TAG, "Cleanup up context: " + this);
2126 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2127 }
2128
2129 final Context getReceiverRestrictedContext() {
2130 if (mReceiverRestrictedContext != null) {
2131 return mReceiverRestrictedContext;
2132 }
2133 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2134 }
2135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 final void setOuterContext(Context context) {
2137 mOuterContext = context;
2138 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 final Context getOuterContext() {
2141 return mOuterContext;
2142 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 final IBinder getActivityToken() {
2145 return mActivityToken;
2146 }
2147
Jeff Sharkey634dc422016-01-30 17:44:15 -07002148 private void checkMode(int mode) {
2149 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2150 if ((mode & MODE_WORLD_READABLE) != 0) {
2151 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2152 }
2153 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2154 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2155 }
2156 }
2157 }
2158
Jeff Brown6e539312015-02-24 18:53:21 -08002159 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002160 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 int extraPermissions) {
2162 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2163 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2164 |extraPermissions;
2165 if ((mode&MODE_WORLD_READABLE) != 0) {
2166 perms |= FileUtils.S_IROTH;
2167 }
2168 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2169 perms |= FileUtils.S_IWOTH;
2170 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002171 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2173 + ", perms=0x" + Integer.toHexString(perms));
2174 }
2175 FileUtils.setPermissions(name, perms, -1, -1);
2176 }
2177
2178 private File makeFilename(File base, String name) {
2179 if (name.indexOf(File.separatorChar) < 0) {
2180 return new File(base, name);
2181 }
2182 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002183 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
2185
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002186 /**
2187 * Ensure that given directories exist, trying to create them if missing. If
2188 * unable to create, they are filtered by replacing with {@code null}.
2189 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002190 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002191 File[] result = new File[dirs.length];
2192 for (int i = 0; i < dirs.length; i++) {
2193 File dir = dirs[i];
2194 if (!dir.exists()) {
2195 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002196 // recheck existence in case of cross-process race
2197 if (!dir.exists()) {
2198 // Failing to mkdir() may be okay, since we might not have
2199 // enough permissions; ask vold to create on our behalf.
2200 final IMountService mount = IMountService.Stub.asInterface(
2201 ServiceManager.getService("mount"));
Christopher Tatecc866da2013-10-02 18:11:01 -07002202 try {
Jeff Sharkey983294592015-07-13 10:25:31 -07002203 final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
2204 if (res != 0) {
2205 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2206 dir = null;
2207 }
2208 } catch (Exception e) {
2209 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002210 dir = null;
2211 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002212 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002213 }
2214 }
2215 result[i] = dir;
2216 }
2217 return result;
2218 }
2219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 // ----------------------------------------------------------------------
2221 // ----------------------------------------------------------------------
2222 // ----------------------------------------------------------------------
2223
2224 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002225 private final ActivityThread mMainThread;
2226 private final UserHandle mUser;
2227
2228 public ApplicationContentResolver(
2229 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002231 mMainThread = Preconditions.checkNotNull(mainThread);
2232 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
2234
2235 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002236 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002237 return mMainThread.acquireProvider(context,
2238 ContentProvider.getAuthorityWithoutUserId(auth),
2239 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 }
2241
2242 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002243 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002244 return mMainThread.acquireExistingProvider(context,
2245 ContentProvider.getAuthorityWithoutUserId(auth),
2246 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002247 }
2248
2249 @Override
2250 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002251 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002253
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002254 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002255 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002256 return mMainThread.acquireProvider(c,
2257 ContentProvider.getAuthorityWithoutUserId(auth),
2258 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002259 }
2260
2261 @Override
2262 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002263 return mMainThread.releaseProvider(icp, false);
2264 }
2265
2266 @Override
2267 public void unstableProviderDied(IContentProvider icp) {
2268 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002269 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002270
2271 @Override
2272 public void appNotRespondingViaProvider(IContentProvider icp) {
2273 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2274 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002275
2276 /** @hide */
2277 protected int resolveUserIdFromAuthority(String auth) {
2278 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281}