blob: 8f424676dbe505e0dd4765fbf20bc999a8c3926e [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
1056 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1057 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001058 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001059 String[] receiverPermissions = receiverPermission == null ? null
1060 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001061 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001062 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001063 ActivityManagerNative.getDefault().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001064 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001065 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001066 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001067 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001068 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001069 }
1070 }
1071
1072 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001073 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001074 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001075 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001076 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001077 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001078 }
1079
1080 @Override
1081 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1082 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001083 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1084 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1085 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1086 }
1087
1088 @Override
1089 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1090 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1091 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001092 IIntentReceiver rd = null;
1093 if (resultReceiver != null) {
1094 if (mPackageInfo != null) {
1095 if (scheduler == null) {
1096 scheduler = mMainThread.getHandler();
1097 }
1098 rd = mPackageInfo.getReceiverDispatcher(
1099 resultReceiver, getOuterContext(), scheduler,
1100 mMainThread.getInstrumentation(), false);
1101 } else {
1102 if (scheduler == null) {
1103 scheduler = mMainThread.getHandler();
1104 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001105 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1106 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001107 }
1108 }
1109 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001110 String[] receiverPermissions = receiverPermission == null ? null
1111 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001112 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001113 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001114 ActivityManagerNative.getDefault().broadcastIntent(
1115 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001116 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001117 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001118 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001119 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001120 }
1121 }
1122
1123 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001124 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001126 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1128 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001129 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 ActivityManagerNative.getDefault().broadcastIntent(
1131 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001132 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001133 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001135 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
1137 }
1138
1139 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001140 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001141 public void sendStickyOrderedBroadcast(Intent intent,
1142 BroadcastReceiver resultReceiver,
1143 Handler scheduler, int initialCode, String initialData,
1144 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001145 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001146 IIntentReceiver rd = null;
1147 if (resultReceiver != null) {
1148 if (mPackageInfo != null) {
1149 if (scheduler == null) {
1150 scheduler = mMainThread.getHandler();
1151 }
1152 rd = mPackageInfo.getReceiverDispatcher(
1153 resultReceiver, getOuterContext(), scheduler,
1154 mMainThread.getInstrumentation(), false);
1155 } else {
1156 if (scheduler == null) {
1157 scheduler = mMainThread.getHandler();
1158 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001159 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001160 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1161 }
1162 }
1163 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1164 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001165 intent.prepareToLeaveProcess(this);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001166 ActivityManagerNative.getDefault().broadcastIntent(
1167 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1168 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001169 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001170 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001171 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001172 }
1173 }
1174
1175 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001176 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 public void removeStickyBroadcast(Intent intent) {
1178 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1179 if (resolvedType != null) {
1180 intent = new Intent(intent);
1181 intent.setDataAndType(intent.getData(), resolvedType);
1182 }
1183 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001184 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001186 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001188 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 }
1191
1192 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001193 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001194 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1195 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1196 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001197 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001198 ActivityManagerNative.getDefault().broadcastIntent(
1199 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001200 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1201 user.getIdentifier());
1202 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001203 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001204 }
1205 }
1206
1207 @Override
1208 @Deprecated
1209 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1210 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1211 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001212 intent.prepareToLeaveProcess(this);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001213 ActivityManagerNative.getDefault().broadcastIntent(
1214 mMainThread.getApplicationThread(), intent, resolvedType, null,
1215 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1216 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001217 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001218 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001219 }
1220 }
1221
1222 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001223 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001224 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1225 UserHandle user, BroadcastReceiver resultReceiver,
1226 Handler scheduler, int initialCode, String initialData,
1227 Bundle initialExtras) {
1228 IIntentReceiver rd = null;
1229 if (resultReceiver != null) {
1230 if (mPackageInfo != null) {
1231 if (scheduler == null) {
1232 scheduler = mMainThread.getHandler();
1233 }
1234 rd = mPackageInfo.getReceiverDispatcher(
1235 resultReceiver, getOuterContext(), scheduler,
1236 mMainThread.getInstrumentation(), false);
1237 } else {
1238 if (scheduler == null) {
1239 scheduler = mMainThread.getHandler();
1240 }
1241 rd = new LoadedApk.ReceiverDispatcher(
1242 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1243 }
1244 }
1245 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1246 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001247 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001248 ActivityManagerNative.getDefault().broadcastIntent(
1249 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1250 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001251 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001252 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001253 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001254 }
1255 }
1256
1257 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001258 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001259 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1260 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1261 if (resolvedType != null) {
1262 intent = new Intent(intent);
1263 intent.setDataAndType(intent.getData(), resolvedType);
1264 }
1265 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001266 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001267 ActivityManagerNative.getDefault().unbroadcastIntent(
1268 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1269 } 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1276 return registerReceiver(receiver, filter, null, null);
1277 }
1278
1279 @Override
1280 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1281 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001282 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001283 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285
Dianne Hackborn20e80982012-08-31 19:00:44 -07001286 @Override
1287 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1288 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1289 return registerReceiverInternal(receiver, user.getIdentifier(),
1290 filter, broadcastPermission, scheduler, getOuterContext());
1291 }
1292
1293 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 IntentFilter filter, String broadcastPermission,
1295 Handler scheduler, Context context) {
1296 IIntentReceiver rd = null;
1297 if (receiver != null) {
1298 if (mPackageInfo != null && context != null) {
1299 if (scheduler == null) {
1300 scheduler = mMainThread.getHandler();
1301 }
1302 rd = mPackageInfo.getReceiverDispatcher(
1303 receiver, context, scheduler,
1304 mMainThread.getInstrumentation(), true);
1305 } else {
1306 if (scheduler == null) {
1307 scheduler = mMainThread.getHandler();
1308 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001309 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001310 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312 }
1313 try {
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001314 final Intent intent = ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001315 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001316 rd, filter, broadcastPermission, userId);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001317 if (intent != null) {
1318 intent.setExtrasClassLoader(getClassLoader());
1319 intent.prepareToEnterProcess();
1320 }
1321 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001323 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
1325 }
1326
1327 @Override
1328 public void unregisterReceiver(BroadcastReceiver receiver) {
1329 if (mPackageInfo != null) {
1330 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1331 getOuterContext(), receiver);
1332 try {
1333 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1334 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001335 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
1337 } else {
1338 throw new RuntimeException("Not supported in system context");
1339 }
1340 }
1341
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001342 private void validateServiceIntent(Intent service) {
1343 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001344 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001345 IllegalArgumentException ex = new IllegalArgumentException(
1346 "Service Intent must be explicit: " + service);
1347 throw ex;
1348 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001349 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1350 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001351 }
1352 }
1353 }
1354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 @Override
1356 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001357 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001358 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001359 }
1360
1361 @Override
1362 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001363 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001364 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001365 }
1366
1367 @Override
1368 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001369 return startServiceCommon(service, user);
1370 }
1371
1372 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001374 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001375 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 ComponentName cn = ActivityManagerNative.getDefault().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001377 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
1378 getContentResolver()), getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001379 if (cn != null) {
1380 if (cn.getPackageName().equals("!")) {
1381 throw new SecurityException(
1382 "Not allowed to start service " + service
1383 + " without permission " + cn.getClassName());
1384 } else if (cn.getPackageName().equals("!!")) {
1385 throw new SecurityException(
1386 "Unable to start service " + service
1387 + ": " + cn.getClassName());
1388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
1390 return cn;
1391 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001392 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394 }
1395
1396 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001397 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001398 return stopServiceCommon(service, user);
1399 }
1400
1401 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001403 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001404 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 int res = ActivityManagerNative.getDefault().stopService(
1406 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001407 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 if (res < 0) {
1409 throw new SecurityException(
1410 "Not allowed to stop service " + service);
1411 }
1412 return res != 0;
1413 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001414 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 }
1417
1418 @Override
1419 public boolean bindService(Intent service, ServiceConnection conn,
1420 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001421 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001422 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1423 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001424 }
1425
1426 /** @hide */
1427 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001428 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1429 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001430 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001431 }
1432
Adrian Roos691546e2016-02-09 10:13:41 -08001433 /** @hide */
1434 @Override
1435 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1436 Handler handler, UserHandle user) {
1437 if (handler == null) {
1438 throw new IllegalArgumentException("handler must not be null.");
1439 }
1440 return bindServiceCommon(service, conn, flags, handler, user);
1441 }
1442
1443 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1444 handler, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001446 if (conn == null) {
1447 throw new IllegalArgumentException("connection is null");
1448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 if (mPackageInfo != null) {
Adrian Roos691546e2016-02-09 10:13:41 -08001450 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 } else {
1452 throw new RuntimeException("Not supported in system context");
1453 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001454 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001456 IBinder token = getActivityToken();
1457 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1458 && mPackageInfo.getApplicationInfo().targetSdkVersion
1459 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1460 flags |= BIND_WAIVE_PRIORITY;
1461 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001462 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 int res = ActivityManagerNative.getDefault().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001464 mMainThread.getApplicationThread(), getActivityToken(), service,
1465 service.resolveTypeIfNeeded(getContentResolver()),
1466 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 if (res < 0) {
1468 throw new SecurityException(
1469 "Not allowed to bind to service " + service);
1470 }
1471 return res != 0;
1472 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001473 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 }
1475 }
1476
1477 @Override
1478 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001479 if (conn == null) {
1480 throw new IllegalArgumentException("connection is null");
1481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (mPackageInfo != null) {
1483 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1484 getOuterContext(), conn);
1485 try {
1486 ActivityManagerNative.getDefault().unbindService(sd);
1487 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001488 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
1490 } else {
1491 throw new RuntimeException("Not supported in system context");
1492 }
1493 }
1494
1495 @Override
1496 public boolean startInstrumentation(ComponentName className,
1497 String profileFile, Bundle arguments) {
1498 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001499 if (arguments != null) {
1500 arguments.setAllowFds(false);
1501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001503 className, profileFile, 0, arguments, null, null, getUserId(),
1504 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001506 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509
1510 @Override
1511 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001512 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
1514
Jeff Brown6e539312015-02-24 18:53:21 -08001515 @Override
1516 public String getSystemServiceName(Class<?> serviceClass) {
1517 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001518 }
1519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 @Override
1521 public int checkPermission(String permission, int pid, int uid) {
1522 if (permission == null) {
1523 throw new IllegalArgumentException("permission is null");
1524 }
1525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 try {
1527 return ActivityManagerNative.getDefault().checkPermission(
1528 permission, pid, uid);
1529 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001530 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
1532 }
1533
Dianne Hackbornff170242014-11-19 10:59:01 -08001534 /** @hide */
1535 @Override
1536 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1537 if (permission == null) {
1538 throw new IllegalArgumentException("permission is null");
1539 }
1540
1541 try {
1542 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1543 permission, pid, uid, callerToken);
1544 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001545 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001546 }
1547 }
1548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 @Override
1550 public int checkCallingPermission(String permission) {
1551 if (permission == null) {
1552 throw new IllegalArgumentException("permission is null");
1553 }
1554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 int pid = Binder.getCallingPid();
1556 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001557 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559 return PackageManager.PERMISSION_DENIED;
1560 }
1561
1562 @Override
1563 public int checkCallingOrSelfPermission(String permission) {
1564 if (permission == null) {
1565 throw new IllegalArgumentException("permission is null");
1566 }
1567
1568 return checkPermission(permission, Binder.getCallingPid(),
1569 Binder.getCallingUid());
1570 }
1571
Svetoslavc6d1c342015-02-26 14:44:43 -08001572 @Override
1573 public int checkSelfPermission(String permission) {
1574 if (permission == null) {
1575 throw new IllegalArgumentException("permission is null");
1576 }
1577
1578 return checkPermission(permission, Process.myPid(), Process.myUid());
1579 }
1580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 private void enforce(
1582 String permission, int resultOfCheck,
1583 boolean selfToo, int uid, String message) {
1584 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1585 throw new SecurityException(
1586 (message != null ? (message + ": ") : "") +
1587 (selfToo
1588 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001589 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 permission +
1591 ".");
1592 }
1593 }
1594
Jeff Brown6e539312015-02-24 18:53:21 -08001595 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 public void enforcePermission(
1597 String permission, int pid, int uid, String message) {
1598 enforce(permission,
1599 checkPermission(permission, pid, uid),
1600 false,
1601 uid,
1602 message);
1603 }
1604
Jeff Brown6e539312015-02-24 18:53:21 -08001605 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 public void enforceCallingPermission(String permission, String message) {
1607 enforce(permission,
1608 checkCallingPermission(permission),
1609 false,
1610 Binder.getCallingUid(),
1611 message);
1612 }
1613
Jeff Brown6e539312015-02-24 18:53:21 -08001614 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 public void enforceCallingOrSelfPermission(
1616 String permission, String message) {
1617 enforce(permission,
1618 checkCallingOrSelfPermission(permission),
1619 true,
1620 Binder.getCallingUid(),
1621 message);
1622 }
1623
1624 @Override
1625 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1626 try {
1627 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001628 mMainThread.getApplicationThread(), toPackage,
1629 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001631 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633 }
1634
1635 @Override
1636 public void revokeUriPermission(Uri uri, int modeFlags) {
1637 try {
1638 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001639 mMainThread.getApplicationThread(),
1640 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001642 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 }
1644 }
1645
1646 @Override
1647 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 try {
1649 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001650 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001651 resolveUserId(uri), null);
1652 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001653 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001654 }
1655 }
1656
1657 /** @hide */
1658 @Override
1659 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1660 try {
1661 return ActivityManagerNative.getDefault().checkUriPermission(
1662 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1663 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001665 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
1667 }
1668
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001669 private int resolveUserId(Uri uri) {
1670 return ContentProvider.getUserIdFromUri(uri, getUserId());
1671 }
1672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 @Override
1674 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 int pid = Binder.getCallingPid();
1676 if (pid != Process.myPid()) {
1677 return checkUriPermission(uri, pid,
1678 Binder.getCallingUid(), modeFlags);
1679 }
1680 return PackageManager.PERMISSION_DENIED;
1681 }
1682
1683 @Override
1684 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1685 return checkUriPermission(uri, Binder.getCallingPid(),
1686 Binder.getCallingUid(), modeFlags);
1687 }
1688
1689 @Override
1690 public int checkUriPermission(Uri uri, String readPermission,
1691 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001692 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1694 + readPermission + " writePermission=" + writePermission
1695 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1696 }
1697 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1698 if (readPermission == null
1699 || checkPermission(readPermission, pid, uid)
1700 == PackageManager.PERMISSION_GRANTED) {
1701 return PackageManager.PERMISSION_GRANTED;
1702 }
1703 }
1704 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1705 if (writePermission == null
1706 || checkPermission(writePermission, pid, uid)
1707 == PackageManager.PERMISSION_GRANTED) {
1708 return PackageManager.PERMISSION_GRANTED;
1709 }
1710 }
1711 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1712 : PackageManager.PERMISSION_DENIED;
1713 }
1714
1715 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001716 StringBuilder builder = new StringBuilder();
1717 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1718 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001720 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1721 builder.append("write and ");
1722 }
1723 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1724 builder.append("persistable and ");
1725 }
1726 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1727 builder.append("prefix and ");
1728 }
1729
1730 if (builder.length() > 5) {
1731 builder.setLength(builder.length() - 5);
1732 return builder.toString();
1733 } else {
1734 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
1737
1738 private void enforceForUri(
1739 int modeFlags, int resultOfCheck, boolean selfToo,
1740 int uid, Uri uri, String message) {
1741 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1742 throw new SecurityException(
1743 (message != null ? (message + ": ") : "") +
1744 (selfToo
1745 ? "Neither user " + uid + " nor current process has "
1746 : "User " + uid + " does not have ") +
1747 uriModeFlagToString(modeFlags) +
1748 " permission on " +
1749 uri +
1750 ".");
1751 }
1752 }
1753
Jeff Brown6e539312015-02-24 18:53:21 -08001754 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 public void enforceUriPermission(
1756 Uri uri, int pid, int uid, int modeFlags, String message) {
1757 enforceForUri(
1758 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1759 false, uid, uri, message);
1760 }
1761
Jeff Brown6e539312015-02-24 18:53:21 -08001762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 public void enforceCallingUriPermission(
1764 Uri uri, int modeFlags, String message) {
1765 enforceForUri(
1766 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001767 false,
1768 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770
Jeff Brown6e539312015-02-24 18:53:21 -08001771 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 public void enforceCallingOrSelfUriPermission(
1773 Uri uri, int modeFlags, String message) {
1774 enforceForUri(
1775 modeFlags,
1776 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1777 Binder.getCallingUid(), uri, message);
1778 }
1779
Jeff Brown6e539312015-02-24 18:53:21 -08001780 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 public void enforceUriPermission(
1782 Uri uri, String readPermission, String writePermission,
1783 int pid, int uid, int modeFlags, String message) {
1784 enforceForUri(modeFlags,
1785 checkUriPermission(
1786 uri, readPermission, writePermission, pid, uid,
1787 modeFlags),
1788 false,
1789 uid,
1790 uri,
1791 message);
1792 }
1793
Tom O'Neill365632e2013-09-09 09:34:58 -07001794 /**
1795 * Logs a warning if the system process directly called a method such as
1796 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1797 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1798 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001799 private void warnIfCallingFromSystemProcess() {
1800 if (Process.myUid() == Process.SYSTEM_UID) {
1801 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001802 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001803 }
1804 }
1805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001807 public Context createApplicationContext(ApplicationInfo application, int flags)
1808 throws NameNotFoundException {
1809 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1810 flags | CONTEXT_REGISTER_PACKAGE);
1811 if (pi != null) {
Svetoslav976e8bd2014-07-16 15:12:03 -07001812 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001813 new UserHandle(UserHandle.getUserId(application.uid)), flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001814 mDisplay, null, Display.INVALID_DISPLAY);
Svetoslav976e8bd2014-07-16 15:12:03 -07001815 if (c.mResources != null) {
1816 return c;
1817 }
1818 }
1819
1820 throw new PackageManager.NameNotFoundException(
1821 "Application package " + application.packageName + " not found");
1822 }
1823
1824 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001826 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001827 return createPackageContextAsUser(packageName, flags,
1828 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001829 }
1830
1831 @Override
1832 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1833 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00001834 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001835 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001836 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
1838
Jeff Browndefd4a62014-03-10 21:24:37 -07001839 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001840 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 if (pi != null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001842 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001843 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 if (c.mResources != null) {
1845 return c;
1846 }
1847 }
1848
1849 // Should be a better exception.
1850 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001851 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853
Romain Guy870e09f2009-07-06 16:35:25 -07001854 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001855 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001856 if (overrideConfiguration == null) {
1857 throw new IllegalArgumentException("overrideConfiguration must not be null");
1858 }
1859
Jeff Browndefd4a62014-03-10 21:24:37 -07001860 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001861 mUser, mFlags, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001862 }
1863
1864 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001865 public Context createDisplayContext(Display display) {
1866 if (display == null) {
1867 throw new IllegalArgumentException("display must not be null");
1868 }
1869
Jeff Browndefd4a62014-03-10 21:24:37 -07001870 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001871 mUser, mFlags, display, null, Display.INVALID_DISPLAY);
Jeff Browna492c3a2012-08-23 19:48:44 -07001872 }
1873
Jeff Browna492c3a2012-08-23 19:48:44 -07001874 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001875 public Context createDeviceProtectedStorageContext() {
1876 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
1877 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001878 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1879 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1880 }
1881
1882 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001883 public Context createCredentialProtectedStorageContext() {
1884 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
1885 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001886 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1887 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1888 }
1889
1890 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001891 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001892 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
1893 }
1894
1895 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001896 public boolean isDeviceProtectedStorage() {
1897 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001898 }
1899
1900 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001901 public boolean isCredentialProtectedStorage() {
1902 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07001903 }
1904
Jeff Brown98365d72012-08-19 20:30:52 -07001905 @Override
Adam Lesinski4ece3d62016-06-16 18:05:41 -07001906 public Display getDisplay() {
1907 final DisplayAdjustments displayAdjustments = mResources.getDisplayAdjustments();
1908 if (mDisplay == null) {
1909 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
1910 displayAdjustments);
1911 }
1912
1913 if (!mDisplay.getDisplayAdjustments().equals(displayAdjustments)) {
1914 mDisplay = mResourcesManager.getAdjustedDisplay(mDisplay.getDisplayId(),
1915 displayAdjustments);
1916 }
1917 return mDisplay;
1918 }
1919
1920 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001921 public DisplayAdjustments getDisplayAdjustments(int displayId) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07001922 return mResources.getDisplayAdjustments();
Jeff Brown98365d72012-08-19 20:30:52 -07001923 }
1924
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001925 @Override
1926 public File getDataDir() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001928 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001929 if (isCredentialProtectedStorage()) {
1930 res = mPackageInfo.getCredentialProtectedDataDirFile();
1931 } else if (isDeviceProtectedStorage()) {
1932 res = mPackageInfo.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001933 } else {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001934 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001935 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001936
1937 if (res != null) {
Jeff Sharkey21f50722016-04-27 12:38:02 -06001938 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Jeff Sharkey24492ae2016-04-25 13:20:25 -06001939 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
1940 new Throwable());
1941 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001942 return res;
1943 } else {
1944 throw new RuntimeException(
1945 "No data directory found for package " + getPackageName());
1946 }
1947 } else {
1948 throw new RuntimeException(
1949 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952
1953 @Override
1954 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07001955 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001957 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 if (!file.exists()) {
1959 file.mkdir();
1960 setFilePermissionsFromMode(file.getPath(), mode,
1961 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1962 }
1963 return file;
1964 }
1965
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001966 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08001967 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001968 public int getUserId() {
1969 return mUser.getIdentifier();
1970 }
1971
Dianne Hackborn21556372010-02-04 16:34:40 -08001972 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001973 LoadedApk packageInfo = new LoadedApk(mainThread);
1974 ContextImpl context = new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001975 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001976 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07001977 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 return context;
1979 }
1980
Jeff Browndefd4a62014-03-10 21:24:37 -07001981 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
1982 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
1983 return new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001984 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001985 }
1986
1987 static ContextImpl createActivityContext(ActivityThread mainThread,
Adam Lesinski082614c2016-03-04 14:33:47 -08001988 LoadedApk packageInfo, IBinder activityToken, int displayId,
1989 Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001990 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski082614c2016-03-04 14:33:47 -08001991 return new ContextImpl(null, mainThread, packageInfo, activityToken, null, 0,
Wale Ogunwale26698512015-06-05 16:55:33 -07001992 null, overrideConfiguration, displayId);
Jeff Browndefd4a62014-03-10 21:24:37 -07001993 }
1994
1995 private ContextImpl(ContextImpl container, ActivityThread mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001996 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001997 Display display, Configuration overrideConfiguration, int createDisplayWithId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002000 // If creator didn't specify which storage to use, use the default
2001 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002002 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2003 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002004 final File dataDir = packageInfo.getDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002005 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
2006 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
2007 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
2008 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002009 }
2010 }
2011
Jeff Browndefd4a62014-03-10 21:24:37 -07002012 mMainThread = mainThread;
2013 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002014 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015
Jeff Browndefd4a62014-03-10 21:24:37 -07002016 if (user == null) {
2017 user = Process.myUserHandle();
2018 }
2019 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07002022 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002023
Wale Ogunwale26698512015-06-05 16:55:33 -07002024 final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
Wale Ogunwalecac3dc62015-06-09 15:35:02 -07002025 ? createDisplayWithId
2026 : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
Wale Ogunwale26698512015-06-05 16:55:33 -07002027
Jeff Browndefd4a62014-03-10 21:24:37 -07002028 CompatibilityInfo compatInfo = null;
2029 if (container != null) {
2030 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
2031 }
Wale Ogunwale9cf99542015-02-18 16:31:34 -08002032 if (compatInfo == null) {
2033 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2034 ? packageInfo.getCompatibilityInfo()
2035 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Jeff Browndefd4a62014-03-10 21:24:37 -07002036 }
Wale Ogunwale26698512015-06-05 16:55:33 -07002037
Jeff Browndefd4a62014-03-10 21:24:37 -07002038 Resources resources = packageInfo.getResources(mainThread);
2039 if (resources != null) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002040 if (displayId != Display.DEFAULT_DISPLAY
Jeff Browndefd4a62014-03-10 21:24:37 -07002041 || overrideConfiguration != null
2042 || (compatInfo != null && compatInfo.applicationScale
2043 != resources.getCompatibilityInfo().applicationScale)) {
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002044
2045 if (container != null) {
2046 // This is a nested Context, so it can't be a base Activity context.
2047 // Just create a regular Resources object associated with the Activity.
2048 resources = mResourcesManager.getResources(
2049 activityToken,
2050 packageInfo.getResDir(),
2051 packageInfo.getSplitResDirs(),
2052 packageInfo.getOverlayDirs(),
2053 packageInfo.getApplicationInfo().sharedLibraryFiles,
2054 displayId,
2055 overrideConfiguration,
2056 compatInfo,
2057 packageInfo.getClassLoader());
2058 } else {
2059 // This is not a nested Context, so it must be the root Activity context.
2060 // All other nested Contexts will inherit the configuration set here.
2061 resources = mResourcesManager.createBaseActivityResources(
2062 activityToken,
2063 packageInfo.getResDir(),
2064 packageInfo.getSplitResDirs(),
2065 packageInfo.getOverlayDirs(),
2066 packageInfo.getApplicationInfo().sharedLibraryFiles,
2067 displayId,
2068 overrideConfiguration,
2069 compatInfo,
2070 packageInfo.getClassLoader());
2071 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002072 }
2073 }
2074 mResources = resources;
2075
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002076 mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
2077 : mResourcesManager.getAdjustedDisplay(displayId, mResources.getDisplayAdjustments());
2078
Jeff Browndefd4a62014-03-10 21:24:37 -07002079 if (container != null) {
2080 mBasePackageName = container.mBasePackageName;
2081 mOpPackageName = container.mOpPackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002082 } else {
2083 mBasePackageName = packageInfo.mPackageName;
2084 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2085 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2086 // Special case: system components allow themselves to be loaded in to other
2087 // processes. For purposes of app ops, we must then consider the context as
2088 // belonging to the package of this process, not the system itself, otherwise
2089 // the package+uid verifications in app ops will fail.
2090 mOpPackageName = ActivityThread.currentPackageName();
2091 } else {
2092 mOpPackageName = mBasePackageName;
2093 }
2094 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002095
2096 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
2098
Narayan Kamath29564cd2014-08-07 10:57:40 +01002099 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2100 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 }
2102
2103 final void scheduleFinalCleanup(String who, String what) {
2104 mMainThread.scheduleContextCleanup(this, who, what);
2105 }
2106
2107 final void performFinalCleanup(String who, String what) {
2108 //Log.i(TAG, "Cleanup up context: " + this);
2109 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2110 }
2111
2112 final Context getReceiverRestrictedContext() {
2113 if (mReceiverRestrictedContext != null) {
2114 return mReceiverRestrictedContext;
2115 }
2116 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2117 }
2118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 final void setOuterContext(Context context) {
2120 mOuterContext = context;
2121 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 final Context getOuterContext() {
2124 return mOuterContext;
2125 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 final IBinder getActivityToken() {
2128 return mActivityToken;
2129 }
2130
Jeff Sharkey634dc422016-01-30 17:44:15 -07002131 private void checkMode(int mode) {
2132 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2133 if ((mode & MODE_WORLD_READABLE) != 0) {
2134 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2135 }
2136 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2137 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2138 }
2139 }
2140 }
2141
Jeff Brown6e539312015-02-24 18:53:21 -08002142 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002143 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 int extraPermissions) {
2145 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2146 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2147 |extraPermissions;
2148 if ((mode&MODE_WORLD_READABLE) != 0) {
2149 perms |= FileUtils.S_IROTH;
2150 }
2151 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2152 perms |= FileUtils.S_IWOTH;
2153 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002154 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2156 + ", perms=0x" + Integer.toHexString(perms));
2157 }
2158 FileUtils.setPermissions(name, perms, -1, -1);
2159 }
2160
2161 private File makeFilename(File base, String name) {
2162 if (name.indexOf(File.separatorChar) < 0) {
2163 return new File(base, name);
2164 }
2165 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002166 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002169 /**
2170 * Ensure that given directories exist, trying to create them if missing. If
2171 * unable to create, they are filtered by replacing with {@code null}.
2172 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002173 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002174 File[] result = new File[dirs.length];
2175 for (int i = 0; i < dirs.length; i++) {
2176 File dir = dirs[i];
2177 if (!dir.exists()) {
2178 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002179 // recheck existence in case of cross-process race
2180 if (!dir.exists()) {
2181 // Failing to mkdir() may be okay, since we might not have
2182 // enough permissions; ask vold to create on our behalf.
2183 final IMountService mount = IMountService.Stub.asInterface(
2184 ServiceManager.getService("mount"));
Christopher Tatecc866da2013-10-02 18:11:01 -07002185 try {
Jeff Sharkey983294592015-07-13 10:25:31 -07002186 final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
2187 if (res != 0) {
2188 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2189 dir = null;
2190 }
2191 } catch (Exception e) {
2192 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002193 dir = null;
2194 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002195 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002196 }
2197 }
2198 result[i] = dir;
2199 }
2200 return result;
2201 }
2202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 // ----------------------------------------------------------------------
2204 // ----------------------------------------------------------------------
2205 // ----------------------------------------------------------------------
2206
2207 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002208 private final ActivityThread mMainThread;
2209 private final UserHandle mUser;
2210
2211 public ApplicationContentResolver(
2212 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002214 mMainThread = Preconditions.checkNotNull(mainThread);
2215 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
2217
2218 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002219 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002220 return mMainThread.acquireProvider(context,
2221 ContentProvider.getAuthorityWithoutUserId(auth),
2222 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
2224
2225 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002226 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002227 return mMainThread.acquireExistingProvider(context,
2228 ContentProvider.getAuthorityWithoutUserId(auth),
2229 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002230 }
2231
2232 @Override
2233 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002234 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002236
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002237 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002238 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002239 return mMainThread.acquireProvider(c,
2240 ContentProvider.getAuthorityWithoutUserId(auth),
2241 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002242 }
2243
2244 @Override
2245 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002246 return mMainThread.releaseProvider(icp, false);
2247 }
2248
2249 @Override
2250 public void unstableProviderDied(IContentProvider icp) {
2251 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002252 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002253
2254 @Override
2255 public void appNotRespondingViaProvider(IContentProvider icp) {
2256 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2257 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002258
2259 /** @hide */
2260 protected int resolveUserIdFromAuthority(String auth) {
2261 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264}