blob: ea940426f5a5f2d88e8a0ee0077f07f4665c7eba [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;
Jason Monkbd983642017-02-24 16:57:40 -050041import android.content.res.CompatResources;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070042import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070043import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070045import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.database.sqlite.SQLiteDatabase;
47import android.database.sqlite.SQLiteDatabase.CursorFactory;
48import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080052import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070054import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080055import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.FileUtils;
57import android.os.Handler;
58import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.os.RemoteException;
Adam Lesinski4e862812016-11-21 16:02:24 -080062import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070063import android.os.UserHandle;
Jeff Sharkeya65e6492017-06-21 13:45:11 -060064import android.os.UserManager;
Jeff Sharkeyae266462017-11-27 13:32:24 -070065import android.os.storage.StorageManager;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -070066import android.system.ErrnoException;
67import android.system.Os;
68import android.system.OsConstants;
Jeff Sharkey83aacde2017-03-22 23:13:39 -060069import android.system.StructStat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070071import android.util.ArrayMap;
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;
Jeff Brown98365d72012-08-19 20:30:52 -070074import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080075import android.view.DisplayAdjustments;
Felipe Lemebb567ae2017-10-04 09:56:21 -070076import android.view.autofill.AutofillManager.AutofillClient;
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
Jeff Sharkey83aacde2017-03-22 23:13:39 -060081import libcore.io.Memory;
Adam Lesinski4e862812016-11-21 16:02:24 -080082
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;
Jeff Sharkey83aacde2017-03-22 23:13:39 -060090import java.nio.ByteOrder;
Christopher Tatefe2368c2017-05-17 15:42:35 -070091import java.util.ArrayList;
Jeff Sharkey7a30a302015-12-08 14:20:06 -070092import java.util.Objects;
Jeff Sharkey8439ac02017-12-12 17:26:23 -070093import java.util.concurrent.Executor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095class ReceiverRestrictedContext extends ContextWrapper {
96 ReceiverRestrictedContext(Context base) {
97 super(base);
98 }
99
100 @Override
101 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
102 return registerReceiver(receiver, filter, null, null);
103 }
104
105 @Override
106 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
107 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700108 if (receiver == null) {
109 // Allow retrieving current sticky broadcast; this is safe since we
110 // aren't actually registering a receiver.
111 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
112 } else {
113 throw new ReceiverCallNotAllowedException(
114 "BroadcastReceiver components are not allowed to register to receive intents");
115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
117
118 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700119 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
120 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700121 if (receiver == null) {
122 // Allow retrieving current sticky broadcast; this is safe since we
123 // aren't actually registering a receiver.
124 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
125 } else {
126 throw new ReceiverCallNotAllowedException(
127 "BroadcastReceiver components are not allowed to register to receive intents");
128 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700129 }
130
131 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
133 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700134 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136}
137
138/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800139 * Common implementation of Context API, which provides the base
140 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800142class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800143 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700144 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600146 private static final String XATTR_INODE_CACHE = "user.inode_cache";
147 private static final String XATTR_INODE_CODE_CACHE = "user.inode_code_cache";
148
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700149 /**
150 * Map from package name, to preference name, to cached preferences.
151 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700152 @GuardedBy("ContextImpl.class")
153 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
154
155 /**
156 * Map from preference name to generated path.
157 */
158 @GuardedBy("ContextImpl.class")
159 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800161 final @NonNull ActivityThread mMainThread;
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400162 final @NonNull LoadedApk mLoadedApk;
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800163 private @Nullable ClassLoader mClassLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -0700164
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800165 private final @Nullable IBinder mActivityToken;
Jeff Browndefd4a62014-03-10 21:24:37 -0700166
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800167 private final @Nullable UserHandle mUser;
Jeff Browndefd4a62014-03-10 21:24:37 -0700168
169 private final ApplicationContentResolver mContentResolver;
170
171 private final String mBasePackageName;
172 private final String mOpPackageName;
173
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700174 private final @NonNull ResourcesManager mResourcesManager;
Adam Lesinski8fa71072016-11-18 18:13:55 -0800175 private @NonNull Resources mResources;
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700176 private @Nullable Display mDisplay; // may be null if default display
Jeff Browndefd4a62014-03-10 21:24:37 -0700177
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700178 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 private int mThemeResource = 0;
182 private Resources.Theme mTheme = null;
183 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800186 // The name of the split this Context is representing. May be null.
187 private @Nullable String mSplitName = null;
188
Felipe Lemebb567ae2017-10-04 09:56:21 -0700189 private AutofillClient mAutofillClient = null;
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 private final Object mSync = new Object();
192
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700193 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700195 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700197 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700199 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700200 private File mNoBackupFilesDir;
201 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700203 @GuardedBy("mSync")
204 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700205
Jeff Brown6e539312015-02-24 18:53:21 -0800206 // The system service cache for the system services that are cached per-ContextImpl.
207 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800208
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700209 static ContextImpl getImpl(Context context) {
210 Context nextContext;
211 while ((context instanceof ContextWrapper) &&
212 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
213 context = nextContext;
214 }
215 return (ContextImpl)context;
216 }
217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 @Override
219 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700220 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222
223 @Override
224 public Resources getResources() {
225 return mResources;
226 }
227
228 @Override
229 public PackageManager getPackageManager() {
230 if (mPackageManager != null) {
231 return mPackageManager;
232 }
233
234 IPackageManager pm = ActivityThread.getPackageManager();
235 if (pm != null) {
236 // Doesn't matter if we make more than one instance.
237 return (mPackageManager = new ApplicationPackageManager(this, pm));
238 }
239
240 return null;
241 }
242
243 @Override
244 public ContentResolver getContentResolver() {
245 return mContentResolver;
246 }
247
248 @Override
249 public Looper getMainLooper() {
250 return mMainThread.getLooper();
251 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 @Override
Jeff Sharkey8439ac02017-12-12 17:26:23 -0700254 public Executor getMainExecutor() {
255 return mMainThread.getExecutor();
256 }
257
258 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 public Context getApplicationContext() {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400260 return (mLoadedApk != null) ?
261 mLoadedApk.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700265 public void setTheme(int resId) {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700266 synchronized (mSync) {
267 if (mThemeResource != resId) {
268 mThemeResource = resId;
269 initializeTheme();
270 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800275 public int getThemeResId() {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700276 synchronized (mSync) {
277 return mThemeResource;
278 }
Dianne Hackborn247fe742011-01-08 17:25:57 -0800279 }
280
281 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 public Resources.Theme getTheme() {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700283 synchronized (mSync) {
284 if (mTheme != null) {
285 return mTheme;
286 }
287
288 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
289 getOuterContext().getApplicationInfo().targetSdkVersion);
290 initializeTheme();
291
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700292 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 }
295
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700296 private void initializeTheme() {
297 if (mTheme == null) {
298 mTheme = mResources.newTheme();
299 }
300 mTheme.applyStyle(mThemeResource, true);
301 }
302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 @Override
304 public ClassLoader getClassLoader() {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400305 return mClassLoader != null ? mClassLoader : (mLoadedApk != null ? mLoadedApk.getClassLoader() : ClassLoader.getSystemClassLoader());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307
308 @Override
309 public String getPackageName() {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400310 if (mLoadedApk != null) {
311 return mLoadedApk.getPackageName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 }
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400313 // No mLoadedApk means this is a Context for the system itself,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800314 // and this here is its name.
315 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
317
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800318 /** @hide */
319 @Override
320 public String getBasePackageName() {
321 return mBasePackageName != null ? mBasePackageName : getPackageName();
322 }
323
Dianne Hackborn95d78532013-09-11 09:51:14 -0700324 /** @hide */
325 @Override
326 public String getOpPackageName() {
327 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
328 }
329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700331 public ApplicationInfo getApplicationInfo() {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400332 if (mLoadedApk != null) {
333 return mLoadedApk.getApplicationInfo();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700334 }
335 throw new RuntimeException("Not supported in system context");
336 }
337
338 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 public String getPackageResourcePath() {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400340 if (mLoadedApk != null) {
341 return mLoadedApk.getResDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 }
343 throw new RuntimeException("Not supported in system context");
344 }
345
346 @Override
347 public String getPackageCodePath() {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400348 if (mLoadedApk != null) {
349 return mLoadedApk.getAppDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 }
351 throw new RuntimeException("Not supported in system context");
352 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200353
Jeff Brown6e539312015-02-24 18:53:21 -0800354 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700356 // At least one application in the world actually passes in a null
357 // name. This happened to work because when we generated the file name
358 // we would stringify it to "null.xml". Nice.
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -0400359 if (mLoadedApk.getApplicationInfo().targetSdkVersion <
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700360 Build.VERSION_CODES.KITKAT) {
361 if (name == null) {
362 name = "null";
363 }
364 }
365
Jeff Sharkeybe782582016-02-15 18:35:57 -0700366 File file;
367 synchronized (ContextImpl.class) {
368 if (mSharedPrefsPaths == null) {
369 mSharedPrefsPaths = new ArrayMap<>();
370 }
371 file = mSharedPrefsPaths.get(name);
372 if (file == null) {
373 file = getSharedPreferencesPath(name);
374 mSharedPrefsPaths.put(name, file);
375 }
376 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700377 return getSharedPreferences(file, mode);
378 }
379
380 @Override
381 public SharedPreferences getSharedPreferences(File file, int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700383 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700384 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
385 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700386 if (sp == null) {
John Reckf3903e92017-05-12 17:12:54 -0700387 checkMode(mode);
388 if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.O) {
389 if (isCredentialProtectedStorage()
Jeff Sharkeya65e6492017-06-21 13:45:11 -0600390 && !getSystemService(UserManager.class)
391 .isUserUnlockingOrUnlocked(UserHandle.myUserId())) {
John Reckf3903e92017-05-12 17:12:54 -0700392 throw new IllegalStateException("SharedPreferences in credential encrypted "
393 + "storage are not available until after user is unlocked");
394 }
395 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700396 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700397 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700398 return sp;
399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800401 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
402 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
403 // If somebody else (some other process) changed the prefs
404 // file behind our back, we reload it. This has been the
405 // historical (if undocumented) behavior.
406 sp.startReloadIfChangedUnexpectedly();
407 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700408 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410
Jeff Sharkey35871f22016-01-29 17:13:29 -0700411 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700412 if (sSharedPrefsCache == null) {
413 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700414 }
415
416 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700417 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700418 if (packagePrefs == null) {
419 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700420 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700421 }
422
423 return packagePrefs;
424 }
425
Christopher Tatefe2368c2017-05-17 15:42:35 -0700426 @Override
427 public void reloadSharedPreferences() {
428 // Build the list of all per-context impls (i.e. caches) we know about
429 ArrayList<SharedPreferencesImpl> spImpls = new ArrayList<>();
430 synchronized (ContextImpl.class) {
431 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
432 for (int i = 0; i < cache.size(); i++) {
433 final SharedPreferencesImpl sp = cache.valueAt(i);
434 if (sp != null) {
435 spImpls.add(sp);
436 }
437 }
438 }
439
440 // Issue the reload outside the cache lock
441 for (int i = 0; i < spImpls.size(); i++) {
442 spImpls.get(i).startReloadIfChangedUnexpectedly();
443 }
444 }
445
Jeff Sharkey35871f22016-01-29 17:13:29 -0700446 /**
447 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700448 * requested prefix.
449 *
450 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700451 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600452 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700453 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
454 @Override
455 public boolean accept(File dir, String name) {
456 return name.startsWith(prefix);
457 }
458 });
459
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700460 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700461 for (File sourceFile : sourceFiles) {
462 final File targetFile = new File(targetDir, sourceFile.getName());
463 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
464 try {
465 FileUtils.copyFileOrThrow(sourceFile, targetFile);
466 FileUtils.copyPermissions(sourceFile, targetFile);
467 if (!sourceFile.delete()) {
468 throw new IOException("Failed to clean up " + sourceFile);
469 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700470 if (res != -1) {
471 res++;
472 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700473 } catch (IOException e) {
474 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700475 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700476 }
477 }
478 return res;
479 }
480
481 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600482 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700483 synchronized (ContextImpl.class) {
484 final File source = sourceContext.getSharedPreferencesPath(name);
485 final File target = getSharedPreferencesPath(name);
486
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600487 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700488 source.getName());
489 if (res > 0) {
490 // We moved at least one file, so evict any in-memory caches for
491 // either location
492 final ArrayMap<File, SharedPreferencesImpl> cache =
493 getSharedPreferencesCacheLocked();
494 cache.remove(source);
495 cache.remove(target);
496 }
497 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700498 }
499 }
500
501 @Override
502 public boolean deleteSharedPreferences(String name) {
503 synchronized (ContextImpl.class) {
504 final File prefs = getSharedPreferencesPath(name);
505 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
506
507 // Evict any in-memory caches
508 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
509 cache.remove(prefs);
510
511 prefs.delete();
512 prefsBackup.delete();
513
514 // We failed if files are still lingering
515 return !(prefs.exists() || prefsBackup.exists());
516 }
517 }
518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 private File getPreferencesDir() {
520 synchronized (mSync) {
521 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700522 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700524 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526 }
527
528 @Override
529 public FileInputStream openFileInput(String name)
530 throws FileNotFoundException {
531 File f = makeFilename(getFilesDir(), name);
532 return new FileInputStream(f);
533 }
534
535 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700536 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
537 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 final boolean append = (mode&MODE_APPEND) != 0;
539 File f = makeFilename(getFilesDir(), name);
540 try {
541 FileOutputStream fos = new FileOutputStream(f, append);
542 setFilePermissionsFromMode(f.getPath(), mode, 0);
543 return fos;
544 } catch (FileNotFoundException e) {
545 }
546
547 File parent = f.getParentFile();
548 parent.mkdir();
549 FileUtils.setPermissions(
550 parent.getPath(),
551 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
552 -1, -1);
553 FileOutputStream fos = new FileOutputStream(f, append);
554 setFilePermissionsFromMode(f.getPath(), mode, 0);
555 return fos;
556 }
557
558 @Override
559 public boolean deleteFile(String name) {
560 File f = makeFilename(getFilesDir(), name);
561 return f.delete();
562 }
563
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700564 /**
565 * Common-path handling of app data dir creation
566 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700567 private static File ensurePrivateDirExists(File file) {
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600568 return ensurePrivateDirExists(file, 0771, -1, null);
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700569 }
570
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600571 private static File ensurePrivateCacheDirExists(File file, String xattr) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700572 final int gid = UserHandle.getCacheAppGid(Process.myUid());
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600573 return ensurePrivateDirExists(file, 02771, gid, xattr);
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700574 }
575
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600576 private static File ensurePrivateDirExists(File file, int mode, int gid, String xattr) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700577 if (!file.exists()) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700578 final String path = file.getAbsolutePath();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700579 try {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700580 Os.mkdir(path, mode);
581 Os.chmod(path, mode);
582 if (gid != -1) {
583 Os.chown(path, -1, gid);
584 }
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700585 } catch (ErrnoException e) {
586 if (e.errno == OsConstants.EEXIST) {
587 // We must have raced with someone; that's okay
588 } else {
589 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700590 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700591 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600592
593 if (xattr != null) {
594 try {
595 final StructStat stat = Os.stat(file.getAbsolutePath());
596 final byte[] value = new byte[8];
597 Memory.pokeLong(value, 0, stat.st_ino, ByteOrder.nativeOrder());
598 Os.setxattr(file.getParentFile().getAbsolutePath(), xattr, value, 0);
599 } catch (ErrnoException e) {
600 Log.w(TAG, "Failed to update " + xattr + ": " + e.getMessage());
601 }
602 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700603 }
604 return file;
605 }
606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 @Override
608 public File getFilesDir() {
609 synchronized (mSync) {
610 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700611 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700613 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700614 }
615 }
616
617 @Override
618 public File getNoBackupFilesDir() {
619 synchronized (mSync) {
620 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700621 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700623 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
625 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800628 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700629 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700630 final File[] dirs = getExternalFilesDirs(type);
631 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700632 }
633
634 @Override
635 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800636 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600637 File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700638 if (type != null) {
639 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800640 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700641 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800642 }
643 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200644
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800645 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800646 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700647 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700648 final File[] dirs = getObbDirs();
649 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700650 }
651
652 @Override
653 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800654 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600655 File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
656 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800657 }
658 }
659
660 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 public File getCacheDir() {
662 synchronized (mSync) {
663 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700664 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600666 return ensurePrivateCacheDirExists(mCacheDir, XATTR_INODE_CACHE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200669
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800670 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700671 public File getCodeCacheDir() {
672 synchronized (mSync) {
673 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700674 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700675 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600676 return ensurePrivateCacheDirExists(mCodeCacheDir, XATTR_INODE_CODE_CACHE);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700677 }
678 }
679
680 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800681 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700682 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700683 final File[] dirs = getExternalCacheDirs();
684 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700685 }
686
687 @Override
688 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800689 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600690 File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
691 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800692 }
693 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700696 public File[] getExternalMediaDirs() {
697 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600698 File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
699 return ensureExternalDirsExistOrFilter(dirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700700 }
701 }
702
Fyodor Kupolov61221292016-09-02 15:21:03 -0700703 /**
704 * @hide
705 */
706 @Nullable
707 @Override
708 public File getPreloadsFileCache() {
709 return Environment.getDataPreloadsFileCacheDirectory(getPackageName());
710 }
711
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700712 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 public File getFileStreamPath(String name) {
714 return makeFilename(getFilesDir(), name);
715 }
716
717 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700718 public File getSharedPreferencesPath(String name) {
719 return makeFilename(getPreferencesDir(), name + ".xml");
720 }
721
722 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700724 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726
727 @Override
728 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700729 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
732 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700733 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
734 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700735 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700736 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700737 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
738 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
739 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
740 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800741 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
742 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
743 }
Jeff Brown47847f32012-03-22 19:13:11 -0700744 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700745 setFilePermissionsFromMode(f.getPath(), mode, 0);
746 return db;
747 }
748
749 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600750 public boolean moveDatabaseFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700751 synchronized (ContextImpl.class) {
752 final File source = sourceContext.getDatabasePath(name);
753 final File target = getDatabasePath(name);
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600754 return moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700755 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700756 }
757 }
758
759 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 public boolean deleteDatabase(String name) {
761 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700762 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800763 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 } catch (Exception e) {
765 }
766 return false;
767 }
768
769 @Override
770 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700771 File dir;
772 File f;
773
774 if (name.charAt(0) == File.separatorChar) {
775 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
776 dir = new File(dirPath);
777 name = name.substring(name.lastIndexOf(File.separatorChar));
778 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700779
780 if (!dir.isDirectory() && dir.mkdir()) {
781 FileUtils.setPermissions(dir.getPath(),
782 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
783 -1, -1);
784 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700785 } else {
786 dir = getDatabasesDir();
787 f = makeFilename(dir, name);
788 }
789
Jeff Sharkey35871f22016-01-29 17:13:29 -0700790 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
792
793 @Override
794 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700795 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 private File getDatabasesDir() {
799 synchronized (mSync) {
800 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700801 if ("android".equals(getPackageName())) {
802 mDatabasesDir = new File("/data/system");
803 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700804 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700807 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800812 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700814 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
816
817 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800818 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700819 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700820 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822
823 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800824 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700826 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 }
828
829 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800830 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700832 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 }
834
835 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800836 @Deprecated
837 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700838 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
840
841 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800842 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700844 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
846
847 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800848 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700850 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
852
Jeff Brown6e539312015-02-24 18:53:21 -0800853 private WallpaperManager getWallpaperManager() {
854 return getSystemService(WallpaperManager.class);
855 }
856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 @Override
858 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700859 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700860 startActivity(intent, null);
861 }
862
Amith Yamasani82644082012-08-03 13:09:11 -0700863 /** @hide */
864 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700865 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700866 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700867 }
868
Dianne Hackborna4972e92012-03-14 10:38:05 -0700869 @Override
870 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700871 warnIfCallingFromSystemProcess();
Jorim Jaggi2adba072016-03-03 13:43:39 +0100872
873 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
874 // generally not allowed, except if the caller specifies the task id the activity should
Bryce Leef465ea02017-05-02 16:58:22 -0700875 // be launched in. A bug was existed between N and O-MR1 which allowed this to work. We
876 // maintain this for backwards compatibility.
877 final int targetSdkVersion = getApplicationInfo().targetSdkVersion;
878
879 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
880 && (targetSdkVersion < Build.VERSION_CODES.N
881 || targetSdkVersion >= Build.VERSION_CODES.P)
882 && (options == null
883 || ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 throw new AndroidRuntimeException(
885 "Calling startActivity() from outside of an Activity "
Bryce Leef465ea02017-05-02 16:58:22 -0700886 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
887 + " Is this really what you want?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700890 getOuterContext(), mMainThread.getApplicationThread(), null,
891 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 }
893
Amith Yamasani258848d2012-08-10 17:06:33 -0700894 /** @hide */
895 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700896 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700897 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800898 ActivityManager.getService().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800899 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700900 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700901 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700902 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700903 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700904 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700905 }
906 }
907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800909 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700910 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700911 startActivities(intents, null);
912 }
913
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700914 /** @hide */
915 @Override
Selim Cinek7fa385a2018-01-24 08:35:28 -0800916 public int startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700917 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
918 throw new AndroidRuntimeException(
919 "Calling startActivities() from outside of an Activity "
920 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
921 + " Is this really what you want?");
922 }
Selim Cinek7fa385a2018-01-24 08:35:28 -0800923 return mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700924 getOuterContext(), mMainThread.getApplicationThread(), null,
925 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700926 }
927
Dianne Hackborna4972e92012-03-14 10:38:05 -0700928 @Override
929 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700930 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800931 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
932 throw new AndroidRuntimeException(
933 "Calling startActivities() from outside of an Activity "
934 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
935 + " Is this really what you want?");
936 }
937 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700938 getOuterContext(), mMainThread.getApplicationThread(), null,
939 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800940 }
941
942 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700943 public void startIntentSender(IntentSender intent,
944 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
945 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700946 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
947 }
948
949 @Override
950 public void startIntentSender(IntentSender intent, Intent fillInIntent,
951 int flagsMask, int flagsValues, int extraFlags, Bundle options)
952 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700953 try {
954 String resolvedType = null;
955 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700956 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -0700957 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700958 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
959 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800960 int result = ActivityManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -0700961 .startActivityIntentSender(mMainThread.getApplicationThread(),
962 intent != null ? intent.getTarget() : null,
963 intent != null ? intent.getWhitelistToken() : null,
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700964 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700965 0, flagsMask, flagsValues, options);
966 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700967 throw new IntentSender.SendIntentException();
968 }
969 Instrumentation.checkStartActivityResult(result, null);
970 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700971 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700972 }
973 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200974
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700975 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700977 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
979 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700980 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800981 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700982 mMainThread.getApplicationThread(), intent, resolvedType, null,
983 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
984 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700986 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988 }
989
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800990 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700992 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700994 String[] receiverPermissions = receiverPermission == null ? null
995 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700997 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800998 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700999 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001000 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1001 null, false, false, getUserId());
1002 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001003 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001004 }
1005 }
1006
1007 @Override
1008 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
1009 warnIfCallingFromSystemProcess();
1010 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1011 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001012 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001013 ActivityManager.getService().broadcastIntent(
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001014 mMainThread.getApplicationThread(), intent, resolvedType, null,
1015 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001016 null, false, false, getUserId());
1017 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001018 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -07001019 }
1020 }
1021
1022 @Override
1023 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
1024 warnIfCallingFromSystemProcess();
1025 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001026 String[] receiverPermissions = receiverPermission == null ? null
1027 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -07001028 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001029 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001030 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001031 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001032 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001033 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001034 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001035 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001036 }
1037 }
1038
1039 @Override
1040 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
1041 warnIfCallingFromSystemProcess();
1042 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001043 String[] receiverPermissions = receiverPermission == null ? null
1044 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001045 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001046 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001047 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001048 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001049 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -07001050 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001052 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 }
1054 }
1055
1056 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -07001057 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001058 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001060 String[] receiverPermissions = receiverPermission == null ? null
1061 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001063 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001064 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001065 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001066 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001067 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001069 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071 }
1072
1073 @Override
1074 public void sendOrderedBroadcast(Intent intent,
1075 String receiverPermission, BroadcastReceiver resultReceiver,
1076 Handler scheduler, int initialCode, String initialData,
1077 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001078 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001079 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1080 }
1081
1082 @Override
1083 public void sendOrderedBroadcast(Intent intent,
1084 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
1085 Handler scheduler, int initialCode, String initialData,
1086 Bundle initialExtras) {
1087 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1088 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001089 }
1090
1091 @Override
1092 public void sendOrderedBroadcast(Intent intent,
1093 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1094 Handler scheduler, int initialCode, String initialData,
1095 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001096 sendOrderedBroadcast(intent, receiverPermission, appOp,
1097 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1098 }
1099
1100 void sendOrderedBroadcast(Intent intent,
1101 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1102 Handler scheduler, int initialCode, String initialData,
1103 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001104 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 IIntentReceiver rd = null;
1106 if (resultReceiver != null) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001107 if (mLoadedApk != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 if (scheduler == null) {
1109 scheduler = mMainThread.getHandler();
1110 }
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001111 rd = mLoadedApk.getReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 resultReceiver, getOuterContext(), scheduler,
1113 mMainThread.getInstrumentation(), false);
1114 } else {
1115 if (scheduler == null) {
1116 scheduler = mMainThread.getHandler();
1117 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001118 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1120 }
1121 }
1122 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001123 String[] receiverPermissions = receiverPermission == null ? null
1124 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001126 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001127 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001129 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001130 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001132 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 }
1135
1136 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001137 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001138 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1139 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001140 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001141 ActivityManager.getService().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001142 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001143 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001144 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001145 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001146 }
1147 }
1148
1149 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001150 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1151 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001152 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1153 }
1154
1155 @Override
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001156 public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission,
1157 Bundle options) {
1158 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1159 String[] receiverPermissions = receiverPermission == null ? null
1160 : new String[] {receiverPermission};
1161 try {
1162 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001163 ActivityManager.getService().broadcastIntent(
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001164 mMainThread.getApplicationThread(), intent, resolvedType, null,
1165 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1166 options, false, false, user.getIdentifier());
1167 } catch (RemoteException e) {
1168 throw e.rethrowFromSystemServer();
1169 }
1170 }
1171
1172 @Override
Svet Ganov16a16892015-04-16 10:32:04 -07001173 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1174 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001175 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001176 String[] receiverPermissions = receiverPermission == null ? null
1177 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001178 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001179 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001180 ActivityManager.getService().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001181 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001182 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001183 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001184 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001185 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001186 }
1187 }
1188
1189 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001190 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001191 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001192 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001193 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001194 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001195 }
1196
1197 @Override
1198 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1199 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001200 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1201 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1202 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1203 }
1204
1205 @Override
1206 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1207 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1208 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001209 IIntentReceiver rd = null;
1210 if (resultReceiver != null) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001211 if (mLoadedApk != null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001212 if (scheduler == null) {
1213 scheduler = mMainThread.getHandler();
1214 }
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001215 rd = mLoadedApk.getReceiverDispatcher(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001216 resultReceiver, getOuterContext(), scheduler,
1217 mMainThread.getInstrumentation(), false);
1218 } else {
1219 if (scheduler == null) {
1220 scheduler = mMainThread.getHandler();
1221 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001222 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1223 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001224 }
1225 }
1226 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001227 String[] receiverPermissions = receiverPermission == null ? null
1228 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001229 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001230 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001231 ActivityManager.getService().broadcastIntent(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001232 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001233 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001234 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001235 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001236 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001237 }
1238 }
1239
1240 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001241 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001243 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1245 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001246 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001247 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001249 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001250 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001252 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254 }
1255
1256 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001257 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001258 public void sendStickyOrderedBroadcast(Intent intent,
1259 BroadcastReceiver resultReceiver,
1260 Handler scheduler, int initialCode, String initialData,
1261 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001262 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001263 IIntentReceiver rd = null;
1264 if (resultReceiver != null) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001265 if (mLoadedApk != null) {
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001266 if (scheduler == null) {
1267 scheduler = mMainThread.getHandler();
1268 }
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001269 rd = mLoadedApk.getReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001270 resultReceiver, getOuterContext(), scheduler,
1271 mMainThread.getInstrumentation(), false);
1272 } else {
1273 if (scheduler == null) {
1274 scheduler = mMainThread.getHandler();
1275 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001276 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001277 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1278 }
1279 }
1280 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1281 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001282 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001283 ActivityManager.getService().broadcastIntent(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001284 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1285 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001286 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001287 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001288 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001289 }
1290 }
1291
1292 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001293 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 public void removeStickyBroadcast(Intent intent) {
1295 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1296 if (resolvedType != null) {
1297 intent = new Intent(intent);
1298 intent.setDataAndType(intent.getData(), resolvedType);
1299 }
1300 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001301 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001302 ActivityManager.getService().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001303 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001305 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 }
1307 }
1308
1309 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001310 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001311 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1312 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1313 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001314 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001315 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001316 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001317 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1318 user.getIdentifier());
1319 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001320 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001321 }
1322 }
1323
1324 @Override
1325 @Deprecated
1326 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1327 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1328 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001329 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001330 ActivityManager.getService().broadcastIntent(
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001331 mMainThread.getApplicationThread(), intent, resolvedType, null,
1332 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1333 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001334 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001335 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001336 }
1337 }
1338
1339 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001340 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001341 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1342 UserHandle user, BroadcastReceiver resultReceiver,
1343 Handler scheduler, int initialCode, String initialData,
1344 Bundle initialExtras) {
1345 IIntentReceiver rd = null;
1346 if (resultReceiver != null) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001347 if (mLoadedApk != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001348 if (scheduler == null) {
1349 scheduler = mMainThread.getHandler();
1350 }
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001351 rd = mLoadedApk.getReceiverDispatcher(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001352 resultReceiver, getOuterContext(), scheduler,
1353 mMainThread.getInstrumentation(), false);
1354 } else {
1355 if (scheduler == null) {
1356 scheduler = mMainThread.getHandler();
1357 }
1358 rd = new LoadedApk.ReceiverDispatcher(
1359 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1360 }
1361 }
1362 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1363 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001364 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001365 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001366 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1367 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001368 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001369 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001370 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001371 }
1372 }
1373
1374 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001375 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001376 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1377 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1378 if (resolvedType != null) {
1379 intent = new Intent(intent);
1380 intent.setDataAndType(intent.getData(), resolvedType);
1381 }
1382 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001383 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001384 ActivityManager.getService().unbroadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001385 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1386 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001387 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001388 }
1389 }
1390
1391 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1393 return registerReceiver(receiver, filter, null, null);
1394 }
1395
1396 @Override
1397 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001398 int flags) {
1399 return registerReceiver(receiver, filter, null, null, flags);
Chad Brubaker816c83b2017-03-02 10:27:59 -08001400 }
1401
1402 @Override
1403 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001405 return registerReceiverInternal(receiver, getUserId(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001406 filter, broadcastPermission, scheduler, getOuterContext(), 0);
Chad Brubaker816c83b2017-03-02 10:27:59 -08001407 }
1408
1409 @Override
1410 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001411 String broadcastPermission, Handler scheduler, int flags) {
Chad Brubaker816c83b2017-03-02 10:27:59 -08001412 return registerReceiverInternal(receiver, getUserId(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001413 filter, broadcastPermission, scheduler, getOuterContext(), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 }
1415
Dianne Hackborn20e80982012-08-31 19:00:44 -07001416 @Override
1417 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1418 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1419 return registerReceiverInternal(receiver, user.getIdentifier(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001420 filter, broadcastPermission, scheduler, getOuterContext(), 0);
Dianne Hackborn20e80982012-08-31 19:00:44 -07001421 }
1422
1423 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 IntentFilter filter, String broadcastPermission,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001425 Handler scheduler, Context context, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 IIntentReceiver rd = null;
1427 if (receiver != null) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001428 if (mLoadedApk != null && context != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 if (scheduler == null) {
1430 scheduler = mMainThread.getHandler();
1431 }
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001432 rd = mLoadedApk.getReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 receiver, context, scheduler,
1434 mMainThread.getInstrumentation(), true);
1435 } else {
1436 if (scheduler == null) {
1437 scheduler = mMainThread.getHandler();
1438 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001439 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001440 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
1443 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001444 final Intent intent = ActivityManager.getService().registerReceiver(
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001445 mMainThread.getApplicationThread(), mBasePackageName, rd, filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001446 broadcastPermission, userId, flags);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001447 if (intent != null) {
1448 intent.setExtrasClassLoader(getClassLoader());
1449 intent.prepareToEnterProcess();
1450 }
1451 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001453 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455 }
1456
1457 @Override
1458 public void unregisterReceiver(BroadcastReceiver receiver) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001459 if (mLoadedApk != null) {
1460 IIntentReceiver rd = mLoadedApk.forgetReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 getOuterContext(), receiver);
1462 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001463 ActivityManager.getService().unregisterReceiver(rd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001465 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
1467 } else {
1468 throw new RuntimeException("Not supported in system context");
1469 }
1470 }
1471
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001472 private void validateServiceIntent(Intent service) {
1473 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001474 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001475 IllegalArgumentException ex = new IllegalArgumentException(
1476 "Service Intent must be explicit: " + service);
1477 throw ex;
1478 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001479 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1480 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001481 }
1482 }
1483 }
1484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 @Override
1486 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001487 warnIfCallingFromSystemProcess();
Christopher Tate242ba3e92017-04-14 15:07:06 -07001488 return startServiceCommon(service, false, mUser);
Christopher Tate42a386b2016-11-07 12:21:21 -08001489 }
1490
1491 @Override
Christopher Tate79047c62017-03-21 11:37:06 -07001492 public ComponentName startForegroundService(Intent service) {
1493 warnIfCallingFromSystemProcess();
Christopher Tate242ba3e92017-04-14 15:07:06 -07001494 return startServiceCommon(service, true, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001495 }
1496
1497 @Override
1498 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001499 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001500 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001501 }
1502
1503 @Override
1504 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Christopher Tate242ba3e92017-04-14 15:07:06 -07001505 return startServiceCommon(service, false, user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001506 }
1507
Christopher Tate42a386b2016-11-07 12:21:21 -08001508 @Override
Christopher Tate79047c62017-03-21 11:37:06 -07001509 public ComponentName startForegroundServiceAsUser(Intent service, UserHandle user) {
Christopher Tate242ba3e92017-04-14 15:07:06 -07001510 return startServiceCommon(service, true, user);
Christopher Tate79047c62017-03-21 11:37:06 -07001511 }
1512
Christopher Tate242ba3e92017-04-14 15:07:06 -07001513 private ComponentName startServiceCommon(Intent service, boolean requireForeground,
1514 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001516 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001517 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001518 ComponentName cn = ActivityManager.getService().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001519 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
Christopher Tate242ba3e92017-04-14 15:07:06 -07001520 getContentResolver()), requireForeground,
Christopher Tate79047c62017-03-21 11:37:06 -07001521 getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001522 if (cn != null) {
1523 if (cn.getPackageName().equals("!")) {
1524 throw new SecurityException(
1525 "Not allowed to start service " + service
1526 + " without permission " + cn.getClassName());
1527 } else if (cn.getPackageName().equals("!!")) {
1528 throw new SecurityException(
1529 "Unable to start service " + service
1530 + ": " + cn.getClassName());
Dianne Hackborn85e35642017-01-12 15:10:57 -08001531 } else if (cn.getPackageName().equals("?")) {
1532 throw new IllegalStateException(
1533 "Not allowed to start service " + service + ": " + cn.getClassName());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 }
1536 return cn;
1537 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001538 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
1540 }
1541
1542 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001543 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001544 return stopServiceCommon(service, user);
1545 }
1546
1547 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001549 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001550 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001551 int res = ActivityManager.getService().stopService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001553 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 if (res < 0) {
1555 throw new SecurityException(
1556 "Not allowed to stop service " + service);
1557 }
1558 return res != 0;
1559 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001560 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
1562 }
1563
1564 @Override
1565 public boolean bindService(Intent service, ServiceConnection conn,
1566 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001567 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001568 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1569 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001570 }
1571
1572 /** @hide */
1573 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001574 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1575 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001576 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001577 }
1578
Adrian Roos691546e2016-02-09 10:13:41 -08001579 /** @hide */
1580 @Override
1581 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1582 Handler handler, UserHandle user) {
1583 if (handler == null) {
1584 throw new IllegalArgumentException("handler must not be null.");
1585 }
1586 return bindServiceCommon(service, conn, flags, handler, user);
1587 }
1588
Tony Mak46aabe52016-11-14 12:53:06 +00001589 /** @hide */
1590 @Override
1591 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler,
1592 int flags) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001593 return mLoadedApk.getServiceDispatcher(conn, getOuterContext(), handler, flags);
Tony Mak46aabe52016-11-14 12:53:06 +00001594 }
1595
1596 /** @hide */
1597 @Override
1598 public IApplicationThread getIApplicationThread() {
1599 return mMainThread.getApplicationThread();
1600 }
1601
Tony Makbf9928d2016-12-22 11:02:45 +00001602 /** @hide */
1603 @Override
1604 public Handler getMainThreadHandler() {
1605 return mMainThread.getHandler();
1606 }
1607
Adrian Roos691546e2016-02-09 10:13:41 -08001608 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1609 handler, UserHandle user) {
Tony Mak46aabe52016-11-14 12:53:06 +00001610 // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001612 if (conn == null) {
1613 throw new IllegalArgumentException("connection is null");
1614 }
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001615 if (mLoadedApk != null) {
1616 sd = mLoadedApk.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 } else {
1618 throw new RuntimeException("Not supported in system context");
1619 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001620 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001622 IBinder token = getActivityToken();
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001623 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mLoadedApk != null
1624 && mLoadedApk.getApplicationInfo().targetSdkVersion
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001625 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1626 flags |= BIND_WAIVE_PRIORITY;
1627 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001628 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001629 int res = ActivityManager.getService().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001630 mMainThread.getApplicationThread(), getActivityToken(), service,
1631 service.resolveTypeIfNeeded(getContentResolver()),
1632 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 if (res < 0) {
1634 throw new SecurityException(
1635 "Not allowed to bind to service " + service);
1636 }
1637 return res != 0;
1638 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001639 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
1641 }
1642
1643 @Override
1644 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001645 if (conn == null) {
1646 throw new IllegalArgumentException("connection is null");
1647 }
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001648 if (mLoadedApk != null) {
1649 IServiceConnection sd = mLoadedApk.forgetServiceDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 getOuterContext(), conn);
1651 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001652 ActivityManager.getService().unbindService(sd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001654 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
1656 } else {
1657 throw new RuntimeException("Not supported in system context");
1658 }
1659 }
1660
1661 @Override
1662 public boolean startInstrumentation(ComponentName className,
1663 String profileFile, Bundle arguments) {
1664 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001665 if (arguments != null) {
1666 arguments.setAllowFds(false);
1667 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001668 return ActivityManager.getService().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001669 className, profileFile, 0, arguments, null, null, getUserId(),
1670 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001672 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
1675
1676 @Override
1677 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001678 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
1680
Jeff Brown6e539312015-02-24 18:53:21 -08001681 @Override
1682 public String getSystemServiceName(Class<?> serviceClass) {
1683 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001684 }
1685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 @Override
1687 public int checkPermission(String permission, int pid, int uid) {
1688 if (permission == null) {
1689 throw new IllegalArgumentException("permission is null");
1690 }
1691
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001692 final IActivityManager am = ActivityManager.getService();
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001693 if (am == null) {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001694 // Well this is super awkward; we somehow don't have an active
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001695 // ActivityManager instance. If we're testing a root or system
1696 // UID, then they totally have whatever permission this is.
1697 final int appId = UserHandle.getAppId(uid);
1698 if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) {
1699 Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " holds " + permission);
1700 return PackageManager.PERMISSION_GRANTED;
1701 }
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001702 }
1703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 try {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001705 return am.checkPermission(permission, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001707 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
1709 }
1710
Dianne Hackbornff170242014-11-19 10:59:01 -08001711 /** @hide */
1712 @Override
1713 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1714 if (permission == null) {
1715 throw new IllegalArgumentException("permission is null");
1716 }
1717
1718 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001719 return ActivityManager.getService().checkPermissionWithToken(
Dianne Hackbornff170242014-11-19 10:59:01 -08001720 permission, pid, uid, callerToken);
1721 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001722 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001723 }
1724 }
1725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 @Override
1727 public int checkCallingPermission(String permission) {
1728 if (permission == null) {
1729 throw new IllegalArgumentException("permission is null");
1730 }
1731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 int pid = Binder.getCallingPid();
1733 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001734 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736 return PackageManager.PERMISSION_DENIED;
1737 }
1738
1739 @Override
1740 public int checkCallingOrSelfPermission(String permission) {
1741 if (permission == null) {
1742 throw new IllegalArgumentException("permission is null");
1743 }
1744
1745 return checkPermission(permission, Binder.getCallingPid(),
1746 Binder.getCallingUid());
1747 }
1748
Svetoslavc6d1c342015-02-26 14:44:43 -08001749 @Override
1750 public int checkSelfPermission(String permission) {
1751 if (permission == null) {
1752 throw new IllegalArgumentException("permission is null");
1753 }
1754
1755 return checkPermission(permission, Process.myPid(), Process.myUid());
1756 }
1757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 private void enforce(
1759 String permission, int resultOfCheck,
1760 boolean selfToo, int uid, String message) {
1761 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1762 throw new SecurityException(
1763 (message != null ? (message + ": ") : "") +
1764 (selfToo
1765 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001766 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 permission +
1768 ".");
1769 }
1770 }
1771
Jeff Brown6e539312015-02-24 18:53:21 -08001772 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 public void enforcePermission(
1774 String permission, int pid, int uid, String message) {
1775 enforce(permission,
1776 checkPermission(permission, pid, uid),
1777 false,
1778 uid,
1779 message);
1780 }
1781
Jeff Brown6e539312015-02-24 18:53:21 -08001782 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 public void enforceCallingPermission(String permission, String message) {
1784 enforce(permission,
1785 checkCallingPermission(permission),
1786 false,
1787 Binder.getCallingUid(),
1788 message);
1789 }
1790
Jeff Brown6e539312015-02-24 18:53:21 -08001791 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 public void enforceCallingOrSelfPermission(
1793 String permission, String message) {
1794 enforce(permission,
1795 checkCallingOrSelfPermission(permission),
1796 true,
1797 Binder.getCallingUid(),
1798 message);
1799 }
1800
1801 @Override
1802 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1803 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001804 ActivityManager.getService().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001805 mMainThread.getApplicationThread(), toPackage,
1806 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001808 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
1810 }
1811
1812 @Override
1813 public void revokeUriPermission(Uri uri, int modeFlags) {
1814 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001815 ActivityManager.getService().revokeUriPermission(
Dianne Hackborna47223f2017-03-30 13:49:13 -07001816 mMainThread.getApplicationThread(), null,
1817 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
1818 } catch (RemoteException e) {
1819 throw e.rethrowFromSystemServer();
1820 }
1821 }
1822
1823 @Override
1824 public void revokeUriPermission(String targetPackage, Uri uri, int modeFlags) {
1825 try {
1826 ActivityManager.getService().revokeUriPermission(
1827 mMainThread.getApplicationThread(), targetPackage,
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001828 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001830 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
1832 }
1833
1834 @Override
1835 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001837 return ActivityManager.getService().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001838 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001839 resolveUserId(uri), null);
1840 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001841 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001842 }
1843 }
1844
1845 /** @hide */
1846 @Override
1847 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1848 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001849 return ActivityManager.getService().checkUriPermission(
Dianne Hackbornff170242014-11-19 10:59:01 -08001850 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1851 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001853 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855 }
1856
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001857 private int resolveUserId(Uri uri) {
1858 return ContentProvider.getUserIdFromUri(uri, getUserId());
1859 }
1860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 @Override
1862 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 int pid = Binder.getCallingPid();
1864 if (pid != Process.myPid()) {
1865 return checkUriPermission(uri, pid,
1866 Binder.getCallingUid(), modeFlags);
1867 }
1868 return PackageManager.PERMISSION_DENIED;
1869 }
1870
1871 @Override
1872 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1873 return checkUriPermission(uri, Binder.getCallingPid(),
1874 Binder.getCallingUid(), modeFlags);
1875 }
1876
1877 @Override
1878 public int checkUriPermission(Uri uri, String readPermission,
1879 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001880 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1882 + readPermission + " writePermission=" + writePermission
1883 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1884 }
1885 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1886 if (readPermission == null
1887 || checkPermission(readPermission, pid, uid)
1888 == PackageManager.PERMISSION_GRANTED) {
1889 return PackageManager.PERMISSION_GRANTED;
1890 }
1891 }
1892 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1893 if (writePermission == null
1894 || checkPermission(writePermission, pid, uid)
1895 == PackageManager.PERMISSION_GRANTED) {
1896 return PackageManager.PERMISSION_GRANTED;
1897 }
1898 }
1899 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1900 : PackageManager.PERMISSION_DENIED;
1901 }
1902
1903 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001904 StringBuilder builder = new StringBuilder();
1905 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1906 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001908 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1909 builder.append("write and ");
1910 }
1911 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1912 builder.append("persistable and ");
1913 }
1914 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1915 builder.append("prefix and ");
1916 }
1917
1918 if (builder.length() > 5) {
1919 builder.setLength(builder.length() - 5);
1920 return builder.toString();
1921 } else {
1922 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
1925
1926 private void enforceForUri(
1927 int modeFlags, int resultOfCheck, boolean selfToo,
1928 int uid, Uri uri, String message) {
1929 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1930 throw new SecurityException(
1931 (message != null ? (message + ": ") : "") +
1932 (selfToo
1933 ? "Neither user " + uid + " nor current process has "
1934 : "User " + uid + " does not have ") +
1935 uriModeFlagToString(modeFlags) +
1936 " permission on " +
1937 uri +
1938 ".");
1939 }
1940 }
1941
Jeff Brown6e539312015-02-24 18:53:21 -08001942 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 public void enforceUriPermission(
1944 Uri uri, int pid, int uid, int modeFlags, String message) {
1945 enforceForUri(
1946 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1947 false, uid, uri, message);
1948 }
1949
Jeff Brown6e539312015-02-24 18:53:21 -08001950 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 public void enforceCallingUriPermission(
1952 Uri uri, int modeFlags, String message) {
1953 enforceForUri(
1954 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001955 false,
1956 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958
Jeff Brown6e539312015-02-24 18:53:21 -08001959 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 public void enforceCallingOrSelfUriPermission(
1961 Uri uri, int modeFlags, String message) {
1962 enforceForUri(
1963 modeFlags,
1964 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1965 Binder.getCallingUid(), uri, message);
1966 }
1967
Jeff Brown6e539312015-02-24 18:53:21 -08001968 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 public void enforceUriPermission(
1970 Uri uri, String readPermission, String writePermission,
1971 int pid, int uid, int modeFlags, String message) {
1972 enforceForUri(modeFlags,
1973 checkUriPermission(
1974 uri, readPermission, writePermission, pid, uid,
1975 modeFlags),
1976 false,
1977 uid,
1978 uri,
1979 message);
1980 }
1981
Tom O'Neill365632e2013-09-09 09:34:58 -07001982 /**
1983 * Logs a warning if the system process directly called a method such as
1984 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1985 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1986 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001987 private void warnIfCallingFromSystemProcess() {
1988 if (Process.myUid() == Process.SYSTEM_UID) {
1989 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001990 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001991 }
1992 }
1993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001995 public Context createApplicationContext(ApplicationInfo application, int flags)
1996 throws NameNotFoundException {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001997 LoadedApk loadedApk = mMainThread.getLoadedApk(application,
1998 mResources.getCompatibilityInfo(),
Svetoslav976e8bd2014-07-16 15:12:03 -07001999 flags | CONTEXT_REGISTER_PACKAGE);
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002000 if (loadedApk != null) {
2001 ContextImpl c = new ContextImpl(this, mMainThread, loadedApk, null, mActivityToken,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002002 new UserHandle(UserHandle.getUserId(application.uid)), flags, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002003
2004 final int displayId = mDisplay != null
2005 ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
2006
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002007 c.setResources(loadedApk.createResources(mActivityToken, null, displayId, null,
Jason Monkbd983642017-02-24 16:57:40 -05002008 getDisplayAdjustments(displayId).getCompatibilityInfo()));
Svetoslav976e8bd2014-07-16 15:12:03 -07002009 if (c.mResources != null) {
2010 return c;
2011 }
2012 }
2013
2014 throw new PackageManager.NameNotFoundException(
2015 "Application package " + application.packageName + " not found");
2016 }
2017
2018 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07002020 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07002021 return createPackageContextAsUser(packageName, flags,
2022 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07002023 }
2024
2025 @Override
2026 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
2027 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00002028 if (packageName.equals("system") || packageName.equals("android")) {
Adam Lesinski8fa71072016-11-18 18:13:55 -08002029 // The system resources are loaded in every application, so we can safely copy
2030 // the context without reloading Resources.
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002031 return new ContextImpl(this, mMainThread, mLoadedApk, null, mActivityToken, user,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002032 flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 }
2034
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002035 LoadedApk loadedApk = mMainThread.getLoadedApkForPackageName(packageName,
2036 mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002037 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002038 if (loadedApk != null) {
2039 ContextImpl c = new ContextImpl(this, mMainThread, loadedApk, null, mActivityToken, user,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002040 flags, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002041
2042 final int displayId = mDisplay != null
2043 ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
2044
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002045 c.setResources(loadedApk.createResources(mActivityToken, null, displayId, null,
Jason Monkbd983642017-02-24 16:57:40 -05002046 getDisplayAdjustments(displayId).getCompatibilityInfo()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 if (c.mResources != null) {
2048 return c;
2049 }
2050 }
2051
2052 // Should be a better exception.
2053 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07002054 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
2056
Romain Guy870e09f2009-07-06 16:35:25 -07002057 @Override
Adam Lesinski4e862812016-11-21 16:02:24 -08002058 public Context createContextForSplit(String splitName) throws NameNotFoundException {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002059 if (!mLoadedApk.getApplicationInfo().requestsIsolatedSplitLoading()) {
Adam Lesinski4e862812016-11-21 16:02:24 -08002060 // All Splits are always loaded.
2061 return this;
2062 }
2063
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002064 final ClassLoader classLoader = mLoadedApk.getSplitClassLoader(splitName);
Adam Lesinski4e862812016-11-21 16:02:24 -08002065
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002066 final ContextImpl context = new ContextImpl(this, mMainThread, mLoadedApk, splitName,
Adam Lesinski4e862812016-11-21 16:02:24 -08002067 mActivityToken, mUser, mFlags, classLoader);
2068
2069 final int displayId = mDisplay != null
2070 ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
2071
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002072 context.setResources(mLoadedApk.getOrCreateResourcesForSplit(splitName,
2073 mActivityToken, displayId));
Adam Lesinski4e862812016-11-21 16:02:24 -08002074 return context;
2075 }
2076
2077 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07002078 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07002079 if (overrideConfiguration == null) {
2080 throw new IllegalArgumentException("overrideConfiguration must not be null");
2081 }
2082
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002083 ContextImpl context = new ContextImpl(this, mMainThread, mLoadedApk, mSplitName,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002084 mActivityToken, mUser, mFlags, mClassLoader);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002085
2086 final int displayId = mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002087 context.setResources(mLoadedApk.createResources(mActivityToken, mSplitName, displayId,
Jason Monkbd983642017-02-24 16:57:40 -05002088 overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo()));
Adam Lesinski8fa71072016-11-18 18:13:55 -08002089 return context;
Dianne Hackborn756220b2012-08-14 16:45:30 -07002090 }
2091
2092 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07002093 public Context createDisplayContext(Display display) {
2094 if (display == null) {
2095 throw new IllegalArgumentException("display must not be null");
2096 }
2097
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002098 ContextImpl context = new ContextImpl(this, mMainThread, mLoadedApk, mSplitName,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002099 mActivityToken, mUser, mFlags, mClassLoader);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002100
2101 final int displayId = display.getDisplayId();
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002102 context.setResources(mLoadedApk.createResources(mActivityToken, mSplitName, displayId,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002103 null, getDisplayAdjustments(displayId).getCompatibilityInfo()));
Adam Lesinski8fa71072016-11-18 18:13:55 -08002104 context.mDisplay = display;
2105 return context;
Jeff Browna492c3a2012-08-23 19:48:44 -07002106 }
2107
Jeff Browna492c3a2012-08-23 19:48:44 -07002108 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002109 public Context createDeviceProtectedStorageContext() {
2110 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
2111 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002112 return new ContextImpl(this, mMainThread, mLoadedApk, mSplitName, mActivityToken, mUser,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002113 flags, mClassLoader);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002114 }
2115
2116 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002117 public Context createCredentialProtectedStorageContext() {
2118 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
2119 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002120 return new ContextImpl(this, mMainThread, mLoadedApk, mSplitName, mActivityToken, mUser,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002121 flags, mClassLoader);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002122 }
2123
2124 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07002125 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002126 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
2127 }
2128
2129 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002130 public boolean isDeviceProtectedStorage() {
2131 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002132 }
2133
2134 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002135 public boolean isCredentialProtectedStorage() {
2136 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07002137 }
2138
Jeff Brown98365d72012-08-19 20:30:52 -07002139 @Override
Seigo Nonaka6d6cd682017-06-22 08:22:18 -07002140 public boolean canLoadUnsafeResources() {
2141 if (getPackageName().equals(getOpPackageName())) {
2142 return true;
2143 }
2144 return (mFlags & Context.CONTEXT_IGNORE_SECURITY) != 0;
2145 }
2146
2147 @Override
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002148 public Display getDisplay() {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002149 if (mDisplay == null) {
2150 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
Bryce Lee609bf652017-02-09 16:50:13 -08002151 mResources);
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002152 }
2153
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002154 return mDisplay;
2155 }
2156
2157 @Override
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002158 public void updateDisplay(int displayId) {
Bryce Lee609bf652017-02-09 16:50:13 -08002159 mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002160 }
2161
2162 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07002163 public DisplayAdjustments getDisplayAdjustments(int displayId) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002164 return mResources.getDisplayAdjustments();
Jeff Brown98365d72012-08-19 20:30:52 -07002165 }
2166
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002167 @Override
2168 public File getDataDir() {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002169 if (mLoadedApk != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07002170 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002171 if (isCredentialProtectedStorage()) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002172 res = mLoadedApk.getCredentialProtectedDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002173 } else if (isDeviceProtectedStorage()) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002174 res = mLoadedApk.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002175 } else {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002176 res = mLoadedApk.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002177 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002178
2179 if (res != null) {
Jeff Sharkey21f50722016-04-27 12:38:02 -06002180 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Jeff Sharkey24492ae2016-04-25 13:20:25 -06002181 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
2182 new Throwable());
2183 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002184 return res;
2185 } else {
2186 throw new RuntimeException(
2187 "No data directory found for package " + getPackageName());
2188 }
2189 } else {
2190 throw new RuntimeException(
2191 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 }
2194
2195 @Override
2196 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07002197 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002199 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 if (!file.exists()) {
2201 file.mkdir();
2202 setFilePermissionsFromMode(file.getPath(), mode,
2203 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
2204 }
2205 return file;
2206 }
2207
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002208 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08002209 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002210 public int getUserId() {
2211 return mUser.getIdentifier();
2212 }
2213
Felipe Lemebb567ae2017-10-04 09:56:21 -07002214 /** @hide */
2215 @Override
2216 public AutofillClient getAutofillClient() {
2217 return mAutofillClient;
2218 }
2219
2220 /** @hide */
2221 @Override
2222 public void setAutofillClient(AutofillClient client) {
2223 mAutofillClient = client;
2224 }
2225
Dianne Hackborn21556372010-02-04 16:34:40 -08002226 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002227 LoadedApk loadedApk = new LoadedApk(mainThread);
2228 ContextImpl context = new ContextImpl(null, mainThread, loadedApk, null, null, null, 0,
Adam Lesinski4e862812016-11-21 16:02:24 -08002229 null);
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002230 context.setResources(loadedApk.getResources());
Jeff Browndefd4a62014-03-10 21:24:37 -07002231 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002232 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 return context;
2234 }
2235
Adam Lesinskia82b6262017-03-21 16:56:17 -07002236 /**
2237 * System Context to be used for UI. This Context has resources that can be themed.
Adam Lesinski6f1a9172017-04-10 16:35:19 -07002238 * Make sure that the created system UI context shares the same LoadedApk as the system context.
Adam Lesinskia82b6262017-03-21 16:56:17 -07002239 */
Adam Lesinski6f1a9172017-04-10 16:35:19 -07002240 static ContextImpl createSystemUiContext(ContextImpl systemContext) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002241 final LoadedApk loadedApk = systemContext.mLoadedApk;
2242 ContextImpl context = new ContextImpl(null, systemContext.mMainThread, loadedApk, null,
Adam Lesinski6f1a9172017-04-10 16:35:19 -07002243 null, null, 0, null);
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002244 context.setResources(loadedApk.createResources(null, null, Display.DEFAULT_DISPLAY, null,
2245 loadedApk.getCompatibilityInfo()));
Adam Lesinskia82b6262017-03-21 16:56:17 -07002246 return context;
2247 }
2248
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002249 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk loadedApk) {
2250 if (loadedApk == null) throw new IllegalArgumentException("loadedApk");
2251 ContextImpl context = new ContextImpl(null, mainThread, loadedApk, null, null, null, 0,
Adam Lesinski4e862812016-11-21 16:02:24 -08002252 null);
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002253 context.setResources(loadedApk.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002254 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002255 }
2256
2257 static ContextImpl createActivityContext(ActivityThread mainThread,
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002258 LoadedApk loadedApk, ActivityInfo activityInfo, IBinder activityToken, int displayId,
Adam Lesinski082614c2016-03-04 14:33:47 -08002259 Configuration overrideConfiguration) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002260 if (loadedApk == null) throw new IllegalArgumentException("loadedApk");
Adam Lesinski8fa71072016-11-18 18:13:55 -08002261
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002262 String[] splitDirs = loadedApk.getSplitResDirs();
2263 ClassLoader classLoader = loadedApk.getClassLoader();
Adam Lesinski4e862812016-11-21 16:02:24 -08002264
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002265 if (loadedApk.getApplicationInfo().requestsIsolatedSplitLoading()) {
Adam Lesinski4e862812016-11-21 16:02:24 -08002266 Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "SplitDependencies");
2267 try {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002268 classLoader = loadedApk.getSplitClassLoader(activityInfo.splitName);
2269 splitDirs = loadedApk.getSplitPaths(activityInfo.splitName);
Adam Lesinski4e862812016-11-21 16:02:24 -08002270 } catch (NameNotFoundException e) {
2271 // Nothing above us can handle a NameNotFoundException, better crash.
2272 throw new RuntimeException(e);
2273 } finally {
2274 Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2275 }
2276 }
2277
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002278 ContextImpl context = new ContextImpl(null, mainThread, loadedApk, activityInfo.splitName,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002279 activityToken, null, 0, classLoader);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002280
2281 // Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
2282 displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
2283
2284 final CompatibilityInfo compatInfo = (displayId == Display.DEFAULT_DISPLAY)
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002285 ? loadedApk.getCompatibilityInfo()
Adam Lesinski8fa71072016-11-18 18:13:55 -08002286 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
2287
Adam Lesinskic9701942016-11-28 19:44:19 -08002288 final ResourcesManager resourcesManager = ResourcesManager.getInstance();
2289
2290 // Create the base resources for which all configuration contexts for this Activity
2291 // will be rebased upon.
Jason Monkbd983642017-02-24 16:57:40 -05002292 context.setResources(resourcesManager.createBaseActivityResources(activityToken,
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002293 loadedApk.getResDir(),
Adam Lesinski4e862812016-11-21 16:02:24 -08002294 splitDirs,
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002295 loadedApk.getOverlayDirs(),
2296 loadedApk.getApplicationInfo().sharedLibraryFiles,
Adam Lesinskic9701942016-11-28 19:44:19 -08002297 displayId,
2298 overrideConfiguration,
2299 compatInfo,
Jason Monkbd983642017-02-24 16:57:40 -05002300 classLoader));
Adam Lesinskic9701942016-11-28 19:44:19 -08002301 context.mDisplay = resourcesManager.getAdjustedDisplay(displayId,
Bryce Lee609bf652017-02-09 16:50:13 -08002302 context.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002303 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002304 }
2305
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002306 private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread,
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002307 @NonNull LoadedApk loadedApk, @Nullable String splitName,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002308 @Nullable IBinder activityToken, @Nullable UserHandle user, int flags,
2309 @Nullable ClassLoader classLoader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002312 // If creator didn't specify which storage to use, use the default
2313 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002314 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2315 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002316 final File dataDir = loadedApk.getDataDirFile();
2317 if (Objects.equals(dataDir, loadedApk.getCredentialProtectedDataDirFile())) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002318 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002319 } else if (Objects.equals(dataDir, loadedApk.getDeviceProtectedDataDirFile())) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002320 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002321 }
2322 }
2323
Jeff Browndefd4a62014-03-10 21:24:37 -07002324 mMainThread = mainThread;
2325 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002326 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327
Jeff Browndefd4a62014-03-10 21:24:37 -07002328 if (user == null) {
2329 user = Process.myUserHandle();
2330 }
2331 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002332
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002333 mLoadedApk = loadedApk;
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002334 mSplitName = splitName;
Adam Lesinski4e862812016-11-21 16:02:24 -08002335 mClassLoader = classLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -07002336 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002337
Jeff Browndefd4a62014-03-10 21:24:37 -07002338 if (container != null) {
2339 mBasePackageName = container.mBasePackageName;
2340 mOpPackageName = container.mOpPackageName;
Jason Monkbd983642017-02-24 16:57:40 -05002341 setResources(container.mResources);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002342 mDisplay = container.mDisplay;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002343 } else {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002344 mBasePackageName = loadedApk.mPackageName;
2345 ApplicationInfo ainfo = loadedApk.getApplicationInfo();
Dianne Hackborn95d78532013-09-11 09:51:14 -07002346 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2347 // Special case: system components allow themselves to be loaded in to other
2348 // processes. For purposes of app ops, we must then consider the context as
2349 // belonging to the package of this process, not the system itself, otherwise
2350 // the package+uid verifications in app ops will fail.
2351 mOpPackageName = ActivityThread.currentPackageName();
2352 } else {
2353 mOpPackageName = mBasePackageName;
2354 }
2355 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002356
2357 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
2359
Jason Monkbd983642017-02-24 16:57:40 -05002360 void setResources(Resources r) {
Jason Monkbd60e5b2017-03-02 12:55:00 -05002361 if (r instanceof CompatResources) {
2362 ((CompatResources) r).setContext(this);
Jason Monkbd983642017-02-24 16:57:40 -05002363 }
Jason Monkbd60e5b2017-03-02 12:55:00 -05002364 mResources = r;
Jason Monkbd983642017-02-24 16:57:40 -05002365 }
2366
Narayan Kamath29564cd2014-08-07 10:57:40 +01002367 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002368 mLoadedApk.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370
2371 final void scheduleFinalCleanup(String who, String what) {
2372 mMainThread.scheduleContextCleanup(this, who, what);
2373 }
2374
2375 final void performFinalCleanup(String who, String what) {
2376 //Log.i(TAG, "Cleanup up context: " + this);
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04002377 mLoadedApk.removeContextRegistrations(getOuterContext(), who, what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
2379
2380 final Context getReceiverRestrictedContext() {
2381 if (mReceiverRestrictedContext != null) {
2382 return mReceiverRestrictedContext;
2383 }
2384 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2385 }
2386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 final void setOuterContext(Context context) {
2388 mOuterContext = context;
2389 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 final Context getOuterContext() {
2392 return mOuterContext;
2393 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002394
Tony Mak46aabe52016-11-14 12:53:06 +00002395 @Override
2396 public IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 return mActivityToken;
2398 }
2399
Jeff Sharkey634dc422016-01-30 17:44:15 -07002400 private void checkMode(int mode) {
2401 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2402 if ((mode & MODE_WORLD_READABLE) != 0) {
2403 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2404 }
2405 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2406 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2407 }
2408 }
2409 }
2410
Jeff Brown6e539312015-02-24 18:53:21 -08002411 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002412 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 int extraPermissions) {
2414 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2415 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2416 |extraPermissions;
2417 if ((mode&MODE_WORLD_READABLE) != 0) {
2418 perms |= FileUtils.S_IROTH;
2419 }
2420 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2421 perms |= FileUtils.S_IWOTH;
2422 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002423 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2425 + ", perms=0x" + Integer.toHexString(perms));
2426 }
2427 FileUtils.setPermissions(name, perms, -1, -1);
2428 }
2429
2430 private File makeFilename(File base, String name) {
2431 if (name.indexOf(File.separatorChar) < 0) {
2432 return new File(base, name);
2433 }
2434 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002435 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
2437
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002438 /**
2439 * Ensure that given directories exist, trying to create them if missing. If
2440 * unable to create, they are filtered by replacing with {@code null}.
2441 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002442 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkeyae266462017-11-27 13:32:24 -07002443 final StorageManager sm = getSystemService(StorageManager.class);
2444 final File[] result = new File[dirs.length];
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002445 for (int i = 0; i < dirs.length; i++) {
2446 File dir = dirs[i];
2447 if (!dir.exists()) {
2448 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002449 // recheck existence in case of cross-process race
2450 if (!dir.exists()) {
2451 // Failing to mkdir() may be okay, since we might not have
2452 // enough permissions; ask vold to create on our behalf.
Christopher Tatecc866da2013-10-02 18:11:01 -07002453 try {
Jeff Sharkeyae266462017-11-27 13:32:24 -07002454 sm.mkdirs(dir);
Jeff Sharkey983294592015-07-13 10:25:31 -07002455 } catch (Exception e) {
2456 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002457 dir = null;
2458 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002459 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002460 }
2461 }
2462 result[i] = dir;
2463 }
2464 return result;
2465 }
2466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 // ----------------------------------------------------------------------
2468 // ----------------------------------------------------------------------
2469 // ----------------------------------------------------------------------
2470
2471 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002472 private final ActivityThread mMainThread;
2473 private final UserHandle mUser;
2474
2475 public ApplicationContentResolver(
2476 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002478 mMainThread = Preconditions.checkNotNull(mainThread);
2479 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 }
2481
2482 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002483 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002484 return mMainThread.acquireProvider(context,
2485 ContentProvider.getAuthorityWithoutUserId(auth),
2486 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 }
2488
2489 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002490 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002491 return mMainThread.acquireExistingProvider(context,
2492 ContentProvider.getAuthorityWithoutUserId(auth),
2493 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002494 }
2495
2496 @Override
2497 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002498 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002500
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002501 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002502 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002503 return mMainThread.acquireProvider(c,
2504 ContentProvider.getAuthorityWithoutUserId(auth),
2505 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002506 }
2507
2508 @Override
2509 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002510 return mMainThread.releaseProvider(icp, false);
2511 }
2512
2513 @Override
2514 public void unstableProviderDied(IContentProvider icp) {
2515 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002516 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002517
2518 @Override
2519 public void appNotRespondingViaProvider(IContentProvider icp) {
2520 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2521 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002522
2523 /** @hide */
2524 protected int resolveUserIdFromAuthority(String auth) {
2525 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528}