blob: 6bb853afd90d6ee2872f70a1a1bb81bd42fcfed8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.BroadcastReceiver;
20import android.content.ComponentName;
Nicolas Prevotd85fc722014-04-16 19:52:08 +010021import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.ContextWrapper;
25import android.content.IContentProvider;
Jeff Brown6e539312015-02-24 18:53:21 -080026import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Intent;
28import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070029import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.ReceiverCallNotAllowedException;
31import android.content.ServiceConnection;
32import android.content.SharedPreferences;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070036import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070038import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070039import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070041import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.database.sqlite.SQLiteDatabase;
43import android.database.sqlite.SQLiteDatabase.CursorFactory;
44import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080048import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070050import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080051import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.FileUtils;
53import android.os.Handler;
54import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070059import android.os.UserHandle;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070060import android.os.storage.IMountService;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -070061import android.system.ErrnoException;
62import android.system.Os;
63import android.system.OsConstants;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070065import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070067import android.util.Slog;
Jeff Brown98365d72012-08-19 20:30:52 -070068import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080069import android.view.DisplayAdjustments;
Dan Egnor95240272009-10-27 18:23:39 -070070
Jeff Sharkey35871f22016-01-29 17:13:29 -070071import com.android.internal.annotations.GuardedBy;
72import com.android.internal.util.Preconditions;
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.io.File;
75import java.io.FileInputStream;
76import java.io.FileNotFoundException;
77import java.io.FileOutputStream;
Jeff Sharkey35871f22016-01-29 17:13:29 -070078import java.io.FilenameFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.io.IOException;
80import java.io.InputStream;
Jeff Sharkey7a30a302015-12-08 14:20:06 -070081import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083class ReceiverRestrictedContext extends ContextWrapper {
84 ReceiverRestrictedContext(Context base) {
85 super(base);
86 }
87
88 @Override
89 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
90 return registerReceiver(receiver, filter, null, null);
91 }
92
93 @Override
94 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
95 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070096 if (receiver == null) {
97 // Allow retrieving current sticky broadcast; this is safe since we
98 // aren't actually registering a receiver.
99 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
100 } else {
101 throw new ReceiverCallNotAllowedException(
102 "BroadcastReceiver components are not allowed to register to receive intents");
103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 }
105
106 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700107 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
108 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700109 if (receiver == null) {
110 // Allow retrieving current sticky broadcast; this is safe since we
111 // aren't actually registering a receiver.
112 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
113 } else {
114 throw new ReceiverCallNotAllowedException(
115 "BroadcastReceiver components are not allowed to register to receive intents");
116 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700117 }
118
119 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
121 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700122 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
124}
125
126/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800127 * Common implementation of Context API, which provides the base
128 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800130class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800131 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700132 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700134 /**
135 * Map from package name, to preference name, to cached preferences.
136 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700137 @GuardedBy("ContextImpl.class")
138 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
139
140 /**
141 * Map from preference name to generated path.
142 */
143 @GuardedBy("ContextImpl.class")
144 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Jeff Browndefd4a62014-03-10 21:24:37 -0700146 final ActivityThread mMainThread;
147 final LoadedApk mPackageInfo;
148
149 private final IBinder mActivityToken;
150
151 private final UserHandle mUser;
152
153 private final ApplicationContentResolver mContentResolver;
154
155 private final String mBasePackageName;
156 private final String mOpPackageName;
157
158 private final ResourcesManager mResourcesManager;
159 private final Resources mResources;
160 private final Display mDisplay; // may be null if default display
161 private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments();
Jeff Browndefd4a62014-03-10 21:24:37 -0700162
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700163 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 private int mThemeResource = 0;
167 private Resources.Theme mTheme = null;
168 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
171 private final Object mSync = new Object();
172
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700173 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700175 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700177 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700179 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700180 private File mNoBackupFilesDir;
181 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700183 @GuardedBy("mSync")
184 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700185
186 @GuardedBy("mSync")
187 private File[] mExternalObbDirs;
188 @GuardedBy("mSync")
189 private File[] mExternalFilesDirs;
190 @GuardedBy("mSync")
191 private File[] mExternalCacheDirs;
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700192 @GuardedBy("mSync")
193 private File[] mExternalMediaDirs;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200194
Jeff Brown6e539312015-02-24 18:53:21 -0800195 // The system service cache for the system services that are cached per-ContextImpl.
196 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800197
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700198 static ContextImpl getImpl(Context context) {
199 Context nextContext;
200 while ((context instanceof ContextWrapper) &&
201 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
202 context = nextContext;
203 }
204 return (ContextImpl)context;
205 }
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 @Override
208 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700209 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211
212 @Override
213 public Resources getResources() {
214 return mResources;
215 }
216
217 @Override
218 public PackageManager getPackageManager() {
219 if (mPackageManager != null) {
220 return mPackageManager;
221 }
222
223 IPackageManager pm = ActivityThread.getPackageManager();
224 if (pm != null) {
225 // Doesn't matter if we make more than one instance.
226 return (mPackageManager = new ApplicationPackageManager(this, pm));
227 }
228
229 return null;
230 }
231
232 @Override
233 public ContentResolver getContentResolver() {
234 return mContentResolver;
235 }
236
237 @Override
238 public Looper getMainLooper() {
239 return mMainThread.getLooper();
240 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 @Override
243 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700244 return (mPackageInfo != null) ?
245 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700249 public void setTheme(int resId) {
250 if (mThemeResource != resId) {
251 mThemeResource = resId;
252 initializeTheme();
253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800257 public int getThemeResId() {
258 return mThemeResource;
259 }
260
261 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 public Resources.Theme getTheme() {
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700263 if (mTheme != null) {
264 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700266
267 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
268 getOuterContext().getApplicationInfo().targetSdkVersion);
269 initializeTheme();
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 return mTheme;
272 }
273
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700274 private void initializeTheme() {
275 if (mTheme == null) {
276 mTheme = mResources.newTheme();
277 }
278 mTheme.applyStyle(mThemeResource, true);
279 }
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 @Override
282 public ClassLoader getClassLoader() {
283 return mPackageInfo != null ?
284 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
285 }
286
287 @Override
288 public String getPackageName() {
289 if (mPackageInfo != null) {
290 return mPackageInfo.getPackageName();
291 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800292 // No mPackageInfo means this is a Context for the system itself,
293 // and this here is its name.
294 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800297 /** @hide */
298 @Override
299 public String getBasePackageName() {
300 return mBasePackageName != null ? mBasePackageName : getPackageName();
301 }
302
Dianne Hackborn95d78532013-09-11 09:51:14 -0700303 /** @hide */
304 @Override
305 public String getOpPackageName() {
306 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
307 }
308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700310 public ApplicationInfo getApplicationInfo() {
311 if (mPackageInfo != null) {
312 return mPackageInfo.getApplicationInfo();
313 }
314 throw new RuntimeException("Not supported in system context");
315 }
316
317 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 public String getPackageResourcePath() {
319 if (mPackageInfo != null) {
320 return mPackageInfo.getResDir();
321 }
322 throw new RuntimeException("Not supported in system context");
323 }
324
325 @Override
326 public String getPackageCodePath() {
327 if (mPackageInfo != null) {
328 return mPackageInfo.getAppDir();
329 }
330 throw new RuntimeException("Not supported in system context");
331 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200332
Jeff Brown6e539312015-02-24 18:53:21 -0800333 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700335 // At least one application in the world actually passes in a null
336 // name. This happened to work because when we generated the file name
337 // we would stringify it to "null.xml". Nice.
338 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
339 Build.VERSION_CODES.KITKAT) {
340 if (name == null) {
341 name = "null";
342 }
343 }
344
Jeff Sharkeybe782582016-02-15 18:35:57 -0700345 File file;
346 synchronized (ContextImpl.class) {
347 if (mSharedPrefsPaths == null) {
348 mSharedPrefsPaths = new ArrayMap<>();
349 }
350 file = mSharedPrefsPaths.get(name);
351 if (file == null) {
352 file = getSharedPreferencesPath(name);
353 mSharedPrefsPaths.put(name, file);
354 }
355 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700356 return getSharedPreferences(file, mode);
357 }
358
359 @Override
360 public SharedPreferences getSharedPreferences(File file, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700361 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700363 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700364 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
365 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700366 if (sp == null) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700367 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700368 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700369 return sp;
370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800372 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
373 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
374 // If somebody else (some other process) changed the prefs
375 // file behind our back, we reload it. This has been the
376 // historical (if undocumented) behavior.
377 sp.startReloadIfChangedUnexpectedly();
378 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700379 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 }
381
Jeff Sharkey35871f22016-01-29 17:13:29 -0700382 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700383 if (sSharedPrefsCache == null) {
384 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700385 }
386
387 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700388 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700389 if (packagePrefs == null) {
390 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700391 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700392 }
393
394 return packagePrefs;
395 }
396
397 /**
398 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700399 * requested prefix.
400 *
401 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700402 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600403 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700404 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
405 @Override
406 public boolean accept(File dir, String name) {
407 return name.startsWith(prefix);
408 }
409 });
410
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700411 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700412 for (File sourceFile : sourceFiles) {
413 final File targetFile = new File(targetDir, sourceFile.getName());
414 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
415 try {
416 FileUtils.copyFileOrThrow(sourceFile, targetFile);
417 FileUtils.copyPermissions(sourceFile, targetFile);
418 if (!sourceFile.delete()) {
419 throw new IOException("Failed to clean up " + sourceFile);
420 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700421 if (res != -1) {
422 res++;
423 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700424 } catch (IOException e) {
425 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700426 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700427 }
428 }
429 return res;
430 }
431
432 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600433 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700434 synchronized (ContextImpl.class) {
435 final File source = sourceContext.getSharedPreferencesPath(name);
436 final File target = getSharedPreferencesPath(name);
437
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600438 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700439 source.getName());
440 if (res > 0) {
441 // We moved at least one file, so evict any in-memory caches for
442 // either location
443 final ArrayMap<File, SharedPreferencesImpl> cache =
444 getSharedPreferencesCacheLocked();
445 cache.remove(source);
446 cache.remove(target);
447 }
448 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700449 }
450 }
451
452 @Override
453 public boolean deleteSharedPreferences(String name) {
454 synchronized (ContextImpl.class) {
455 final File prefs = getSharedPreferencesPath(name);
456 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
457
458 // Evict any in-memory caches
459 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
460 cache.remove(prefs);
461
462 prefs.delete();
463 prefsBackup.delete();
464
465 // We failed if files are still lingering
466 return !(prefs.exists() || prefsBackup.exists());
467 }
468 }
469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 private File getPreferencesDir() {
471 synchronized (mSync) {
472 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700473 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700475 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477 }
478
479 @Override
480 public FileInputStream openFileInput(String name)
481 throws FileNotFoundException {
482 File f = makeFilename(getFilesDir(), name);
483 return new FileInputStream(f);
484 }
485
486 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700487 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
488 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 final boolean append = (mode&MODE_APPEND) != 0;
490 File f = makeFilename(getFilesDir(), name);
491 try {
492 FileOutputStream fos = new FileOutputStream(f, append);
493 setFilePermissionsFromMode(f.getPath(), mode, 0);
494 return fos;
495 } catch (FileNotFoundException e) {
496 }
497
498 File parent = f.getParentFile();
499 parent.mkdir();
500 FileUtils.setPermissions(
501 parent.getPath(),
502 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
503 -1, -1);
504 FileOutputStream fos = new FileOutputStream(f, append);
505 setFilePermissionsFromMode(f.getPath(), mode, 0);
506 return fos;
507 }
508
509 @Override
510 public boolean deleteFile(String name) {
511 File f = makeFilename(getFilesDir(), name);
512 return f.delete();
513 }
514
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700515 /**
516 * Common-path handling of app data dir creation
517 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700518 private static File ensurePrivateDirExists(File file) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700519 if (!file.exists()) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700520 try {
521 Os.mkdir(file.getAbsolutePath(), 0771);
Jeff Sharkey46ed6f42016-02-15 14:16:08 -0700522 Os.chmod(file.getAbsolutePath(), 0771);
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700523 } catch (ErrnoException e) {
524 if (e.errno == OsConstants.EEXIST) {
525 // We must have raced with someone; that's okay
526 } else {
527 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700528 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700529 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700530 }
531 return file;
532 }
533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 @Override
535 public File getFilesDir() {
536 synchronized (mSync) {
537 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700538 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700540 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700541 }
542 }
543
544 @Override
545 public File getNoBackupFilesDir() {
546 synchronized (mSync) {
547 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700548 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700550 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800555 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700556 // Operates on primary external storage
557 return getExternalFilesDirs(type)[0];
558 }
559
560 @Override
561 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800562 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700563 if (mExternalFilesDirs == null) {
564 mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800565 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700566
567 // Splice in requested type, if any
568 File[] dirs = mExternalFilesDirs;
569 if (type != null) {
570 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800571 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700572
573 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700574 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800575 }
576 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200577
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800578 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800579 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700580 // Operates on primary external storage
581 return getObbDirs()[0];
582 }
583
584 @Override
585 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800586 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700587 if (mExternalObbDirs == null) {
588 mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800589 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700590
591 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700592 return ensureExternalDirsExistOrFilter(mExternalObbDirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800593 }
594 }
595
596 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 public File getCacheDir() {
598 synchronized (mSync) {
599 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700600 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700602 return ensurePrivateDirExists(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200605
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800606 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700607 public File getCodeCacheDir() {
608 synchronized (mSync) {
609 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700610 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700611 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700612 return ensurePrivateDirExists(mCodeCacheDir);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700613 }
614 }
615
616 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800617 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700618 // Operates on primary external storage
619 return getExternalCacheDirs()[0];
620 }
621
622 @Override
623 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800624 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700625 if (mExternalCacheDirs == null) {
626 mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800627 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700628
629 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700630 return ensureExternalDirsExistOrFilter(mExternalCacheDirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800631 }
632 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700635 public File[] getExternalMediaDirs() {
636 synchronized (mSync) {
637 if (mExternalMediaDirs == null) {
638 mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
639 }
640
641 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700642 return ensureExternalDirsExistOrFilter(mExternalMediaDirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700643 }
644 }
645
646 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 public File getFileStreamPath(String name) {
648 return makeFilename(getFilesDir(), name);
649 }
650
651 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700652 public File getSharedPreferencesPath(String name) {
653 return makeFilename(getPreferencesDir(), name + ".xml");
654 }
655
656 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700658 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660
661 @Override
662 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700663 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665
666 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700667 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
668 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700669 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700670 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700671 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
672 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
673 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
674 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800675 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
676 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
677 }
Jeff Brown47847f32012-03-22 19:13:11 -0700678 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700679 setFilePermissionsFromMode(f.getPath(), mode, 0);
680 return db;
681 }
682
683 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600684 public boolean moveDatabaseFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700685 synchronized (ContextImpl.class) {
686 final File source = sourceContext.getDatabasePath(name);
687 final File target = getDatabasePath(name);
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600688 return moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700689 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700690 }
691 }
692
693 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 public boolean deleteDatabase(String name) {
695 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700696 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800697 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 } catch (Exception e) {
699 }
700 return false;
701 }
702
703 @Override
704 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700705 File dir;
706 File f;
707
708 if (name.charAt(0) == File.separatorChar) {
709 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
710 dir = new File(dirPath);
711 name = name.substring(name.lastIndexOf(File.separatorChar));
712 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700713
714 if (!dir.isDirectory() && dir.mkdir()) {
715 FileUtils.setPermissions(dir.getPath(),
716 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
717 -1, -1);
718 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700719 } else {
720 dir = getDatabasesDir();
721 f = makeFilename(dir, name);
722 }
723
Jeff Sharkey35871f22016-01-29 17:13:29 -0700724 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726
727 @Override
728 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700729 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 private File getDatabasesDir() {
733 synchronized (mSync) {
734 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700735 if ("android".equals(getPackageName())) {
736 mDatabasesDir = new File("/data/system");
737 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700738 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700741 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
743 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800746 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700748 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750
751 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800752 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700753 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700754 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756
757 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800758 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700760 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762
763 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800764 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700766 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
768
769 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800770 @Deprecated
771 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700772 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
774
775 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800776 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700778 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780
781 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800782 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700784 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786
Jeff Brown6e539312015-02-24 18:53:21 -0800787 private WallpaperManager getWallpaperManager() {
788 return getSystemService(WallpaperManager.class);
789 }
790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 @Override
792 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700793 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700794 startActivity(intent, null);
795 }
796
Amith Yamasani82644082012-08-03 13:09:11 -0700797 /** @hide */
798 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700799 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700800 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700801 }
802
Dianne Hackborna4972e92012-03-14 10:38:05 -0700803 @Override
804 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700805 warnIfCallingFromSystemProcess();
Jorim Jaggi2adba072016-03-03 13:43:39 +0100806
807 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
808 // generally not allowed, except if the caller specifies the task id the activity should
809 // be launched in.
810 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
811 && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 throw new AndroidRuntimeException(
813 "Calling startActivity() from outside of an Activity "
814 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
815 + " Is this really what you want?");
816 }
817 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700818 getOuterContext(), mMainThread.getApplicationThread(), null,
819 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
821
Amith Yamasani258848d2012-08-10 17:06:33 -0700822 /** @hide */
823 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700824 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700825 try {
826 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800827 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700828 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700829 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700830 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700831 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700832 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700833 }
834 }
835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800837 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700838 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700839 startActivities(intents, null);
840 }
841
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700842 /** @hide */
843 @Override
844 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
845 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
846 throw new AndroidRuntimeException(
847 "Calling startActivities() from outside of an Activity "
848 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
849 + " Is this really what you want?");
850 }
851 mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700852 getOuterContext(), mMainThread.getApplicationThread(), null,
853 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700854 }
855
Dianne Hackborna4972e92012-03-14 10:38:05 -0700856 @Override
857 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700858 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800859 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
860 throw new AndroidRuntimeException(
861 "Calling startActivities() from outside of an Activity "
862 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
863 + " Is this really what you want?");
864 }
865 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700866 getOuterContext(), mMainThread.getApplicationThread(), null,
867 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800868 }
869
870 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700871 public void startIntentSender(IntentSender intent,
872 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
873 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700874 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
875 }
876
877 @Override
878 public void startIntentSender(IntentSender intent, Intent fillInIntent,
879 int flagsMask, int flagsValues, int extraFlags, Bundle options)
880 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700881 try {
882 String resolvedType = null;
883 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700884 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -0700885 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700886 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
887 }
888 int result = ActivityManagerNative.getDefault()
889 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
890 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700891 0, flagsMask, flagsValues, options);
892 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700893 throw new IntentSender.SendIntentException();
894 }
895 Instrumentation.checkStartActivityResult(result, null);
896 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700897 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700898 }
899 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200900
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700901 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700903 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
905 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700906 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700908 mMainThread.getApplicationThread(), intent, resolvedType, null,
909 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
910 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700912 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914 }
915
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700918 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700920 String[] receiverPermissions = receiverPermission == null ? null
921 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700923 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700925 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700926 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
927 null, false, false, getUserId());
928 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700929 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700930 }
931 }
932
933 @Override
934 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
935 warnIfCallingFromSystemProcess();
936 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
937 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700938 intent.prepareToLeaveProcess(this);
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700939 ActivityManagerNative.getDefault().broadcastIntent(
940 mMainThread.getApplicationThread(), intent, resolvedType, null,
941 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700942 null, false, false, getUserId());
943 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700944 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -0700945 }
946 }
947
948 @Override
949 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
950 warnIfCallingFromSystemProcess();
951 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700952 String[] receiverPermissions = receiverPermission == null ? null
953 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -0700954 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700955 intent.prepareToLeaveProcess(this);
Dianne Hackborna750a632015-06-16 17:18:23 -0700956 ActivityManagerNative.getDefault().broadcastIntent(
957 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700958 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700959 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800960 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700961 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800962 }
963 }
964
965 @Override
966 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
967 warnIfCallingFromSystemProcess();
968 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700969 String[] receiverPermissions = receiverPermission == null ? null
970 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800971 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700972 intent.prepareToLeaveProcess(this);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800973 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700974 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700975 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -0700976 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700978 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980 }
981
982 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -0700983 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700984 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700986 String[] receiverPermissions = receiverPermission == null ? null
987 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700989 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700991 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700992 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700993 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700995 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997 }
998
999 @Override
1000 public void sendOrderedBroadcast(Intent intent,
1001 String receiverPermission, BroadcastReceiver resultReceiver,
1002 Handler scheduler, int initialCode, String initialData,
1003 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001004 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001005 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1006 }
1007
1008 @Override
1009 public void sendOrderedBroadcast(Intent intent,
1010 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
1011 Handler scheduler, int initialCode, String initialData,
1012 Bundle initialExtras) {
1013 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1014 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001015 }
1016
1017 @Override
1018 public void sendOrderedBroadcast(Intent intent,
1019 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1020 Handler scheduler, int initialCode, String initialData,
1021 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001022 sendOrderedBroadcast(intent, receiverPermission, appOp,
1023 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1024 }
1025
1026 void sendOrderedBroadcast(Intent intent,
1027 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1028 Handler scheduler, int initialCode, String initialData,
1029 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001030 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 IIntentReceiver rd = null;
1032 if (resultReceiver != null) {
1033 if (mPackageInfo != null) {
1034 if (scheduler == null) {
1035 scheduler = mMainThread.getHandler();
1036 }
1037 rd = mPackageInfo.getReceiverDispatcher(
1038 resultReceiver, getOuterContext(), scheduler,
1039 mMainThread.getInstrumentation(), false);
1040 } else {
1041 if (scheduler == null) {
1042 scheduler = mMainThread.getHandler();
1043 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001044 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1046 }
1047 }
1048 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001049 String[] receiverPermissions = receiverPermission == null ? null
1050 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001052 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 ActivityManagerNative.getDefault().broadcastIntent(
1054 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001055 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001056 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001058 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060 }
1061
1062 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001063 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001064 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1065 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001066 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001067 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001068 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001069 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001070 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001071 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001072 }
1073 }
1074
1075 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001076 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1077 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001078 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1079 }
1080
1081 @Override
1082 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1083 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001084 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001085 String[] receiverPermissions = receiverPermission == null ? null
1086 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001087 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001088 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001089 ActivityManagerNative.getDefault().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001090 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001091 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001092 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001093 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001094 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001095 }
1096 }
1097
1098 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001099 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001100 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001101 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001102 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001103 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001104 }
1105
1106 @Override
1107 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1108 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001109 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1110 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1111 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1112 }
1113
1114 @Override
1115 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1116 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1117 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001118 IIntentReceiver rd = null;
1119 if (resultReceiver != null) {
1120 if (mPackageInfo != null) {
1121 if (scheduler == null) {
1122 scheduler = mMainThread.getHandler();
1123 }
1124 rd = mPackageInfo.getReceiverDispatcher(
1125 resultReceiver, getOuterContext(), scheduler,
1126 mMainThread.getInstrumentation(), false);
1127 } else {
1128 if (scheduler == null) {
1129 scheduler = mMainThread.getHandler();
1130 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001131 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1132 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001133 }
1134 }
1135 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001136 String[] receiverPermissions = receiverPermission == null ? null
1137 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001138 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001139 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001140 ActivityManagerNative.getDefault().broadcastIntent(
1141 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001142 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001143 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001144 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001145 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001146 }
1147 }
1148
1149 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001150 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001152 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1154 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001155 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 ActivityManagerNative.getDefault().broadcastIntent(
1157 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001158 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001159 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001161 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
1163 }
1164
1165 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001166 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001167 public void sendStickyOrderedBroadcast(Intent intent,
1168 BroadcastReceiver resultReceiver,
1169 Handler scheduler, int initialCode, String initialData,
1170 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001171 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001172 IIntentReceiver rd = null;
1173 if (resultReceiver != null) {
1174 if (mPackageInfo != null) {
1175 if (scheduler == null) {
1176 scheduler = mMainThread.getHandler();
1177 }
1178 rd = mPackageInfo.getReceiverDispatcher(
1179 resultReceiver, getOuterContext(), scheduler,
1180 mMainThread.getInstrumentation(), false);
1181 } else {
1182 if (scheduler == null) {
1183 scheduler = mMainThread.getHandler();
1184 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001185 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001186 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1187 }
1188 }
1189 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1190 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001191 intent.prepareToLeaveProcess(this);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001192 ActivityManagerNative.getDefault().broadcastIntent(
1193 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1194 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001195 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001196 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001197 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001198 }
1199 }
1200
1201 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001202 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 public void removeStickyBroadcast(Intent intent) {
1204 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1205 if (resolvedType != null) {
1206 intent = new Intent(intent);
1207 intent.setDataAndType(intent.getData(), resolvedType);
1208 }
1209 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001210 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001212 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001214 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
1216 }
1217
1218 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001219 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001220 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1221 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1222 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001223 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001224 ActivityManagerNative.getDefault().broadcastIntent(
1225 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001226 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1227 user.getIdentifier());
1228 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001229 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001230 }
1231 }
1232
1233 @Override
1234 @Deprecated
1235 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1236 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1237 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001238 intent.prepareToLeaveProcess(this);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001239 ActivityManagerNative.getDefault().broadcastIntent(
1240 mMainThread.getApplicationThread(), intent, resolvedType, null,
1241 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1242 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001243 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001244 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001245 }
1246 }
1247
1248 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001249 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001250 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1251 UserHandle user, BroadcastReceiver resultReceiver,
1252 Handler scheduler, int initialCode, String initialData,
1253 Bundle initialExtras) {
1254 IIntentReceiver rd = null;
1255 if (resultReceiver != null) {
1256 if (mPackageInfo != null) {
1257 if (scheduler == null) {
1258 scheduler = mMainThread.getHandler();
1259 }
1260 rd = mPackageInfo.getReceiverDispatcher(
1261 resultReceiver, getOuterContext(), scheduler,
1262 mMainThread.getInstrumentation(), false);
1263 } else {
1264 if (scheduler == null) {
1265 scheduler = mMainThread.getHandler();
1266 }
1267 rd = new LoadedApk.ReceiverDispatcher(
1268 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1269 }
1270 }
1271 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1272 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001273 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001274 ActivityManagerNative.getDefault().broadcastIntent(
1275 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1276 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001277 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001278 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001279 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001280 }
1281 }
1282
1283 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001284 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001285 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1286 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1287 if (resolvedType != null) {
1288 intent = new Intent(intent);
1289 intent.setDataAndType(intent.getData(), resolvedType);
1290 }
1291 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001292 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001293 ActivityManagerNative.getDefault().unbroadcastIntent(
1294 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1295 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001296 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001297 }
1298 }
1299
1300 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1302 return registerReceiver(receiver, filter, null, null);
1303 }
1304
1305 @Override
1306 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1307 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001308 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001309 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311
Dianne Hackborn20e80982012-08-31 19:00:44 -07001312 @Override
1313 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1314 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1315 return registerReceiverInternal(receiver, user.getIdentifier(),
1316 filter, broadcastPermission, scheduler, getOuterContext());
1317 }
1318
1319 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 IntentFilter filter, String broadcastPermission,
1321 Handler scheduler, Context context) {
1322 IIntentReceiver rd = null;
1323 if (receiver != null) {
1324 if (mPackageInfo != null && context != null) {
1325 if (scheduler == null) {
1326 scheduler = mMainThread.getHandler();
1327 }
1328 rd = mPackageInfo.getReceiverDispatcher(
1329 receiver, context, scheduler,
1330 mMainThread.getInstrumentation(), true);
1331 } else {
1332 if (scheduler == null) {
1333 scheduler = mMainThread.getHandler();
1334 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001335 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001336 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338 }
1339 try {
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001340 final Intent intent = ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001341 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001342 rd, filter, broadcastPermission, userId);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001343 if (intent != null) {
1344 intent.setExtrasClassLoader(getClassLoader());
1345 intent.prepareToEnterProcess();
1346 }
1347 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001349 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351 }
1352
1353 @Override
1354 public void unregisterReceiver(BroadcastReceiver receiver) {
1355 if (mPackageInfo != null) {
1356 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1357 getOuterContext(), receiver);
1358 try {
1359 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1360 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001361 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
1363 } else {
1364 throw new RuntimeException("Not supported in system context");
1365 }
1366 }
1367
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001368 private void validateServiceIntent(Intent service) {
1369 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001370 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001371 IllegalArgumentException ex = new IllegalArgumentException(
1372 "Service Intent must be explicit: " + service);
1373 throw ex;
1374 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001375 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1376 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001377 }
1378 }
1379 }
1380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 @Override
1382 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001383 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001384 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001385 }
1386
1387 @Override
1388 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001389 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001390 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001391 }
1392
1393 @Override
1394 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001395 return startServiceCommon(service, user);
1396 }
1397
1398 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001400 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001401 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 ComponentName cn = ActivityManagerNative.getDefault().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001403 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
1404 getContentResolver()), getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001405 if (cn != null) {
1406 if (cn.getPackageName().equals("!")) {
1407 throw new SecurityException(
1408 "Not allowed to start service " + service
1409 + " without permission " + cn.getClassName());
1410 } else if (cn.getPackageName().equals("!!")) {
1411 throw new SecurityException(
1412 "Unable to start service " + service
1413 + ": " + cn.getClassName());
1414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 return cn;
1417 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001418 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
1420 }
1421
1422 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001423 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001424 return stopServiceCommon(service, user);
1425 }
1426
1427 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001429 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001430 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 int res = ActivityManagerNative.getDefault().stopService(
1432 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001433 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 if (res < 0) {
1435 throw new SecurityException(
1436 "Not allowed to stop service " + service);
1437 }
1438 return res != 0;
1439 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001440 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
1443
1444 @Override
1445 public boolean bindService(Intent service, ServiceConnection conn,
1446 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001447 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001448 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1449 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001450 }
1451
1452 /** @hide */
1453 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001454 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1455 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001456 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001457 }
1458
Adrian Roos691546e2016-02-09 10:13:41 -08001459 /** @hide */
1460 @Override
1461 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1462 Handler handler, UserHandle user) {
1463 if (handler == null) {
1464 throw new IllegalArgumentException("handler must not be null.");
1465 }
1466 return bindServiceCommon(service, conn, flags, handler, user);
1467 }
1468
1469 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1470 handler, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001472 if (conn == null) {
1473 throw new IllegalArgumentException("connection is null");
1474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 if (mPackageInfo != null) {
Adrian Roos691546e2016-02-09 10:13:41 -08001476 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 } else {
1478 throw new RuntimeException("Not supported in system context");
1479 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001480 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001482 IBinder token = getActivityToken();
1483 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1484 && mPackageInfo.getApplicationInfo().targetSdkVersion
1485 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1486 flags |= BIND_WAIVE_PRIORITY;
1487 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001488 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 int res = ActivityManagerNative.getDefault().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001490 mMainThread.getApplicationThread(), getActivityToken(), service,
1491 service.resolveTypeIfNeeded(getContentResolver()),
1492 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 if (res < 0) {
1494 throw new SecurityException(
1495 "Not allowed to bind to service " + service);
1496 }
1497 return res != 0;
1498 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001499 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 }
1501 }
1502
1503 @Override
1504 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001505 if (conn == null) {
1506 throw new IllegalArgumentException("connection is null");
1507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 if (mPackageInfo != null) {
1509 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1510 getOuterContext(), conn);
1511 try {
1512 ActivityManagerNative.getDefault().unbindService(sd);
1513 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001514 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 }
1516 } else {
1517 throw new RuntimeException("Not supported in system context");
1518 }
1519 }
1520
1521 @Override
1522 public boolean startInstrumentation(ComponentName className,
1523 String profileFile, Bundle arguments) {
1524 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001525 if (arguments != null) {
1526 arguments.setAllowFds(false);
1527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001529 className, profileFile, 0, arguments, null, null, getUserId(),
1530 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001532 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
1535
1536 @Override
1537 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001538 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
1540
Jeff Brown6e539312015-02-24 18:53:21 -08001541 @Override
1542 public String getSystemServiceName(Class<?> serviceClass) {
1543 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001544 }
1545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 @Override
1547 public int checkPermission(String permission, int pid, int uid) {
1548 if (permission == null) {
1549 throw new IllegalArgumentException("permission is null");
1550 }
1551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 try {
1553 return ActivityManagerNative.getDefault().checkPermission(
1554 permission, pid, uid);
1555 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001556 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
1558 }
1559
Dianne Hackbornff170242014-11-19 10:59:01 -08001560 /** @hide */
1561 @Override
1562 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1563 if (permission == null) {
1564 throw new IllegalArgumentException("permission is null");
1565 }
1566
1567 try {
1568 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1569 permission, pid, uid, callerToken);
1570 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001571 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001572 }
1573 }
1574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 @Override
1576 public int checkCallingPermission(String permission) {
1577 if (permission == null) {
1578 throw new IllegalArgumentException("permission is null");
1579 }
1580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 int pid = Binder.getCallingPid();
1582 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001583 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 }
1585 return PackageManager.PERMISSION_DENIED;
1586 }
1587
1588 @Override
1589 public int checkCallingOrSelfPermission(String permission) {
1590 if (permission == null) {
1591 throw new IllegalArgumentException("permission is null");
1592 }
1593
1594 return checkPermission(permission, Binder.getCallingPid(),
1595 Binder.getCallingUid());
1596 }
1597
Svetoslavc6d1c342015-02-26 14:44:43 -08001598 @Override
1599 public int checkSelfPermission(String permission) {
1600 if (permission == null) {
1601 throw new IllegalArgumentException("permission is null");
1602 }
1603
1604 return checkPermission(permission, Process.myPid(), Process.myUid());
1605 }
1606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 private void enforce(
1608 String permission, int resultOfCheck,
1609 boolean selfToo, int uid, String message) {
1610 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1611 throw new SecurityException(
1612 (message != null ? (message + ": ") : "") +
1613 (selfToo
1614 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001615 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 permission +
1617 ".");
1618 }
1619 }
1620
Jeff Brown6e539312015-02-24 18:53:21 -08001621 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 public void enforcePermission(
1623 String permission, int pid, int uid, String message) {
1624 enforce(permission,
1625 checkPermission(permission, pid, uid),
1626 false,
1627 uid,
1628 message);
1629 }
1630
Jeff Brown6e539312015-02-24 18:53:21 -08001631 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 public void enforceCallingPermission(String permission, String message) {
1633 enforce(permission,
1634 checkCallingPermission(permission),
1635 false,
1636 Binder.getCallingUid(),
1637 message);
1638 }
1639
Jeff Brown6e539312015-02-24 18:53:21 -08001640 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 public void enforceCallingOrSelfPermission(
1642 String permission, String message) {
1643 enforce(permission,
1644 checkCallingOrSelfPermission(permission),
1645 true,
1646 Binder.getCallingUid(),
1647 message);
1648 }
1649
1650 @Override
1651 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1652 try {
1653 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001654 mMainThread.getApplicationThread(), toPackage,
1655 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001657 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
1659 }
1660
1661 @Override
1662 public void revokeUriPermission(Uri uri, int modeFlags) {
1663 try {
1664 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001665 mMainThread.getApplicationThread(),
1666 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001668 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
1670 }
1671
1672 @Override
1673 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 try {
1675 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001676 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001677 resolveUserId(uri), null);
1678 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001679 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001680 }
1681 }
1682
1683 /** @hide */
1684 @Override
1685 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1686 try {
1687 return ActivityManagerNative.getDefault().checkUriPermission(
1688 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1689 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001691 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
1693 }
1694
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001695 private int resolveUserId(Uri uri) {
1696 return ContentProvider.getUserIdFromUri(uri, getUserId());
1697 }
1698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 @Override
1700 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 int pid = Binder.getCallingPid();
1702 if (pid != Process.myPid()) {
1703 return checkUriPermission(uri, pid,
1704 Binder.getCallingUid(), modeFlags);
1705 }
1706 return PackageManager.PERMISSION_DENIED;
1707 }
1708
1709 @Override
1710 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1711 return checkUriPermission(uri, Binder.getCallingPid(),
1712 Binder.getCallingUid(), modeFlags);
1713 }
1714
1715 @Override
1716 public int checkUriPermission(Uri uri, String readPermission,
1717 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001718 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1720 + readPermission + " writePermission=" + writePermission
1721 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1722 }
1723 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1724 if (readPermission == null
1725 || checkPermission(readPermission, pid, uid)
1726 == PackageManager.PERMISSION_GRANTED) {
1727 return PackageManager.PERMISSION_GRANTED;
1728 }
1729 }
1730 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1731 if (writePermission == null
1732 || checkPermission(writePermission, pid, uid)
1733 == PackageManager.PERMISSION_GRANTED) {
1734 return PackageManager.PERMISSION_GRANTED;
1735 }
1736 }
1737 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1738 : PackageManager.PERMISSION_DENIED;
1739 }
1740
1741 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001742 StringBuilder builder = new StringBuilder();
1743 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1744 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001746 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1747 builder.append("write and ");
1748 }
1749 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1750 builder.append("persistable and ");
1751 }
1752 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1753 builder.append("prefix and ");
1754 }
1755
1756 if (builder.length() > 5) {
1757 builder.setLength(builder.length() - 5);
1758 return builder.toString();
1759 } else {
1760 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763
1764 private void enforceForUri(
1765 int modeFlags, int resultOfCheck, boolean selfToo,
1766 int uid, Uri uri, String message) {
1767 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1768 throw new SecurityException(
1769 (message != null ? (message + ": ") : "") +
1770 (selfToo
1771 ? "Neither user " + uid + " nor current process has "
1772 : "User " + uid + " does not have ") +
1773 uriModeFlagToString(modeFlags) +
1774 " permission on " +
1775 uri +
1776 ".");
1777 }
1778 }
1779
Jeff Brown6e539312015-02-24 18:53:21 -08001780 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 public void enforceUriPermission(
1782 Uri uri, int pid, int uid, int modeFlags, String message) {
1783 enforceForUri(
1784 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1785 false, uid, uri, message);
1786 }
1787
Jeff Brown6e539312015-02-24 18:53:21 -08001788 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 public void enforceCallingUriPermission(
1790 Uri uri, int modeFlags, String message) {
1791 enforceForUri(
1792 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001793 false,
1794 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796
Jeff Brown6e539312015-02-24 18:53:21 -08001797 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 public void enforceCallingOrSelfUriPermission(
1799 Uri uri, int modeFlags, String message) {
1800 enforceForUri(
1801 modeFlags,
1802 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1803 Binder.getCallingUid(), uri, message);
1804 }
1805
Jeff Brown6e539312015-02-24 18:53:21 -08001806 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 public void enforceUriPermission(
1808 Uri uri, String readPermission, String writePermission,
1809 int pid, int uid, int modeFlags, String message) {
1810 enforceForUri(modeFlags,
1811 checkUriPermission(
1812 uri, readPermission, writePermission, pid, uid,
1813 modeFlags),
1814 false,
1815 uid,
1816 uri,
1817 message);
1818 }
1819
Tom O'Neill365632e2013-09-09 09:34:58 -07001820 /**
1821 * Logs a warning if the system process directly called a method such as
1822 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1823 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1824 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001825 private void warnIfCallingFromSystemProcess() {
1826 if (Process.myUid() == Process.SYSTEM_UID) {
1827 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001828 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001829 }
1830 }
1831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001833 public Context createApplicationContext(ApplicationInfo application, int flags)
1834 throws NameNotFoundException {
1835 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1836 flags | CONTEXT_REGISTER_PACKAGE);
1837 if (pi != null) {
Svetoslav976e8bd2014-07-16 15:12:03 -07001838 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001839 new UserHandle(UserHandle.getUserId(application.uid)), flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001840 mDisplay, null, Display.INVALID_DISPLAY);
Svetoslav976e8bd2014-07-16 15:12:03 -07001841 if (c.mResources != null) {
1842 return c;
1843 }
1844 }
1845
1846 throw new PackageManager.NameNotFoundException(
1847 "Application package " + application.packageName + " not found");
1848 }
1849
1850 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001852 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001853 return createPackageContextAsUser(packageName, flags,
1854 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001855 }
1856
1857 @Override
1858 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1859 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00001860 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001861 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001862 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 }
1864
Jeff Browndefd4a62014-03-10 21:24:37 -07001865 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001866 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 if (pi != null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001868 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001869 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 if (c.mResources != null) {
1871 return c;
1872 }
1873 }
1874
1875 // Should be a better exception.
1876 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001877 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 }
1879
Romain Guy870e09f2009-07-06 16:35:25 -07001880 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001881 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001882 if (overrideConfiguration == null) {
1883 throw new IllegalArgumentException("overrideConfiguration must not be null");
1884 }
1885
Jeff Browndefd4a62014-03-10 21:24:37 -07001886 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001887 mUser, mFlags, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001888 }
1889
1890 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001891 public Context createDisplayContext(Display display) {
1892 if (display == null) {
1893 throw new IllegalArgumentException("display must not be null");
1894 }
1895
Jeff Browndefd4a62014-03-10 21:24:37 -07001896 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001897 mUser, mFlags, display, null, Display.INVALID_DISPLAY);
Jeff Browna492c3a2012-08-23 19:48:44 -07001898 }
1899
Jeff Brown6e539312015-02-24 18:53:21 -08001900 Display getDisplay() {
1901 if (mDisplay != null) {
1902 return mDisplay;
1903 }
Wale Ogunwale26698512015-06-05 16:55:33 -07001904 return ResourcesManager.getInstance().getAdjustedDisplay(
1905 Display.DEFAULT_DISPLAY, mDisplayAdjustments);
Jeff Brown6e539312015-02-24 18:53:21 -08001906 }
1907
Jeff Browna492c3a2012-08-23 19:48:44 -07001908 private int getDisplayId() {
1909 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1910 }
1911
1912 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001913 public Context createDeviceProtectedStorageContext() {
1914 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
1915 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001916 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1917 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1918 }
1919
1920 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001921 public Context createCredentialProtectedStorageContext() {
1922 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
1923 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001924 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1925 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1926 }
1927
1928 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001929 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001930 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
1931 }
1932
1933 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001934 public boolean isDeviceProtectedStorage() {
1935 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001936 }
1937
1938 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001939 public boolean isCredentialProtectedStorage() {
1940 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07001941 }
1942
Jeff Brown98365d72012-08-19 20:30:52 -07001943 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001944 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1945 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -07001946 }
1947
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001948 @Override
1949 public File getDataDir() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001951 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001952 if (isCredentialProtectedStorage()) {
1953 res = mPackageInfo.getCredentialProtectedDataDirFile();
1954 } else if (isDeviceProtectedStorage()) {
1955 res = mPackageInfo.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001956 } else {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001957 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001958 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001959
1960 if (res != null) {
1961 return res;
1962 } else {
1963 throw new RuntimeException(
1964 "No data directory found for package " + getPackageName());
1965 }
1966 } else {
1967 throw new RuntimeException(
1968 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
1971
1972 @Override
1973 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07001974 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001976 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 if (!file.exists()) {
1978 file.mkdir();
1979 setFilePermissionsFromMode(file.getPath(), mode,
1980 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1981 }
1982 return file;
1983 }
1984
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001985 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08001986 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001987 public int getUserId() {
1988 return mUser.getIdentifier();
1989 }
1990
Dianne Hackborn21556372010-02-04 16:34:40 -08001991 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001992 LoadedApk packageInfo = new LoadedApk(mainThread);
1993 ContextImpl context = new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001994 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001995 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07001996 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 return context;
1998 }
1999
Jeff Browndefd4a62014-03-10 21:24:37 -07002000 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
2001 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
2002 return new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002003 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07002004 }
2005
2006 static ContextImpl createActivityContext(ActivityThread mainThread,
Adam Lesinski082614c2016-03-04 14:33:47 -08002007 LoadedApk packageInfo, IBinder activityToken, int displayId,
2008 Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002009 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski082614c2016-03-04 14:33:47 -08002010 return new ContextImpl(null, mainThread, packageInfo, activityToken, null, 0,
Wale Ogunwale26698512015-06-05 16:55:33 -07002011 null, overrideConfiguration, displayId);
Jeff Browndefd4a62014-03-10 21:24:37 -07002012 }
2013
2014 private ContextImpl(ContextImpl container, ActivityThread mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002015 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07002016 Display display, Configuration overrideConfiguration, int createDisplayWithId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002019 // If creator didn't specify which storage to use, use the default
2020 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002021 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2022 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002023 final File dataDir = packageInfo.getDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002024 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
2025 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
2026 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
2027 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002028 }
2029 }
2030
Jeff Browndefd4a62014-03-10 21:24:37 -07002031 mMainThread = mainThread;
2032 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002033 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034
Jeff Browndefd4a62014-03-10 21:24:37 -07002035 if (user == null) {
2036 user = Process.myUserHandle();
2037 }
2038 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07002041 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002042
Wale Ogunwale26698512015-06-05 16:55:33 -07002043 final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
Wale Ogunwalecac3dc62015-06-09 15:35:02 -07002044 ? createDisplayWithId
2045 : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
Wale Ogunwale26698512015-06-05 16:55:33 -07002046
Jeff Browndefd4a62014-03-10 21:24:37 -07002047 CompatibilityInfo compatInfo = null;
2048 if (container != null) {
2049 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
2050 }
Wale Ogunwale9cf99542015-02-18 16:31:34 -08002051 if (compatInfo == null) {
2052 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2053 ? packageInfo.getCompatibilityInfo()
2054 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Jeff Browndefd4a62014-03-10 21:24:37 -07002055 }
2056 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002057 mDisplayAdjustments.setConfiguration(overrideConfiguration);
Jeff Browndefd4a62014-03-10 21:24:37 -07002058
Wale Ogunwale26698512015-06-05 16:55:33 -07002059 mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
2060 : ResourcesManager.getInstance().getAdjustedDisplay(displayId, mDisplayAdjustments);
2061
Jeff Browndefd4a62014-03-10 21:24:37 -07002062 Resources resources = packageInfo.getResources(mainThread);
2063 if (resources != null) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002064 if (displayId != Display.DEFAULT_DISPLAY
Jeff Browndefd4a62014-03-10 21:24:37 -07002065 || overrideConfiguration != null
2066 || (compatInfo != null && compatInfo.applicationScale
2067 != resources.getCompatibilityInfo().applicationScale)) {
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002068
2069 if (container != null) {
2070 // This is a nested Context, so it can't be a base Activity context.
2071 // Just create a regular Resources object associated with the Activity.
2072 resources = mResourcesManager.getResources(
2073 activityToken,
2074 packageInfo.getResDir(),
2075 packageInfo.getSplitResDirs(),
2076 packageInfo.getOverlayDirs(),
2077 packageInfo.getApplicationInfo().sharedLibraryFiles,
2078 displayId,
2079 overrideConfiguration,
2080 compatInfo,
2081 packageInfo.getClassLoader());
2082 } else {
2083 // This is not a nested Context, so it must be the root Activity context.
2084 // All other nested Contexts will inherit the configuration set here.
2085 resources = mResourcesManager.createBaseActivityResources(
2086 activityToken,
2087 packageInfo.getResDir(),
2088 packageInfo.getSplitResDirs(),
2089 packageInfo.getOverlayDirs(),
2090 packageInfo.getApplicationInfo().sharedLibraryFiles,
2091 displayId,
2092 overrideConfiguration,
2093 compatInfo,
2094 packageInfo.getClassLoader());
2095 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002096 }
2097 }
2098 mResources = resources;
2099
2100 if (container != null) {
2101 mBasePackageName = container.mBasePackageName;
2102 mOpPackageName = container.mOpPackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002103 } else {
2104 mBasePackageName = packageInfo.mPackageName;
2105 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2106 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2107 // Special case: system components allow themselves to be loaded in to other
2108 // processes. For purposes of app ops, we must then consider the context as
2109 // belonging to the package of this process, not the system itself, otherwise
2110 // the package+uid verifications in app ops will fail.
2111 mOpPackageName = ActivityThread.currentPackageName();
2112 } else {
2113 mOpPackageName = mBasePackageName;
2114 }
2115 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002116
2117 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
2119
Narayan Kamath29564cd2014-08-07 10:57:40 +01002120 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2121 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123
2124 final void scheduleFinalCleanup(String who, String what) {
2125 mMainThread.scheduleContextCleanup(this, who, what);
2126 }
2127
2128 final void performFinalCleanup(String who, String what) {
2129 //Log.i(TAG, "Cleanup up context: " + this);
2130 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2131 }
2132
2133 final Context getReceiverRestrictedContext() {
2134 if (mReceiverRestrictedContext != null) {
2135 return mReceiverRestrictedContext;
2136 }
2137 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2138 }
2139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 final void setOuterContext(Context context) {
2141 mOuterContext = context;
2142 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 final Context getOuterContext() {
2145 return mOuterContext;
2146 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 final IBinder getActivityToken() {
2149 return mActivityToken;
2150 }
2151
Jeff Sharkey634dc422016-01-30 17:44:15 -07002152 private void checkMode(int mode) {
2153 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2154 if ((mode & MODE_WORLD_READABLE) != 0) {
2155 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2156 }
2157 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2158 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2159 }
2160 }
2161 }
2162
Jeff Brown6e539312015-02-24 18:53:21 -08002163 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002164 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 int extraPermissions) {
2166 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2167 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2168 |extraPermissions;
2169 if ((mode&MODE_WORLD_READABLE) != 0) {
2170 perms |= FileUtils.S_IROTH;
2171 }
2172 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2173 perms |= FileUtils.S_IWOTH;
2174 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002175 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2177 + ", perms=0x" + Integer.toHexString(perms));
2178 }
2179 FileUtils.setPermissions(name, perms, -1, -1);
2180 }
2181
2182 private File makeFilename(File base, String name) {
2183 if (name.indexOf(File.separatorChar) < 0) {
2184 return new File(base, name);
2185 }
2186 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002187 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
2189
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002190 /**
2191 * Ensure that given directories exist, trying to create them if missing. If
2192 * unable to create, they are filtered by replacing with {@code null}.
2193 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002194 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002195 File[] result = new File[dirs.length];
2196 for (int i = 0; i < dirs.length; i++) {
2197 File dir = dirs[i];
2198 if (!dir.exists()) {
2199 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002200 // recheck existence in case of cross-process race
2201 if (!dir.exists()) {
2202 // Failing to mkdir() may be okay, since we might not have
2203 // enough permissions; ask vold to create on our behalf.
2204 final IMountService mount = IMountService.Stub.asInterface(
2205 ServiceManager.getService("mount"));
Christopher Tatecc866da2013-10-02 18:11:01 -07002206 try {
Jeff Sharkey983294592015-07-13 10:25:31 -07002207 final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
2208 if (res != 0) {
2209 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2210 dir = null;
2211 }
2212 } catch (Exception e) {
2213 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002214 dir = null;
2215 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002216 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002217 }
2218 }
2219 result[i] = dir;
2220 }
2221 return result;
2222 }
2223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 // ----------------------------------------------------------------------
2225 // ----------------------------------------------------------------------
2226 // ----------------------------------------------------------------------
2227
2228 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002229 private final ActivityThread mMainThread;
2230 private final UserHandle mUser;
2231
2232 public ApplicationContentResolver(
2233 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002235 mMainThread = Preconditions.checkNotNull(mainThread);
2236 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 }
2238
2239 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002240 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002241 return mMainThread.acquireProvider(context,
2242 ContentProvider.getAuthorityWithoutUserId(auth),
2243 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 }
2245
2246 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002247 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002248 return mMainThread.acquireExistingProvider(context,
2249 ContentProvider.getAuthorityWithoutUserId(auth),
2250 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002251 }
2252
2253 @Override
2254 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002255 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002257
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002258 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002259 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002260 return mMainThread.acquireProvider(c,
2261 ContentProvider.getAuthorityWithoutUserId(auth),
2262 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002263 }
2264
2265 @Override
2266 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002267 return mMainThread.releaseProvider(icp, false);
2268 }
2269
2270 @Override
2271 public void unstableProviderDied(IContentProvider icp) {
2272 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002273 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002274
2275 @Override
2276 public void appNotRespondingViaProvider(IContentProvider icp) {
2277 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2278 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002279
2280 /** @hide */
2281 protected int resolveUserIdFromAuthority(String auth) {
2282 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285}