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