blob: d0700fbef484ae131529d885c07817c631667d7d [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070038import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070040import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070041import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070043import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.database.sqlite.SQLiteDatabase;
45import android.database.sqlite.SQLiteDatabase.CursorFactory;
46import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080050import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070052import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080053import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.FileUtils;
55import android.os.Handler;
56import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070061import android.os.UserHandle;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070062import android.os.storage.IMountService;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -070063import android.system.ErrnoException;
64import android.system.Os;
65import android.system.OsConstants;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070067import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070069import android.util.Slog;
Jeff Brown98365d72012-08-19 20:30:52 -070070import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080071import android.view.DisplayAdjustments;
Dan Egnor95240272009-10-27 18:23:39 -070072
Jeff Sharkey35871f22016-01-29 17:13:29 -070073import com.android.internal.annotations.GuardedBy;
74import com.android.internal.util.Preconditions;
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import java.io.File;
77import java.io.FileInputStream;
78import java.io.FileNotFoundException;
79import java.io.FileOutputStream;
Jeff Sharkey35871f22016-01-29 17:13:29 -070080import java.io.FilenameFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.io.IOException;
82import java.io.InputStream;
Jeff Sharkey7a30a302015-12-08 14:20:06 -070083import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085class ReceiverRestrictedContext extends ContextWrapper {
86 ReceiverRestrictedContext(Context base) {
87 super(base);
88 }
89
90 @Override
91 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
92 return registerReceiver(receiver, filter, null, null);
93 }
94
95 @Override
96 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
97 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070098 if (receiver == null) {
99 // Allow retrieving current sticky broadcast; this is safe since we
100 // aren't actually registering a receiver.
101 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
102 } else {
103 throw new ReceiverCallNotAllowedException(
104 "BroadcastReceiver components are not allowed to register to receive intents");
105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 }
107
108 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700109 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
110 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700111 if (receiver == null) {
112 // Allow retrieving current sticky broadcast; this is safe since we
113 // aren't actually registering a receiver.
114 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
115 } else {
116 throw new ReceiverCallNotAllowedException(
117 "BroadcastReceiver components are not allowed to register to receive intents");
118 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700119 }
120
121 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
123 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700124 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
126}
127
128/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800129 * Common implementation of Context API, which provides the base
130 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800132class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800133 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700134 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700136 /**
137 * Map from package name, to preference name, to cached preferences.
138 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700139 @GuardedBy("ContextImpl.class")
140 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
141
142 /**
143 * Map from preference name to generated path.
144 */
145 @GuardedBy("ContextImpl.class")
146 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
Jeff Browndefd4a62014-03-10 21:24:37 -0700148 final ActivityThread mMainThread;
149 final LoadedApk mPackageInfo;
150
151 private final IBinder mActivityToken;
152
153 private final UserHandle mUser;
154
155 private final ApplicationContentResolver mContentResolver;
156
157 private final String mBasePackageName;
158 private final String mOpPackageName;
159
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700160 private final @NonNull ResourcesManager mResourcesManager;
Adam Lesinski8fa71072016-11-18 18:13:55 -0800161 private @NonNull Resources mResources;
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700162 private @Nullable Display mDisplay; // may be null if default display
Jeff Browndefd4a62014-03-10 21:24:37 -0700163
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700164 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 private int mThemeResource = 0;
168 private Resources.Theme mTheme = null;
169 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
172 private final Object mSync = new Object();
173
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700174 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700176 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700178 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700180 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700181 private File mNoBackupFilesDir;
182 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700184 @GuardedBy("mSync")
185 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700186
Jeff Brown6e539312015-02-24 18:53:21 -0800187 // The system service cache for the system services that are cached per-ContextImpl.
188 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800189
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700190 static ContextImpl getImpl(Context context) {
191 Context nextContext;
192 while ((context instanceof ContextWrapper) &&
193 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
194 context = nextContext;
195 }
196 return (ContextImpl)context;
197 }
198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 @Override
200 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700201 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
203
204 @Override
205 public Resources getResources() {
206 return mResources;
207 }
208
209 @Override
210 public PackageManager getPackageManager() {
211 if (mPackageManager != null) {
212 return mPackageManager;
213 }
214
215 IPackageManager pm = ActivityThread.getPackageManager();
216 if (pm != null) {
217 // Doesn't matter if we make more than one instance.
218 return (mPackageManager = new ApplicationPackageManager(this, pm));
219 }
220
221 return null;
222 }
223
224 @Override
225 public ContentResolver getContentResolver() {
226 return mContentResolver;
227 }
228
229 @Override
230 public Looper getMainLooper() {
231 return mMainThread.getLooper();
232 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 @Override
235 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700236 return (mPackageInfo != null) ?
237 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700241 public void setTheme(int resId) {
242 if (mThemeResource != resId) {
243 mThemeResource = resId;
244 initializeTheme();
245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800249 public int getThemeResId() {
250 return mThemeResource;
251 }
252
253 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 public Resources.Theme getTheme() {
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700255 if (mTheme != null) {
256 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700258
259 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
260 getOuterContext().getApplicationInfo().targetSdkVersion);
261 initializeTheme();
262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 return mTheme;
264 }
265
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700266 private void initializeTheme() {
267 if (mTheme == null) {
268 mTheme = mResources.newTheme();
269 }
270 mTheme.applyStyle(mThemeResource, true);
271 }
272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 @Override
274 public ClassLoader getClassLoader() {
275 return mPackageInfo != null ?
276 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
277 }
278
279 @Override
280 public String getPackageName() {
281 if (mPackageInfo != null) {
282 return mPackageInfo.getPackageName();
283 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800284 // No mPackageInfo means this is a Context for the system itself,
285 // and this here is its name.
286 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800289 /** @hide */
290 @Override
291 public String getBasePackageName() {
292 return mBasePackageName != null ? mBasePackageName : getPackageName();
293 }
294
Dianne Hackborn95d78532013-09-11 09:51:14 -0700295 /** @hide */
296 @Override
297 public String getOpPackageName() {
298 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
299 }
300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700302 public ApplicationInfo getApplicationInfo() {
303 if (mPackageInfo != null) {
304 return mPackageInfo.getApplicationInfo();
305 }
306 throw new RuntimeException("Not supported in system context");
307 }
308
309 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 public String getPackageResourcePath() {
311 if (mPackageInfo != null) {
312 return mPackageInfo.getResDir();
313 }
314 throw new RuntimeException("Not supported in system context");
315 }
316
317 @Override
318 public String getPackageCodePath() {
319 if (mPackageInfo != null) {
320 return mPackageInfo.getAppDir();
321 }
322 throw new RuntimeException("Not supported in system context");
323 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200324
Jeff Brown6e539312015-02-24 18:53:21 -0800325 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700327 // At least one application in the world actually passes in a null
328 // name. This happened to work because when we generated the file name
329 // we would stringify it to "null.xml". Nice.
330 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
331 Build.VERSION_CODES.KITKAT) {
332 if (name == null) {
333 name = "null";
334 }
335 }
336
Jeff Sharkeybe782582016-02-15 18:35:57 -0700337 File file;
338 synchronized (ContextImpl.class) {
339 if (mSharedPrefsPaths == null) {
340 mSharedPrefsPaths = new ArrayMap<>();
341 }
342 file = mSharedPrefsPaths.get(name);
343 if (file == null) {
344 file = getSharedPreferencesPath(name);
345 mSharedPrefsPaths.put(name, file);
346 }
347 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700348 return getSharedPreferences(file, mode);
349 }
350
351 @Override
352 public SharedPreferences getSharedPreferences(File file, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700353 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700355 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700356 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
357 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700358 if (sp == null) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700359 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700360 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700361 return sp;
362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800364 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
365 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
366 // If somebody else (some other process) changed the prefs
367 // file behind our back, we reload it. This has been the
368 // historical (if undocumented) behavior.
369 sp.startReloadIfChangedUnexpectedly();
370 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700371 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373
Jeff Sharkey35871f22016-01-29 17:13:29 -0700374 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700375 if (sSharedPrefsCache == null) {
376 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700377 }
378
379 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700380 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700381 if (packagePrefs == null) {
382 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700383 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700384 }
385
386 return packagePrefs;
387 }
388
389 /**
390 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700391 * requested prefix.
392 *
393 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700394 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600395 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700396 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
397 @Override
398 public boolean accept(File dir, String name) {
399 return name.startsWith(prefix);
400 }
401 });
402
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700403 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700404 for (File sourceFile : sourceFiles) {
405 final File targetFile = new File(targetDir, sourceFile.getName());
406 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
407 try {
408 FileUtils.copyFileOrThrow(sourceFile, targetFile);
409 FileUtils.copyPermissions(sourceFile, targetFile);
410 if (!sourceFile.delete()) {
411 throw new IOException("Failed to clean up " + sourceFile);
412 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700413 if (res != -1) {
414 res++;
415 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700416 } catch (IOException e) {
417 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700418 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700419 }
420 }
421 return res;
422 }
423
424 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600425 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700426 synchronized (ContextImpl.class) {
427 final File source = sourceContext.getSharedPreferencesPath(name);
428 final File target = getSharedPreferencesPath(name);
429
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600430 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700431 source.getName());
432 if (res > 0) {
433 // We moved at least one file, so evict any in-memory caches for
434 // either location
435 final ArrayMap<File, SharedPreferencesImpl> cache =
436 getSharedPreferencesCacheLocked();
437 cache.remove(source);
438 cache.remove(target);
439 }
440 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700441 }
442 }
443
444 @Override
445 public boolean deleteSharedPreferences(String name) {
446 synchronized (ContextImpl.class) {
447 final File prefs = getSharedPreferencesPath(name);
448 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
449
450 // Evict any in-memory caches
451 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
452 cache.remove(prefs);
453
454 prefs.delete();
455 prefsBackup.delete();
456
457 // We failed if files are still lingering
458 return !(prefs.exists() || prefsBackup.exists());
459 }
460 }
461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 private File getPreferencesDir() {
463 synchronized (mSync) {
464 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700465 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700467 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469 }
470
471 @Override
472 public FileInputStream openFileInput(String name)
473 throws FileNotFoundException {
474 File f = makeFilename(getFilesDir(), name);
475 return new FileInputStream(f);
476 }
477
478 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700479 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
480 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 final boolean append = (mode&MODE_APPEND) != 0;
482 File f = makeFilename(getFilesDir(), name);
483 try {
484 FileOutputStream fos = new FileOutputStream(f, append);
485 setFilePermissionsFromMode(f.getPath(), mode, 0);
486 return fos;
487 } catch (FileNotFoundException e) {
488 }
489
490 File parent = f.getParentFile();
491 parent.mkdir();
492 FileUtils.setPermissions(
493 parent.getPath(),
494 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
495 -1, -1);
496 FileOutputStream fos = new FileOutputStream(f, append);
497 setFilePermissionsFromMode(f.getPath(), mode, 0);
498 return fos;
499 }
500
501 @Override
502 public boolean deleteFile(String name) {
503 File f = makeFilename(getFilesDir(), name);
504 return f.delete();
505 }
506
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700507 /**
508 * Common-path handling of app data dir creation
509 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700510 private static File ensurePrivateDirExists(File file) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700511 if (!file.exists()) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700512 try {
513 Os.mkdir(file.getAbsolutePath(), 0771);
Jeff Sharkey46ed6f42016-02-15 14:16:08 -0700514 Os.chmod(file.getAbsolutePath(), 0771);
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700515 } catch (ErrnoException e) {
516 if (e.errno == OsConstants.EEXIST) {
517 // We must have raced with someone; that's okay
518 } else {
519 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700520 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700521 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700522 }
523 return file;
524 }
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 @Override
527 public File getFilesDir() {
528 synchronized (mSync) {
529 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700530 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700532 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700533 }
534 }
535
536 @Override
537 public File getNoBackupFilesDir() {
538 synchronized (mSync) {
539 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700540 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700542 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800547 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700548 // Operates on primary external storage
549 return getExternalFilesDirs(type)[0];
550 }
551
552 @Override
553 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800554 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600555 File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700556 if (type != null) {
557 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800558 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700559 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800560 }
561 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200562
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800563 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800564 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700565 // Operates on primary external storage
566 return getObbDirs()[0];
567 }
568
569 @Override
570 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800571 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600572 File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
573 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800574 }
575 }
576
577 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 public File getCacheDir() {
579 synchronized (mSync) {
580 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700581 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700583 return ensurePrivateDirExists(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200586
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800587 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700588 public File getCodeCacheDir() {
589 synchronized (mSync) {
590 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700591 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700592 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700593 return ensurePrivateDirExists(mCodeCacheDir);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700594 }
595 }
596
597 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800598 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700599 // Operates on primary external storage
600 return getExternalCacheDirs()[0];
601 }
602
603 @Override
604 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800605 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600606 File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
607 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800608 }
609 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700612 public File[] getExternalMediaDirs() {
613 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600614 File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
615 return ensureExternalDirsExistOrFilter(dirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700616 }
617 }
618
619 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 public File getFileStreamPath(String name) {
621 return makeFilename(getFilesDir(), name);
622 }
623
624 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700625 public File getSharedPreferencesPath(String name) {
626 return makeFilename(getPreferencesDir(), name + ".xml");
627 }
628
629 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700631 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633
634 @Override
635 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700636 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638
639 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700640 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
641 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700642 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700643 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700644 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
645 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
646 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
647 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800648 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
649 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
650 }
Jeff Brown47847f32012-03-22 19:13:11 -0700651 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700652 setFilePermissionsFromMode(f.getPath(), mode, 0);
653 return db;
654 }
655
656 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600657 public boolean moveDatabaseFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700658 synchronized (ContextImpl.class) {
659 final File source = sourceContext.getDatabasePath(name);
660 final File target = getDatabasePath(name);
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600661 return moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700662 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700663 }
664 }
665
666 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 public boolean deleteDatabase(String name) {
668 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700669 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800670 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 } catch (Exception e) {
672 }
673 return false;
674 }
675
676 @Override
677 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700678 File dir;
679 File f;
680
681 if (name.charAt(0) == File.separatorChar) {
682 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
683 dir = new File(dirPath);
684 name = name.substring(name.lastIndexOf(File.separatorChar));
685 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700686
687 if (!dir.isDirectory() && dir.mkdir()) {
688 FileUtils.setPermissions(dir.getPath(),
689 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
690 -1, -1);
691 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700692 } else {
693 dir = getDatabasesDir();
694 f = makeFilename(dir, name);
695 }
696
Jeff Sharkey35871f22016-01-29 17:13:29 -0700697 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699
700 @Override
701 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700702 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 private File getDatabasesDir() {
706 synchronized (mSync) {
707 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700708 if ("android".equals(getPackageName())) {
709 mDatabasesDir = new File("/data/system");
710 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700711 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700714 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800719 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700721 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723
724 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800725 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700726 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700727 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729
730 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800731 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700733 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
735
736 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800737 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700739 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
741
742 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800743 @Deprecated
744 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700745 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747
748 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800749 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700751 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753
754 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800755 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700757 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
Jeff Brown6e539312015-02-24 18:53:21 -0800760 private WallpaperManager getWallpaperManager() {
761 return getSystemService(WallpaperManager.class);
762 }
763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 @Override
765 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700766 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700767 startActivity(intent, null);
768 }
769
Amith Yamasani82644082012-08-03 13:09:11 -0700770 /** @hide */
771 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700772 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700773 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700774 }
775
Dianne Hackborna4972e92012-03-14 10:38:05 -0700776 @Override
777 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700778 warnIfCallingFromSystemProcess();
Jorim Jaggi2adba072016-03-03 13:43:39 +0100779
780 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
781 // generally not allowed, except if the caller specifies the task id the activity should
782 // be launched in.
783 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
784 && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 throw new AndroidRuntimeException(
786 "Calling startActivity() from outside of an Activity "
787 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
788 + " Is this really what you want?");
789 }
790 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700791 getOuterContext(), mMainThread.getApplicationThread(), null,
792 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
794
Amith Yamasani258848d2012-08-10 17:06:33 -0700795 /** @hide */
796 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700797 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700798 try {
799 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800800 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700801 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700802 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700803 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700804 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700805 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700806 }
807 }
808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800810 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700811 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700812 startActivities(intents, null);
813 }
814
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700815 /** @hide */
816 @Override
817 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
818 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
819 throw new AndroidRuntimeException(
820 "Calling startActivities() from outside of an Activity "
821 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
822 + " Is this really what you want?");
823 }
824 mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700825 getOuterContext(), mMainThread.getApplicationThread(), null,
826 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700827 }
828
Dianne Hackborna4972e92012-03-14 10:38:05 -0700829 @Override
830 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700831 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800832 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
833 throw new AndroidRuntimeException(
834 "Calling startActivities() from outside of an Activity "
835 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
836 + " Is this really what you want?");
837 }
838 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700839 getOuterContext(), mMainThread.getApplicationThread(), null,
840 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800841 }
842
843 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700844 public void startIntentSender(IntentSender intent,
845 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
846 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700847 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
848 }
849
850 @Override
851 public void startIntentSender(IntentSender intent, Intent fillInIntent,
852 int flagsMask, int flagsValues, int extraFlags, Bundle options)
853 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700854 try {
855 String resolvedType = null;
856 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700857 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -0700858 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700859 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
860 }
861 int result = ActivityManagerNative.getDefault()
862 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
863 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700864 0, flagsMask, flagsValues, options);
865 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700866 throw new IntentSender.SendIntentException();
867 }
868 Instrumentation.checkStartActivityResult(result, null);
869 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700870 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700871 }
872 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200873
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700874 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700876 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
878 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700879 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700881 mMainThread.getApplicationThread(), intent, resolvedType, null,
882 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
883 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700885 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
887 }
888
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800889 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700891 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700893 String[] receiverPermissions = receiverPermission == null ? null
894 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700896 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700898 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700899 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
900 null, false, false, getUserId());
901 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700902 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700903 }
904 }
905
906 @Override
907 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
908 warnIfCallingFromSystemProcess();
909 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
910 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700911 intent.prepareToLeaveProcess(this);
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700912 ActivityManagerNative.getDefault().broadcastIntent(
913 mMainThread.getApplicationThread(), intent, resolvedType, null,
914 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700915 null, false, false, getUserId());
916 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700917 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -0700918 }
919 }
920
921 @Override
922 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
923 warnIfCallingFromSystemProcess();
924 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700925 String[] receiverPermissions = receiverPermission == null ? null
926 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -0700927 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700928 intent.prepareToLeaveProcess(this);
Dianne Hackborna750a632015-06-16 17:18:23 -0700929 ActivityManagerNative.getDefault().broadcastIntent(
930 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700931 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700932 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800933 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700934 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800935 }
936 }
937
938 @Override
939 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
940 warnIfCallingFromSystemProcess();
941 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700942 String[] receiverPermissions = receiverPermission == null ? null
943 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800944 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700945 intent.prepareToLeaveProcess(this);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800946 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700947 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700948 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -0700949 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700951 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953 }
954
955 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -0700956 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700957 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700959 String[] receiverPermissions = receiverPermission == null ? null
960 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700962 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700964 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700965 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700966 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700968 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 }
970 }
971
972 @Override
973 public void sendOrderedBroadcast(Intent intent,
974 String receiverPermission, BroadcastReceiver resultReceiver,
975 Handler scheduler, int initialCode, String initialData,
976 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800977 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700978 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
979 }
980
981 @Override
982 public void sendOrderedBroadcast(Intent intent,
983 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
984 Handler scheduler, int initialCode, String initialData,
985 Bundle initialExtras) {
986 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
987 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800988 }
989
990 @Override
991 public void sendOrderedBroadcast(Intent intent,
992 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
993 Handler scheduler, int initialCode, String initialData,
994 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -0700995 sendOrderedBroadcast(intent, receiverPermission, appOp,
996 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
997 }
998
999 void sendOrderedBroadcast(Intent intent,
1000 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1001 Handler scheduler, int initialCode, String initialData,
1002 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001003 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 IIntentReceiver rd = null;
1005 if (resultReceiver != null) {
1006 if (mPackageInfo != null) {
1007 if (scheduler == null) {
1008 scheduler = mMainThread.getHandler();
1009 }
1010 rd = mPackageInfo.getReceiverDispatcher(
1011 resultReceiver, getOuterContext(), scheduler,
1012 mMainThread.getInstrumentation(), false);
1013 } else {
1014 if (scheduler == null) {
1015 scheduler = mMainThread.getHandler();
1016 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001017 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1019 }
1020 }
1021 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001022 String[] receiverPermissions = receiverPermission == null ? null
1023 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001025 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 ActivityManagerNative.getDefault().broadcastIntent(
1027 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001028 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001029 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001031 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 }
1033 }
1034
1035 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001036 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001037 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1038 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001039 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001040 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001041 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001042 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001043 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001044 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001045 }
1046 }
1047
1048 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001049 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1050 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001051 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1052 }
1053
1054 @Override
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001055 public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission,
1056 Bundle options) {
1057 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1058 String[] receiverPermissions = receiverPermission == null ? null
1059 : new String[] {receiverPermission};
1060 try {
1061 intent.prepareToLeaveProcess(this);
1062 ActivityManagerNative.getDefault().broadcastIntent(
1063 mMainThread.getApplicationThread(), intent, resolvedType, null,
1064 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1065 options, false, false, user.getIdentifier());
1066 } catch (RemoteException e) {
1067 throw e.rethrowFromSystemServer();
1068 }
1069 }
1070
1071 @Override
Svet Ganov16a16892015-04-16 10:32:04 -07001072 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1073 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001074 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001075 String[] receiverPermissions = receiverPermission == null ? null
1076 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001077 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001078 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001079 ActivityManagerNative.getDefault().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001080 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001081 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001082 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001083 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001084 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001085 }
1086 }
1087
1088 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001089 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001090 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001091 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001092 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001093 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001094 }
1095
1096 @Override
1097 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1098 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001099 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1100 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1101 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1102 }
1103
1104 @Override
1105 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1106 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1107 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001108 IIntentReceiver rd = null;
1109 if (resultReceiver != null) {
1110 if (mPackageInfo != null) {
1111 if (scheduler == null) {
1112 scheduler = mMainThread.getHandler();
1113 }
1114 rd = mPackageInfo.getReceiverDispatcher(
1115 resultReceiver, getOuterContext(), scheduler,
1116 mMainThread.getInstrumentation(), false);
1117 } else {
1118 if (scheduler == null) {
1119 scheduler = mMainThread.getHandler();
1120 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001121 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1122 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001123 }
1124 }
1125 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001126 String[] receiverPermissions = receiverPermission == null ? null
1127 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001128 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001129 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001130 ActivityManagerNative.getDefault().broadcastIntent(
1131 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001132 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001133 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001134 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001135 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001136 }
1137 }
1138
1139 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001140 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001142 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1144 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001145 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 ActivityManagerNative.getDefault().broadcastIntent(
1147 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001148 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001149 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001151 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153 }
1154
1155 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001156 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001157 public void sendStickyOrderedBroadcast(Intent intent,
1158 BroadcastReceiver resultReceiver,
1159 Handler scheduler, int initialCode, String initialData,
1160 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001161 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001162 IIntentReceiver rd = null;
1163 if (resultReceiver != null) {
1164 if (mPackageInfo != null) {
1165 if (scheduler == null) {
1166 scheduler = mMainThread.getHandler();
1167 }
1168 rd = mPackageInfo.getReceiverDispatcher(
1169 resultReceiver, getOuterContext(), scheduler,
1170 mMainThread.getInstrumentation(), false);
1171 } else {
1172 if (scheduler == null) {
1173 scheduler = mMainThread.getHandler();
1174 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001175 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001176 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1177 }
1178 }
1179 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1180 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001181 intent.prepareToLeaveProcess(this);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001182 ActivityManagerNative.getDefault().broadcastIntent(
1183 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1184 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001185 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001186 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001187 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001188 }
1189 }
1190
1191 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001192 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 public void removeStickyBroadcast(Intent intent) {
1194 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1195 if (resolvedType != null) {
1196 intent = new Intent(intent);
1197 intent.setDataAndType(intent.getData(), resolvedType);
1198 }
1199 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001200 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001202 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001204 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206 }
1207
1208 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001209 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001210 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1211 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1212 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001213 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001214 ActivityManagerNative.getDefault().broadcastIntent(
1215 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001216 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1217 user.getIdentifier());
1218 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001219 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001220 }
1221 }
1222
1223 @Override
1224 @Deprecated
1225 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1226 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1227 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001228 intent.prepareToLeaveProcess(this);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001229 ActivityManagerNative.getDefault().broadcastIntent(
1230 mMainThread.getApplicationThread(), intent, resolvedType, null,
1231 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1232 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001233 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001234 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001235 }
1236 }
1237
1238 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001239 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001240 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1241 UserHandle user, BroadcastReceiver resultReceiver,
1242 Handler scheduler, int initialCode, String initialData,
1243 Bundle initialExtras) {
1244 IIntentReceiver rd = null;
1245 if (resultReceiver != null) {
1246 if (mPackageInfo != null) {
1247 if (scheduler == null) {
1248 scheduler = mMainThread.getHandler();
1249 }
1250 rd = mPackageInfo.getReceiverDispatcher(
1251 resultReceiver, getOuterContext(), scheduler,
1252 mMainThread.getInstrumentation(), false);
1253 } else {
1254 if (scheduler == null) {
1255 scheduler = mMainThread.getHandler();
1256 }
1257 rd = new LoadedApk.ReceiverDispatcher(
1258 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1259 }
1260 }
1261 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1262 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001263 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001264 ActivityManagerNative.getDefault().broadcastIntent(
1265 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1266 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001267 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001268 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001269 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001270 }
1271 }
1272
1273 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001274 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001275 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1276 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1277 if (resolvedType != null) {
1278 intent = new Intent(intent);
1279 intent.setDataAndType(intent.getData(), resolvedType);
1280 }
1281 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001282 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001283 ActivityManagerNative.getDefault().unbroadcastIntent(
1284 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1285 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001286 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001287 }
1288 }
1289
1290 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1292 return registerReceiver(receiver, filter, null, null);
1293 }
1294
1295 @Override
1296 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1297 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001298 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001299 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301
Dianne Hackborn20e80982012-08-31 19:00:44 -07001302 @Override
1303 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1304 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1305 return registerReceiverInternal(receiver, user.getIdentifier(),
1306 filter, broadcastPermission, scheduler, getOuterContext());
1307 }
1308
1309 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 IntentFilter filter, String broadcastPermission,
1311 Handler scheduler, Context context) {
1312 IIntentReceiver rd = null;
1313 if (receiver != null) {
1314 if (mPackageInfo != null && context != null) {
1315 if (scheduler == null) {
1316 scheduler = mMainThread.getHandler();
1317 }
1318 rd = mPackageInfo.getReceiverDispatcher(
1319 receiver, context, scheduler,
1320 mMainThread.getInstrumentation(), true);
1321 } else {
1322 if (scheduler == null) {
1323 scheduler = mMainThread.getHandler();
1324 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001325 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001326 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 }
1328 }
1329 try {
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001330 final Intent intent = ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001331 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001332 rd, filter, broadcastPermission, userId);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001333 if (intent != null) {
1334 intent.setExtrasClassLoader(getClassLoader());
1335 intent.prepareToEnterProcess();
1336 }
1337 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001339 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
1342
1343 @Override
1344 public void unregisterReceiver(BroadcastReceiver receiver) {
1345 if (mPackageInfo != null) {
1346 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1347 getOuterContext(), receiver);
1348 try {
1349 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1350 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001351 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 } else {
1354 throw new RuntimeException("Not supported in system context");
1355 }
1356 }
1357
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001358 private void validateServiceIntent(Intent service) {
1359 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001360 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001361 IllegalArgumentException ex = new IllegalArgumentException(
1362 "Service Intent must be explicit: " + service);
1363 throw ex;
1364 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001365 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1366 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001367 }
1368 }
1369 }
1370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 @Override
1372 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001373 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001374 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001375 }
1376
1377 @Override
1378 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001379 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001380 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001381 }
1382
1383 @Override
1384 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001385 return startServiceCommon(service, user);
1386 }
1387
1388 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001390 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001391 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 ComponentName cn = ActivityManagerNative.getDefault().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001393 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
1394 getContentResolver()), getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001395 if (cn != null) {
1396 if (cn.getPackageName().equals("!")) {
1397 throw new SecurityException(
1398 "Not allowed to start service " + service
1399 + " without permission " + cn.getClassName());
1400 } else if (cn.getPackageName().equals("!!")) {
1401 throw new SecurityException(
1402 "Unable to start service " + service
1403 + ": " + cn.getClassName());
1404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
1406 return cn;
1407 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001408 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410 }
1411
1412 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001413 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001414 return stopServiceCommon(service, user);
1415 }
1416
1417 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001419 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001420 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 int res = ActivityManagerNative.getDefault().stopService(
1422 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001423 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 if (res < 0) {
1425 throw new SecurityException(
1426 "Not allowed to stop service " + service);
1427 }
1428 return res != 0;
1429 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001430 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432 }
1433
1434 @Override
1435 public boolean bindService(Intent service, ServiceConnection conn,
1436 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001437 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001438 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1439 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001440 }
1441
1442 /** @hide */
1443 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001444 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1445 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001446 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001447 }
1448
Adrian Roos691546e2016-02-09 10:13:41 -08001449 /** @hide */
1450 @Override
1451 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1452 Handler handler, UserHandle user) {
1453 if (handler == null) {
1454 throw new IllegalArgumentException("handler must not be null.");
1455 }
1456 return bindServiceCommon(service, conn, flags, handler, user);
1457 }
1458
1459 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1460 handler, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001462 if (conn == null) {
1463 throw new IllegalArgumentException("connection is null");
1464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 if (mPackageInfo != null) {
Adrian Roos691546e2016-02-09 10:13:41 -08001466 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 } else {
1468 throw new RuntimeException("Not supported in system context");
1469 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001470 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001472 IBinder token = getActivityToken();
1473 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1474 && mPackageInfo.getApplicationInfo().targetSdkVersion
1475 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1476 flags |= BIND_WAIVE_PRIORITY;
1477 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001478 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 int res = ActivityManagerNative.getDefault().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001480 mMainThread.getApplicationThread(), getActivityToken(), service,
1481 service.resolveTypeIfNeeded(getContentResolver()),
1482 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 if (res < 0) {
1484 throw new SecurityException(
1485 "Not allowed to bind to service " + service);
1486 }
1487 return res != 0;
1488 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001489 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 }
1492
1493 @Override
1494 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001495 if (conn == null) {
1496 throw new IllegalArgumentException("connection is null");
1497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 if (mPackageInfo != null) {
1499 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1500 getOuterContext(), conn);
1501 try {
1502 ActivityManagerNative.getDefault().unbindService(sd);
1503 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001504 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506 } else {
1507 throw new RuntimeException("Not supported in system context");
1508 }
1509 }
1510
1511 @Override
1512 public boolean startInstrumentation(ComponentName className,
1513 String profileFile, Bundle arguments) {
1514 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001515 if (arguments != null) {
1516 arguments.setAllowFds(false);
1517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001519 className, profileFile, 0, arguments, null, null, getUserId(),
1520 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001522 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525
1526 @Override
1527 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001528 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
1530
Jeff Brown6e539312015-02-24 18:53:21 -08001531 @Override
1532 public String getSystemServiceName(Class<?> serviceClass) {
1533 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001534 }
1535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 @Override
1537 public int checkPermission(String permission, int pid, int uid) {
1538 if (permission == null) {
1539 throw new IllegalArgumentException("permission is null");
1540 }
1541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 try {
1543 return ActivityManagerNative.getDefault().checkPermission(
1544 permission, pid, uid);
1545 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001546 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548 }
1549
Dianne Hackbornff170242014-11-19 10:59:01 -08001550 /** @hide */
1551 @Override
1552 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1553 if (permission == null) {
1554 throw new IllegalArgumentException("permission is null");
1555 }
1556
1557 try {
1558 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1559 permission, pid, uid, callerToken);
1560 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001561 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001562 }
1563 }
1564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 @Override
1566 public int checkCallingPermission(String permission) {
1567 if (permission == null) {
1568 throw new IllegalArgumentException("permission is null");
1569 }
1570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 int pid = Binder.getCallingPid();
1572 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001573 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575 return PackageManager.PERMISSION_DENIED;
1576 }
1577
1578 @Override
1579 public int checkCallingOrSelfPermission(String permission) {
1580 if (permission == null) {
1581 throw new IllegalArgumentException("permission is null");
1582 }
1583
1584 return checkPermission(permission, Binder.getCallingPid(),
1585 Binder.getCallingUid());
1586 }
1587
Svetoslavc6d1c342015-02-26 14:44:43 -08001588 @Override
1589 public int checkSelfPermission(String permission) {
1590 if (permission == null) {
1591 throw new IllegalArgumentException("permission is null");
1592 }
1593
1594 return checkPermission(permission, Process.myPid(), Process.myUid());
1595 }
1596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 private void enforce(
1598 String permission, int resultOfCheck,
1599 boolean selfToo, int uid, String message) {
1600 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1601 throw new SecurityException(
1602 (message != null ? (message + ": ") : "") +
1603 (selfToo
1604 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001605 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 permission +
1607 ".");
1608 }
1609 }
1610
Jeff Brown6e539312015-02-24 18:53:21 -08001611 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 public void enforcePermission(
1613 String permission, int pid, int uid, String message) {
1614 enforce(permission,
1615 checkPermission(permission, pid, uid),
1616 false,
1617 uid,
1618 message);
1619 }
1620
Jeff Brown6e539312015-02-24 18:53:21 -08001621 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 public void enforceCallingPermission(String permission, String message) {
1623 enforce(permission,
1624 checkCallingPermission(permission),
1625 false,
1626 Binder.getCallingUid(),
1627 message);
1628 }
1629
Jeff Brown6e539312015-02-24 18:53:21 -08001630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 public void enforceCallingOrSelfPermission(
1632 String permission, String message) {
1633 enforce(permission,
1634 checkCallingOrSelfPermission(permission),
1635 true,
1636 Binder.getCallingUid(),
1637 message);
1638 }
1639
1640 @Override
1641 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1642 try {
1643 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001644 mMainThread.getApplicationThread(), toPackage,
1645 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001647 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 }
1649 }
1650
1651 @Override
1652 public void revokeUriPermission(Uri uri, int modeFlags) {
1653 try {
1654 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001655 mMainThread.getApplicationThread(),
1656 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001658 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660 }
1661
1662 @Override
1663 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 try {
1665 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001666 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001667 resolveUserId(uri), null);
1668 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001669 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001670 }
1671 }
1672
1673 /** @hide */
1674 @Override
1675 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1676 try {
1677 return ActivityManagerNative.getDefault().checkUriPermission(
1678 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1679 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001681 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 }
1684
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001685 private int resolveUserId(Uri uri) {
1686 return ContentProvider.getUserIdFromUri(uri, getUserId());
1687 }
1688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 @Override
1690 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 int pid = Binder.getCallingPid();
1692 if (pid != Process.myPid()) {
1693 return checkUriPermission(uri, pid,
1694 Binder.getCallingUid(), modeFlags);
1695 }
1696 return PackageManager.PERMISSION_DENIED;
1697 }
1698
1699 @Override
1700 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1701 return checkUriPermission(uri, Binder.getCallingPid(),
1702 Binder.getCallingUid(), modeFlags);
1703 }
1704
1705 @Override
1706 public int checkUriPermission(Uri uri, String readPermission,
1707 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001708 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1710 + readPermission + " writePermission=" + writePermission
1711 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1712 }
1713 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1714 if (readPermission == null
1715 || checkPermission(readPermission, pid, uid)
1716 == PackageManager.PERMISSION_GRANTED) {
1717 return PackageManager.PERMISSION_GRANTED;
1718 }
1719 }
1720 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1721 if (writePermission == null
1722 || checkPermission(writePermission, pid, uid)
1723 == PackageManager.PERMISSION_GRANTED) {
1724 return PackageManager.PERMISSION_GRANTED;
1725 }
1726 }
1727 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1728 : PackageManager.PERMISSION_DENIED;
1729 }
1730
1731 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001732 StringBuilder builder = new StringBuilder();
1733 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1734 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001736 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1737 builder.append("write and ");
1738 }
1739 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1740 builder.append("persistable and ");
1741 }
1742 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1743 builder.append("prefix and ");
1744 }
1745
1746 if (builder.length() > 5) {
1747 builder.setLength(builder.length() - 5);
1748 return builder.toString();
1749 } else {
1750 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
1753
1754 private void enforceForUri(
1755 int modeFlags, int resultOfCheck, boolean selfToo,
1756 int uid, Uri uri, String message) {
1757 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1758 throw new SecurityException(
1759 (message != null ? (message + ": ") : "") +
1760 (selfToo
1761 ? "Neither user " + uid + " nor current process has "
1762 : "User " + uid + " does not have ") +
1763 uriModeFlagToString(modeFlags) +
1764 " permission on " +
1765 uri +
1766 ".");
1767 }
1768 }
1769
Jeff Brown6e539312015-02-24 18:53:21 -08001770 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 public void enforceUriPermission(
1772 Uri uri, int pid, int uid, int modeFlags, String message) {
1773 enforceForUri(
1774 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1775 false, uid, uri, message);
1776 }
1777
Jeff Brown6e539312015-02-24 18:53:21 -08001778 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 public void enforceCallingUriPermission(
1780 Uri uri, int modeFlags, String message) {
1781 enforceForUri(
1782 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001783 false,
1784 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
1786
Jeff Brown6e539312015-02-24 18:53:21 -08001787 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 public void enforceCallingOrSelfUriPermission(
1789 Uri uri, int modeFlags, String message) {
1790 enforceForUri(
1791 modeFlags,
1792 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1793 Binder.getCallingUid(), uri, message);
1794 }
1795
Jeff Brown6e539312015-02-24 18:53:21 -08001796 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 public void enforceUriPermission(
1798 Uri uri, String readPermission, String writePermission,
1799 int pid, int uid, int modeFlags, String message) {
1800 enforceForUri(modeFlags,
1801 checkUriPermission(
1802 uri, readPermission, writePermission, pid, uid,
1803 modeFlags),
1804 false,
1805 uid,
1806 uri,
1807 message);
1808 }
1809
Tom O'Neill365632e2013-09-09 09:34:58 -07001810 /**
1811 * Logs a warning if the system process directly called a method such as
1812 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1813 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1814 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001815 private void warnIfCallingFromSystemProcess() {
1816 if (Process.myUid() == Process.SYSTEM_UID) {
1817 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001818 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001819 }
1820 }
1821
Adam Lesinski8fa71072016-11-18 18:13:55 -08001822 private static Resources createResources(IBinder activityToken, LoadedApk pi, int displayId,
1823 Configuration overrideConfig, CompatibilityInfo compatInfo) {
1824 return ResourcesManager.getInstance().getResources(activityToken,
1825 pi.getResDir(),
1826 pi.getSplitResDirs(),
1827 pi.getOverlayDirs(),
1828 pi.getApplicationInfo().sharedLibraryFiles,
1829 displayId,
1830 overrideConfig,
1831 compatInfo,
1832 pi.getClassLoader());
1833 }
1834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001836 public Context createApplicationContext(ApplicationInfo application, int flags)
1837 throws NameNotFoundException {
1838 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1839 flags | CONTEXT_REGISTER_PACKAGE);
1840 if (pi != null) {
Svetoslav976e8bd2014-07-16 15:12:03 -07001841 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Adam Lesinski8fa71072016-11-18 18:13:55 -08001842 new UserHandle(UserHandle.getUserId(application.uid)), flags);
1843
1844 final int displayId = mDisplay != null
1845 ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1846
1847 c.mResources = createResources(mActivityToken, pi, displayId, null,
1848 getDisplayAdjustments(displayId).getCompatibilityInfo());
Svetoslav976e8bd2014-07-16 15:12:03 -07001849 if (c.mResources != null) {
1850 return c;
1851 }
1852 }
1853
1854 throw new PackageManager.NameNotFoundException(
1855 "Application package " + application.packageName + " not found");
1856 }
1857
1858 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001860 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001861 return createPackageContextAsUser(packageName, flags,
1862 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001863 }
1864
1865 @Override
1866 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1867 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00001868 if (packageName.equals("system") || packageName.equals("android")) {
Adam Lesinski8fa71072016-11-18 18:13:55 -08001869 // The system resources are loaded in every application, so we can safely copy
1870 // the context without reloading Resources.
1871 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, user, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
1873
Jeff Browndefd4a62014-03-10 21:24:37 -07001874 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001875 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 if (pi != null) {
Adam Lesinski8fa71072016-11-18 18:13:55 -08001877 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken, user, flags);
1878
1879 final int displayId = mDisplay != null
1880 ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1881
1882 c.mResources = createResources(mActivityToken, pi, displayId, null,
1883 getDisplayAdjustments(displayId).getCompatibilityInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 if (c.mResources != null) {
1885 return c;
1886 }
1887 }
1888
1889 // Should be a better exception.
1890 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001891 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
1893
Romain Guy870e09f2009-07-06 16:35:25 -07001894 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001895 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001896 if (overrideConfiguration == null) {
1897 throw new IllegalArgumentException("overrideConfiguration must not be null");
1898 }
1899
Adam Lesinski8fa71072016-11-18 18:13:55 -08001900 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1901 mUser, mFlags);
1902
1903 final int displayId = mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1904 context.mResources = createResources(mActivityToken, mPackageInfo, displayId,
1905 overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo());
1906 return context;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001907 }
1908
1909 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001910 public Context createDisplayContext(Display display) {
1911 if (display == null) {
1912 throw new IllegalArgumentException("display must not be null");
1913 }
1914
Adam Lesinski8fa71072016-11-18 18:13:55 -08001915 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1916 mUser, mFlags);
1917
1918 final int displayId = display.getDisplayId();
1919 context.mResources = createResources(mActivityToken, mPackageInfo, displayId, null,
1920 getDisplayAdjustments(displayId).getCompatibilityInfo());
1921 context.mDisplay = display;
1922 return context;
Jeff Browna492c3a2012-08-23 19:48:44 -07001923 }
1924
Jeff Browna492c3a2012-08-23 19:48:44 -07001925 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001926 public Context createDeviceProtectedStorageContext() {
1927 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
1928 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Adam Lesinski8fa71072016-11-18 18:13:55 -08001929 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, mUser, flags);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001930 }
1931
1932 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001933 public Context createCredentialProtectedStorageContext() {
1934 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
1935 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Adam Lesinski8fa71072016-11-18 18:13:55 -08001936 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, mUser, flags);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001937 }
1938
1939 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001940 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001941 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
1942 }
1943
1944 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001945 public boolean isDeviceProtectedStorage() {
1946 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001947 }
1948
1949 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001950 public boolean isCredentialProtectedStorage() {
1951 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07001952 }
1953
Jeff Brown98365d72012-08-19 20:30:52 -07001954 @Override
Adam Lesinski4ece3d62016-06-16 18:05:41 -07001955 public Display getDisplay() {
1956 final DisplayAdjustments displayAdjustments = mResources.getDisplayAdjustments();
1957 if (mDisplay == null) {
1958 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
1959 displayAdjustments);
1960 }
1961
1962 if (!mDisplay.getDisplayAdjustments().equals(displayAdjustments)) {
1963 mDisplay = mResourcesManager.getAdjustedDisplay(mDisplay.getDisplayId(),
1964 displayAdjustments);
1965 }
1966 return mDisplay;
1967 }
1968
1969 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001970 public DisplayAdjustments getDisplayAdjustments(int displayId) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07001971 return mResources.getDisplayAdjustments();
Jeff Brown98365d72012-08-19 20:30:52 -07001972 }
1973
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001974 @Override
1975 public File getDataDir() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001977 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001978 if (isCredentialProtectedStorage()) {
1979 res = mPackageInfo.getCredentialProtectedDataDirFile();
1980 } else if (isDeviceProtectedStorage()) {
1981 res = mPackageInfo.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001982 } else {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001983 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001984 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001985
1986 if (res != null) {
Jeff Sharkey21f50722016-04-27 12:38:02 -06001987 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Jeff Sharkey24492ae2016-04-25 13:20:25 -06001988 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
1989 new Throwable());
1990 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001991 return res;
1992 } else {
1993 throw new RuntimeException(
1994 "No data directory found for package " + getPackageName());
1995 }
1996 } else {
1997 throw new RuntimeException(
1998 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001
2002 @Override
2003 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07002004 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002006 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 if (!file.exists()) {
2008 file.mkdir();
2009 setFilePermissionsFromMode(file.getPath(), mode,
2010 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
2011 }
2012 return file;
2013 }
2014
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002015 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08002016 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002017 public int getUserId() {
2018 return mUser.getIdentifier();
2019 }
2020
Dianne Hackborn21556372010-02-04 16:34:40 -08002021 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002022 LoadedApk packageInfo = new LoadedApk(mainThread);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002023 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, 0);
2024 context.mResources = packageInfo.getResources(mainThread);
Jeff Browndefd4a62014-03-10 21:24:37 -07002025 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002026 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 return context;
2028 }
2029
Jeff Browndefd4a62014-03-10 21:24:37 -07002030 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
2031 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski8fa71072016-11-18 18:13:55 -08002032 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, 0);
2033 context.mResources = packageInfo.getResources(mainThread);
2034 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002035 }
2036
2037 static ContextImpl createActivityContext(ActivityThread mainThread,
Adam Lesinski082614c2016-03-04 14:33:47 -08002038 LoadedApk packageInfo, IBinder activityToken, int displayId,
2039 Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002040 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski8fa71072016-11-18 18:13:55 -08002041
2042 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, activityToken, null,
2043 0);
2044
2045 // Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
2046 displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
2047
2048 final CompatibilityInfo compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2049 ? packageInfo.getCompatibilityInfo()
2050 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
2051
2052 context.mResources = createResources(activityToken, packageInfo, displayId,
2053 overrideConfiguration, compatInfo);
2054 context.mDisplay = ResourcesManager.getInstance().getAdjustedDisplay(displayId,
2055 context.mResources.getDisplayAdjustments());
2056 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002057 }
2058
2059 private ContextImpl(ContextImpl container, ActivityThread mainThread,
Adam Lesinski8fa71072016-11-18 18:13:55 -08002060 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002063 // If creator didn't specify which storage to use, use the default
2064 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002065 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2066 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002067 final File dataDir = packageInfo.getDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002068 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
2069 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
2070 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
2071 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002072 }
2073 }
2074
Jeff Browndefd4a62014-03-10 21:24:37 -07002075 mMainThread = mainThread;
2076 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002077 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078
Jeff Browndefd4a62014-03-10 21:24:37 -07002079 if (user == null) {
2080 user = Process.myUserHandle();
2081 }
2082 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07002085 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002086
Jeff Browndefd4a62014-03-10 21:24:37 -07002087 if (container != null) {
2088 mBasePackageName = container.mBasePackageName;
2089 mOpPackageName = container.mOpPackageName;
Adam Lesinski8fa71072016-11-18 18:13:55 -08002090 mResources = container.mResources;
2091 mDisplay = container.mDisplay;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002092 } else {
2093 mBasePackageName = packageInfo.mPackageName;
2094 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2095 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2096 // Special case: system components allow themselves to be loaded in to other
2097 // processes. For purposes of app ops, we must then consider the context as
2098 // belonging to the package of this process, not the system itself, otherwise
2099 // the package+uid verifications in app ops will fail.
2100 mOpPackageName = ActivityThread.currentPackageName();
2101 } else {
2102 mOpPackageName = mBasePackageName;
2103 }
2104 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002105
2106 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
2108
Narayan Kamath29564cd2014-08-07 10:57:40 +01002109 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2110 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112
2113 final void scheduleFinalCleanup(String who, String what) {
2114 mMainThread.scheduleContextCleanup(this, who, what);
2115 }
2116
2117 final void performFinalCleanup(String who, String what) {
2118 //Log.i(TAG, "Cleanup up context: " + this);
2119 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2120 }
2121
2122 final Context getReceiverRestrictedContext() {
2123 if (mReceiverRestrictedContext != null) {
2124 return mReceiverRestrictedContext;
2125 }
2126 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2127 }
2128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 final void setOuterContext(Context context) {
2130 mOuterContext = context;
2131 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 final Context getOuterContext() {
2134 return mOuterContext;
2135 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 final IBinder getActivityToken() {
2138 return mActivityToken;
2139 }
2140
Jeff Sharkey634dc422016-01-30 17:44:15 -07002141 private void checkMode(int mode) {
2142 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2143 if ((mode & MODE_WORLD_READABLE) != 0) {
2144 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2145 }
2146 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2147 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2148 }
2149 }
2150 }
2151
Jeff Brown6e539312015-02-24 18:53:21 -08002152 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002153 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 int extraPermissions) {
2155 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2156 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2157 |extraPermissions;
2158 if ((mode&MODE_WORLD_READABLE) != 0) {
2159 perms |= FileUtils.S_IROTH;
2160 }
2161 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2162 perms |= FileUtils.S_IWOTH;
2163 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002164 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2166 + ", perms=0x" + Integer.toHexString(perms));
2167 }
2168 FileUtils.setPermissions(name, perms, -1, -1);
2169 }
2170
2171 private File makeFilename(File base, String name) {
2172 if (name.indexOf(File.separatorChar) < 0) {
2173 return new File(base, name);
2174 }
2175 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002176 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
2178
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002179 /**
2180 * Ensure that given directories exist, trying to create them if missing. If
2181 * unable to create, they are filtered by replacing with {@code null}.
2182 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002183 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002184 File[] result = new File[dirs.length];
2185 for (int i = 0; i < dirs.length; i++) {
2186 File dir = dirs[i];
2187 if (!dir.exists()) {
2188 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002189 // recheck existence in case of cross-process race
2190 if (!dir.exists()) {
2191 // Failing to mkdir() may be okay, since we might not have
2192 // enough permissions; ask vold to create on our behalf.
2193 final IMountService mount = IMountService.Stub.asInterface(
2194 ServiceManager.getService("mount"));
Christopher Tatecc866da2013-10-02 18:11:01 -07002195 try {
Jeff Sharkey983294592015-07-13 10:25:31 -07002196 final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
2197 if (res != 0) {
2198 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2199 dir = null;
2200 }
2201 } catch (Exception e) {
2202 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002203 dir = null;
2204 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002205 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002206 }
2207 }
2208 result[i] = dir;
2209 }
2210 return result;
2211 }
2212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 // ----------------------------------------------------------------------
2214 // ----------------------------------------------------------------------
2215 // ----------------------------------------------------------------------
2216
2217 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002218 private final ActivityThread mMainThread;
2219 private final UserHandle mUser;
2220
2221 public ApplicationContentResolver(
2222 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002224 mMainThread = Preconditions.checkNotNull(mainThread);
2225 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
2227
2228 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002229 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002230 return mMainThread.acquireProvider(context,
2231 ContentProvider.getAuthorityWithoutUserId(auth),
2232 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
2234
2235 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002236 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002237 return mMainThread.acquireExistingProvider(context,
2238 ContentProvider.getAuthorityWithoutUserId(auth),
2239 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002240 }
2241
2242 @Override
2243 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002244 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002246
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002247 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002248 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002249 return mMainThread.acquireProvider(c,
2250 ContentProvider.getAuthorityWithoutUserId(auth),
2251 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002252 }
2253
2254 @Override
2255 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002256 return mMainThread.releaseProvider(icp, false);
2257 }
2258
2259 @Override
2260 public void unstableProviderDied(IContentProvider icp) {
2261 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002262 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002263
2264 @Override
2265 public void appNotRespondingViaProvider(IContentProvider icp) {
2266 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2267 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002268
2269 /** @hide */
2270 protected int resolveUserIdFromAuthority(String auth) {
2271 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274}