blob: 69be171ee46edbe5e23742986d75e91ee955b8cb [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
Makoto Onukid67070e2018-03-30 12:39:14 -070019import android.annotation.IntDef;
Adam Lesinski4ece3d62016-06-16 18:05:41 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Wale Ogunwale691af682019-02-11 03:09:10 -080022import android.annotation.TestApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010023import android.annotation.UnsupportedAppUsage;
Felipe Lemea4f39cd2019-02-19 15:08:59 -080024import android.content.AutofillOptions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.BroadcastReceiver;
26import android.content.ComponentName;
Felipe Leme326f15a2019-02-19 09:42:24 -080027import android.content.ContentCaptureOptions;
Nicolas Prevotd85fc722014-04-16 19:52:08 +010028import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.ContentResolver;
30import android.content.Context;
31import android.content.ContextWrapper;
32import android.content.IContentProvider;
Jeff Brown6e539312015-02-24 18:53:21 -080033import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.Intent;
35import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070036import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.ReceiverCallNotAllowedException;
38import android.content.ServiceConnection;
39import android.content.SharedPreferences;
Adam Lesinski4e862812016-11-21 16:02:24 -080040import android.content.pm.ActivityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070044import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.res.AssetManager;
Jason Monkbd983642017-02-24 16:57:40 -050046import android.content.res.CompatResources;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070047import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070048import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070050import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.database.sqlite.SQLiteDatabase;
52import android.database.sqlite.SQLiteDatabase.CursorFactory;
53import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080057import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070059import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080060import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.FileUtils;
62import android.os.Handler;
63import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.os.RemoteException;
Adam Lesinski4e862812016-11-21 16:02:24 -080067import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070068import android.os.UserHandle;
Jeff Sharkeya65e6492017-06-21 13:45:11 -060069import android.os.UserManager;
Jeff Sharkeyae266462017-11-27 13:32:24 -070070import android.os.storage.StorageManager;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -070071import android.system.ErrnoException;
72import android.system.Os;
73import android.system.OsConstants;
Jeff Sharkey83aacde2017-03-22 23:13:39 -060074import android.system.StructStat;
Jordan Liua32e6fc2019-10-29 10:33:55 -070075import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070077import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070079import android.util.Slog;
Jeff Brown98365d72012-08-19 20:30:52 -070080import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080081import android.view.DisplayAdjustments;
Felipe Lemebb567ae2017-10-04 09:56:21 -070082import android.view.autofill.AutofillManager.AutofillClient;
Dan Egnor95240272009-10-27 18:23:39 -070083
Jeff Sharkey35871f22016-01-29 17:13:29 -070084import com.android.internal.annotations.GuardedBy;
85import com.android.internal.util.Preconditions;
86
Jeff Sharkeydd02e332018-06-27 14:41:57 -060087import dalvik.system.BlockGuard;
88
Jeff Sharkey83aacde2017-03-22 23:13:39 -060089import libcore.io.Memory;
Adam Lesinski4e862812016-11-21 16:02:24 -080090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.io.File;
92import java.io.FileInputStream;
93import java.io.FileNotFoundException;
94import java.io.FileOutputStream;
Jeff Sharkey35871f22016-01-29 17:13:29 -070095import java.io.FilenameFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import java.io.IOException;
97import java.io.InputStream;
Makoto Onukid67070e2018-03-30 12:39:14 -070098import java.lang.annotation.Retention;
99import java.lang.annotation.RetentionPolicy;
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600100import java.nio.ByteOrder;
Christopher Tatefe2368c2017-05-17 15:42:35 -0700101import java.util.ArrayList;
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700102import java.util.Objects;
Jeff Sharkey8439ac02017-12-12 17:26:23 -0700103import java.util.concurrent.Executor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105class ReceiverRestrictedContext extends ContextWrapper {
Andrei Onea4b2116c2019-02-28 15:13:42 +0000106 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 ReceiverRestrictedContext(Context base) {
108 super(base);
109 }
110
111 @Override
112 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
113 return registerReceiver(receiver, filter, null, null);
114 }
115
116 @Override
117 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
118 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700119 if (receiver == null) {
120 // Allow retrieving current sticky broadcast; this is safe since we
121 // aren't actually registering a receiver.
122 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
123 } else {
124 throw new ReceiverCallNotAllowedException(
125 "BroadcastReceiver components are not allowed to register to receive intents");
126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 }
128
129 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700130 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
131 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700132 if (receiver == null) {
133 // Allow retrieving current sticky broadcast; this is safe since we
134 // aren't actually registering a receiver.
135 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
136 } else {
137 throw new ReceiverCallNotAllowedException(
138 "BroadcastReceiver components are not allowed to register to receive intents");
139 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700140 }
141
142 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
144 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700145 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800147
148 @Override
Bo Liu58a57662019-03-06 20:21:45 +0000149 public boolean bindService(
150 Intent service, int flags, Executor executor, ServiceConnection conn) {
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800151 throw new ReceiverCallNotAllowedException(
Bo Liu58a57662019-03-06 20:21:45 +0000152 "BroadcastReceiver components are not allowed to bind to services");
153 }
154
155 @Override
156 public boolean bindIsolatedService(Intent service, int flags, String instanceName,
157 Executor executor, ServiceConnection conn) {
158 throw new ReceiverCallNotAllowedException(
159 "BroadcastReceiver components are not allowed to bind to services");
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161}
162
163/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800164 * Common implementation of Context API, which provides the base
165 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800167class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800168 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700169 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600171 private static final String XATTR_INODE_CACHE = "user.inode_cache";
172 private static final String XATTR_INODE_CODE_CACHE = "user.inode_code_cache";
173
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700174 /**
175 * Map from package name, to preference name, to cached preferences.
176 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700177 @GuardedBy("ContextImpl.class")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100178 @UnsupportedAppUsage
Jeff Sharkeybe782582016-02-15 18:35:57 -0700179 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
180
181 /**
182 * Map from preference name to generated path.
183 */
184 @GuardedBy("ContextImpl.class")
Mathew Inwood8c854f82018-09-14 12:35:36 +0100185 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkeybe782582016-02-15 18:35:57 -0700186 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100188 @UnsupportedAppUsage
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800189 final @NonNull ActivityThread mMainThread;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100190 @UnsupportedAppUsage
Todd Kennedy233a0b12018-01-29 20:30:24 +0000191 final @NonNull LoadedApk mPackageInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100192 @UnsupportedAppUsage
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800193 private @Nullable ClassLoader mClassLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -0700194
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800195 private final @Nullable IBinder mActivityToken;
Jeff Browndefd4a62014-03-10 21:24:37 -0700196
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700197 private final @NonNull UserHandle mUser;
Jeff Browndefd4a62014-03-10 21:24:37 -0700198
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100199 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -0700200 private final ApplicationContentResolver mContentResolver;
201
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100202 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -0700203 private final String mBasePackageName;
Mathew Inwood8c854f82018-09-14 12:35:36 +0100204 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Browndefd4a62014-03-10 21:24:37 -0700205 private final String mOpPackageName;
206
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700207 private final @NonNull ResourcesManager mResourcesManager;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100208 @UnsupportedAppUsage
Adam Lesinski8fa71072016-11-18 18:13:55 -0800209 private @NonNull Resources mResources;
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700210 private @Nullable Display mDisplay; // may be null if default display
Jeff Browndefd4a62014-03-10 21:24:37 -0700211
Mathew Inwood8c854f82018-09-14 12:35:36 +0100212 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700213 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700214
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100215 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 private Context mOuterContext;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100217 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 private int mThemeResource = 0;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100219 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 private Resources.Theme mTheme = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100221 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800225 // The name of the split this Context is representing. May be null.
226 private @Nullable String mSplitName = null;
227
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800228 private @Nullable AutofillClient mAutofillClient = null;
229 private @Nullable AutofillOptions mAutofillOptions;
Felipe Lemebb567ae2017-10-04 09:56:21 -0700230
Felipe Leme326f15a2019-02-19 09:42:24 -0800231 private ContentCaptureOptions mContentCaptureOptions = null;
Felipe Lemeecb08be2018-11-27 15:48:47 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 private final Object mSync = new Object();
234
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700235 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700237 @GuardedBy("mSync")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100238 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700240 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700242 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700243 private File mNoBackupFilesDir;
244 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700246 @GuardedBy("mSync")
247 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700248
Jeff Brown6e539312015-02-24 18:53:21 -0800249 // The system service cache for the system services that are cached per-ContextImpl.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100250 @UnsupportedAppUsage
Jeff Brown6e539312015-02-24 18:53:21 -0800251 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800252
Makoto Onukif896f122018-01-25 09:50:24 -0800253 static final int STATE_UNINITIALIZED = 0;
254 static final int STATE_INITIALIZING = 1;
255 static final int STATE_READY = 2;
Makoto Onukid67070e2018-03-30 12:39:14 -0700256 static final int STATE_NOT_FOUND = 3;
257
258 /** @hide */
259 @IntDef(prefix = { "STATE_" }, value = {
260 STATE_UNINITIALIZED,
261 STATE_INITIALIZING,
262 STATE_READY,
263 STATE_NOT_FOUND,
264 })
265 @Retention(RetentionPolicy.SOURCE)
266 @interface ServiceInitializationState {}
Makoto Onukif896f122018-01-25 09:50:24 -0800267
268 /**
269 * Initialization state for each service. Any of {@link #STATE_UNINITIALIZED},
270 * {@link #STATE_INITIALIZING} or {@link #STATE_READY},
271 */
Makoto Onukid67070e2018-03-30 12:39:14 -0700272 @ServiceInitializationState
273 final int[] mServiceInitializationStateArray = new int[mServiceCache.length];
Makoto Onukif896f122018-01-25 09:50:24 -0800274
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100275 @UnsupportedAppUsage
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700276 static ContextImpl getImpl(Context context) {
277 Context nextContext;
278 while ((context instanceof ContextWrapper) &&
279 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
280 context = nextContext;
281 }
282 return (ContextImpl)context;
283 }
284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 @Override
286 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700287 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 }
289
290 @Override
291 public Resources getResources() {
292 return mResources;
293 }
294
295 @Override
296 public PackageManager getPackageManager() {
297 if (mPackageManager != null) {
298 return mPackageManager;
299 }
300
301 IPackageManager pm = ActivityThread.getPackageManager();
302 if (pm != null) {
303 // Doesn't matter if we make more than one instance.
304 return (mPackageManager = new ApplicationPackageManager(this, pm));
305 }
306
307 return null;
308 }
309
310 @Override
311 public ContentResolver getContentResolver() {
312 return mContentResolver;
313 }
314
315 @Override
316 public Looper getMainLooper() {
317 return mMainThread.getLooper();
318 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 @Override
Jeff Sharkey8439ac02017-12-12 17:26:23 -0700321 public Executor getMainExecutor() {
322 return mMainThread.getExecutor();
323 }
324
325 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 public Context getApplicationContext() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000327 return (mPackageInfo != null) ?
328 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700332 public void setTheme(int resId) {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700333 synchronized (mSync) {
334 if (mThemeResource != resId) {
335 mThemeResource = resId;
336 initializeTheme();
337 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800342 public int getThemeResId() {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700343 synchronized (mSync) {
344 return mThemeResource;
345 }
Dianne Hackborn247fe742011-01-08 17:25:57 -0800346 }
347
348 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 public Resources.Theme getTheme() {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700350 synchronized (mSync) {
351 if (mTheme != null) {
352 return mTheme;
353 }
354
355 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
356 getOuterContext().getApplicationInfo().targetSdkVersion);
357 initializeTheme();
358
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700359 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700363 private void initializeTheme() {
364 if (mTheme == null) {
365 mTheme = mResources.newTheme();
366 }
367 mTheme.applyStyle(mThemeResource, true);
368 }
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 @Override
371 public ClassLoader getClassLoader() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000372 return mClassLoader != null ? mClassLoader : (mPackageInfo != null ? mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 }
374
375 @Override
376 public String getPackageName() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000377 if (mPackageInfo != null) {
378 return mPackageInfo.getPackageName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
Todd Kennedy233a0b12018-01-29 20:30:24 +0000380 // No mPackageInfo means this is a Context for the system itself,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800381 // and this here is its name.
382 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
384
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800385 /** @hide */
386 @Override
387 public String getBasePackageName() {
388 return mBasePackageName != null ? mBasePackageName : getPackageName();
389 }
390
Dianne Hackborn95d78532013-09-11 09:51:14 -0700391 /** @hide */
392 @Override
393 public String getOpPackageName() {
394 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
395 }
396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700398 public ApplicationInfo getApplicationInfo() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000399 if (mPackageInfo != null) {
400 return mPackageInfo.getApplicationInfo();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700401 }
402 throw new RuntimeException("Not supported in system context");
403 }
404
405 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 public String getPackageResourcePath() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000407 if (mPackageInfo != null) {
408 return mPackageInfo.getResDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410 throw new RuntimeException("Not supported in system context");
411 }
412
413 @Override
414 public String getPackageCodePath() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000415 if (mPackageInfo != null) {
416 return mPackageInfo.getAppDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 throw new RuntimeException("Not supported in system context");
419 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200420
Jeff Brown6e539312015-02-24 18:53:21 -0800421 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700423 // At least one application in the world actually passes in a null
424 // name. This happened to work because when we generated the file name
425 // we would stringify it to "null.xml". Nice.
Todd Kennedy233a0b12018-01-29 20:30:24 +0000426 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700427 Build.VERSION_CODES.KITKAT) {
428 if (name == null) {
429 name = "null";
430 }
431 }
432
Jeff Sharkeybe782582016-02-15 18:35:57 -0700433 File file;
434 synchronized (ContextImpl.class) {
435 if (mSharedPrefsPaths == null) {
436 mSharedPrefsPaths = new ArrayMap<>();
437 }
438 file = mSharedPrefsPaths.get(name);
439 if (file == null) {
440 file = getSharedPreferencesPath(name);
441 mSharedPrefsPaths.put(name, file);
442 }
443 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700444 return getSharedPreferences(file, mode);
445 }
446
447 @Override
448 public SharedPreferences getSharedPreferences(File file, int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700450 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700451 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
452 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700453 if (sp == null) {
John Reckf3903e92017-05-12 17:12:54 -0700454 checkMode(mode);
455 if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.O) {
456 if (isCredentialProtectedStorage()
Jeff Sharkeya65e6492017-06-21 13:45:11 -0600457 && !getSystemService(UserManager.class)
458 .isUserUnlockingOrUnlocked(UserHandle.myUserId())) {
John Reckf3903e92017-05-12 17:12:54 -0700459 throw new IllegalStateException("SharedPreferences in credential encrypted "
460 + "storage are not available until after user is unlocked");
461 }
462 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700463 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700464 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700465 return sp;
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800468 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
469 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
470 // If somebody else (some other process) changed the prefs
471 // file behind our back, we reload it. This has been the
472 // historical (if undocumented) behavior.
473 sp.startReloadIfChangedUnexpectedly();
474 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700475 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477
Andreas Gampe3f24e692018-02-05 13:24:28 -0800478 @GuardedBy("ContextImpl.class")
Jeff Sharkey35871f22016-01-29 17:13:29 -0700479 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700480 if (sSharedPrefsCache == null) {
481 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700482 }
483
484 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700485 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700486 if (packagePrefs == null) {
487 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700488 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700489 }
490
491 return packagePrefs;
492 }
493
Christopher Tatefe2368c2017-05-17 15:42:35 -0700494 @Override
495 public void reloadSharedPreferences() {
496 // Build the list of all per-context impls (i.e. caches) we know about
497 ArrayList<SharedPreferencesImpl> spImpls = new ArrayList<>();
498 synchronized (ContextImpl.class) {
499 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
500 for (int i = 0; i < cache.size(); i++) {
501 final SharedPreferencesImpl sp = cache.valueAt(i);
502 if (sp != null) {
503 spImpls.add(sp);
504 }
505 }
506 }
507
508 // Issue the reload outside the cache lock
509 for (int i = 0; i < spImpls.size(); i++) {
510 spImpls.get(i).startReloadIfChangedUnexpectedly();
511 }
512 }
513
Jeff Sharkey35871f22016-01-29 17:13:29 -0700514 /**
515 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700516 * requested prefix.
517 *
518 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700519 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600520 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700521 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
522 @Override
523 public boolean accept(File dir, String name) {
524 return name.startsWith(prefix);
525 }
526 });
527
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700528 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700529 for (File sourceFile : sourceFiles) {
530 final File targetFile = new File(targetDir, sourceFile.getName());
531 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
532 try {
533 FileUtils.copyFileOrThrow(sourceFile, targetFile);
534 FileUtils.copyPermissions(sourceFile, targetFile);
535 if (!sourceFile.delete()) {
536 throw new IOException("Failed to clean up " + sourceFile);
537 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700538 if (res != -1) {
539 res++;
540 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700541 } catch (IOException e) {
542 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700543 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700544 }
545 }
546 return res;
547 }
548
549 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600550 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700551 synchronized (ContextImpl.class) {
552 final File source = sourceContext.getSharedPreferencesPath(name);
553 final File target = getSharedPreferencesPath(name);
554
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600555 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700556 source.getName());
557 if (res > 0) {
558 // We moved at least one file, so evict any in-memory caches for
559 // either location
560 final ArrayMap<File, SharedPreferencesImpl> cache =
561 getSharedPreferencesCacheLocked();
562 cache.remove(source);
563 cache.remove(target);
564 }
565 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700566 }
567 }
568
569 @Override
570 public boolean deleteSharedPreferences(String name) {
571 synchronized (ContextImpl.class) {
572 final File prefs = getSharedPreferencesPath(name);
573 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
574
575 // Evict any in-memory caches
576 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
577 cache.remove(prefs);
578
579 prefs.delete();
580 prefsBackup.delete();
581
582 // We failed if files are still lingering
583 return !(prefs.exists() || prefsBackup.exists());
584 }
585 }
586
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100587 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 private File getPreferencesDir() {
589 synchronized (mSync) {
590 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700591 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700593 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595 }
596
597 @Override
598 public FileInputStream openFileInput(String name)
599 throws FileNotFoundException {
600 File f = makeFilename(getFilesDir(), name);
601 return new FileInputStream(f);
602 }
603
604 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700605 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
606 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 final boolean append = (mode&MODE_APPEND) != 0;
608 File f = makeFilename(getFilesDir(), name);
609 try {
610 FileOutputStream fos = new FileOutputStream(f, append);
611 setFilePermissionsFromMode(f.getPath(), mode, 0);
612 return fos;
613 } catch (FileNotFoundException e) {
614 }
615
616 File parent = f.getParentFile();
617 parent.mkdir();
618 FileUtils.setPermissions(
619 parent.getPath(),
620 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
621 -1, -1);
622 FileOutputStream fos = new FileOutputStream(f, append);
623 setFilePermissionsFromMode(f.getPath(), mode, 0);
624 return fos;
625 }
626
627 @Override
628 public boolean deleteFile(String name) {
629 File f = makeFilename(getFilesDir(), name);
630 return f.delete();
631 }
632
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700633 /**
634 * Common-path handling of app data dir creation
635 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700636 private static File ensurePrivateDirExists(File file) {
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600637 return ensurePrivateDirExists(file, 0771, -1, null);
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700638 }
639
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600640 private static File ensurePrivateCacheDirExists(File file, String xattr) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700641 final int gid = UserHandle.getCacheAppGid(Process.myUid());
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600642 return ensurePrivateDirExists(file, 02771, gid, xattr);
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700643 }
644
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600645 private static File ensurePrivateDirExists(File file, int mode, int gid, String xattr) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700646 if (!file.exists()) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700647 final String path = file.getAbsolutePath();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700648 try {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700649 Os.mkdir(path, mode);
650 Os.chmod(path, mode);
651 if (gid != -1) {
652 Os.chown(path, -1, gid);
653 }
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700654 } catch (ErrnoException e) {
655 if (e.errno == OsConstants.EEXIST) {
656 // We must have raced with someone; that's okay
657 } else {
658 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700659 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700660 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600661
662 if (xattr != null) {
663 try {
664 final StructStat stat = Os.stat(file.getAbsolutePath());
665 final byte[] value = new byte[8];
666 Memory.pokeLong(value, 0, stat.st_ino, ByteOrder.nativeOrder());
667 Os.setxattr(file.getParentFile().getAbsolutePath(), xattr, value, 0);
668 } catch (ErrnoException e) {
669 Log.w(TAG, "Failed to update " + xattr + ": " + e.getMessage());
670 }
671 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700672 }
673 return file;
674 }
675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 @Override
677 public File getFilesDir() {
678 synchronized (mSync) {
679 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700680 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700682 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700683 }
684 }
685
686 @Override
687 public File getNoBackupFilesDir() {
688 synchronized (mSync) {
689 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700690 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700692 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800697 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700698 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700699 final File[] dirs = getExternalFilesDirs(type);
700 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700701 }
702
703 @Override
704 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800705 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600706 File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700707 if (type != null) {
708 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800709 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700710 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800711 }
712 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200713
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800714 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800715 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700716 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700717 final File[] dirs = getObbDirs();
718 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700719 }
720
721 @Override
722 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800723 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600724 File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
725 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800726 }
727 }
728
729 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 public File getCacheDir() {
731 synchronized (mSync) {
732 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700733 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600735 return ensurePrivateCacheDirExists(mCacheDir, XATTR_INODE_CACHE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200738
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800739 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700740 public File getCodeCacheDir() {
741 synchronized (mSync) {
742 if (mCodeCacheDir == null) {
Alex Light2d0691c2019-10-03 14:36:57 -0700743 mCodeCacheDir = getCodeCacheDirBeforeBind(getDataDir());
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700744 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600745 return ensurePrivateCacheDirExists(mCodeCacheDir, XATTR_INODE_CODE_CACHE);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700746 }
747 }
748
Alex Light2d0691c2019-10-03 14:36:57 -0700749 /**
750 * Helper for getting code-cache dir potentially before application bind.
751 *
752 * @hide
753 */
754 static File getCodeCacheDirBeforeBind(File dataDir) {
755 return new File(dataDir, "code_cache");
756 }
757
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700758 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800759 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700760 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700761 final File[] dirs = getExternalCacheDirs();
762 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700763 }
764
765 @Override
766 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800767 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600768 File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
769 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800770 }
771 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700774 public File[] getExternalMediaDirs() {
775 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600776 File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
777 return ensureExternalDirsExistOrFilter(dirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700778 }
779 }
780
Fyodor Kupolov61221292016-09-02 15:21:03 -0700781 /**
782 * @hide
783 */
784 @Nullable
785 @Override
786 public File getPreloadsFileCache() {
787 return Environment.getDataPreloadsFileCacheDirectory(getPackageName());
788 }
789
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700790 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 public File getFileStreamPath(String name) {
792 return makeFilename(getFilesDir(), name);
793 }
794
795 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700796 public File getSharedPreferencesPath(String name) {
797 return makeFilename(getPreferencesDir(), name + ".xml");
798 }
799
800 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700802 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804
805 @Override
806 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700807 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
810 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700811 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
812 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700813 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700814 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700815 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
816 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
817 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
818 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800819 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
820 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
821 }
Jeff Brown47847f32012-03-22 19:13:11 -0700822 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700823 setFilePermissionsFromMode(f.getPath(), mode, 0);
824 return db;
825 }
826
827 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600828 public boolean moveDatabaseFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700829 synchronized (ContextImpl.class) {
830 final File source = sourceContext.getDatabasePath(name);
831 final File target = getDatabasePath(name);
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600832 return moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700833 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700834 }
835 }
836
837 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 public boolean deleteDatabase(String name) {
839 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700840 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800841 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 } catch (Exception e) {
843 }
844 return false;
845 }
846
847 @Override
848 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700849 File dir;
850 File f;
851
852 if (name.charAt(0) == File.separatorChar) {
853 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
854 dir = new File(dirPath);
855 name = name.substring(name.lastIndexOf(File.separatorChar));
856 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700857
858 if (!dir.isDirectory() && dir.mkdir()) {
859 FileUtils.setPermissions(dir.getPath(),
860 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
861 -1, -1);
862 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700863 } else {
864 dir = getDatabasesDir();
865 f = makeFilename(dir, name);
866 }
867
Jeff Sharkey35871f22016-01-29 17:13:29 -0700868 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870
871 @Override
872 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700873 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 }
875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 private File getDatabasesDir() {
877 synchronized (mSync) {
878 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700879 if ("android".equals(getPackageName())) {
880 mDatabasesDir = new File("/data/system");
881 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700882 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700885 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
887 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800890 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700892 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
895 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800896 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700897 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700898 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
900
901 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800902 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700904 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906
907 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800908 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700910 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
912
913 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800914 @Deprecated
915 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700916 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
918
919 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800920 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700922 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
924
925 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800926 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700928 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 }
930
Jeff Brown6e539312015-02-24 18:53:21 -0800931 private WallpaperManager getWallpaperManager() {
932 return getSystemService(WallpaperManager.class);
933 }
934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 @Override
936 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700937 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700938 startActivity(intent, null);
939 }
940
Amith Yamasani82644082012-08-03 13:09:11 -0700941 /** @hide */
942 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700943 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700944 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700945 }
946
Dianne Hackborna4972e92012-03-14 10:38:05 -0700947 @Override
948 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700949 warnIfCallingFromSystemProcess();
Jorim Jaggi2adba072016-03-03 13:43:39 +0100950
951 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
952 // generally not allowed, except if the caller specifies the task id the activity should
Bryce Leef465ea02017-05-02 16:58:22 -0700953 // be launched in. A bug was existed between N and O-MR1 which allowed this to work. We
954 // maintain this for backwards compatibility.
955 final int targetSdkVersion = getApplicationInfo().targetSdkVersion;
956
957 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
958 && (targetSdkVersion < Build.VERSION_CODES.N
959 || targetSdkVersion >= Build.VERSION_CODES.P)
960 && (options == null
961 || ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 throw new AndroidRuntimeException(
963 "Calling startActivity() from outside of an Activity "
Bryce Leef465ea02017-05-02 16:58:22 -0700964 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
965 + " Is this really what you want?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 }
967 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700968 getOuterContext(), mMainThread.getApplicationThread(), null,
969 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971
Amith Yamasani258848d2012-08-10 17:06:33 -0700972 /** @hide */
973 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700974 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700975 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700976 ActivityTaskManager.getService().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800977 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700978 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700979 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700980 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700981 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700982 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700983 }
984 }
985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800987 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700988 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700989 startActivities(intents, null);
990 }
991
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700992 /** @hide */
993 @Override
Selim Cinek7fa385a2018-01-24 08:35:28 -0800994 public int startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700995 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
996 throw new AndroidRuntimeException(
997 "Calling startActivities() from outside of an Activity "
998 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
999 + " Is this really what you want?");
1000 }
Selim Cinek7fa385a2018-01-24 08:35:28 -08001001 return mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -07001002 getOuterContext(), mMainThread.getApplicationThread(), null,
1003 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -07001004 }
1005
Dianne Hackborna4972e92012-03-14 10:38:05 -07001006 @Override
1007 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001008 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001009 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1010 throw new AndroidRuntimeException(
1011 "Calling startActivities() from outside of an Activity "
1012 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
1013 + " Is this really what you want?");
1014 }
1015 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -07001016 getOuterContext(), mMainThread.getApplicationThread(), null,
1017 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001018 }
1019
1020 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001021 public void startIntentSender(IntentSender intent,
1022 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
1023 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001024 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
1025 }
1026
1027 @Override
1028 public void startIntentSender(IntentSender intent, Intent fillInIntent,
1029 int flagsMask, int flagsValues, int extraFlags, Bundle options)
1030 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001031 try {
1032 String resolvedType = null;
1033 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001034 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07001035 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001036 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
1037 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001038 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07001039 .startActivityIntentSender(mMainThread.getApplicationThread(),
1040 intent != null ? intent.getTarget() : null,
1041 intent != null ? intent.getWhitelistToken() : null,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001042 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001043 0, flagsMask, flagsValues, options);
1044 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001045 throw new IntentSender.SendIntentException();
1046 }
1047 Instrumentation.checkStartActivityResult(result, null);
1048 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001049 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001050 }
1051 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001052
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001053 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001055 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1057 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001058 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001059 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001060 mMainThread.getApplicationThread(), intent, resolvedType, null,
1061 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
1062 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001064 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
1066 }
1067
Amith Yamasani67cf7d32012-02-16 14:31:23 -08001068 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001070 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001072 String[] receiverPermissions = receiverPermission == null ? null
1073 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001075 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001076 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001077 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001078 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1079 null, false, false, getUserId());
1080 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001081 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001082 }
1083 }
1084
1085 @Override
1086 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
1087 warnIfCallingFromSystemProcess();
1088 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1089 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001090 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001091 ActivityManager.getService().broadcastIntent(
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001092 mMainThread.getApplicationThread(), intent, resolvedType, null,
1093 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001094 null, false, false, getUserId());
1095 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001096 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -07001097 }
1098 }
1099
1100 @Override
Tyler Gunnf955e562018-04-26 14:43:31 -07001101 public void sendBroadcastAsUserMultiplePermissions(Intent intent, UserHandle user,
1102 String[] receiverPermissions) {
Tyler Gunnf955e562018-04-26 14:43:31 -07001103 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1104 try {
1105 intent.prepareToLeaveProcess(this);
1106 ActivityManager.getService().broadcastIntent(
1107 mMainThread.getApplicationThread(), intent, resolvedType, null,
1108 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1109 null, false, false, user.getIdentifier());
1110 } catch (RemoteException e) {
1111 throw e.rethrowFromSystemServer();
1112 }
1113 }
1114
1115 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -07001116 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
1117 warnIfCallingFromSystemProcess();
1118 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001119 String[] receiverPermissions = receiverPermission == null ? null
1120 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -07001121 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001122 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001123 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001124 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001125 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001126 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001127 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001128 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001129 }
1130 }
1131
1132 @Override
1133 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
1134 warnIfCallingFromSystemProcess();
1135 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001136 String[] receiverPermissions = receiverPermission == null ? null
1137 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001138 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001139 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001140 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001141 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001142 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -07001143 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001145 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 }
1148
1149 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -07001150 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001151 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001153 String[] receiverPermissions = receiverPermission == null ? null
1154 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001156 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001157 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001158 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001159 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001160 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001162 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164 }
1165
1166 @Override
1167 public void sendOrderedBroadcast(Intent intent,
1168 String receiverPermission, BroadcastReceiver resultReceiver,
1169 Handler scheduler, int initialCode, String initialData,
1170 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001171 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001172 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1173 }
1174
1175 @Override
1176 public void sendOrderedBroadcast(Intent intent,
1177 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
1178 Handler scheduler, int initialCode, String initialData,
1179 Bundle initialExtras) {
1180 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1181 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001182 }
1183
1184 @Override
1185 public void sendOrderedBroadcast(Intent intent,
1186 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1187 Handler scheduler, int initialCode, String initialData,
1188 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001189 sendOrderedBroadcast(intent, receiverPermission, appOp,
1190 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1191 }
1192
1193 void sendOrderedBroadcast(Intent intent,
1194 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1195 Handler scheduler, int initialCode, String initialData,
1196 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001197 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 IIntentReceiver rd = null;
1199 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001200 if (mPackageInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 if (scheduler == null) {
1202 scheduler = mMainThread.getHandler();
1203 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001204 rd = mPackageInfo.getReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 resultReceiver, getOuterContext(), scheduler,
1206 mMainThread.getInstrumentation(), false);
1207 } else {
1208 if (scheduler == null) {
1209 scheduler = mMainThread.getHandler();
1210 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001211 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1213 }
1214 }
1215 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001216 String[] receiverPermissions = receiverPermission == null ? null
1217 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001219 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001220 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001222 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001223 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001225 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 }
1228
1229 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001230 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001231 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1232 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001233 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001234 ActivityManager.getService().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001235 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001236 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001237 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001238 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001239 }
1240 }
1241
1242 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001243 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1244 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001245 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1246 }
1247
1248 @Override
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001249 public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission,
1250 Bundle options) {
1251 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1252 String[] receiverPermissions = receiverPermission == null ? null
1253 : new String[] {receiverPermission};
1254 try {
1255 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001256 ActivityManager.getService().broadcastIntent(
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001257 mMainThread.getApplicationThread(), intent, resolvedType, null,
1258 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1259 options, false, false, user.getIdentifier());
1260 } catch (RemoteException e) {
1261 throw e.rethrowFromSystemServer();
1262 }
1263 }
1264
1265 @Override
Svet Ganov16a16892015-04-16 10:32:04 -07001266 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1267 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001268 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001269 String[] receiverPermissions = receiverPermission == null ? null
1270 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001271 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001272 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001273 ActivityManager.getService().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001274 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001275 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001276 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001277 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001278 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001279 }
1280 }
1281
1282 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001283 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001284 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001285 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001286 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001287 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001288 }
1289
1290 @Override
1291 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1292 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001293 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1294 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1295 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1296 }
1297
1298 @Override
1299 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1300 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1301 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001302 IIntentReceiver rd = null;
1303 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001304 if (mPackageInfo != null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001305 if (scheduler == null) {
1306 scheduler = mMainThread.getHandler();
1307 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001308 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001309 resultReceiver, getOuterContext(), scheduler,
1310 mMainThread.getInstrumentation(), false);
1311 } else {
1312 if (scheduler == null) {
1313 scheduler = mMainThread.getHandler();
1314 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001315 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1316 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001317 }
1318 }
1319 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001320 String[] receiverPermissions = receiverPermission == null ? null
1321 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001322 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001323 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001324 ActivityManager.getService().broadcastIntent(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001325 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001326 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001327 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001328 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001329 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001330 }
1331 }
1332
1333 @Override
Jordan Liua32e6fc2019-10-29 10:33:55 -07001334 public void sendOrderedBroadcast(Intent intent, String receiverPermission,
1335 String receiverAppOp, BroadcastReceiver resultReceiver, Handler scheduler,
1336 int initialCode, String initialData, @Nullable Bundle initialExtras) {
1337 int intAppOp = AppOpsManager.OP_NONE;
1338 if (!TextUtils.isEmpty(receiverAppOp)) {
1339 intAppOp = AppOpsManager.strOpToOp(receiverAppOp);
1340 }
1341 sendOrderedBroadcastAsUser(intent, getUser(),
1342 receiverPermission, intAppOp, resultReceiver, scheduler, initialCode, initialData,
1343 initialExtras);
1344 }
1345
1346 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001347 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001349 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1351 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001352 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001353 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001355 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001356 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001358 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 }
1360 }
1361
1362 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001363 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001364 public void sendStickyOrderedBroadcast(Intent intent,
1365 BroadcastReceiver resultReceiver,
1366 Handler scheduler, int initialCode, String initialData,
1367 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001368 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001369 IIntentReceiver rd = null;
1370 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001371 if (mPackageInfo != null) {
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001372 if (scheduler == null) {
1373 scheduler = mMainThread.getHandler();
1374 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001375 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001376 resultReceiver, getOuterContext(), scheduler,
1377 mMainThread.getInstrumentation(), false);
1378 } else {
1379 if (scheduler == null) {
1380 scheduler = mMainThread.getHandler();
1381 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001382 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001383 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1384 }
1385 }
1386 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1387 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001388 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001389 ActivityManager.getService().broadcastIntent(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001390 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1391 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001392 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001393 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001394 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001395 }
1396 }
1397
1398 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001399 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 public void removeStickyBroadcast(Intent intent) {
1401 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1402 if (resolvedType != null) {
1403 intent = new Intent(intent);
1404 intent.setDataAndType(intent.getData(), resolvedType);
1405 }
1406 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001407 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001408 ActivityManager.getService().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001409 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001411 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
1413 }
1414
1415 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001416 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001417 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1418 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1419 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001420 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001421 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001422 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001423 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1424 user.getIdentifier());
1425 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001426 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001427 }
1428 }
1429
1430 @Override
1431 @Deprecated
1432 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1433 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1434 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001435 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001436 ActivityManager.getService().broadcastIntent(
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001437 mMainThread.getApplicationThread(), intent, resolvedType, null,
1438 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1439 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001440 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001441 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001442 }
1443 }
1444
1445 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001446 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001447 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1448 UserHandle user, BroadcastReceiver resultReceiver,
1449 Handler scheduler, int initialCode, String initialData,
1450 Bundle initialExtras) {
1451 IIntentReceiver rd = null;
1452 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001453 if (mPackageInfo != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001454 if (scheduler == null) {
1455 scheduler = mMainThread.getHandler();
1456 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001457 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001458 resultReceiver, getOuterContext(), scheduler,
1459 mMainThread.getInstrumentation(), false);
1460 } else {
1461 if (scheduler == null) {
1462 scheduler = mMainThread.getHandler();
1463 }
1464 rd = new LoadedApk.ReceiverDispatcher(
1465 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1466 }
1467 }
1468 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1469 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001470 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001471 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001472 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1473 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001474 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001475 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001476 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001477 }
1478 }
1479
1480 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001481 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001482 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1483 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1484 if (resolvedType != null) {
1485 intent = new Intent(intent);
1486 intent.setDataAndType(intent.getData(), resolvedType);
1487 }
1488 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001489 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001490 ActivityManager.getService().unbroadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001491 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1492 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001493 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001494 }
1495 }
1496
1497 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1499 return registerReceiver(receiver, filter, null, null);
1500 }
1501
1502 @Override
1503 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001504 int flags) {
1505 return registerReceiver(receiver, filter, null, null, flags);
Chad Brubaker816c83b2017-03-02 10:27:59 -08001506 }
1507
1508 @Override
1509 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001511 return registerReceiverInternal(receiver, getUserId(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001512 filter, broadcastPermission, scheduler, getOuterContext(), 0);
Chad Brubaker816c83b2017-03-02 10:27:59 -08001513 }
1514
1515 @Override
1516 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001517 String broadcastPermission, Handler scheduler, int flags) {
Chad Brubaker816c83b2017-03-02 10:27:59 -08001518 return registerReceiverInternal(receiver, getUserId(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001519 filter, broadcastPermission, scheduler, getOuterContext(), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
1521
Dianne Hackborn20e80982012-08-31 19:00:44 -07001522 @Override
1523 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1524 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1525 return registerReceiverInternal(receiver, user.getIdentifier(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001526 filter, broadcastPermission, scheduler, getOuterContext(), 0);
Dianne Hackborn20e80982012-08-31 19:00:44 -07001527 }
1528
1529 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 IntentFilter filter, String broadcastPermission,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001531 Handler scheduler, Context context, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 IIntentReceiver rd = null;
1533 if (receiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001534 if (mPackageInfo != null && context != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 if (scheduler == null) {
1536 scheduler = mMainThread.getHandler();
1537 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001538 rd = mPackageInfo.getReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 receiver, context, scheduler,
1540 mMainThread.getInstrumentation(), true);
1541 } else {
1542 if (scheduler == null) {
1543 scheduler = mMainThread.getHandler();
1544 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001545 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001546 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548 }
1549 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001550 final Intent intent = ActivityManager.getService().registerReceiver(
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001551 mMainThread.getApplicationThread(), mBasePackageName, rd, filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001552 broadcastPermission, userId, flags);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001553 if (intent != null) {
1554 intent.setExtrasClassLoader(getClassLoader());
1555 intent.prepareToEnterProcess();
1556 }
1557 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001559 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561 }
1562
1563 @Override
1564 public void unregisterReceiver(BroadcastReceiver receiver) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001565 if (mPackageInfo != null) {
1566 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 getOuterContext(), receiver);
1568 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001569 ActivityManager.getService().unregisterReceiver(rd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001571 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573 } else {
1574 throw new RuntimeException("Not supported in system context");
1575 }
1576 }
1577
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001578 private void validateServiceIntent(Intent service) {
1579 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001580 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001581 IllegalArgumentException ex = new IllegalArgumentException(
1582 "Service Intent must be explicit: " + service);
1583 throw ex;
1584 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001585 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1586 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001587 }
1588 }
1589 }
1590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 @Override
1592 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001593 warnIfCallingFromSystemProcess();
Christopher Tate242ba3e92017-04-14 15:07:06 -07001594 return startServiceCommon(service, false, mUser);
Christopher Tate42a386b2016-11-07 12:21:21 -08001595 }
1596
1597 @Override
Christopher Tate79047c62017-03-21 11:37:06 -07001598 public ComponentName startForegroundService(Intent service) {
1599 warnIfCallingFromSystemProcess();
Christopher Tate242ba3e92017-04-14 15:07:06 -07001600 return startServiceCommon(service, true, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001601 }
1602
1603 @Override
1604 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001605 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001606 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001607 }
1608
1609 @Override
1610 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Christopher Tate242ba3e92017-04-14 15:07:06 -07001611 return startServiceCommon(service, false, user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001612 }
1613
Christopher Tate42a386b2016-11-07 12:21:21 -08001614 @Override
Christopher Tate79047c62017-03-21 11:37:06 -07001615 public ComponentName startForegroundServiceAsUser(Intent service, UserHandle user) {
Christopher Tate242ba3e92017-04-14 15:07:06 -07001616 return startServiceCommon(service, true, user);
Christopher Tate79047c62017-03-21 11:37:06 -07001617 }
1618
Christopher Tate242ba3e92017-04-14 15:07:06 -07001619 private ComponentName startServiceCommon(Intent service, boolean requireForeground,
1620 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001622 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001623 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001624 ComponentName cn = ActivityManager.getService().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001625 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
Christopher Tate242ba3e92017-04-14 15:07:06 -07001626 getContentResolver()), requireForeground,
Christopher Tate79047c62017-03-21 11:37:06 -07001627 getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001628 if (cn != null) {
1629 if (cn.getPackageName().equals("!")) {
1630 throw new SecurityException(
1631 "Not allowed to start service " + service
1632 + " without permission " + cn.getClassName());
1633 } else if (cn.getPackageName().equals("!!")) {
1634 throw new SecurityException(
1635 "Unable to start service " + service
1636 + ": " + cn.getClassName());
Dianne Hackborn85e35642017-01-12 15:10:57 -08001637 } else if (cn.getPackageName().equals("?")) {
1638 throw new IllegalStateException(
1639 "Not allowed to start service " + service + ": " + cn.getClassName());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
1642 return cn;
1643 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001644 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 }
1646 }
1647
1648 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001649 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001650 return stopServiceCommon(service, user);
1651 }
1652
1653 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001655 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001656 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001657 int res = ActivityManager.getService().stopService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001659 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 if (res < 0) {
1661 throw new SecurityException(
1662 "Not allowed to stop service " + service);
1663 }
1664 return res != 0;
1665 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001666 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 }
1668 }
1669
1670 @Override
Bo Liu58a57662019-03-06 20:21:45 +00001671 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001672 warnIfCallingFromSystemProcess();
Bo Liu58a57662019-03-06 20:21:45 +00001673 return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null,
1674 getUser());
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001675 }
1676
1677 @Override
Bo Liu58a57662019-03-06 20:21:45 +00001678 public boolean bindService(
1679 Intent service, int flags, Executor executor, ServiceConnection conn) {
1680 warnIfCallingFromSystemProcess();
1681 return bindServiceCommon(service, conn, flags, null, null, executor, getUser());
1682 }
1683
1684 @Override
1685 public boolean bindIsolatedService(Intent service, int flags, String instanceName,
1686 Executor executor, ServiceConnection conn) {
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001687 warnIfCallingFromSystemProcess();
1688 if (instanceName == null) {
1689 throw new NullPointerException("null instanceName");
1690 }
Bo Liu58a57662019-03-06 20:21:45 +00001691 return bindServiceCommon(service, conn, flags, instanceName, null, executor, getUser());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001692 }
1693
1694 /** @hide */
1695 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001696 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1697 UserHandle user) {
Bo Liu58a57662019-03-06 20:21:45 +00001698 return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null, user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001699 }
1700
Adrian Roos691546e2016-02-09 10:13:41 -08001701 /** @hide */
1702 @Override
1703 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1704 Handler handler, UserHandle user) {
1705 if (handler == null) {
1706 throw new IllegalArgumentException("handler must not be null.");
1707 }
Bo Liu58a57662019-03-06 20:21:45 +00001708 return bindServiceCommon(service, conn, flags, null, handler, null, user);
Adrian Roos691546e2016-02-09 10:13:41 -08001709 }
1710
Tony Mak46aabe52016-11-14 12:53:06 +00001711 /** @hide */
1712 @Override
1713 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler,
1714 int flags) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001715 return mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
Tony Mak46aabe52016-11-14 12:53:06 +00001716 }
1717
1718 /** @hide */
1719 @Override
1720 public IApplicationThread getIApplicationThread() {
1721 return mMainThread.getApplicationThread();
1722 }
1723
Tony Makbf9928d2016-12-22 11:02:45 +00001724 /** @hide */
1725 @Override
1726 public Handler getMainThreadHandler() {
1727 return mMainThread.getHandler();
1728 }
1729
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001730 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,
Bo Liu58a57662019-03-06 20:21:45 +00001731 String instanceName, Handler handler, Executor executor, UserHandle user) {
Tony Mak46aabe52016-11-14 12:53:06 +00001732 // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001734 if (conn == null) {
1735 throw new IllegalArgumentException("connection is null");
1736 }
Bo Liu58a57662019-03-06 20:21:45 +00001737 if (handler != null && executor != null) {
1738 throw new IllegalArgumentException("Handler and Executor both supplied");
1739 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001740 if (mPackageInfo != null) {
Bo Liu58a57662019-03-06 20:21:45 +00001741 if (executor != null) {
1742 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), executor, flags);
1743 } else {
1744 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
1745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 } else {
1747 throw new RuntimeException("Not supported in system context");
1748 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001749 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001751 IBinder token = getActivityToken();
Todd Kennedy233a0b12018-01-29 20:30:24 +00001752 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1753 && mPackageInfo.getApplicationInfo().targetSdkVersion
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001754 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1755 flags |= BIND_WAIVE_PRIORITY;
1756 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001757 service.prepareToLeaveProcess(this);
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001758 int res = ActivityManager.getService().bindIsolatedService(
Svet Ganov99b60432015-06-27 13:15:22 -07001759 mMainThread.getApplicationThread(), getActivityToken(), service,
1760 service.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001761 sd, flags, instanceName, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 if (res < 0) {
1763 throw new SecurityException(
1764 "Not allowed to bind to service " + service);
1765 }
1766 return res != 0;
1767 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001768 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770 }
1771
1772 @Override
Dianne Hackborna631d562018-11-20 15:58:15 -08001773 public void updateServiceGroup(@NonNull ServiceConnection conn, int group, int importance) {
1774 if (conn == null) {
1775 throw new IllegalArgumentException("connection is null");
1776 }
1777 if (mPackageInfo != null) {
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -08001778 IServiceConnection sd = mPackageInfo.lookupServiceDispatcher(conn, getOuterContext());
1779 if (sd == null) {
1780 throw new IllegalArgumentException("ServiceConnection not currently bound: "
1781 + conn);
1782 }
Dianne Hackborna631d562018-11-20 15:58:15 -08001783 try {
1784 ActivityManager.getService().updateServiceGroup(sd, group, importance);
1785 } catch (RemoteException e) {
1786 throw e.rethrowFromSystemServer();
1787 }
1788 } else {
1789 throw new RuntimeException("Not supported in system context");
1790 }
1791 }
1792
1793 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001795 if (conn == null) {
1796 throw new IllegalArgumentException("connection is null");
1797 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001798 if (mPackageInfo != null) {
1799 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 getOuterContext(), conn);
1801 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001802 ActivityManager.getService().unbindService(sd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001804 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 }
1806 } else {
1807 throw new RuntimeException("Not supported in system context");
1808 }
1809 }
1810
1811 @Override
1812 public boolean startInstrumentation(ComponentName className,
1813 String profileFile, Bundle arguments) {
1814 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001815 if (arguments != null) {
1816 arguments.setAllowFds(false);
1817 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001818 return ActivityManager.getService().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001819 className, profileFile, 0, arguments, null, null, getUserId(),
1820 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001822 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
1825
1826 @Override
1827 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001828 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 }
1830
Jeff Brown6e539312015-02-24 18:53:21 -08001831 @Override
1832 public String getSystemServiceName(Class<?> serviceClass) {
1833 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001834 }
1835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 @Override
1837 public int checkPermission(String permission, int pid, int uid) {
1838 if (permission == null) {
1839 throw new IllegalArgumentException("permission is null");
1840 }
1841
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001842 final IActivityManager am = ActivityManager.getService();
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001843 if (am == null) {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001844 // Well this is super awkward; we somehow don't have an active
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001845 // ActivityManager instance. If we're testing a root or system
1846 // UID, then they totally have whatever permission this is.
1847 final int appId = UserHandle.getAppId(uid);
1848 if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) {
1849 Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " holds " + permission);
1850 return PackageManager.PERMISSION_GRANTED;
1851 }
Erik Wolsheimere21576f2018-03-29 16:21:41 -07001852 Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " does not hold "
1853 + permission);
1854 return PackageManager.PERMISSION_DENIED;
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001855 }
1856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 try {
Tim Murrayd06c6d292019-05-07 06:53:55 +00001858 return am.checkPermission(permission, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001860 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
1862 }
1863
Dianne Hackbornff170242014-11-19 10:59:01 -08001864 /** @hide */
1865 @Override
1866 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1867 if (permission == null) {
1868 throw new IllegalArgumentException("permission is null");
1869 }
1870
1871 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001872 return ActivityManager.getService().checkPermissionWithToken(
Dianne Hackbornff170242014-11-19 10:59:01 -08001873 permission, pid, uid, callerToken);
1874 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001875 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001876 }
1877 }
1878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 @Override
1880 public int checkCallingPermission(String permission) {
1881 if (permission == null) {
1882 throw new IllegalArgumentException("permission is null");
1883 }
1884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 int pid = Binder.getCallingPid();
1886 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001887 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
1889 return PackageManager.PERMISSION_DENIED;
1890 }
1891
1892 @Override
1893 public int checkCallingOrSelfPermission(String permission) {
1894 if (permission == null) {
1895 throw new IllegalArgumentException("permission is null");
1896 }
1897
1898 return checkPermission(permission, Binder.getCallingPid(),
1899 Binder.getCallingUid());
1900 }
1901
Svetoslavc6d1c342015-02-26 14:44:43 -08001902 @Override
1903 public int checkSelfPermission(String permission) {
1904 if (permission == null) {
1905 throw new IllegalArgumentException("permission is null");
1906 }
1907
1908 return checkPermission(permission, Process.myPid(), Process.myUid());
1909 }
1910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 private void enforce(
1912 String permission, int resultOfCheck,
1913 boolean selfToo, int uid, String message) {
1914 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1915 throw new SecurityException(
1916 (message != null ? (message + ": ") : "") +
1917 (selfToo
1918 ? "Neither user " + uid + " nor current process has "
Tim Murrayd06c6d292019-05-07 06:53:55 +00001919 : "uid " + uid + " does not have ") +
1920 permission +
1921 ".");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923 }
1924
Jeff Brown6e539312015-02-24 18:53:21 -08001925 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 public void enforcePermission(
1927 String permission, int pid, int uid, String message) {
Tim Murrayd06c6d292019-05-07 06:53:55 +00001928 enforce(permission,
1929 checkPermission(permission, pid, uid),
1930 false,
1931 uid,
1932 message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934
Jeff Brown6e539312015-02-24 18:53:21 -08001935 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 public void enforceCallingPermission(String permission, String message) {
Tim Murrayd06c6d292019-05-07 06:53:55 +00001937 enforce(permission,
1938 checkCallingPermission(permission),
1939 false,
1940 Binder.getCallingUid(),
1941 message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
1943
Jeff Brown6e539312015-02-24 18:53:21 -08001944 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 public void enforceCallingOrSelfPermission(
1946 String permission, String message) {
Tim Murrayd06c6d292019-05-07 06:53:55 +00001947 enforce(permission,
1948 checkCallingOrSelfPermission(permission),
1949 true,
1950 Binder.getCallingUid(),
1951 message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
1953
1954 @Override
1955 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1956 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001957 ActivityManager.getService().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001958 mMainThread.getApplicationThread(), toPackage,
1959 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001961 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
1963 }
1964
1965 @Override
1966 public void revokeUriPermission(Uri uri, int modeFlags) {
1967 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001968 ActivityManager.getService().revokeUriPermission(
Dianne Hackborna47223f2017-03-30 13:49:13 -07001969 mMainThread.getApplicationThread(), null,
1970 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
1971 } catch (RemoteException e) {
1972 throw e.rethrowFromSystemServer();
1973 }
1974 }
1975
1976 @Override
1977 public void revokeUriPermission(String targetPackage, Uri uri, int modeFlags) {
1978 try {
1979 ActivityManager.getService().revokeUriPermission(
1980 mMainThread.getApplicationThread(), targetPackage,
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001981 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001983 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985 }
1986
1987 @Override
1988 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001990 return ActivityManager.getService().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001991 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001992 resolveUserId(uri), null);
1993 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001994 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001995 }
1996 }
1997
1998 /** @hide */
1999 @Override
2000 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
2001 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002002 return ActivityManager.getService().checkUriPermission(
Dianne Hackbornff170242014-11-19 10:59:01 -08002003 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
2004 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002006 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008 }
2009
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002010 private int resolveUserId(Uri uri) {
2011 return ContentProvider.getUserIdFromUri(uri, getUserId());
2012 }
2013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 @Override
2015 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 int pid = Binder.getCallingPid();
2017 if (pid != Process.myPid()) {
2018 return checkUriPermission(uri, pid,
2019 Binder.getCallingUid(), modeFlags);
2020 }
2021 return PackageManager.PERMISSION_DENIED;
2022 }
2023
2024 @Override
2025 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
2026 return checkUriPermission(uri, Binder.getCallingPid(),
2027 Binder.getCallingUid(), modeFlags);
2028 }
2029
2030 @Override
2031 public int checkUriPermission(Uri uri, String readPermission,
2032 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002033 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
2035 + readPermission + " writePermission=" + writePermission
2036 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
2037 }
2038 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
2039 if (readPermission == null
2040 || checkPermission(readPermission, pid, uid)
2041 == PackageManager.PERMISSION_GRANTED) {
2042 return PackageManager.PERMISSION_GRANTED;
2043 }
2044 }
2045 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
2046 if (writePermission == null
2047 || checkPermission(writePermission, pid, uid)
2048 == PackageManager.PERMISSION_GRANTED) {
2049 return PackageManager.PERMISSION_GRANTED;
2050 }
2051 }
2052 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
2053 : PackageManager.PERMISSION_DENIED;
2054 }
2055
2056 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07002057 StringBuilder builder = new StringBuilder();
2058 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
2059 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07002061 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
2062 builder.append("write and ");
2063 }
2064 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
2065 builder.append("persistable and ");
2066 }
2067 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
2068 builder.append("prefix and ");
2069 }
2070
2071 if (builder.length() > 5) {
2072 builder.setLength(builder.length() - 5);
2073 return builder.toString();
2074 } else {
2075 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
2076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
2078
2079 private void enforceForUri(
2080 int modeFlags, int resultOfCheck, boolean selfToo,
2081 int uid, Uri uri, String message) {
2082 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
2083 throw new SecurityException(
2084 (message != null ? (message + ": ") : "") +
2085 (selfToo
2086 ? "Neither user " + uid + " nor current process has "
2087 : "User " + uid + " does not have ") +
2088 uriModeFlagToString(modeFlags) +
2089 " permission on " +
2090 uri +
2091 ".");
2092 }
2093 }
2094
Jeff Brown6e539312015-02-24 18:53:21 -08002095 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 public void enforceUriPermission(
2097 Uri uri, int pid, int uid, int modeFlags, String message) {
2098 enforceForUri(
2099 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
2100 false, uid, uri, message);
2101 }
2102
Jeff Brown6e539312015-02-24 18:53:21 -08002103 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 public void enforceCallingUriPermission(
2105 Uri uri, int modeFlags, String message) {
2106 enforceForUri(
2107 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07002108 false,
2109 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
2111
Jeff Brown6e539312015-02-24 18:53:21 -08002112 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 public void enforceCallingOrSelfUriPermission(
2114 Uri uri, int modeFlags, String message) {
2115 enforceForUri(
2116 modeFlags,
2117 checkCallingOrSelfUriPermission(uri, modeFlags), true,
2118 Binder.getCallingUid(), uri, message);
2119 }
2120
Jeff Brown6e539312015-02-24 18:53:21 -08002121 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 public void enforceUriPermission(
2123 Uri uri, String readPermission, String writePermission,
2124 int pid, int uid, int modeFlags, String message) {
2125 enforceForUri(modeFlags,
2126 checkUriPermission(
2127 uri, readPermission, writePermission, pid, uid,
2128 modeFlags),
2129 false,
2130 uid,
2131 uri,
2132 message);
2133 }
2134
Tom O'Neill365632e2013-09-09 09:34:58 -07002135 /**
2136 * Logs a warning if the system process directly called a method such as
2137 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
2138 * The "AsUser" variants allow us to properly enforce the user's restrictions.
2139 */
Amith Yamasanicd757062012-10-19 18:23:52 -07002140 private void warnIfCallingFromSystemProcess() {
2141 if (Process.myUid() == Process.SYSTEM_UID) {
2142 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08002143 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07002144 }
2145 }
2146
Todd Kennedy233a0b12018-01-29 20:30:24 +00002147 private static Resources createResources(IBinder activityToken, LoadedApk pi, String splitName,
Winson710e3ec2019-02-25 13:38:20 -08002148 int displayId, Configuration overrideConfig, CompatibilityInfo compatInfo) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002149 final String[] splitResDirs;
2150 final ClassLoader classLoader;
2151 try {
2152 splitResDirs = pi.getSplitPaths(splitName);
2153 classLoader = pi.getSplitClassLoader(splitName);
2154 } catch (NameNotFoundException e) {
2155 throw new RuntimeException(e);
2156 }
2157 return ResourcesManager.getInstance().getResources(activityToken,
2158 pi.getResDir(),
2159 splitResDirs,
Winson710e3ec2019-02-25 13:38:20 -08002160 pi.getOverlayDirs(),
Todd Kennedy233a0b12018-01-29 20:30:24 +00002161 pi.getApplicationInfo().sharedLibraryFiles,
2162 displayId,
2163 overrideConfig,
2164 compatInfo,
2165 classLoader);
2166 }
2167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07002169 public Context createApplicationContext(ApplicationInfo application, int flags)
2170 throws NameNotFoundException {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002171 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
Svetoslav976e8bd2014-07-16 15:12:03 -07002172 flags | CONTEXT_REGISTER_PACKAGE);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002173 if (pi != null) {
2174 ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken,
Makoto Onuki4becc352019-01-30 13:30:41 -08002175 new UserHandle(UserHandle.getUserId(application.uid)), flags, null, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002176
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002177 final int displayId = getDisplayId();
Winson710e3ec2019-02-25 13:38:20 -08002178
Todd Kennedy233a0b12018-01-29 20:30:24 +00002179 c.setResources(createResources(mActivityToken, pi, null, displayId, null,
Winson710e3ec2019-02-25 13:38:20 -08002180 getDisplayAdjustments(displayId).getCompatibilityInfo()));
Svetoslav976e8bd2014-07-16 15:12:03 -07002181 if (c.mResources != null) {
2182 return c;
2183 }
2184 }
2185
2186 throw new PackageManager.NameNotFoundException(
2187 "Application package " + application.packageName + " not found");
2188 }
2189
2190 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07002192 throws NameNotFoundException {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002193 return createPackageContextAsUser(packageName, flags, mUser);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002194 }
2195
2196 @Override
2197 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
2198 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00002199 if (packageName.equals("system") || packageName.equals("android")) {
Adam Lesinski8fa71072016-11-18 18:13:55 -08002200 // The system resources are loaded in every application, so we can safely copy
2201 // the context without reloading Resources.
Todd Kennedy233a0b12018-01-29 20:30:24 +00002202 return new ContextImpl(this, mMainThread, mPackageInfo, null, mActivityToken, user,
Makoto Onuki4becc352019-01-30 13:30:41 -08002203 flags, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
2205
Todd Kennedy233a0b12018-01-29 20:30:24 +00002206 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002207 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
Todd Kennedy233a0b12018-01-29 20:30:24 +00002208 if (pi != null) {
2209 ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken, user,
Makoto Onuki4becc352019-01-30 13:30:41 -08002210 flags, null, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002211
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002212 final int displayId = getDisplayId();
Adam Lesinski8fa71072016-11-18 18:13:55 -08002213
Todd Kennedy233a0b12018-01-29 20:30:24 +00002214 c.setResources(createResources(mActivityToken, pi, null, displayId, null,
Winson710e3ec2019-02-25 13:38:20 -08002215 getDisplayAdjustments(displayId).getCompatibilityInfo()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 if (c.mResources != null) {
2217 return c;
2218 }
2219 }
2220
2221 // Should be a better exception.
2222 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07002223 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225
Romain Guy870e09f2009-07-06 16:35:25 -07002226 @Override
Adam Lesinski4e862812016-11-21 16:02:24 -08002227 public Context createContextForSplit(String splitName) throws NameNotFoundException {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002228 if (!mPackageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
Adam Lesinski4e862812016-11-21 16:02:24 -08002229 // All Splits are always loaded.
2230 return this;
2231 }
2232
Todd Kennedy233a0b12018-01-29 20:30:24 +00002233 final ClassLoader classLoader = mPackageInfo.getSplitClassLoader(splitName);
2234 final String[] paths = mPackageInfo.getSplitPaths(splitName);
Adam Lesinski4e862812016-11-21 16:02:24 -08002235
Todd Kennedy233a0b12018-01-29 20:30:24 +00002236 final ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, splitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002237 mActivityToken, mUser, mFlags, classLoader, null);
Adam Lesinski4e862812016-11-21 16:02:24 -08002238
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002239 final int displayId = getDisplayId();
Adam Lesinski4e862812016-11-21 16:02:24 -08002240
Todd Kennedy233a0b12018-01-29 20:30:24 +00002241 context.setResources(ResourcesManager.getInstance().getResources(
2242 mActivityToken,
2243 mPackageInfo.getResDir(),
2244 paths,
2245 mPackageInfo.getOverlayDirs(),
2246 mPackageInfo.getApplicationInfo().sharedLibraryFiles,
2247 displayId,
2248 null,
2249 mPackageInfo.getCompatibilityInfo(),
2250 classLoader));
Adam Lesinski4e862812016-11-21 16:02:24 -08002251 return context;
2252 }
2253
2254 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07002255 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07002256 if (overrideConfiguration == null) {
2257 throw new IllegalArgumentException("overrideConfiguration must not be null");
2258 }
2259
Todd Kennedy233a0b12018-01-29 20:30:24 +00002260 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002261 mActivityToken, mUser, mFlags, mClassLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002262
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002263 final int displayId = getDisplayId();
Todd Kennedy233a0b12018-01-29 20:30:24 +00002264 context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
Winson710e3ec2019-02-25 13:38:20 -08002265 overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo()));
Adam Lesinski8fa71072016-11-18 18:13:55 -08002266 return context;
Dianne Hackborn756220b2012-08-14 16:45:30 -07002267 }
2268
2269 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07002270 public Context createDisplayContext(Display display) {
2271 if (display == null) {
2272 throw new IllegalArgumentException("display must not be null");
2273 }
2274
Todd Kennedy233a0b12018-01-29 20:30:24 +00002275 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002276 mActivityToken, mUser, mFlags, mClassLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002277
2278 final int displayId = display.getDisplayId();
Todd Kennedy233a0b12018-01-29 20:30:24 +00002279 context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
Winson710e3ec2019-02-25 13:38:20 -08002280 null, getDisplayAdjustments(displayId).getCompatibilityInfo()));
Adam Lesinski8fa71072016-11-18 18:13:55 -08002281 context.mDisplay = display;
2282 return context;
Jeff Browna492c3a2012-08-23 19:48:44 -07002283 }
2284
Jeff Browna492c3a2012-08-23 19:48:44 -07002285 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002286 public Context createDeviceProtectedStorageContext() {
2287 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
2288 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002289 return new ContextImpl(this, mMainThread, mPackageInfo, mSplitName, mActivityToken, mUser,
Makoto Onuki4becc352019-01-30 13:30:41 -08002290 flags, mClassLoader, null);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002291 }
2292
2293 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002294 public Context createCredentialProtectedStorageContext() {
2295 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
2296 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002297 return new ContextImpl(this, mMainThread, mPackageInfo, mSplitName, mActivityToken, mUser,
Makoto Onuki4becc352019-01-30 13:30:41 -08002298 flags, mClassLoader, null);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002299 }
2300
2301 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07002302 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002303 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
2304 }
2305
2306 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002307 public boolean isDeviceProtectedStorage() {
2308 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002309 }
2310
2311 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002312 public boolean isCredentialProtectedStorage() {
2313 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07002314 }
2315
Jeff Brown98365d72012-08-19 20:30:52 -07002316 @Override
Seigo Nonaka6d6cd682017-06-22 08:22:18 -07002317 public boolean canLoadUnsafeResources() {
2318 if (getPackageName().equals(getOpPackageName())) {
2319 return true;
2320 }
2321 return (mFlags & Context.CONTEXT_IGNORE_SECURITY) != 0;
2322 }
2323
Wale Ogunwale691af682019-02-11 03:09:10 -08002324 @TestApi
Seigo Nonaka6d6cd682017-06-22 08:22:18 -07002325 @Override
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002326 public Display getDisplay() {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002327 if (mDisplay == null) {
2328 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
Bryce Lee609bf652017-02-09 16:50:13 -08002329 mResources);
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002330 }
2331
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002332 return mDisplay;
2333 }
2334
2335 @Override
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002336 public int getDisplayId() {
2337 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
2338 }
2339
2340 @Override
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002341 public void updateDisplay(int displayId) {
Bryce Lee609bf652017-02-09 16:50:13 -08002342 mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002343 }
2344
2345 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07002346 public DisplayAdjustments getDisplayAdjustments(int displayId) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002347 return mResources.getDisplayAdjustments();
Jeff Brown98365d72012-08-19 20:30:52 -07002348 }
2349
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002350 @Override
2351 public File getDataDir() {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002352 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07002353 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002354 if (isCredentialProtectedStorage()) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002355 res = mPackageInfo.getCredentialProtectedDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002356 } else if (isDeviceProtectedStorage()) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002357 res = mPackageInfo.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002358 } else {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002359 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002360 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002361
2362 if (res != null) {
Jeff Sharkey21f50722016-04-27 12:38:02 -06002363 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Jeff Sharkey24492ae2016-04-25 13:20:25 -06002364 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
2365 new Throwable());
2366 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002367 return res;
2368 } else {
2369 throw new RuntimeException(
2370 "No data directory found for package " + getPackageName());
2371 }
2372 } else {
2373 throw new RuntimeException(
2374 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 }
2377
2378 @Override
2379 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07002380 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002382 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 if (!file.exists()) {
2384 file.mkdir();
2385 setFilePermissionsFromMode(file.getPath(), mode,
2386 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
2387 }
2388 return file;
2389 }
2390
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002391 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08002392 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002393 public UserHandle getUser() {
2394 return mUser;
2395 }
2396
2397 /** {@hide} */
2398 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002399 public int getUserId() {
2400 return mUser.getIdentifier();
2401 }
2402
Felipe Lemebb567ae2017-10-04 09:56:21 -07002403 /** @hide */
2404 @Override
2405 public AutofillClient getAutofillClient() {
2406 return mAutofillClient;
2407 }
2408
2409 /** @hide */
2410 @Override
2411 public void setAutofillClient(AutofillClient client) {
2412 mAutofillClient = client;
2413 }
2414
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002415 /** @hide */
2416 @Override
Felipe Lemea4f39cd2019-02-19 15:08:59 -08002417 public AutofillOptions getAutofillOptions() {
2418 return mAutofillOptions;
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002419 }
2420
2421 /** @hide */
2422 @Override
Felipe Lemea4f39cd2019-02-19 15:08:59 -08002423 public void setAutofillOptions(AutofillOptions options) {
2424 mAutofillOptions = options;
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002425 }
2426
Felipe Lemeecb08be2018-11-27 15:48:47 -08002427 /** @hide */
2428 @Override
Felipe Leme326f15a2019-02-19 09:42:24 -08002429 public ContentCaptureOptions getContentCaptureOptions() {
2430 return mContentCaptureOptions;
Felipe Lemeecb08be2018-11-27 15:48:47 -08002431 }
2432
2433 /** @hide */
2434 @Override
Felipe Leme326f15a2019-02-19 09:42:24 -08002435 public void setContentCaptureOptions(ContentCaptureOptions options) {
2436 mContentCaptureOptions = options;
Felipe Lemeecb08be2018-11-27 15:48:47 -08002437 }
2438
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002439 @UnsupportedAppUsage
Dianne Hackborn21556372010-02-04 16:34:40 -08002440 static ContextImpl createSystemContext(ActivityThread mainThread) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002441 LoadedApk packageInfo = new LoadedApk(mainThread);
2442 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
Makoto Onuki4becc352019-01-30 13:30:41 -08002443 null, null);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002444 context.setResources(packageInfo.getResources());
Jeff Browndefd4a62014-03-10 21:24:37 -07002445 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002446 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 return context;
2448 }
2449
Adam Lesinskia82b6262017-03-21 16:56:17 -07002450 /**
2451 * System Context to be used for UI. This Context has resources that can be themed.
Adam Lesinski6f1a9172017-04-10 16:35:19 -07002452 * Make sure that the created system UI context shares the same LoadedApk as the system context.
lumark0b05f9e2018-11-26 15:09:06 +08002453 * @param systemContext The system context which created by
2454 * {@link #createSystemContext(ActivityThread)}.
2455 * @param displayId The ID of the display where the UI is shown.
Adam Lesinskia82b6262017-03-21 16:56:17 -07002456 */
lumark0b05f9e2018-11-26 15:09:06 +08002457 static ContextImpl createSystemUiContext(ContextImpl systemContext, int displayId) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002458 final LoadedApk packageInfo = systemContext.mPackageInfo;
2459 ContextImpl context = new ContextImpl(null, systemContext.mMainThread, packageInfo, null,
Makoto Onuki4becc352019-01-30 13:30:41 -08002460 null, null, 0, null, null);
lumark0b05f9e2018-11-26 15:09:06 +08002461 context.setResources(createResources(null, packageInfo, null, displayId, null,
Winson710e3ec2019-02-25 13:38:20 -08002462 packageInfo.getCompatibilityInfo()));
lumark0b05f9e2018-11-26 15:09:06 +08002463 context.updateDisplay(displayId);
Adam Lesinskia82b6262017-03-21 16:56:17 -07002464 return context;
2465 }
2466
lumark0b05f9e2018-11-26 15:09:06 +08002467 /**
2468 * The overloaded method of {@link #createSystemUiContext(ContextImpl, int)}.
2469 * Uses {@Code Display.DEFAULT_DISPLAY} as the target display.
2470 */
2471 static ContextImpl createSystemUiContext(ContextImpl systemContext) {
2472 return createSystemUiContext(systemContext, Display.DEFAULT_DISPLAY);
2473 }
2474
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002475 @UnsupportedAppUsage
Todd Kennedy233a0b12018-01-29 20:30:24 +00002476 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
Makoto Onuki4becc352019-01-30 13:30:41 -08002477 return createAppContext(mainThread, packageInfo, null);
2478 }
2479
2480 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo,
2481 String opPackageName) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002482 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
2483 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
Makoto Onuki4becc352019-01-30 13:30:41 -08002484 null, opPackageName);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002485 context.setResources(packageInfo.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002486 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002487 }
2488
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002489 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -07002490 static ContextImpl createActivityContext(ActivityThread mainThread,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002491 LoadedApk packageInfo, ActivityInfo activityInfo, IBinder activityToken, int displayId,
Adam Lesinski082614c2016-03-04 14:33:47 -08002492 Configuration overrideConfiguration) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002493 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski8fa71072016-11-18 18:13:55 -08002494
Todd Kennedy233a0b12018-01-29 20:30:24 +00002495 String[] splitDirs = packageInfo.getSplitResDirs();
2496 ClassLoader classLoader = packageInfo.getClassLoader();
Adam Lesinski4e862812016-11-21 16:02:24 -08002497
Todd Kennedy233a0b12018-01-29 20:30:24 +00002498 if (packageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
Adam Lesinski4e862812016-11-21 16:02:24 -08002499 Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "SplitDependencies");
2500 try {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002501 classLoader = packageInfo.getSplitClassLoader(activityInfo.splitName);
2502 splitDirs = packageInfo.getSplitPaths(activityInfo.splitName);
Adam Lesinski4e862812016-11-21 16:02:24 -08002503 } catch (NameNotFoundException e) {
2504 // Nothing above us can handle a NameNotFoundException, better crash.
2505 throw new RuntimeException(e);
2506 } finally {
2507 Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2508 }
2509 }
2510
Todd Kennedy233a0b12018-01-29 20:30:24 +00002511 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, activityInfo.splitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002512 activityToken, null, 0, classLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002513
2514 // Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
2515 displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
2516
2517 final CompatibilityInfo compatInfo = (displayId == Display.DEFAULT_DISPLAY)
Todd Kennedy233a0b12018-01-29 20:30:24 +00002518 ? packageInfo.getCompatibilityInfo()
Adam Lesinski8fa71072016-11-18 18:13:55 -08002519 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
2520
Adam Lesinskic9701942016-11-28 19:44:19 -08002521 final ResourcesManager resourcesManager = ResourcesManager.getInstance();
2522
2523 // Create the base resources for which all configuration contexts for this Activity
2524 // will be rebased upon.
Jason Monkbd983642017-02-24 16:57:40 -05002525 context.setResources(resourcesManager.createBaseActivityResources(activityToken,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002526 packageInfo.getResDir(),
Adam Lesinski4e862812016-11-21 16:02:24 -08002527 splitDirs,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002528 packageInfo.getOverlayDirs(),
2529 packageInfo.getApplicationInfo().sharedLibraryFiles,
Adam Lesinskic9701942016-11-28 19:44:19 -08002530 displayId,
2531 overrideConfiguration,
2532 compatInfo,
Jason Monkbd983642017-02-24 16:57:40 -05002533 classLoader));
Adam Lesinskic9701942016-11-28 19:44:19 -08002534 context.mDisplay = resourcesManager.getAdjustedDisplay(displayId,
Bryce Lee609bf652017-02-09 16:50:13 -08002535 context.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002536 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002537 }
2538
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002539 private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002540 @NonNull LoadedApk packageInfo, @Nullable String splitName,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002541 @Nullable IBinder activityToken, @Nullable UserHandle user, int flags,
Makoto Onuki4becc352019-01-30 13:30:41 -08002542 @Nullable ClassLoader classLoader, @Nullable String overrideOpPackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002545 // If creator didn't specify which storage to use, use the default
2546 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002547 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2548 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002549 final File dataDir = packageInfo.getDataDirFile();
2550 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002551 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002552 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002553 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002554 }
2555 }
2556
Jeff Browndefd4a62014-03-10 21:24:37 -07002557 mMainThread = mainThread;
2558 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002559 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560
Jeff Browndefd4a62014-03-10 21:24:37 -07002561 if (user == null) {
2562 user = Process.myUserHandle();
2563 }
2564 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002565
Todd Kennedy233a0b12018-01-29 20:30:24 +00002566 mPackageInfo = packageInfo;
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002567 mSplitName = splitName;
Adam Lesinski4e862812016-11-21 16:02:24 -08002568 mClassLoader = classLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -07002569 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002570
Makoto Onuki4becc352019-01-30 13:30:41 -08002571 String opPackageName;
2572
Jeff Browndefd4a62014-03-10 21:24:37 -07002573 if (container != null) {
2574 mBasePackageName = container.mBasePackageName;
Makoto Onuki4becc352019-01-30 13:30:41 -08002575 opPackageName = container.mOpPackageName;
Jason Monkbd983642017-02-24 16:57:40 -05002576 setResources(container.mResources);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002577 mDisplay = container.mDisplay;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002578 } else {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002579 mBasePackageName = packageInfo.mPackageName;
2580 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
Dianne Hackborn95d78532013-09-11 09:51:14 -07002581 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2582 // Special case: system components allow themselves to be loaded in to other
2583 // processes. For purposes of app ops, we must then consider the context as
2584 // belonging to the package of this process, not the system itself, otherwise
2585 // the package+uid verifications in app ops will fail.
Makoto Onuki4becc352019-01-30 13:30:41 -08002586 opPackageName = ActivityThread.currentPackageName();
Dianne Hackborn95d78532013-09-11 09:51:14 -07002587 } else {
Makoto Onuki4becc352019-01-30 13:30:41 -08002588 opPackageName = mBasePackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002589 }
2590 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002591
Makoto Onuki4becc352019-01-30 13:30:41 -08002592 mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName;
2593
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002594 mContentResolver = new ApplicationContentResolver(this, mainThread);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 }
2596
Jason Monkbd983642017-02-24 16:57:40 -05002597 void setResources(Resources r) {
Jason Monkbd60e5b2017-03-02 12:55:00 -05002598 if (r instanceof CompatResources) {
2599 ((CompatResources) r).setContext(this);
Jason Monkbd983642017-02-24 16:57:40 -05002600 }
Jason Monkbd60e5b2017-03-02 12:55:00 -05002601 mResources = r;
Jason Monkbd983642017-02-24 16:57:40 -05002602 }
2603
Narayan Kamath29564cd2014-08-07 10:57:40 +01002604 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002605 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 }
2607
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002608 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 final void scheduleFinalCleanup(String who, String what) {
2610 mMainThread.scheduleContextCleanup(this, who, what);
2611 }
2612
2613 final void performFinalCleanup(String who, String what) {
2614 //Log.i(TAG, "Cleanup up context: " + this);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002615 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
2617
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002618 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 final Context getReceiverRestrictedContext() {
2620 if (mReceiverRestrictedContext != null) {
2621 return mReceiverRestrictedContext;
2622 }
2623 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2624 }
2625
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002626 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 final void setOuterContext(Context context) {
2628 mOuterContext = context;
2629 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002630
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002631 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 final Context getOuterContext() {
2633 return mOuterContext;
2634 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002635
Tony Mak46aabe52016-11-14 12:53:06 +00002636 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002637 @UnsupportedAppUsage
Tony Mak46aabe52016-11-14 12:53:06 +00002638 public IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 return mActivityToken;
2640 }
2641
Jeff Sharkey634dc422016-01-30 17:44:15 -07002642 private void checkMode(int mode) {
2643 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2644 if ((mode & MODE_WORLD_READABLE) != 0) {
2645 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2646 }
2647 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2648 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2649 }
2650 }
2651 }
2652
Jeff Brown6e539312015-02-24 18:53:21 -08002653 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002654 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 int extraPermissions) {
2656 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2657 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2658 |extraPermissions;
2659 if ((mode&MODE_WORLD_READABLE) != 0) {
2660 perms |= FileUtils.S_IROTH;
2661 }
2662 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2663 perms |= FileUtils.S_IWOTH;
2664 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002665 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2667 + ", perms=0x" + Integer.toHexString(perms));
2668 }
2669 FileUtils.setPermissions(name, perms, -1, -1);
2670 }
2671
2672 private File makeFilename(File base, String name) {
2673 if (name.indexOf(File.separatorChar) < 0) {
Jeff Sharkeydd02e332018-06-27 14:41:57 -06002674 final File res = new File(base, name);
2675 // We report as filesystem access here to give us the best shot at
2676 // detecting apps that will pass the path down to native code.
2677 BlockGuard.getVmPolicy().onPathAccess(res.getPath());
2678 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 }
2680 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002681 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 }
2683
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002684 /**
2685 * Ensure that given directories exist, trying to create them if missing. If
2686 * unable to create, they are filtered by replacing with {@code null}.
2687 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002688 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkeyae266462017-11-27 13:32:24 -07002689 final StorageManager sm = getSystemService(StorageManager.class);
2690 final File[] result = new File[dirs.length];
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002691 for (int i = 0; i < dirs.length; i++) {
2692 File dir = dirs[i];
2693 if (!dir.exists()) {
2694 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002695 // recheck existence in case of cross-process race
2696 if (!dir.exists()) {
2697 // Failing to mkdir() may be okay, since we might not have
2698 // enough permissions; ask vold to create on our behalf.
Christopher Tatecc866da2013-10-02 18:11:01 -07002699 try {
Jeff Sharkeyae266462017-11-27 13:32:24 -07002700 sm.mkdirs(dir);
Jeff Sharkey983294592015-07-13 10:25:31 -07002701 } catch (Exception e) {
2702 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002703 dir = null;
2704 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002705 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002706 }
2707 }
2708 result[i] = dir;
2709 }
2710 return result;
2711 }
2712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 // ----------------------------------------------------------------------
2714 // ----------------------------------------------------------------------
2715 // ----------------------------------------------------------------------
2716
2717 private static final class ApplicationContentResolver extends ContentResolver {
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002718 @UnsupportedAppUsage
Jeff Sharkey6d515712012-09-20 16:06:08 -07002719 private final ActivityThread mMainThread;
Jeff Sharkey6d515712012-09-20 16:06:08 -07002720
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002721 public ApplicationContentResolver(Context context, ActivityThread mainThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002723 mMainThread = Preconditions.checkNotNull(mainThread);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
2725
2726 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002727 @UnsupportedAppUsage
Jeff Sharkey6d515712012-09-20 16:06:08 -07002728 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002729 return mMainThread.acquireProvider(context,
2730 ContentProvider.getAuthorityWithoutUserId(auth),
2731 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 }
2733
2734 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002735 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002736 return mMainThread.acquireExistingProvider(context,
2737 ContentProvider.getAuthorityWithoutUserId(auth),
2738 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002739 }
2740
2741 @Override
2742 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002743 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002745
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002746 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002747 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002748 return mMainThread.acquireProvider(c,
2749 ContentProvider.getAuthorityWithoutUserId(auth),
2750 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002751 }
2752
2753 @Override
2754 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002755 return mMainThread.releaseProvider(icp, false);
2756 }
2757
2758 @Override
2759 public void unstableProviderDied(IContentProvider icp) {
2760 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002761 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002762
2763 @Override
2764 public void appNotRespondingViaProvider(IContentProvider icp) {
2765 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2766 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002767
2768 /** @hide */
2769 protected int resolveUserIdFromAuthority(String auth) {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002770 return ContentProvider.getUserIdFromAuthority(auth, getUserId());
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773}