blob: 5945eef52b2ce6fc606e6a27d1b918965f5ad6b5 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070076import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070078import android.util.Slog;
Jeff Brown98365d72012-08-19 20:30:52 -070079import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080080import android.view.DisplayAdjustments;
Felipe Lemebb567ae2017-10-04 09:56:21 -070081import android.view.autofill.AutofillManager.AutofillClient;
Dan Egnor95240272009-10-27 18:23:39 -070082
Jeff Sharkey35871f22016-01-29 17:13:29 -070083import com.android.internal.annotations.GuardedBy;
84import com.android.internal.util.Preconditions;
85
Jeff Sharkeydd02e332018-06-27 14:41:57 -060086import dalvik.system.BlockGuard;
87
Jeff Sharkey83aacde2017-03-22 23:13:39 -060088import libcore.io.Memory;
Adam Lesinski4e862812016-11-21 16:02:24 -080089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import java.io.File;
91import java.io.FileInputStream;
92import java.io.FileNotFoundException;
93import java.io.FileOutputStream;
Jeff Sharkey35871f22016-01-29 17:13:29 -070094import java.io.FilenameFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.io.IOException;
96import java.io.InputStream;
Makoto Onukid67070e2018-03-30 12:39:14 -070097import java.lang.annotation.Retention;
98import java.lang.annotation.RetentionPolicy;
Jeff Sharkey83aacde2017-03-22 23:13:39 -060099import java.nio.ByteOrder;
Christopher Tatefe2368c2017-05-17 15:42:35 -0700100import java.util.ArrayList;
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700101import java.util.Objects;
Jeff Sharkey8439ac02017-12-12 17:26:23 -0700102import java.util.concurrent.Executor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104class ReceiverRestrictedContext extends ContextWrapper {
Andrei Onea4b2116c2019-02-28 15:13:42 +0000105 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 ReceiverRestrictedContext(Context base) {
107 super(base);
108 }
109
110 @Override
111 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
112 return registerReceiver(receiver, filter, null, null);
113 }
114
115 @Override
116 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
117 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700118 if (receiver == null) {
119 // Allow retrieving current sticky broadcast; this is safe since we
120 // aren't actually registering a receiver.
121 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
122 } else {
123 throw new ReceiverCallNotAllowedException(
124 "BroadcastReceiver components are not allowed to register to receive intents");
125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 }
127
128 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700129 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
130 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700131 if (receiver == null) {
132 // Allow retrieving current sticky broadcast; this is safe since we
133 // aren't actually registering a receiver.
134 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
135 } else {
136 throw new ReceiverCallNotAllowedException(
137 "BroadcastReceiver components are not allowed to register to receive intents");
138 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700139 }
140
141 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
143 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700144 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800146
147 @Override
Bo Liu58a57662019-03-06 20:21:45 +0000148 public boolean bindService(
149 Intent service, int flags, Executor executor, ServiceConnection conn) {
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800150 throw new ReceiverCallNotAllowedException(
Bo Liu58a57662019-03-06 20:21:45 +0000151 "BroadcastReceiver components are not allowed to bind to services");
152 }
153
154 @Override
155 public boolean bindIsolatedService(Intent service, int flags, String instanceName,
156 Executor executor, ServiceConnection conn) {
157 throw new ReceiverCallNotAllowedException(
158 "BroadcastReceiver components are not allowed to bind to services");
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160}
161
162/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800163 * Common implementation of Context API, which provides the base
164 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800166class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800167 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700168 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600170 private static final String XATTR_INODE_CACHE = "user.inode_cache";
171 private static final String XATTR_INODE_CODE_CACHE = "user.inode_code_cache";
172
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700173 /**
174 * Map from package name, to preference name, to cached preferences.
175 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700176 @GuardedBy("ContextImpl.class")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100177 @UnsupportedAppUsage
Jeff Sharkeybe782582016-02-15 18:35:57 -0700178 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
179
180 /**
181 * Map from preference name to generated path.
182 */
183 @GuardedBy("ContextImpl.class")
Mathew Inwood8c854f82018-09-14 12:35:36 +0100184 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkeybe782582016-02-15 18:35:57 -0700185 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100187 @UnsupportedAppUsage
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800188 final @NonNull ActivityThread mMainThread;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100189 @UnsupportedAppUsage
Todd Kennedy233a0b12018-01-29 20:30:24 +0000190 final @NonNull LoadedApk mPackageInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100191 @UnsupportedAppUsage
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800192 private @Nullable ClassLoader mClassLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -0700193
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800194 private final @Nullable IBinder mActivityToken;
Jeff Browndefd4a62014-03-10 21:24:37 -0700195
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700196 private final @NonNull UserHandle mUser;
Jeff Browndefd4a62014-03-10 21:24:37 -0700197
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100198 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -0700199 private final ApplicationContentResolver mContentResolver;
200
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100201 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -0700202 private final String mBasePackageName;
Mathew Inwood8c854f82018-09-14 12:35:36 +0100203 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Browndefd4a62014-03-10 21:24:37 -0700204 private final String mOpPackageName;
205
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700206 private final @NonNull ResourcesManager mResourcesManager;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100207 @UnsupportedAppUsage
Adam Lesinski8fa71072016-11-18 18:13:55 -0800208 private @NonNull Resources mResources;
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700209 private @Nullable Display mDisplay; // may be null if default display
Jeff Browndefd4a62014-03-10 21:24:37 -0700210
Mathew Inwood8c854f82018-09-14 12:35:36 +0100211 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700212 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700213
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100214 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 private Context mOuterContext;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100216 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 private int mThemeResource = 0;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100218 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 private Resources.Theme mTheme = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100220 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800224 // The name of the split this Context is representing. May be null.
225 private @Nullable String mSplitName = null;
226
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800227 private @Nullable AutofillClient mAutofillClient = null;
228 private @Nullable AutofillOptions mAutofillOptions;
Felipe Lemebb567ae2017-10-04 09:56:21 -0700229
Felipe Leme326f15a2019-02-19 09:42:24 -0800230 private ContentCaptureOptions mContentCaptureOptions = null;
Felipe Lemeecb08be2018-11-27 15:48:47 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 private final Object mSync = new Object();
233
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700234 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700236 @GuardedBy("mSync")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100237 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700239 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700241 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700242 private File mNoBackupFilesDir;
243 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700245 @GuardedBy("mSync")
246 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700247
Jeff Brown6e539312015-02-24 18:53:21 -0800248 // The system service cache for the system services that are cached per-ContextImpl.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100249 @UnsupportedAppUsage
Jeff Brown6e539312015-02-24 18:53:21 -0800250 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800251
Makoto Onukif896f122018-01-25 09:50:24 -0800252 static final int STATE_UNINITIALIZED = 0;
253 static final int STATE_INITIALIZING = 1;
254 static final int STATE_READY = 2;
Makoto Onukid67070e2018-03-30 12:39:14 -0700255 static final int STATE_NOT_FOUND = 3;
256
257 /** @hide */
258 @IntDef(prefix = { "STATE_" }, value = {
259 STATE_UNINITIALIZED,
260 STATE_INITIALIZING,
261 STATE_READY,
262 STATE_NOT_FOUND,
263 })
264 @Retention(RetentionPolicy.SOURCE)
265 @interface ServiceInitializationState {}
Makoto Onukif896f122018-01-25 09:50:24 -0800266
267 /**
268 * Initialization state for each service. Any of {@link #STATE_UNINITIALIZED},
269 * {@link #STATE_INITIALIZING} or {@link #STATE_READY},
270 */
Makoto Onukid67070e2018-03-30 12:39:14 -0700271 @ServiceInitializationState
272 final int[] mServiceInitializationStateArray = new int[mServiceCache.length];
Makoto Onukif896f122018-01-25 09:50:24 -0800273
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100274 @UnsupportedAppUsage
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700275 static ContextImpl getImpl(Context context) {
276 Context nextContext;
277 while ((context instanceof ContextWrapper) &&
278 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
279 context = nextContext;
280 }
281 return (ContextImpl)context;
282 }
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 @Override
285 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700286 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288
289 @Override
290 public Resources getResources() {
291 return mResources;
292 }
293
294 @Override
295 public PackageManager getPackageManager() {
296 if (mPackageManager != null) {
297 return mPackageManager;
298 }
299
300 IPackageManager pm = ActivityThread.getPackageManager();
301 if (pm != null) {
302 // Doesn't matter if we make more than one instance.
303 return (mPackageManager = new ApplicationPackageManager(this, pm));
304 }
305
306 return null;
307 }
308
309 @Override
310 public ContentResolver getContentResolver() {
311 return mContentResolver;
312 }
313
314 @Override
315 public Looper getMainLooper() {
316 return mMainThread.getLooper();
317 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 @Override
Jeff Sharkey8439ac02017-12-12 17:26:23 -0700320 public Executor getMainExecutor() {
321 return mMainThread.getExecutor();
322 }
323
324 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 public Context getApplicationContext() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000326 return (mPackageInfo != null) ?
327 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700331 public void setTheme(int resId) {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700332 synchronized (mSync) {
333 if (mThemeResource != resId) {
334 mThemeResource = resId;
335 initializeTheme();
336 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800341 public int getThemeResId() {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700342 synchronized (mSync) {
343 return mThemeResource;
344 }
Dianne Hackborn247fe742011-01-08 17:25:57 -0800345 }
346
347 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 public Resources.Theme getTheme() {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700349 synchronized (mSync) {
350 if (mTheme != null) {
351 return mTheme;
352 }
353
354 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
355 getOuterContext().getApplicationInfo().targetSdkVersion);
356 initializeTheme();
357
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700358 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
361
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700362 private void initializeTheme() {
363 if (mTheme == null) {
364 mTheme = mResources.newTheme();
365 }
366 mTheme.applyStyle(mThemeResource, true);
367 }
368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 @Override
370 public ClassLoader getClassLoader() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000371 return mClassLoader != null ? mClassLoader : (mPackageInfo != null ? mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373
374 @Override
375 public String getPackageName() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000376 if (mPackageInfo != null) {
377 return mPackageInfo.getPackageName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
Todd Kennedy233a0b12018-01-29 20:30:24 +0000379 // No mPackageInfo means this is a Context for the system itself,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800380 // and this here is its name.
381 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800384 /** @hide */
385 @Override
386 public String getBasePackageName() {
387 return mBasePackageName != null ? mBasePackageName : getPackageName();
388 }
389
Dianne Hackborn95d78532013-09-11 09:51:14 -0700390 /** @hide */
391 @Override
392 public String getOpPackageName() {
393 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
394 }
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700397 public ApplicationInfo getApplicationInfo() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000398 if (mPackageInfo != null) {
399 return mPackageInfo.getApplicationInfo();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700400 }
401 throw new RuntimeException("Not supported in system context");
402 }
403
404 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 public String getPackageResourcePath() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000406 if (mPackageInfo != null) {
407 return mPackageInfo.getResDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 throw new RuntimeException("Not supported in system context");
410 }
411
412 @Override
413 public String getPackageCodePath() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000414 if (mPackageInfo != null) {
415 return mPackageInfo.getAppDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 throw new RuntimeException("Not supported in system context");
418 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200419
Jeff Brown6e539312015-02-24 18:53:21 -0800420 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700422 // At least one application in the world actually passes in a null
423 // name. This happened to work because when we generated the file name
424 // we would stringify it to "null.xml". Nice.
Todd Kennedy233a0b12018-01-29 20:30:24 +0000425 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700426 Build.VERSION_CODES.KITKAT) {
427 if (name == null) {
428 name = "null";
429 }
430 }
431
Jeff Sharkeybe782582016-02-15 18:35:57 -0700432 File file;
433 synchronized (ContextImpl.class) {
434 if (mSharedPrefsPaths == null) {
435 mSharedPrefsPaths = new ArrayMap<>();
436 }
437 file = mSharedPrefsPaths.get(name);
438 if (file == null) {
439 file = getSharedPreferencesPath(name);
440 mSharedPrefsPaths.put(name, file);
441 }
442 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700443 return getSharedPreferences(file, mode);
444 }
445
446 @Override
447 public SharedPreferences getSharedPreferences(File file, int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700449 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700450 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
451 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700452 if (sp == null) {
John Reckf3903e92017-05-12 17:12:54 -0700453 checkMode(mode);
454 if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.O) {
455 if (isCredentialProtectedStorage()
Jeff Sharkeya65e6492017-06-21 13:45:11 -0600456 && !getSystemService(UserManager.class)
457 .isUserUnlockingOrUnlocked(UserHandle.myUserId())) {
John Reckf3903e92017-05-12 17:12:54 -0700458 throw new IllegalStateException("SharedPreferences in credential encrypted "
459 + "storage are not available until after user is unlocked");
460 }
461 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700462 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700463 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700464 return sp;
465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800467 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
468 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
469 // If somebody else (some other process) changed the prefs
470 // file behind our back, we reload it. This has been the
471 // historical (if undocumented) behavior.
472 sp.startReloadIfChangedUnexpectedly();
473 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700474 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
476
Andreas Gampe3f24e692018-02-05 13:24:28 -0800477 @GuardedBy("ContextImpl.class")
Jeff Sharkey35871f22016-01-29 17:13:29 -0700478 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700479 if (sSharedPrefsCache == null) {
480 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700481 }
482
483 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700484 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700485 if (packagePrefs == null) {
486 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700487 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700488 }
489
490 return packagePrefs;
491 }
492
Christopher Tatefe2368c2017-05-17 15:42:35 -0700493 @Override
494 public void reloadSharedPreferences() {
495 // Build the list of all per-context impls (i.e. caches) we know about
496 ArrayList<SharedPreferencesImpl> spImpls = new ArrayList<>();
497 synchronized (ContextImpl.class) {
498 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
499 for (int i = 0; i < cache.size(); i++) {
500 final SharedPreferencesImpl sp = cache.valueAt(i);
501 if (sp != null) {
502 spImpls.add(sp);
503 }
504 }
505 }
506
507 // Issue the reload outside the cache lock
508 for (int i = 0; i < spImpls.size(); i++) {
509 spImpls.get(i).startReloadIfChangedUnexpectedly();
510 }
511 }
512
Jeff Sharkey35871f22016-01-29 17:13:29 -0700513 /**
514 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700515 * requested prefix.
516 *
517 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700518 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600519 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700520 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
521 @Override
522 public boolean accept(File dir, String name) {
523 return name.startsWith(prefix);
524 }
525 });
526
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700527 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700528 for (File sourceFile : sourceFiles) {
529 final File targetFile = new File(targetDir, sourceFile.getName());
530 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
531 try {
532 FileUtils.copyFileOrThrow(sourceFile, targetFile);
533 FileUtils.copyPermissions(sourceFile, targetFile);
534 if (!sourceFile.delete()) {
535 throw new IOException("Failed to clean up " + sourceFile);
536 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700537 if (res != -1) {
538 res++;
539 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700540 } catch (IOException e) {
541 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700542 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700543 }
544 }
545 return res;
546 }
547
548 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600549 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700550 synchronized (ContextImpl.class) {
551 final File source = sourceContext.getSharedPreferencesPath(name);
552 final File target = getSharedPreferencesPath(name);
553
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600554 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700555 source.getName());
556 if (res > 0) {
557 // We moved at least one file, so evict any in-memory caches for
558 // either location
559 final ArrayMap<File, SharedPreferencesImpl> cache =
560 getSharedPreferencesCacheLocked();
561 cache.remove(source);
562 cache.remove(target);
563 }
564 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700565 }
566 }
567
568 @Override
569 public boolean deleteSharedPreferences(String name) {
570 synchronized (ContextImpl.class) {
571 final File prefs = getSharedPreferencesPath(name);
572 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
573
574 // Evict any in-memory caches
575 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
576 cache.remove(prefs);
577
578 prefs.delete();
579 prefsBackup.delete();
580
581 // We failed if files are still lingering
582 return !(prefs.exists() || prefsBackup.exists());
583 }
584 }
585
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100586 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 private File getPreferencesDir() {
588 synchronized (mSync) {
589 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700590 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700592 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
594 }
595
596 @Override
597 public FileInputStream openFileInput(String name)
598 throws FileNotFoundException {
599 File f = makeFilename(getFilesDir(), name);
600 return new FileInputStream(f);
601 }
602
603 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700604 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
605 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 final boolean append = (mode&MODE_APPEND) != 0;
607 File f = makeFilename(getFilesDir(), name);
608 try {
609 FileOutputStream fos = new FileOutputStream(f, append);
610 setFilePermissionsFromMode(f.getPath(), mode, 0);
611 return fos;
612 } catch (FileNotFoundException e) {
613 }
614
615 File parent = f.getParentFile();
616 parent.mkdir();
617 FileUtils.setPermissions(
618 parent.getPath(),
619 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
620 -1, -1);
621 FileOutputStream fos = new FileOutputStream(f, append);
622 setFilePermissionsFromMode(f.getPath(), mode, 0);
623 return fos;
624 }
625
626 @Override
627 public boolean deleteFile(String name) {
628 File f = makeFilename(getFilesDir(), name);
629 return f.delete();
630 }
631
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700632 /**
633 * Common-path handling of app data dir creation
634 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700635 private static File ensurePrivateDirExists(File file) {
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600636 return ensurePrivateDirExists(file, 0771, -1, null);
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700637 }
638
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600639 private static File ensurePrivateCacheDirExists(File file, String xattr) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700640 final int gid = UserHandle.getCacheAppGid(Process.myUid());
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600641 return ensurePrivateDirExists(file, 02771, gid, xattr);
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700642 }
643
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600644 private static File ensurePrivateDirExists(File file, int mode, int gid, String xattr) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700645 if (!file.exists()) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700646 final String path = file.getAbsolutePath();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700647 try {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700648 Os.mkdir(path, mode);
649 Os.chmod(path, mode);
650 if (gid != -1) {
651 Os.chown(path, -1, gid);
652 }
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700653 } catch (ErrnoException e) {
654 if (e.errno == OsConstants.EEXIST) {
655 // We must have raced with someone; that's okay
656 } else {
657 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700658 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700659 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600660
661 if (xattr != null) {
662 try {
663 final StructStat stat = Os.stat(file.getAbsolutePath());
664 final byte[] value = new byte[8];
665 Memory.pokeLong(value, 0, stat.st_ino, ByteOrder.nativeOrder());
666 Os.setxattr(file.getParentFile().getAbsolutePath(), xattr, value, 0);
667 } catch (ErrnoException e) {
668 Log.w(TAG, "Failed to update " + xattr + ": " + e.getMessage());
669 }
670 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700671 }
672 return file;
673 }
674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 @Override
676 public File getFilesDir() {
677 synchronized (mSync) {
678 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700679 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700681 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700682 }
683 }
684
685 @Override
686 public File getNoBackupFilesDir() {
687 synchronized (mSync) {
688 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700689 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700691 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800696 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700697 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700698 final File[] dirs = getExternalFilesDirs(type);
699 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700700 }
701
702 @Override
703 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800704 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600705 File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700706 if (type != null) {
707 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800708 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700709 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800710 }
711 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200712
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800713 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800714 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700715 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700716 final File[] dirs = getObbDirs();
717 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700718 }
719
720 @Override
721 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800722 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600723 File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
724 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800725 }
726 }
727
728 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 public File getCacheDir() {
730 synchronized (mSync) {
731 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700732 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600734 return ensurePrivateCacheDirExists(mCacheDir, XATTR_INODE_CACHE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200737
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800738 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700739 public File getCodeCacheDir() {
740 synchronized (mSync) {
741 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700742 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700743 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600744 return ensurePrivateCacheDirExists(mCodeCacheDir, XATTR_INODE_CODE_CACHE);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700745 }
746 }
747
748 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800749 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700750 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700751 final File[] dirs = getExternalCacheDirs();
752 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700753 }
754
755 @Override
756 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800757 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600758 File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
759 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800760 }
761 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700764 public File[] getExternalMediaDirs() {
765 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600766 File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
767 return ensureExternalDirsExistOrFilter(dirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700768 }
769 }
770
Fyodor Kupolov61221292016-09-02 15:21:03 -0700771 /**
772 * @hide
773 */
774 @Nullable
775 @Override
776 public File getPreloadsFileCache() {
777 return Environment.getDataPreloadsFileCacheDirectory(getPackageName());
778 }
779
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700780 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 public File getFileStreamPath(String name) {
782 return makeFilename(getFilesDir(), name);
783 }
784
785 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700786 public File getSharedPreferencesPath(String name) {
787 return makeFilename(getPreferencesDir(), name + ".xml");
788 }
789
790 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700792 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
794
795 @Override
796 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700797 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799
800 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700801 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
802 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700803 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700804 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700805 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
806 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
807 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
808 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800809 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
810 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
811 }
Jeff Brown47847f32012-03-22 19:13:11 -0700812 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700813 setFilePermissionsFromMode(f.getPath(), mode, 0);
814 return db;
815 }
816
817 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600818 public boolean moveDatabaseFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700819 synchronized (ContextImpl.class) {
820 final File source = sourceContext.getDatabasePath(name);
821 final File target = getDatabasePath(name);
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600822 return moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700823 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700824 }
825 }
826
827 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 public boolean deleteDatabase(String name) {
829 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700830 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800831 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 } catch (Exception e) {
833 }
834 return false;
835 }
836
837 @Override
838 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700839 File dir;
840 File f;
841
842 if (name.charAt(0) == File.separatorChar) {
843 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
844 dir = new File(dirPath);
845 name = name.substring(name.lastIndexOf(File.separatorChar));
846 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700847
848 if (!dir.isDirectory() && dir.mkdir()) {
849 FileUtils.setPermissions(dir.getPath(),
850 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
851 -1, -1);
852 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700853 } else {
854 dir = getDatabasesDir();
855 f = makeFilename(dir, name);
856 }
857
Jeff Sharkey35871f22016-01-29 17:13:29 -0700858 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 }
860
861 @Override
862 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700863 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 }
865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 private File getDatabasesDir() {
867 synchronized (mSync) {
868 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700869 if ("android".equals(getPackageName())) {
870 mDatabasesDir = new File("/data/system");
871 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700872 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700875 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
877 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800880 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700882 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 }
884
885 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800886 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700887 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700888 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 }
890
891 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800892 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700894 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 }
896
897 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800898 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700900 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 }
902
903 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800904 @Deprecated
905 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700906 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
908
909 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800910 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700912 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
915 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800916 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700918 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920
Jeff Brown6e539312015-02-24 18:53:21 -0800921 private WallpaperManager getWallpaperManager() {
922 return getSystemService(WallpaperManager.class);
923 }
924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 @Override
926 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700927 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700928 startActivity(intent, null);
929 }
930
Amith Yamasani82644082012-08-03 13:09:11 -0700931 /** @hide */
932 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700933 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700934 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700935 }
936
Dianne Hackborna4972e92012-03-14 10:38:05 -0700937 @Override
938 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700939 warnIfCallingFromSystemProcess();
Jorim Jaggi2adba072016-03-03 13:43:39 +0100940
941 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
942 // generally not allowed, except if the caller specifies the task id the activity should
Bryce Leef465ea02017-05-02 16:58:22 -0700943 // be launched in. A bug was existed between N and O-MR1 which allowed this to work. We
944 // maintain this for backwards compatibility.
945 final int targetSdkVersion = getApplicationInfo().targetSdkVersion;
946
947 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
948 && (targetSdkVersion < Build.VERSION_CODES.N
949 || targetSdkVersion >= Build.VERSION_CODES.P)
950 && (options == null
951 || ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 throw new AndroidRuntimeException(
953 "Calling startActivity() from outside of an Activity "
Bryce Leef465ea02017-05-02 16:58:22 -0700954 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
955 + " Is this really what you want?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700958 getOuterContext(), mMainThread.getApplicationThread(), null,
959 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 }
961
Amith Yamasani258848d2012-08-10 17:06:33 -0700962 /** @hide */
963 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700964 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700965 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700966 ActivityTaskManager.getService().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800967 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700968 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700969 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700970 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700971 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700972 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700973 }
974 }
975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800977 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700978 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700979 startActivities(intents, null);
980 }
981
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700982 /** @hide */
983 @Override
Selim Cinek7fa385a2018-01-24 08:35:28 -0800984 public int startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700985 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
986 throw new AndroidRuntimeException(
987 "Calling startActivities() from outside of an Activity "
988 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
989 + " Is this really what you want?");
990 }
Selim Cinek7fa385a2018-01-24 08:35:28 -0800991 return mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700992 getOuterContext(), mMainThread.getApplicationThread(), null,
993 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700994 }
995
Dianne Hackborna4972e92012-03-14 10:38:05 -0700996 @Override
997 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700998 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800999 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1000 throw new AndroidRuntimeException(
1001 "Calling startActivities() from outside of an Activity "
1002 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
1003 + " Is this really what you want?");
1004 }
1005 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -07001006 getOuterContext(), mMainThread.getApplicationThread(), null,
1007 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001008 }
1009
1010 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001011 public void startIntentSender(IntentSender intent,
1012 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
1013 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001014 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
1015 }
1016
1017 @Override
1018 public void startIntentSender(IntentSender intent, Intent fillInIntent,
1019 int flagsMask, int flagsValues, int extraFlags, Bundle options)
1020 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001021 try {
1022 String resolvedType = null;
1023 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001024 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07001025 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001026 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
1027 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001028 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07001029 .startActivityIntentSender(mMainThread.getApplicationThread(),
1030 intent != null ? intent.getTarget() : null,
1031 intent != null ? intent.getWhitelistToken() : null,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001032 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001033 0, flagsMask, flagsValues, options);
1034 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001035 throw new IntentSender.SendIntentException();
1036 }
1037 Instrumentation.checkStartActivityResult(result, null);
1038 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001039 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001040 }
1041 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001042
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001043 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001045 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1047 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001048 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001049 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001050 mMainThread.getApplicationThread(), intent, resolvedType, null,
1051 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
1052 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001054 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
1056 }
1057
Amith Yamasani67cf7d32012-02-16 14:31:23 -08001058 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001060 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001062 String[] receiverPermissions = receiverPermission == null ? null
1063 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001065 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001066 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001067 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001068 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1069 null, false, false, getUserId());
1070 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001071 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001072 }
1073 }
1074
1075 @Override
1076 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
1077 warnIfCallingFromSystemProcess();
1078 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1079 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001080 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001081 ActivityManager.getService().broadcastIntent(
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001082 mMainThread.getApplicationThread(), intent, resolvedType, null,
1083 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001084 null, false, false, getUserId());
1085 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001086 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -07001087 }
1088 }
1089
1090 @Override
Tyler Gunnf955e562018-04-26 14:43:31 -07001091 public void sendBroadcastAsUserMultiplePermissions(Intent intent, UserHandle user,
1092 String[] receiverPermissions) {
1093 warnIfCallingFromSystemProcess();
1094 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1095 try {
1096 intent.prepareToLeaveProcess(this);
1097 ActivityManager.getService().broadcastIntent(
1098 mMainThread.getApplicationThread(), intent, resolvedType, null,
1099 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1100 null, false, false, user.getIdentifier());
1101 } catch (RemoteException e) {
1102 throw e.rethrowFromSystemServer();
1103 }
1104 }
1105
1106 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -07001107 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
1108 warnIfCallingFromSystemProcess();
1109 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001110 String[] receiverPermissions = receiverPermission == null ? null
1111 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -07001112 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001113 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001114 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001115 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001116 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001117 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001118 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001119 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001120 }
1121 }
1122
1123 @Override
1124 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
1125 warnIfCallingFromSystemProcess();
1126 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001127 String[] receiverPermissions = receiverPermission == null ? null
1128 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001129 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001130 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001131 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001132 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001133 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -07001134 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001136 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138 }
1139
1140 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -07001141 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001142 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001144 String[] receiverPermissions = receiverPermission == null ? null
1145 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001147 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001148 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001149 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001150 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001151 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001153 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
1155 }
1156
1157 @Override
1158 public void sendOrderedBroadcast(Intent intent,
1159 String receiverPermission, BroadcastReceiver resultReceiver,
1160 Handler scheduler, int initialCode, String initialData,
1161 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001162 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001163 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1164 }
1165
1166 @Override
1167 public void sendOrderedBroadcast(Intent intent,
1168 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
1169 Handler scheduler, int initialCode, String initialData,
1170 Bundle initialExtras) {
1171 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1172 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001173 }
1174
1175 @Override
1176 public void sendOrderedBroadcast(Intent intent,
1177 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1178 Handler scheduler, int initialCode, String initialData,
1179 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001180 sendOrderedBroadcast(intent, receiverPermission, appOp,
1181 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1182 }
1183
1184 void sendOrderedBroadcast(Intent intent,
1185 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1186 Handler scheduler, int initialCode, String initialData,
1187 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001188 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 IIntentReceiver rd = null;
1190 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001191 if (mPackageInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 if (scheduler == null) {
1193 scheduler = mMainThread.getHandler();
1194 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001195 rd = mPackageInfo.getReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 resultReceiver, getOuterContext(), scheduler,
1197 mMainThread.getInstrumentation(), false);
1198 } else {
1199 if (scheduler == null) {
1200 scheduler = mMainThread.getHandler();
1201 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001202 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1204 }
1205 }
1206 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001207 String[] receiverPermissions = receiverPermission == null ? null
1208 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001210 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001211 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001213 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001214 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001216 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
1218 }
1219
1220 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001221 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001222 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1223 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001224 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001225 ActivityManager.getService().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001226 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001227 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001228 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001229 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001230 }
1231 }
1232
1233 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001234 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1235 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001236 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1237 }
1238
1239 @Override
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001240 public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission,
1241 Bundle options) {
1242 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1243 String[] receiverPermissions = receiverPermission == null ? null
1244 : new String[] {receiverPermission};
1245 try {
1246 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001247 ActivityManager.getService().broadcastIntent(
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001248 mMainThread.getApplicationThread(), intent, resolvedType, null,
1249 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1250 options, false, false, user.getIdentifier());
1251 } catch (RemoteException e) {
1252 throw e.rethrowFromSystemServer();
1253 }
1254 }
1255
1256 @Override
Svet Ganov16a16892015-04-16 10:32:04 -07001257 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1258 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001259 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001260 String[] receiverPermissions = receiverPermission == null ? null
1261 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001262 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001263 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001264 ActivityManager.getService().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001265 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001266 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001267 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001268 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001269 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001270 }
1271 }
1272
1273 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001274 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001275 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001276 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001277 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001278 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001279 }
1280
1281 @Override
1282 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1283 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001284 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1285 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1286 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1287 }
1288
1289 @Override
1290 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1291 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1292 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001293 IIntentReceiver rd = null;
1294 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001295 if (mPackageInfo != null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001296 if (scheduler == null) {
1297 scheduler = mMainThread.getHandler();
1298 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001299 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001300 resultReceiver, getOuterContext(), scheduler,
1301 mMainThread.getInstrumentation(), false);
1302 } else {
1303 if (scheduler == null) {
1304 scheduler = mMainThread.getHandler();
1305 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001306 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1307 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001308 }
1309 }
1310 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001311 String[] receiverPermissions = receiverPermission == null ? null
1312 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001313 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001314 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001315 ActivityManager.getService().broadcastIntent(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001316 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001317 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001318 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001319 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001320 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001321 }
1322 }
1323
1324 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001325 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001327 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1329 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001330 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001331 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001333 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001334 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001336 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338 }
1339
1340 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001341 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001342 public void sendStickyOrderedBroadcast(Intent intent,
1343 BroadcastReceiver resultReceiver,
1344 Handler scheduler, int initialCode, String initialData,
1345 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001346 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001347 IIntentReceiver rd = null;
1348 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001349 if (mPackageInfo != null) {
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001350 if (scheduler == null) {
1351 scheduler = mMainThread.getHandler();
1352 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001353 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001354 resultReceiver, getOuterContext(), scheduler,
1355 mMainThread.getInstrumentation(), false);
1356 } else {
1357 if (scheduler == null) {
1358 scheduler = mMainThread.getHandler();
1359 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001360 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001361 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1362 }
1363 }
1364 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1365 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001366 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001367 ActivityManager.getService().broadcastIntent(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001368 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1369 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001370 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001371 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001372 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001373 }
1374 }
1375
1376 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001377 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 public void removeStickyBroadcast(Intent intent) {
1379 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1380 if (resolvedType != null) {
1381 intent = new Intent(intent);
1382 intent.setDataAndType(intent.getData(), resolvedType);
1383 }
1384 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001385 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001386 ActivityManager.getService().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001387 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001389 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
1391 }
1392
1393 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001394 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001395 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1396 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1397 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001398 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001399 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001400 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001401 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1402 user.getIdentifier());
1403 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001404 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001405 }
1406 }
1407
1408 @Override
1409 @Deprecated
1410 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1411 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1412 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001413 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001414 ActivityManager.getService().broadcastIntent(
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001415 mMainThread.getApplicationThread(), intent, resolvedType, null,
1416 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1417 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001418 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001419 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001420 }
1421 }
1422
1423 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001424 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001425 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1426 UserHandle user, BroadcastReceiver resultReceiver,
1427 Handler scheduler, int initialCode, String initialData,
1428 Bundle initialExtras) {
1429 IIntentReceiver rd = null;
1430 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001431 if (mPackageInfo != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001432 if (scheduler == null) {
1433 scheduler = mMainThread.getHandler();
1434 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001435 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001436 resultReceiver, getOuterContext(), scheduler,
1437 mMainThread.getInstrumentation(), false);
1438 } else {
1439 if (scheduler == null) {
1440 scheduler = mMainThread.getHandler();
1441 }
1442 rd = new LoadedApk.ReceiverDispatcher(
1443 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1444 }
1445 }
1446 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1447 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001448 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001449 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001450 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1451 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001452 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001453 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001454 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001455 }
1456 }
1457
1458 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001459 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001460 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1461 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1462 if (resolvedType != null) {
1463 intent = new Intent(intent);
1464 intent.setDataAndType(intent.getData(), resolvedType);
1465 }
1466 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001467 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001468 ActivityManager.getService().unbroadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001469 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1470 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001471 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001472 }
1473 }
1474
1475 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1477 return registerReceiver(receiver, filter, null, null);
1478 }
1479
1480 @Override
1481 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001482 int flags) {
1483 return registerReceiver(receiver, filter, null, null, flags);
Chad Brubaker816c83b2017-03-02 10:27:59 -08001484 }
1485
1486 @Override
1487 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001489 return registerReceiverInternal(receiver, getUserId(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001490 filter, broadcastPermission, scheduler, getOuterContext(), 0);
Chad Brubaker816c83b2017-03-02 10:27:59 -08001491 }
1492
1493 @Override
1494 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001495 String broadcastPermission, Handler scheduler, int flags) {
Chad Brubaker816c83b2017-03-02 10:27:59 -08001496 return registerReceiverInternal(receiver, getUserId(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001497 filter, broadcastPermission, scheduler, getOuterContext(), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
1499
Dianne Hackborn20e80982012-08-31 19:00:44 -07001500 @Override
1501 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1502 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1503 return registerReceiverInternal(receiver, user.getIdentifier(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001504 filter, broadcastPermission, scheduler, getOuterContext(), 0);
Dianne Hackborn20e80982012-08-31 19:00:44 -07001505 }
1506
1507 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 IntentFilter filter, String broadcastPermission,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001509 Handler scheduler, Context context, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 IIntentReceiver rd = null;
1511 if (receiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001512 if (mPackageInfo != null && context != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 if (scheduler == null) {
1514 scheduler = mMainThread.getHandler();
1515 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001516 rd = mPackageInfo.getReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 receiver, context, scheduler,
1518 mMainThread.getInstrumentation(), true);
1519 } else {
1520 if (scheduler == null) {
1521 scheduler = mMainThread.getHandler();
1522 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001523 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001524 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
1526 }
1527 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001528 final Intent intent = ActivityManager.getService().registerReceiver(
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001529 mMainThread.getApplicationThread(), mBasePackageName, rd, filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001530 broadcastPermission, userId, flags);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001531 if (intent != null) {
1532 intent.setExtrasClassLoader(getClassLoader());
1533 intent.prepareToEnterProcess();
1534 }
1535 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001537 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539 }
1540
1541 @Override
1542 public void unregisterReceiver(BroadcastReceiver receiver) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001543 if (mPackageInfo != null) {
1544 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 getOuterContext(), receiver);
1546 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001547 ActivityManager.getService().unregisterReceiver(rd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001549 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
1551 } else {
1552 throw new RuntimeException("Not supported in system context");
1553 }
1554 }
1555
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001556 private void validateServiceIntent(Intent service) {
1557 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001558 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001559 IllegalArgumentException ex = new IllegalArgumentException(
1560 "Service Intent must be explicit: " + service);
1561 throw ex;
1562 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001563 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1564 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001565 }
1566 }
1567 }
1568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 @Override
1570 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001571 warnIfCallingFromSystemProcess();
Christopher Tate242ba3e92017-04-14 15:07:06 -07001572 return startServiceCommon(service, false, mUser);
Christopher Tate42a386b2016-11-07 12:21:21 -08001573 }
1574
1575 @Override
Christopher Tate79047c62017-03-21 11:37:06 -07001576 public ComponentName startForegroundService(Intent service) {
1577 warnIfCallingFromSystemProcess();
Christopher Tate242ba3e92017-04-14 15:07:06 -07001578 return startServiceCommon(service, true, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001579 }
1580
1581 @Override
1582 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001583 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001584 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001585 }
1586
1587 @Override
1588 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Christopher Tate242ba3e92017-04-14 15:07:06 -07001589 return startServiceCommon(service, false, user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001590 }
1591
Christopher Tate42a386b2016-11-07 12:21:21 -08001592 @Override
Christopher Tate79047c62017-03-21 11:37:06 -07001593 public ComponentName startForegroundServiceAsUser(Intent service, UserHandle user) {
Christopher Tate242ba3e92017-04-14 15:07:06 -07001594 return startServiceCommon(service, true, user);
Christopher Tate79047c62017-03-21 11:37:06 -07001595 }
1596
Christopher Tate242ba3e92017-04-14 15:07:06 -07001597 private ComponentName startServiceCommon(Intent service, boolean requireForeground,
1598 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001600 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001601 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001602 ComponentName cn = ActivityManager.getService().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001603 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
Christopher Tate242ba3e92017-04-14 15:07:06 -07001604 getContentResolver()), requireForeground,
Christopher Tate79047c62017-03-21 11:37:06 -07001605 getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001606 if (cn != null) {
1607 if (cn.getPackageName().equals("!")) {
1608 throw new SecurityException(
1609 "Not allowed to start service " + service
1610 + " without permission " + cn.getClassName());
1611 } else if (cn.getPackageName().equals("!!")) {
1612 throw new SecurityException(
1613 "Unable to start service " + service
1614 + ": " + cn.getClassName());
Dianne Hackborn85e35642017-01-12 15:10:57 -08001615 } else if (cn.getPackageName().equals("?")) {
1616 throw new IllegalStateException(
1617 "Not allowed to start service " + service + ": " + cn.getClassName());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 }
1620 return cn;
1621 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001622 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
1624 }
1625
1626 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001627 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001628 return stopServiceCommon(service, user);
1629 }
1630
1631 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001633 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001634 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001635 int res = ActivityManager.getService().stopService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001637 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 if (res < 0) {
1639 throw new SecurityException(
1640 "Not allowed to stop service " + service);
1641 }
1642 return res != 0;
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
Bo Liu58a57662019-03-06 20:21:45 +00001649 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001650 warnIfCallingFromSystemProcess();
Bo Liu58a57662019-03-06 20:21:45 +00001651 return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null,
1652 getUser());
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001653 }
1654
1655 @Override
Bo Liu58a57662019-03-06 20:21:45 +00001656 public boolean bindService(
1657 Intent service, int flags, Executor executor, ServiceConnection conn) {
1658 warnIfCallingFromSystemProcess();
1659 return bindServiceCommon(service, conn, flags, null, null, executor, getUser());
1660 }
1661
1662 @Override
1663 public boolean bindIsolatedService(Intent service, int flags, String instanceName,
1664 Executor executor, ServiceConnection conn) {
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001665 warnIfCallingFromSystemProcess();
1666 if (instanceName == null) {
1667 throw new NullPointerException("null instanceName");
1668 }
Bo Liu58a57662019-03-06 20:21:45 +00001669 return bindServiceCommon(service, conn, flags, instanceName, null, executor, getUser());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001670 }
1671
1672 /** @hide */
1673 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001674 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1675 UserHandle user) {
Bo Liu58a57662019-03-06 20:21:45 +00001676 return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null, user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001677 }
1678
Adrian Roos691546e2016-02-09 10:13:41 -08001679 /** @hide */
1680 @Override
1681 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1682 Handler handler, UserHandle user) {
1683 if (handler == null) {
1684 throw new IllegalArgumentException("handler must not be null.");
1685 }
Bo Liu58a57662019-03-06 20:21:45 +00001686 return bindServiceCommon(service, conn, flags, null, handler, null, user);
Adrian Roos691546e2016-02-09 10:13:41 -08001687 }
1688
Tony Mak46aabe52016-11-14 12:53:06 +00001689 /** @hide */
1690 @Override
1691 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler,
1692 int flags) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001693 return mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
Tony Mak46aabe52016-11-14 12:53:06 +00001694 }
1695
1696 /** @hide */
1697 @Override
1698 public IApplicationThread getIApplicationThread() {
1699 return mMainThread.getApplicationThread();
1700 }
1701
Tony Makbf9928d2016-12-22 11:02:45 +00001702 /** @hide */
1703 @Override
1704 public Handler getMainThreadHandler() {
1705 return mMainThread.getHandler();
1706 }
1707
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001708 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,
Bo Liu58a57662019-03-06 20:21:45 +00001709 String instanceName, Handler handler, Executor executor, UserHandle user) {
Tony Mak46aabe52016-11-14 12:53:06 +00001710 // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001712 if (conn == null) {
1713 throw new IllegalArgumentException("connection is null");
1714 }
Bo Liu58a57662019-03-06 20:21:45 +00001715 if (handler != null && executor != null) {
1716 throw new IllegalArgumentException("Handler and Executor both supplied");
1717 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001718 if (mPackageInfo != null) {
Bo Liu58a57662019-03-06 20:21:45 +00001719 if (executor != null) {
1720 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), executor, flags);
1721 } else {
1722 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
1723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 } else {
1725 throw new RuntimeException("Not supported in system context");
1726 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001727 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001729 IBinder token = getActivityToken();
Todd Kennedy233a0b12018-01-29 20:30:24 +00001730 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1731 && mPackageInfo.getApplicationInfo().targetSdkVersion
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001732 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1733 flags |= BIND_WAIVE_PRIORITY;
1734 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001735 service.prepareToLeaveProcess(this);
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001736 int res = ActivityManager.getService().bindIsolatedService(
Svet Ganov99b60432015-06-27 13:15:22 -07001737 mMainThread.getApplicationThread(), getActivityToken(), service,
1738 service.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001739 sd, flags, instanceName, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 if (res < 0) {
1741 throw new SecurityException(
1742 "Not allowed to bind to service " + service);
1743 }
1744 return res != 0;
1745 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001746 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
1748 }
1749
1750 @Override
Dianne Hackborna631d562018-11-20 15:58:15 -08001751 public void updateServiceGroup(@NonNull ServiceConnection conn, int group, int importance) {
1752 if (conn == null) {
1753 throw new IllegalArgumentException("connection is null");
1754 }
1755 if (mPackageInfo != null) {
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -08001756 IServiceConnection sd = mPackageInfo.lookupServiceDispatcher(conn, getOuterContext());
1757 if (sd == null) {
1758 throw new IllegalArgumentException("ServiceConnection not currently bound: "
1759 + conn);
1760 }
Dianne Hackborna631d562018-11-20 15:58:15 -08001761 try {
1762 ActivityManager.getService().updateServiceGroup(sd, group, importance);
1763 } catch (RemoteException e) {
1764 throw e.rethrowFromSystemServer();
1765 }
1766 } else {
1767 throw new RuntimeException("Not supported in system context");
1768 }
1769 }
1770
1771 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001773 if (conn == null) {
1774 throw new IllegalArgumentException("connection is null");
1775 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001776 if (mPackageInfo != null) {
1777 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 getOuterContext(), conn);
1779 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001780 ActivityManager.getService().unbindService(sd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001782 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784 } else {
1785 throw new RuntimeException("Not supported in system context");
1786 }
1787 }
1788
1789 @Override
1790 public boolean startInstrumentation(ComponentName className,
1791 String profileFile, Bundle arguments) {
1792 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001793 if (arguments != null) {
1794 arguments.setAllowFds(false);
1795 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001796 return ActivityManager.getService().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001797 className, profileFile, 0, arguments, null, null, getUserId(),
1798 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001800 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803
1804 @Override
1805 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001806 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
1808
Jeff Brown6e539312015-02-24 18:53:21 -08001809 @Override
1810 public String getSystemServiceName(Class<?> serviceClass) {
1811 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001812 }
1813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 @Override
1815 public int checkPermission(String permission, int pid, int uid) {
1816 if (permission == null) {
1817 throw new IllegalArgumentException("permission is null");
1818 }
1819
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001820 final IActivityManager am = ActivityManager.getService();
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001821 if (am == null) {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001822 // Well this is super awkward; we somehow don't have an active
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001823 // ActivityManager instance. If we're testing a root or system
1824 // UID, then they totally have whatever permission this is.
1825 final int appId = UserHandle.getAppId(uid);
1826 if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) {
1827 Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " holds " + permission);
1828 return PackageManager.PERMISSION_GRANTED;
1829 }
Erik Wolsheimere21576f2018-03-29 16:21:41 -07001830 Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " does not hold "
1831 + permission);
1832 return PackageManager.PERMISSION_DENIED;
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001833 }
1834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 try {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001836 return am.checkPermission(permission, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001838 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840 }
1841
Dianne Hackbornff170242014-11-19 10:59:01 -08001842 /** @hide */
1843 @Override
1844 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1845 if (permission == null) {
1846 throw new IllegalArgumentException("permission is null");
1847 }
1848
1849 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001850 return ActivityManager.getService().checkPermissionWithToken(
Dianne Hackbornff170242014-11-19 10:59:01 -08001851 permission, pid, uid, callerToken);
1852 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001853 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001854 }
1855 }
1856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 @Override
1858 public int checkCallingPermission(String permission) {
1859 if (permission == null) {
1860 throw new IllegalArgumentException("permission is null");
1861 }
1862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 int pid = Binder.getCallingPid();
1864 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001865 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 }
1867 return PackageManager.PERMISSION_DENIED;
1868 }
1869
1870 @Override
1871 public int checkCallingOrSelfPermission(String permission) {
1872 if (permission == null) {
1873 throw new IllegalArgumentException("permission is null");
1874 }
1875
1876 return checkPermission(permission, Binder.getCallingPid(),
1877 Binder.getCallingUid());
1878 }
1879
Svetoslavc6d1c342015-02-26 14:44:43 -08001880 @Override
1881 public int checkSelfPermission(String permission) {
1882 if (permission == null) {
1883 throw new IllegalArgumentException("permission is null");
1884 }
1885
1886 return checkPermission(permission, Process.myPid(), Process.myUid());
1887 }
1888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 private void enforce(
1890 String permission, int resultOfCheck,
1891 boolean selfToo, int uid, String message) {
1892 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1893 throw new SecurityException(
1894 (message != null ? (message + ": ") : "") +
1895 (selfToo
1896 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001897 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 permission +
1899 ".");
1900 }
1901 }
1902
Jeff Brown6e539312015-02-24 18:53:21 -08001903 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 public void enforcePermission(
1905 String permission, int pid, int uid, String message) {
1906 enforce(permission,
1907 checkPermission(permission, pid, uid),
1908 false,
1909 uid,
1910 message);
1911 }
1912
Jeff Brown6e539312015-02-24 18:53:21 -08001913 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 public void enforceCallingPermission(String permission, String message) {
1915 enforce(permission,
1916 checkCallingPermission(permission),
1917 false,
1918 Binder.getCallingUid(),
1919 message);
1920 }
1921
Jeff Brown6e539312015-02-24 18:53:21 -08001922 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 public void enforceCallingOrSelfPermission(
1924 String permission, String message) {
1925 enforce(permission,
1926 checkCallingOrSelfPermission(permission),
1927 true,
1928 Binder.getCallingUid(),
1929 message);
1930 }
1931
1932 @Override
1933 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1934 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001935 ActivityManager.getService().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001936 mMainThread.getApplicationThread(), toPackage,
1937 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001939 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 }
1941 }
1942
1943 @Override
1944 public void revokeUriPermission(Uri uri, int modeFlags) {
1945 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001946 ActivityManager.getService().revokeUriPermission(
Dianne Hackborna47223f2017-03-30 13:49:13 -07001947 mMainThread.getApplicationThread(), null,
1948 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
1949 } catch (RemoteException e) {
1950 throw e.rethrowFromSystemServer();
1951 }
1952 }
1953
1954 @Override
1955 public void revokeUriPermission(String targetPackage, Uri uri, int modeFlags) {
1956 try {
1957 ActivityManager.getService().revokeUriPermission(
1958 mMainThread.getApplicationThread(), targetPackage,
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001959 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 int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001968 return ActivityManager.getService().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001969 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001970 resolveUserId(uri), null);
1971 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001972 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001973 }
1974 }
1975
1976 /** @hide */
1977 @Override
1978 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1979 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001980 return ActivityManager.getService().checkUriPermission(
Dianne Hackbornff170242014-11-19 10:59:01 -08001981 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1982 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001984 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986 }
1987
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001988 private int resolveUserId(Uri uri) {
1989 return ContentProvider.getUserIdFromUri(uri, getUserId());
1990 }
1991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 @Override
1993 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 int pid = Binder.getCallingPid();
1995 if (pid != Process.myPid()) {
1996 return checkUriPermission(uri, pid,
1997 Binder.getCallingUid(), modeFlags);
1998 }
1999 return PackageManager.PERMISSION_DENIED;
2000 }
2001
2002 @Override
2003 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
2004 return checkUriPermission(uri, Binder.getCallingPid(),
2005 Binder.getCallingUid(), modeFlags);
2006 }
2007
2008 @Override
2009 public int checkUriPermission(Uri uri, String readPermission,
2010 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002011 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
2013 + readPermission + " writePermission=" + writePermission
2014 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
2015 }
2016 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
2017 if (readPermission == null
2018 || checkPermission(readPermission, pid, uid)
2019 == PackageManager.PERMISSION_GRANTED) {
2020 return PackageManager.PERMISSION_GRANTED;
2021 }
2022 }
2023 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
2024 if (writePermission == null
2025 || checkPermission(writePermission, pid, uid)
2026 == PackageManager.PERMISSION_GRANTED) {
2027 return PackageManager.PERMISSION_GRANTED;
2028 }
2029 }
2030 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
2031 : PackageManager.PERMISSION_DENIED;
2032 }
2033
2034 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07002035 StringBuilder builder = new StringBuilder();
2036 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
2037 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07002039 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
2040 builder.append("write and ");
2041 }
2042 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
2043 builder.append("persistable and ");
2044 }
2045 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
2046 builder.append("prefix and ");
2047 }
2048
2049 if (builder.length() > 5) {
2050 builder.setLength(builder.length() - 5);
2051 return builder.toString();
2052 } else {
2053 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
2054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
2056
2057 private void enforceForUri(
2058 int modeFlags, int resultOfCheck, boolean selfToo,
2059 int uid, Uri uri, String message) {
2060 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
2061 throw new SecurityException(
2062 (message != null ? (message + ": ") : "") +
2063 (selfToo
2064 ? "Neither user " + uid + " nor current process has "
2065 : "User " + uid + " does not have ") +
2066 uriModeFlagToString(modeFlags) +
2067 " permission on " +
2068 uri +
2069 ".");
2070 }
2071 }
2072
Jeff Brown6e539312015-02-24 18:53:21 -08002073 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 public void enforceUriPermission(
2075 Uri uri, int pid, int uid, int modeFlags, String message) {
2076 enforceForUri(
2077 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
2078 false, uid, uri, message);
2079 }
2080
Jeff Brown6e539312015-02-24 18:53:21 -08002081 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 public void enforceCallingUriPermission(
2083 Uri uri, int modeFlags, String message) {
2084 enforceForUri(
2085 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07002086 false,
2087 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089
Jeff Brown6e539312015-02-24 18:53:21 -08002090 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 public void enforceCallingOrSelfUriPermission(
2092 Uri uri, int modeFlags, String message) {
2093 enforceForUri(
2094 modeFlags,
2095 checkCallingOrSelfUriPermission(uri, modeFlags), true,
2096 Binder.getCallingUid(), uri, message);
2097 }
2098
Jeff Brown6e539312015-02-24 18:53:21 -08002099 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 public void enforceUriPermission(
2101 Uri uri, String readPermission, String writePermission,
2102 int pid, int uid, int modeFlags, String message) {
2103 enforceForUri(modeFlags,
2104 checkUriPermission(
2105 uri, readPermission, writePermission, pid, uid,
2106 modeFlags),
2107 false,
2108 uid,
2109 uri,
2110 message);
2111 }
2112
Tom O'Neill365632e2013-09-09 09:34:58 -07002113 /**
2114 * Logs a warning if the system process directly called a method such as
2115 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
2116 * The "AsUser" variants allow us to properly enforce the user's restrictions.
2117 */
Amith Yamasanicd757062012-10-19 18:23:52 -07002118 private void warnIfCallingFromSystemProcess() {
2119 if (Process.myUid() == Process.SYSTEM_UID) {
2120 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08002121 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07002122 }
2123 }
2124
Todd Kennedy233a0b12018-01-29 20:30:24 +00002125 private static Resources createResources(IBinder activityToken, LoadedApk pi, String splitName,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002126 int displayId, Configuration overrideConfig, CompatibilityInfo compatInfo,
2127 String[] overlayDirs) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002128 final String[] splitResDirs;
2129 final ClassLoader classLoader;
2130 try {
2131 splitResDirs = pi.getSplitPaths(splitName);
2132 classLoader = pi.getSplitClassLoader(splitName);
2133 } catch (NameNotFoundException e) {
2134 throw new RuntimeException(e);
2135 }
2136 return ResourcesManager.getInstance().getResources(activityToken,
2137 pi.getResDir(),
2138 splitResDirs,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002139 overlayDirs,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002140 pi.getApplicationInfo().sharedLibraryFiles,
2141 displayId,
2142 overrideConfig,
2143 compatInfo,
2144 classLoader);
2145 }
2146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07002148 public Context createApplicationContext(ApplicationInfo application, int flags)
2149 throws NameNotFoundException {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002150 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
Svetoslav976e8bd2014-07-16 15:12:03 -07002151 flags | CONTEXT_REGISTER_PACKAGE);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002152 if (pi != null) {
2153 ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken,
Makoto Onuki4becc352019-01-30 13:30:41 -08002154 new UserHandle(UserHandle.getUserId(application.uid)), flags, null, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002155
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002156 final int displayId = getDisplayId();
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002157 // overlayDirs is retrieved directly from ApplicationInfo since ActivityThread may have
2158 // a LoadedApk containing Resources with stale overlays for a remote application.
2159 final String[] overlayDirs = application.resourceDirs;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002160 c.setResources(createResources(mActivityToken, pi, null, displayId, null,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002161 getDisplayAdjustments(displayId).getCompatibilityInfo(), overlayDirs));
Svetoslav976e8bd2014-07-16 15:12:03 -07002162 if (c.mResources != null) {
2163 return c;
2164 }
2165 }
2166
2167 throw new PackageManager.NameNotFoundException(
2168 "Application package " + application.packageName + " not found");
2169 }
2170
2171 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07002173 throws NameNotFoundException {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002174 return createPackageContextAsUser(packageName, flags, mUser);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002175 }
2176
2177 @Override
2178 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
2179 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00002180 if (packageName.equals("system") || packageName.equals("android")) {
Adam Lesinski8fa71072016-11-18 18:13:55 -08002181 // The system resources are loaded in every application, so we can safely copy
2182 // the context without reloading Resources.
Todd Kennedy233a0b12018-01-29 20:30:24 +00002183 return new ContextImpl(this, mMainThread, mPackageInfo, null, mActivityToken, user,
Makoto Onuki4becc352019-01-30 13:30:41 -08002184 flags, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 }
2186
Todd Kennedy233a0b12018-01-29 20:30:24 +00002187 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002188 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
Todd Kennedy233a0b12018-01-29 20:30:24 +00002189 if (pi != null) {
2190 ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken, user,
Makoto Onuki4becc352019-01-30 13:30:41 -08002191 flags, null, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002192
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002193 final int displayId = getDisplayId();
Adam Lesinski8fa71072016-11-18 18:13:55 -08002194
Todd Kennedy233a0b12018-01-29 20:30:24 +00002195 c.setResources(createResources(mActivityToken, pi, null, displayId, null,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002196 getDisplayAdjustments(displayId).getCompatibilityInfo(), pi.getOverlayDirs()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 if (c.mResources != null) {
2198 return c;
2199 }
2200 }
2201
2202 // Should be a better exception.
2203 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07002204 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 }
2206
Romain Guy870e09f2009-07-06 16:35:25 -07002207 @Override
Adam Lesinski4e862812016-11-21 16:02:24 -08002208 public Context createContextForSplit(String splitName) throws NameNotFoundException {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002209 if (!mPackageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
Adam Lesinski4e862812016-11-21 16:02:24 -08002210 // All Splits are always loaded.
2211 return this;
2212 }
2213
Todd Kennedy233a0b12018-01-29 20:30:24 +00002214 final ClassLoader classLoader = mPackageInfo.getSplitClassLoader(splitName);
2215 final String[] paths = mPackageInfo.getSplitPaths(splitName);
Adam Lesinski4e862812016-11-21 16:02:24 -08002216
Todd Kennedy233a0b12018-01-29 20:30:24 +00002217 final ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, splitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002218 mActivityToken, mUser, mFlags, classLoader, null);
Adam Lesinski4e862812016-11-21 16:02:24 -08002219
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002220 final int displayId = getDisplayId();
Adam Lesinski4e862812016-11-21 16:02:24 -08002221
Todd Kennedy233a0b12018-01-29 20:30:24 +00002222 context.setResources(ResourcesManager.getInstance().getResources(
2223 mActivityToken,
2224 mPackageInfo.getResDir(),
2225 paths,
2226 mPackageInfo.getOverlayDirs(),
2227 mPackageInfo.getApplicationInfo().sharedLibraryFiles,
2228 displayId,
2229 null,
2230 mPackageInfo.getCompatibilityInfo(),
2231 classLoader));
Adam Lesinski4e862812016-11-21 16:02:24 -08002232 return context;
2233 }
2234
2235 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07002236 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07002237 if (overrideConfiguration == null) {
2238 throw new IllegalArgumentException("overrideConfiguration must not be null");
2239 }
2240
Todd Kennedy233a0b12018-01-29 20:30:24 +00002241 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002242 mActivityToken, mUser, mFlags, mClassLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002243
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002244 final int displayId = getDisplayId();
Todd Kennedy233a0b12018-01-29 20:30:24 +00002245 context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002246 overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo(),
2247 mPackageInfo.getOverlayDirs()));
Adam Lesinski8fa71072016-11-18 18:13:55 -08002248 return context;
Dianne Hackborn756220b2012-08-14 16:45:30 -07002249 }
2250
2251 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07002252 public Context createDisplayContext(Display display) {
2253 if (display == null) {
2254 throw new IllegalArgumentException("display must not be null");
2255 }
2256
Todd Kennedy233a0b12018-01-29 20:30:24 +00002257 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002258 mActivityToken, mUser, mFlags, mClassLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002259
2260 final int displayId = display.getDisplayId();
Todd Kennedy233a0b12018-01-29 20:30:24 +00002261 context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002262 null, getDisplayAdjustments(displayId).getCompatibilityInfo(),
2263 mPackageInfo.getOverlayDirs()));
Adam Lesinski8fa71072016-11-18 18:13:55 -08002264 context.mDisplay = display;
2265 return context;
Jeff Browna492c3a2012-08-23 19:48:44 -07002266 }
2267
Jeff Browna492c3a2012-08-23 19:48:44 -07002268 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002269 public Context createDeviceProtectedStorageContext() {
2270 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
2271 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002272 return new ContextImpl(this, mMainThread, mPackageInfo, mSplitName, mActivityToken, mUser,
Makoto Onuki4becc352019-01-30 13:30:41 -08002273 flags, mClassLoader, null);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002274 }
2275
2276 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002277 public Context createCredentialProtectedStorageContext() {
2278 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
2279 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002280 return new ContextImpl(this, mMainThread, mPackageInfo, mSplitName, mActivityToken, mUser,
Makoto Onuki4becc352019-01-30 13:30:41 -08002281 flags, mClassLoader, null);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002282 }
2283
2284 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07002285 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002286 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
2287 }
2288
2289 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002290 public boolean isDeviceProtectedStorage() {
2291 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002292 }
2293
2294 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002295 public boolean isCredentialProtectedStorage() {
2296 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07002297 }
2298
Jeff Brown98365d72012-08-19 20:30:52 -07002299 @Override
Seigo Nonaka6d6cd682017-06-22 08:22:18 -07002300 public boolean canLoadUnsafeResources() {
2301 if (getPackageName().equals(getOpPackageName())) {
2302 return true;
2303 }
2304 return (mFlags & Context.CONTEXT_IGNORE_SECURITY) != 0;
2305 }
2306
Wale Ogunwale691af682019-02-11 03:09:10 -08002307 @TestApi
Seigo Nonaka6d6cd682017-06-22 08:22:18 -07002308 @Override
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002309 public Display getDisplay() {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002310 if (mDisplay == null) {
2311 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
Bryce Lee609bf652017-02-09 16:50:13 -08002312 mResources);
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002313 }
2314
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002315 return mDisplay;
2316 }
2317
2318 @Override
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002319 public int getDisplayId() {
2320 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
2321 }
2322
2323 @Override
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002324 public void updateDisplay(int displayId) {
Bryce Lee609bf652017-02-09 16:50:13 -08002325 mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002326 }
2327
2328 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07002329 public DisplayAdjustments getDisplayAdjustments(int displayId) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002330 return mResources.getDisplayAdjustments();
Jeff Brown98365d72012-08-19 20:30:52 -07002331 }
2332
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002333 @Override
2334 public File getDataDir() {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002335 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07002336 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002337 if (isCredentialProtectedStorage()) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002338 res = mPackageInfo.getCredentialProtectedDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002339 } else if (isDeviceProtectedStorage()) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002340 res = mPackageInfo.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002341 } else {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002342 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002343 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002344
2345 if (res != null) {
Jeff Sharkey21f50722016-04-27 12:38:02 -06002346 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Jeff Sharkey24492ae2016-04-25 13:20:25 -06002347 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
2348 new Throwable());
2349 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002350 return res;
2351 } else {
2352 throw new RuntimeException(
2353 "No data directory found for package " + getPackageName());
2354 }
2355 } else {
2356 throw new RuntimeException(
2357 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
2360
2361 @Override
2362 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07002363 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002365 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 if (!file.exists()) {
2367 file.mkdir();
2368 setFilePermissionsFromMode(file.getPath(), mode,
2369 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
2370 }
2371 return file;
2372 }
2373
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002374 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08002375 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002376 public UserHandle getUser() {
2377 return mUser;
2378 }
2379
2380 /** {@hide} */
2381 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002382 public int getUserId() {
2383 return mUser.getIdentifier();
2384 }
2385
Felipe Lemebb567ae2017-10-04 09:56:21 -07002386 /** @hide */
2387 @Override
2388 public AutofillClient getAutofillClient() {
2389 return mAutofillClient;
2390 }
2391
2392 /** @hide */
2393 @Override
2394 public void setAutofillClient(AutofillClient client) {
2395 mAutofillClient = client;
2396 }
2397
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002398 /** @hide */
2399 @Override
Felipe Lemea4f39cd2019-02-19 15:08:59 -08002400 public AutofillOptions getAutofillOptions() {
2401 return mAutofillOptions;
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002402 }
2403
2404 /** @hide */
2405 @Override
Felipe Lemea4f39cd2019-02-19 15:08:59 -08002406 public void setAutofillOptions(AutofillOptions options) {
2407 mAutofillOptions = options;
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002408 }
2409
Felipe Lemeecb08be2018-11-27 15:48:47 -08002410 /** @hide */
2411 @Override
Felipe Leme326f15a2019-02-19 09:42:24 -08002412 public ContentCaptureOptions getContentCaptureOptions() {
2413 return mContentCaptureOptions;
Felipe Lemeecb08be2018-11-27 15:48:47 -08002414 }
2415
2416 /** @hide */
2417 @Override
Felipe Leme326f15a2019-02-19 09:42:24 -08002418 public void setContentCaptureOptions(ContentCaptureOptions options) {
2419 mContentCaptureOptions = options;
Felipe Lemeecb08be2018-11-27 15:48:47 -08002420 }
2421
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002422 @UnsupportedAppUsage
Dianne Hackborn21556372010-02-04 16:34:40 -08002423 static ContextImpl createSystemContext(ActivityThread mainThread) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002424 LoadedApk packageInfo = new LoadedApk(mainThread);
2425 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
Makoto Onuki4becc352019-01-30 13:30:41 -08002426 null, null);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002427 context.setResources(packageInfo.getResources());
Jeff Browndefd4a62014-03-10 21:24:37 -07002428 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002429 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 return context;
2431 }
2432
Adam Lesinskia82b6262017-03-21 16:56:17 -07002433 /**
2434 * System Context to be used for UI. This Context has resources that can be themed.
Adam Lesinski6f1a9172017-04-10 16:35:19 -07002435 * Make sure that the created system UI context shares the same LoadedApk as the system context.
lumark0b05f9e2018-11-26 15:09:06 +08002436 * @param systemContext The system context which created by
2437 * {@link #createSystemContext(ActivityThread)}.
2438 * @param displayId The ID of the display where the UI is shown.
Adam Lesinskia82b6262017-03-21 16:56:17 -07002439 */
lumark0b05f9e2018-11-26 15:09:06 +08002440 static ContextImpl createSystemUiContext(ContextImpl systemContext, int displayId) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002441 final LoadedApk packageInfo = systemContext.mPackageInfo;
2442 ContextImpl context = new ContextImpl(null, systemContext.mMainThread, packageInfo, null,
Makoto Onuki4becc352019-01-30 13:30:41 -08002443 null, null, 0, null, null);
lumark0b05f9e2018-11-26 15:09:06 +08002444 context.setResources(createResources(null, packageInfo, null, displayId, null,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002445 packageInfo.getCompatibilityInfo(), packageInfo.getOverlayDirs()));
lumark0b05f9e2018-11-26 15:09:06 +08002446 context.updateDisplay(displayId);
Adam Lesinskia82b6262017-03-21 16:56:17 -07002447 return context;
2448 }
2449
lumark0b05f9e2018-11-26 15:09:06 +08002450 /**
2451 * The overloaded method of {@link #createSystemUiContext(ContextImpl, int)}.
2452 * Uses {@Code Display.DEFAULT_DISPLAY} as the target display.
2453 */
2454 static ContextImpl createSystemUiContext(ContextImpl systemContext) {
2455 return createSystemUiContext(systemContext, Display.DEFAULT_DISPLAY);
2456 }
2457
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002458 @UnsupportedAppUsage
Todd Kennedy233a0b12018-01-29 20:30:24 +00002459 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
Makoto Onuki4becc352019-01-30 13:30:41 -08002460 return createAppContext(mainThread, packageInfo, null);
2461 }
2462
2463 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo,
2464 String opPackageName) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002465 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
2466 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
Makoto Onuki4becc352019-01-30 13:30:41 -08002467 null, opPackageName);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002468 context.setResources(packageInfo.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002469 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002470 }
2471
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002472 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -07002473 static ContextImpl createActivityContext(ActivityThread mainThread,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002474 LoadedApk packageInfo, ActivityInfo activityInfo, IBinder activityToken, int displayId,
Adam Lesinski082614c2016-03-04 14:33:47 -08002475 Configuration overrideConfiguration) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002476 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski8fa71072016-11-18 18:13:55 -08002477
Todd Kennedy233a0b12018-01-29 20:30:24 +00002478 String[] splitDirs = packageInfo.getSplitResDirs();
2479 ClassLoader classLoader = packageInfo.getClassLoader();
Adam Lesinski4e862812016-11-21 16:02:24 -08002480
Todd Kennedy233a0b12018-01-29 20:30:24 +00002481 if (packageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
Adam Lesinski4e862812016-11-21 16:02:24 -08002482 Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "SplitDependencies");
2483 try {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002484 classLoader = packageInfo.getSplitClassLoader(activityInfo.splitName);
2485 splitDirs = packageInfo.getSplitPaths(activityInfo.splitName);
Adam Lesinski4e862812016-11-21 16:02:24 -08002486 } catch (NameNotFoundException e) {
2487 // Nothing above us can handle a NameNotFoundException, better crash.
2488 throw new RuntimeException(e);
2489 } finally {
2490 Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2491 }
2492 }
2493
Todd Kennedy233a0b12018-01-29 20:30:24 +00002494 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, activityInfo.splitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002495 activityToken, null, 0, classLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002496
2497 // Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
2498 displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
2499
2500 final CompatibilityInfo compatInfo = (displayId == Display.DEFAULT_DISPLAY)
Todd Kennedy233a0b12018-01-29 20:30:24 +00002501 ? packageInfo.getCompatibilityInfo()
Adam Lesinski8fa71072016-11-18 18:13:55 -08002502 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
2503
Adam Lesinskic9701942016-11-28 19:44:19 -08002504 final ResourcesManager resourcesManager = ResourcesManager.getInstance();
2505
2506 // Create the base resources for which all configuration contexts for this Activity
2507 // will be rebased upon.
Jason Monkbd983642017-02-24 16:57:40 -05002508 context.setResources(resourcesManager.createBaseActivityResources(activityToken,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002509 packageInfo.getResDir(),
Adam Lesinski4e862812016-11-21 16:02:24 -08002510 splitDirs,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002511 packageInfo.getOverlayDirs(),
2512 packageInfo.getApplicationInfo().sharedLibraryFiles,
Adam Lesinskic9701942016-11-28 19:44:19 -08002513 displayId,
2514 overrideConfiguration,
2515 compatInfo,
Jason Monkbd983642017-02-24 16:57:40 -05002516 classLoader));
Adam Lesinskic9701942016-11-28 19:44:19 -08002517 context.mDisplay = resourcesManager.getAdjustedDisplay(displayId,
Bryce Lee609bf652017-02-09 16:50:13 -08002518 context.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002519 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002520 }
2521
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002522 private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002523 @NonNull LoadedApk packageInfo, @Nullable String splitName,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002524 @Nullable IBinder activityToken, @Nullable UserHandle user, int flags,
Makoto Onuki4becc352019-01-30 13:30:41 -08002525 @Nullable ClassLoader classLoader, @Nullable String overrideOpPackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002528 // If creator didn't specify which storage to use, use the default
2529 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002530 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2531 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002532 final File dataDir = packageInfo.getDataDirFile();
2533 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002534 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002535 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002536 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002537 }
2538 }
2539
Jeff Browndefd4a62014-03-10 21:24:37 -07002540 mMainThread = mainThread;
2541 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002542 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543
Jeff Browndefd4a62014-03-10 21:24:37 -07002544 if (user == null) {
2545 user = Process.myUserHandle();
2546 }
2547 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002548
Todd Kennedy233a0b12018-01-29 20:30:24 +00002549 mPackageInfo = packageInfo;
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002550 mSplitName = splitName;
Adam Lesinski4e862812016-11-21 16:02:24 -08002551 mClassLoader = classLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -07002552 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002553
Makoto Onuki4becc352019-01-30 13:30:41 -08002554 String opPackageName;
2555
Jeff Browndefd4a62014-03-10 21:24:37 -07002556 if (container != null) {
2557 mBasePackageName = container.mBasePackageName;
Makoto Onuki4becc352019-01-30 13:30:41 -08002558 opPackageName = container.mOpPackageName;
Jason Monkbd983642017-02-24 16:57:40 -05002559 setResources(container.mResources);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002560 mDisplay = container.mDisplay;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002561 } else {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002562 mBasePackageName = packageInfo.mPackageName;
2563 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
Dianne Hackborn95d78532013-09-11 09:51:14 -07002564 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2565 // Special case: system components allow themselves to be loaded in to other
2566 // processes. For purposes of app ops, we must then consider the context as
2567 // belonging to the package of this process, not the system itself, otherwise
2568 // the package+uid verifications in app ops will fail.
Makoto Onuki4becc352019-01-30 13:30:41 -08002569 opPackageName = ActivityThread.currentPackageName();
Dianne Hackborn95d78532013-09-11 09:51:14 -07002570 } else {
Makoto Onuki4becc352019-01-30 13:30:41 -08002571 opPackageName = mBasePackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002572 }
2573 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002574
Makoto Onuki4becc352019-01-30 13:30:41 -08002575 mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName;
2576
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002577 mContentResolver = new ApplicationContentResolver(this, mainThread);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579
Jason Monkbd983642017-02-24 16:57:40 -05002580 void setResources(Resources r) {
Jason Monkbd60e5b2017-03-02 12:55:00 -05002581 if (r instanceof CompatResources) {
2582 ((CompatResources) r).setContext(this);
Jason Monkbd983642017-02-24 16:57:40 -05002583 }
Jason Monkbd60e5b2017-03-02 12:55:00 -05002584 mResources = r;
Jason Monkbd983642017-02-24 16:57:40 -05002585 }
2586
Narayan Kamath29564cd2014-08-07 10:57:40 +01002587 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002588 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 }
2590
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002591 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 final void scheduleFinalCleanup(String who, String what) {
2593 mMainThread.scheduleContextCleanup(this, who, what);
2594 }
2595
2596 final void performFinalCleanup(String who, String what) {
2597 //Log.i(TAG, "Cleanup up context: " + this);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002598 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 }
2600
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002601 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 final Context getReceiverRestrictedContext() {
2603 if (mReceiverRestrictedContext != null) {
2604 return mReceiverRestrictedContext;
2605 }
2606 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2607 }
2608
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002609 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 final void setOuterContext(Context context) {
2611 mOuterContext = context;
2612 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002613
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002614 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 final Context getOuterContext() {
2616 return mOuterContext;
2617 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002618
Tony Mak46aabe52016-11-14 12:53:06 +00002619 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002620 @UnsupportedAppUsage
Tony Mak46aabe52016-11-14 12:53:06 +00002621 public IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 return mActivityToken;
2623 }
2624
Jeff Sharkey634dc422016-01-30 17:44:15 -07002625 private void checkMode(int mode) {
2626 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2627 if ((mode & MODE_WORLD_READABLE) != 0) {
2628 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2629 }
2630 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2631 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2632 }
2633 }
2634 }
2635
Jeff Brown6e539312015-02-24 18:53:21 -08002636 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002637 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 int extraPermissions) {
2639 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2640 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2641 |extraPermissions;
2642 if ((mode&MODE_WORLD_READABLE) != 0) {
2643 perms |= FileUtils.S_IROTH;
2644 }
2645 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2646 perms |= FileUtils.S_IWOTH;
2647 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002648 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2650 + ", perms=0x" + Integer.toHexString(perms));
2651 }
2652 FileUtils.setPermissions(name, perms, -1, -1);
2653 }
2654
2655 private File makeFilename(File base, String name) {
2656 if (name.indexOf(File.separatorChar) < 0) {
Jeff Sharkeydd02e332018-06-27 14:41:57 -06002657 final File res = new File(base, name);
2658 // We report as filesystem access here to give us the best shot at
2659 // detecting apps that will pass the path down to native code.
2660 BlockGuard.getVmPolicy().onPathAccess(res.getPath());
2661 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 }
2663 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002664 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
2666
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002667 /**
2668 * Ensure that given directories exist, trying to create them if missing. If
2669 * unable to create, they are filtered by replacing with {@code null}.
2670 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002671 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkeyae266462017-11-27 13:32:24 -07002672 final StorageManager sm = getSystemService(StorageManager.class);
2673 final File[] result = new File[dirs.length];
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002674 for (int i = 0; i < dirs.length; i++) {
2675 File dir = dirs[i];
2676 if (!dir.exists()) {
2677 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002678 // recheck existence in case of cross-process race
2679 if (!dir.exists()) {
2680 // Failing to mkdir() may be okay, since we might not have
2681 // enough permissions; ask vold to create on our behalf.
Christopher Tatecc866da2013-10-02 18:11:01 -07002682 try {
Jeff Sharkeyae266462017-11-27 13:32:24 -07002683 sm.mkdirs(dir);
Jeff Sharkey983294592015-07-13 10:25:31 -07002684 } catch (Exception e) {
2685 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002686 dir = null;
2687 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002688 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002689 }
2690 }
2691 result[i] = dir;
2692 }
2693 return result;
2694 }
2695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 // ----------------------------------------------------------------------
2697 // ----------------------------------------------------------------------
2698 // ----------------------------------------------------------------------
2699
2700 private static final class ApplicationContentResolver extends ContentResolver {
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002701 @UnsupportedAppUsage
Jeff Sharkey6d515712012-09-20 16:06:08 -07002702 private final ActivityThread mMainThread;
Jeff Sharkey6d515712012-09-20 16:06:08 -07002703
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002704 public ApplicationContentResolver(Context context, ActivityThread mainThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002706 mMainThread = Preconditions.checkNotNull(mainThread);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
2708
2709 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002710 @UnsupportedAppUsage
Jeff Sharkey6d515712012-09-20 16:06:08 -07002711 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002712 return mMainThread.acquireProvider(context,
2713 ContentProvider.getAuthorityWithoutUserId(auth),
2714 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 }
2716
2717 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002718 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002719 return mMainThread.acquireExistingProvider(context,
2720 ContentProvider.getAuthorityWithoutUserId(auth),
2721 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002722 }
2723
2724 @Override
2725 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002726 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002728
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002729 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002730 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002731 return mMainThread.acquireProvider(c,
2732 ContentProvider.getAuthorityWithoutUserId(auth),
2733 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002734 }
2735
2736 @Override
2737 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002738 return mMainThread.releaseProvider(icp, false);
2739 }
2740
2741 @Override
2742 public void unstableProviderDied(IContentProvider icp) {
2743 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002744 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002745
2746 @Override
2747 public void appNotRespondingViaProvider(IContentProvider icp) {
2748 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2749 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002750
2751 /** @hide */
2752 protected int resolveUserIdFromAuthority(String auth) {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002753 return ContentProvider.getUserIdFromAuthority(auth, getUserId());
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756}