blob: 3e9b9877ee492c8563ce98206fdaebb5726d0902 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Adam Lesinski4ece3d62016-06-16 18:05:41 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Nicolas Prevotd85fc722014-04-16 19:52:08 +010023import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.ContextWrapper;
27import android.content.IContentProvider;
Jeff Brown6e539312015-02-24 18:53:21 -080028import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Intent;
30import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070031import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.ReceiverCallNotAllowedException;
33import android.content.ServiceConnection;
34import android.content.SharedPreferences;
Adam Lesinski4e862812016-11-21 16:02:24 -080035import android.content.pm.ActivityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070039import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070041import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070042import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070044import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.database.sqlite.SQLiteDatabase;
46import android.database.sqlite.SQLiteDatabase.CursorFactory;
47import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080051import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070053import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080054import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.FileUtils;
56import android.os.Handler;
57import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.ServiceManager;
Adam Lesinski4e862812016-11-21 16:02:24 -080062import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070063import android.os.UserHandle;
Sudheer Shanka2250d562016-11-07 15:41:02 -080064import android.os.storage.IStorageManager;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -070065import android.system.ErrnoException;
66import android.system.Os;
67import android.system.OsConstants;
Adam Lesinski4e862812016-11-21 16:02:24 -080068import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070070import android.util.ArrayMap;
Adam Lesinski4e862812016-11-21 16:02:24 -080071import android.util.IntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070073import android.util.Slog;
Adam Lesinski4e862812016-11-21 16:02:24 -080074import android.util.SparseBooleanArray;
Jeff Brown98365d72012-08-19 20:30:52 -070075import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080076import android.view.DisplayAdjustments;
Dan Egnor95240272009-10-27 18:23:39 -070077
Jeff Sharkey35871f22016-01-29 17:13:29 -070078import com.android.internal.annotations.GuardedBy;
79import com.android.internal.util.Preconditions;
80
Adam Lesinski4e862812016-11-21 16:02:24 -080081import dalvik.system.PathClassLoader;
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.io.File;
84import java.io.FileInputStream;
85import java.io.FileNotFoundException;
86import java.io.FileOutputStream;
Jeff Sharkey35871f22016-01-29 17:13:29 -070087import java.io.FilenameFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.io.IOException;
89import java.io.InputStream;
Adam Lesinski4e862812016-11-21 16:02:24 -080090import java.util.ArrayList;
91import java.util.Arrays;
Jeff Sharkey7a30a302015-12-08 14:20:06 -070092import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094class ReceiverRestrictedContext extends ContextWrapper {
95 ReceiverRestrictedContext(Context base) {
96 super(base);
97 }
98
99 @Override
100 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
101 return registerReceiver(receiver, filter, null, null);
102 }
103
104 @Override
105 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
106 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700107 if (receiver == null) {
108 // Allow retrieving current sticky broadcast; this is safe since we
109 // aren't actually registering a receiver.
110 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
111 } else {
112 throw new ReceiverCallNotAllowedException(
113 "BroadcastReceiver components are not allowed to register to receive intents");
114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
116
117 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700118 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
119 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700120 if (receiver == null) {
121 // Allow retrieving current sticky broadcast; this is safe since we
122 // aren't actually registering a receiver.
123 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
124 } else {
125 throw new ReceiverCallNotAllowedException(
126 "BroadcastReceiver components are not allowed to register to receive intents");
127 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700128 }
129
130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
132 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700133 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
135}
136
137/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800138 * Common implementation of Context API, which provides the base
139 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800141class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800142 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700143 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700145 /**
146 * Map from package name, to preference name, to cached preferences.
147 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700148 @GuardedBy("ContextImpl.class")
149 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
150
151 /**
152 * Map from preference name to generated path.
153 */
154 @GuardedBy("ContextImpl.class")
155 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Jeff Browndefd4a62014-03-10 21:24:37 -0700157 final ActivityThread mMainThread;
158 final LoadedApk mPackageInfo;
Adam Lesinski4e862812016-11-21 16:02:24 -0800159 private ClassLoader mClassLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -0700160
161 private final IBinder mActivityToken;
162
163 private final UserHandle mUser;
164
165 private final ApplicationContentResolver mContentResolver;
166
167 private final String mBasePackageName;
168 private final String mOpPackageName;
169
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700170 private final @NonNull ResourcesManager mResourcesManager;
Adam Lesinski8fa71072016-11-18 18:13:55 -0800171 private @NonNull Resources mResources;
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700172 private @Nullable Display mDisplay; // may be null if default display
Jeff Browndefd4a62014-03-10 21:24:37 -0700173
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700174 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 private int mThemeResource = 0;
178 private Resources.Theme mTheme = null;
179 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
182 private final Object mSync = new Object();
183
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700184 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700186 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700188 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700190 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700191 private File mNoBackupFilesDir;
192 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700194 @GuardedBy("mSync")
195 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700196
Jeff Brown6e539312015-02-24 18:53:21 -0800197 // The system service cache for the system services that are cached per-ContextImpl.
198 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800199
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700200 static ContextImpl getImpl(Context context) {
201 Context nextContext;
202 while ((context instanceof ContextWrapper) &&
203 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
204 context = nextContext;
205 }
206 return (ContextImpl)context;
207 }
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 @Override
210 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700211 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
213
214 @Override
215 public Resources getResources() {
216 return mResources;
217 }
218
219 @Override
220 public PackageManager getPackageManager() {
221 if (mPackageManager != null) {
222 return mPackageManager;
223 }
224
225 IPackageManager pm = ActivityThread.getPackageManager();
226 if (pm != null) {
227 // Doesn't matter if we make more than one instance.
228 return (mPackageManager = new ApplicationPackageManager(this, pm));
229 }
230
231 return null;
232 }
233
234 @Override
235 public ContentResolver getContentResolver() {
236 return mContentResolver;
237 }
238
239 @Override
240 public Looper getMainLooper() {
241 return mMainThread.getLooper();
242 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 @Override
245 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700246 return (mPackageInfo != null) ?
247 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700251 public void setTheme(int resId) {
252 if (mThemeResource != resId) {
253 mThemeResource = resId;
254 initializeTheme();
255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800259 public int getThemeResId() {
260 return mThemeResource;
261 }
262
263 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 public Resources.Theme getTheme() {
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700265 if (mTheme != null) {
266 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700268
269 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
270 getOuterContext().getApplicationInfo().targetSdkVersion);
271 initializeTheme();
272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 return mTheme;
274 }
275
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700276 private void initializeTheme() {
277 if (mTheme == null) {
278 mTheme = mResources.newTheme();
279 }
280 mTheme.applyStyle(mThemeResource, true);
281 }
282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 @Override
284 public ClassLoader getClassLoader() {
Adam Lesinski4e862812016-11-21 16:02:24 -0800285 return mClassLoader != null ? mClassLoader : (mPackageInfo != null ? mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
287
288 @Override
289 public String getPackageName() {
290 if (mPackageInfo != null) {
291 return mPackageInfo.getPackageName();
292 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800293 // No mPackageInfo means this is a Context for the system itself,
294 // and this here is its name.
295 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
297
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800298 /** @hide */
299 @Override
300 public String getBasePackageName() {
301 return mBasePackageName != null ? mBasePackageName : getPackageName();
302 }
303
Dianne Hackborn95d78532013-09-11 09:51:14 -0700304 /** @hide */
305 @Override
306 public String getOpPackageName() {
307 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
308 }
309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700311 public ApplicationInfo getApplicationInfo() {
312 if (mPackageInfo != null) {
313 return mPackageInfo.getApplicationInfo();
314 }
315 throw new RuntimeException("Not supported in system context");
316 }
317
318 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 public String getPackageResourcePath() {
320 if (mPackageInfo != null) {
321 return mPackageInfo.getResDir();
322 }
323 throw new RuntimeException("Not supported in system context");
324 }
325
326 @Override
327 public String getPackageCodePath() {
328 if (mPackageInfo != null) {
329 return mPackageInfo.getAppDir();
330 }
331 throw new RuntimeException("Not supported in system context");
332 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200333
Jeff Brown6e539312015-02-24 18:53:21 -0800334 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700336 // At least one application in the world actually passes in a null
337 // name. This happened to work because when we generated the file name
338 // we would stringify it to "null.xml". Nice.
339 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
340 Build.VERSION_CODES.KITKAT) {
341 if (name == null) {
342 name = "null";
343 }
344 }
345
Jeff Sharkeybe782582016-02-15 18:35:57 -0700346 File file;
347 synchronized (ContextImpl.class) {
348 if (mSharedPrefsPaths == null) {
349 mSharedPrefsPaths = new ArrayMap<>();
350 }
351 file = mSharedPrefsPaths.get(name);
352 if (file == null) {
353 file = getSharedPreferencesPath(name);
354 mSharedPrefsPaths.put(name, file);
355 }
356 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700357 return getSharedPreferences(file, mode);
358 }
359
360 @Override
361 public SharedPreferences getSharedPreferences(File file, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700362 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700364 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700365 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
366 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700367 if (sp == null) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700368 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700369 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700370 return sp;
371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800373 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
374 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
375 // If somebody else (some other process) changed the prefs
376 // file behind our back, we reload it. This has been the
377 // historical (if undocumented) behavior.
378 sp.startReloadIfChangedUnexpectedly();
379 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700380 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382
Jeff Sharkey35871f22016-01-29 17:13:29 -0700383 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700384 if (sSharedPrefsCache == null) {
385 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700386 }
387
388 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700389 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700390 if (packagePrefs == null) {
391 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700392 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700393 }
394
395 return packagePrefs;
396 }
397
398 /**
399 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700400 * requested prefix.
401 *
402 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700403 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600404 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700405 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
406 @Override
407 public boolean accept(File dir, String name) {
408 return name.startsWith(prefix);
409 }
410 });
411
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700412 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700413 for (File sourceFile : sourceFiles) {
414 final File targetFile = new File(targetDir, sourceFile.getName());
415 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
416 try {
417 FileUtils.copyFileOrThrow(sourceFile, targetFile);
418 FileUtils.copyPermissions(sourceFile, targetFile);
419 if (!sourceFile.delete()) {
420 throw new IOException("Failed to clean up " + sourceFile);
421 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700422 if (res != -1) {
423 res++;
424 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700425 } catch (IOException e) {
426 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700427 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700428 }
429 }
430 return res;
431 }
432
433 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600434 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700435 synchronized (ContextImpl.class) {
436 final File source = sourceContext.getSharedPreferencesPath(name);
437 final File target = getSharedPreferencesPath(name);
438
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600439 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700440 source.getName());
441 if (res > 0) {
442 // We moved at least one file, so evict any in-memory caches for
443 // either location
444 final ArrayMap<File, SharedPreferencesImpl> cache =
445 getSharedPreferencesCacheLocked();
446 cache.remove(source);
447 cache.remove(target);
448 }
449 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700450 }
451 }
452
453 @Override
454 public boolean deleteSharedPreferences(String name) {
455 synchronized (ContextImpl.class) {
456 final File prefs = getSharedPreferencesPath(name);
457 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
458
459 // Evict any in-memory caches
460 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
461 cache.remove(prefs);
462
463 prefs.delete();
464 prefsBackup.delete();
465
466 // We failed if files are still lingering
467 return !(prefs.exists() || prefsBackup.exists());
468 }
469 }
470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 private File getPreferencesDir() {
472 synchronized (mSync) {
473 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700474 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700476 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478 }
479
480 @Override
481 public FileInputStream openFileInput(String name)
482 throws FileNotFoundException {
483 File f = makeFilename(getFilesDir(), name);
484 return new FileInputStream(f);
485 }
486
487 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700488 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
489 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 final boolean append = (mode&MODE_APPEND) != 0;
491 File f = makeFilename(getFilesDir(), name);
492 try {
493 FileOutputStream fos = new FileOutputStream(f, append);
494 setFilePermissionsFromMode(f.getPath(), mode, 0);
495 return fos;
496 } catch (FileNotFoundException e) {
497 }
498
499 File parent = f.getParentFile();
500 parent.mkdir();
501 FileUtils.setPermissions(
502 parent.getPath(),
503 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
504 -1, -1);
505 FileOutputStream fos = new FileOutputStream(f, append);
506 setFilePermissionsFromMode(f.getPath(), mode, 0);
507 return fos;
508 }
509
510 @Override
511 public boolean deleteFile(String name) {
512 File f = makeFilename(getFilesDir(), name);
513 return f.delete();
514 }
515
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700516 /**
517 * Common-path handling of app data dir creation
518 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700519 private static File ensurePrivateDirExists(File file) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700520 return ensurePrivateDirExists(file, 0771, -1);
521 }
522
523 private static File ensurePrivateCacheDirExists(File file) {
524 final int gid = UserHandle.getCacheAppGid(Process.myUid());
525 return ensurePrivateDirExists(file, 02771, gid);
526 }
527
528 private static File ensurePrivateDirExists(File file, int mode, int gid) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700529 if (!file.exists()) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700530 final String path = file.getAbsolutePath();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700531 try {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700532 Os.mkdir(path, mode);
533 Os.chmod(path, mode);
534 if (gid != -1) {
535 Os.chown(path, -1, gid);
536 }
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700537 } catch (ErrnoException e) {
538 if (e.errno == OsConstants.EEXIST) {
539 // We must have raced with someone; that's okay
540 } else {
541 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700542 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700543 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700544 }
545 return file;
546 }
547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 @Override
549 public File getFilesDir() {
550 synchronized (mSync) {
551 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700552 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700554 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700555 }
556 }
557
558 @Override
559 public File getNoBackupFilesDir() {
560 synchronized (mSync) {
561 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700562 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700564 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800569 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700570 // Operates on primary external storage
571 return getExternalFilesDirs(type)[0];
572 }
573
574 @Override
575 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800576 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600577 File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700578 if (type != null) {
579 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800580 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700581 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800582 }
583 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200584
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800585 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800586 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700587 // Operates on primary external storage
588 return getObbDirs()[0];
589 }
590
591 @Override
592 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800593 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600594 File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
595 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800596 }
597 }
598
599 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 public File getCacheDir() {
601 synchronized (mSync) {
602 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700603 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700605 return ensurePrivateCacheDirExists(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200608
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800609 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700610 public File getCodeCacheDir() {
611 synchronized (mSync) {
612 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700613 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700614 }
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700615 return ensurePrivateCacheDirExists(mCodeCacheDir);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700616 }
617 }
618
619 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800620 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700621 // Operates on primary external storage
622 return getExternalCacheDirs()[0];
623 }
624
625 @Override
626 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800627 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600628 File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
629 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800630 }
631 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700634 public File[] getExternalMediaDirs() {
635 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600636 File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
637 return ensureExternalDirsExistOrFilter(dirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700638 }
639 }
640
Fyodor Kupolov61221292016-09-02 15:21:03 -0700641 /**
642 * @hide
643 */
644 @Nullable
645 @Override
646 public File getPreloadsFileCache() {
647 return Environment.getDataPreloadsFileCacheDirectory(getPackageName());
648 }
649
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700650 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 public File getFileStreamPath(String name) {
652 return makeFilename(getFilesDir(), name);
653 }
654
655 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700656 public File getSharedPreferencesPath(String name) {
657 return makeFilename(getPreferencesDir(), name + ".xml");
658 }
659
660 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700662 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664
665 @Override
666 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700667 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
669
670 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700671 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
672 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700673 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700674 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700675 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
676 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
677 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
678 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800679 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
680 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
681 }
Jeff Brown47847f32012-03-22 19:13:11 -0700682 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700683 setFilePermissionsFromMode(f.getPath(), mode, 0);
684 return db;
685 }
686
687 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600688 public boolean moveDatabaseFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700689 synchronized (ContextImpl.class) {
690 final File source = sourceContext.getDatabasePath(name);
691 final File target = getDatabasePath(name);
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600692 return moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700693 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700694 }
695 }
696
697 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 public boolean deleteDatabase(String name) {
699 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700700 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800701 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 } catch (Exception e) {
703 }
704 return false;
705 }
706
707 @Override
708 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700709 File dir;
710 File f;
711
712 if (name.charAt(0) == File.separatorChar) {
713 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
714 dir = new File(dirPath);
715 name = name.substring(name.lastIndexOf(File.separatorChar));
716 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700717
718 if (!dir.isDirectory() && dir.mkdir()) {
719 FileUtils.setPermissions(dir.getPath(),
720 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
721 -1, -1);
722 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700723 } else {
724 dir = getDatabasesDir();
725 f = makeFilename(dir, name);
726 }
727
Jeff Sharkey35871f22016-01-29 17:13:29 -0700728 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730
731 @Override
732 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700733 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 private File getDatabasesDir() {
737 synchronized (mSync) {
738 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700739 if ("android".equals(getPackageName())) {
740 mDatabasesDir = new File("/data/system");
741 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700742 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700745 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800750 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700752 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 }
754
755 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800756 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700757 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700758 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760
761 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800762 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700764 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 }
766
767 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800768 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700770 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772
773 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800774 @Deprecated
775 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700776 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
778
779 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800780 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700782 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784
785 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800786 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700788 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
790
Jeff Brown6e539312015-02-24 18:53:21 -0800791 private WallpaperManager getWallpaperManager() {
792 return getSystemService(WallpaperManager.class);
793 }
794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 @Override
796 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700797 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700798 startActivity(intent, null);
799 }
800
Amith Yamasani82644082012-08-03 13:09:11 -0700801 /** @hide */
802 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700803 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700804 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700805 }
806
Dianne Hackborna4972e92012-03-14 10:38:05 -0700807 @Override
808 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700809 warnIfCallingFromSystemProcess();
Jorim Jaggi2adba072016-03-03 13:43:39 +0100810
811 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
812 // generally not allowed, except if the caller specifies the task id the activity should
813 // be launched in.
814 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
815 && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 throw new AndroidRuntimeException(
817 "Calling startActivity() from outside of an Activity "
818 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
819 + " Is this really what you want?");
820 }
821 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700822 getOuterContext(), mMainThread.getApplicationThread(), null,
823 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
825
Amith Yamasani258848d2012-08-10 17:06:33 -0700826 /** @hide */
827 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700828 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700829 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800830 ActivityManager.getService().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800831 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700832 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700833 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700834 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700835 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700836 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700837 }
838 }
839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800841 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700842 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700843 startActivities(intents, null);
844 }
845
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700846 /** @hide */
847 @Override
848 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
849 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
850 throw new AndroidRuntimeException(
851 "Calling startActivities() from outside of an Activity "
852 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
853 + " Is this really what you want?");
854 }
855 mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700856 getOuterContext(), mMainThread.getApplicationThread(), null,
857 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700858 }
859
Dianne Hackborna4972e92012-03-14 10:38:05 -0700860 @Override
861 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700862 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800863 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
864 throw new AndroidRuntimeException(
865 "Calling startActivities() from outside of an Activity "
866 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
867 + " Is this really what you want?");
868 }
869 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700870 getOuterContext(), mMainThread.getApplicationThread(), null,
871 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800872 }
873
874 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700875 public void startIntentSender(IntentSender intent,
876 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
877 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700878 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
879 }
880
881 @Override
882 public void startIntentSender(IntentSender intent, Intent fillInIntent,
883 int flagsMask, int flagsValues, int extraFlags, Bundle options)
884 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700885 try {
886 String resolvedType = null;
887 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700888 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -0700889 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700890 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
891 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800892 int result = ActivityManager.getService()
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700893 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
894 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700895 0, flagsMask, flagsValues, options);
896 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700897 throw new IntentSender.SendIntentException();
898 }
899 Instrumentation.checkStartActivityResult(result, null);
900 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700901 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700902 }
903 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200904
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700905 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700907 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
909 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700910 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800911 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700912 mMainThread.getApplicationThread(), intent, resolvedType, null,
913 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
914 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700916 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
918 }
919
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800920 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700922 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700924 String[] receiverPermissions = receiverPermission == null ? null
925 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700927 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800928 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700929 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700930 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
931 null, false, false, getUserId());
932 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700933 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700934 }
935 }
936
937 @Override
938 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
939 warnIfCallingFromSystemProcess();
940 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
941 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700942 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800943 ActivityManager.getService().broadcastIntent(
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700944 mMainThread.getApplicationThread(), intent, resolvedType, null,
945 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700946 null, false, false, getUserId());
947 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700948 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -0700949 }
950 }
951
952 @Override
953 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
954 warnIfCallingFromSystemProcess();
955 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700956 String[] receiverPermissions = receiverPermission == null ? null
957 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -0700958 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700959 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800960 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700961 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700962 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700963 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800964 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700965 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800966 }
967 }
968
969 @Override
970 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
971 warnIfCallingFromSystemProcess();
972 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700973 String[] receiverPermissions = receiverPermission == null ? null
974 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800975 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700976 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800977 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700978 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700979 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -0700980 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700982 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984 }
985
986 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -0700987 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700988 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700990 String[] receiverPermissions = receiverPermission == null ? null
991 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700993 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800994 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700995 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700996 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700997 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700999 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001 }
1002
1003 @Override
1004 public void sendOrderedBroadcast(Intent intent,
1005 String receiverPermission, BroadcastReceiver resultReceiver,
1006 Handler scheduler, int initialCode, String initialData,
1007 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001008 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001009 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1010 }
1011
1012 @Override
1013 public void sendOrderedBroadcast(Intent intent,
1014 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
1015 Handler scheduler, int initialCode, String initialData,
1016 Bundle initialExtras) {
1017 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1018 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001019 }
1020
1021 @Override
1022 public void sendOrderedBroadcast(Intent intent,
1023 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1024 Handler scheduler, int initialCode, String initialData,
1025 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001026 sendOrderedBroadcast(intent, receiverPermission, appOp,
1027 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1028 }
1029
1030 void sendOrderedBroadcast(Intent intent,
1031 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1032 Handler scheduler, int initialCode, String initialData,
1033 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001034 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 IIntentReceiver rd = null;
1036 if (resultReceiver != null) {
1037 if (mPackageInfo != null) {
1038 if (scheduler == null) {
1039 scheduler = mMainThread.getHandler();
1040 }
1041 rd = mPackageInfo.getReceiverDispatcher(
1042 resultReceiver, getOuterContext(), scheduler,
1043 mMainThread.getInstrumentation(), false);
1044 } else {
1045 if (scheduler == null) {
1046 scheduler = mMainThread.getHandler();
1047 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001048 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1050 }
1051 }
1052 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001053 String[] receiverPermissions = receiverPermission == null ? null
1054 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001056 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001057 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001059 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001060 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001062 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 }
1065
1066 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001067 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001068 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1069 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001070 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001071 ActivityManager.getService().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001072 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001073 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001074 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001075 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001076 }
1077 }
1078
1079 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001080 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1081 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001082 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1083 }
1084
1085 @Override
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001086 public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission,
1087 Bundle options) {
1088 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1089 String[] receiverPermissions = receiverPermission == null ? null
1090 : new String[] {receiverPermission};
1091 try {
1092 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001093 ActivityManager.getService().broadcastIntent(
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001094 mMainThread.getApplicationThread(), intent, resolvedType, null,
1095 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1096 options, false, false, user.getIdentifier());
1097 } catch (RemoteException e) {
1098 throw e.rethrowFromSystemServer();
1099 }
1100 }
1101
1102 @Override
Svet Ganov16a16892015-04-16 10:32:04 -07001103 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1104 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001105 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001106 String[] receiverPermissions = receiverPermission == null ? null
1107 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001108 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001109 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001110 ActivityManager.getService().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001111 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001112 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001113 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001114 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001115 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001116 }
1117 }
1118
1119 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001120 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001121 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001122 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001123 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001124 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001125 }
1126
1127 @Override
1128 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1129 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001130 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1131 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1132 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1133 }
1134
1135 @Override
1136 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1137 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1138 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001139 IIntentReceiver rd = null;
1140 if (resultReceiver != null) {
1141 if (mPackageInfo != null) {
1142 if (scheduler == null) {
1143 scheduler = mMainThread.getHandler();
1144 }
1145 rd = mPackageInfo.getReceiverDispatcher(
1146 resultReceiver, getOuterContext(), scheduler,
1147 mMainThread.getInstrumentation(), false);
1148 } else {
1149 if (scheduler == null) {
1150 scheduler = mMainThread.getHandler();
1151 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001152 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1153 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001154 }
1155 }
1156 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001157 String[] receiverPermissions = receiverPermission == null ? null
1158 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001159 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001160 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001161 ActivityManager.getService().broadcastIntent(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001162 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001163 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001164 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001165 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001166 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001167 }
1168 }
1169
1170 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001171 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001173 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1175 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001176 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001177 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001179 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001180 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001182 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184 }
1185
1186 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001187 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001188 public void sendStickyOrderedBroadcast(Intent intent,
1189 BroadcastReceiver resultReceiver,
1190 Handler scheduler, int initialCode, String initialData,
1191 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001192 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001193 IIntentReceiver rd = null;
1194 if (resultReceiver != null) {
1195 if (mPackageInfo != null) {
1196 if (scheduler == null) {
1197 scheduler = mMainThread.getHandler();
1198 }
1199 rd = mPackageInfo.getReceiverDispatcher(
1200 resultReceiver, getOuterContext(), scheduler,
1201 mMainThread.getInstrumentation(), false);
1202 } else {
1203 if (scheduler == null) {
1204 scheduler = mMainThread.getHandler();
1205 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001206 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001207 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1208 }
1209 }
1210 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1211 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001212 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001213 ActivityManager.getService().broadcastIntent(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001214 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1215 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001216 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001217 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001218 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001219 }
1220 }
1221
1222 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001223 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 public void removeStickyBroadcast(Intent intent) {
1225 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1226 if (resolvedType != null) {
1227 intent = new Intent(intent);
1228 intent.setDataAndType(intent.getData(), resolvedType);
1229 }
1230 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001231 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001232 ActivityManager.getService().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001233 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001235 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237 }
1238
1239 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001240 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001241 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1242 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1243 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001244 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001245 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001246 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001247 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1248 user.getIdentifier());
1249 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001250 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001251 }
1252 }
1253
1254 @Override
1255 @Deprecated
1256 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1257 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1258 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001259 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001260 ActivityManager.getService().broadcastIntent(
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001261 mMainThread.getApplicationThread(), intent, resolvedType, null,
1262 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1263 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001264 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001265 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001266 }
1267 }
1268
1269 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001270 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001271 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1272 UserHandle user, BroadcastReceiver resultReceiver,
1273 Handler scheduler, int initialCode, String initialData,
1274 Bundle initialExtras) {
1275 IIntentReceiver rd = null;
1276 if (resultReceiver != null) {
1277 if (mPackageInfo != null) {
1278 if (scheduler == null) {
1279 scheduler = mMainThread.getHandler();
1280 }
1281 rd = mPackageInfo.getReceiverDispatcher(
1282 resultReceiver, getOuterContext(), scheduler,
1283 mMainThread.getInstrumentation(), false);
1284 } else {
1285 if (scheduler == null) {
1286 scheduler = mMainThread.getHandler();
1287 }
1288 rd = new LoadedApk.ReceiverDispatcher(
1289 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1290 }
1291 }
1292 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1293 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001294 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001295 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001296 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1297 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001298 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001299 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001300 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001301 }
1302 }
1303
1304 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001305 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001306 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1307 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1308 if (resolvedType != null) {
1309 intent = new Intent(intent);
1310 intent.setDataAndType(intent.getData(), resolvedType);
1311 }
1312 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001313 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001314 ActivityManager.getService().unbroadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001315 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1316 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001317 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001318 }
1319 }
1320
1321 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1323 return registerReceiver(receiver, filter, null, null);
1324 }
1325
1326 @Override
1327 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1328 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001329 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001330 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332
Dianne Hackborn20e80982012-08-31 19:00:44 -07001333 @Override
1334 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1335 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1336 return registerReceiverInternal(receiver, user.getIdentifier(),
1337 filter, broadcastPermission, scheduler, getOuterContext());
1338 }
1339
1340 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 IntentFilter filter, String broadcastPermission,
1342 Handler scheduler, Context context) {
1343 IIntentReceiver rd = null;
1344 if (receiver != null) {
1345 if (mPackageInfo != null && context != null) {
1346 if (scheduler == null) {
1347 scheduler = mMainThread.getHandler();
1348 }
1349 rd = mPackageInfo.getReceiverDispatcher(
1350 receiver, context, scheduler,
1351 mMainThread.getInstrumentation(), true);
1352 } else {
1353 if (scheduler == null) {
1354 scheduler = mMainThread.getHandler();
1355 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001356 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001357 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 }
1360 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001361 final Intent intent = ActivityManager.getService().registerReceiver(
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001362 mMainThread.getApplicationThread(), mBasePackageName, rd, filter,
1363 broadcastPermission, userId);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001364 if (intent != null) {
1365 intent.setExtrasClassLoader(getClassLoader());
1366 intent.prepareToEnterProcess();
1367 }
1368 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001370 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372 }
1373
1374 @Override
1375 public void unregisterReceiver(BroadcastReceiver receiver) {
1376 if (mPackageInfo != null) {
1377 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1378 getOuterContext(), receiver);
1379 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001380 ActivityManager.getService().unregisterReceiver(rd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001382 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
1384 } else {
1385 throw new RuntimeException("Not supported in system context");
1386 }
1387 }
1388
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001389 private void validateServiceIntent(Intent service) {
1390 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001391 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001392 IllegalArgumentException ex = new IllegalArgumentException(
1393 "Service Intent must be explicit: " + service);
1394 throw ex;
1395 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001396 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1397 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001398 }
1399 }
1400 }
1401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 @Override
1403 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001404 warnIfCallingFromSystemProcess();
Christopher Tate42a386b2016-11-07 12:21:21 -08001405 return startServiceCommon(service, -1, null, mUser);
1406 }
1407
1408 @Override
1409 public ComponentName startServiceInForeground(Intent service,
1410 int id, Notification notification) {
1411 warnIfCallingFromSystemProcess();
1412 return startServiceCommon(service, id, notification, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001413 }
1414
1415 @Override
1416 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001417 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001418 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001419 }
1420
1421 @Override
1422 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Christopher Tate42a386b2016-11-07 12:21:21 -08001423 return startServiceCommon(service, -1, null, user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001424 }
1425
Christopher Tate42a386b2016-11-07 12:21:21 -08001426 @Override
1427 public ComponentName startServiceInForegroundAsUser(Intent service,
1428 int id, Notification notification, UserHandle user) {
1429 return startServiceCommon(service, id, notification, user);
1430 }
1431
1432 private ComponentName startServiceCommon(Intent service, int id, Notification notification,
1433 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001435 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001436 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001437 ComponentName cn = ActivityManager.getService().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001438 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
Christopher Tate42a386b2016-11-07 12:21:21 -08001439 getContentResolver()), id, notification, getOpPackageName(),
1440 user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001441 if (cn != null) {
1442 if (cn.getPackageName().equals("!")) {
1443 throw new SecurityException(
1444 "Not allowed to start service " + service
1445 + " without permission " + cn.getClassName());
1446 } else if (cn.getPackageName().equals("!!")) {
1447 throw new SecurityException(
1448 "Unable to start service " + service
1449 + ": " + cn.getClassName());
Dianne Hackborn85e35642017-01-12 15:10:57 -08001450 } else if (cn.getPackageName().equals("?")) {
1451 throw new IllegalStateException(
1452 "Not allowed to start service " + service + ": " + cn.getClassName());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455 return cn;
1456 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001457 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
1459 }
1460
1461 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001462 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001463 return stopServiceCommon(service, user);
1464 }
1465
1466 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001468 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001469 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001470 int res = ActivityManager.getService().stopService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001472 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 if (res < 0) {
1474 throw new SecurityException(
1475 "Not allowed to stop service " + service);
1476 }
1477 return res != 0;
1478 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001479 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481 }
1482
1483 @Override
1484 public boolean bindService(Intent service, ServiceConnection conn,
1485 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001486 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001487 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1488 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001489 }
1490
1491 /** @hide */
1492 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001493 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1494 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001495 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001496 }
1497
Adrian Roos691546e2016-02-09 10:13:41 -08001498 /** @hide */
1499 @Override
1500 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1501 Handler handler, UserHandle user) {
1502 if (handler == null) {
1503 throw new IllegalArgumentException("handler must not be null.");
1504 }
1505 return bindServiceCommon(service, conn, flags, handler, user);
1506 }
1507
Tony Mak46aabe52016-11-14 12:53:06 +00001508 /** @hide */
1509 @Override
1510 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler,
1511 int flags) {
1512 return mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
1513 }
1514
1515 /** @hide */
1516 @Override
1517 public IApplicationThread getIApplicationThread() {
1518 return mMainThread.getApplicationThread();
1519 }
1520
Tony Makbf9928d2016-12-22 11:02:45 +00001521 /** @hide */
1522 @Override
1523 public Handler getMainThreadHandler() {
1524 return mMainThread.getHandler();
1525 }
1526
Adrian Roos691546e2016-02-09 10:13:41 -08001527 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1528 handler, UserHandle user) {
Tony Mak46aabe52016-11-14 12:53:06 +00001529 // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001531 if (conn == null) {
1532 throw new IllegalArgumentException("connection is null");
1533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 if (mPackageInfo != null) {
Adrian Roos691546e2016-02-09 10:13:41 -08001535 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 } else {
1537 throw new RuntimeException("Not supported in system context");
1538 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001539 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001541 IBinder token = getActivityToken();
1542 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1543 && mPackageInfo.getApplicationInfo().targetSdkVersion
1544 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1545 flags |= BIND_WAIVE_PRIORITY;
1546 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001547 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001548 int res = ActivityManager.getService().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001549 mMainThread.getApplicationThread(), getActivityToken(), service,
1550 service.resolveTypeIfNeeded(getContentResolver()),
1551 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 if (res < 0) {
1553 throw new SecurityException(
1554 "Not allowed to bind to service " + service);
1555 }
1556 return res != 0;
1557 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001558 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
1560 }
1561
1562 @Override
1563 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001564 if (conn == null) {
1565 throw new IllegalArgumentException("connection is null");
1566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 if (mPackageInfo != null) {
1568 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1569 getOuterContext(), conn);
1570 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001571 ActivityManager.getService().unbindService(sd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001573 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575 } else {
1576 throw new RuntimeException("Not supported in system context");
1577 }
1578 }
1579
1580 @Override
1581 public boolean startInstrumentation(ComponentName className,
1582 String profileFile, Bundle arguments) {
1583 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001584 if (arguments != null) {
1585 arguments.setAllowFds(false);
1586 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001587 return ActivityManager.getService().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001588 className, profileFile, 0, arguments, null, null, getUserId(),
1589 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001591 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
1594
1595 @Override
1596 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001597 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
1599
Jeff Brown6e539312015-02-24 18:53:21 -08001600 @Override
1601 public String getSystemServiceName(Class<?> serviceClass) {
1602 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001603 }
1604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 @Override
1606 public int checkPermission(String permission, int pid, int uid) {
1607 if (permission == null) {
1608 throw new IllegalArgumentException("permission is null");
1609 }
1610
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001611 final IActivityManager am = ActivityManager.getService();
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001612 if (am == null) {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001613 // Well this is super awkward; we somehow don't have an active
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001614 // ActivityManager instance. If we're testing a root or system
1615 // UID, then they totally have whatever permission this is.
1616 final int appId = UserHandle.getAppId(uid);
1617 if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) {
1618 Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " holds " + permission);
1619 return PackageManager.PERMISSION_GRANTED;
1620 }
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001621 }
1622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 try {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001624 return am.checkPermission(permission, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001626 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 }
1628 }
1629
Dianne Hackbornff170242014-11-19 10:59:01 -08001630 /** @hide */
1631 @Override
1632 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1633 if (permission == null) {
1634 throw new IllegalArgumentException("permission is null");
1635 }
1636
1637 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001638 return ActivityManager.getService().checkPermissionWithToken(
Dianne Hackbornff170242014-11-19 10:59:01 -08001639 permission, pid, uid, callerToken);
1640 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001641 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001642 }
1643 }
1644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 @Override
1646 public int checkCallingPermission(String permission) {
1647 if (permission == null) {
1648 throw new IllegalArgumentException("permission is null");
1649 }
1650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 int pid = Binder.getCallingPid();
1652 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001653 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
1655 return PackageManager.PERMISSION_DENIED;
1656 }
1657
1658 @Override
1659 public int checkCallingOrSelfPermission(String permission) {
1660 if (permission == null) {
1661 throw new IllegalArgumentException("permission is null");
1662 }
1663
1664 return checkPermission(permission, Binder.getCallingPid(),
1665 Binder.getCallingUid());
1666 }
1667
Svetoslavc6d1c342015-02-26 14:44:43 -08001668 @Override
1669 public int checkSelfPermission(String permission) {
1670 if (permission == null) {
1671 throw new IllegalArgumentException("permission is null");
1672 }
1673
1674 return checkPermission(permission, Process.myPid(), Process.myUid());
1675 }
1676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 private void enforce(
1678 String permission, int resultOfCheck,
1679 boolean selfToo, int uid, String message) {
1680 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1681 throw new SecurityException(
1682 (message != null ? (message + ": ") : "") +
1683 (selfToo
1684 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001685 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 permission +
1687 ".");
1688 }
1689 }
1690
Jeff Brown6e539312015-02-24 18:53:21 -08001691 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 public void enforcePermission(
1693 String permission, int pid, int uid, String message) {
1694 enforce(permission,
1695 checkPermission(permission, pid, uid),
1696 false,
1697 uid,
1698 message);
1699 }
1700
Jeff Brown6e539312015-02-24 18:53:21 -08001701 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 public void enforceCallingPermission(String permission, String message) {
1703 enforce(permission,
1704 checkCallingPermission(permission),
1705 false,
1706 Binder.getCallingUid(),
1707 message);
1708 }
1709
Jeff Brown6e539312015-02-24 18:53:21 -08001710 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 public void enforceCallingOrSelfPermission(
1712 String permission, String message) {
1713 enforce(permission,
1714 checkCallingOrSelfPermission(permission),
1715 true,
1716 Binder.getCallingUid(),
1717 message);
1718 }
1719
1720 @Override
1721 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1722 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001723 ActivityManager.getService().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001724 mMainThread.getApplicationThread(), toPackage,
1725 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001727 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729 }
1730
1731 @Override
1732 public void revokeUriPermission(Uri uri, int modeFlags) {
1733 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001734 ActivityManager.getService().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001735 mMainThread.getApplicationThread(),
1736 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001738 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
1740 }
1741
1742 @Override
1743 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001745 return ActivityManager.getService().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001746 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001747 resolveUserId(uri), null);
1748 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001749 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001750 }
1751 }
1752
1753 /** @hide */
1754 @Override
1755 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1756 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001757 return ActivityManager.getService().checkUriPermission(
Dianne Hackbornff170242014-11-19 10:59:01 -08001758 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1759 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001761 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763 }
1764
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001765 private int resolveUserId(Uri uri) {
1766 return ContentProvider.getUserIdFromUri(uri, getUserId());
1767 }
1768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 @Override
1770 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 int pid = Binder.getCallingPid();
1772 if (pid != Process.myPid()) {
1773 return checkUriPermission(uri, pid,
1774 Binder.getCallingUid(), modeFlags);
1775 }
1776 return PackageManager.PERMISSION_DENIED;
1777 }
1778
1779 @Override
1780 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1781 return checkUriPermission(uri, Binder.getCallingPid(),
1782 Binder.getCallingUid(), modeFlags);
1783 }
1784
1785 @Override
1786 public int checkUriPermission(Uri uri, String readPermission,
1787 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001788 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1790 + readPermission + " writePermission=" + writePermission
1791 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1792 }
1793 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1794 if (readPermission == null
1795 || checkPermission(readPermission, pid, uid)
1796 == PackageManager.PERMISSION_GRANTED) {
1797 return PackageManager.PERMISSION_GRANTED;
1798 }
1799 }
1800 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1801 if (writePermission == null
1802 || checkPermission(writePermission, pid, uid)
1803 == PackageManager.PERMISSION_GRANTED) {
1804 return PackageManager.PERMISSION_GRANTED;
1805 }
1806 }
1807 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1808 : PackageManager.PERMISSION_DENIED;
1809 }
1810
1811 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001812 StringBuilder builder = new StringBuilder();
1813 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1814 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001816 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1817 builder.append("write and ");
1818 }
1819 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1820 builder.append("persistable and ");
1821 }
1822 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1823 builder.append("prefix and ");
1824 }
1825
1826 if (builder.length() > 5) {
1827 builder.setLength(builder.length() - 5);
1828 return builder.toString();
1829 } else {
1830 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833
1834 private void enforceForUri(
1835 int modeFlags, int resultOfCheck, boolean selfToo,
1836 int uid, Uri uri, String message) {
1837 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1838 throw new SecurityException(
1839 (message != null ? (message + ": ") : "") +
1840 (selfToo
1841 ? "Neither user " + uid + " nor current process has "
1842 : "User " + uid + " does not have ") +
1843 uriModeFlagToString(modeFlags) +
1844 " permission on " +
1845 uri +
1846 ".");
1847 }
1848 }
1849
Jeff Brown6e539312015-02-24 18:53:21 -08001850 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 public void enforceUriPermission(
1852 Uri uri, int pid, int uid, int modeFlags, String message) {
1853 enforceForUri(
1854 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1855 false, uid, uri, message);
1856 }
1857
Jeff Brown6e539312015-02-24 18:53:21 -08001858 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 public void enforceCallingUriPermission(
1860 Uri uri, int modeFlags, String message) {
1861 enforceForUri(
1862 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001863 false,
1864 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 }
1866
Jeff Brown6e539312015-02-24 18:53:21 -08001867 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 public void enforceCallingOrSelfUriPermission(
1869 Uri uri, int modeFlags, String message) {
1870 enforceForUri(
1871 modeFlags,
1872 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1873 Binder.getCallingUid(), uri, message);
1874 }
1875
Jeff Brown6e539312015-02-24 18:53:21 -08001876 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 public void enforceUriPermission(
1878 Uri uri, String readPermission, String writePermission,
1879 int pid, int uid, int modeFlags, String message) {
1880 enforceForUri(modeFlags,
1881 checkUriPermission(
1882 uri, readPermission, writePermission, pid, uid,
1883 modeFlags),
1884 false,
1885 uid,
1886 uri,
1887 message);
1888 }
1889
Tom O'Neill365632e2013-09-09 09:34:58 -07001890 /**
1891 * Logs a warning if the system process directly called a method such as
1892 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1893 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1894 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001895 private void warnIfCallingFromSystemProcess() {
1896 if (Process.myUid() == Process.SYSTEM_UID) {
1897 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001898 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001899 }
1900 }
1901
Adam Lesinski8fa71072016-11-18 18:13:55 -08001902 private static Resources createResources(IBinder activityToken, LoadedApk pi, int displayId,
1903 Configuration overrideConfig, CompatibilityInfo compatInfo) {
1904 return ResourcesManager.getInstance().getResources(activityToken,
1905 pi.getResDir(),
1906 pi.getSplitResDirs(),
1907 pi.getOverlayDirs(),
Adam Lesinski0e618832017-02-07 11:40:12 -08001908 pi.getApplicationInfo().sharedLibraryFiles,
Adam Lesinski8fa71072016-11-18 18:13:55 -08001909 displayId,
1910 overrideConfig,
1911 compatInfo,
1912 pi.getClassLoader());
1913 }
1914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001916 public Context createApplicationContext(ApplicationInfo application, int flags)
1917 throws NameNotFoundException {
1918 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1919 flags | CONTEXT_REGISTER_PACKAGE);
1920 if (pi != null) {
Svetoslav976e8bd2014-07-16 15:12:03 -07001921 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Adam Lesinski4e862812016-11-21 16:02:24 -08001922 new UserHandle(UserHandle.getUserId(application.uid)), flags,
1923 null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08001924
1925 final int displayId = mDisplay != null
1926 ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1927
1928 c.mResources = createResources(mActivityToken, pi, displayId, null,
1929 getDisplayAdjustments(displayId).getCompatibilityInfo());
Svetoslav976e8bd2014-07-16 15:12:03 -07001930 if (c.mResources != null) {
1931 return c;
1932 }
1933 }
1934
1935 throw new PackageManager.NameNotFoundException(
1936 "Application package " + application.packageName + " not found");
1937 }
1938
1939 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001941 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001942 return createPackageContextAsUser(packageName, flags,
1943 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001944 }
1945
1946 @Override
1947 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1948 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00001949 if (packageName.equals("system") || packageName.equals("android")) {
Adam Lesinski8fa71072016-11-18 18:13:55 -08001950 // The system resources are loaded in every application, so we can safely copy
1951 // the context without reloading Resources.
Adam Lesinski4e862812016-11-21 16:02:24 -08001952 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, user, flags,
1953 null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
1955
Jeff Browndefd4a62014-03-10 21:24:37 -07001956 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001957 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 if (pi != null) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001959 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken, user, flags,
1960 null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08001961
1962 final int displayId = mDisplay != null
1963 ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1964
1965 c.mResources = createResources(mActivityToken, pi, displayId, null,
1966 getDisplayAdjustments(displayId).getCompatibilityInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 if (c.mResources != null) {
1968 return c;
1969 }
1970 }
1971
1972 // Should be a better exception.
1973 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001974 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
1976
Romain Guy870e09f2009-07-06 16:35:25 -07001977 @Override
Adam Lesinski4e862812016-11-21 16:02:24 -08001978 public Context createContextForSplit(String splitName) throws NameNotFoundException {
1979 if (!mPackageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
1980 // All Splits are always loaded.
1981 return this;
1982 }
1983
1984 final ClassLoader classLoader = mPackageInfo.getSplitClassLoader(splitName);
1985 final String[] paths = mPackageInfo.getSplitPaths(splitName);
1986
1987 final ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo,
1988 mActivityToken, mUser, mFlags, classLoader);
1989
1990 final int displayId = mDisplay != null
1991 ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1992
1993 context.mResources = ResourcesManager.getInstance().getResources(
1994 mActivityToken,
1995 mPackageInfo.getResDir(),
1996 paths,
1997 mPackageInfo.getOverlayDirs(),
1998 mPackageInfo.getApplicationInfo().sharedLibraryFiles,
1999 displayId,
2000 null,
2001 mPackageInfo.getCompatibilityInfo(),
2002 classLoader);
2003 return context;
2004 }
2005
2006 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07002007 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07002008 if (overrideConfiguration == null) {
2009 throw new IllegalArgumentException("overrideConfiguration must not be null");
2010 }
2011
Adam Lesinski8fa71072016-11-18 18:13:55 -08002012 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Adam Lesinski4e862812016-11-21 16:02:24 -08002013 mUser, mFlags, mClassLoader);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002014
2015 final int displayId = mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
2016 context.mResources = createResources(mActivityToken, mPackageInfo, displayId,
2017 overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo());
2018 return context;
Dianne Hackborn756220b2012-08-14 16:45:30 -07002019 }
2020
2021 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07002022 public Context createDisplayContext(Display display) {
2023 if (display == null) {
2024 throw new IllegalArgumentException("display must not be null");
2025 }
2026
Adam Lesinski8fa71072016-11-18 18:13:55 -08002027 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Adam Lesinski4e862812016-11-21 16:02:24 -08002028 mUser, mFlags, mClassLoader);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002029
2030 final int displayId = display.getDisplayId();
2031 context.mResources = createResources(mActivityToken, mPackageInfo, displayId, null,
2032 getDisplayAdjustments(displayId).getCompatibilityInfo());
2033 context.mDisplay = display;
2034 return context;
Jeff Browna492c3a2012-08-23 19:48:44 -07002035 }
2036
Jeff Browna492c3a2012-08-23 19:48:44 -07002037 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002038 public Context createDeviceProtectedStorageContext() {
2039 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
2040 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Adam Lesinski4e862812016-11-21 16:02:24 -08002041 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, mUser, flags,
2042 mClassLoader);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002043 }
2044
2045 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002046 public Context createCredentialProtectedStorageContext() {
2047 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
2048 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Adam Lesinski4e862812016-11-21 16:02:24 -08002049 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, mUser, flags,
2050 mClassLoader);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002051 }
2052
2053 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07002054 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002055 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
2056 }
2057
2058 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002059 public boolean isDeviceProtectedStorage() {
2060 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002061 }
2062
2063 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002064 public boolean isCredentialProtectedStorage() {
2065 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07002066 }
2067
Jeff Brown98365d72012-08-19 20:30:52 -07002068 @Override
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002069 public Display getDisplay() {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002070 if (mDisplay == null) {
2071 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
Bryce Lee609bf652017-02-09 16:50:13 -08002072 mResources);
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002073 }
2074
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002075 return mDisplay;
2076 }
2077
2078 @Override
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002079 public void updateDisplay(int displayId) {
Bryce Lee609bf652017-02-09 16:50:13 -08002080 mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002081 }
2082
2083 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07002084 public DisplayAdjustments getDisplayAdjustments(int displayId) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002085 return mResources.getDisplayAdjustments();
Jeff Brown98365d72012-08-19 20:30:52 -07002086 }
2087
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002088 @Override
2089 public File getDataDir() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07002091 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002092 if (isCredentialProtectedStorage()) {
2093 res = mPackageInfo.getCredentialProtectedDataDirFile();
2094 } else if (isDeviceProtectedStorage()) {
2095 res = mPackageInfo.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002096 } else {
Jeff Sharkey35871f22016-01-29 17:13:29 -07002097 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002098 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002099
2100 if (res != null) {
Jeff Sharkey21f50722016-04-27 12:38:02 -06002101 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Jeff Sharkey24492ae2016-04-25 13:20:25 -06002102 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
2103 new Throwable());
2104 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002105 return res;
2106 } else {
2107 throw new RuntimeException(
2108 "No data directory found for package " + getPackageName());
2109 }
2110 } else {
2111 throw new RuntimeException(
2112 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
2115
2116 @Override
2117 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07002118 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002120 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 if (!file.exists()) {
2122 file.mkdir();
2123 setFilePermissionsFromMode(file.getPath(), mode,
2124 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
2125 }
2126 return file;
2127 }
2128
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002129 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08002130 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002131 public int getUserId() {
2132 return mUser.getIdentifier();
2133 }
2134
Dianne Hackborn21556372010-02-04 16:34:40 -08002135 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002136 LoadedApk packageInfo = new LoadedApk(mainThread);
Adam Lesinski4e862812016-11-21 16:02:24 -08002137 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, 0,
2138 null);
2139 context.mResources = packageInfo.getResources();
Jeff Browndefd4a62014-03-10 21:24:37 -07002140 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002141 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 return context;
2143 }
2144
Jeff Browndefd4a62014-03-10 21:24:37 -07002145 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
2146 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski4e862812016-11-21 16:02:24 -08002147 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, 0,
2148 null);
2149 context.mResources = packageInfo.getResources();
Adam Lesinski8fa71072016-11-18 18:13:55 -08002150 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002151 }
2152
2153 static ContextImpl createActivityContext(ActivityThread mainThread,
Adam Lesinski4e862812016-11-21 16:02:24 -08002154 LoadedApk packageInfo, ActivityInfo activityInfo, IBinder activityToken, int displayId,
Adam Lesinski082614c2016-03-04 14:33:47 -08002155 Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002156 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski8fa71072016-11-18 18:13:55 -08002157
Adam Lesinski4e862812016-11-21 16:02:24 -08002158 String[] splitDirs = packageInfo.getSplitResDirs();
2159 ClassLoader classLoader = packageInfo.getClassLoader();
2160
2161 if (packageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
2162 Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "SplitDependencies");
2163 try {
2164 classLoader = packageInfo.getSplitClassLoader(activityInfo.splitName);
2165 splitDirs = packageInfo.getSplitPaths(activityInfo.splitName);
2166 } catch (NameNotFoundException e) {
2167 // Nothing above us can handle a NameNotFoundException, better crash.
2168 throw new RuntimeException(e);
2169 } finally {
2170 Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2171 }
2172 }
2173
Adam Lesinski8fa71072016-11-18 18:13:55 -08002174 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, activityToken, null,
Adam Lesinski4e862812016-11-21 16:02:24 -08002175 0, classLoader);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002176
2177 // Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
2178 displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
2179
2180 final CompatibilityInfo compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2181 ? packageInfo.getCompatibilityInfo()
2182 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
2183
Adam Lesinskic9701942016-11-28 19:44:19 -08002184 final ResourcesManager resourcesManager = ResourcesManager.getInstance();
2185
2186 // Create the base resources for which all configuration contexts for this Activity
2187 // will be rebased upon.
2188 context.mResources = resourcesManager.createBaseActivityResources(activityToken,
2189 packageInfo.getResDir(),
Adam Lesinski4e862812016-11-21 16:02:24 -08002190 splitDirs,
Adam Lesinskic9701942016-11-28 19:44:19 -08002191 packageInfo.getOverlayDirs(),
Adam Lesinski0e618832017-02-07 11:40:12 -08002192 packageInfo.getApplicationInfo().sharedLibraryFiles,
Adam Lesinskic9701942016-11-28 19:44:19 -08002193 displayId,
2194 overrideConfiguration,
2195 compatInfo,
Adam Lesinski4e862812016-11-21 16:02:24 -08002196 classLoader);
Adam Lesinskic9701942016-11-28 19:44:19 -08002197 context.mDisplay = resourcesManager.getAdjustedDisplay(displayId,
Bryce Lee609bf652017-02-09 16:50:13 -08002198 context.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002199 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002200 }
2201
2202 private ContextImpl(ContextImpl container, ActivityThread mainThread,
Adam Lesinski4e862812016-11-21 16:02:24 -08002203 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,
2204 ClassLoader classLoader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002207 // If creator didn't specify which storage to use, use the default
2208 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002209 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2210 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002211 final File dataDir = packageInfo.getDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002212 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
2213 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
2214 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
2215 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002216 }
2217 }
2218
Jeff Browndefd4a62014-03-10 21:24:37 -07002219 mMainThread = mainThread;
2220 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002221 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222
Jeff Browndefd4a62014-03-10 21:24:37 -07002223 if (user == null) {
2224 user = Process.myUserHandle();
2225 }
2226 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 mPackageInfo = packageInfo;
Adam Lesinski4e862812016-11-21 16:02:24 -08002229 mClassLoader = classLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -07002230 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002231
Jeff Browndefd4a62014-03-10 21:24:37 -07002232 if (container != null) {
2233 mBasePackageName = container.mBasePackageName;
2234 mOpPackageName = container.mOpPackageName;
Adam Lesinski8fa71072016-11-18 18:13:55 -08002235 mResources = container.mResources;
2236 mDisplay = container.mDisplay;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002237 } else {
2238 mBasePackageName = packageInfo.mPackageName;
2239 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2240 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2241 // Special case: system components allow themselves to be loaded in to other
2242 // processes. For purposes of app ops, we must then consider the context as
2243 // belonging to the package of this process, not the system itself, otherwise
2244 // the package+uid verifications in app ops will fail.
2245 mOpPackageName = ActivityThread.currentPackageName();
2246 } else {
2247 mOpPackageName = mBasePackageName;
2248 }
2249 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002250
2251 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 }
2253
Narayan Kamath29564cd2014-08-07 10:57:40 +01002254 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2255 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 }
2257
2258 final void scheduleFinalCleanup(String who, String what) {
2259 mMainThread.scheduleContextCleanup(this, who, what);
2260 }
2261
2262 final void performFinalCleanup(String who, String what) {
2263 //Log.i(TAG, "Cleanup up context: " + this);
2264 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2265 }
2266
2267 final Context getReceiverRestrictedContext() {
2268 if (mReceiverRestrictedContext != null) {
2269 return mReceiverRestrictedContext;
2270 }
2271 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2272 }
2273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 final void setOuterContext(Context context) {
2275 mOuterContext = context;
2276 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 final Context getOuterContext() {
2279 return mOuterContext;
2280 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002281
Tony Mak46aabe52016-11-14 12:53:06 +00002282 @Override
2283 public IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 return mActivityToken;
2285 }
2286
Jeff Sharkey634dc422016-01-30 17:44:15 -07002287 private void checkMode(int mode) {
2288 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2289 if ((mode & MODE_WORLD_READABLE) != 0) {
2290 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2291 }
2292 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2293 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2294 }
2295 }
2296 }
2297
Jeff Brown6e539312015-02-24 18:53:21 -08002298 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002299 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 int extraPermissions) {
2301 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2302 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2303 |extraPermissions;
2304 if ((mode&MODE_WORLD_READABLE) != 0) {
2305 perms |= FileUtils.S_IROTH;
2306 }
2307 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2308 perms |= FileUtils.S_IWOTH;
2309 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002310 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2312 + ", perms=0x" + Integer.toHexString(perms));
2313 }
2314 FileUtils.setPermissions(name, perms, -1, -1);
2315 }
2316
2317 private File makeFilename(File base, String name) {
2318 if (name.indexOf(File.separatorChar) < 0) {
2319 return new File(base, name);
2320 }
2321 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002322 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 }
2324
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002325 /**
2326 * Ensure that given directories exist, trying to create them if missing. If
2327 * unable to create, they are filtered by replacing with {@code null}.
2328 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002329 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002330 File[] result = new File[dirs.length];
2331 for (int i = 0; i < dirs.length; i++) {
2332 File dir = dirs[i];
2333 if (!dir.exists()) {
2334 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002335 // recheck existence in case of cross-process race
2336 if (!dir.exists()) {
2337 // Failing to mkdir() may be okay, since we might not have
2338 // enough permissions; ask vold to create on our behalf.
Sudheer Shanka2250d562016-11-07 15:41:02 -08002339 final IStorageManager storageManager = IStorageManager.Stub.asInterface(
Christopher Tatecc866da2013-10-02 18:11:01 -07002340 ServiceManager.getService("mount"));
Christopher Tatecc866da2013-10-02 18:11:01 -07002341 try {
Sudheer Shanka2250d562016-11-07 15:41:02 -08002342 final int res = storageManager.mkdirs(
2343 getPackageName(), dir.getAbsolutePath());
Jeff Sharkey983294592015-07-13 10:25:31 -07002344 if (res != 0) {
2345 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2346 dir = null;
2347 }
2348 } catch (Exception e) {
2349 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002350 dir = null;
2351 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002352 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002353 }
2354 }
2355 result[i] = dir;
2356 }
2357 return result;
2358 }
2359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 // ----------------------------------------------------------------------
2361 // ----------------------------------------------------------------------
2362 // ----------------------------------------------------------------------
2363
2364 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002365 private final ActivityThread mMainThread;
2366 private final UserHandle mUser;
2367
2368 public ApplicationContentResolver(
2369 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002371 mMainThread = Preconditions.checkNotNull(mainThread);
2372 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 }
2374
2375 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002376 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002377 return mMainThread.acquireProvider(context,
2378 ContentProvider.getAuthorityWithoutUserId(auth),
2379 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 }
2381
2382 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002383 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002384 return mMainThread.acquireExistingProvider(context,
2385 ContentProvider.getAuthorityWithoutUserId(auth),
2386 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002387 }
2388
2389 @Override
2390 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002391 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002393
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002394 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002395 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002396 return mMainThread.acquireProvider(c,
2397 ContentProvider.getAuthorityWithoutUserId(auth),
2398 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002399 }
2400
2401 @Override
2402 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002403 return mMainThread.releaseProvider(icp, false);
2404 }
2405
2406 @Override
2407 public void unstableProviderDied(IContentProvider icp) {
2408 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002409 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002410
2411 @Override
2412 public void appNotRespondingViaProvider(IContentProvider icp) {
2413 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2414 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002415
2416 /** @hide */
2417 protected int resolveUserIdFromAuthority(String auth) {
2418 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421}