The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
Artur Satayev | c895b1b | 2019-12-10 17:47:51 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 23 | import android.content.AutofillOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.ComponentName; |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 26 | import android.content.ContentCaptureOptions; |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 27 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.ContentResolver; |
| 29 | import android.content.Context; |
| 30 | import android.content.ContextWrapper; |
| 31 | import android.content.IContentProvider; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 32 | import android.content.IIntentReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 35 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.ReceiverCallNotAllowedException; |
| 37 | import android.content.ServiceConnection; |
| 38 | import android.content.SharedPreferences; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 39 | import android.content.pm.ActivityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 43 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.content.res.AssetManager; |
Jason Monk | bd98364 | 2017-02-24 16:57:40 -0500 | [diff] [blame] | 45 | import android.content.res.CompatResources; |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 46 | import android.content.res.CompatibilityInfo; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 47 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.content.res.Resources; |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 49 | import android.content.res.loader.ResourcesLoader; |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 50 | import android.database.DatabaseErrorHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.database.sqlite.SQLiteDatabase; |
| 52 | import android.database.sqlite.SQLiteDatabase.CursorFactory; |
| 53 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.os.Binder; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 57 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.os.Bundle; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 59 | import android.os.Debug; |
Oscar Montemayor | 539d3c4 | 2010-01-29 15:27:00 -0800 | [diff] [blame] | 60 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.os.FileUtils; |
| 62 | import android.os.Handler; |
| 63 | import android.os.IBinder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 64 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 66 | import android.os.RemoteException; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 67 | import android.os.Trace; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 68 | import android.os.UserHandle; |
Jeff Sharkey | a65e649 | 2017-06-21 13:45:11 -0600 | [diff] [blame] | 69 | import android.os.UserManager; |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 70 | import android.os.storage.StorageManager; |
Todd Kennedy | 8f13598 | 2019-07-02 07:35:15 -0700 | [diff] [blame] | 71 | import android.permission.IPermissionManager; |
Jeff Sharkey | e84bdd3 | 2016-02-08 12:16:00 -0700 | [diff] [blame] | 72 | import android.system.ErrnoException; |
| 73 | import android.system.Os; |
| 74 | import android.system.OsConstants; |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 75 | import android.system.StructStat; |
Jordan Liu | 5473553 | 2019-10-29 10:33:55 -0700 | [diff] [blame] | 76 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import android.util.AndroidRuntimeException; |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 78 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import android.util.Log; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 80 | import android.util.Slog; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 81 | import android.view.Display; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 82 | import android.view.DisplayAdjustments; |
Felipe Leme | bb567ae | 2017-10-04 09:56:21 -0700 | [diff] [blame] | 83 | import android.view.autofill.AutofillManager.AutofillClient; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 85 | import com.android.internal.annotations.GuardedBy; |
| 86 | import com.android.internal.util.Preconditions; |
| 87 | |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 88 | import dalvik.system.BlockGuard; |
| 89 | |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 90 | import libcore.io.Memory; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 91 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import java.io.File; |
| 93 | import java.io.FileInputStream; |
| 94 | import java.io.FileNotFoundException; |
| 95 | import java.io.FileOutputStream; |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 96 | import java.io.FilenameFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import java.io.IOException; |
| 98 | import java.io.InputStream; |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 99 | import java.lang.annotation.Retention; |
| 100 | import java.lang.annotation.RetentionPolicy; |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 101 | import java.nio.ByteOrder; |
Felka Chang | 0d824d4 | 2019-11-19 15:06:52 +0800 | [diff] [blame] | 102 | import java.nio.file.Path; |
Christopher Tate | fe2368c | 2017-05-17 15:42:35 -0700 | [diff] [blame] | 103 | import java.util.ArrayList; |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 104 | import java.util.List; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 105 | import java.util.Objects; |
Jeff Sharkey | 8439ac0 | 2017-12-12 17:26:23 -0700 | [diff] [blame] | 106 | import java.util.concurrent.Executor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | class ReceiverRestrictedContext extends ContextWrapper { |
Andrei Onea | 4b2116c | 2019-02-28 15:13:42 +0000 | [diff] [blame] | 109 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | ReceiverRestrictedContext(Context base) { |
| 111 | super(base); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 116 | return registerReceiver(receiver, filter, null, null); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 121 | String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 122 | if (receiver == null) { |
| 123 | // Allow retrieving current sticky broadcast; this is safe since we |
| 124 | // aren't actually registering a receiver. |
| 125 | return super.registerReceiver(null, filter, broadcastPermission, scheduler); |
| 126 | } else { |
| 127 | throw new ReceiverCallNotAllowedException( |
| 128 | "BroadcastReceiver components are not allowed to register to receive intents"); |
| 129 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Meng Wang | 3f9e036 | 2019-11-05 10:43:45 -0800 | [diff] [blame] | 133 | public Intent registerReceiverForAllUsers(BroadcastReceiver receiver, IntentFilter filter, |
| 134 | String broadcastPermission, Handler scheduler) { |
| 135 | return registerReceiverAsUser( |
| 136 | receiver, UserHandle.ALL, filter, broadcastPermission, scheduler); |
| 137 | } |
| 138 | |
| 139 | @Override |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 140 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 141 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 142 | if (receiver == null) { |
| 143 | // Allow retrieving current sticky broadcast; this is safe since we |
| 144 | // aren't actually registering a receiver. |
| 145 | return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler); |
| 146 | } else { |
| 147 | throw new ReceiverCallNotAllowedException( |
| 148 | "BroadcastReceiver components are not allowed to register to receive intents"); |
| 149 | } |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 154 | throw new ReceiverCallNotAllowedException( |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 155 | "BroadcastReceiver components are not allowed to bind to services"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 157 | |
| 158 | @Override |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 159 | public boolean bindService( |
| 160 | Intent service, int flags, Executor executor, ServiceConnection conn) { |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 161 | throw new ReceiverCallNotAllowedException( |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 162 | "BroadcastReceiver components are not allowed to bind to services"); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public boolean bindIsolatedService(Intent service, int flags, String instanceName, |
| 167 | Executor executor, ServiceConnection conn) { |
| 168 | throw new ReceiverCallNotAllowedException( |
| 169 | "BroadcastReceiver components are not allowed to bind to services"); |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 170 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 174 | * Common implementation of Context API, which provides the base |
| 175 | * context object for Activity and other application components. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | */ |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 177 | class ContextImpl extends Context { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 178 | private final static String TAG = "ContextImpl"; |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 179 | private final static boolean DEBUG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 181 | private static final String XATTR_INODE_CACHE = "user.inode_cache"; |
| 182 | private static final String XATTR_INODE_CODE_CACHE = "user.inode_code_cache"; |
| 183 | |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Map from package name, to preference name, to cached preferences. |
| 186 | */ |
Jeff Sharkey | be78258 | 2016-02-15 18:35:57 -0700 | [diff] [blame] | 187 | @GuardedBy("ContextImpl.class") |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 188 | @UnsupportedAppUsage |
Jeff Sharkey | be78258 | 2016-02-15 18:35:57 -0700 | [diff] [blame] | 189 | private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache; |
| 190 | |
| 191 | /** |
| 192 | * Map from preference name to generated path. |
| 193 | */ |
| 194 | @GuardedBy("ContextImpl.class") |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 195 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Sharkey | be78258 | 2016-02-15 18:35:57 -0700 | [diff] [blame] | 196 | private ArrayMap<String, File> mSharedPrefsPaths; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 198 | @UnsupportedAppUsage |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 199 | final @NonNull ActivityThread mMainThread; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 200 | @UnsupportedAppUsage |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 201 | final @NonNull LoadedApk mPackageInfo; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 202 | @UnsupportedAppUsage |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 203 | private @Nullable ClassLoader mClassLoader; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 204 | |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 205 | private final @Nullable IBinder mToken; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 206 | |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 207 | private final @NonNull UserHandle mUser; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 208 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 209 | @UnsupportedAppUsage |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 210 | private final ApplicationContentResolver mContentResolver; |
| 211 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 212 | @UnsupportedAppUsage |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 213 | private final String mBasePackageName; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 214 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 215 | private final String mOpPackageName; |
| 216 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 217 | /** If of feature this context is for */ |
| 218 | private final @Nullable String mFeatureId; |
| 219 | |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 220 | private final @NonNull ResourcesManager mResourcesManager; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 221 | @UnsupportedAppUsage |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 222 | private @NonNull Resources mResources; |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 223 | private @Nullable Display mDisplay; // may be null if invalid display or not initialized yet. |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 224 | |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 225 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 226 | private final int mFlags; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 227 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 228 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | private Context mOuterContext; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 230 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | private int mThemeResource = 0; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 232 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | private Resources.Theme mTheme = null; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 234 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | private PackageManager mPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | private Context mReceiverRestrictedContext = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 238 | // The name of the split this Context is representing. May be null. |
| 239 | private @Nullable String mSplitName = null; |
| 240 | |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 241 | private @Nullable AutofillClient mAutofillClient = null; |
| 242 | private @Nullable AutofillOptions mAutofillOptions; |
Felipe Leme | bb567ae | 2017-10-04 09:56:21 -0700 | [diff] [blame] | 243 | |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 244 | private ContentCaptureOptions mContentCaptureOptions = null; |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | private final Object mSync = new Object(); |
| 247 | |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 248 | private boolean mIsSystemOrSystemUiContext; |
| 249 | private boolean mIsUiContext; |
| 250 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 251 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | private File mDatabasesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 253 | @GuardedBy("mSync") |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 254 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | private File mPreferencesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 256 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | private File mFilesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 258 | @GuardedBy("mSync") |
Felka Chang | 0d824d4 | 2019-11-19 15:06:52 +0800 | [diff] [blame] | 259 | private File mCratesDir; |
| 260 | @GuardedBy("mSync") |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 261 | private File mNoBackupFilesDir; |
| 262 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | private File mCacheDir; |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 264 | @GuardedBy("mSync") |
| 265 | private File mCodeCacheDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 266 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 267 | // The system service cache for the system services that are cached per-ContextImpl. |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 268 | @UnsupportedAppUsage |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 269 | final Object[] mServiceCache = SystemServiceRegistry.createServiceCache(); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 270 | |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 271 | static final int STATE_UNINITIALIZED = 0; |
| 272 | static final int STATE_INITIALIZING = 1; |
| 273 | static final int STATE_READY = 2; |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 274 | static final int STATE_NOT_FOUND = 3; |
| 275 | |
| 276 | /** @hide */ |
| 277 | @IntDef(prefix = { "STATE_" }, value = { |
| 278 | STATE_UNINITIALIZED, |
| 279 | STATE_INITIALIZING, |
| 280 | STATE_READY, |
| 281 | STATE_NOT_FOUND, |
| 282 | }) |
| 283 | @Retention(RetentionPolicy.SOURCE) |
| 284 | @interface ServiceInitializationState {} |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 285 | |
| 286 | /** |
| 287 | * Initialization state for each service. Any of {@link #STATE_UNINITIALIZED}, |
| 288 | * {@link #STATE_INITIALIZING} or {@link #STATE_READY}, |
| 289 | */ |
Makoto Onuki | d67070e | 2018-03-30 12:39:14 -0700 | [diff] [blame] | 290 | @ServiceInitializationState |
| 291 | final int[] mServiceInitializationStateArray = new int[mServiceCache.length]; |
Makoto Onuki | f896f12 | 2018-01-25 09:50:24 -0800 | [diff] [blame] | 292 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 293 | @UnsupportedAppUsage |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 294 | static ContextImpl getImpl(Context context) { |
| 295 | Context nextContext; |
| 296 | while ((context instanceof ContextWrapper) && |
| 297 | (nextContext=((ContextWrapper)context).getBaseContext()) != null) { |
| 298 | context = nextContext; |
| 299 | } |
| 300 | return (ContextImpl)context; |
| 301 | } |
| 302 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | @Override |
| 304 | public AssetManager getAssets() { |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 305 | return getResources().getAssets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public Resources getResources() { |
| 310 | return mResources; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public PackageManager getPackageManager() { |
| 315 | if (mPackageManager != null) { |
| 316 | return mPackageManager; |
| 317 | } |
| 318 | |
Todd Kennedy | 8f13598 | 2019-07-02 07:35:15 -0700 | [diff] [blame] | 319 | final IPackageManager pm = ActivityThread.getPackageManager(); |
| 320 | final IPermissionManager permissionManager = ActivityThread.getPermissionManager(); |
| 321 | if (pm != null && permissionManager != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | // Doesn't matter if we make more than one instance. |
Todd Kennedy | 8f13598 | 2019-07-02 07:35:15 -0700 | [diff] [blame] | 323 | return (mPackageManager = new ApplicationPackageManager(this, pm, permissionManager)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public ContentResolver getContentResolver() { |
| 331 | return mContentResolver; |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public Looper getMainLooper() { |
| 336 | return mMainThread.getLooper(); |
| 337 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | @Override |
Jeff Sharkey | 8439ac0 | 2017-12-12 17:26:23 -0700 | [diff] [blame] | 340 | public Executor getMainExecutor() { |
| 341 | return mMainThread.getExecutor(); |
| 342 | } |
| 343 | |
| 344 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | public Context getApplicationContext() { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 346 | return (mPackageInfo != null) ? |
| 347 | mPackageInfo.getApplication() : mMainThread.getApplication(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | @Override |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 351 | public void setTheme(int resId) { |
Adam Lesinski | 1e87a36 | 2017-06-12 12:22:10 -0700 | [diff] [blame] | 352 | synchronized (mSync) { |
| 353 | if (mThemeResource != resId) { |
| 354 | mThemeResource = resId; |
| 355 | initializeTheme(); |
| 356 | } |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 357 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | @Override |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 361 | public int getThemeResId() { |
Adam Lesinski | 1e87a36 | 2017-06-12 12:22:10 -0700 | [diff] [blame] | 362 | synchronized (mSync) { |
| 363 | return mThemeResource; |
| 364 | } |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | public Resources.Theme getTheme() { |
Adam Lesinski | 1e87a36 | 2017-06-12 12:22:10 -0700 | [diff] [blame] | 369 | synchronized (mSync) { |
| 370 | if (mTheme != null) { |
| 371 | return mTheme; |
| 372 | } |
| 373 | |
| 374 | mThemeResource = Resources.selectDefaultTheme(mThemeResource, |
| 375 | getOuterContext().getApplicationInfo().targetSdkVersion); |
| 376 | initializeTheme(); |
| 377 | |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 378 | return mTheme; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 382 | private void initializeTheme() { |
| 383 | if (mTheme == null) { |
| 384 | mTheme = mResources.newTheme(); |
| 385 | } |
| 386 | mTheme.applyStyle(mThemeResource, true); |
| 387 | } |
| 388 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | @Override |
| 390 | public ClassLoader getClassLoader() { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 391 | return mClassLoader != null ? mClassLoader : (mPackageInfo != null ? mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public String getPackageName() { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 396 | if (mPackageInfo != null) { |
| 397 | return mPackageInfo.getPackageName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | } |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 399 | // No mPackageInfo means this is a Context for the system itself, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 400 | // and this here is its name. |
| 401 | return "android"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 404 | /** @hide */ |
| 405 | @Override |
| 406 | public String getBasePackageName() { |
| 407 | return mBasePackageName != null ? mBasePackageName : getPackageName(); |
| 408 | } |
| 409 | |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 410 | /** @hide */ |
| 411 | @Override |
| 412 | public String getOpPackageName() { |
| 413 | return mOpPackageName != null ? mOpPackageName : getBasePackageName(); |
| 414 | } |
| 415 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 416 | /** @hide */ |
| 417 | @Override |
| 418 | public @Nullable String getFeatureId() { |
| 419 | return mFeatureId; |
| 420 | } |
| 421 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | @Override |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 423 | public ApplicationInfo getApplicationInfo() { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 424 | if (mPackageInfo != null) { |
| 425 | return mPackageInfo.getApplicationInfo(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 426 | } |
| 427 | throw new RuntimeException("Not supported in system context"); |
| 428 | } |
| 429 | |
| 430 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | public String getPackageResourcePath() { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 432 | if (mPackageInfo != null) { |
| 433 | return mPackageInfo.getResDir(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | throw new RuntimeException("Not supported in system context"); |
| 436 | } |
| 437 | |
| 438 | @Override |
| 439 | public String getPackageCodePath() { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 440 | if (mPackageInfo != null) { |
| 441 | return mPackageInfo.getAppDir(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | } |
| 443 | throw new RuntimeException("Not supported in system context"); |
| 444 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 445 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 446 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | public SharedPreferences getSharedPreferences(String name, int mode) { |
Jeff Sharkey | 8fc29cf | 2015-11-30 17:51:00 -0700 | [diff] [blame] | 448 | // At least one application in the world actually passes in a null |
| 449 | // name. This happened to work because when we generated the file name |
| 450 | // we would stringify it to "null.xml". Nice. |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 451 | if (mPackageInfo.getApplicationInfo().targetSdkVersion < |
Jeff Sharkey | 8fc29cf | 2015-11-30 17:51:00 -0700 | [diff] [blame] | 452 | Build.VERSION_CODES.KITKAT) { |
| 453 | if (name == null) { |
| 454 | name = "null"; |
| 455 | } |
| 456 | } |
| 457 | |
Jeff Sharkey | be78258 | 2016-02-15 18:35:57 -0700 | [diff] [blame] | 458 | File file; |
| 459 | synchronized (ContextImpl.class) { |
| 460 | if (mSharedPrefsPaths == null) { |
| 461 | mSharedPrefsPaths = new ArrayMap<>(); |
| 462 | } |
| 463 | file = mSharedPrefsPaths.get(name); |
| 464 | if (file == null) { |
| 465 | file = getSharedPreferencesPath(name); |
| 466 | mSharedPrefsPaths.put(name, file); |
| 467 | } |
| 468 | } |
Jeff Sharkey | 8fc29cf | 2015-11-30 17:51:00 -0700 | [diff] [blame] | 469 | return getSharedPreferences(file, mode); |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public SharedPreferences getSharedPreferences(File file, int mode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | SharedPreferencesImpl sp; |
Dianne Hackborn | f691359 | 2013-09-05 13:21:24 -0700 | [diff] [blame] | 475 | synchronized (ContextImpl.class) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 476 | final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked(); |
| 477 | sp = cache.get(file); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 478 | if (sp == null) { |
John Reck | f3903e9 | 2017-05-12 17:12:54 -0700 | [diff] [blame] | 479 | checkMode(mode); |
| 480 | if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.O) { |
| 481 | if (isCredentialProtectedStorage() |
Jeff Sharkey | a65e649 | 2017-06-21 13:45:11 -0600 | [diff] [blame] | 482 | && !getSystemService(UserManager.class) |
| 483 | .isUserUnlockingOrUnlocked(UserHandle.myUserId())) { |
John Reck | f3903e9 | 2017-05-12 17:12:54 -0700 | [diff] [blame] | 484 | throw new IllegalStateException("SharedPreferences in credential encrypted " |
| 485 | + "storage are not available until after user is unlocked"); |
| 486 | } |
| 487 | } |
Jeff Sharkey | 8fc29cf | 2015-11-30 17:51:00 -0700 | [diff] [blame] | 488 | sp = new SharedPreferencesImpl(file, mode); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 489 | cache.put(file, sp); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 490 | return sp; |
| 491 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | } |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 493 | if ((mode & Context.MODE_MULTI_PROCESS) != 0 || |
| 494 | getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { |
| 495 | // If somebody else (some other process) changed the prefs |
| 496 | // file behind our back, we reload it. This has been the |
| 497 | // historical (if undocumented) behavior. |
| 498 | sp.startReloadIfChangedUnexpectedly(); |
| 499 | } |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 500 | return sp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 503 | @GuardedBy("ContextImpl.class") |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 504 | private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() { |
Jeff Sharkey | be78258 | 2016-02-15 18:35:57 -0700 | [diff] [blame] | 505 | if (sSharedPrefsCache == null) { |
| 506 | sSharedPrefsCache = new ArrayMap<>(); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | final String packageName = getPackageName(); |
Jeff Sharkey | be78258 | 2016-02-15 18:35:57 -0700 | [diff] [blame] | 510 | ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 511 | if (packagePrefs == null) { |
| 512 | packagePrefs = new ArrayMap<>(); |
Jeff Sharkey | be78258 | 2016-02-15 18:35:57 -0700 | [diff] [blame] | 513 | sSharedPrefsCache.put(packageName, packagePrefs); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return packagePrefs; |
| 517 | } |
| 518 | |
Christopher Tate | fe2368c | 2017-05-17 15:42:35 -0700 | [diff] [blame] | 519 | @Override |
| 520 | public void reloadSharedPreferences() { |
| 521 | // Build the list of all per-context impls (i.e. caches) we know about |
| 522 | ArrayList<SharedPreferencesImpl> spImpls = new ArrayList<>(); |
| 523 | synchronized (ContextImpl.class) { |
| 524 | final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked(); |
| 525 | for (int i = 0; i < cache.size(); i++) { |
| 526 | final SharedPreferencesImpl sp = cache.valueAt(i); |
| 527 | if (sp != null) { |
| 528 | spImpls.add(sp); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // Issue the reload outside the cache lock |
| 534 | for (int i = 0; i < spImpls.size(); i++) { |
| 535 | spImpls.get(i).startReloadIfChangedUnexpectedly(); |
| 536 | } |
| 537 | } |
| 538 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 539 | /** |
| 540 | * Try our best to migrate all files from source to target that match |
Jeff Sharkey | 390f2ed | 2016-03-01 15:25:03 -0700 | [diff] [blame] | 541 | * requested prefix. |
| 542 | * |
| 543 | * @return the number of files moved, or -1 if there was trouble. |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 544 | */ |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 545 | private static int moveFiles(File sourceDir, File targetDir, final String prefix) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 546 | final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() { |
| 547 | @Override |
| 548 | public boolean accept(File dir, String name) { |
| 549 | return name.startsWith(prefix); |
| 550 | } |
| 551 | }); |
| 552 | |
Jeff Sharkey | 390f2ed | 2016-03-01 15:25:03 -0700 | [diff] [blame] | 553 | int res = 0; |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 554 | for (File sourceFile : sourceFiles) { |
| 555 | final File targetFile = new File(targetDir, sourceFile.getName()); |
| 556 | Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile); |
| 557 | try { |
| 558 | FileUtils.copyFileOrThrow(sourceFile, targetFile); |
| 559 | FileUtils.copyPermissions(sourceFile, targetFile); |
| 560 | if (!sourceFile.delete()) { |
| 561 | throw new IOException("Failed to clean up " + sourceFile); |
| 562 | } |
Jeff Sharkey | 390f2ed | 2016-03-01 15:25:03 -0700 | [diff] [blame] | 563 | if (res != -1) { |
| 564 | res++; |
| 565 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 566 | } catch (IOException e) { |
| 567 | Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e); |
Jeff Sharkey | 390f2ed | 2016-03-01 15:25:03 -0700 | [diff] [blame] | 568 | res = -1; |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | return res; |
| 572 | } |
| 573 | |
| 574 | @Override |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 575 | public boolean moveSharedPreferencesFrom(Context sourceContext, String name) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 576 | synchronized (ContextImpl.class) { |
| 577 | final File source = sourceContext.getSharedPreferencesPath(name); |
| 578 | final File target = getSharedPreferencesPath(name); |
| 579 | |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 580 | final int res = moveFiles(source.getParentFile(), target.getParentFile(), |
Jeff Sharkey | 390f2ed | 2016-03-01 15:25:03 -0700 | [diff] [blame] | 581 | source.getName()); |
| 582 | if (res > 0) { |
| 583 | // We moved at least one file, so evict any in-memory caches for |
| 584 | // either location |
| 585 | final ArrayMap<File, SharedPreferencesImpl> cache = |
| 586 | getSharedPreferencesCacheLocked(); |
| 587 | cache.remove(source); |
| 588 | cache.remove(target); |
| 589 | } |
| 590 | return res != -1; |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public boolean deleteSharedPreferences(String name) { |
| 596 | synchronized (ContextImpl.class) { |
| 597 | final File prefs = getSharedPreferencesPath(name); |
| 598 | final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs); |
| 599 | |
| 600 | // Evict any in-memory caches |
| 601 | final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked(); |
| 602 | cache.remove(prefs); |
| 603 | |
| 604 | prefs.delete(); |
| 605 | prefsBackup.delete(); |
| 606 | |
| 607 | // We failed if files are still lingering |
| 608 | return !(prefs.exists() || prefsBackup.exists()); |
| 609 | } |
| 610 | } |
| 611 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 612 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | private File getPreferencesDir() { |
| 614 | synchronized (mSync) { |
| 615 | if (mPreferencesDir == null) { |
Jeff Sharkey | 2c1ba9a | 2016-02-17 15:29:38 -0700 | [diff] [blame] | 616 | mPreferencesDir = new File(getDataDir(), "shared_prefs"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 618 | return ensurePrivateDirExists(mPreferencesDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public FileInputStream openFileInput(String name) |
| 624 | throws FileNotFoundException { |
| 625 | File f = makeFilename(getFilesDir(), name); |
| 626 | return new FileInputStream(f); |
| 627 | } |
| 628 | |
| 629 | @Override |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 630 | public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException { |
| 631 | checkMode(mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | final boolean append = (mode&MODE_APPEND) != 0; |
| 633 | File f = makeFilename(getFilesDir(), name); |
| 634 | try { |
| 635 | FileOutputStream fos = new FileOutputStream(f, append); |
| 636 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 637 | return fos; |
| 638 | } catch (FileNotFoundException e) { |
| 639 | } |
| 640 | |
| 641 | File parent = f.getParentFile(); |
| 642 | parent.mkdir(); |
| 643 | FileUtils.setPermissions( |
| 644 | parent.getPath(), |
| 645 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 646 | -1, -1); |
| 647 | FileOutputStream fos = new FileOutputStream(f, append); |
| 648 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 649 | return fos; |
| 650 | } |
| 651 | |
| 652 | @Override |
| 653 | public boolean deleteFile(String name) { |
| 654 | File f = makeFilename(getFilesDir(), name); |
| 655 | return f.delete(); |
| 656 | } |
| 657 | |
Jeff Sharkey | e84bdd3 | 2016-02-08 12:16:00 -0700 | [diff] [blame] | 658 | /** |
| 659 | * Common-path handling of app data dir creation |
| 660 | */ |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 661 | private static File ensurePrivateDirExists(File file) { |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 662 | return ensurePrivateDirExists(file, 0771, -1, null); |
Jeff Sharkey | 5eb3eb5 | 2016-12-13 08:44:51 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 665 | private static File ensurePrivateCacheDirExists(File file, String xattr) { |
Jeff Sharkey | 5eb3eb5 | 2016-12-13 08:44:51 -0700 | [diff] [blame] | 666 | final int gid = UserHandle.getCacheAppGid(Process.myUid()); |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 667 | return ensurePrivateDirExists(file, 02771, gid, xattr); |
Jeff Sharkey | 5eb3eb5 | 2016-12-13 08:44:51 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 670 | private static File ensurePrivateDirExists(File file, int mode, int gid, String xattr) { |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 671 | if (!file.exists()) { |
Jeff Sharkey | 5eb3eb5 | 2016-12-13 08:44:51 -0700 | [diff] [blame] | 672 | final String path = file.getAbsolutePath(); |
Jeff Sharkey | e84bdd3 | 2016-02-08 12:16:00 -0700 | [diff] [blame] | 673 | try { |
Jeff Sharkey | 5eb3eb5 | 2016-12-13 08:44:51 -0700 | [diff] [blame] | 674 | Os.mkdir(path, mode); |
| 675 | Os.chmod(path, mode); |
| 676 | if (gid != -1) { |
| 677 | Os.chown(path, -1, gid); |
| 678 | } |
Jeff Sharkey | e84bdd3 | 2016-02-08 12:16:00 -0700 | [diff] [blame] | 679 | } catch (ErrnoException e) { |
| 680 | if (e.errno == OsConstants.EEXIST) { |
| 681 | // We must have raced with someone; that's okay |
| 682 | } else { |
| 683 | Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage()); |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 684 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 685 | } |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 686 | |
| 687 | if (xattr != null) { |
| 688 | try { |
| 689 | final StructStat stat = Os.stat(file.getAbsolutePath()); |
| 690 | final byte[] value = new byte[8]; |
| 691 | Memory.pokeLong(value, 0, stat.st_ino, ByteOrder.nativeOrder()); |
| 692 | Os.setxattr(file.getParentFile().getAbsolutePath(), xattr, value, 0); |
| 693 | } catch (ErrnoException e) { |
| 694 | Log.w(TAG, "Failed to update " + xattr + ": " + e.getMessage()); |
| 695 | } |
| 696 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 697 | } |
| 698 | return file; |
| 699 | } |
| 700 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | @Override |
| 702 | public File getFilesDir() { |
| 703 | synchronized (mSync) { |
| 704 | if (mFilesDir == null) { |
Jeff Sharkey | 2c1ba9a | 2016-02-17 15:29:38 -0700 | [diff] [blame] | 705 | mFilesDir = new File(getDataDir(), "files"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 707 | return ensurePrivateDirExists(mFilesDir); |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | @Override |
Felka Chang | 0d824d4 | 2019-11-19 15:06:52 +0800 | [diff] [blame] | 712 | public File getCrateDir(@NonNull String crateId) { |
| 713 | Preconditions.checkArgument(FileUtils.isValidExtFilename(crateId), "invalidated crateId"); |
| 714 | final Path cratesRootPath = getDataDir().toPath().resolve("crates"); |
| 715 | final Path absoluteNormalizedCratePath = cratesRootPath.resolve(crateId) |
| 716 | .toAbsolutePath().normalize(); |
| 717 | |
| 718 | synchronized (mSync) { |
| 719 | if (mCratesDir == null) { |
| 720 | mCratesDir = cratesRootPath.toFile(); |
| 721 | } |
| 722 | ensurePrivateDirExists(mCratesDir); |
| 723 | } |
| 724 | |
| 725 | File cratedDir = absoluteNormalizedCratePath.toFile(); |
| 726 | return ensurePrivateDirExists(cratedDir); |
| 727 | } |
| 728 | |
| 729 | @Override |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 730 | public File getNoBackupFilesDir() { |
| 731 | synchronized (mSync) { |
| 732 | if (mNoBackupFilesDir == null) { |
Jeff Sharkey | 2c1ba9a | 2016-02-17 15:29:38 -0700 | [diff] [blame] | 733 | mNoBackupFilesDir = new File(getDataDir(), "no_backup"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 735 | return ensurePrivateDirExists(mNoBackupFilesDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 738 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | @Override |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 740 | public File getExternalFilesDir(String type) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 741 | // Operates on primary external storage |
Christopher Tate | 11179b4 | 2017-07-13 15:18:38 -0700 | [diff] [blame] | 742 | final File[] dirs = getExternalFilesDirs(type); |
| 743 | return (dirs != null && dirs.length > 0) ? dirs[0] : null; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | @Override |
| 747 | public File[] getExternalFilesDirs(String type) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 748 | synchronized (mSync) { |
Jeff Sharkey | 4a2b119 | 2016-06-27 17:43:15 -0600 | [diff] [blame] | 749 | File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName()); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 750 | if (type != null) { |
| 751 | dirs = Environment.buildPaths(dirs, type); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 752 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 753 | return ensureExternalDirsExistOrFilter(dirs); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 756 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 757 | @Override |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 758 | public File getObbDir() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 759 | // Operates on primary external storage |
Christopher Tate | 11179b4 | 2017-07-13 15:18:38 -0700 | [diff] [blame] | 760 | final File[] dirs = getObbDirs(); |
| 761 | return (dirs != null && dirs.length > 0) ? dirs[0] : null; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public File[] getObbDirs() { |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 766 | synchronized (mSync) { |
Jeff Sharkey | 4a2b119 | 2016-06-27 17:43:15 -0600 | [diff] [blame] | 767 | File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName()); |
| 768 | return ensureExternalDirsExistOrFilter(dirs); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
| 772 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | public File getCacheDir() { |
| 774 | synchronized (mSync) { |
| 775 | if (mCacheDir == null) { |
Jeff Sharkey | 2c1ba9a | 2016-02-17 15:29:38 -0700 | [diff] [blame] | 776 | mCacheDir = new File(getDataDir(), "cache"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | } |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 778 | return ensurePrivateCacheDirExists(mCacheDir, XATTR_INODE_CACHE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 781 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 782 | @Override |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 783 | public File getCodeCacheDir() { |
| 784 | synchronized (mSync) { |
| 785 | if (mCodeCacheDir == null) { |
Alex Light | 2d0691c | 2019-10-03 14:36:57 -0700 | [diff] [blame] | 786 | mCodeCacheDir = getCodeCacheDirBeforeBind(getDataDir()); |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 787 | } |
Jeff Sharkey | 83aacde | 2017-03-22 23:13:39 -0600 | [diff] [blame] | 788 | return ensurePrivateCacheDirExists(mCodeCacheDir, XATTR_INODE_CODE_CACHE); |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Alex Light | 2d0691c | 2019-10-03 14:36:57 -0700 | [diff] [blame] | 792 | /** |
| 793 | * Helper for getting code-cache dir potentially before application bind. |
| 794 | * |
| 795 | * @hide |
| 796 | */ |
| 797 | static File getCodeCacheDirBeforeBind(File dataDir) { |
| 798 | return new File(dataDir, "code_cache"); |
| 799 | } |
| 800 | |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 801 | @Override |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 802 | public File getExternalCacheDir() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 803 | // Operates on primary external storage |
Christopher Tate | 11179b4 | 2017-07-13 15:18:38 -0700 | [diff] [blame] | 804 | final File[] dirs = getExternalCacheDirs(); |
| 805 | return (dirs != null && dirs.length > 0) ? dirs[0] : null; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | @Override |
| 809 | public File[] getExternalCacheDirs() { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 810 | synchronized (mSync) { |
Jeff Sharkey | 4a2b119 | 2016-06-27 17:43:15 -0600 | [diff] [blame] | 811 | File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName()); |
| 812 | return ensureExternalDirsExistOrFilter(dirs); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 813 | } |
| 814 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 815 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | @Override |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 817 | public File[] getExternalMediaDirs() { |
| 818 | synchronized (mSync) { |
Jeff Sharkey | 4a2b119 | 2016-06-27 17:43:15 -0600 | [diff] [blame] | 819 | File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName()); |
| 820 | return ensureExternalDirsExistOrFilter(dirs); |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
Fyodor Kupolov | 6122129 | 2016-09-02 15:21:03 -0700 | [diff] [blame] | 824 | /** |
| 825 | * @hide |
| 826 | */ |
| 827 | @Nullable |
| 828 | @Override |
| 829 | public File getPreloadsFileCache() { |
| 830 | return Environment.getDataPreloadsFileCacheDirectory(getPackageName()); |
| 831 | } |
| 832 | |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 833 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | public File getFileStreamPath(String name) { |
| 835 | return makeFilename(getFilesDir(), name); |
| 836 | } |
| 837 | |
| 838 | @Override |
Jeff Sharkey | 6a6cdaf | 2015-12-07 19:25:19 -0700 | [diff] [blame] | 839 | public File getSharedPreferencesPath(String name) { |
| 840 | return makeFilename(getPreferencesDir(), name + ".xml"); |
| 841 | } |
| 842 | |
| 843 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | public String[] fileList() { |
Jeff Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 845 | return FileUtils.listOrEmpty(getFilesDir()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | @Override |
| 849 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 850 | return openOrCreateDatabase(name, mode, factory, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | @Override |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 854 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, |
| 855 | DatabaseErrorHandler errorHandler) { |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 856 | checkMode(mode); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 857 | File f = getDatabasePath(name); |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 858 | int flags = SQLiteDatabase.CREATE_IF_NECESSARY; |
| 859 | if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { |
| 860 | flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; |
| 861 | } |
Sunny Goyal | a21e6b2 | 2015-12-02 09:51:02 -0800 | [diff] [blame] | 862 | if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) { |
| 863 | flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS; |
| 864 | } |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 865 | SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 866 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 867 | return db; |
| 868 | } |
| 869 | |
| 870 | @Override |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 871 | public boolean moveDatabaseFrom(Context sourceContext, String name) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 872 | synchronized (ContextImpl.class) { |
| 873 | final File source = sourceContext.getDatabasePath(name); |
| 874 | final File target = getDatabasePath(name); |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 875 | return moveFiles(source.getParentFile(), target.getParentFile(), |
Jeff Sharkey | 390f2ed | 2016-03-01 15:25:03 -0700 | [diff] [blame] | 876 | source.getName()) != -1; |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
| 880 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | public boolean deleteDatabase(String name) { |
| 882 | try { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 883 | File f = getDatabasePath(name); |
Jeff Brown | 79087e4 | 2012-03-01 19:52:44 -0800 | [diff] [blame] | 884 | return SQLiteDatabase.deleteDatabase(f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | } catch (Exception e) { |
| 886 | } |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | @Override |
| 891 | public File getDatabasePath(String name) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 892 | File dir; |
| 893 | File f; |
| 894 | |
| 895 | if (name.charAt(0) == File.separatorChar) { |
| 896 | String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar)); |
| 897 | dir = new File(dirPath); |
| 898 | name = name.substring(name.lastIndexOf(File.separatorChar)); |
| 899 | f = new File(dir, name); |
Jeff Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 900 | |
| 901 | if (!dir.isDirectory() && dir.mkdir()) { |
| 902 | FileUtils.setPermissions(dir.getPath(), |
| 903 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 904 | -1, -1); |
| 905 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 906 | } else { |
| 907 | dir = getDatabasesDir(); |
| 908 | f = makeFilename(dir, name); |
| 909 | } |
| 910 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 911 | return f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public String[] databaseList() { |
Jeff Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 916 | return FileUtils.listOrEmpty(getDatabasesDir()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | } |
| 918 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | private File getDatabasesDir() { |
| 920 | synchronized (mSync) { |
| 921 | if (mDatabasesDir == null) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 922 | if ("android".equals(getPackageName())) { |
| 923 | mDatabasesDir = new File("/data/system"); |
| 924 | } else { |
Jeff Sharkey | 2c1ba9a | 2016-02-17 15:29:38 -0700 | [diff] [blame] | 925 | mDatabasesDir = new File(getDataDir(), "databases"); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 926 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 928 | return ensurePrivateDirExists(mDatabasesDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | } |
| 930 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 931 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 933 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | public Drawable getWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 935 | return getWallpaperManager().getDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 939 | @Deprecated |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 940 | public Drawable peekWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 941 | return getWallpaperManager().peekDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 945 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | public int getWallpaperDesiredMinimumWidth() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 947 | return getWallpaperManager().getDesiredMinimumWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 951 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | public int getWallpaperDesiredMinimumHeight() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 953 | return getWallpaperManager().getDesiredMinimumHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 957 | @Deprecated |
| 958 | public void setWallpaper(Bitmap bitmap) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 959 | getWallpaperManager().setBitmap(bitmap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 963 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | public void setWallpaper(InputStream data) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 965 | getWallpaperManager().setStream(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 969 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | public void clearWallpaper() throws IOException { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 971 | getWallpaperManager().clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 974 | private WallpaperManager getWallpaperManager() { |
| 975 | return getSystemService(WallpaperManager.class); |
| 976 | } |
| 977 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | @Override |
| 979 | public void startActivity(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 980 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 981 | startActivity(intent, null); |
| 982 | } |
| 983 | |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 984 | /** @hide */ |
| 985 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 986 | public void startActivityAsUser(Intent intent, UserHandle user) { |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 987 | startActivityAsUser(intent, null, user); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 990 | @Override |
| 991 | public void startActivity(Intent intent, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 992 | warnIfCallingFromSystemProcess(); |
Jorim Jaggi | 2adba07 | 2016-03-03 13:43:39 +0100 | [diff] [blame] | 993 | |
| 994 | // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is |
| 995 | // generally not allowed, except if the caller specifies the task id the activity should |
Bryce Lee | f465ea0 | 2017-05-02 16:58:22 -0700 | [diff] [blame] | 996 | // be launched in. A bug was existed between N and O-MR1 which allowed this to work. We |
| 997 | // maintain this for backwards compatibility. |
| 998 | final int targetSdkVersion = getApplicationInfo().targetSdkVersion; |
| 999 | |
| 1000 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 |
| 1001 | && (targetSdkVersion < Build.VERSION_CODES.N |
| 1002 | || targetSdkVersion >= Build.VERSION_CODES.P) |
| 1003 | && (options == null |
| 1004 | || ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | throw new AndroidRuntimeException( |
| 1006 | "Calling startActivity() from outside of an Activity " |
Bryce Lee | f465ea0 | 2017-05-02 16:58:22 -0700 | [diff] [blame] | 1007 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag." |
| 1008 | + " Is this really what you want?"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | } |
| 1010 | mMainThread.getInstrumentation().execStartActivity( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1011 | getOuterContext(), mMainThread.getApplicationThread(), null, |
| 1012 | (Activity) null, intent, -1, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1015 | /** @hide */ |
| 1016 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1017 | public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1018 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 1019 | ActivityTaskManager.getService().startActivityAsUser( |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1020 | mMainThread.getApplicationThread(), getBasePackageName(), intent, |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1021 | intent.resolveTypeIfNeeded(getContentResolver()), |
Jeff Hao | 1b012d3 | 2014-08-20 10:35:34 -0700 | [diff] [blame] | 1022 | null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options, |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1023 | user.getIdentifier()); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1024 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1025 | throw e.rethrowFromSystemServer(); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | @Override |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 1030 | public void startActivities(Intent[] intents) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1031 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1032 | startActivities(intents, null); |
| 1033 | } |
| 1034 | |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 1035 | /** @hide */ |
| 1036 | @Override |
Selim Cinek | 7fa385a | 2018-01-24 08:35:28 -0800 | [diff] [blame] | 1037 | public int startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) { |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 1038 | if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 1039 | throw new AndroidRuntimeException( |
| 1040 | "Calling startActivities() from outside of an Activity " |
| 1041 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent." |
| 1042 | + " Is this really what you want?"); |
| 1043 | } |
Selim Cinek | 7fa385a | 2018-01-24 08:35:28 -0800 | [diff] [blame] | 1044 | return mMainThread.getInstrumentation().execStartActivitiesAsUser( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1045 | getOuterContext(), mMainThread.getApplicationThread(), null, |
| 1046 | (Activity) null, intents, options, userHandle.getIdentifier()); |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1049 | @Override |
| 1050 | public void startActivities(Intent[] intents, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1051 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 1052 | if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 1053 | throw new AndroidRuntimeException( |
| 1054 | "Calling startActivities() from outside of an Activity " |
| 1055 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent." |
| 1056 | + " Is this really what you want?"); |
| 1057 | } |
| 1058 | mMainThread.getInstrumentation().execStartActivities( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1059 | getOuterContext(), mMainThread.getApplicationThread(), null, |
| 1060 | (Activity) null, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | @Override |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1064 | public void startIntentSender(IntentSender intent, |
| 1065 | Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
| 1066 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1067 | startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null); |
| 1068 | } |
| 1069 | |
| 1070 | @Override |
| 1071 | public void startIntentSender(IntentSender intent, Intent fillInIntent, |
| 1072 | int flagsMask, int flagsValues, int extraFlags, Bundle options) |
| 1073 | throws IntentSender.SendIntentException { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1074 | try { |
| 1075 | String resolvedType = null; |
| 1076 | if (fillInIntent != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1077 | fillInIntent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1078 | fillInIntent.prepareToLeaveProcess(this); |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1079 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 1080 | } |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 1081 | int result = ActivityTaskManager.getService() |
Dianne Hackborn | 3e6e385 | 2017-05-19 16:12:08 -0700 | [diff] [blame] | 1082 | .startActivityIntentSender(mMainThread.getApplicationThread(), |
| 1083 | intent != null ? intent.getTarget() : null, |
| 1084 | intent != null ? intent.getWhitelistToken() : null, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1085 | fillInIntent, resolvedType, null, null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1086 | 0, flagsMask, flagsValues, options); |
| 1087 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1088 | throw new IntentSender.SendIntentException(); |
| 1089 | } |
| 1090 | Instrumentation.checkStartActivityResult(result, null); |
| 1091 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1092 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1093 | } |
| 1094 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1095 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1096 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | public void sendBroadcast(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1098 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1100 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1101 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1102 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1103 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 1104 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false, |
| 1105 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1107 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
Amith Yamasani | 67cf7d3 | 2012-02-16 14:31:23 -0800 | [diff] [blame] | 1111 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | public void sendBroadcast(Intent intent, String receiverPermission) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1113 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1115 | String[] receiverPermissions = receiverPermission == null ? null |
| 1116 | : new String[] {receiverPermission}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1118 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1119 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1120 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1121 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
| 1122 | null, false, false, getUserId()); |
| 1123 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1124 | throw e.rethrowFromSystemServer(); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | @Override |
| 1129 | public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) { |
| 1130 | warnIfCallingFromSystemProcess(); |
| 1131 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1132 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1133 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1134 | ActivityManager.getService().broadcastIntent( |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1135 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 1136 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1137 | null, false, false, getUserId()); |
| 1138 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1139 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | @Override |
Tyler Gunn | f955e56 | 2018-04-26 14:43:31 -0700 | [diff] [blame] | 1144 | public void sendBroadcastAsUserMultiplePermissions(Intent intent, UserHandle user, |
| 1145 | String[] receiverPermissions) { |
Tyler Gunn | f955e56 | 2018-04-26 14:43:31 -0700 | [diff] [blame] | 1146 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1147 | try { |
| 1148 | intent.prepareToLeaveProcess(this); |
| 1149 | ActivityManager.getService().broadcastIntent( |
| 1150 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 1151 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
| 1152 | null, false, false, user.getIdentifier()); |
| 1153 | } catch (RemoteException e) { |
| 1154 | throw e.rethrowFromSystemServer(); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | @Override |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1159 | public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) { |
| 1160 | warnIfCallingFromSystemProcess(); |
| 1161 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1162 | String[] receiverPermissions = receiverPermission == null ? null |
| 1163 | : new String[] {receiverPermission}; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1164 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1165 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1166 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1167 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1168 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1169 | options, false, false, getUserId()); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1170 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1171 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | @Override |
| 1176 | public void sendBroadcast(Intent intent, String receiverPermission, int appOp) { |
| 1177 | warnIfCallingFromSystemProcess(); |
| 1178 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1179 | String[] receiverPermissions = receiverPermission == null ? null |
| 1180 | : new String[] {receiverPermission}; |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1181 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1182 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1183 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1184 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1185 | Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1186 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1188 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | @Override |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1193 | public void sendOrderedBroadcast(Intent intent, String receiverPermission) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1194 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1196 | String[] receiverPermissions = receiverPermission == null ? null |
| 1197 | : new String[] {receiverPermission}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1199 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1200 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1201 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1202 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1203 | null, true, false, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1205 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | @Override |
| 1210 | public void sendOrderedBroadcast(Intent intent, |
| 1211 | String receiverPermission, BroadcastReceiver resultReceiver, |
| 1212 | Handler scheduler, int initialCode, String initialData, |
| 1213 | Bundle initialExtras) { |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1214 | sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1215 | resultReceiver, scheduler, initialCode, initialData, initialExtras, null); |
| 1216 | } |
| 1217 | |
| 1218 | @Override |
| 1219 | public void sendOrderedBroadcast(Intent intent, |
| 1220 | String receiverPermission, Bundle options, BroadcastReceiver resultReceiver, |
| 1221 | Handler scheduler, int initialCode, String initialData, |
| 1222 | Bundle initialExtras) { |
| 1223 | sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE, |
| 1224 | resultReceiver, scheduler, initialCode, initialData, initialExtras, options); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | @Override |
| 1228 | public void sendOrderedBroadcast(Intent intent, |
| 1229 | String receiverPermission, int appOp, BroadcastReceiver resultReceiver, |
| 1230 | Handler scheduler, int initialCode, String initialData, |
| 1231 | Bundle initialExtras) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1232 | sendOrderedBroadcast(intent, receiverPermission, appOp, |
| 1233 | resultReceiver, scheduler, initialCode, initialData, initialExtras, null); |
| 1234 | } |
| 1235 | |
| 1236 | void sendOrderedBroadcast(Intent intent, |
| 1237 | String receiverPermission, int appOp, BroadcastReceiver resultReceiver, |
| 1238 | Handler scheduler, int initialCode, String initialData, |
| 1239 | Bundle initialExtras, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1240 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1241 | IIntentReceiver rd = null; |
| 1242 | if (resultReceiver != null) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1243 | if (mPackageInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1244 | if (scheduler == null) { |
| 1245 | scheduler = mMainThread.getHandler(); |
| 1246 | } |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1247 | rd = mPackageInfo.getReceiverDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | resultReceiver, getOuterContext(), scheduler, |
| 1249 | mMainThread.getInstrumentation(), false); |
| 1250 | } else { |
| 1251 | if (scheduler == null) { |
| 1252 | scheduler = mMainThread.getHandler(); |
| 1253 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1254 | rd = new LoadedApk.ReceiverDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1256 | } |
| 1257 | } |
| 1258 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1259 | String[] receiverPermissions = receiverPermission == null ? null |
| 1260 | : new String[] {receiverPermission}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1261 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1262 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1263 | ActivityManager.getService().broadcastIntent( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1264 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1265 | initialCode, initialData, initialExtras, receiverPermissions, appOp, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1266 | options, true, false, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1268 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1273 | public void sendBroadcastAsUser(Intent intent, UserHandle user) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1274 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1275 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1276 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1277 | ActivityManager.getService().broadcastIntent(mMainThread.getApplicationThread(), |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1278 | intent, resolvedType, null, Activity.RESULT_OK, null, null, null, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1279 | AppOpsManager.OP_NONE, null, false, false, user.getIdentifier()); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1280 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1281 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1286 | public void sendBroadcastAsUser(Intent intent, UserHandle user, |
| 1287 | String receiverPermission) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1288 | sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE); |
| 1289 | } |
| 1290 | |
| 1291 | @Override |
Chad Brubaker | 52c8edc | 2016-07-25 14:30:26 -0700 | [diff] [blame] | 1292 | public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, |
| 1293 | Bundle options) { |
| 1294 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1295 | String[] receiverPermissions = receiverPermission == null ? null |
| 1296 | : new String[] {receiverPermission}; |
| 1297 | try { |
| 1298 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1299 | ActivityManager.getService().broadcastIntent( |
Chad Brubaker | 52c8edc | 2016-07-25 14:30:26 -0700 | [diff] [blame] | 1300 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 1301 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
| 1302 | options, false, false, user.getIdentifier()); |
| 1303 | } catch (RemoteException e) { |
| 1304 | throw e.rethrowFromSystemServer(); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | @Override |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1309 | public void sendBroadcastAsUser(Intent intent, UserHandle user, |
| 1310 | String receiverPermission, int appOp) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1311 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1312 | String[] receiverPermissions = receiverPermission == null ? null |
| 1313 | : new String[] {receiverPermission}; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1314 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1315 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1316 | ActivityManager.getService().broadcastIntent( |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1317 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1318 | Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false, |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1319 | user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1320 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1321 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1326 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1327 | String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1328 | int initialCode, String initialData, Bundle initialExtras) { |
Amith Yamasani | 3cf7572 | 2014-05-16 12:37:29 -0700 | [diff] [blame] | 1329 | sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1330 | null, resultReceiver, scheduler, initialCode, initialData, initialExtras); |
Amith Yamasani | 3cf7572 | 2014-05-16 12:37:29 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | @Override |
| 1334 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
| 1335 | String receiverPermission, int appOp, BroadcastReceiver resultReceiver, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1336 | Handler scheduler, int initialCode, String initialData, Bundle initialExtras) { |
| 1337 | sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp, |
| 1338 | null, resultReceiver, scheduler, initialCode, initialData, initialExtras); |
| 1339 | } |
| 1340 | |
| 1341 | @Override |
| 1342 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
| 1343 | String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver, |
| 1344 | Handler scheduler, int initialCode, String initialData, Bundle initialExtras) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1345 | IIntentReceiver rd = null; |
| 1346 | if (resultReceiver != null) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1347 | if (mPackageInfo != null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1348 | if (scheduler == null) { |
| 1349 | scheduler = mMainThread.getHandler(); |
| 1350 | } |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1351 | rd = mPackageInfo.getReceiverDispatcher( |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1352 | resultReceiver, getOuterContext(), scheduler, |
| 1353 | mMainThread.getInstrumentation(), false); |
| 1354 | } else { |
| 1355 | if (scheduler == null) { |
| 1356 | scheduler = mMainThread.getHandler(); |
| 1357 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1358 | rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(), |
| 1359 | scheduler, null, false).getIIntentReceiver(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1363 | String[] receiverPermissions = receiverPermission == null ? null |
| 1364 | : new String[] {receiverPermission}; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1365 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1366 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1367 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1368 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1369 | initialCode, initialData, initialExtras, receiverPermissions, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1370 | appOp, options, true, false, user.getIdentifier()); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1371 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1372 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | @Override |
Jordan Liu | 5473553 | 2019-10-29 10:33:55 -0700 | [diff] [blame] | 1377 | public void sendOrderedBroadcast(Intent intent, String receiverPermission, |
| 1378 | String receiverAppOp, BroadcastReceiver resultReceiver, Handler scheduler, |
| 1379 | int initialCode, String initialData, @Nullable Bundle initialExtras) { |
| 1380 | int intAppOp = AppOpsManager.OP_NONE; |
| 1381 | if (!TextUtils.isEmpty(receiverAppOp)) { |
| 1382 | intAppOp = AppOpsManager.strOpToOp(receiverAppOp); |
| 1383 | } |
| 1384 | sendOrderedBroadcastAsUser(intent, getUser(), |
| 1385 | receiverPermission, intAppOp, resultReceiver, scheduler, initialCode, initialData, |
| 1386 | initialExtras); |
| 1387 | } |
| 1388 | |
| 1389 | @Override |
Peter Wang | 7701f57 | 2019-11-07 16:57:23 -0800 | [diff] [blame] | 1390 | public void sendOrderedBroadcast(Intent intent, String receiverPermission, String receiverAppOp, |
| 1391 | Bundle options, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, |
| 1392 | String initialData, @Nullable Bundle initialExtras) { |
| 1393 | int intAppOp = AppOpsManager.OP_NONE; |
| 1394 | if (!TextUtils.isEmpty(receiverAppOp)) { |
| 1395 | intAppOp = AppOpsManager.strOpToOp(receiverAppOp); |
| 1396 | } |
| 1397 | sendOrderedBroadcastAsUser(intent, getUser(), receiverPermission, intAppOp, options, |
| 1398 | resultReceiver, scheduler, initialCode, initialData, initialExtras); |
| 1399 | } |
| 1400 | |
| 1401 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1402 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | public void sendStickyBroadcast(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1404 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1406 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1407 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1408 | ActivityManager.getService().broadcastIntent( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1410 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1411 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1413 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1418 | @Deprecated |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1419 | public void sendStickyOrderedBroadcast(Intent intent, |
| 1420 | BroadcastReceiver resultReceiver, |
| 1421 | Handler scheduler, int initialCode, String initialData, |
| 1422 | Bundle initialExtras) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1423 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1424 | IIntentReceiver rd = null; |
| 1425 | if (resultReceiver != null) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1426 | if (mPackageInfo != null) { |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1427 | if (scheduler == null) { |
| 1428 | scheduler = mMainThread.getHandler(); |
| 1429 | } |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1430 | rd = mPackageInfo.getReceiverDispatcher( |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1431 | resultReceiver, getOuterContext(), scheduler, |
| 1432 | mMainThread.getInstrumentation(), false); |
| 1433 | } else { |
| 1434 | if (scheduler == null) { |
| 1435 | scheduler = mMainThread.getHandler(); |
| 1436 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1437 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1438 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1439 | } |
| 1440 | } |
| 1441 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1442 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1443 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1444 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1445 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1446 | initialCode, initialData, initialExtras, null, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1447 | AppOpsManager.OP_NONE, null, true, true, getUserId()); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1448 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1449 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1454 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1455 | public void removeStickyBroadcast(Intent intent) { |
| 1456 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1457 | if (resolvedType != null) { |
| 1458 | intent = new Intent(intent); |
| 1459 | intent.setDataAndType(intent.getData(), resolvedType); |
| 1460 | } |
| 1461 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1462 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1463 | ActivityManager.getService().unbroadcastIntent( |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1464 | mMainThread.getApplicationThread(), intent, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1466 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1471 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1472 | public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { |
| 1473 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1474 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1475 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1476 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1477 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1478 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true, |
| 1479 | user.getIdentifier()); |
| 1480 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1481 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | @Override |
| 1486 | @Deprecated |
| 1487 | public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) { |
| 1488 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1489 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1490 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1491 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1492 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 1493 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true, |
| 1494 | user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1495 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1496 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1501 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1502 | public void sendStickyOrderedBroadcastAsUser(Intent intent, |
| 1503 | UserHandle user, BroadcastReceiver resultReceiver, |
| 1504 | Handler scheduler, int initialCode, String initialData, |
| 1505 | Bundle initialExtras) { |
| 1506 | IIntentReceiver rd = null; |
| 1507 | if (resultReceiver != null) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1508 | if (mPackageInfo != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1509 | if (scheduler == null) { |
| 1510 | scheduler = mMainThread.getHandler(); |
| 1511 | } |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1512 | rd = mPackageInfo.getReceiverDispatcher( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1513 | resultReceiver, getOuterContext(), scheduler, |
| 1514 | mMainThread.getInstrumentation(), false); |
| 1515 | } else { |
| 1516 | if (scheduler == null) { |
| 1517 | scheduler = mMainThread.getHandler(); |
| 1518 | } |
| 1519 | rd = new LoadedApk.ReceiverDispatcher( |
| 1520 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1521 | } |
| 1522 | } |
| 1523 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1524 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1525 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1526 | ActivityManager.getService().broadcastIntent( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1527 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1528 | initialCode, initialData, initialExtras, null, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1529 | AppOpsManager.OP_NONE, null, true, true, user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1530 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1531 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1536 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1537 | public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { |
| 1538 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1539 | if (resolvedType != null) { |
| 1540 | intent = new Intent(intent); |
| 1541 | intent.setDataAndType(intent.getData(), resolvedType); |
| 1542 | } |
| 1543 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1544 | intent.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1545 | ActivityManager.getService().unbroadcastIntent( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1546 | mMainThread.getApplicationThread(), intent, user.getIdentifier()); |
| 1547 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1548 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1553 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 1554 | return registerReceiver(receiver, filter, null, null); |
| 1555 | } |
| 1556 | |
| 1557 | @Override |
| 1558 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
Chad Brubaker | 6d6015f | 2017-04-18 11:25:16 -0700 | [diff] [blame] | 1559 | int flags) { |
| 1560 | return registerReceiver(receiver, filter, null, null, flags); |
Chad Brubaker | 816c83b | 2017-03-02 10:27:59 -0800 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | @Override |
| 1564 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1566 | return registerReceiverInternal(receiver, getUserId(), |
Chad Brubaker | 6d6015f | 2017-04-18 11:25:16 -0700 | [diff] [blame] | 1567 | filter, broadcastPermission, scheduler, getOuterContext(), 0); |
Chad Brubaker | 816c83b | 2017-03-02 10:27:59 -0800 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | @Override |
| 1571 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
Chad Brubaker | 6d6015f | 2017-04-18 11:25:16 -0700 | [diff] [blame] | 1572 | String broadcastPermission, Handler scheduler, int flags) { |
Chad Brubaker | 816c83b | 2017-03-02 10:27:59 -0800 | [diff] [blame] | 1573 | return registerReceiverInternal(receiver, getUserId(), |
Chad Brubaker | 6d6015f | 2017-04-18 11:25:16 -0700 | [diff] [blame] | 1574 | filter, broadcastPermission, scheduler, getOuterContext(), flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | } |
| 1576 | |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1577 | @Override |
Meng Wang | 3f9e036 | 2019-11-05 10:43:45 -0800 | [diff] [blame] | 1578 | public Intent registerReceiverForAllUsers(BroadcastReceiver receiver, |
| 1579 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 1580 | return registerReceiverAsUser(receiver, UserHandle.ALL, |
| 1581 | filter, broadcastPermission, scheduler); |
| 1582 | } |
| 1583 | |
| 1584 | @Override |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1585 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 1586 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 1587 | return registerReceiverInternal(receiver, user.getIdentifier(), |
Chad Brubaker | 6d6015f | 2017-04-18 11:25:16 -0700 | [diff] [blame] | 1588 | filter, broadcastPermission, scheduler, getOuterContext(), 0); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | IntentFilter filter, String broadcastPermission, |
Chad Brubaker | 6d6015f | 2017-04-18 11:25:16 -0700 | [diff] [blame] | 1593 | Handler scheduler, Context context, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | IIntentReceiver rd = null; |
| 1595 | if (receiver != null) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1596 | if (mPackageInfo != null && context != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1597 | if (scheduler == null) { |
| 1598 | scheduler = mMainThread.getHandler(); |
| 1599 | } |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1600 | rd = mPackageInfo.getReceiverDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | receiver, context, scheduler, |
| 1602 | mMainThread.getInstrumentation(), true); |
| 1603 | } else { |
| 1604 | if (scheduler == null) { |
| 1605 | scheduler = mMainThread.getHandler(); |
| 1606 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1607 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 1608 | receiver, context, scheduler, null, true).getIIntentReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1612 | final Intent intent = ActivityManager.getService().registerReceiver( |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 1613 | mMainThread.getApplicationThread(), mBasePackageName, rd, filter, |
Chad Brubaker | 6d6015f | 2017-04-18 11:25:16 -0700 | [diff] [blame] | 1614 | broadcastPermission, userId, flags); |
Jeff Sharkey | d136e51 | 2016-03-09 22:30:56 -0700 | [diff] [blame] | 1615 | if (intent != null) { |
| 1616 | intent.setExtrasClassLoader(getClassLoader()); |
| 1617 | intent.prepareToEnterProcess(); |
| 1618 | } |
| 1619 | return intent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1621 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | @Override |
| 1626 | public void unregisterReceiver(BroadcastReceiver receiver) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1627 | if (mPackageInfo != null) { |
| 1628 | IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | getOuterContext(), receiver); |
| 1630 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1631 | ActivityManager.getService().unregisterReceiver(rd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1633 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1634 | } |
| 1635 | } else { |
| 1636 | throw new RuntimeException("Not supported in system context"); |
| 1637 | } |
| 1638 | } |
| 1639 | |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1640 | private void validateServiceIntent(Intent service) { |
| 1641 | if (service.getComponent() == null && service.getPackage() == null) { |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 1642 | if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) { |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1643 | IllegalArgumentException ex = new IllegalArgumentException( |
| 1644 | "Service Intent must be explicit: " + service); |
| 1645 | throw ex; |
| 1646 | } else { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1647 | Log.w(TAG, "Implicit intents with startService are not safe: " + service |
| 1648 | + " " + Debug.getCallers(2, 3)); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | @Override |
| 1654 | public ComponentName startService(Intent service) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1655 | warnIfCallingFromSystemProcess(); |
Christopher Tate | 242ba3e9 | 2017-04-14 15:07:06 -0700 | [diff] [blame] | 1656 | return startServiceCommon(service, false, mUser); |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | @Override |
Christopher Tate | 79047c6 | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1660 | public ComponentName startForegroundService(Intent service) { |
| 1661 | warnIfCallingFromSystemProcess(); |
Christopher Tate | 242ba3e9 | 2017-04-14 15:07:06 -0700 | [diff] [blame] | 1662 | return startServiceCommon(service, true, mUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | @Override |
| 1666 | public boolean stopService(Intent service) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1667 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1668 | return stopServiceCommon(service, mUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | @Override |
| 1672 | public ComponentName startServiceAsUser(Intent service, UserHandle user) { |
Christopher Tate | 242ba3e9 | 2017-04-14 15:07:06 -0700 | [diff] [blame] | 1673 | return startServiceCommon(service, false, user); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1676 | @Override |
Christopher Tate | 79047c6 | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1677 | public ComponentName startForegroundServiceAsUser(Intent service, UserHandle user) { |
Christopher Tate | 242ba3e9 | 2017-04-14 15:07:06 -0700 | [diff] [blame] | 1678 | return startServiceCommon(service, true, user); |
Christopher Tate | 79047c6 | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
Christopher Tate | 242ba3e9 | 2017-04-14 15:07:06 -0700 | [diff] [blame] | 1681 | private ComponentName startServiceCommon(Intent service, boolean requireForeground, |
| 1682 | UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1683 | try { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1684 | validateServiceIntent(service); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1685 | service.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1686 | ComponentName cn = ActivityManager.getService().startService( |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1687 | mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded( |
Christopher Tate | 242ba3e9 | 2017-04-14 15:07:06 -0700 | [diff] [blame] | 1688 | getContentResolver()), requireForeground, |
Christopher Tate | 79047c6 | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1689 | getOpPackageName(), user.getIdentifier()); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1690 | if (cn != null) { |
| 1691 | if (cn.getPackageName().equals("!")) { |
| 1692 | throw new SecurityException( |
| 1693 | "Not allowed to start service " + service |
| 1694 | + " without permission " + cn.getClassName()); |
| 1695 | } else if (cn.getPackageName().equals("!!")) { |
| 1696 | throw new SecurityException( |
| 1697 | "Unable to start service " + service |
| 1698 | + ": " + cn.getClassName()); |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 1699 | } else if (cn.getPackageName().equals("?")) { |
| 1700 | throw new IllegalStateException( |
| 1701 | "Not allowed to start service " + service + ": " + cn.getClassName()); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1702 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | } |
| 1704 | return cn; |
| 1705 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1706 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | @Override |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1711 | public boolean stopServiceAsUser(Intent service, UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1712 | return stopServiceCommon(service, user); |
| 1713 | } |
| 1714 | |
| 1715 | private boolean stopServiceCommon(Intent service, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1716 | try { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1717 | validateServiceIntent(service); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1718 | service.prepareToLeaveProcess(this); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1719 | int res = ActivityManager.getService().stopService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | mMainThread.getApplicationThread(), service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1721 | service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | if (res < 0) { |
| 1723 | throw new SecurityException( |
| 1724 | "Not allowed to stop service " + service); |
| 1725 | } |
| 1726 | return res != 0; |
| 1727 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1728 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | @Override |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1733 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1734 | warnIfCallingFromSystemProcess(); |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1735 | return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null, |
| 1736 | getUser()); |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | @Override |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1740 | public boolean bindService( |
| 1741 | Intent service, int flags, Executor executor, ServiceConnection conn) { |
| 1742 | warnIfCallingFromSystemProcess(); |
| 1743 | return bindServiceCommon(service, conn, flags, null, null, executor, getUser()); |
| 1744 | } |
| 1745 | |
| 1746 | @Override |
| 1747 | public boolean bindIsolatedService(Intent service, int flags, String instanceName, |
| 1748 | Executor executor, ServiceConnection conn) { |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 1749 | warnIfCallingFromSystemProcess(); |
| 1750 | if (instanceName == null) { |
| 1751 | throw new NullPointerException("null instanceName"); |
| 1752 | } |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1753 | return bindServiceCommon(service, conn, flags, instanceName, null, executor, getUser()); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /** @hide */ |
| 1757 | @Override |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1758 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 1759 | UserHandle user) { |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1760 | return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null, user); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Adrian Roos | 691546e | 2016-02-09 10:13:41 -0800 | [diff] [blame] | 1763 | /** @hide */ |
| 1764 | @Override |
| 1765 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 1766 | Handler handler, UserHandle user) { |
| 1767 | if (handler == null) { |
| 1768 | throw new IllegalArgumentException("handler must not be null."); |
| 1769 | } |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1770 | return bindServiceCommon(service, conn, flags, null, handler, null, user); |
Adrian Roos | 691546e | 2016-02-09 10:13:41 -0800 | [diff] [blame] | 1771 | } |
| 1772 | |
Tony Mak | 46aabe5 | 2016-11-14 12:53:06 +0000 | [diff] [blame] | 1773 | /** @hide */ |
| 1774 | @Override |
| 1775 | public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler, |
| 1776 | int flags) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1777 | return mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags); |
Tony Mak | 46aabe5 | 2016-11-14 12:53:06 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | /** @hide */ |
| 1781 | @Override |
| 1782 | public IApplicationThread getIApplicationThread() { |
| 1783 | return mMainThread.getApplicationThread(); |
| 1784 | } |
| 1785 | |
Tony Mak | bf9928d | 2016-12-22 11:02:45 +0000 | [diff] [blame] | 1786 | /** @hide */ |
| 1787 | @Override |
| 1788 | public Handler getMainThreadHandler() { |
| 1789 | return mMainThread.getHandler(); |
| 1790 | } |
| 1791 | |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 1792 | private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1793 | String instanceName, Handler handler, Executor executor, UserHandle user) { |
Tony Mak | 46aabe5 | 2016-11-14 12:53:06 +0000 | [diff] [blame] | 1794 | // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | IServiceConnection sd; |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1796 | if (conn == null) { |
| 1797 | throw new IllegalArgumentException("connection is null"); |
| 1798 | } |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1799 | if (handler != null && executor != null) { |
| 1800 | throw new IllegalArgumentException("Handler and Executor both supplied"); |
| 1801 | } |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1802 | if (mPackageInfo != null) { |
Bo Liu | 58a5766 | 2019-03-06 20:21:45 +0000 | [diff] [blame] | 1803 | if (executor != null) { |
| 1804 | sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), executor, flags); |
| 1805 | } else { |
| 1806 | sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags); |
| 1807 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1808 | } else { |
| 1809 | throw new RuntimeException("Not supported in system context"); |
| 1810 | } |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1811 | validateServiceIntent(service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1812 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1813 | IBinder token = getActivityToken(); |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1814 | if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null |
| 1815 | && mPackageInfo.getApplicationInfo().targetSdkVersion |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1816 | < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 1817 | flags |= BIND_WAIVE_PRIORITY; |
| 1818 | } |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1819 | service.prepareToLeaveProcess(this); |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 1820 | int res = ActivityManager.getService().bindIsolatedService( |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1821 | mMainThread.getApplicationThread(), getActivityToken(), service, |
| 1822 | service.resolveTypeIfNeeded(getContentResolver()), |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 1823 | sd, flags, instanceName, getOpPackageName(), user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1824 | if (res < 0) { |
| 1825 | throw new SecurityException( |
| 1826 | "Not allowed to bind to service " + service); |
| 1827 | } |
| 1828 | return res != 0; |
| 1829 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1830 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | @Override |
Dianne Hackborn | a631d56 | 2018-11-20 15:58:15 -0800 | [diff] [blame] | 1835 | public void updateServiceGroup(@NonNull ServiceConnection conn, int group, int importance) { |
| 1836 | if (conn == null) { |
| 1837 | throw new IllegalArgumentException("connection is null"); |
| 1838 | } |
| 1839 | if (mPackageInfo != null) { |
Dianne Hackborn | 2f55e5a | 2018-11-30 16:31:31 -0800 | [diff] [blame] | 1840 | IServiceConnection sd = mPackageInfo.lookupServiceDispatcher(conn, getOuterContext()); |
| 1841 | if (sd == null) { |
| 1842 | throw new IllegalArgumentException("ServiceConnection not currently bound: " |
| 1843 | + conn); |
| 1844 | } |
Dianne Hackborn | a631d56 | 2018-11-20 15:58:15 -0800 | [diff] [blame] | 1845 | try { |
| 1846 | ActivityManager.getService().updateServiceGroup(sd, group, importance); |
| 1847 | } catch (RemoteException e) { |
| 1848 | throw e.rethrowFromSystemServer(); |
| 1849 | } |
| 1850 | } else { |
| 1851 | throw new RuntimeException("Not supported in system context"); |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1856 | public void unbindService(ServiceConnection conn) { |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1857 | if (conn == null) { |
| 1858 | throw new IllegalArgumentException("connection is null"); |
| 1859 | } |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 1860 | if (mPackageInfo != null) { |
| 1861 | IServiceConnection sd = mPackageInfo.forgetServiceDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1862 | getOuterContext(), conn); |
| 1863 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1864 | ActivityManager.getService().unbindService(sd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1865 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1866 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1867 | } |
| 1868 | } else { |
| 1869 | throw new RuntimeException("Not supported in system context"); |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | @Override |
| 1874 | public boolean startInstrumentation(ComponentName className, |
| 1875 | String profileFile, Bundle arguments) { |
| 1876 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1877 | if (arguments != null) { |
| 1878 | arguments.setAllowFds(false); |
| 1879 | } |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1880 | return ActivityManager.getService().startInstrumentation( |
Narayan Kamath | 8dcfefd | 2014-05-15 18:12:59 +0100 | [diff] [blame] | 1881 | className, profileFile, 0, arguments, null, null, getUserId(), |
| 1882 | null /* ABI override */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1884 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1885 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | @Override |
| 1889 | public Object getSystemService(String name) { |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 1890 | if (isUiComponent(name) && !isUiContext()) { |
| 1891 | Log.w(TAG, name + " should be accessed from Activity or other visual Context"); |
| 1892 | } |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1893 | return SystemServiceRegistry.getSystemService(this, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1894 | } |
| 1895 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1896 | @Override |
| 1897 | public String getSystemServiceName(Class<?> serviceClass) { |
| 1898 | return SystemServiceRegistry.getSystemServiceName(serviceClass); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 1901 | boolean isUiContext() { |
| 1902 | return mIsSystemOrSystemUiContext || mIsUiContext; |
| 1903 | } |
| 1904 | |
| 1905 | private static boolean isUiComponent(String name) { |
| 1906 | return WINDOW_SERVICE.equals(name) || LAYOUT_INFLATER_SERVICE.equals(name) |
| 1907 | || WALLPAPER_SERVICE.equals(name); |
| 1908 | } |
| 1909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1910 | @Override |
| 1911 | public int checkPermission(String permission, int pid, int uid) { |
| 1912 | if (permission == null) { |
| 1913 | throw new IllegalArgumentException("permission is null"); |
| 1914 | } |
| 1915 | |
Jeff Sharkey | fe6f85c | 2017-01-20 10:42:57 -0700 | [diff] [blame] | 1916 | final IActivityManager am = ActivityManager.getService(); |
Jeff Sharkey | d69b083 | 2017-01-23 11:37:24 -0700 | [diff] [blame] | 1917 | if (am == null) { |
Jeff Sharkey | fe6f85c | 2017-01-20 10:42:57 -0700 | [diff] [blame] | 1918 | // Well this is super awkward; we somehow don't have an active |
Jeff Sharkey | d69b083 | 2017-01-23 11:37:24 -0700 | [diff] [blame] | 1919 | // ActivityManager instance. If we're testing a root or system |
| 1920 | // UID, then they totally have whatever permission this is. |
| 1921 | final int appId = UserHandle.getAppId(uid); |
| 1922 | if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) { |
| 1923 | Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " holds " + permission); |
| 1924 | return PackageManager.PERMISSION_GRANTED; |
| 1925 | } |
Erik Wolsheimer | e21576f | 2018-03-29 16:21:41 -0700 | [diff] [blame] | 1926 | Slog.w(TAG, "Missing ActivityManager; assuming " + uid + " does not hold " |
| 1927 | + permission); |
| 1928 | return PackageManager.PERMISSION_DENIED; |
Jeff Sharkey | fe6f85c | 2017-01-20 10:42:57 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1931 | try { |
Tim Murray | d06c6d29 | 2019-05-07 06:53:55 +0000 | [diff] [blame] | 1932 | return am.checkPermission(permission, pid, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1933 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1934 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 1938 | /** @hide */ |
| 1939 | @Override |
| 1940 | public int checkPermission(String permission, int pid, int uid, IBinder callerToken) { |
| 1941 | if (permission == null) { |
| 1942 | throw new IllegalArgumentException("permission is null"); |
| 1943 | } |
| 1944 | |
| 1945 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1946 | return ActivityManager.getService().checkPermissionWithToken( |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 1947 | permission, pid, uid, callerToken); |
| 1948 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1949 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1953 | @Override |
| 1954 | public int checkCallingPermission(String permission) { |
| 1955 | if (permission == null) { |
| 1956 | throw new IllegalArgumentException("permission is null"); |
| 1957 | } |
| 1958 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1959 | int pid = Binder.getCallingPid(); |
| 1960 | if (pid != Process.myPid()) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1961 | return checkPermission(permission, pid, Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1962 | } |
| 1963 | return PackageManager.PERMISSION_DENIED; |
| 1964 | } |
| 1965 | |
| 1966 | @Override |
| 1967 | public int checkCallingOrSelfPermission(String permission) { |
| 1968 | if (permission == null) { |
| 1969 | throw new IllegalArgumentException("permission is null"); |
| 1970 | } |
| 1971 | |
| 1972 | return checkPermission(permission, Binder.getCallingPid(), |
| 1973 | Binder.getCallingUid()); |
| 1974 | } |
| 1975 | |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1976 | @Override |
| 1977 | public int checkSelfPermission(String permission) { |
| 1978 | if (permission == null) { |
| 1979 | throw new IllegalArgumentException("permission is null"); |
| 1980 | } |
| 1981 | |
| 1982 | return checkPermission(permission, Process.myPid(), Process.myUid()); |
| 1983 | } |
| 1984 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1985 | private void enforce( |
| 1986 | String permission, int resultOfCheck, |
| 1987 | boolean selfToo, int uid, String message) { |
| 1988 | if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { |
| 1989 | throw new SecurityException( |
| 1990 | (message != null ? (message + ": ") : "") + |
| 1991 | (selfToo |
| 1992 | ? "Neither user " + uid + " nor current process has " |
Tim Murray | d06c6d29 | 2019-05-07 06:53:55 +0000 | [diff] [blame] | 1993 | : "uid " + uid + " does not have ") + |
| 1994 | permission + |
| 1995 | "."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1999 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2000 | public void enforcePermission( |
| 2001 | String permission, int pid, int uid, String message) { |
Tim Murray | d06c6d29 | 2019-05-07 06:53:55 +0000 | [diff] [blame] | 2002 | enforce(permission, |
| 2003 | checkPermission(permission, pid, uid), |
| 2004 | false, |
| 2005 | uid, |
| 2006 | message); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | } |
| 2008 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2009 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2010 | public void enforceCallingPermission(String permission, String message) { |
Tim Murray | d06c6d29 | 2019-05-07 06:53:55 +0000 | [diff] [blame] | 2011 | enforce(permission, |
| 2012 | checkCallingPermission(permission), |
| 2013 | false, |
| 2014 | Binder.getCallingUid(), |
| 2015 | message); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | } |
| 2017 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2018 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2019 | public void enforceCallingOrSelfPermission( |
| 2020 | String permission, String message) { |
Tim Murray | d06c6d29 | 2019-05-07 06:53:55 +0000 | [diff] [blame] | 2021 | enforce(permission, |
| 2022 | checkCallingOrSelfPermission(permission), |
| 2023 | true, |
| 2024 | Binder.getCallingUid(), |
| 2025 | message); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | @Override |
| 2029 | public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { |
| 2030 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2031 | ActivityManager.getService().grantUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2032 | mMainThread.getApplicationThread(), toPackage, |
| 2033 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2034 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2035 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | @Override |
| 2040 | public void revokeUriPermission(Uri uri, int modeFlags) { |
| 2041 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2042 | ActivityManager.getService().revokeUriPermission( |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 2043 | mMainThread.getApplicationThread(), null, |
| 2044 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
| 2045 | } catch (RemoteException e) { |
| 2046 | throw e.rethrowFromSystemServer(); |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | @Override |
| 2051 | public void revokeUriPermission(String targetPackage, Uri uri, int modeFlags) { |
| 2052 | try { |
| 2053 | ActivityManager.getService().revokeUriPermission( |
| 2054 | mMainThread.getApplicationThread(), targetPackage, |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2055 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2056 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2057 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | @Override |
| 2062 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2063 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2064 | return ActivityManager.getService().checkUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2065 | ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags, |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 2066 | resolveUserId(uri), null); |
| 2067 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2068 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | /** @hide */ |
| 2073 | @Override |
| 2074 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) { |
| 2075 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2076 | return ActivityManager.getService().checkUriPermission( |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 2077 | ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags, |
| 2078 | resolveUserId(uri), callerToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2080 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2084 | private int resolveUserId(Uri uri) { |
| 2085 | return ContentProvider.getUserIdFromUri(uri, getUserId()); |
| 2086 | } |
| 2087 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | @Override |
| 2089 | public int checkCallingUriPermission(Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2090 | int pid = Binder.getCallingPid(); |
| 2091 | if (pid != Process.myPid()) { |
| 2092 | return checkUriPermission(uri, pid, |
| 2093 | Binder.getCallingUid(), modeFlags); |
| 2094 | } |
| 2095 | return PackageManager.PERMISSION_DENIED; |
| 2096 | } |
| 2097 | |
| 2098 | @Override |
| 2099 | public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { |
| 2100 | return checkUriPermission(uri, Binder.getCallingPid(), |
| 2101 | Binder.getCallingUid(), modeFlags); |
| 2102 | } |
| 2103 | |
| 2104 | @Override |
| 2105 | public int checkUriPermission(Uri uri, String readPermission, |
| 2106 | String writePermission, int pid, int uid, int modeFlags) { |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 2107 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2108 | Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission=" |
| 2109 | + readPermission + " writePermission=" + writePermission |
| 2110 | + " pid=" + pid + " uid=" + uid + " mode" + modeFlags); |
| 2111 | } |
| 2112 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 2113 | if (readPermission == null |
| 2114 | || checkPermission(readPermission, pid, uid) |
| 2115 | == PackageManager.PERMISSION_GRANTED) { |
| 2116 | return PackageManager.PERMISSION_GRANTED; |
| 2117 | } |
| 2118 | } |
| 2119 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 2120 | if (writePermission == null |
| 2121 | || checkPermission(writePermission, pid, uid) |
| 2122 | == PackageManager.PERMISSION_GRANTED) { |
| 2123 | return PackageManager.PERMISSION_GRANTED; |
| 2124 | } |
| 2125 | } |
| 2126 | return uri != null ? checkUriPermission(uri, pid, uid, modeFlags) |
| 2127 | : PackageManager.PERMISSION_DENIED; |
| 2128 | } |
| 2129 | |
| 2130 | private String uriModeFlagToString(int uriModeFlags) { |
Jeff Sharkey | 846318a | 2014-04-04 12:12:41 -0700 | [diff] [blame] | 2131 | StringBuilder builder = new StringBuilder(); |
| 2132 | if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 2133 | builder.append("read and "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2134 | } |
Jeff Sharkey | 846318a | 2014-04-04 12:12:41 -0700 | [diff] [blame] | 2135 | if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 2136 | builder.append("write and "); |
| 2137 | } |
| 2138 | if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) { |
| 2139 | builder.append("persistable and "); |
| 2140 | } |
| 2141 | if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) { |
| 2142 | builder.append("prefix and "); |
| 2143 | } |
| 2144 | |
| 2145 | if (builder.length() > 5) { |
| 2146 | builder.setLength(builder.length() - 5); |
| 2147 | return builder.toString(); |
| 2148 | } else { |
| 2149 | throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags); |
| 2150 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | private void enforceForUri( |
| 2154 | int modeFlags, int resultOfCheck, boolean selfToo, |
| 2155 | int uid, Uri uri, String message) { |
| 2156 | if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { |
| 2157 | throw new SecurityException( |
| 2158 | (message != null ? (message + ": ") : "") + |
| 2159 | (selfToo |
| 2160 | ? "Neither user " + uid + " nor current process has " |
| 2161 | : "User " + uid + " does not have ") + |
| 2162 | uriModeFlagToString(modeFlags) + |
| 2163 | " permission on " + |
| 2164 | uri + |
| 2165 | "."); |
| 2166 | } |
| 2167 | } |
| 2168 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2169 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2170 | public void enforceUriPermission( |
| 2171 | Uri uri, int pid, int uid, int modeFlags, String message) { |
| 2172 | enforceForUri( |
| 2173 | modeFlags, checkUriPermission(uri, pid, uid, modeFlags), |
| 2174 | false, uid, uri, message); |
| 2175 | } |
| 2176 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2177 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2178 | public void enforceCallingUriPermission( |
| 2179 | Uri uri, int modeFlags, String message) { |
| 2180 | enforceForUri( |
| 2181 | modeFlags, checkCallingUriPermission(uri, modeFlags), |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2182 | false, |
| 2183 | Binder.getCallingUid(), uri, message); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2184 | } |
| 2185 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2186 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2187 | public void enforceCallingOrSelfUriPermission( |
| 2188 | Uri uri, int modeFlags, String message) { |
| 2189 | enforceForUri( |
| 2190 | modeFlags, |
| 2191 | checkCallingOrSelfUriPermission(uri, modeFlags), true, |
| 2192 | Binder.getCallingUid(), uri, message); |
| 2193 | } |
| 2194 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2195 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2196 | public void enforceUriPermission( |
| 2197 | Uri uri, String readPermission, String writePermission, |
| 2198 | int pid, int uid, int modeFlags, String message) { |
| 2199 | enforceForUri(modeFlags, |
| 2200 | checkUriPermission( |
| 2201 | uri, readPermission, writePermission, pid, uid, |
| 2202 | modeFlags), |
| 2203 | false, |
| 2204 | uid, |
| 2205 | uri, |
| 2206 | message); |
| 2207 | } |
| 2208 | |
Tom O'Neill | 365632e | 2013-09-09 09:34:58 -0700 | [diff] [blame] | 2209 | /** |
| 2210 | * Logs a warning if the system process directly called a method such as |
| 2211 | * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}. |
| 2212 | * The "AsUser" variants allow us to properly enforce the user's restrictions. |
| 2213 | */ |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 2214 | private void warnIfCallingFromSystemProcess() { |
| 2215 | if (Process.myUid() == Process.SYSTEM_UID) { |
| 2216 | Slog.w(TAG, "Calling a method in the system process without a qualified user: " |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 2217 | + Debug.getCallers(5)); |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2221 | private static Resources createResources(IBinder activityToken, LoadedApk pi, String splitName, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2222 | int displayId, Configuration overrideConfig, CompatibilityInfo compatInfo, |
| 2223 | List<ResourcesLoader> resourcesLoader) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2224 | final String[] splitResDirs; |
| 2225 | final ClassLoader classLoader; |
| 2226 | try { |
| 2227 | splitResDirs = pi.getSplitPaths(splitName); |
| 2228 | classLoader = pi.getSplitClassLoader(splitName); |
| 2229 | } catch (NameNotFoundException e) { |
| 2230 | throw new RuntimeException(e); |
| 2231 | } |
| 2232 | return ResourcesManager.getInstance().getResources(activityToken, |
| 2233 | pi.getResDir(), |
| 2234 | splitResDirs, |
Winson | 710e3ec | 2019-02-25 13:38:20 -0800 | [diff] [blame] | 2235 | pi.getOverlayDirs(), |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2236 | pi.getApplicationInfo().sharedLibraryFiles, |
| 2237 | displayId, |
| 2238 | overrideConfig, |
| 2239 | compatInfo, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2240 | classLoader, |
| 2241 | resourcesLoader); |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2244 | @Override |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2245 | public Context createApplicationContext(ApplicationInfo application, int flags) |
| 2246 | throws NameNotFoundException { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2247 | LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(), |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2248 | flags | CONTEXT_REGISTER_PACKAGE); |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2249 | if (pi != null) { |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2250 | ContextImpl c = new ContextImpl(this, mMainThread, pi, null, null, mToken, |
Makoto Onuki | 4becc35 | 2019-01-30 13:30:41 -0800 | [diff] [blame] | 2251 | new UserHandle(UserHandle.getUserId(application.uid)), flags, null, null); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2252 | |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 2253 | final int displayId = getDisplayId(); |
Winson | 710e3ec | 2019-02-25 13:38:20 -0800 | [diff] [blame] | 2254 | |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2255 | c.setResources(createResources(mToken, pi, null, displayId, null, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2256 | getDisplayAdjustments(displayId).getCompatibilityInfo(), null)); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2257 | if (c.mResources != null) { |
| 2258 | return c; |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | throw new PackageManager.NameNotFoundException( |
| 2263 | "Application package " + application.packageName + " not found"); |
| 2264 | } |
| 2265 | |
| 2266 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2267 | public Context createPackageContext(String packageName, int flags) |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2268 | throws NameNotFoundException { |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 2269 | return createPackageContextAsUser(packageName, flags, mUser); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | @Override |
| 2273 | public Context createPackageContextAsUser(String packageName, int flags, UserHandle user) |
| 2274 | throws NameNotFoundException { |
Kenny Guy | c2a4060 | 2014-09-08 18:51:15 +0000 | [diff] [blame] | 2275 | if (packageName.equals("system") || packageName.equals("android")) { |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2276 | // The system resources are loaded in every application, so we can safely copy |
| 2277 | // the context without reloading Resources. |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2278 | return new ContextImpl(this, mMainThread, mPackageInfo, mFeatureId, null, |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2279 | mToken, user, flags, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2282 | LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 2283 | flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier()); |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2284 | if (pi != null) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2285 | ContextImpl c = new ContextImpl(this, mMainThread, pi, mFeatureId, null, |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2286 | mToken, user, flags, null, null); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2287 | |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 2288 | final int displayId = getDisplayId(); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2289 | |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2290 | c.setResources(createResources(mToken, pi, null, displayId, null, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2291 | getDisplayAdjustments(displayId).getCompatibilityInfo(), null)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2292 | if (c.mResources != null) { |
| 2293 | return c; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | // Should be a better exception. |
| 2298 | throw new PackageManager.NameNotFoundException( |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2299 | "Application package " + packageName + " not found"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2300 | } |
| 2301 | |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 2302 | @Override |
Makoto Onuki | e5449f0 | 2019-10-11 20:19:58 -0700 | [diff] [blame] | 2303 | public Context createContextAsUser(UserHandle user, @CreatePackageOptions int flags) { |
Makoto Onuki | b844001 | 2019-10-09 15:33:11 -0700 | [diff] [blame] | 2304 | try { |
Makoto Onuki | e5449f0 | 2019-10-11 20:19:58 -0700 | [diff] [blame] | 2305 | return createPackageContextAsUser(getPackageName(), flags, user); |
Makoto Onuki | b844001 | 2019-10-09 15:33:11 -0700 | [diff] [blame] | 2306 | } catch (NameNotFoundException e) { |
| 2307 | throw new IllegalStateException("Own package not found: package=" + getPackageName()); |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | @Override |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2312 | public Context createContextForSplit(String splitName) throws NameNotFoundException { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2313 | if (!mPackageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2314 | // All Splits are always loaded. |
| 2315 | return this; |
| 2316 | } |
| 2317 | |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2318 | final ClassLoader classLoader = mPackageInfo.getSplitClassLoader(splitName); |
| 2319 | final String[] paths = mPackageInfo.getSplitPaths(splitName); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2320 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2321 | final ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2322 | mFeatureId, splitName, mToken, mUser, mFlags, classLoader, null); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2323 | |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 2324 | final int displayId = getDisplayId(); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2325 | |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2326 | context.setResources(ResourcesManager.getInstance().getResources( |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2327 | mToken, |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2328 | mPackageInfo.getResDir(), |
| 2329 | paths, |
| 2330 | mPackageInfo.getOverlayDirs(), |
| 2331 | mPackageInfo.getApplicationInfo().sharedLibraryFiles, |
| 2332 | displayId, |
| 2333 | null, |
| 2334 | mPackageInfo.getCompatibilityInfo(), |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2335 | classLoader, |
| 2336 | mResources.getLoaders())); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2337 | return context; |
| 2338 | } |
| 2339 | |
| 2340 | @Override |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 2341 | public Context createConfigurationContext(Configuration overrideConfiguration) { |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 2342 | if (overrideConfiguration == null) { |
| 2343 | throw new IllegalArgumentException("overrideConfiguration must not be null"); |
| 2344 | } |
| 2345 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2346 | ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mFeatureId, |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2347 | mSplitName, mToken, mUser, mFlags, mClassLoader, null); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2348 | |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 2349 | final int displayId = getDisplayId(); |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2350 | |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2351 | context.setResources(createResources(mToken, mPackageInfo, mSplitName, displayId, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2352 | overrideConfiguration, getDisplayAdjustments(displayId).getCompatibilityInfo(), |
| 2353 | mResources.getLoaders())); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2354 | return context; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | @Override |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 2358 | public Context createDisplayContext(Display display) { |
| 2359 | if (display == null) { |
| 2360 | throw new IllegalArgumentException("display must not be null"); |
| 2361 | } |
| 2362 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2363 | ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mFeatureId, |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2364 | mSplitName, mToken, mUser, mFlags, mClassLoader, null); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2365 | |
| 2366 | final int displayId = display.getDisplayId(); |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2367 | |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2368 | context.setResources(createResources(mToken, mPackageInfo, mSplitName, displayId, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2369 | null, getDisplayAdjustments(displayId).getCompatibilityInfo(), |
| 2370 | mResources.getLoaders())); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2371 | context.mDisplay = display; |
| 2372 | return context; |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 2373 | } |
| 2374 | |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 2375 | @Override |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2376 | public @NonNull WindowContext createWindowContext(int type) { |
| 2377 | if (getDisplay() == null) { |
| 2378 | throw new UnsupportedOperationException("WindowContext can only be created from " |
| 2379 | + "other visual contexts, such as Activity or one created with " |
| 2380 | + "Context#createDisplayContext(Display)"); |
| 2381 | } |
| 2382 | return new WindowContext(this, null /* token */, type); |
| 2383 | } |
| 2384 | |
| 2385 | ContextImpl createBaseWindowContext(IBinder token) { |
| 2386 | ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mFeatureId, |
| 2387 | mSplitName, token, mUser, mFlags, mClassLoader, null); |
| 2388 | context.mIsUiContext = true; |
| 2389 | return context; |
| 2390 | } |
| 2391 | |
| 2392 | @Override |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2393 | public @NonNull Context createFeatureContext(@Nullable String featureId) { |
| 2394 | return new ContextImpl(this, mMainThread, mPackageInfo, featureId, mSplitName, |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2395 | mToken, mUser, mFlags, mClassLoader, null); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | @Override |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2399 | public Context createDeviceProtectedStorageContext() { |
| 2400 | final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) |
| 2401 | | Context.CONTEXT_DEVICE_PROTECTED_STORAGE; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2402 | return new ContextImpl(this, mMainThread, mPackageInfo, mFeatureId, mSplitName, |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2403 | mToken, mUser, flags, mClassLoader, null); |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | @Override |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2407 | public Context createCredentialProtectedStorageContext() { |
| 2408 | final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE) |
| 2409 | | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2410 | return new ContextImpl(this, mMainThread, mPackageInfo, mFeatureId, mSplitName, |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2411 | mToken, mUser, flags, mClassLoader, null); |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | @Override |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 2415 | public boolean isRestricted() { |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2416 | return (mFlags & Context.CONTEXT_RESTRICTED) != 0; |
| 2417 | } |
| 2418 | |
| 2419 | @Override |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2420 | public boolean isDeviceProtectedStorage() { |
| 2421 | return (mFlags & Context.CONTEXT_DEVICE_PROTECTED_STORAGE) != 0; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | @Override |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2425 | public boolean isCredentialProtectedStorage() { |
| 2426 | return (mFlags & Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE) != 0; |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 2429 | @Override |
Seigo Nonaka | 6d6cd68 | 2017-06-22 08:22:18 -0700 | [diff] [blame] | 2430 | public boolean canLoadUnsafeResources() { |
| 2431 | if (getPackageName().equals(getOpPackageName())) { |
| 2432 | return true; |
| 2433 | } |
| 2434 | return (mFlags & Context.CONTEXT_IGNORE_SECURITY) != 0; |
| 2435 | } |
| 2436 | |
| 2437 | @Override |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 2438 | public Display getDisplay() { |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 2439 | if (mDisplay == null) { |
| 2440 | return mResourcesManager.getAdjustedDisplay(Display.DEFAULT_DISPLAY, |
Bryce Lee | 609bf65 | 2017-02-09 16:50:13 -0800 | [diff] [blame] | 2441 | mResources); |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 2444 | return mDisplay; |
| 2445 | } |
| 2446 | |
| 2447 | @Override |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 2448 | public int getDisplayId() { |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2449 | final Display display = getDisplay(); |
| 2450 | return display != null ? display.getDisplayId() : Display.DEFAULT_DISPLAY; |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | @Override |
Andrii Kulian | b047b8b | 2017-02-08 18:38:26 -0800 | [diff] [blame] | 2454 | public void updateDisplay(int displayId) { |
Bryce Lee | 609bf65 | 2017-02-09 16:50:13 -0800 | [diff] [blame] | 2455 | mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources); |
Andrii Kulian | b047b8b | 2017-02-08 18:38:26 -0800 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | @Override |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 2459 | public DisplayAdjustments getDisplayAdjustments(int displayId) { |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 2460 | return mResources.getDisplayAdjustments(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 2461 | } |
| 2462 | |
Jeff Sharkey | 2c1ba9a | 2016-02-17 15:29:38 -0700 | [diff] [blame] | 2463 | @Override |
| 2464 | public File getDataDir() { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2465 | if (mPackageInfo != null) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 2466 | File res = null; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2467 | if (isCredentialProtectedStorage()) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2468 | res = mPackageInfo.getCredentialProtectedDataDirFile(); |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2469 | } else if (isDeviceProtectedStorage()) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2470 | res = mPackageInfo.getDeviceProtectedDataDirFile(); |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2471 | } else { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2472 | res = mPackageInfo.getDataDirFile(); |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2473 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 2474 | |
| 2475 | if (res != null) { |
Jeff Sharkey | 21f5072 | 2016-04-27 12:38:02 -0600 | [diff] [blame] | 2476 | if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) { |
Jeff Sharkey | 24492ae | 2016-04-25 13:20:25 -0600 | [diff] [blame] | 2477 | Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(), |
| 2478 | new Throwable()); |
| 2479 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 2480 | return res; |
| 2481 | } else { |
| 2482 | throw new RuntimeException( |
| 2483 | "No data directory found for package " + getPackageName()); |
| 2484 | } |
| 2485 | } else { |
| 2486 | throw new RuntimeException( |
| 2487 | "No package details found for package " + getPackageName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2488 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | @Override |
| 2492 | public File getDir(String name, int mode) { |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 2493 | checkMode(mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2494 | name = "app_" + name; |
Jeff Sharkey | 2c1ba9a | 2016-02-17 15:29:38 -0700 | [diff] [blame] | 2495 | File file = makeFilename(getDataDir(), name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2496 | if (!file.exists()) { |
| 2497 | file.mkdir(); |
| 2498 | setFilePermissionsFromMode(file.getPath(), mode, |
| 2499 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH); |
| 2500 | } |
| 2501 | return file; |
| 2502 | } |
| 2503 | |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 2504 | /** {@hide} */ |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2505 | @Override |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 2506 | public UserHandle getUser() { |
| 2507 | return mUser; |
| 2508 | } |
| 2509 | |
| 2510 | /** {@hide} */ |
| 2511 | @Override |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 2512 | public int getUserId() { |
| 2513 | return mUser.getIdentifier(); |
| 2514 | } |
| 2515 | |
Felipe Leme | bb567ae | 2017-10-04 09:56:21 -0700 | [diff] [blame] | 2516 | /** @hide */ |
| 2517 | @Override |
| 2518 | public AutofillClient getAutofillClient() { |
| 2519 | return mAutofillClient; |
| 2520 | } |
| 2521 | |
| 2522 | /** @hide */ |
| 2523 | @Override |
| 2524 | public void setAutofillClient(AutofillClient client) { |
| 2525 | mAutofillClient = client; |
| 2526 | } |
| 2527 | |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2528 | /** @hide */ |
| 2529 | @Override |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 2530 | public AutofillOptions getAutofillOptions() { |
| 2531 | return mAutofillOptions; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | /** @hide */ |
| 2535 | @Override |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 2536 | public void setAutofillOptions(AutofillOptions options) { |
| 2537 | mAutofillOptions = options; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2538 | } |
| 2539 | |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 2540 | /** @hide */ |
| 2541 | @Override |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 2542 | public ContentCaptureOptions getContentCaptureOptions() { |
| 2543 | return mContentCaptureOptions; |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | /** @hide */ |
| 2547 | @Override |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 2548 | public void setContentCaptureOptions(ContentCaptureOptions options) { |
| 2549 | mContentCaptureOptions = options; |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 2550 | } |
| 2551 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2552 | @UnsupportedAppUsage |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 2553 | static ContextImpl createSystemContext(ActivityThread mainThread) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2554 | LoadedApk packageInfo = new LoadedApk(mainThread); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2555 | ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, null, |
| 2556 | 0, null, null); |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2557 | context.setResources(packageInfo.getResources()); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2558 | context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(), |
Adam Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 2559 | context.mResourcesManager.getDisplayMetrics()); |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2560 | context.mIsSystemOrSystemUiContext = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2561 | return context; |
| 2562 | } |
| 2563 | |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 2564 | /** |
| 2565 | * System Context to be used for UI. This Context has resources that can be themed. |
Adam Lesinski | 6f1a917 | 2017-04-10 16:35:19 -0700 | [diff] [blame] | 2566 | * Make sure that the created system UI context shares the same LoadedApk as the system context. |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 2567 | * @param systemContext The system context which created by |
| 2568 | * {@link #createSystemContext(ActivityThread)}. |
| 2569 | * @param displayId The ID of the display where the UI is shown. |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 2570 | */ |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 2571 | static ContextImpl createSystemUiContext(ContextImpl systemContext, int displayId) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2572 | final LoadedApk packageInfo = systemContext.mPackageInfo; |
| 2573 | ContextImpl context = new ContextImpl(null, systemContext.mMainThread, packageInfo, null, |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2574 | null, null, null, 0, null, null); |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 2575 | context.setResources(createResources(null, packageInfo, null, displayId, null, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2576 | packageInfo.getCompatibilityInfo(), null)); |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 2577 | context.updateDisplay(displayId); |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2578 | context.mIsSystemOrSystemUiContext = true; |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 2579 | return context; |
| 2580 | } |
| 2581 | |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 2582 | /** |
| 2583 | * The overloaded method of {@link #createSystemUiContext(ContextImpl, int)}. |
| 2584 | * Uses {@Code Display.DEFAULT_DISPLAY} as the target display. |
| 2585 | */ |
| 2586 | static ContextImpl createSystemUiContext(ContextImpl systemContext) { |
| 2587 | return createSystemUiContext(systemContext, Display.DEFAULT_DISPLAY); |
| 2588 | } |
| 2589 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2590 | @UnsupportedAppUsage |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2591 | static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) { |
Makoto Onuki | 4becc35 | 2019-01-30 13:30:41 -0800 | [diff] [blame] | 2592 | return createAppContext(mainThread, packageInfo, null); |
| 2593 | } |
| 2594 | |
| 2595 | static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo, |
| 2596 | String opPackageName) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2597 | if (packageInfo == null) throw new IllegalArgumentException("packageInfo"); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2598 | ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, null, |
| 2599 | 0, null, opPackageName); |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2600 | context.setResources(packageInfo.getResources()); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2601 | return context; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2604 | @UnsupportedAppUsage |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2605 | static ContextImpl createActivityContext(ActivityThread mainThread, |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2606 | LoadedApk packageInfo, ActivityInfo activityInfo, IBinder activityToken, int displayId, |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 2607 | Configuration overrideConfiguration) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2608 | if (packageInfo == null) throw new IllegalArgumentException("packageInfo"); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2609 | |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2610 | String[] splitDirs = packageInfo.getSplitResDirs(); |
| 2611 | ClassLoader classLoader = packageInfo.getClassLoader(); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2612 | |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2613 | if (packageInfo.getApplicationInfo().requestsIsolatedSplitLoading()) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2614 | Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "SplitDependencies"); |
| 2615 | try { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2616 | classLoader = packageInfo.getSplitClassLoader(activityInfo.splitName); |
| 2617 | splitDirs = packageInfo.getSplitPaths(activityInfo.splitName); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2618 | } catch (NameNotFoundException e) { |
| 2619 | // Nothing above us can handle a NameNotFoundException, better crash. |
| 2620 | throw new RuntimeException(e); |
| 2621 | } finally { |
| 2622 | Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); |
| 2623 | } |
| 2624 | } |
| 2625 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2626 | ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, |
| 2627 | activityInfo.splitName, activityToken, null, 0, classLoader, null); |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2628 | context.mIsUiContext = true; |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2629 | |
| 2630 | // Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY. |
| 2631 | displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY; |
| 2632 | |
| 2633 | final CompatibilityInfo compatInfo = (displayId == Display.DEFAULT_DISPLAY) |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2634 | ? packageInfo.getCompatibilityInfo() |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2635 | : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; |
| 2636 | |
Adam Lesinski | c970194 | 2016-11-28 19:44:19 -0800 | [diff] [blame] | 2637 | final ResourcesManager resourcesManager = ResourcesManager.getInstance(); |
| 2638 | |
| 2639 | // Create the base resources for which all configuration contexts for this Activity |
| 2640 | // will be rebased upon. |
Jason Monk | bd98364 | 2017-02-24 16:57:40 -0500 | [diff] [blame] | 2641 | context.setResources(resourcesManager.createBaseActivityResources(activityToken, |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2642 | packageInfo.getResDir(), |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2643 | splitDirs, |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2644 | packageInfo.getOverlayDirs(), |
| 2645 | packageInfo.getApplicationInfo().sharedLibraryFiles, |
Adam Lesinski | c970194 | 2016-11-28 19:44:19 -0800 | [diff] [blame] | 2646 | displayId, |
| 2647 | overrideConfiguration, |
| 2648 | compatInfo, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 2649 | classLoader, |
| 2650 | packageInfo.getApplication().getResources().getLoaders())); |
Adam Lesinski | c970194 | 2016-11-28 19:44:19 -0800 | [diff] [blame] | 2651 | context.mDisplay = resourcesManager.getAdjustedDisplay(displayId, |
Bryce Lee | 609bf65 | 2017-02-09 16:50:13 -0800 | [diff] [blame] | 2652 | context.getResources()); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2653 | return context; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2654 | } |
| 2655 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 2656 | private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread, |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2657 | @NonNull LoadedApk packageInfo, @Nullable String featureId, |
| 2658 | @Nullable String splitName, @Nullable IBinder activityToken, @Nullable UserHandle user, |
| 2659 | int flags, @Nullable ClassLoader classLoader, @Nullable String overrideOpPackageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2660 | mOuterContext = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2662 | // If creator didn't specify which storage to use, use the default |
| 2663 | // location for application. |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2664 | if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE |
| 2665 | | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2666 | final File dataDir = packageInfo.getDataDirFile(); |
| 2667 | if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) { |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2668 | flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE; |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2669 | } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) { |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2670 | flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2671 | } |
| 2672 | } |
| 2673 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2674 | mMainThread = mainThread; |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2675 | mToken = activityToken; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 2676 | mFlags = flags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2677 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2678 | if (user == null) { |
| 2679 | user = Process.myUserHandle(); |
| 2680 | } |
| 2681 | mUser = user; |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 2682 | |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2683 | mPackageInfo = packageInfo; |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 2684 | mSplitName = splitName; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2685 | mClassLoader = classLoader; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2686 | mResourcesManager = ResourcesManager.getInstance(); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2687 | |
Makoto Onuki | 4becc35 | 2019-01-30 13:30:41 -0800 | [diff] [blame] | 2688 | String opPackageName; |
| 2689 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2690 | if (container != null) { |
| 2691 | mBasePackageName = container.mBasePackageName; |
Makoto Onuki | 4becc35 | 2019-01-30 13:30:41 -0800 | [diff] [blame] | 2692 | opPackageName = container.mOpPackageName; |
Jason Monk | bd98364 | 2017-02-24 16:57:40 -0500 | [diff] [blame] | 2693 | setResources(container.mResources); |
Adam Lesinski | 8fa7107 | 2016-11-18 18:13:55 -0800 | [diff] [blame] | 2694 | mDisplay = container.mDisplay; |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2695 | mIsSystemOrSystemUiContext = container.mIsSystemOrSystemUiContext; |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 2696 | } else { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2697 | mBasePackageName = packageInfo.mPackageName; |
| 2698 | ApplicationInfo ainfo = packageInfo.getApplicationInfo(); |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 2699 | if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) { |
| 2700 | // Special case: system components allow themselves to be loaded in to other |
| 2701 | // processes. For purposes of app ops, we must then consider the context as |
| 2702 | // belonging to the package of this process, not the system itself, otherwise |
| 2703 | // the package+uid verifications in app ops will fail. |
Makoto Onuki | 4becc35 | 2019-01-30 13:30:41 -0800 | [diff] [blame] | 2704 | opPackageName = ActivityThread.currentPackageName(); |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 2705 | } else { |
Makoto Onuki | 4becc35 | 2019-01-30 13:30:41 -0800 | [diff] [blame] | 2706 | opPackageName = mBasePackageName; |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 2707 | } |
| 2708 | } |
Xin Guan | 2bcb97b | 2014-09-10 15:24:30 -0500 | [diff] [blame] | 2709 | |
Makoto Onuki | 4becc35 | 2019-01-30 13:30:41 -0800 | [diff] [blame] | 2710 | mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2711 | mFeatureId = featureId; |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 2712 | mContentResolver = new ApplicationContentResolver(this, mainThread); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2713 | } |
| 2714 | |
Jason Monk | bd98364 | 2017-02-24 16:57:40 -0500 | [diff] [blame] | 2715 | void setResources(Resources r) { |
Jason Monk | bd60e5b | 2017-03-02 12:55:00 -0500 | [diff] [blame] | 2716 | if (r instanceof CompatResources) { |
| 2717 | ((CompatResources) r).setContext(this); |
Jason Monk | bd98364 | 2017-02-24 16:57:40 -0500 | [diff] [blame] | 2718 | } |
Jason Monk | bd60e5b | 2017-03-02 12:55:00 -0500 | [diff] [blame] | 2719 | mResources = r; |
Jason Monk | bd98364 | 2017-02-24 16:57:40 -0500 | [diff] [blame] | 2720 | } |
| 2721 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 2722 | void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) { |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2723 | mPackageInfo.installSystemApplicationInfo(info, classLoader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2724 | } |
| 2725 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2726 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2727 | final void scheduleFinalCleanup(String who, String what) { |
| 2728 | mMainThread.scheduleContextCleanup(this, who, what); |
| 2729 | } |
| 2730 | |
| 2731 | final void performFinalCleanup(String who, String what) { |
| 2732 | //Log.i(TAG, "Cleanup up context: " + this); |
Todd Kennedy | 233a0b1 | 2018-01-29 20:30:24 +0000 | [diff] [blame] | 2733 | mPackageInfo.removeContextRegistrations(getOuterContext(), who, what); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2734 | } |
| 2735 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2736 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | final Context getReceiverRestrictedContext() { |
| 2738 | if (mReceiverRestrictedContext != null) { |
| 2739 | return mReceiverRestrictedContext; |
| 2740 | } |
| 2741 | return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext()); |
| 2742 | } |
| 2743 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2744 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | final void setOuterContext(Context context) { |
| 2746 | mOuterContext = context; |
| 2747 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2748 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2749 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2750 | final Context getOuterContext() { |
| 2751 | return mOuterContext; |
| 2752 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2753 | |
Tony Mak | 46aabe5 | 2016-11-14 12:53:06 +0000 | [diff] [blame] | 2754 | @Override |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2755 | @UnsupportedAppUsage |
Tony Mak | 46aabe5 | 2016-11-14 12:53:06 +0000 | [diff] [blame] | 2756 | public IBinder getActivityToken() { |
Andrii Kulian | 4a31697 | 2020-01-21 21:41:38 -0800 | [diff] [blame] | 2757 | return mToken; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2758 | } |
| 2759 | |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 2760 | private void checkMode(int mode) { |
| 2761 | if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) { |
| 2762 | if ((mode & MODE_WORLD_READABLE) != 0) { |
| 2763 | throw new SecurityException("MODE_WORLD_READABLE no longer supported"); |
| 2764 | } |
| 2765 | if ((mode & MODE_WORLD_WRITEABLE) != 0) { |
| 2766 | throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported"); |
| 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2771 | @SuppressWarnings("deprecation") |
Brad Fitzpatrick | d3da440 | 2010-11-10 08:27:11 -0800 | [diff] [blame] | 2772 | static void setFilePermissionsFromMode(String name, int mode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2773 | int extraPermissions) { |
| 2774 | int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR |
| 2775 | |FileUtils.S_IRGRP|FileUtils.S_IWGRP |
| 2776 | |extraPermissions; |
| 2777 | if ((mode&MODE_WORLD_READABLE) != 0) { |
| 2778 | perms |= FileUtils.S_IROTH; |
| 2779 | } |
| 2780 | if ((mode&MODE_WORLD_WRITEABLE) != 0) { |
| 2781 | perms |= FileUtils.S_IWOTH; |
| 2782 | } |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 2783 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2784 | Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode) |
| 2785 | + ", perms=0x" + Integer.toHexString(perms)); |
| 2786 | } |
| 2787 | FileUtils.setPermissions(name, perms, -1, -1); |
| 2788 | } |
| 2789 | |
| 2790 | private File makeFilename(File base, String name) { |
| 2791 | if (name.indexOf(File.separatorChar) < 0) { |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 2792 | final File res = new File(base, name); |
| 2793 | // We report as filesystem access here to give us the best shot at |
| 2794 | // detecting apps that will pass the path down to native code. |
| 2795 | BlockGuard.getVmPolicy().onPathAccess(res.getPath()); |
| 2796 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2797 | } |
| 2798 | throw new IllegalArgumentException( |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2799 | "File " + name + " contains a path separator"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2800 | } |
| 2801 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2802 | /** |
| 2803 | * Ensure that given directories exist, trying to create them if missing. If |
| 2804 | * unable to create, they are filtered by replacing with {@code null}. |
| 2805 | */ |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 2806 | private File[] ensureExternalDirsExistOrFilter(File[] dirs) { |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 2807 | final StorageManager sm = getSystemService(StorageManager.class); |
| 2808 | final File[] result = new File[dirs.length]; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2809 | for (int i = 0; i < dirs.length; i++) { |
| 2810 | File dir = dirs[i]; |
| 2811 | if (!dir.exists()) { |
| 2812 | if (!dir.mkdirs()) { |
Christopher Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 2813 | // recheck existence in case of cross-process race |
| 2814 | if (!dir.exists()) { |
| 2815 | // Failing to mkdir() may be okay, since we might not have |
| 2816 | // enough permissions; ask vold to create on our behalf. |
Christopher Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 2817 | try { |
Jeff Sharkey | ae26646 | 2017-11-27 13:32:24 -0700 | [diff] [blame] | 2818 | sm.mkdirs(dir); |
Jeff Sharkey | 98329459 | 2015-07-13 10:25:31 -0700 | [diff] [blame] | 2819 | } catch (Exception e) { |
| 2820 | Log.w(TAG, "Failed to ensure " + dir + ": " + e); |
Christopher Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 2821 | dir = null; |
| 2822 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2823 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2824 | } |
| 2825 | } |
| 2826 | result[i] = dir; |
| 2827 | } |
| 2828 | return result; |
| 2829 | } |
| 2830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2831 | // ---------------------------------------------------------------------- |
| 2832 | // ---------------------------------------------------------------------- |
| 2833 | // ---------------------------------------------------------------------- |
| 2834 | |
| 2835 | private static final class ApplicationContentResolver extends ContentResolver { |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2836 | @UnsupportedAppUsage |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2837 | private final ActivityThread mMainThread; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2838 | |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 2839 | public ApplicationContentResolver(Context context, ActivityThread mainThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2840 | super(context); |
Daulet Zhanguzin | 0af97d6 | 2019-12-30 15:41:28 +0000 | [diff] [blame] | 2841 | mMainThread = Objects.requireNonNull(mainThread); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | @Override |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 2845 | @UnsupportedAppUsage |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2846 | protected IContentProvider acquireProvider(Context context, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2847 | return mMainThread.acquireProvider(context, |
| 2848 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2849 | resolveUserIdFromAuthority(auth), true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2853 | protected IContentProvider acquireExistingProvider(Context context, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2854 | return mMainThread.acquireExistingProvider(context, |
| 2855 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2856 | resolveUserIdFromAuthority(auth), true); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | @Override |
| 2860 | public boolean releaseProvider(IContentProvider provider) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 2861 | return mMainThread.releaseProvider(provider, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2862 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2863 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2864 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2865 | protected IContentProvider acquireUnstableProvider(Context c, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2866 | return mMainThread.acquireProvider(c, |
| 2867 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2868 | resolveUserIdFromAuthority(auth), false); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | @Override |
| 2872 | public boolean releaseUnstableProvider(IContentProvider icp) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 2873 | return mMainThread.releaseProvider(icp, false); |
| 2874 | } |
| 2875 | |
| 2876 | @Override |
| 2877 | public void unstableProviderDied(IContentProvider icp) { |
| 2878 | mMainThread.handleUnstableProviderDied(icp.asBinder(), true); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2879 | } |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 2880 | |
| 2881 | @Override |
| 2882 | public void appNotRespondingViaProvider(IContentProvider icp) { |
| 2883 | mMainThread.appNotRespondingViaProvider(icp.asBinder()); |
| 2884 | } |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2885 | |
| 2886 | /** @hide */ |
| 2887 | protected int resolveUserIdFromAuthority(String auth) { |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 2888 | return ContentProvider.getUserIdFromAuthority(auth, getUserId()); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2889 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2890 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2891 | } |