blob: b607f9adebbe1c35429dceec2b6b79458bd1abfb [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 {
105 ReceiverRestrictedContext(Context base) {
106 super(base);
107 }
108
109 @Override
110 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
111 return registerReceiver(receiver, filter, null, null);
112 }
113
114 @Override
115 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
116 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700117 if (receiver == null) {
118 // Allow retrieving current sticky broadcast; this is safe since we
119 // aren't actually registering a receiver.
120 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
121 } else {
122 throw new ReceiverCallNotAllowedException(
123 "BroadcastReceiver components are not allowed to register to receive intents");
124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
126
127 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700128 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
129 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700130 if (receiver == null) {
131 // Allow retrieving current sticky broadcast; this is safe since we
132 // aren't actually registering a receiver.
133 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
134 } else {
135 throw new ReceiverCallNotAllowedException(
136 "BroadcastReceiver components are not allowed to register to receive intents");
137 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700138 }
139
140 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
142 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700143 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 }
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800145
146 @Override
147 public boolean bindIsolatedService(Intent service, ServiceConnection conn, int flags,
148 String instanceName) {
149 throw new ReceiverCallNotAllowedException(
150 "BroadcastReceiver components are not allowed to bind to services");
151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152}
153
154/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800155 * Common implementation of Context API, which provides the base
156 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800158class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800159 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700160 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600162 private static final String XATTR_INODE_CACHE = "user.inode_cache";
163 private static final String XATTR_INODE_CODE_CACHE = "user.inode_code_cache";
164
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700165 /**
166 * Map from package name, to preference name, to cached preferences.
167 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700168 @GuardedBy("ContextImpl.class")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100169 @UnsupportedAppUsage
Jeff Sharkeybe782582016-02-15 18:35:57 -0700170 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
171
172 /**
173 * Map from preference name to generated path.
174 */
175 @GuardedBy("ContextImpl.class")
Mathew Inwood8c854f82018-09-14 12:35:36 +0100176 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkeybe782582016-02-15 18:35:57 -0700177 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100179 @UnsupportedAppUsage
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800180 final @NonNull ActivityThread mMainThread;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100181 @UnsupportedAppUsage
Todd Kennedy233a0b12018-01-29 20:30:24 +0000182 final @NonNull LoadedApk mPackageInfo;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100183 @UnsupportedAppUsage
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800184 private @Nullable ClassLoader mClassLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -0700185
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800186 private final @Nullable IBinder mActivityToken;
Jeff Browndefd4a62014-03-10 21:24:37 -0700187
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700188 private final @NonNull UserHandle mUser;
Jeff Browndefd4a62014-03-10 21:24:37 -0700189
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100190 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -0700191 private final ApplicationContentResolver mContentResolver;
192
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100193 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -0700194 private final String mBasePackageName;
Mathew Inwood8c854f82018-09-14 12:35:36 +0100195 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Browndefd4a62014-03-10 21:24:37 -0700196 private final String mOpPackageName;
197
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700198 private final @NonNull ResourcesManager mResourcesManager;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100199 @UnsupportedAppUsage
Adam Lesinski8fa71072016-11-18 18:13:55 -0800200 private @NonNull Resources mResources;
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700201 private @Nullable Display mDisplay; // may be null if default display
Jeff Browndefd4a62014-03-10 21:24:37 -0700202
Mathew Inwood8c854f82018-09-14 12:35:36 +0100203 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700204 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700205
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100206 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 private Context mOuterContext;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100208 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 private int mThemeResource = 0;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100210 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 private Resources.Theme mTheme = null;
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100212 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
Adam Lesinski1665d0f2017-03-10 14:46:57 -0800216 // The name of the split this Context is representing. May be null.
217 private @Nullable String mSplitName = null;
218
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800219 private @Nullable AutofillClient mAutofillClient = null;
220 private @Nullable AutofillOptions mAutofillOptions;
Felipe Lemebb567ae2017-10-04 09:56:21 -0700221
Felipe Leme326f15a2019-02-19 09:42:24 -0800222 private ContentCaptureOptions mContentCaptureOptions = null;
Felipe Lemeecb08be2018-11-27 15:48:47 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 private final Object mSync = new Object();
225
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700226 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700228 @GuardedBy("mSync")
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100229 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700231 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700233 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700234 private File mNoBackupFilesDir;
235 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700237 @GuardedBy("mSync")
238 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700239
Jeff Brown6e539312015-02-24 18:53:21 -0800240 // The system service cache for the system services that are cached per-ContextImpl.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100241 @UnsupportedAppUsage
Jeff Brown6e539312015-02-24 18:53:21 -0800242 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800243
Makoto Onukif896f122018-01-25 09:50:24 -0800244 static final int STATE_UNINITIALIZED = 0;
245 static final int STATE_INITIALIZING = 1;
246 static final int STATE_READY = 2;
Makoto Onukid67070e2018-03-30 12:39:14 -0700247 static final int STATE_NOT_FOUND = 3;
248
249 /** @hide */
250 @IntDef(prefix = { "STATE_" }, value = {
251 STATE_UNINITIALIZED,
252 STATE_INITIALIZING,
253 STATE_READY,
254 STATE_NOT_FOUND,
255 })
256 @Retention(RetentionPolicy.SOURCE)
257 @interface ServiceInitializationState {}
Makoto Onukif896f122018-01-25 09:50:24 -0800258
259 /**
260 * Initialization state for each service. Any of {@link #STATE_UNINITIALIZED},
261 * {@link #STATE_INITIALIZING} or {@link #STATE_READY},
262 */
Makoto Onukid67070e2018-03-30 12:39:14 -0700263 @ServiceInitializationState
264 final int[] mServiceInitializationStateArray = new int[mServiceCache.length];
Makoto Onukif896f122018-01-25 09:50:24 -0800265
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100266 @UnsupportedAppUsage
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700267 static ContextImpl getImpl(Context context) {
268 Context nextContext;
269 while ((context instanceof ContextWrapper) &&
270 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
271 context = nextContext;
272 }
273 return (ContextImpl)context;
274 }
275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 @Override
277 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700278 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 }
280
281 @Override
282 public Resources getResources() {
283 return mResources;
284 }
285
286 @Override
287 public PackageManager getPackageManager() {
288 if (mPackageManager != null) {
289 return mPackageManager;
290 }
291
292 IPackageManager pm = ActivityThread.getPackageManager();
293 if (pm != null) {
294 // Doesn't matter if we make more than one instance.
295 return (mPackageManager = new ApplicationPackageManager(this, pm));
296 }
297
298 return null;
299 }
300
301 @Override
302 public ContentResolver getContentResolver() {
303 return mContentResolver;
304 }
305
306 @Override
307 public Looper getMainLooper() {
308 return mMainThread.getLooper();
309 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 @Override
Jeff Sharkey8439ac02017-12-12 17:26:23 -0700312 public Executor getMainExecutor() {
313 return mMainThread.getExecutor();
314 }
315
316 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 public Context getApplicationContext() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000318 return (mPackageInfo != null) ?
319 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700323 public void setTheme(int resId) {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700324 synchronized (mSync) {
325 if (mThemeResource != resId) {
326 mThemeResource = resId;
327 initializeTheme();
328 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800333 public int getThemeResId() {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700334 synchronized (mSync) {
335 return mThemeResource;
336 }
Dianne Hackborn247fe742011-01-08 17:25:57 -0800337 }
338
339 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 public Resources.Theme getTheme() {
Adam Lesinski1e87a362017-06-12 12:22:10 -0700341 synchronized (mSync) {
342 if (mTheme != null) {
343 return mTheme;
344 }
345
346 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
347 getOuterContext().getApplicationInfo().targetSdkVersion);
348 initializeTheme();
349
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700350 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700354 private void initializeTheme() {
355 if (mTheme == null) {
356 mTheme = mResources.newTheme();
357 }
358 mTheme.applyStyle(mThemeResource, true);
359 }
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 @Override
362 public ClassLoader getClassLoader() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000363 return mClassLoader != null ? mClassLoader : (mPackageInfo != null ? mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
365
366 @Override
367 public String getPackageName() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000368 if (mPackageInfo != null) {
369 return mPackageInfo.getPackageName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
Todd Kennedy233a0b12018-01-29 20:30:24 +0000371 // No mPackageInfo means this is a Context for the system itself,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800372 // and this here is its name.
373 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800376 /** @hide */
377 @Override
378 public String getBasePackageName() {
379 return mBasePackageName != null ? mBasePackageName : getPackageName();
380 }
381
Dianne Hackborn95d78532013-09-11 09:51:14 -0700382 /** @hide */
383 @Override
384 public String getOpPackageName() {
385 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
386 }
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700389 public ApplicationInfo getApplicationInfo() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000390 if (mPackageInfo != null) {
391 return mPackageInfo.getApplicationInfo();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700392 }
393 throw new RuntimeException("Not supported in system context");
394 }
395
396 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 public String getPackageResourcePath() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000398 if (mPackageInfo != null) {
399 return mPackageInfo.getResDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
401 throw new RuntimeException("Not supported in system context");
402 }
403
404 @Override
405 public String getPackageCodePath() {
Todd Kennedy233a0b12018-01-29 20:30:24 +0000406 if (mPackageInfo != null) {
407 return mPackageInfo.getAppDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 throw new RuntimeException("Not supported in system context");
410 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200411
Jeff Brown6e539312015-02-24 18:53:21 -0800412 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700414 // At least one application in the world actually passes in a null
415 // name. This happened to work because when we generated the file name
416 // we would stringify it to "null.xml". Nice.
Todd Kennedy233a0b12018-01-29 20:30:24 +0000417 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700418 Build.VERSION_CODES.KITKAT) {
419 if (name == null) {
420 name = "null";
421 }
422 }
423
Jeff Sharkeybe782582016-02-15 18:35:57 -0700424 File file;
425 synchronized (ContextImpl.class) {
426 if (mSharedPrefsPaths == null) {
427 mSharedPrefsPaths = new ArrayMap<>();
428 }
429 file = mSharedPrefsPaths.get(name);
430 if (file == null) {
431 file = getSharedPreferencesPath(name);
432 mSharedPrefsPaths.put(name, file);
433 }
434 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700435 return getSharedPreferences(file, mode);
436 }
437
438 @Override
439 public SharedPreferences getSharedPreferences(File file, int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700441 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700442 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
443 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700444 if (sp == null) {
John Reckf3903e92017-05-12 17:12:54 -0700445 checkMode(mode);
446 if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.O) {
447 if (isCredentialProtectedStorage()
Jeff Sharkeya65e6492017-06-21 13:45:11 -0600448 && !getSystemService(UserManager.class)
449 .isUserUnlockingOrUnlocked(UserHandle.myUserId())) {
John Reckf3903e92017-05-12 17:12:54 -0700450 throw new IllegalStateException("SharedPreferences in credential encrypted "
451 + "storage are not available until after user is unlocked");
452 }
453 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700454 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700455 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700456 return sp;
457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800459 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
460 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
461 // If somebody else (some other process) changed the prefs
462 // file behind our back, we reload it. This has been the
463 // historical (if undocumented) behavior.
464 sp.startReloadIfChangedUnexpectedly();
465 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700466 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
468
Andreas Gampe3f24e692018-02-05 13:24:28 -0800469 @GuardedBy("ContextImpl.class")
Jeff Sharkey35871f22016-01-29 17:13:29 -0700470 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700471 if (sSharedPrefsCache == null) {
472 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700473 }
474
475 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700476 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700477 if (packagePrefs == null) {
478 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700479 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700480 }
481
482 return packagePrefs;
483 }
484
Christopher Tatefe2368c2017-05-17 15:42:35 -0700485 @Override
486 public void reloadSharedPreferences() {
487 // Build the list of all per-context impls (i.e. caches) we know about
488 ArrayList<SharedPreferencesImpl> spImpls = new ArrayList<>();
489 synchronized (ContextImpl.class) {
490 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
491 for (int i = 0; i < cache.size(); i++) {
492 final SharedPreferencesImpl sp = cache.valueAt(i);
493 if (sp != null) {
494 spImpls.add(sp);
495 }
496 }
497 }
498
499 // Issue the reload outside the cache lock
500 for (int i = 0; i < spImpls.size(); i++) {
501 spImpls.get(i).startReloadIfChangedUnexpectedly();
502 }
503 }
504
Jeff Sharkey35871f22016-01-29 17:13:29 -0700505 /**
506 * Try our best to migrate all files from source to target that match
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700507 * requested prefix.
508 *
509 * @return the number of files moved, or -1 if there was trouble.
Jeff Sharkey35871f22016-01-29 17:13:29 -0700510 */
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600511 private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700512 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
513 @Override
514 public boolean accept(File dir, String name) {
515 return name.startsWith(prefix);
516 }
517 });
518
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700519 int res = 0;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700520 for (File sourceFile : sourceFiles) {
521 final File targetFile = new File(targetDir, sourceFile.getName());
522 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
523 try {
524 FileUtils.copyFileOrThrow(sourceFile, targetFile);
525 FileUtils.copyPermissions(sourceFile, targetFile);
526 if (!sourceFile.delete()) {
527 throw new IOException("Failed to clean up " + sourceFile);
528 }
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700529 if (res != -1) {
530 res++;
531 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700532 } catch (IOException e) {
533 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700534 res = -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700535 }
536 }
537 return res;
538 }
539
540 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600541 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700542 synchronized (ContextImpl.class) {
543 final File source = sourceContext.getSharedPreferencesPath(name);
544 final File target = getSharedPreferencesPath(name);
545
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600546 final int res = moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700547 source.getName());
548 if (res > 0) {
549 // We moved at least one file, so evict any in-memory caches for
550 // either location
551 final ArrayMap<File, SharedPreferencesImpl> cache =
552 getSharedPreferencesCacheLocked();
553 cache.remove(source);
554 cache.remove(target);
555 }
556 return res != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700557 }
558 }
559
560 @Override
561 public boolean deleteSharedPreferences(String name) {
562 synchronized (ContextImpl.class) {
563 final File prefs = getSharedPreferencesPath(name);
564 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
565
566 // Evict any in-memory caches
567 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
568 cache.remove(prefs);
569
570 prefs.delete();
571 prefsBackup.delete();
572
573 // We failed if files are still lingering
574 return !(prefs.exists() || prefsBackup.exists());
575 }
576 }
577
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100578 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 private File getPreferencesDir() {
580 synchronized (mSync) {
581 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700582 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700584 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586 }
587
588 @Override
589 public FileInputStream openFileInput(String name)
590 throws FileNotFoundException {
591 File f = makeFilename(getFilesDir(), name);
592 return new FileInputStream(f);
593 }
594
595 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700596 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
597 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 final boolean append = (mode&MODE_APPEND) != 0;
599 File f = makeFilename(getFilesDir(), name);
600 try {
601 FileOutputStream fos = new FileOutputStream(f, append);
602 setFilePermissionsFromMode(f.getPath(), mode, 0);
603 return fos;
604 } catch (FileNotFoundException e) {
605 }
606
607 File parent = f.getParentFile();
608 parent.mkdir();
609 FileUtils.setPermissions(
610 parent.getPath(),
611 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
612 -1, -1);
613 FileOutputStream fos = new FileOutputStream(f, append);
614 setFilePermissionsFromMode(f.getPath(), mode, 0);
615 return fos;
616 }
617
618 @Override
619 public boolean deleteFile(String name) {
620 File f = makeFilename(getFilesDir(), name);
621 return f.delete();
622 }
623
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700624 /**
625 * Common-path handling of app data dir creation
626 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700627 private static File ensurePrivateDirExists(File file) {
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600628 return ensurePrivateDirExists(file, 0771, -1, null);
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700629 }
630
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600631 private static File ensurePrivateCacheDirExists(File file, String xattr) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700632 final int gid = UserHandle.getCacheAppGid(Process.myUid());
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600633 return ensurePrivateDirExists(file, 02771, gid, xattr);
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700634 }
635
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600636 private static File ensurePrivateDirExists(File file, int mode, int gid, String xattr) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700637 if (!file.exists()) {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700638 final String path = file.getAbsolutePath();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700639 try {
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700640 Os.mkdir(path, mode);
641 Os.chmod(path, mode);
642 if (gid != -1) {
643 Os.chown(path, -1, gid);
644 }
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700645 } catch (ErrnoException e) {
646 if (e.errno == OsConstants.EEXIST) {
647 // We must have raced with someone; that's okay
648 } else {
649 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700650 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700651 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600652
653 if (xattr != null) {
654 try {
655 final StructStat stat = Os.stat(file.getAbsolutePath());
656 final byte[] value = new byte[8];
657 Memory.pokeLong(value, 0, stat.st_ino, ByteOrder.nativeOrder());
658 Os.setxattr(file.getParentFile().getAbsolutePath(), xattr, value, 0);
659 } catch (ErrnoException e) {
660 Log.w(TAG, "Failed to update " + xattr + ": " + e.getMessage());
661 }
662 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700663 }
664 return file;
665 }
666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 @Override
668 public File getFilesDir() {
669 synchronized (mSync) {
670 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700671 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700673 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700674 }
675 }
676
677 @Override
678 public File getNoBackupFilesDir() {
679 synchronized (mSync) {
680 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700681 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700683 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800688 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700689 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700690 final File[] dirs = getExternalFilesDirs(type);
691 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700692 }
693
694 @Override
695 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800696 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600697 File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700698 if (type != null) {
699 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800700 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700701 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800702 }
703 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200704
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800705 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800706 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700707 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700708 final File[] dirs = getObbDirs();
709 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700710 }
711
712 @Override
713 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800714 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600715 File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
716 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800717 }
718 }
719
720 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 public File getCacheDir() {
722 synchronized (mSync) {
723 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700724 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600726 return ensurePrivateCacheDirExists(mCacheDir, XATTR_INODE_CACHE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200729
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800730 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700731 public File getCodeCacheDir() {
732 synchronized (mSync) {
733 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700734 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700735 }
Jeff Sharkey83aacde2017-03-22 23:13:39 -0600736 return ensurePrivateCacheDirExists(mCodeCacheDir, XATTR_INODE_CODE_CACHE);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700737 }
738 }
739
740 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800741 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700742 // Operates on primary external storage
Christopher Tate11179b42017-07-13 15:18:38 -0700743 final File[] dirs = getExternalCacheDirs();
744 return (dirs != null && dirs.length > 0) ? dirs[0] : null;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700745 }
746
747 @Override
748 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800749 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600750 File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
751 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800752 }
753 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700756 public File[] getExternalMediaDirs() {
757 synchronized (mSync) {
Jeff Sharkey4a2b1192016-06-27 17:43:15 -0600758 File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
759 return ensureExternalDirsExistOrFilter(dirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700760 }
761 }
762
Fyodor Kupolov61221292016-09-02 15:21:03 -0700763 /**
764 * @hide
765 */
766 @Nullable
767 @Override
768 public File getPreloadsFileCache() {
769 return Environment.getDataPreloadsFileCacheDirectory(getPackageName());
770 }
771
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700772 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 public File getFileStreamPath(String name) {
774 return makeFilename(getFilesDir(), name);
775 }
776
777 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700778 public File getSharedPreferencesPath(String name) {
779 return makeFilename(getPreferencesDir(), name + ".xml");
780 }
781
782 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700784 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786
787 @Override
788 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700789 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
791
792 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700793 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
794 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700795 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700796 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700797 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
798 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
799 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
800 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800801 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
802 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
803 }
Jeff Brown47847f32012-03-22 19:13:11 -0700804 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700805 setFilePermissionsFromMode(f.getPath(), mode, 0);
806 return db;
807 }
808
809 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600810 public boolean moveDatabaseFrom(Context sourceContext, String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700811 synchronized (ContextImpl.class) {
812 final File source = sourceContext.getDatabasePath(name);
813 final File target = getDatabasePath(name);
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600814 return moveFiles(source.getParentFile(), target.getParentFile(),
Jeff Sharkey390f2ed2016-03-01 15:25:03 -0700815 source.getName()) != -1;
Jeff Sharkey35871f22016-01-29 17:13:29 -0700816 }
817 }
818
819 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 public boolean deleteDatabase(String name) {
821 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700822 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800823 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 } catch (Exception e) {
825 }
826 return false;
827 }
828
829 @Override
830 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700831 File dir;
832 File f;
833
834 if (name.charAt(0) == File.separatorChar) {
835 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
836 dir = new File(dirPath);
837 name = name.substring(name.lastIndexOf(File.separatorChar));
838 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700839
840 if (!dir.isDirectory() && dir.mkdir()) {
841 FileUtils.setPermissions(dir.getPath(),
842 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
843 -1, -1);
844 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700845 } else {
846 dir = getDatabasesDir();
847 f = makeFilename(dir, name);
848 }
849
Jeff Sharkey35871f22016-01-29 17:13:29 -0700850 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
852
853 @Override
854 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700855 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 private File getDatabasesDir() {
859 synchronized (mSync) {
860 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700861 if ("android".equals(getPackageName())) {
862 mDatabasesDir = new File("/data/system");
863 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700864 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700867 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 }
869 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800872 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700874 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
876
877 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800878 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700879 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700880 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
882
883 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800884 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700886 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888
889 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800890 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700892 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
895 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800896 @Deprecated
897 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700898 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
900
901 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800902 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700904 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906
907 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800908 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700910 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
912
Jeff Brown6e539312015-02-24 18:53:21 -0800913 private WallpaperManager getWallpaperManager() {
914 return getSystemService(WallpaperManager.class);
915 }
916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 @Override
918 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700919 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700920 startActivity(intent, null);
921 }
922
Amith Yamasani82644082012-08-03 13:09:11 -0700923 /** @hide */
924 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700925 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700926 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700927 }
928
Dianne Hackborna4972e92012-03-14 10:38:05 -0700929 @Override
930 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700931 warnIfCallingFromSystemProcess();
Jorim Jaggi2adba072016-03-03 13:43:39 +0100932
933 // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
934 // generally not allowed, except if the caller specifies the task id the activity should
Bryce Leef465ea02017-05-02 16:58:22 -0700935 // be launched in. A bug was existed between N and O-MR1 which allowed this to work. We
936 // maintain this for backwards compatibility.
937 final int targetSdkVersion = getApplicationInfo().targetSdkVersion;
938
939 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
940 && (targetSdkVersion < Build.VERSION_CODES.N
941 || targetSdkVersion >= Build.VERSION_CODES.P)
942 && (options == null
943 || ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 throw new AndroidRuntimeException(
945 "Calling startActivity() from outside of an Activity "
Bryce Leef465ea02017-05-02 16:58:22 -0700946 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
947 + " Is this really what you want?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700950 getOuterContext(), mMainThread.getApplicationThread(), null,
951 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953
Amith Yamasani258848d2012-08-10 17:06:33 -0700954 /** @hide */
955 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700956 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700957 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700958 ActivityTaskManager.getService().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800959 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700960 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700961 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700962 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700963 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700964 throw e.rethrowFromSystemServer();
Amith Yamasani258848d2012-08-10 17:06:33 -0700965 }
966 }
967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800969 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700970 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700971 startActivities(intents, null);
972 }
973
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700974 /** @hide */
975 @Override
Selim Cinek7fa385a2018-01-24 08:35:28 -0800976 public int startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700977 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
978 throw new AndroidRuntimeException(
979 "Calling startActivities() from outside of an Activity "
980 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
981 + " Is this really what you want?");
982 }
Selim Cinek7fa385a2018-01-24 08:35:28 -0800983 return mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700984 getOuterContext(), mMainThread.getApplicationThread(), null,
985 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700986 }
987
Dianne Hackborna4972e92012-03-14 10:38:05 -0700988 @Override
989 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700990 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800991 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
992 throw new AndroidRuntimeException(
993 "Calling startActivities() from outside of an Activity "
994 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
995 + " Is this really what you want?");
996 }
997 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700998 getOuterContext(), mMainThread.getApplicationThread(), null,
999 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001000 }
1001
1002 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001003 public void startIntentSender(IntentSender intent,
1004 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
1005 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001006 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
1007 }
1008
1009 @Override
1010 public void startIntentSender(IntentSender intent, Intent fillInIntent,
1011 int flagsMask, int flagsValues, int extraFlags, Bundle options)
1012 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001013 try {
1014 String resolvedType = null;
1015 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001016 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07001017 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001018 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
1019 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001020 int result = ActivityTaskManager.getService()
Dianne Hackborn3e6e3852017-05-19 16:12:08 -07001021 .startActivityIntentSender(mMainThread.getApplicationThread(),
1022 intent != null ? intent.getTarget() : null,
1023 intent != null ? intent.getWhitelistToken() : null,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001024 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001025 0, flagsMask, flagsValues, options);
1026 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001027 throw new IntentSender.SendIntentException();
1028 }
1029 Instrumentation.checkStartActivityResult(result, null);
1030 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001031 throw e.rethrowFromSystemServer();
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001032 }
1033 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001034
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001035 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001037 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1039 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001040 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001041 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001042 mMainThread.getApplicationThread(), intent, resolvedType, null,
1043 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
1044 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001046 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048 }
1049
Amith Yamasani67cf7d32012-02-16 14:31:23 -08001050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001052 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001054 String[] receiverPermissions = receiverPermission == null ? null
1055 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001057 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001058 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001059 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001060 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1061 null, false, false, getUserId());
1062 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001063 throw e.rethrowFromSystemServer();
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001064 }
1065 }
1066
1067 @Override
1068 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
1069 warnIfCallingFromSystemProcess();
1070 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1071 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001072 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001073 ActivityManager.getService().broadcastIntent(
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001074 mMainThread.getApplicationThread(), intent, resolvedType, null,
1075 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001076 null, false, false, getUserId());
1077 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001078 throw e.rethrowFromSystemServer();
Dianne Hackborna750a632015-06-16 17:18:23 -07001079 }
1080 }
1081
1082 @Override
Tyler Gunnf955e562018-04-26 14:43:31 -07001083 public void sendBroadcastAsUserMultiplePermissions(Intent intent, UserHandle user,
1084 String[] receiverPermissions) {
1085 warnIfCallingFromSystemProcess();
1086 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1087 try {
1088 intent.prepareToLeaveProcess(this);
1089 ActivityManager.getService().broadcastIntent(
1090 mMainThread.getApplicationThread(), intent, resolvedType, null,
1091 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1092 null, false, false, user.getIdentifier());
1093 } catch (RemoteException e) {
1094 throw e.rethrowFromSystemServer();
1095 }
1096 }
1097
1098 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -07001099 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
1100 warnIfCallingFromSystemProcess();
1101 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001102 String[] receiverPermissions = receiverPermission == null ? null
1103 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -07001104 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001105 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001106 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001107 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001108 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001109 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001110 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001111 throw e.rethrowFromSystemServer();
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001112 }
1113 }
1114
1115 @Override
1116 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
1117 warnIfCallingFromSystemProcess();
1118 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001119 String[] receiverPermissions = receiverPermission == null ? null
1120 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001121 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001122 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001123 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001124 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001125 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -07001126 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001128 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
1130 }
1131
1132 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -07001133 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001134 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001136 String[] receiverPermissions = receiverPermission == null ? null
1137 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001139 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001140 ActivityManager.getService().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -07001141 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001142 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001143 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001145 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 }
1148
1149 @Override
1150 public void sendOrderedBroadcast(Intent intent,
1151 String receiverPermission, BroadcastReceiver resultReceiver,
1152 Handler scheduler, int initialCode, String initialData,
1153 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001154 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -07001155 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1156 }
1157
1158 @Override
1159 public void sendOrderedBroadcast(Intent intent,
1160 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
1161 Handler scheduler, int initialCode, String initialData,
1162 Bundle initialExtras) {
1163 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1164 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001165 }
1166
1167 @Override
1168 public void sendOrderedBroadcast(Intent intent,
1169 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1170 Handler scheduler, int initialCode, String initialData,
1171 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001172 sendOrderedBroadcast(intent, receiverPermission, appOp,
1173 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1174 }
1175
1176 void sendOrderedBroadcast(Intent intent,
1177 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1178 Handler scheduler, int initialCode, String initialData,
1179 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001180 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 IIntentReceiver rd = null;
1182 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001183 if (mPackageInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 if (scheduler == null) {
1185 scheduler = mMainThread.getHandler();
1186 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001187 rd = mPackageInfo.getReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 resultReceiver, getOuterContext(), scheduler,
1189 mMainThread.getInstrumentation(), false);
1190 } else {
1191 if (scheduler == null) {
1192 scheduler = mMainThread.getHandler();
1193 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001194 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1196 }
1197 }
1198 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001199 String[] receiverPermissions = receiverPermission == null ? null
1200 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001202 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001203 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001205 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001206 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001208 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
1210 }
1211
1212 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001213 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001214 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1215 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001216 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001217 ActivityManager.getService().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001218 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001219 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001220 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001221 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001222 }
1223 }
1224
1225 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001226 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1227 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001228 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1229 }
1230
1231 @Override
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001232 public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission,
1233 Bundle options) {
1234 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1235 String[] receiverPermissions = receiverPermission == null ? null
1236 : new String[] {receiverPermission};
1237 try {
1238 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001239 ActivityManager.getService().broadcastIntent(
Chad Brubaker52c8edc2016-07-25 14:30:26 -07001240 mMainThread.getApplicationThread(), intent, resolvedType, null,
1241 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
1242 options, false, false, user.getIdentifier());
1243 } catch (RemoteException e) {
1244 throw e.rethrowFromSystemServer();
1245 }
1246 }
1247
1248 @Override
Svet Ganov16a16892015-04-16 10:32:04 -07001249 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1250 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001251 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001252 String[] receiverPermissions = receiverPermission == null ? null
1253 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001254 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001255 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001256 ActivityManager.getService().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001257 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001258 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001259 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001260 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001261 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001262 }
1263 }
1264
1265 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001266 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001267 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001268 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001269 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001270 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001271 }
1272
1273 @Override
1274 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1275 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001276 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1277 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1278 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1279 }
1280
1281 @Override
1282 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1283 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1284 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001285 IIntentReceiver rd = null;
1286 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001287 if (mPackageInfo != null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001288 if (scheduler == null) {
1289 scheduler = mMainThread.getHandler();
1290 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001291 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001292 resultReceiver, getOuterContext(), scheduler,
1293 mMainThread.getInstrumentation(), false);
1294 } else {
1295 if (scheduler == null) {
1296 scheduler = mMainThread.getHandler();
1297 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001298 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1299 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001300 }
1301 }
1302 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001303 String[] receiverPermissions = receiverPermission == null ? null
1304 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001305 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001306 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001307 ActivityManager.getService().broadcastIntent(
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001308 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001309 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001310 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001311 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001312 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001313 }
1314 }
1315
1316 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001317 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001319 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1321 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001322 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001323 ActivityManager.getService().broadcastIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001325 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001326 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001328 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
1330 }
1331
1332 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001333 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001334 public void sendStickyOrderedBroadcast(Intent intent,
1335 BroadcastReceiver resultReceiver,
1336 Handler scheduler, int initialCode, String initialData,
1337 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001338 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001339 IIntentReceiver rd = null;
1340 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001341 if (mPackageInfo != null) {
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001342 if (scheduler == null) {
1343 scheduler = mMainThread.getHandler();
1344 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001345 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001346 resultReceiver, getOuterContext(), scheduler,
1347 mMainThread.getInstrumentation(), false);
1348 } else {
1349 if (scheduler == null) {
1350 scheduler = mMainThread.getHandler();
1351 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001352 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001353 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1354 }
1355 }
1356 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1357 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001358 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001359 ActivityManager.getService().broadcastIntent(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001360 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1361 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001362 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001363 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001364 throw e.rethrowFromSystemServer();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001365 }
1366 }
1367
1368 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001369 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 public void removeStickyBroadcast(Intent intent) {
1371 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1372 if (resolvedType != null) {
1373 intent = new Intent(intent);
1374 intent.setDataAndType(intent.getData(), resolvedType);
1375 }
1376 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001377 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001378 ActivityManager.getService().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001379 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001381 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383 }
1384
1385 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001386 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001387 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1388 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1389 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001390 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001391 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001392 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001393 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1394 user.getIdentifier());
1395 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001396 throw e.rethrowFromSystemServer();
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001397 }
1398 }
1399
1400 @Override
1401 @Deprecated
1402 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1403 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1404 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001405 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001406 ActivityManager.getService().broadcastIntent(
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001407 mMainThread.getApplicationThread(), intent, resolvedType, null,
1408 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1409 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001410 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001411 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001412 }
1413 }
1414
1415 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001416 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001417 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1418 UserHandle user, BroadcastReceiver resultReceiver,
1419 Handler scheduler, int initialCode, String initialData,
1420 Bundle initialExtras) {
1421 IIntentReceiver rd = null;
1422 if (resultReceiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001423 if (mPackageInfo != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001424 if (scheduler == null) {
1425 scheduler = mMainThread.getHandler();
1426 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001427 rd = mPackageInfo.getReceiverDispatcher(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001428 resultReceiver, getOuterContext(), scheduler,
1429 mMainThread.getInstrumentation(), false);
1430 } else {
1431 if (scheduler == null) {
1432 scheduler = mMainThread.getHandler();
1433 }
1434 rd = new LoadedApk.ReceiverDispatcher(
1435 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1436 }
1437 }
1438 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1439 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001440 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001441 ActivityManager.getService().broadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001442 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1443 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001444 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001445 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001446 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001447 }
1448 }
1449
1450 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001451 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001452 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1453 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1454 if (resolvedType != null) {
1455 intent = new Intent(intent);
1456 intent.setDataAndType(intent.getData(), resolvedType);
1457 }
1458 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001459 intent.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001460 ActivityManager.getService().unbroadcastIntent(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001461 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1462 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001463 throw e.rethrowFromSystemServer();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001464 }
1465 }
1466
1467 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1469 return registerReceiver(receiver, filter, null, null);
1470 }
1471
1472 @Override
1473 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001474 int flags) {
1475 return registerReceiver(receiver, filter, null, null, flags);
Chad Brubaker816c83b2017-03-02 10:27:59 -08001476 }
1477
1478 @Override
1479 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001481 return registerReceiverInternal(receiver, getUserId(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001482 filter, broadcastPermission, scheduler, getOuterContext(), 0);
Chad Brubaker816c83b2017-03-02 10:27:59 -08001483 }
1484
1485 @Override
1486 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001487 String broadcastPermission, Handler scheduler, int flags) {
Chad Brubaker816c83b2017-03-02 10:27:59 -08001488 return registerReceiverInternal(receiver, getUserId(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001489 filter, broadcastPermission, scheduler, getOuterContext(), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491
Dianne Hackborn20e80982012-08-31 19:00:44 -07001492 @Override
1493 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1494 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1495 return registerReceiverInternal(receiver, user.getIdentifier(),
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001496 filter, broadcastPermission, scheduler, getOuterContext(), 0);
Dianne Hackborn20e80982012-08-31 19:00:44 -07001497 }
1498
1499 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 IntentFilter filter, String broadcastPermission,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001501 Handler scheduler, Context context, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 IIntentReceiver rd = null;
1503 if (receiver != null) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001504 if (mPackageInfo != null && context != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 if (scheduler == null) {
1506 scheduler = mMainThread.getHandler();
1507 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001508 rd = mPackageInfo.getReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 receiver, context, scheduler,
1510 mMainThread.getInstrumentation(), true);
1511 } else {
1512 if (scheduler == null) {
1513 scheduler = mMainThread.getHandler();
1514 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001515 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001516 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
1518 }
1519 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001520 final Intent intent = ActivityManager.getService().registerReceiver(
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001521 mMainThread.getApplicationThread(), mBasePackageName, rd, filter,
Chad Brubaker6d6015f2017-04-18 11:25:16 -07001522 broadcastPermission, userId, flags);
Jeff Sharkeyd136e512016-03-09 22:30:56 -07001523 if (intent != null) {
1524 intent.setExtrasClassLoader(getClassLoader());
1525 intent.prepareToEnterProcess();
1526 }
1527 return intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001529 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
1531 }
1532
1533 @Override
1534 public void unregisterReceiver(BroadcastReceiver receiver) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001535 if (mPackageInfo != null) {
1536 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 getOuterContext(), receiver);
1538 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001539 ActivityManager.getService().unregisterReceiver(rd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001541 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
1543 } else {
1544 throw new RuntimeException("Not supported in system context");
1545 }
1546 }
1547
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001548 private void validateServiceIntent(Intent service) {
1549 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001550 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001551 IllegalArgumentException ex = new IllegalArgumentException(
1552 "Service Intent must be explicit: " + service);
1553 throw ex;
1554 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001555 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1556 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001557 }
1558 }
1559 }
1560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 @Override
1562 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001563 warnIfCallingFromSystemProcess();
Christopher Tate242ba3e92017-04-14 15:07:06 -07001564 return startServiceCommon(service, false, mUser);
Christopher Tate42a386b2016-11-07 12:21:21 -08001565 }
1566
1567 @Override
Christopher Tate79047c62017-03-21 11:37:06 -07001568 public ComponentName startForegroundService(Intent service) {
1569 warnIfCallingFromSystemProcess();
Christopher Tate242ba3e92017-04-14 15:07:06 -07001570 return startServiceCommon(service, true, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001571 }
1572
1573 @Override
1574 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001575 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001576 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001577 }
1578
1579 @Override
1580 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Christopher Tate242ba3e92017-04-14 15:07:06 -07001581 return startServiceCommon(service, false, user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001582 }
1583
Christopher Tate42a386b2016-11-07 12:21:21 -08001584 @Override
Christopher Tate79047c62017-03-21 11:37:06 -07001585 public ComponentName startForegroundServiceAsUser(Intent service, UserHandle user) {
Christopher Tate242ba3e92017-04-14 15:07:06 -07001586 return startServiceCommon(service, true, user);
Christopher Tate79047c62017-03-21 11:37:06 -07001587 }
1588
Christopher Tate242ba3e92017-04-14 15:07:06 -07001589 private ComponentName startServiceCommon(Intent service, boolean requireForeground,
1590 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001592 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001593 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001594 ComponentName cn = ActivityManager.getService().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001595 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
Christopher Tate242ba3e92017-04-14 15:07:06 -07001596 getContentResolver()), requireForeground,
Christopher Tate79047c62017-03-21 11:37:06 -07001597 getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001598 if (cn != null) {
1599 if (cn.getPackageName().equals("!")) {
1600 throw new SecurityException(
1601 "Not allowed to start service " + service
1602 + " without permission " + cn.getClassName());
1603 } else if (cn.getPackageName().equals("!!")) {
1604 throw new SecurityException(
1605 "Unable to start service " + service
1606 + ": " + cn.getClassName());
Dianne Hackborn85e35642017-01-12 15:10:57 -08001607 } else if (cn.getPackageName().equals("?")) {
1608 throw new IllegalStateException(
1609 "Not allowed to start service " + service + ": " + cn.getClassName());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
1612 return cn;
1613 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001614 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
1616 }
1617
1618 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001619 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001620 return stopServiceCommon(service, user);
1621 }
1622
1623 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001625 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001626 service.prepareToLeaveProcess(this);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001627 int res = ActivityManager.getService().stopService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001629 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 if (res < 0) {
1631 throw new SecurityException(
1632 "Not allowed to stop service " + service);
1633 }
1634 return res != 0;
1635 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001636 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
1638 }
1639
1640 @Override
1641 public boolean bindService(Intent service, ServiceConnection conn,
1642 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001643 warnIfCallingFromSystemProcess();
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001644 return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), getUser());
1645 }
1646
1647 @Override
1648 public boolean bindIsolatedService(Intent service, ServiceConnection conn,
1649 int flags, String instanceName) {
1650 warnIfCallingFromSystemProcess();
1651 if (instanceName == null) {
1652 throw new NullPointerException("null instanceName");
1653 }
1654 return bindServiceCommon(service, conn, flags, instanceName, mMainThread.getHandler(),
1655 getUser());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001656 }
1657
1658 /** @hide */
1659 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001660 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1661 UserHandle user) {
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001662 return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001663 }
1664
Adrian Roos691546e2016-02-09 10:13:41 -08001665 /** @hide */
1666 @Override
1667 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1668 Handler handler, UserHandle user) {
1669 if (handler == null) {
1670 throw new IllegalArgumentException("handler must not be null.");
1671 }
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001672 return bindServiceCommon(service, conn, flags, null, handler, user);
Adrian Roos691546e2016-02-09 10:13:41 -08001673 }
1674
Tony Mak46aabe52016-11-14 12:53:06 +00001675 /** @hide */
1676 @Override
1677 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler,
1678 int flags) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00001679 return mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
Tony Mak46aabe52016-11-14 12:53:06 +00001680 }
1681
1682 /** @hide */
1683 @Override
1684 public IApplicationThread getIApplicationThread() {
1685 return mMainThread.getApplicationThread();
1686 }
1687
Tony Makbf9928d2016-12-22 11:02:45 +00001688 /** @hide */
1689 @Override
1690 public Handler getMainThreadHandler() {
1691 return mMainThread.getHandler();
1692 }
1693
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001694 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,
1695 String instanceName, Handler
Adrian Roos691546e2016-02-09 10:13:41 -08001696 handler, UserHandle user) {
Tony Mak46aabe52016-11-14 12:53:06 +00001697 // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001699 if (conn == null) {
1700 throw new IllegalArgumentException("connection is null");
1701 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001702 if (mPackageInfo != null) {
1703 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 } else {
1705 throw new RuntimeException("Not supported in system context");
1706 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001707 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001709 IBinder token = getActivityToken();
Todd Kennedy233a0b12018-01-29 20:30:24 +00001710 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1711 && mPackageInfo.getApplicationInfo().targetSdkVersion
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001712 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1713 flags |= BIND_WAIVE_PRIORITY;
1714 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001715 service.prepareToLeaveProcess(this);
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001716 int res = ActivityManager.getService().bindIsolatedService(
Svet Ganov99b60432015-06-27 13:15:22 -07001717 mMainThread.getApplicationThread(), getActivityToken(), service,
1718 service.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn27b4d942018-11-12 15:01:40 -08001719 sd, flags, instanceName, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 if (res < 0) {
1721 throw new SecurityException(
1722 "Not allowed to bind to service " + service);
1723 }
1724 return res != 0;
1725 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001726 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
1728 }
1729
1730 @Override
Dianne Hackborna631d562018-11-20 15:58:15 -08001731 public void updateServiceGroup(@NonNull ServiceConnection conn, int group, int importance) {
1732 if (conn == null) {
1733 throw new IllegalArgumentException("connection is null");
1734 }
1735 if (mPackageInfo != null) {
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -08001736 IServiceConnection sd = mPackageInfo.lookupServiceDispatcher(conn, getOuterContext());
1737 if (sd == null) {
1738 throw new IllegalArgumentException("ServiceConnection not currently bound: "
1739 + conn);
1740 }
Dianne Hackborna631d562018-11-20 15:58:15 -08001741 try {
1742 ActivityManager.getService().updateServiceGroup(sd, group, importance);
1743 } catch (RemoteException e) {
1744 throw e.rethrowFromSystemServer();
1745 }
1746 } else {
1747 throw new RuntimeException("Not supported in system context");
1748 }
1749 }
1750
1751 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001753 if (conn == null) {
1754 throw new IllegalArgumentException("connection is null");
1755 }
Todd Kennedy233a0b12018-01-29 20:30:24 +00001756 if (mPackageInfo != null) {
1757 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 getOuterContext(), conn);
1759 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001760 ActivityManager.getService().unbindService(sd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001762 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 }
1764 } else {
1765 throw new RuntimeException("Not supported in system context");
1766 }
1767 }
1768
1769 @Override
1770 public boolean startInstrumentation(ComponentName className,
1771 String profileFile, Bundle arguments) {
1772 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001773 if (arguments != null) {
1774 arguments.setAllowFds(false);
1775 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001776 return ActivityManager.getService().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001777 className, profileFile, 0, arguments, null, null, getUserId(),
1778 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001780 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
1783
1784 @Override
1785 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001786 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788
Jeff Brown6e539312015-02-24 18:53:21 -08001789 @Override
1790 public String getSystemServiceName(Class<?> serviceClass) {
1791 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001792 }
1793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 @Override
1795 public int checkPermission(String permission, int pid, int uid) {
1796 if (permission == null) {
1797 throw new IllegalArgumentException("permission is null");
1798 }
1799
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001800 final IActivityManager am = ActivityManager.getService();
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001801 if (am == null) {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001802 // Well this is super awkward; we somehow don't have an active
Jeff Sharkeyd69b0832017-01-23 11:37:24 -07001803 // ActivityManager instance. If we're testing a root or system
1804 // UID, then they totally have whatever permission this is.
1805 final int appId = UserHandle.getAppId(uid);
1806 if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) {
1807 Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " holds " + permission);
1808 return PackageManager.PERMISSION_GRANTED;
1809 }
Erik Wolsheimere21576f2018-03-29 16:21:41 -07001810 Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " does not hold "
1811 + permission);
1812 return PackageManager.PERMISSION_DENIED;
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001813 }
1814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 try {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07001816 return am.checkPermission(permission, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001818 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
1820 }
1821
Dianne Hackbornff170242014-11-19 10:59:01 -08001822 /** @hide */
1823 @Override
1824 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1825 if (permission == null) {
1826 throw new IllegalArgumentException("permission is null");
1827 }
1828
1829 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001830 return ActivityManager.getService().checkPermissionWithToken(
Dianne Hackbornff170242014-11-19 10:59:01 -08001831 permission, pid, uid, callerToken);
1832 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001833 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001834 }
1835 }
1836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 @Override
1838 public int checkCallingPermission(String permission) {
1839 if (permission == null) {
1840 throw new IllegalArgumentException("permission is null");
1841 }
1842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 int pid = Binder.getCallingPid();
1844 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001845 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
1847 return PackageManager.PERMISSION_DENIED;
1848 }
1849
1850 @Override
1851 public int checkCallingOrSelfPermission(String permission) {
1852 if (permission == null) {
1853 throw new IllegalArgumentException("permission is null");
1854 }
1855
1856 return checkPermission(permission, Binder.getCallingPid(),
1857 Binder.getCallingUid());
1858 }
1859
Svetoslavc6d1c342015-02-26 14:44:43 -08001860 @Override
1861 public int checkSelfPermission(String permission) {
1862 if (permission == null) {
1863 throw new IllegalArgumentException("permission is null");
1864 }
1865
1866 return checkPermission(permission, Process.myPid(), Process.myUid());
1867 }
1868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 private void enforce(
1870 String permission, int resultOfCheck,
1871 boolean selfToo, int uid, String message) {
1872 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1873 throw new SecurityException(
1874 (message != null ? (message + ": ") : "") +
1875 (selfToo
1876 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001877 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 permission +
1879 ".");
1880 }
1881 }
1882
Jeff Brown6e539312015-02-24 18:53:21 -08001883 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 public void enforcePermission(
1885 String permission, int pid, int uid, String message) {
1886 enforce(permission,
1887 checkPermission(permission, pid, uid),
1888 false,
1889 uid,
1890 message);
1891 }
1892
Jeff Brown6e539312015-02-24 18:53:21 -08001893 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 public void enforceCallingPermission(String permission, String message) {
1895 enforce(permission,
1896 checkCallingPermission(permission),
1897 false,
1898 Binder.getCallingUid(),
1899 message);
1900 }
1901
Jeff Brown6e539312015-02-24 18:53:21 -08001902 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 public void enforceCallingOrSelfPermission(
1904 String permission, String message) {
1905 enforce(permission,
1906 checkCallingOrSelfPermission(permission),
1907 true,
1908 Binder.getCallingUid(),
1909 message);
1910 }
1911
1912 @Override
1913 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1914 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001915 ActivityManager.getService().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001916 mMainThread.getApplicationThread(), toPackage,
1917 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001919 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
1921 }
1922
1923 @Override
1924 public void revokeUriPermission(Uri uri, int modeFlags) {
1925 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001926 ActivityManager.getService().revokeUriPermission(
Dianne Hackborna47223f2017-03-30 13:49:13 -07001927 mMainThread.getApplicationThread(), null,
1928 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
1929 } catch (RemoteException e) {
1930 throw e.rethrowFromSystemServer();
1931 }
1932 }
1933
1934 @Override
1935 public void revokeUriPermission(String targetPackage, Uri uri, int modeFlags) {
1936 try {
1937 ActivityManager.getService().revokeUriPermission(
1938 mMainThread.getApplicationThread(), targetPackage,
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001939 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001941 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
1943 }
1944
1945 @Override
1946 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001948 return ActivityManager.getService().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001949 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001950 resolveUserId(uri), null);
1951 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001952 throw e.rethrowFromSystemServer();
Dianne Hackbornff170242014-11-19 10:59:01 -08001953 }
1954 }
1955
1956 /** @hide */
1957 @Override
1958 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1959 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001960 return ActivityManager.getService().checkUriPermission(
Dianne Hackbornff170242014-11-19 10:59:01 -08001961 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1962 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001964 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966 }
1967
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001968 private int resolveUserId(Uri uri) {
1969 return ContentProvider.getUserIdFromUri(uri, getUserId());
1970 }
1971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 @Override
1973 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 int pid = Binder.getCallingPid();
1975 if (pid != Process.myPid()) {
1976 return checkUriPermission(uri, pid,
1977 Binder.getCallingUid(), modeFlags);
1978 }
1979 return PackageManager.PERMISSION_DENIED;
1980 }
1981
1982 @Override
1983 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1984 return checkUriPermission(uri, Binder.getCallingPid(),
1985 Binder.getCallingUid(), modeFlags);
1986 }
1987
1988 @Override
1989 public int checkUriPermission(Uri uri, String readPermission,
1990 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001991 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1993 + readPermission + " writePermission=" + writePermission
1994 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1995 }
1996 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1997 if (readPermission == null
1998 || checkPermission(readPermission, pid, uid)
1999 == PackageManager.PERMISSION_GRANTED) {
2000 return PackageManager.PERMISSION_GRANTED;
2001 }
2002 }
2003 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
2004 if (writePermission == null
2005 || checkPermission(writePermission, pid, uid)
2006 == PackageManager.PERMISSION_GRANTED) {
2007 return PackageManager.PERMISSION_GRANTED;
2008 }
2009 }
2010 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
2011 : PackageManager.PERMISSION_DENIED;
2012 }
2013
2014 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07002015 StringBuilder builder = new StringBuilder();
2016 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
2017 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07002019 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
2020 builder.append("write and ");
2021 }
2022 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
2023 builder.append("persistable and ");
2024 }
2025 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
2026 builder.append("prefix and ");
2027 }
2028
2029 if (builder.length() > 5) {
2030 builder.setLength(builder.length() - 5);
2031 return builder.toString();
2032 } else {
2033 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
2034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
2036
2037 private void enforceForUri(
2038 int modeFlags, int resultOfCheck, boolean selfToo,
2039 int uid, Uri uri, String message) {
2040 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
2041 throw new SecurityException(
2042 (message != null ? (message + ": ") : "") +
2043 (selfToo
2044 ? "Neither user " + uid + " nor current process has "
2045 : "User " + uid + " does not have ") +
2046 uriModeFlagToString(modeFlags) +
2047 " permission on " +
2048 uri +
2049 ".");
2050 }
2051 }
2052
Jeff Brown6e539312015-02-24 18:53:21 -08002053 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 public void enforceUriPermission(
2055 Uri uri, int pid, int uid, int modeFlags, String message) {
2056 enforceForUri(
2057 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
2058 false, uid, uri, message);
2059 }
2060
Jeff Brown6e539312015-02-24 18:53:21 -08002061 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 public void enforceCallingUriPermission(
2063 Uri uri, int modeFlags, String message) {
2064 enforceForUri(
2065 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07002066 false,
2067 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
2069
Jeff Brown6e539312015-02-24 18:53:21 -08002070 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 public void enforceCallingOrSelfUriPermission(
2072 Uri uri, int modeFlags, String message) {
2073 enforceForUri(
2074 modeFlags,
2075 checkCallingOrSelfUriPermission(uri, modeFlags), true,
2076 Binder.getCallingUid(), uri, message);
2077 }
2078
Jeff Brown6e539312015-02-24 18:53:21 -08002079 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 public void enforceUriPermission(
2081 Uri uri, String readPermission, String writePermission,
2082 int pid, int uid, int modeFlags, String message) {
2083 enforceForUri(modeFlags,
2084 checkUriPermission(
2085 uri, readPermission, writePermission, pid, uid,
2086 modeFlags),
2087 false,
2088 uid,
2089 uri,
2090 message);
2091 }
2092
Tom O'Neill365632e2013-09-09 09:34:58 -07002093 /**
2094 * Logs a warning if the system process directly called a method such as
2095 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
2096 * The "AsUser" variants allow us to properly enforce the user's restrictions.
2097 */
Amith Yamasanicd757062012-10-19 18:23:52 -07002098 private void warnIfCallingFromSystemProcess() {
2099 if (Process.myUid() == Process.SYSTEM_UID) {
2100 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08002101 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07002102 }
2103 }
2104
Todd Kennedy233a0b12018-01-29 20:30:24 +00002105 private static Resources createResources(IBinder activityToken, LoadedApk pi, String splitName,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002106 int displayId, Configuration overrideConfig, CompatibilityInfo compatInfo,
2107 String[] overlayDirs) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002108 final String[] splitResDirs;
2109 final ClassLoader classLoader;
2110 try {
2111 splitResDirs = pi.getSplitPaths(splitName);
2112 classLoader = pi.getSplitClassLoader(splitName);
2113 } catch (NameNotFoundException e) {
2114 throw new RuntimeException(e);
2115 }
2116 return ResourcesManager.getInstance().getResources(activityToken,
2117 pi.getResDir(),
2118 splitResDirs,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002119 overlayDirs,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002120 pi.getApplicationInfo().sharedLibraryFiles,
2121 displayId,
2122 overrideConfig,
2123 compatInfo,
2124 classLoader);
2125 }
2126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07002128 public Context createApplicationContext(ApplicationInfo application, int flags)
2129 throws NameNotFoundException {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002130 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
Svetoslav976e8bd2014-07-16 15:12:03 -07002131 flags | CONTEXT_REGISTER_PACKAGE);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002132 if (pi != null) {
2133 ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken,
Makoto Onuki4becc352019-01-30 13:30:41 -08002134 new UserHandle(UserHandle.getUserId(application.uid)), flags, null, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002135
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002136 final int displayId = getDisplayId();
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002137 // overlayDirs is retrieved directly from ApplicationInfo since ActivityThread may have
2138 // a LoadedApk containing Resources with stale overlays for a remote application.
2139 final String[] overlayDirs = application.resourceDirs;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002140 c.setResources(createResources(mActivityToken, pi, null, displayId, null,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002141 getDisplayAdjustments(displayId).getCompatibilityInfo(), overlayDirs));
Svetoslav976e8bd2014-07-16 15:12:03 -07002142 if (c.mResources != null) {
2143 return c;
2144 }
2145 }
2146
2147 throw new PackageManager.NameNotFoundException(
2148 "Application package " + application.packageName + " not found");
2149 }
2150
2151 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07002153 throws NameNotFoundException {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002154 return createPackageContextAsUser(packageName, flags, mUser);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002155 }
2156
2157 @Override
2158 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
2159 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00002160 if (packageName.equals("system") || packageName.equals("android")) {
Adam Lesinski8fa71072016-11-18 18:13:55 -08002161 // The system resources are loaded in every application, so we can safely copy
2162 // the context without reloading Resources.
Todd Kennedy233a0b12018-01-29 20:30:24 +00002163 return new ContextImpl(this, mMainThread, mPackageInfo, null, mActivityToken, user,
Makoto Onuki4becc352019-01-30 13:30:41 -08002164 flags, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
2166
Todd Kennedy233a0b12018-01-29 20:30:24 +00002167 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002168 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
Todd Kennedy233a0b12018-01-29 20:30:24 +00002169 if (pi != null) {
2170 ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken, user,
Makoto Onuki4becc352019-01-30 13:30:41 -08002171 flags, null, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002172
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002173 final int displayId = getDisplayId();
Adam Lesinski8fa71072016-11-18 18:13:55 -08002174
Todd Kennedy233a0b12018-01-29 20:30:24 +00002175 c.setResources(createResources(mActivityToken, pi, null, displayId, null,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002176 getDisplayAdjustments(displayId).getCompatibilityInfo(), pi.getOverlayDirs()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 if (c.mResources != null) {
2178 return c;
2179 }
2180 }
2181
2182 // Should be a better exception.
2183 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07002184 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 }
2186
Romain Guy870e09f2009-07-06 16:35:25 -07002187 @Override
Adam Lesinski4e862812016-11-21 16:02:24 -08002188 public Context createContextForSplit(String splitName) throws NameNotFoundException {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002189 if (!mPackageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
Adam Lesinski4e862812016-11-21 16:02:24 -08002190 // All Splits are always loaded.
2191 return this;
2192 }
2193
Todd Kennedy233a0b12018-01-29 20:30:24 +00002194 final ClassLoader classLoader = mPackageInfo.getSplitClassLoader(splitName);
2195 final String[] paths = mPackageInfo.getSplitPaths(splitName);
Adam Lesinski4e862812016-11-21 16:02:24 -08002196
Todd Kennedy233a0b12018-01-29 20:30:24 +00002197 final ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, splitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002198 mActivityToken, mUser, mFlags, classLoader, null);
Adam Lesinski4e862812016-11-21 16:02:24 -08002199
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002200 final int displayId = getDisplayId();
Adam Lesinski4e862812016-11-21 16:02:24 -08002201
Todd Kennedy233a0b12018-01-29 20:30:24 +00002202 context.setResources(ResourcesManager.getInstance().getResources(
2203 mActivityToken,
2204 mPackageInfo.getResDir(),
2205 paths,
2206 mPackageInfo.getOverlayDirs(),
2207 mPackageInfo.getApplicationInfo().sharedLibraryFiles,
2208 displayId,
2209 null,
2210 mPackageInfo.getCompatibilityInfo(),
2211 classLoader));
Adam Lesinski4e862812016-11-21 16:02:24 -08002212 return context;
2213 }
2214
2215 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07002216 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07002217 if (overrideConfiguration == null) {
2218 throw new IllegalArgumentException("overrideConfiguration must not be null");
2219 }
2220
Todd Kennedy233a0b12018-01-29 20:30:24 +00002221 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002222 mActivityToken, mUser, mFlags, mClassLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002223
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002224 final int displayId = getDisplayId();
Todd Kennedy233a0b12018-01-29 20:30:24 +00002225 context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002226 overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo(),
2227 mPackageInfo.getOverlayDirs()));
Adam Lesinski8fa71072016-11-18 18:13:55 -08002228 return context;
Dianne Hackborn756220b2012-08-14 16:45:30 -07002229 }
2230
2231 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07002232 public Context createDisplayContext(Display display) {
2233 if (display == null) {
2234 throw new IllegalArgumentException("display must not be null");
2235 }
2236
Todd Kennedy233a0b12018-01-29 20:30:24 +00002237 ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002238 mActivityToken, mUser, mFlags, mClassLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002239
2240 final int displayId = display.getDisplayId();
Todd Kennedy233a0b12018-01-29 20:30:24 +00002241 context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002242 null, getDisplayAdjustments(displayId).getCompatibilityInfo(),
2243 mPackageInfo.getOverlayDirs()));
Adam Lesinski8fa71072016-11-18 18:13:55 -08002244 context.mDisplay = display;
2245 return context;
Jeff Browna492c3a2012-08-23 19:48:44 -07002246 }
2247
Jeff Browna492c3a2012-08-23 19:48:44 -07002248 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002249 public Context createDeviceProtectedStorageContext() {
2250 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
2251 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002252 return new ContextImpl(this, mMainThread, mPackageInfo, mSplitName, mActivityToken, mUser,
Makoto Onuki4becc352019-01-30 13:30:41 -08002253 flags, mClassLoader, null);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002254 }
2255
2256 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002257 public Context createCredentialProtectedStorageContext() {
2258 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
2259 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002260 return new ContextImpl(this, mMainThread, mPackageInfo, mSplitName, mActivityToken, mUser,
Makoto Onuki4becc352019-01-30 13:30:41 -08002261 flags, mClassLoader, null);
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002262 }
2263
2264 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07002265 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002266 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
2267 }
2268
2269 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002270 public boolean isDeviceProtectedStorage() {
2271 return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002272 }
2273
2274 @Override
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002275 public boolean isCredentialProtectedStorage() {
2276 return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07002277 }
2278
Jeff Brown98365d72012-08-19 20:30:52 -07002279 @Override
Seigo Nonaka6d6cd682017-06-22 08:22:18 -07002280 public boolean canLoadUnsafeResources() {
2281 if (getPackageName().equals(getOpPackageName())) {
2282 return true;
2283 }
2284 return (mFlags & Context.CONTEXT_IGNORE_SECURITY) != 0;
2285 }
2286
Wale Ogunwale691af682019-02-11 03:09:10 -08002287 @TestApi
Seigo Nonaka6d6cd682017-06-22 08:22:18 -07002288 @Override
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002289 public Display getDisplay() {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002290 if (mDisplay == null) {
2291 return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY,
Bryce Lee609bf652017-02-09 16:50:13 -08002292 mResources);
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002293 }
2294
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002295 return mDisplay;
2296 }
2297
2298 @Override
Yohei Yukawa5281b6b2018-10-15 07:38:25 +08002299 public int getDisplayId() {
2300 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
2301 }
2302
2303 @Override
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002304 public void updateDisplay(int displayId) {
Bryce Lee609bf652017-02-09 16:50:13 -08002305 mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002306 }
2307
2308 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07002309 public DisplayAdjustments getDisplayAdjustments(int displayId) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -07002310 return mResources.getDisplayAdjustments();
Jeff Brown98365d72012-08-19 20:30:52 -07002311 }
2312
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002313 @Override
2314 public File getDataDir() {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002315 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07002316 File res = null;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002317 if (isCredentialProtectedStorage()) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002318 res = mPackageInfo.getCredentialProtectedDataDirFile();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002319 } else if (isDeviceProtectedStorage()) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002320 res = mPackageInfo.getDeviceProtectedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002321 } else {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002322 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002323 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002324
2325 if (res != null) {
Jeff Sharkey21f50722016-04-27 12:38:02 -06002326 if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Jeff Sharkey24492ae2016-04-25 13:20:25 -06002327 Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
2328 new Throwable());
2329 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07002330 return res;
2331 } else {
2332 throw new RuntimeException(
2333 "No data directory found for package " + getPackageName());
2334 }
2335 } else {
2336 throw new RuntimeException(
2337 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 }
2340
2341 @Override
2342 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07002343 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07002345 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 if (!file.exists()) {
2347 file.mkdir();
2348 setFilePermissionsFromMode(file.getPath(), mode,
2349 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
2350 }
2351 return file;
2352 }
2353
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002354 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08002355 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002356 public UserHandle getUser() {
2357 return mUser;
2358 }
2359
2360 /** {@hide} */
2361 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002362 public int getUserId() {
2363 return mUser.getIdentifier();
2364 }
2365
Felipe Lemebb567ae2017-10-04 09:56:21 -07002366 /** @hide */
2367 @Override
2368 public AutofillClient getAutofillClient() {
2369 return mAutofillClient;
2370 }
2371
2372 /** @hide */
2373 @Override
2374 public void setAutofillClient(AutofillClient client) {
2375 mAutofillClient = client;
2376 }
2377
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002378 /** @hide */
2379 @Override
Felipe Lemea4f39cd2019-02-19 15:08:59 -08002380 public AutofillOptions getAutofillOptions() {
2381 return mAutofillOptions;
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002382 }
2383
2384 /** @hide */
2385 @Override
Felipe Lemea4f39cd2019-02-19 15:08:59 -08002386 public void setAutofillOptions(AutofillOptions options) {
2387 mAutofillOptions = options;
Svetoslav Ganov24c90452017-12-27 15:17:14 -08002388 }
2389
Felipe Lemeecb08be2018-11-27 15:48:47 -08002390 /** @hide */
2391 @Override
Felipe Leme326f15a2019-02-19 09:42:24 -08002392 public ContentCaptureOptions getContentCaptureOptions() {
2393 return mContentCaptureOptions;
Felipe Lemeecb08be2018-11-27 15:48:47 -08002394 }
2395
2396 /** @hide */
2397 @Override
Felipe Leme326f15a2019-02-19 09:42:24 -08002398 public void setContentCaptureOptions(ContentCaptureOptions options) {
2399 mContentCaptureOptions = options;
Felipe Lemeecb08be2018-11-27 15:48:47 -08002400 }
2401
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002402 @UnsupportedAppUsage
Dianne Hackborn21556372010-02-04 16:34:40 -08002403 static ContextImpl createSystemContext(ActivityThread mainThread) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002404 LoadedApk packageInfo = new LoadedApk(mainThread);
2405 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
Makoto Onuki4becc352019-01-30 13:30:41 -08002406 null, null);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002407 context.setResources(packageInfo.getResources());
Jeff Browndefd4a62014-03-10 21:24:37 -07002408 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Adam Lesinski7f3f4992016-03-30 10:32:15 -07002409 context.mResourcesManager.getDisplayMetrics());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 return context;
2411 }
2412
Adam Lesinskia82b6262017-03-21 16:56:17 -07002413 /**
2414 * System Context to be used for UI. This Context has resources that can be themed.
Adam Lesinski6f1a9172017-04-10 16:35:19 -07002415 * Make sure that the created system UI context shares the same LoadedApk as the system context.
lumark0b05f9e2018-11-26 15:09:06 +08002416 * @param systemContext The system context which created by
2417 * {@link #createSystemContext(ActivityThread)}.
2418 * @param displayId The ID of the display where the UI is shown.
Adam Lesinskia82b6262017-03-21 16:56:17 -07002419 */
lumark0b05f9e2018-11-26 15:09:06 +08002420 static ContextImpl createSystemUiContext(ContextImpl systemContext, int displayId) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002421 final LoadedApk packageInfo = systemContext.mPackageInfo;
2422 ContextImpl context = new ContextImpl(null, systemContext.mMainThread, packageInfo, null,
Makoto Onuki4becc352019-01-30 13:30:41 -08002423 null, null, 0, null, null);
lumark0b05f9e2018-11-26 15:09:06 +08002424 context.setResources(createResources(null, packageInfo, null, displayId, null,
Amin Shaikhc3f0b062019-02-12 19:00:17 -05002425 packageInfo.getCompatibilityInfo(), packageInfo.getOverlayDirs()));
lumark0b05f9e2018-11-26 15:09:06 +08002426 context.updateDisplay(displayId);
Adam Lesinskia82b6262017-03-21 16:56:17 -07002427 return context;
2428 }
2429
lumark0b05f9e2018-11-26 15:09:06 +08002430 /**
2431 * The overloaded method of {@link #createSystemUiContext(ContextImpl, int)}.
2432 * Uses {@Code Display.DEFAULT_DISPLAY} as the target display.
2433 */
2434 static ContextImpl createSystemUiContext(ContextImpl systemContext) {
2435 return createSystemUiContext(systemContext, Display.DEFAULT_DISPLAY);
2436 }
2437
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002438 @UnsupportedAppUsage
Todd Kennedy233a0b12018-01-29 20:30:24 +00002439 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
Makoto Onuki4becc352019-01-30 13:30:41 -08002440 return createAppContext(mainThread, packageInfo, null);
2441 }
2442
2443 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo,
2444 String opPackageName) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002445 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
2446 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
Makoto Onuki4becc352019-01-30 13:30:41 -08002447 null, opPackageName);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002448 context.setResources(packageInfo.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002449 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002450 }
2451
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002452 @UnsupportedAppUsage
Jeff Browndefd4a62014-03-10 21:24:37 -07002453 static ContextImpl createActivityContext(ActivityThread mainThread,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002454 LoadedApk packageInfo, ActivityInfo activityInfo, IBinder activityToken, int displayId,
Adam Lesinski082614c2016-03-04 14:33:47 -08002455 Configuration overrideConfiguration) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002456 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Adam Lesinski8fa71072016-11-18 18:13:55 -08002457
Todd Kennedy233a0b12018-01-29 20:30:24 +00002458 String[] splitDirs = packageInfo.getSplitResDirs();
2459 ClassLoader classLoader = packageInfo.getClassLoader();
Adam Lesinski4e862812016-11-21 16:02:24 -08002460
Todd Kennedy233a0b12018-01-29 20:30:24 +00002461 if (packageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) {
Adam Lesinski4e862812016-11-21 16:02:24 -08002462 Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "SplitDependencies");
2463 try {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002464 classLoader = packageInfo.getSplitClassLoader(activityInfo.splitName);
2465 splitDirs = packageInfo.getSplitPaths(activityInfo.splitName);
Adam Lesinski4e862812016-11-21 16:02:24 -08002466 } catch (NameNotFoundException e) {
2467 // Nothing above us can handle a NameNotFoundException, better crash.
2468 throw new RuntimeException(e);
2469 } finally {
2470 Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
2471 }
2472 }
2473
Todd Kennedy233a0b12018-01-29 20:30:24 +00002474 ContextImpl context = new ContextImpl(null, mainThread, packageInfo, activityInfo.splitName,
Makoto Onuki4becc352019-01-30 13:30:41 -08002475 activityToken, null, 0, classLoader, null);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002476
2477 // Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
2478 displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
2479
2480 final CompatibilityInfo compatInfo = (displayId == Display.DEFAULT_DISPLAY)
Todd Kennedy233a0b12018-01-29 20:30:24 +00002481 ? packageInfo.getCompatibilityInfo()
Adam Lesinski8fa71072016-11-18 18:13:55 -08002482 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
2483
Adam Lesinskic9701942016-11-28 19:44:19 -08002484 final ResourcesManager resourcesManager = ResourcesManager.getInstance();
2485
2486 // Create the base resources for which all configuration contexts for this Activity
2487 // will be rebased upon.
Jason Monkbd983642017-02-24 16:57:40 -05002488 context.setResources(resourcesManager.createBaseActivityResources(activityToken,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002489 packageInfo.getResDir(),
Adam Lesinski4e862812016-11-21 16:02:24 -08002490 splitDirs,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002491 packageInfo.getOverlayDirs(),
2492 packageInfo.getApplicationInfo().sharedLibraryFiles,
Adam Lesinskic9701942016-11-28 19:44:19 -08002493 displayId,
2494 overrideConfiguration,
2495 compatInfo,
Jason Monkbd983642017-02-24 16:57:40 -05002496 classLoader));
Adam Lesinskic9701942016-11-28 19:44:19 -08002497 context.mDisplay = resourcesManager.getAdjustedDisplay(displayId,
Bryce Lee609bf652017-02-09 16:50:13 -08002498 context.getResources());
Adam Lesinski8fa71072016-11-18 18:13:55 -08002499 return context;
Jeff Browndefd4a62014-03-10 21:24:37 -07002500 }
2501
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002502 private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread,
Todd Kennedy233a0b12018-01-29 20:30:24 +00002503 @NonNull LoadedApk packageInfo, @Nullable String splitName,
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002504 @Nullable IBinder activityToken, @Nullable UserHandle user, int flags,
Makoto Onuki4becc352019-01-30 13:30:41 -08002505 @Nullable ClassLoader classLoader, @Nullable String overrideOpPackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002508 // If creator didn't specify which storage to use, use the default
2509 // location for application.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002510 if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
2511 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002512 final File dataDir = packageInfo.getDataDirFile();
2513 if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002514 flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
Todd Kennedy233a0b12018-01-29 20:30:24 +00002515 } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002516 flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002517 }
2518 }
2519
Jeff Browndefd4a62014-03-10 21:24:37 -07002520 mMainThread = mainThread;
2521 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002522 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523
Jeff Browndefd4a62014-03-10 21:24:37 -07002524 if (user == null) {
2525 user = Process.myUserHandle();
2526 }
2527 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002528
Todd Kennedy233a0b12018-01-29 20:30:24 +00002529 mPackageInfo = packageInfo;
Adam Lesinski1665d0f2017-03-10 14:46:57 -08002530 mSplitName = splitName;
Adam Lesinski4e862812016-11-21 16:02:24 -08002531 mClassLoader = classLoader;
Jeff Browndefd4a62014-03-10 21:24:37 -07002532 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002533
Makoto Onuki4becc352019-01-30 13:30:41 -08002534 String opPackageName;
2535
Jeff Browndefd4a62014-03-10 21:24:37 -07002536 if (container != null) {
2537 mBasePackageName = container.mBasePackageName;
Makoto Onuki4becc352019-01-30 13:30:41 -08002538 opPackageName = container.mOpPackageName;
Jason Monkbd983642017-02-24 16:57:40 -05002539 setResources(container.mResources);
Adam Lesinski8fa71072016-11-18 18:13:55 -08002540 mDisplay = container.mDisplay;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002541 } else {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002542 mBasePackageName = packageInfo.mPackageName;
2543 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
Dianne Hackborn95d78532013-09-11 09:51:14 -07002544 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2545 // Special case: system components allow themselves to be loaded in to other
2546 // processes. For purposes of app ops, we must then consider the context as
2547 // belonging to the package of this process, not the system itself, otherwise
2548 // the package+uid verifications in app ops will fail.
Makoto Onuki4becc352019-01-30 13:30:41 -08002549 opPackageName = ActivityThread.currentPackageName();
Dianne Hackborn95d78532013-09-11 09:51:14 -07002550 } else {
Makoto Onuki4becc352019-01-30 13:30:41 -08002551 opPackageName = mBasePackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002552 }
2553 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002554
Makoto Onuki4becc352019-01-30 13:30:41 -08002555 mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName;
2556
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002557 mContentResolver = new ApplicationContentResolver(this, mainThread);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 }
2559
Jason Monkbd983642017-02-24 16:57:40 -05002560 void setResources(Resources r) {
Jason Monkbd60e5b2017-03-02 12:55:00 -05002561 if (r instanceof CompatResources) {
2562 ((CompatResources) r).setContext(this);
Jason Monkbd983642017-02-24 16:57:40 -05002563 }
Jason Monkbd60e5b2017-03-02 12:55:00 -05002564 mResources = r;
Jason Monkbd983642017-02-24 16:57:40 -05002565 }
2566
Narayan Kamath29564cd2014-08-07 10:57:40 +01002567 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Todd Kennedy233a0b12018-01-29 20:30:24 +00002568 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
2570
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002571 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 final void scheduleFinalCleanup(String who, String what) {
2573 mMainThread.scheduleContextCleanup(this, who, what);
2574 }
2575
2576 final void performFinalCleanup(String who, String what) {
2577 //Log.i(TAG, "Cleanup up context: " + this);
Todd Kennedy233a0b12018-01-29 20:30:24 +00002578 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 }
2580
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002581 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 final Context getReceiverRestrictedContext() {
2583 if (mReceiverRestrictedContext != null) {
2584 return mReceiverRestrictedContext;
2585 }
2586 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2587 }
2588
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002589 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 final void setOuterContext(Context context) {
2591 mOuterContext = context;
2592 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002593
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002594 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 final Context getOuterContext() {
2596 return mOuterContext;
2597 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002598
Tony Mak46aabe52016-11-14 12:53:06 +00002599 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002600 @UnsupportedAppUsage
Tony Mak46aabe52016-11-14 12:53:06 +00002601 public IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 return mActivityToken;
2603 }
2604
Jeff Sharkey634dc422016-01-30 17:44:15 -07002605 private void checkMode(int mode) {
2606 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2607 if ((mode & MODE_WORLD_READABLE) != 0) {
2608 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2609 }
2610 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2611 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2612 }
2613 }
2614 }
2615
Jeff Brown6e539312015-02-24 18:53:21 -08002616 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002617 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 int extraPermissions) {
2619 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2620 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2621 |extraPermissions;
2622 if ((mode&MODE_WORLD_READABLE) != 0) {
2623 perms |= FileUtils.S_IROTH;
2624 }
2625 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2626 perms |= FileUtils.S_IWOTH;
2627 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002628 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2630 + ", perms=0x" + Integer.toHexString(perms));
2631 }
2632 FileUtils.setPermissions(name, perms, -1, -1);
2633 }
2634
2635 private File makeFilename(File base, String name) {
2636 if (name.indexOf(File.separatorChar) < 0) {
Jeff Sharkeydd02e332018-06-27 14:41:57 -06002637 final File res = new File(base, name);
2638 // We report as filesystem access here to give us the best shot at
2639 // detecting apps that will pass the path down to native code.
2640 BlockGuard.getVmPolicy().onPathAccess(res.getPath());
2641 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 }
2643 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002644 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
2646
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002647 /**
2648 * Ensure that given directories exist, trying to create them if missing. If
2649 * unable to create, they are filtered by replacing with {@code null}.
2650 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002651 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkeyae266462017-11-27 13:32:24 -07002652 final StorageManager sm = getSystemService(StorageManager.class);
2653 final File[] result = new File[dirs.length];
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002654 for (int i = 0; i < dirs.length; i++) {
2655 File dir = dirs[i];
2656 if (!dir.exists()) {
2657 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002658 // recheck existence in case of cross-process race
2659 if (!dir.exists()) {
2660 // Failing to mkdir() may be okay, since we might not have
2661 // enough permissions; ask vold to create on our behalf.
Christopher Tatecc866da2013-10-02 18:11:01 -07002662 try {
Jeff Sharkeyae266462017-11-27 13:32:24 -07002663 sm.mkdirs(dir);
Jeff Sharkey983294592015-07-13 10:25:31 -07002664 } catch (Exception e) {
2665 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002666 dir = null;
2667 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002668 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002669 }
2670 }
2671 result[i] = dir;
2672 }
2673 return result;
2674 }
2675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 // ----------------------------------------------------------------------
2677 // ----------------------------------------------------------------------
2678 // ----------------------------------------------------------------------
2679
2680 private static final class ApplicationContentResolver extends ContentResolver {
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002681 @UnsupportedAppUsage
Jeff Sharkey6d515712012-09-20 16:06:08 -07002682 private final ActivityThread mMainThread;
Jeff Sharkey6d515712012-09-20 16:06:08 -07002683
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002684 public ApplicationContentResolver(Context context, ActivityThread mainThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002686 mMainThread = Preconditions.checkNotNull(mainThread);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 }
2688
2689 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002690 @UnsupportedAppUsage
Jeff Sharkey6d515712012-09-20 16:06:08 -07002691 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002692 return mMainThread.acquireProvider(context,
2693 ContentProvider.getAuthorityWithoutUserId(auth),
2694 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 }
2696
2697 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002698 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002699 return mMainThread.acquireExistingProvider(context,
2700 ContentProvider.getAuthorityWithoutUserId(auth),
2701 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002702 }
2703
2704 @Override
2705 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002706 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002708
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002709 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002710 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002711 return mMainThread.acquireProvider(c,
2712 ContentProvider.getAuthorityWithoutUserId(auth),
2713 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002714 }
2715
2716 @Override
2717 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002718 return mMainThread.releaseProvider(icp, false);
2719 }
2720
2721 @Override
2722 public void unstableProviderDied(IContentProvider icp) {
2723 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002724 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002725
2726 @Override
2727 public void appNotRespondingViaProvider(IContentProvider icp) {
2728 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2729 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002730
2731 /** @hide */
2732 protected int resolveUserIdFromAuthority(String auth) {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002733 return ContentProvider.getUserIdFromAuthority(auth, getUserId());
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736}