Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Adam Lesinski | d33ef56 | 2017-01-19 14:49:59 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.IIntentReceiver; |
| 25 | import android.content.Intent; |
| 26 | import android.content.ServiceConnection; |
| 27 | import android.content.pm.ApplicationInfo; |
| 28 | import android.content.pm.IPackageManager; |
| 29 | import android.content.pm.PackageManager; |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 30 | import android.content.pm.PackageManager.NameNotFoundException; |
Calin Juravle | 6ae39fc | 2018-01-19 20:32:47 -0800 | [diff] [blame] | 31 | import android.content.pm.dex.ArtManager; |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 32 | import android.content.pm.split.SplitDependencyLoader; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 33 | import android.content.res.AssetManager; |
| 34 | import android.content.res.CompatibilityInfo; |
Jeff Hamilton | 7541ca4d | 2017-10-24 15:20:43 -0400 | [diff] [blame] | 35 | import android.content.res.Configuration; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 36 | import android.content.res.Resources; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 37 | import android.os.Build; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 38 | import android.os.Bundle; |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 39 | import android.os.FileUtils; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 40 | import android.os.Handler; |
| 41 | import android.os.IBinder; |
| 42 | import android.os.Process; |
| 43 | import android.os.RemoteException; |
Brad Fitzpatrick | 624d50f | 2010-11-09 14:25:12 -0800 | [diff] [blame] | 44 | import android.os.StrictMode; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 45 | import android.os.SystemProperties; |
Dianne Hackborn | 1ded0b1 | 2012-04-26 14:14:50 -0700 | [diff] [blame] | 46 | import android.os.Trace; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 47 | import android.os.UserHandle; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 48 | import android.text.TextUtils; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 49 | import android.util.AndroidRuntimeException; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 50 | import android.util.ArrayMap; |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 51 | import android.util.Log; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 52 | import android.util.Slog; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 53 | import android.util.SparseArray; |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 54 | import android.view.Display; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 55 | import android.view.DisplayAdjustments; |
Calin Juravle | 6ae39fc | 2018-01-19 20:32:47 -0800 | [diff] [blame] | 56 | |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 57 | import com.android.internal.util.ArrayUtils; |
Calin Juravle | 6ae39fc | 2018-01-19 20:32:47 -0800 | [diff] [blame] | 58 | |
Narayan Kamath | 7dba6eb | 2014-07-16 08:53:30 +0100 | [diff] [blame] | 59 | import dalvik.system.VMRuntime; |
Calin Juravle | 6ae39fc | 2018-01-19 20:32:47 -0800 | [diff] [blame] | 60 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 61 | import java.io.File; |
| 62 | import java.io.IOException; |
| 63 | import java.io.InputStream; |
| 64 | import java.lang.ref.WeakReference; |
Adam Lesinski | 1e466385 | 2014-08-15 14:47:28 -0700 | [diff] [blame] | 65 | import java.lang.reflect.InvocationTargetException; |
| 66 | import java.lang.reflect.Method; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 67 | import java.net.URL; |
Jeff Sharkey | 8a4c972 | 2014-06-16 13:48:42 -0700 | [diff] [blame] | 68 | import java.util.ArrayList; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 69 | import java.util.Arrays; |
Jeff Sharkey | 8a4c972 | 2014-06-16 13:48:42 -0700 | [diff] [blame] | 70 | import java.util.Collections; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 71 | import java.util.Enumeration; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 72 | import java.util.List; |
Narayan Kamath | 2053168 | 2014-07-14 13:18:43 +0100 | [diff] [blame] | 73 | import java.util.Objects; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 74 | |
| 75 | final class IntentReceiverLeaked extends AndroidRuntimeException { |
| 76 | public IntentReceiverLeaked(String msg) { |
| 77 | super(msg); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | final class ServiceConnectionLeaked extends AndroidRuntimeException { |
| 82 | public ServiceConnectionLeaked(String msg) { |
| 83 | super(msg); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Local state maintained about a currently loaded .apk. |
| 89 | * @hide |
| 90 | */ |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 91 | public final class LoadedApk { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 92 | static final String TAG = "LoadedApk"; |
| 93 | static final boolean DEBUG = false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 94 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 95 | private final ActivityThread mActivityThread; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 96 | final String mPackageName; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 97 | private ApplicationInfo mApplicationInfo; |
| 98 | private String mAppDir; |
| 99 | private String mResDir; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 100 | private String[] mOverlayDirs; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 101 | private String mDataDir; |
| 102 | private String mLibDir; |
| 103 | private File mDataDirFile; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 104 | private File mDeviceProtectedDataDirFile; |
| 105 | private File mCredentialProtectedDataDirFile; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 106 | private final ClassLoader mBaseClassLoader; |
| 107 | private final boolean mSecurityViolation; |
| 108 | private final boolean mIncludeCode; |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 109 | private final boolean mRegisterPackage; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 110 | private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments(); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 111 | /** WARNING: This may change. Don't hold external references to it. */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 112 | Resources mResources; |
| 113 | private ClassLoader mClassLoader; |
| 114 | private Application mApplication; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 115 | |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 116 | private String[] mSplitNames; |
| 117 | private String[] mSplitAppDirs; |
| 118 | private String[] mSplitResDirs; |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 119 | private String[] mSplitClassLoaderNames; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 120 | |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 121 | private final ArrayMap<Context, ArrayMap<BroadcastReceiver, ReceiverDispatcher>> mReceivers |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 122 | = new ArrayMap<>(); |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 123 | private final ArrayMap<Context, ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>> mUnregisteredReceivers |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 124 | = new ArrayMap<>(); |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 125 | private final ArrayMap<Context, ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher>> mServices |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 126 | = new ArrayMap<>(); |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 127 | private final ArrayMap<Context, ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher>> mUnboundServices |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 128 | = new ArrayMap<>(); |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 129 | private AppComponentFactory mAppComponentFactory; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 130 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 131 | Application getApplication() { |
| 132 | return mApplication; |
| 133 | } |
| 134 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Create information about a new .apk |
| 137 | * |
| 138 | * NOTE: This constructor is called with ActivityThread's lock held, |
| 139 | * so MUST NOT call back out to the activity manager. |
| 140 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 141 | public LoadedApk(ActivityThread activityThread, ApplicationInfo aInfo, |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 142 | CompatibilityInfo compatInfo, ClassLoader baseLoader, |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 143 | boolean securityViolation, boolean includeCode, boolean registerPackage) { |
Narayan Kamath | 7dba6eb | 2014-07-16 08:53:30 +0100 | [diff] [blame] | 144 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 145 | mActivityThread = activityThread; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 146 | setApplicationInfo(aInfo); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 147 | mPackageName = aInfo.packageName; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 148 | mBaseClassLoader = baseLoader; |
| 149 | mSecurityViolation = securityViolation; |
| 150 | mIncludeCode = includeCode; |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 151 | mRegisterPackage = registerPackage; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 152 | mDisplayAdjustments.setCompatibilityInfo(compatInfo); |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 153 | mAppComponentFactory = createAppFactory(mApplicationInfo, mBaseClassLoader); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Narayan Kamath | 7dba6eb | 2014-07-16 08:53:30 +0100 | [diff] [blame] | 156 | private static ApplicationInfo adjustNativeLibraryPaths(ApplicationInfo info) { |
| 157 | // If we're dealing with a multi-arch application that has both |
| 158 | // 32 and 64 bit shared libraries, we might need to choose the secondary |
| 159 | // depending on what the current runtime's instruction set is. |
| 160 | if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) { |
| 161 | final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet(); |
jgu21 | 4741cd9 | 2014-12-17 17:23:29 -0500 | [diff] [blame] | 162 | |
| 163 | // Get the instruction set that the libraries of secondary Abi is supported. |
| 164 | // In presence of a native bridge this might be different than the one secondary Abi used. |
| 165 | String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi); |
| 166 | final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa); |
| 167 | secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa; |
Narayan Kamath | 7dba6eb | 2014-07-16 08:53:30 +0100 | [diff] [blame] | 168 | |
| 169 | // If the runtimeIsa is the same as the primary isa, then we do nothing. |
| 170 | // Everything will be set up correctly because info.nativeLibraryDir will |
| 171 | // correspond to the right ISA. |
| 172 | if (runtimeIsa.equals(secondaryIsa)) { |
| 173 | final ApplicationInfo modified = new ApplicationInfo(info); |
| 174 | modified.nativeLibraryDir = modified.secondaryNativeLibraryDir; |
Dmitriy Ivanov | e56b3f6 | 2015-06-05 22:00:13 -0700 | [diff] [blame] | 175 | modified.primaryCpuAbi = modified.secondaryCpuAbi; |
Narayan Kamath | 7dba6eb | 2014-07-16 08:53:30 +0100 | [diff] [blame] | 176 | return modified; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return info; |
| 181 | } |
| 182 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Create information about the system package. |
| 185 | * Must call {@link #installSystemApplicationInfo} later. |
| 186 | */ |
| 187 | LoadedApk(ActivityThread activityThread) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 188 | mActivityThread = activityThread; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 189 | mApplicationInfo = new ApplicationInfo(); |
| 190 | mApplicationInfo.packageName = "android"; |
| 191 | mPackageName = "android"; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 192 | mAppDir = null; |
| 193 | mResDir = null; |
Jeff Sharkey | 8a4c972 | 2014-06-16 13:48:42 -0700 | [diff] [blame] | 194 | mSplitAppDirs = null; |
| 195 | mSplitResDirs = null; |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 196 | mSplitClassLoaderNames = null; |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 197 | mOverlayDirs = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 198 | mDataDir = null; |
| 199 | mDataDirFile = null; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 200 | mDeviceProtectedDataDirFile = null; |
| 201 | mCredentialProtectedDataDirFile = null; |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 202 | mLibDir = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 203 | mBaseClassLoader = null; |
| 204 | mSecurityViolation = false; |
| 205 | mIncludeCode = true; |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 206 | mRegisterPackage = false; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 207 | mClassLoader = ClassLoader.getSystemClassLoader(); |
| 208 | mResources = Resources.getSystem(); |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 209 | mAppComponentFactory = createAppFactory(mApplicationInfo, mClassLoader); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Sets application info about the system package. |
| 214 | */ |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 215 | void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 216 | assert info.packageName.equals("android"); |
| 217 | mApplicationInfo = info; |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 218 | mClassLoader = classLoader; |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 219 | mAppComponentFactory = createAppFactory(info, classLoader); |
| 220 | } |
| 221 | |
| 222 | private AppComponentFactory createAppFactory(ApplicationInfo appInfo, ClassLoader cl) { |
| 223 | if (appInfo.appComponentFactory != null) { |
| 224 | try { |
| 225 | return (AppComponentFactory) cl.loadClass(appInfo.appComponentFactory) |
| 226 | .newInstance(); |
| 227 | } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { |
| 228 | Slog.e(TAG, "Unable to instantiate appComponentFactory", e); |
| 229 | } |
| 230 | } |
| 231 | return AppComponentFactory.DEFAULT; |
| 232 | } |
| 233 | |
| 234 | public AppComponentFactory getAppFactory() { |
| 235 | return mAppComponentFactory; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | public String getPackageName() { |
| 239 | return mPackageName; |
| 240 | } |
| 241 | |
| 242 | public ApplicationInfo getApplicationInfo() { |
| 243 | return mApplicationInfo; |
| 244 | } |
| 245 | |
Jeff Sharkey | 369f509 | 2016-02-29 11:16:21 -0700 | [diff] [blame] | 246 | public int getTargetSdkVersion() { |
| 247 | return mApplicationInfo.targetSdkVersion; |
| 248 | } |
| 249 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 250 | public boolean isSecurityViolation() { |
| 251 | return mSecurityViolation; |
| 252 | } |
| 253 | |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 254 | public CompatibilityInfo getCompatibilityInfo() { |
| 255 | return mDisplayAdjustments.getCompatibilityInfo(); |
| 256 | } |
| 257 | |
| 258 | public void setCompatibilityInfo(CompatibilityInfo compatInfo) { |
| 259 | mDisplayAdjustments.setCompatibilityInfo(compatInfo); |
| 260 | } |
| 261 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 262 | /** |
| 263 | * Gets the array of shared libraries that are listed as |
| 264 | * used by the given package. |
| 265 | * |
| 266 | * @param packageName the name of the package (note: not its |
| 267 | * file name) |
| 268 | * @return null-ok; the array of shared libraries, each one |
| 269 | * a fully-qualified path |
| 270 | */ |
| 271 | private static String[] getLibrariesFor(String packageName) { |
| 272 | ApplicationInfo ai = null; |
| 273 | try { |
| 274 | ai = ActivityThread.getPackageManager().getApplicationInfo(packageName, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 275 | PackageManager.GET_SHARED_LIBRARY_FILES, UserHandle.myUserId()); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 276 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 277 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if (ai == null) { |
| 281 | return null; |
| 282 | } |
| 283 | |
| 284 | return ai.sharedLibraryFiles; |
| 285 | } |
| 286 | |
Adam Lesinski | d33ef56 | 2017-01-19 14:49:59 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Update the ApplicationInfo for an app. If oldPaths is null, all the paths are considered |
| 289 | * new. |
| 290 | * @param aInfo The new ApplicationInfo to use for this LoadedApk |
| 291 | * @param oldPaths The code paths for the old ApplicationInfo object. null means no paths can |
| 292 | * be reused. |
| 293 | */ |
| 294 | public void updateApplicationInfo(@NonNull ApplicationInfo aInfo, |
| 295 | @Nullable List<String> oldPaths) { |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 296 | setApplicationInfo(aInfo); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 297 | |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 298 | final List<String> newPaths = new ArrayList<>(); |
Dimitry Ivanov | 638d810 | 2017-02-22 15:39:42 -0800 | [diff] [blame] | 299 | makePaths(mActivityThread, aInfo, newPaths); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 300 | final List<String> addedPaths = new ArrayList<>(newPaths.size()); |
| 301 | |
| 302 | if (oldPaths != null) { |
| 303 | for (String path : newPaths) { |
| 304 | final String apkName = path.substring(path.lastIndexOf(File.separator)); |
| 305 | boolean match = false; |
| 306 | for (String oldPath : oldPaths) { |
Adam Lesinski | a5ca624 | 2017-03-01 15:45:12 -0800 | [diff] [blame] | 307 | final String oldApkName = oldPath.substring(oldPath.lastIndexOf(File.separator)); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 308 | if (apkName.equals(oldApkName)) { |
| 309 | match = true; |
| 310 | break; |
| 311 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 312 | } |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 313 | if (!match) { |
| 314 | addedPaths.add(path); |
| 315 | } |
| 316 | } |
| 317 | } else { |
| 318 | addedPaths.addAll(newPaths); |
| 319 | } |
| 320 | synchronized (this) { |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 321 | createOrUpdateClassLoaderLocked(addedPaths); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 322 | if (mResources != null) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 323 | final String[] splitPaths; |
| 324 | try { |
| 325 | splitPaths = getSplitPaths(null); |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 326 | } catch (NameNotFoundException e) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 327 | // This should NEVER fail. |
| 328 | throw new AssertionError("null split not found"); |
| 329 | } |
| 330 | |
| 331 | mResources = ResourcesManager.getInstance().getResources(null, mResDir, |
Adam Lesinski | 0e61883 | 2017-02-07 11:40:12 -0800 | [diff] [blame] | 332 | splitPaths, mOverlayDirs, mApplicationInfo.sharedLibraryFiles, |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 333 | Display.DEFAULT_DISPLAY, null, getCompatibilityInfo(), |
| 334 | getClassLoader()); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 337 | mAppComponentFactory = createAppFactory(aInfo, mClassLoader); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | private void setApplicationInfo(ApplicationInfo aInfo) { |
| 341 | final int myUid = Process.myUid(); |
| 342 | aInfo = adjustNativeLibraryPaths(aInfo); |
| 343 | mApplicationInfo = aInfo; |
| 344 | mAppDir = aInfo.sourceDir; |
| 345 | mResDir = aInfo.uid == myUid ? aInfo.sourceDir : aInfo.publicSourceDir; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 346 | mOverlayDirs = aInfo.resourceDirs; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 347 | mDataDir = aInfo.dataDir; |
| 348 | mLibDir = aInfo.nativeLibraryDir; |
| 349 | mDataDirFile = FileUtils.newFileOrNull(aInfo.dataDir); |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 350 | mDeviceProtectedDataDirFile = FileUtils.newFileOrNull(aInfo.deviceProtectedDataDir); |
| 351 | mCredentialProtectedDataDirFile = FileUtils.newFileOrNull(aInfo.credentialProtectedDataDir); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 352 | |
| 353 | mSplitNames = aInfo.splitNames; |
| 354 | mSplitAppDirs = aInfo.splitSourceDirs; |
| 355 | mSplitResDirs = aInfo.uid == myUid ? aInfo.splitSourceDirs : aInfo.splitPublicSourceDirs; |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 356 | mSplitClassLoaderNames = aInfo.splitClassLoaderNames; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 357 | |
| 358 | if (aInfo.requestsIsolatedSplitLoading() && !ArrayUtils.isEmpty(mSplitNames)) { |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 359 | mSplitLoader = new SplitDependencyLoaderImpl(aInfo.splitDependencies); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 360 | } |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Dimitry Ivanov | 638d810 | 2017-02-22 15:39:42 -0800 | [diff] [blame] | 363 | public static void makePaths(ActivityThread activityThread, |
| 364 | ApplicationInfo aInfo, |
| 365 | List<String> outZipPaths) { |
| 366 | makePaths(activityThread, false, aInfo, outZipPaths, null); |
| 367 | } |
| 368 | |
| 369 | public static void makePaths(ActivityThread activityThread, |
| 370 | boolean isBundledApp, |
| 371 | ApplicationInfo aInfo, |
| 372 | List<String> outZipPaths, |
| 373 | List<String> outLibPaths) { |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 374 | final String appDir = aInfo.sourceDir; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 375 | final String libDir = aInfo.nativeLibraryDir; |
| 376 | final String[] sharedLibraries = aInfo.sharedLibraryFiles; |
| 377 | |
| 378 | outZipPaths.clear(); |
| 379 | outZipPaths.add(appDir); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 380 | |
| 381 | // Do not load all available splits if the app requested isolated split loading. |
| 382 | if (aInfo.splitSourceDirs != null && !aInfo.requestsIsolatedSplitLoading()) { |
| 383 | Collections.addAll(outZipPaths, aInfo.splitSourceDirs); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (outLibPaths != null) { |
| 387 | outLibPaths.clear(); |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * The following is a bit of a hack to inject |
| 392 | * instrumentation into the system: If the app |
| 393 | * being started matches one of the instrumentation names, |
| 394 | * then we combine both the "instrumentation" and |
| 395 | * "instrumented" app into the path, along with the |
| 396 | * concatenation of both apps' shared library lists. |
| 397 | */ |
| 398 | |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 399 | String[] instrumentationLibs = null; |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 400 | // activityThread will be null when called from the WebView zygote; just assume |
| 401 | // no instrumentation applies in this case. |
| 402 | if (activityThread != null) { |
| 403 | String instrumentationPackageName = activityThread.mInstrumentationPackageName; |
| 404 | String instrumentationAppDir = activityThread.mInstrumentationAppDir; |
| 405 | String[] instrumentationSplitAppDirs = activityThread.mInstrumentationSplitAppDirs; |
| 406 | String instrumentationLibDir = activityThread.mInstrumentationLibDir; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 407 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 408 | String instrumentedAppDir = activityThread.mInstrumentedAppDir; |
| 409 | String[] instrumentedSplitAppDirs = activityThread.mInstrumentedSplitAppDirs; |
| 410 | String instrumentedLibDir = activityThread.mInstrumentedLibDir; |
| 411 | |
| 412 | if (appDir.equals(instrumentationAppDir) |
| 413 | || appDir.equals(instrumentedAppDir)) { |
| 414 | outZipPaths.clear(); |
| 415 | outZipPaths.add(instrumentationAppDir); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 416 | |
| 417 | // Only add splits if the app did not request isolated split loading. |
| 418 | if (!aInfo.requestsIsolatedSplitLoading()) { |
| 419 | if (instrumentationSplitAppDirs != null) { |
| 420 | Collections.addAll(outZipPaths, instrumentationSplitAppDirs); |
| 421 | } |
| 422 | |
| 423 | if (!instrumentationAppDir.equals(instrumentedAppDir)) { |
| 424 | outZipPaths.add(instrumentedAppDir); |
| 425 | if (instrumentedSplitAppDirs != null) { |
| 426 | Collections.addAll(outZipPaths, instrumentedSplitAppDirs); |
| 427 | } |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 428 | } |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 429 | } |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 430 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 431 | if (outLibPaths != null) { |
| 432 | outLibPaths.add(instrumentationLibDir); |
| 433 | if (!instrumentationLibDir.equals(instrumentedLibDir)) { |
| 434 | outLibPaths.add(instrumentedLibDir); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (!instrumentedAppDir.equals(instrumentationAppDir)) { |
| 439 | instrumentationLibs = getLibrariesFor(instrumentationPackageName); |
| 440 | } |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | if (outLibPaths != null) { |
| 445 | if (outLibPaths.isEmpty()) { |
| 446 | outLibPaths.add(libDir); |
| 447 | } |
| 448 | |
| 449 | // Add path to libraries in apk for current abi. Do this now because more entries |
| 450 | // will be added to zipPaths that shouldn't be part of the library path. |
| 451 | if (aInfo.primaryCpuAbi != null) { |
Alex Light | 20ed24f | 2016-04-20 14:07:43 -0700 | [diff] [blame] | 452 | // Add fake libs into the library search path if we target prior to N. |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 453 | if (aInfo.targetSdkVersion < Build.VERSION_CODES.N) { |
Alex Light | 20ed24f | 2016-04-20 14:07:43 -0700 | [diff] [blame] | 454 | outLibPaths.add("/system/fake-libs" + |
| 455 | (VMRuntime.is64BitAbi(aInfo.primaryCpuAbi) ? "64" : "")); |
| 456 | } |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 457 | for (String apk : outZipPaths) { |
| 458 | outLibPaths.add(apk + "!/lib/" + aInfo.primaryCpuAbi); |
| 459 | } |
| 460 | } |
| 461 | |
Dimitry Ivanov | 638d810 | 2017-02-22 15:39:42 -0800 | [diff] [blame] | 462 | if (isBundledApp) { |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 463 | // Add path to system libraries to libPaths; |
| 464 | // Access to system libs should be limited |
| 465 | // to bundled applications; this is why updated |
| 466 | // system apps are not included. |
| 467 | outLibPaths.add(System.getProperty("java.library.path")); |
| 468 | } |
| 469 | } |
| 470 | |
Jeff Hao | 090892f | 2017-04-24 17:37:31 -0700 | [diff] [blame] | 471 | // Prepend the shared libraries, maintaining their original order where possible. |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 472 | if (sharedLibraries != null) { |
Jeff Hao | 090892f | 2017-04-24 17:37:31 -0700 | [diff] [blame] | 473 | int index = 0; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 474 | for (String lib : sharedLibraries) { |
| 475 | if (!outZipPaths.contains(lib)) { |
Jeff Hao | 090892f | 2017-04-24 17:37:31 -0700 | [diff] [blame] | 476 | outZipPaths.add(index, lib); |
| 477 | index++; |
Svetoslav Ganov | 40610e3 | 2017-06-20 19:35:51 -0700 | [diff] [blame] | 478 | appendApkLibPathIfNeeded(lib, aInfo, outLibPaths); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if (instrumentationLibs != null) { |
| 484 | for (String lib : instrumentationLibs) { |
| 485 | if (!outZipPaths.contains(lib)) { |
| 486 | outZipPaths.add(0, lib); |
Svetoslav Ganov | 40610e3 | 2017-06-20 19:35:51 -0700 | [diff] [blame] | 487 | appendApkLibPathIfNeeded(lib, aInfo, outLibPaths); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Svetoslav Ganov | 40610e3 | 2017-06-20 19:35:51 -0700 | [diff] [blame] | 493 | /** |
| 494 | * This method appends a path to the appropriate native library folder of a |
| 495 | * library if this library is hosted in an APK. This allows support for native |
| 496 | * shared libraries. The library API is determined based on the application |
| 497 | * ABI. |
| 498 | * |
| 499 | * @param path Path to the library. |
| 500 | * @param applicationInfo The application depending on the library. |
| 501 | * @param outLibPaths List to which to add the native lib path if needed. |
| 502 | */ |
| 503 | private static void appendApkLibPathIfNeeded(@NonNull String path, |
| 504 | @NonNull ApplicationInfo applicationInfo, @Nullable List<String> outLibPaths) { |
| 505 | // Looking at the suffix is a little hacky but a safe and simple solution. |
| 506 | // We will be revisiting code in the next release and clean this up. |
| 507 | if (outLibPaths != null && applicationInfo.primaryCpuAbi != null && path.endsWith(".apk")) { |
| 508 | if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.O) { |
| 509 | outLibPaths.add(path + "!/lib/" + applicationInfo.primaryCpuAbi); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 514 | /* |
| 515 | * All indices received by the super class should be shifted by 1 when accessing mSplitNames, |
| 516 | * etc. The super class assumes the base APK is index 0, while the PackageManager APIs don't |
| 517 | * include the base APK in the list of splits. |
| 518 | */ |
| 519 | private class SplitDependencyLoaderImpl extends SplitDependencyLoader<NameNotFoundException> { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 520 | private final String[][] mCachedResourcePaths; |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 521 | private final ClassLoader[] mCachedClassLoaders; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 522 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 523 | SplitDependencyLoaderImpl(@NonNull SparseArray<int[]> dependencies) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 524 | super(dependencies); |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 525 | mCachedResourcePaths = new String[mSplitNames.length + 1][]; |
| 526 | mCachedClassLoaders = new ClassLoader[mSplitNames.length + 1]; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | @Override |
| 530 | protected boolean isSplitCached(int splitIdx) { |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 531 | return mCachedClassLoaders[splitIdx] != null; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | @Override |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 535 | protected void constructSplit(int splitIdx, @NonNull int[] configSplitIndices, |
| 536 | int parentSplitIdx) throws NameNotFoundException { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 537 | final ArrayList<String> splitPaths = new ArrayList<>(); |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 538 | if (splitIdx == 0) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 539 | createOrUpdateClassLoaderLocked(null); |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 540 | mCachedClassLoaders[0] = mClassLoader; |
| 541 | |
| 542 | // Never add the base resources here, they always get added no matter what. |
| 543 | for (int configSplitIdx : configSplitIndices) { |
| 544 | splitPaths.add(mSplitResDirs[configSplitIdx - 1]); |
| 545 | } |
| 546 | mCachedResourcePaths[0] = splitPaths.toArray(new String[splitPaths.size()]); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 550 | // Since we handled the special base case above, parentSplitIdx is always valid. |
| 551 | final ClassLoader parent = mCachedClassLoaders[parentSplitIdx]; |
| 552 | mCachedClassLoaders[splitIdx] = ApplicationLoaders.getDefault().getClassLoader( |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 553 | mSplitAppDirs[splitIdx - 1], getTargetSdkVersion(), false, null, null, parent, |
| 554 | mSplitClassLoaderNames[splitIdx - 1]); |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 555 | |
| 556 | Collections.addAll(splitPaths, mCachedResourcePaths[parentSplitIdx]); |
| 557 | splitPaths.add(mSplitResDirs[splitIdx - 1]); |
| 558 | for (int configSplitIdx : configSplitIndices) { |
| 559 | splitPaths.add(mSplitResDirs[configSplitIdx - 1]); |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 560 | } |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 561 | mCachedResourcePaths[splitIdx] = splitPaths.toArray(new String[splitPaths.size()]); |
| 562 | } |
| 563 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 564 | private int ensureSplitLoaded(String splitName) throws NameNotFoundException { |
| 565 | int idx = 0; |
| 566 | if (splitName != null) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 567 | idx = Arrays.binarySearch(mSplitNames, splitName); |
| 568 | if (idx < 0) { |
| 569 | throw new PackageManager.NameNotFoundException( |
| 570 | "Split name '" + splitName + "' is not installed"); |
| 571 | } |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 572 | idx += 1; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 573 | } |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 574 | loadDependenciesForSplit(idx); |
| 575 | return idx; |
| 576 | } |
| 577 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 578 | ClassLoader getClassLoaderForSplit(String splitName) throws NameNotFoundException { |
| 579 | return mCachedClassLoaders[ensureSplitLoaded(splitName)]; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 582 | String[] getSplitPathsForSplit(String splitName) throws NameNotFoundException { |
| 583 | return mCachedResourcePaths[ensureSplitLoaded(splitName)]; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 587 | private SplitDependencyLoaderImpl mSplitLoader; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 588 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 589 | ClassLoader getSplitClassLoader(String splitName) throws NameNotFoundException { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 590 | if (mSplitLoader == null) { |
| 591 | return mClassLoader; |
| 592 | } |
| 593 | return mSplitLoader.getClassLoaderForSplit(splitName); |
| 594 | } |
| 595 | |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 596 | String[] getSplitPaths(String splitName) throws NameNotFoundException { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 597 | if (mSplitLoader == null) { |
| 598 | return mSplitResDirs; |
| 599 | } |
| 600 | return mSplitLoader.getSplitPathsForSplit(splitName); |
| 601 | } |
| 602 | |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 603 | private void createOrUpdateClassLoaderLocked(List<String> addedPaths) { |
| 604 | if (mPackageName.equals("android")) { |
Calin Juravle | 430ef45 | 2016-04-22 17:43:07 +0100 | [diff] [blame] | 605 | // Note: This branch is taken for system server and we don't need to setup |
| 606 | // jit profiling support. |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 607 | if (mClassLoader != null) { |
| 608 | // nothing to update |
| 609 | return; |
Dimitry Ivanov | b9c9026 | 2016-02-19 14:09:20 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 612 | if (mBaseClassLoader != null) { |
| 613 | mClassLoader = mBaseClassLoader; |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 614 | } else { |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 615 | mClassLoader = ClassLoader.getSystemClassLoader(); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 616 | } |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 617 | |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | // Avoid the binder call when the package is the current application package. |
| 622 | // The activity manager will perform ensure that dexopt is performed before |
| 623 | // spinning up the process. |
Narayan Kamath | bf07e2a | 2017-03-15 11:37:32 +0000 | [diff] [blame] | 624 | if (!Objects.equals(mPackageName, ActivityThread.currentPackageName()) && mIncludeCode) { |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 625 | try { |
Brian Carlstrom | ca82e61 | 2016-04-19 23:16:08 -0700 | [diff] [blame] | 626 | ActivityThread.getPackageManager().notifyPackageUse(mPackageName, |
| 627 | PackageManager.NOTIFY_PACKAGE_USE_CROSS_PACKAGE); |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 628 | } catch (RemoteException re) { |
| 629 | throw re.rethrowFromSystemServer(); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 630 | } |
| 631 | } |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 632 | |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 633 | if (mRegisterPackage) { |
| 634 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 635 | ActivityManager.getService().addPackageDependency(mPackageName); |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 636 | } catch (RemoteException e) { |
| 637 | throw e.rethrowFromSystemServer(); |
| 638 | } |
| 639 | } |
| 640 | |
Andreas Gampe | a8a58ff | 2016-05-18 11:58:39 -0700 | [diff] [blame] | 641 | // Lists for the elements of zip/code and native libraries. |
| 642 | // |
| 643 | // Both lists are usually not empty. We expect on average one APK for the zip component, |
| 644 | // but shared libraries and splits are not uncommon. We expect at least three elements |
| 645 | // for native libraries (app-based, system, vendor). As such, give both some breathing |
| 646 | // space and initialize to a small value (instead of incurring growth code). |
| 647 | final List<String> zipPaths = new ArrayList<>(10); |
| 648 | final List<String> libPaths = new ArrayList<>(10); |
Narayan Kamath | 8995b00 | 2016-05-11 20:31:09 +0100 | [diff] [blame] | 649 | |
Jiyong Park | fcad696 | 2017-08-22 10:24:28 +0900 | [diff] [blame] | 650 | boolean isBundledApp = mApplicationInfo.isSystemApp() |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 651 | && !mApplicationInfo.isUpdatedSystemApp(); |
Dimitry Ivanov | bf3b5f7 | 2016-05-03 11:34:58 -0700 | [diff] [blame] | 652 | |
Jiyong Park | fcad696 | 2017-08-22 10:24:28 +0900 | [diff] [blame] | 653 | // Vendor apks are treated as bundled only when /vendor/lib is in the default search |
| 654 | // paths. If not, they are treated as unbundled; access to system libs is limited. |
| 655 | // Having /vendor/lib in the default search paths means that all system processes |
| 656 | // are allowed to use any vendor library, which in turn means that system is dependent |
| 657 | // on vendor partition. In the contrary, not having /vendor/lib in the default search |
| 658 | // paths mean that the two partitions are separated and thus we can treat vendor apks |
| 659 | // as unbundled. |
| 660 | final String defaultSearchPaths = System.getProperty("java.library.path"); |
| 661 | final boolean treatVendorApkAsUnbundled = !defaultSearchPaths.contains("/vendor/lib"); |
Jiyong Park | 63495c2 | 2017-09-15 07:48:06 +0900 | [diff] [blame] | 662 | if (mApplicationInfo.getCodePath() != null |
Jiyong Park | 002fdbd | 2017-02-13 20:50:31 +0900 | [diff] [blame] | 663 | && mApplicationInfo.isVendor() && treatVendorApkAsUnbundled) { |
Jiyong Park | fcad696 | 2017-08-22 10:24:28 +0900 | [diff] [blame] | 664 | isBundledApp = false; |
| 665 | } |
| 666 | |
Dimitry Ivanov | 638d810 | 2017-02-22 15:39:42 -0800 | [diff] [blame] | 667 | makePaths(mActivityThread, isBundledApp, mApplicationInfo, zipPaths, libPaths); |
| 668 | |
Dimitry Ivanov | eb96b00 | 2016-05-10 10:51:40 -0700 | [diff] [blame] | 669 | String libraryPermittedPath = mDataDir; |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 670 | if (isBundledApp) { |
| 671 | // This is necessary to grant bundled apps access to |
| 672 | // libraries located in subdirectories of /system/lib |
Jiyong Park | fcad696 | 2017-08-22 10:24:28 +0900 | [diff] [blame] | 673 | libraryPermittedPath += File.pathSeparator + defaultSearchPaths; |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 674 | } |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 675 | |
| 676 | final String librarySearchPath = TextUtils.join(File.pathSeparator, libPaths); |
| 677 | |
Narayan Kamath | 8995b00 | 2016-05-11 20:31:09 +0100 | [diff] [blame] | 678 | // If we're not asked to include code, we construct a classloader that has |
| 679 | // no code path included. We still need to set up the library search paths |
| 680 | // and permitted path because NativeActivity relies on it (it attempts to |
| 681 | // call System.loadLibrary() on a classloader from a LoadedApk with |
| 682 | // mIncludeCode == false). |
| 683 | if (!mIncludeCode) { |
| 684 | if (mClassLoader == null) { |
| 685 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 686 | mClassLoader = ApplicationLoaders.getDefault().getClassLoader( |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 687 | "" /* codePath */, mApplicationInfo.targetSdkVersion, isBundledApp, |
| 688 | librarySearchPath, libraryPermittedPath, mBaseClassLoader, |
| 689 | null /* classLoaderName */); |
Narayan Kamath | 8995b00 | 2016-05-11 20:31:09 +0100 | [diff] [blame] | 690 | StrictMode.setThreadPolicy(oldPolicy); |
| 691 | } |
| 692 | |
| 693 | return; |
| 694 | } |
| 695 | |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 696 | /* |
Narayan Kamath | 8995b00 | 2016-05-11 20:31:09 +0100 | [diff] [blame] | 697 | * With all the combination done (if necessary, actually create the java class |
| 698 | * loader and set up JIT profiling support if necessary. |
Andreas Gampe | a8a58ff | 2016-05-18 11:58:39 -0700 | [diff] [blame] | 699 | * |
| 700 | * In many cases this is a single APK, so try to avoid the StringBuilder in TextUtils. |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 701 | */ |
Andreas Gampe | a8a58ff | 2016-05-18 11:58:39 -0700 | [diff] [blame] | 702 | final String zip = (zipPaths.size() == 1) ? zipPaths.get(0) : |
| 703 | TextUtils.join(File.pathSeparator, zipPaths); |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 704 | |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 705 | if (DEBUG) Slog.v(ActivityThread.TAG, "Class path: " + zip + |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 706 | ", JNI path: " + librarySearchPath); |
| 707 | |
Calin Juravle | 430ef45 | 2016-04-22 17:43:07 +0100 | [diff] [blame] | 708 | boolean needToSetupJitProfiles = false; |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 709 | if (mClassLoader == null) { |
| 710 | // Temporarily disable logging of disk reads on the Looper thread |
| 711 | // as this is early and necessary. |
| 712 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 713 | |
| 714 | mClassLoader = ApplicationLoaders.getDefault().getClassLoader(zip, |
| 715 | mApplicationInfo.targetSdkVersion, isBundledApp, librarySearchPath, |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 716 | libraryPermittedPath, mBaseClassLoader, |
| 717 | mApplicationInfo.classLoaderName); |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 718 | |
| 719 | StrictMode.setThreadPolicy(oldPolicy); |
Calin Juravle | 430ef45 | 2016-04-22 17:43:07 +0100 | [diff] [blame] | 720 | // Setup the class loader paths for profiling. |
| 721 | needToSetupJitProfiles = true; |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | if (addedPaths != null && addedPaths.size() > 0) { |
| 725 | final String add = TextUtils.join(File.pathSeparator, addedPaths); |
| 726 | ApplicationLoaders.getDefault().addPath(mClassLoader, add); |
Calin Juravle | 430ef45 | 2016-04-22 17:43:07 +0100 | [diff] [blame] | 727 | // Setup the new code paths for profiling. |
| 728 | needToSetupJitProfiles = true; |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 729 | } |
Calin Juravle | 2a727d7 | 2016-04-15 19:33:46 +0100 | [diff] [blame] | 730 | |
| 731 | // Setup jit profile support. |
Narayan Kamath | 2d4a222 | 2016-04-25 19:16:56 +0100 | [diff] [blame] | 732 | // |
Calin Juravle | 2a727d7 | 2016-04-15 19:33:46 +0100 | [diff] [blame] | 733 | // It is ok to call this multiple times if the application gets updated with new splits. |
| 734 | // The runtime only keeps track of unique code paths and can handle re-registration of |
| 735 | // the same code path. There's no need to pass `addedPaths` since any new code paths |
| 736 | // are already in `mApplicationInfo`. |
Narayan Kamath | 2d4a222 | 2016-04-25 19:16:56 +0100 | [diff] [blame] | 737 | // |
| 738 | // It is NOT ok to call this function from the system_server (for any of the packages it |
| 739 | // loads code from) so we explicitly disallow it there. |
| 740 | if (needToSetupJitProfiles && !ActivityThread.isSystem()) { |
Calin Juravle | 430ef45 | 2016-04-22 17:43:07 +0100 | [diff] [blame] | 741 | setupJitProfileSupport(); |
Calin Juravle | 430ef45 | 2016-04-22 17:43:07 +0100 | [diff] [blame] | 742 | } |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | public ClassLoader getClassLoader() { |
| 746 | synchronized (this) { |
| 747 | if (mClassLoader == null) { |
Dimitry Ivanov | 43b28b1 | 2016-03-14 17:15:38 -0700 | [diff] [blame] | 748 | createOrUpdateClassLoaderLocked(null /*addedPaths*/); |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 749 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 750 | return mClassLoader; |
| 751 | } |
| 752 | } |
| 753 | |
Calin Juravle | 2a727d7 | 2016-04-15 19:33:46 +0100 | [diff] [blame] | 754 | private void setupJitProfileSupport() { |
| 755 | if (!SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false)) { |
| 756 | return; |
| 757 | } |
Calin Juravle | 126f780 | 2016-05-24 15:23:47 +0100 | [diff] [blame] | 758 | // Only set up profile support if the loaded apk has the same uid as the |
| 759 | // current process. |
| 760 | // Currently, we do not support profiling across different apps. |
| 761 | // (e.g. application's uid might be different when the code is |
| 762 | // loaded by another app via createApplicationContext) |
| 763 | if (mApplicationInfo.uid != Process.myUid()) { |
| 764 | return; |
| 765 | } |
| 766 | |
Calin Juravle | 2a727d7 | 2016-04-15 19:33:46 +0100 | [diff] [blame] | 767 | final List<String> codePaths = new ArrayList<>(); |
| 768 | if ((mApplicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0) { |
| 769 | codePaths.add(mApplicationInfo.sourceDir); |
| 770 | } |
| 771 | if (mApplicationInfo.splitSourceDirs != null) { |
| 772 | Collections.addAll(codePaths, mApplicationInfo.splitSourceDirs); |
| 773 | } |
| 774 | |
| 775 | if (codePaths.isEmpty()) { |
| 776 | // If there are no code paths there's no need to setup a profile file and register with |
| 777 | // the runtime, |
| 778 | return; |
| 779 | } |
| 780 | |
Calin Juravle | 6ae39fc | 2018-01-19 20:32:47 -0800 | [diff] [blame] | 781 | for (int i = codePaths.size() - 1; i >= 0; i--) { |
| 782 | String splitName = i == 0 ? null : mApplicationInfo.splitNames[i - 1]; |
| 783 | String profileFile = ArtManager.getCurrentProfilePath( |
| 784 | mPackageName, UserHandle.myUserId(), splitName); |
| 785 | VMRuntime.registerAppInfo(profileFile, new String[] {codePaths.get(i)}); |
| 786 | } |
Calin Juravle | f5a7bfc | 2017-03-13 23:30:30 -0700 | [diff] [blame] | 787 | |
| 788 | // Register the app data directory with the reporter. It will |
| 789 | // help deciding whether or not a dex file is the primary apk or a |
| 790 | // secondary dex. |
| 791 | DexLoadReporter.getInstance().registerAppDataDir(mPackageName, mDataDir); |
Calin Juravle | 2a727d7 | 2016-04-15 19:33:46 +0100 | [diff] [blame] | 792 | } |
| 793 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 794 | /** |
| 795 | * Setup value for Thread.getContextClassLoader(). If the |
| 796 | * package will not run in in a VM with other packages, we set |
| 797 | * the Java context ClassLoader to the |
| 798 | * PackageInfo.getClassLoader value. However, if this VM can |
| 799 | * contain multiple packages, we intead set the Java context |
| 800 | * ClassLoader to a proxy that will warn about the use of Java |
| 801 | * context ClassLoaders and then fall through to use the |
| 802 | * system ClassLoader. |
| 803 | * |
| 804 | * <p> Note that this is similar to but not the same as the |
| 805 | * android.content.Context.getClassLoader(). While both |
| 806 | * context class loaders are typically set to the |
| 807 | * PathClassLoader used to load the package archive in the |
| 808 | * single application per VM case, a single Android process |
| 809 | * may contain several Contexts executing on one thread with |
| 810 | * their own logical ClassLoaders while the Java context |
| 811 | * ClassLoader is a thread local. This is why in the case when |
| 812 | * we have multiple packages per VM we do not set the Java |
| 813 | * context ClassLoader to an arbitrary but instead warn the |
| 814 | * user to set their own if we detect that they are using a |
| 815 | * Java library that expects it to be set. |
| 816 | */ |
| 817 | private void initializeJavaContextClassLoader() { |
| 818 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 819 | android.content.pm.PackageInfo pi; |
| 820 | try { |
Jeff Sharkey | c7bacab | 2016-02-09 15:56:11 -0700 | [diff] [blame] | 821 | pi = pm.getPackageInfo(mPackageName, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 822 | UserHandle.myUserId()); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 823 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 824 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 208d937 | 2013-02-25 13:17:54 -0800 | [diff] [blame] | 825 | } |
| 826 | if (pi == null) { |
| 827 | throw new IllegalStateException("Unable to get package info for " |
| 828 | + mPackageName + "; is package not installed?"); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 829 | } |
| 830 | /* |
| 831 | * Two possible indications that this package could be |
| 832 | * sharing its virtual machine with other packages: |
| 833 | * |
| 834 | * 1.) the sharedUserId attribute is set in the manifest, |
| 835 | * indicating a request to share a VM with other |
| 836 | * packages with the same sharedUserId. |
| 837 | * |
| 838 | * 2.) the application element of the manifest has an |
| 839 | * attribute specifying a non-default process name, |
| 840 | * indicating the desire to run in another packages VM. |
| 841 | */ |
| 842 | boolean sharedUserIdSet = (pi.sharedUserId != null); |
| 843 | boolean processNameNotDefault = |
| 844 | (pi.applicationInfo != null && |
| 845 | !mPackageName.equals(pi.applicationInfo.processName)); |
| 846 | boolean sharable = (sharedUserIdSet || processNameNotDefault); |
| 847 | ClassLoader contextClassLoader = |
| 848 | (sharable) |
| 849 | ? new WarningContextClassLoader() |
| 850 | : mClassLoader; |
| 851 | Thread.currentThread().setContextClassLoader(contextClassLoader); |
| 852 | } |
| 853 | |
| 854 | private static class WarningContextClassLoader extends ClassLoader { |
| 855 | |
| 856 | private static boolean warned = false; |
| 857 | |
| 858 | private void warn(String methodName) { |
| 859 | if (warned) { |
| 860 | return; |
| 861 | } |
| 862 | warned = true; |
| 863 | Thread.currentThread().setContextClassLoader(getParent()); |
| 864 | Slog.w(ActivityThread.TAG, "ClassLoader." + methodName + ": " + |
| 865 | "The class loader returned by " + |
| 866 | "Thread.getContextClassLoader() may fail for processes " + |
| 867 | "that host multiple applications. You should explicitly " + |
| 868 | "specify a context class loader. For example: " + |
| 869 | "Thread.setContextClassLoader(getClass().getClassLoader());"); |
| 870 | } |
| 871 | |
| 872 | @Override public URL getResource(String resName) { |
| 873 | warn("getResource"); |
| 874 | return getParent().getResource(resName); |
| 875 | } |
| 876 | |
| 877 | @Override public Enumeration<URL> getResources(String resName) throws IOException { |
| 878 | warn("getResources"); |
| 879 | return getParent().getResources(resName); |
| 880 | } |
| 881 | |
| 882 | @Override public InputStream getResourceAsStream(String resName) { |
| 883 | warn("getResourceAsStream"); |
| 884 | return getParent().getResourceAsStream(resName); |
| 885 | } |
| 886 | |
| 887 | @Override public Class<?> loadClass(String className) throws ClassNotFoundException { |
| 888 | warn("loadClass"); |
| 889 | return getParent().loadClass(className); |
| 890 | } |
| 891 | |
| 892 | @Override public void setClassAssertionStatus(String cname, boolean enable) { |
| 893 | warn("setClassAssertionStatus"); |
| 894 | getParent().setClassAssertionStatus(cname, enable); |
| 895 | } |
| 896 | |
| 897 | @Override public void setPackageAssertionStatus(String pname, boolean enable) { |
| 898 | warn("setPackageAssertionStatus"); |
| 899 | getParent().setPackageAssertionStatus(pname, enable); |
| 900 | } |
| 901 | |
| 902 | @Override public void setDefaultAssertionStatus(boolean enable) { |
| 903 | warn("setDefaultAssertionStatus"); |
| 904 | getParent().setDefaultAssertionStatus(enable); |
| 905 | } |
| 906 | |
| 907 | @Override public void clearAssertionStatus() { |
| 908 | warn("clearAssertionStatus"); |
| 909 | getParent().clearAssertionStatus(); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | public String getAppDir() { |
| 914 | return mAppDir; |
| 915 | } |
| 916 | |
Brian Carlstrom | d893a89 | 2012-04-01 21:30:26 -0700 | [diff] [blame] | 917 | public String getLibDir() { |
| 918 | return mLibDir; |
| 919 | } |
| 920 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 921 | public String getResDir() { |
| 922 | return mResDir; |
| 923 | } |
| 924 | |
Jeff Sharkey | 8a4c972 | 2014-06-16 13:48:42 -0700 | [diff] [blame] | 925 | public String[] getSplitAppDirs() { |
| 926 | return mSplitAppDirs; |
| 927 | } |
| 928 | |
| 929 | public String[] getSplitResDirs() { |
| 930 | return mSplitResDirs; |
| 931 | } |
| 932 | |
MÃ¥rten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 933 | public String[] getOverlayDirs() { |
| 934 | return mOverlayDirs; |
| 935 | } |
| 936 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 937 | public String getDataDir() { |
| 938 | return mDataDir; |
| 939 | } |
| 940 | |
| 941 | public File getDataDirFile() { |
| 942 | return mDataDirFile; |
| 943 | } |
| 944 | |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 945 | public File getDeviceProtectedDataDirFile() { |
| 946 | return mDeviceProtectedDataDirFile; |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 949 | public File getCredentialProtectedDataDirFile() { |
| 950 | return mCredentialProtectedDataDirFile; |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 953 | public AssetManager getAssets() { |
| 954 | return getResources().getAssets(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 957 | public Resources getResources() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 958 | if (mResources == null) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 959 | final String[] splitPaths; |
| 960 | try { |
| 961 | splitPaths = getSplitPaths(null); |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 962 | } catch (NameNotFoundException e) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 963 | // This should never fail. |
| 964 | throw new AssertionError("null split not found"); |
| 965 | } |
| 966 | |
Jeff Hamilton | 7541ca4d | 2017-10-24 15:20:43 -0400 | [diff] [blame] | 967 | mResources = ResourcesManager.getInstance().getResources( |
| 968 | null, |
| 969 | mResDir, |
| 970 | splitPaths, |
| 971 | mOverlayDirs, |
| 972 | mApplicationInfo.sharedLibraryFiles, |
| 973 | Display.DEFAULT_DISPLAY, |
| 974 | null, |
| 975 | getCompatibilityInfo(), |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 976 | getClassLoader()); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 977 | } |
| 978 | return mResources; |
| 979 | } |
| 980 | |
Jeff Hamilton | 7541ca4d | 2017-10-24 15:20:43 -0400 | [diff] [blame] | 981 | public Resources getOrCreateResourcesForSplit(@NonNull String splitName, |
| 982 | @Nullable IBinder activityToken, int displayId) throws NameNotFoundException { |
| 983 | return ResourcesManager.getInstance().getResources( |
| 984 | activityToken, |
| 985 | mResDir, |
| 986 | getSplitPaths(splitName), |
| 987 | mOverlayDirs, |
| 988 | mApplicationInfo.sharedLibraryFiles, |
| 989 | displayId, |
| 990 | null, |
| 991 | getCompatibilityInfo(), |
| 992 | getSplitClassLoader(splitName)); |
| 993 | } |
| 994 | |
| 995 | /** |
| 996 | * Creates the top level resources for the given package. Will return an existing |
| 997 | * Resources if one has already been created. |
| 998 | */ |
| 999 | public Resources getOrCreateTopLevelResources(@NonNull ApplicationInfo appInfo) { |
| 1000 | // Request for this app, short circuit |
| 1001 | if (appInfo.uid == Process.myUid()) { |
| 1002 | return getResources(); |
| 1003 | } |
| 1004 | |
| 1005 | // Get resources for a different package |
| 1006 | return ResourcesManager.getInstance().getResources( |
| 1007 | null, |
| 1008 | appInfo.publicSourceDir, |
| 1009 | appInfo.splitPublicSourceDirs, |
| 1010 | appInfo.resourceDirs, |
| 1011 | appInfo.sharedLibraryFiles, |
| 1012 | Display.DEFAULT_DISPLAY, |
| 1013 | null, |
| 1014 | getCompatibilityInfo(), |
| 1015 | getClassLoader()); |
| 1016 | } |
| 1017 | |
| 1018 | public Resources createResources(IBinder activityToken, String splitName, |
| 1019 | int displayId, Configuration overrideConfig, CompatibilityInfo compatInfo) { |
| 1020 | final String[] splitResDirs; |
| 1021 | final ClassLoader classLoader; |
| 1022 | try { |
| 1023 | splitResDirs = getSplitPaths(splitName); |
| 1024 | classLoader = getSplitClassLoader(splitName); |
| 1025 | } catch (NameNotFoundException e) { |
| 1026 | throw new RuntimeException(e); |
| 1027 | } |
| 1028 | return ResourcesManager.getInstance().getResources(activityToken, |
| 1029 | mResDir, |
| 1030 | splitResDirs, |
| 1031 | mOverlayDirs, |
| 1032 | mApplicationInfo.sharedLibraryFiles, |
| 1033 | displayId, |
| 1034 | overrideConfig, |
| 1035 | compatInfo, |
| 1036 | classLoader); |
| 1037 | } |
| 1038 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1039 | public Application makeApplication(boolean forceDefaultAppClass, |
| 1040 | Instrumentation instrumentation) { |
| 1041 | if (mApplication != null) { |
| 1042 | return mApplication; |
| 1043 | } |
| 1044 | |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 1045 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "makeApplication"); |
| 1046 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1047 | Application app = null; |
| 1048 | |
| 1049 | String appClass = mApplicationInfo.className; |
| 1050 | if (forceDefaultAppClass || (appClass == null)) { |
| 1051 | appClass = "android.app.Application"; |
| 1052 | } |
| 1053 | |
| 1054 | try { |
| 1055 | java.lang.ClassLoader cl = getClassLoader(); |
Narayan Kamath | 8091edb | 2014-10-15 11:38:44 +0100 | [diff] [blame] | 1056 | if (!mPackageName.equals("android")) { |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 1057 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1058 | "initializeJavaContextClassLoader"); |
Narayan Kamath | 8091edb | 2014-10-15 11:38:44 +0100 | [diff] [blame] | 1059 | initializeJavaContextClassLoader(); |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 1060 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Narayan Kamath | 8091edb | 2014-10-15 11:38:44 +0100 | [diff] [blame] | 1061 | } |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1062 | ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1063 | app = mActivityThread.mInstrumentation.newApplication( |
| 1064 | cl, appClass, appContext); |
| 1065 | appContext.setOuterContext(app); |
| 1066 | } catch (Exception e) { |
| 1067 | if (!mActivityThread.mInstrumentation.onException(app, e)) { |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 1068 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1069 | throw new RuntimeException( |
| 1070 | "Unable to instantiate application " + appClass |
| 1071 | + ": " + e.toString(), e); |
| 1072 | } |
| 1073 | } |
| 1074 | mActivityThread.mAllApplications.add(app); |
| 1075 | mApplication = app; |
| 1076 | |
| 1077 | if (instrumentation != null) { |
| 1078 | try { |
| 1079 | instrumentation.callApplicationOnCreate(app); |
| 1080 | } catch (Exception e) { |
| 1081 | if (!instrumentation.onException(app, e)) { |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 1082 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1083 | throw new RuntimeException( |
| 1084 | "Unable to create application " + app.getClass().getName() |
| 1085 | + ": " + e.toString(), e); |
| 1086 | } |
| 1087 | } |
| 1088 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1089 | |
| 1090 | // Rewrite the R 'constants' for all library apks. |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 1091 | SparseArray<String> packageIdentifiers = getAssets().getAssignedPackageIdentifiers(); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1092 | final int N = packageIdentifiers.size(); |
| 1093 | for (int i = 0; i < N; i++) { |
| 1094 | final int id = packageIdentifiers.keyAt(i); |
| 1095 | if (id == 0x01 || id == 0x7f) { |
| 1096 | continue; |
| 1097 | } |
| 1098 | |
| 1099 | rewriteRValues(getClassLoader(), packageIdentifiers.valueAt(i), id); |
| 1100 | } |
| 1101 | |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 1102 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 1103 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1104 | return app; |
| 1105 | } |
| 1106 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1107 | private void rewriteRValues(ClassLoader cl, String packageName, int id) { |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 1108 | final Class<?> rClazz; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1109 | try { |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 1110 | rClazz = cl.loadClass(packageName + ".R"); |
| 1111 | } catch (ClassNotFoundException e) { |
| 1112 | // This is not necessarily an error, as some packages do not ship with resources |
| 1113 | // (or they do not need rewriting). |
Adam Lesinski | 1e466385 | 2014-08-15 14:47:28 -0700 | [diff] [blame] | 1114 | Log.i(TAG, "No resource references to update in package " + packageName); |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 1115 | return; |
| 1116 | } |
| 1117 | |
Adam Lesinski | 1e466385 | 2014-08-15 14:47:28 -0700 | [diff] [blame] | 1118 | final Method callback; |
Adam Lesinski | 2cb761e | 2014-08-15 13:59:02 -0700 | [diff] [blame] | 1119 | try { |
Adam Lesinski | 1e466385 | 2014-08-15 14:47:28 -0700 | [diff] [blame] | 1120 | callback = rClazz.getMethod("onResourcesLoaded", int.class); |
| 1121 | } catch (NoSuchMethodException e) { |
| 1122 | // No rewriting to be done. |
| 1123 | return; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1124 | } |
Adam Lesinski | 1e466385 | 2014-08-15 14:47:28 -0700 | [diff] [blame] | 1125 | |
| 1126 | Throwable cause; |
| 1127 | try { |
| 1128 | callback.invoke(null, id); |
| 1129 | return; |
| 1130 | } catch (IllegalAccessException e) { |
| 1131 | cause = e; |
| 1132 | } catch (InvocationTargetException e) { |
| 1133 | cause = e.getCause(); |
| 1134 | } |
| 1135 | |
| 1136 | throw new RuntimeException("Failed to rewrite resource references for " + packageName, |
| 1137 | cause); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1140 | public void removeContextRegistrations(Context context, |
| 1141 | String who, String what) { |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 1142 | final boolean reportRegistrationLeaks = StrictMode.vmRegistrationLeaksEnabled(); |
Adam Lesinski | 23d40c4 | 2014-11-20 11:58:21 -0800 | [diff] [blame] | 1143 | synchronized (mReceivers) { |
| 1144 | ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> rmap = |
| 1145 | mReceivers.remove(context); |
| 1146 | if (rmap != null) { |
| 1147 | for (int i = 0; i < rmap.size(); i++) { |
| 1148 | LoadedApk.ReceiverDispatcher rd = rmap.valueAt(i); |
| 1149 | IntentReceiverLeaked leak = new IntentReceiverLeaked( |
| 1150 | what + " " + who + " has leaked IntentReceiver " |
| 1151 | + rd.getIntentReceiver() + " that was " + |
| 1152 | "originally registered here. Are you missing a " + |
| 1153 | "call to unregisterReceiver()?"); |
| 1154 | leak.setStackTrace(rd.getLocation().getStackTrace()); |
| 1155 | Slog.e(ActivityThread.TAG, leak.getMessage(), leak); |
| 1156 | if (reportRegistrationLeaks) { |
| 1157 | StrictMode.onIntentReceiverLeaked(leak); |
| 1158 | } |
| 1159 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1160 | ActivityManager.getService().unregisterReceiver( |
Adam Lesinski | 23d40c4 | 2014-11-20 11:58:21 -0800 | [diff] [blame] | 1161 | rd.getIIntentReceiver()); |
| 1162 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1163 | throw e.rethrowFromSystemServer(); |
Adam Lesinski | 23d40c4 | 2014-11-20 11:58:21 -0800 | [diff] [blame] | 1164 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
Adam Lesinski | 23d40c4 | 2014-11-20 11:58:21 -0800 | [diff] [blame] | 1167 | mUnregisteredReceivers.remove(context); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1168 | } |
Adam Lesinski | 23d40c4 | 2014-11-20 11:58:21 -0800 | [diff] [blame] | 1169 | |
| 1170 | synchronized (mServices) { |
| 1171 | //Slog.i(TAG, "Receiver registrations: " + mReceivers); |
| 1172 | ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> smap = |
| 1173 | mServices.remove(context); |
| 1174 | if (smap != null) { |
| 1175 | for (int i = 0; i < smap.size(); i++) { |
| 1176 | LoadedApk.ServiceDispatcher sd = smap.valueAt(i); |
| 1177 | ServiceConnectionLeaked leak = new ServiceConnectionLeaked( |
| 1178 | what + " " + who + " has leaked ServiceConnection " |
| 1179 | + sd.getServiceConnection() + " that was originally bound here"); |
| 1180 | leak.setStackTrace(sd.getLocation().getStackTrace()); |
| 1181 | Slog.e(ActivityThread.TAG, leak.getMessage(), leak); |
| 1182 | if (reportRegistrationLeaks) { |
| 1183 | StrictMode.onServiceConnectionLeaked(leak); |
| 1184 | } |
| 1185 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1186 | ActivityManager.getService().unbindService( |
Adam Lesinski | 23d40c4 | 2014-11-20 11:58:21 -0800 | [diff] [blame] | 1187 | sd.getIServiceConnection()); |
| 1188 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1189 | throw e.rethrowFromSystemServer(); |
Adam Lesinski | 23d40c4 | 2014-11-20 11:58:21 -0800 | [diff] [blame] | 1190 | } |
| 1191 | sd.doForget(); |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 1192 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1193 | } |
Adam Lesinski | 23d40c4 | 2014-11-20 11:58:21 -0800 | [diff] [blame] | 1194 | mUnboundServices.remove(context); |
| 1195 | //Slog.i(TAG, "Service registrations: " + mServices); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1196 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | public IIntentReceiver getReceiverDispatcher(BroadcastReceiver r, |
| 1200 | Context context, Handler handler, |
| 1201 | Instrumentation instrumentation, boolean registered) { |
| 1202 | synchronized (mReceivers) { |
| 1203 | LoadedApk.ReceiverDispatcher rd = null; |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1204 | ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> map = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1205 | if (registered) { |
| 1206 | map = mReceivers.get(context); |
| 1207 | if (map != null) { |
| 1208 | rd = map.get(r); |
| 1209 | } |
| 1210 | } |
| 1211 | if (rd == null) { |
| 1212 | rd = new ReceiverDispatcher(r, context, handler, |
| 1213 | instrumentation, registered); |
| 1214 | if (registered) { |
| 1215 | if (map == null) { |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1216 | map = new ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1217 | mReceivers.put(context, map); |
| 1218 | } |
| 1219 | map.put(r, rd); |
| 1220 | } |
| 1221 | } else { |
| 1222 | rd.validate(context, handler); |
| 1223 | } |
Dianne Hackborn | c2d9c8e | 2011-01-24 21:59:21 -0800 | [diff] [blame] | 1224 | rd.mForgotten = false; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1225 | return rd.getIIntentReceiver(); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | public IIntentReceiver forgetReceiverDispatcher(Context context, |
| 1230 | BroadcastReceiver r) { |
| 1231 | synchronized (mReceivers) { |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1232 | ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> map = mReceivers.get(context); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1233 | LoadedApk.ReceiverDispatcher rd = null; |
| 1234 | if (map != null) { |
| 1235 | rd = map.get(r); |
| 1236 | if (rd != null) { |
| 1237 | map.remove(r); |
| 1238 | if (map.size() == 0) { |
| 1239 | mReceivers.remove(context); |
| 1240 | } |
| 1241 | if (r.getDebugUnregister()) { |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1242 | ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> holder |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1243 | = mUnregisteredReceivers.get(context); |
| 1244 | if (holder == null) { |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1245 | holder = new ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1246 | mUnregisteredReceivers.put(context, holder); |
| 1247 | } |
| 1248 | RuntimeException ex = new IllegalArgumentException( |
| 1249 | "Originally unregistered here:"); |
| 1250 | ex.fillInStackTrace(); |
| 1251 | rd.setUnregisterLocation(ex); |
| 1252 | holder.put(r, rd); |
| 1253 | } |
Dianne Hackborn | c2d9c8e | 2011-01-24 21:59:21 -0800 | [diff] [blame] | 1254 | rd.mForgotten = true; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1255 | return rd.getIIntentReceiver(); |
| 1256 | } |
| 1257 | } |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1258 | ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> holder |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1259 | = mUnregisteredReceivers.get(context); |
| 1260 | if (holder != null) { |
| 1261 | rd = holder.get(r); |
| 1262 | if (rd != null) { |
| 1263 | RuntimeException ex = rd.getUnregisterLocation(); |
| 1264 | throw new IllegalArgumentException( |
| 1265 | "Unregistering Receiver " + r |
| 1266 | + " that was already unregistered", ex); |
| 1267 | } |
| 1268 | } |
| 1269 | if (context == null) { |
| 1270 | throw new IllegalStateException("Unbinding Receiver " + r |
| 1271 | + " from Context that is no longer in use: " + context); |
| 1272 | } else { |
| 1273 | throw new IllegalArgumentException("Receiver not registered: " + r); |
| 1274 | } |
| 1275 | |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | static final class ReceiverDispatcher { |
| 1280 | |
| 1281 | final static class InnerReceiver extends IIntentReceiver.Stub { |
| 1282 | final WeakReference<LoadedApk.ReceiverDispatcher> mDispatcher; |
| 1283 | final LoadedApk.ReceiverDispatcher mStrongRef; |
| 1284 | |
| 1285 | InnerReceiver(LoadedApk.ReceiverDispatcher rd, boolean strong) { |
| 1286 | mDispatcher = new WeakReference<LoadedApk.ReceiverDispatcher>(rd); |
| 1287 | mStrongRef = strong ? rd : null; |
| 1288 | } |
Jeff Sharkey | d136e51 | 2016-03-09 22:30:56 -0700 | [diff] [blame] | 1289 | |
| 1290 | @Override |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1291 | public void performReceive(Intent intent, int resultCode, String data, |
| 1292 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
Dianne Hackborn | 2675616 | 2016-05-17 13:40:44 -0700 | [diff] [blame] | 1293 | final LoadedApk.ReceiverDispatcher rd; |
| 1294 | if (intent == null) { |
| 1295 | Log.wtf(TAG, "Null intent received"); |
| 1296 | rd = null; |
| 1297 | } else { |
| 1298 | rd = mDispatcher.get(); |
| 1299 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1300 | if (ActivityThread.DEBUG_BROADCAST) { |
| 1301 | int seq = intent.getIntExtra("seq", -1); |
Dianne Hackborn | 2675616 | 2016-05-17 13:40:44 -0700 | [diff] [blame] | 1302 | Slog.i(ActivityThread.TAG, "Receiving broadcast " + intent.getAction() |
| 1303 | + " seq=" + seq + " to " + (rd != null ? rd.mReceiver : null)); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1304 | } |
| 1305 | if (rd != null) { |
| 1306 | rd.performReceive(intent, resultCode, data, extras, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1307 | ordered, sticky, sendingUser); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1308 | } else { |
| 1309 | // The activity manager dispatched a broadcast to a registered |
| 1310 | // receiver in this process, but before it could be delivered the |
| 1311 | // receiver was unregistered. Acknowledge the broadcast on its |
| 1312 | // behalf so that the system's broadcast sequence can continue. |
| 1313 | if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG, |
| 1314 | "Finishing broadcast to unregistered receiver"); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1315 | IActivityManager mgr = ActivityManager.getService(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1316 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1317 | if (extras != null) { |
| 1318 | extras.setAllowFds(false); |
| 1319 | } |
riddle_hsu | 1f5ac4d | 2015-01-03 15:38:21 +0800 | [diff] [blame] | 1320 | mgr.finishReceiver(this, resultCode, data, extras, false, intent.getFlags()); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1321 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1322 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | final IIntentReceiver.Stub mIIntentReceiver; |
| 1329 | final BroadcastReceiver mReceiver; |
| 1330 | final Context mContext; |
| 1331 | final Handler mActivityThread; |
| 1332 | final Instrumentation mInstrumentation; |
| 1333 | final boolean mRegistered; |
| 1334 | final IntentReceiverLeaked mLocation; |
| 1335 | RuntimeException mUnregisterLocation; |
Dianne Hackborn | c2d9c8e | 2011-01-24 21:59:21 -0800 | [diff] [blame] | 1336 | boolean mForgotten; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1337 | |
Makoto Onuki | 08408b9 | 2017-05-09 13:48:53 -0700 | [diff] [blame] | 1338 | final class Args extends BroadcastReceiver.PendingResult { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1339 | private Intent mCurIntent; |
Dianne Hackborn | e829fef | 2010-10-26 17:44:01 -0700 | [diff] [blame] | 1340 | private final boolean mOrdered; |
Dianne Hackborn | 17c7753 | 2016-06-22 16:49:14 -0700 | [diff] [blame] | 1341 | private boolean mDispatched; |
Makoto Onuki | f3bb6f3 | 2017-05-02 12:02:23 -0700 | [diff] [blame] | 1342 | private Throwable mPreviousRunStacktrace; // To investigate b/37809561. STOPSHIP remove. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1343 | |
Dianne Hackborn | e829fef | 2010-10-26 17:44:01 -0700 | [diff] [blame] | 1344 | public Args(Intent intent, int resultCode, String resultData, Bundle resultExtras, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1345 | boolean ordered, boolean sticky, int sendingUser) { |
Dianne Hackborn | e829fef | 2010-10-26 17:44:01 -0700 | [diff] [blame] | 1346 | super(resultCode, resultData, resultExtras, |
riddle_hsu | 1f5ac4d | 2015-01-03 15:38:21 +0800 | [diff] [blame] | 1347 | mRegistered ? TYPE_REGISTERED : TYPE_UNREGISTERED, ordered, |
| 1348 | sticky, mIIntentReceiver.asBinder(), sendingUser, intent.getFlags()); |
Dianne Hackborn | e829fef | 2010-10-26 17:44:01 -0700 | [diff] [blame] | 1349 | mCurIntent = intent; |
| 1350 | mOrdered = ordered; |
| 1351 | } |
Dianne Hackborn | 17c7753 | 2016-06-22 16:49:14 -0700 | [diff] [blame] | 1352 | |
Makoto Onuki | 08408b9 | 2017-05-09 13:48:53 -0700 | [diff] [blame] | 1353 | public final Runnable getRunnable() { |
| 1354 | return () -> { |
| 1355 | final BroadcastReceiver receiver = mReceiver; |
| 1356 | final boolean ordered = mOrdered; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1357 | |
Makoto Onuki | 08408b9 | 2017-05-09 13:48:53 -0700 | [diff] [blame] | 1358 | if (ActivityThread.DEBUG_BROADCAST) { |
| 1359 | int seq = mCurIntent.getIntExtra("seq", -1); |
| 1360 | Slog.i(ActivityThread.TAG, "Dispatching broadcast " + mCurIntent.getAction() |
| 1361 | + " seq=" + seq + " to " + mReceiver); |
| 1362 | Slog.i(ActivityThread.TAG, " mRegistered=" + mRegistered |
| 1363 | + " mOrderedHint=" + ordered); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1364 | } |
Makoto Onuki | 08408b9 | 2017-05-09 13:48:53 -0700 | [diff] [blame] | 1365 | |
| 1366 | final IActivityManager mgr = ActivityManager.getService(); |
| 1367 | final Intent intent = mCurIntent; |
| 1368 | if (intent == null) { |
| 1369 | Log.wtf(TAG, "Null intent being dispatched, mDispatched=" + mDispatched |
| 1370 | + ": run() previously called at " |
| 1371 | + Log.getStackTraceString(mPreviousRunStacktrace)); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1372 | } |
Makoto Onuki | 08408b9 | 2017-05-09 13:48:53 -0700 | [diff] [blame] | 1373 | |
| 1374 | mCurIntent = null; |
| 1375 | mDispatched = true; |
| 1376 | mPreviousRunStacktrace = new Throwable("Previous stacktrace"); |
| 1377 | if (receiver == null || intent == null || mForgotten) { |
| 1378 | if (mRegistered && ordered) { |
| 1379 | if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG, |
| 1380 | "Finishing null broadcast to " + mReceiver); |
| 1381 | sendFinished(mgr); |
| 1382 | } |
| 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveReg"); |
| 1387 | try { |
| 1388 | ClassLoader cl = mReceiver.getClass().getClassLoader(); |
| 1389 | intent.setExtrasClassLoader(cl); |
| 1390 | intent.prepareToEnterProcess(); |
| 1391 | setExtrasClassLoader(cl); |
| 1392 | receiver.setPendingResult(this); |
| 1393 | receiver.onReceive(mContext, intent); |
| 1394 | } catch (Exception e) { |
| 1395 | if (mRegistered && ordered) { |
| 1396 | if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG, |
| 1397 | "Finishing failed broadcast to " + mReceiver); |
| 1398 | sendFinished(mgr); |
| 1399 | } |
| 1400 | if (mInstrumentation == null || |
| 1401 | !mInstrumentation.onException(mReceiver, e)) { |
| 1402 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 1403 | throw new RuntimeException( |
| 1404 | "Error receiving broadcast " + intent |
| 1405 | + " in " + mReceiver, e); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | if (receiver.getPendingResult() != null) { |
| 1410 | finish(); |
| 1411 | } |
| 1412 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 1413 | }; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | ReceiverDispatcher(BroadcastReceiver receiver, Context context, |
| 1418 | Handler activityThread, Instrumentation instrumentation, |
| 1419 | boolean registered) { |
| 1420 | if (activityThread == null) { |
| 1421 | throw new NullPointerException("Handler must not be null"); |
| 1422 | } |
| 1423 | |
| 1424 | mIIntentReceiver = new InnerReceiver(this, !registered); |
| 1425 | mReceiver = receiver; |
| 1426 | mContext = context; |
| 1427 | mActivityThread = activityThread; |
| 1428 | mInstrumentation = instrumentation; |
| 1429 | mRegistered = registered; |
| 1430 | mLocation = new IntentReceiverLeaked(null); |
| 1431 | mLocation.fillInStackTrace(); |
| 1432 | } |
| 1433 | |
| 1434 | void validate(Context context, Handler activityThread) { |
| 1435 | if (mContext != context) { |
| 1436 | throw new IllegalStateException( |
| 1437 | "Receiver " + mReceiver + |
| 1438 | " registered with differing Context (was " + |
| 1439 | mContext + " now " + context + ")"); |
| 1440 | } |
| 1441 | if (mActivityThread != activityThread) { |
| 1442 | throw new IllegalStateException( |
| 1443 | "Receiver " + mReceiver + |
| 1444 | " registered with differing handler (was " + |
| 1445 | mActivityThread + " now " + activityThread + ")"); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | IntentReceiverLeaked getLocation() { |
| 1450 | return mLocation; |
| 1451 | } |
| 1452 | |
| 1453 | BroadcastReceiver getIntentReceiver() { |
| 1454 | return mReceiver; |
| 1455 | } |
| 1456 | |
| 1457 | IIntentReceiver getIIntentReceiver() { |
| 1458 | return mIIntentReceiver; |
| 1459 | } |
| 1460 | |
| 1461 | void setUnregisterLocation(RuntimeException ex) { |
| 1462 | mUnregisterLocation = ex; |
| 1463 | } |
| 1464 | |
| 1465 | RuntimeException getUnregisterLocation() { |
| 1466 | return mUnregisterLocation; |
| 1467 | } |
| 1468 | |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1469 | public void performReceive(Intent intent, int resultCode, String data, |
| 1470 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
Dianne Hackborn | 41bfaf8 | 2016-06-08 16:33:29 -0700 | [diff] [blame] | 1471 | final Args args = new Args(intent, resultCode, data, extras, ordered, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1472 | sticky, sendingUser); |
Dianne Hackborn | 41bfaf8 | 2016-06-08 16:33:29 -0700 | [diff] [blame] | 1473 | if (intent == null) { |
| 1474 | Log.wtf(TAG, "Null intent received"); |
| 1475 | } else { |
| 1476 | if (ActivityThread.DEBUG_BROADCAST) { |
| 1477 | int seq = intent.getIntExtra("seq", -1); |
| 1478 | Slog.i(ActivityThread.TAG, "Enqueueing broadcast " + intent.getAction() |
| 1479 | + " seq=" + seq + " to " + mReceiver); |
| 1480 | } |
| 1481 | } |
Makoto Onuki | 08408b9 | 2017-05-09 13:48:53 -0700 | [diff] [blame] | 1482 | if (intent == null || !mActivityThread.post(args.getRunnable())) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1483 | if (mRegistered && ordered) { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1484 | IActivityManager mgr = ActivityManager.getService(); |
Dianne Hackborn | e829fef | 2010-10-26 17:44:01 -0700 | [diff] [blame] | 1485 | if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG, |
| 1486 | "Finishing sync broadcast to " + mReceiver); |
| 1487 | args.sendFinished(mgr); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | } |
| 1493 | |
| 1494 | public final IServiceConnection getServiceDispatcher(ServiceConnection c, |
| 1495 | Context context, Handler handler, int flags) { |
| 1496 | synchronized (mServices) { |
| 1497 | LoadedApk.ServiceDispatcher sd = null; |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1498 | ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> map = mServices.get(context); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1499 | if (map != null) { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1500 | if (DEBUG) Slog.d(TAG, "Returning existing dispatcher " + sd + " for conn " + c); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1501 | sd = map.get(c); |
| 1502 | } |
| 1503 | if (sd == null) { |
| 1504 | sd = new ServiceDispatcher(c, context, handler, flags); |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1505 | if (DEBUG) Slog.d(TAG, "Creating new dispatcher " + sd + " for conn " + c); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1506 | if (map == null) { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1507 | map = new ArrayMap<>(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1508 | mServices.put(context, map); |
| 1509 | } |
| 1510 | map.put(c, sd); |
| 1511 | } else { |
| 1512 | sd.validate(context, handler); |
| 1513 | } |
| 1514 | return sd.getIServiceConnection(); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | public final IServiceConnection forgetServiceDispatcher(Context context, |
| 1519 | ServiceConnection c) { |
| 1520 | synchronized (mServices) { |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1521 | ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> map |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1522 | = mServices.get(context); |
| 1523 | LoadedApk.ServiceDispatcher sd = null; |
| 1524 | if (map != null) { |
| 1525 | sd = map.get(c); |
| 1526 | if (sd != null) { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1527 | if (DEBUG) Slog.d(TAG, "Removing dispatcher " + sd + " for conn " + c); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1528 | map.remove(c); |
| 1529 | sd.doForget(); |
| 1530 | if (map.size() == 0) { |
| 1531 | mServices.remove(context); |
| 1532 | } |
| 1533 | if ((sd.getFlags()&Context.BIND_DEBUG_UNBIND) != 0) { |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1534 | ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> holder |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1535 | = mUnboundServices.get(context); |
| 1536 | if (holder == null) { |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1537 | holder = new ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher>(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1538 | mUnboundServices.put(context, holder); |
| 1539 | } |
| 1540 | RuntimeException ex = new IllegalArgumentException( |
| 1541 | "Originally unbound here:"); |
| 1542 | ex.fillInStackTrace(); |
| 1543 | sd.setUnbindLocation(ex); |
| 1544 | holder.put(c, sd); |
| 1545 | } |
| 1546 | return sd.getIServiceConnection(); |
| 1547 | } |
| 1548 | } |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1549 | ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> holder |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1550 | = mUnboundServices.get(context); |
| 1551 | if (holder != null) { |
| 1552 | sd = holder.get(c); |
| 1553 | if (sd != null) { |
| 1554 | RuntimeException ex = sd.getUnbindLocation(); |
| 1555 | throw new IllegalArgumentException( |
| 1556 | "Unbinding Service " + c |
| 1557 | + " that was already unbound", ex); |
| 1558 | } |
| 1559 | } |
| 1560 | if (context == null) { |
| 1561 | throw new IllegalStateException("Unbinding Service " + c |
| 1562 | + " from Context that is no longer in use: " + context); |
| 1563 | } else { |
| 1564 | throw new IllegalArgumentException("Service not registered: " + c); |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | static final class ServiceDispatcher { |
| 1570 | private final ServiceDispatcher.InnerConnection mIServiceConnection; |
| 1571 | private final ServiceConnection mConnection; |
| 1572 | private final Context mContext; |
| 1573 | private final Handler mActivityThread; |
| 1574 | private final ServiceConnectionLeaked mLocation; |
| 1575 | private final int mFlags; |
| 1576 | |
| 1577 | private RuntimeException mUnbindLocation; |
| 1578 | |
Dianne Hackborn | 5a6ef73 | 2011-11-11 12:31:52 -0800 | [diff] [blame] | 1579 | private boolean mForgotten; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1580 | |
| 1581 | private static class ConnectionInfo { |
| 1582 | IBinder binder; |
| 1583 | IBinder.DeathRecipient deathMonitor; |
| 1584 | } |
| 1585 | |
| 1586 | private static class InnerConnection extends IServiceConnection.Stub { |
| 1587 | final WeakReference<LoadedApk.ServiceDispatcher> mDispatcher; |
| 1588 | |
| 1589 | InnerConnection(LoadedApk.ServiceDispatcher sd) { |
| 1590 | mDispatcher = new WeakReference<LoadedApk.ServiceDispatcher>(sd); |
| 1591 | } |
| 1592 | |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1593 | public void connected(ComponentName name, IBinder service, boolean dead) |
| 1594 | throws RemoteException { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1595 | LoadedApk.ServiceDispatcher sd = mDispatcher.get(); |
| 1596 | if (sd != null) { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1597 | sd.connected(name, service, dead); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1602 | private final ArrayMap<ComponentName, ServiceDispatcher.ConnectionInfo> mActiveConnections |
| 1603 | = new ArrayMap<ComponentName, ServiceDispatcher.ConnectionInfo>(); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1604 | |
| 1605 | ServiceDispatcher(ServiceConnection conn, |
| 1606 | Context context, Handler activityThread, int flags) { |
| 1607 | mIServiceConnection = new InnerConnection(this); |
| 1608 | mConnection = conn; |
| 1609 | mContext = context; |
| 1610 | mActivityThread = activityThread; |
| 1611 | mLocation = new ServiceConnectionLeaked(null); |
| 1612 | mLocation.fillInStackTrace(); |
| 1613 | mFlags = flags; |
| 1614 | } |
| 1615 | |
| 1616 | void validate(Context context, Handler activityThread) { |
| 1617 | if (mContext != context) { |
| 1618 | throw new RuntimeException( |
| 1619 | "ServiceConnection " + mConnection + |
| 1620 | " registered with differing Context (was " + |
| 1621 | mContext + " now " + context + ")"); |
| 1622 | } |
| 1623 | if (mActivityThread != activityThread) { |
| 1624 | throw new RuntimeException( |
| 1625 | "ServiceConnection " + mConnection + |
| 1626 | " registered with differing handler (was " + |
| 1627 | mActivityThread + " now " + activityThread + ")"); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | void doForget() { |
| 1632 | synchronized(this) { |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 1633 | for (int i=0; i<mActiveConnections.size(); i++) { |
| 1634 | ServiceDispatcher.ConnectionInfo ci = mActiveConnections.valueAt(i); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1635 | ci.binder.unlinkToDeath(ci.deathMonitor, 0); |
| 1636 | } |
| 1637 | mActiveConnections.clear(); |
Dianne Hackborn | 5a6ef73 | 2011-11-11 12:31:52 -0800 | [diff] [blame] | 1638 | mForgotten = true; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | ServiceConnectionLeaked getLocation() { |
| 1643 | return mLocation; |
| 1644 | } |
| 1645 | |
| 1646 | ServiceConnection getServiceConnection() { |
| 1647 | return mConnection; |
| 1648 | } |
| 1649 | |
| 1650 | IServiceConnection getIServiceConnection() { |
| 1651 | return mIServiceConnection; |
| 1652 | } |
| 1653 | |
| 1654 | int getFlags() { |
| 1655 | return mFlags; |
| 1656 | } |
| 1657 | |
| 1658 | void setUnbindLocation(RuntimeException ex) { |
| 1659 | mUnbindLocation = ex; |
| 1660 | } |
| 1661 | |
| 1662 | RuntimeException getUnbindLocation() { |
| 1663 | return mUnbindLocation; |
| 1664 | } |
| 1665 | |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1666 | public void connected(ComponentName name, IBinder service, boolean dead) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1667 | if (mActivityThread != null) { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1668 | mActivityThread.post(new RunConnection(name, service, 0, dead)); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1669 | } else { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1670 | doConnected(name, service, dead); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | public void death(ComponentName name, IBinder service) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1675 | if (mActivityThread != null) { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1676 | mActivityThread.post(new RunConnection(name, service, 1, false)); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1677 | } else { |
| 1678 | doDeath(name, service); |
| 1679 | } |
| 1680 | } |
| 1681 | |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1682 | public void doConnected(ComponentName name, IBinder service, boolean dead) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1683 | ServiceDispatcher.ConnectionInfo old; |
| 1684 | ServiceDispatcher.ConnectionInfo info; |
| 1685 | |
| 1686 | synchronized (this) { |
Dianne Hackborn | 5a6ef73 | 2011-11-11 12:31:52 -0800 | [diff] [blame] | 1687 | if (mForgotten) { |
| 1688 | // We unbound before receiving the connection; ignore |
| 1689 | // any connection received. |
| 1690 | return; |
| 1691 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1692 | old = mActiveConnections.get(name); |
| 1693 | if (old != null && old.binder == service) { |
| 1694 | // Huh, already have this one. Oh well! |
| 1695 | return; |
| 1696 | } |
| 1697 | |
| 1698 | if (service != null) { |
| 1699 | // A new service is being connected... set it all up. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1700 | info = new ConnectionInfo(); |
| 1701 | info.binder = service; |
| 1702 | info.deathMonitor = new DeathMonitor(name, service); |
| 1703 | try { |
| 1704 | service.linkToDeath(info.deathMonitor, 0); |
| 1705 | mActiveConnections.put(name, info); |
| 1706 | } catch (RemoteException e) { |
| 1707 | // This service was dead before we got it... just |
| 1708 | // don't do anything with it. |
| 1709 | mActiveConnections.remove(name); |
| 1710 | return; |
| 1711 | } |
| 1712 | |
| 1713 | } else { |
| 1714 | // The named service is being disconnected... clean up. |
| 1715 | mActiveConnections.remove(name); |
| 1716 | } |
| 1717 | |
| 1718 | if (old != null) { |
| 1719 | old.binder.unlinkToDeath(old.deathMonitor, 0); |
| 1720 | } |
| 1721 | } |
| 1722 | |
Adrian Roos | a9b4318 | 2016-07-21 13:20:38 -0700 | [diff] [blame] | 1723 | // If there was an old service, it is now disconnected. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1724 | if (old != null) { |
| 1725 | mConnection.onServiceDisconnected(name); |
| 1726 | } |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1727 | if (dead) { |
Dianne Hackborn | ac26534 | 2017-04-19 14:53:18 -0700 | [diff] [blame] | 1728 | mConnection.onBindingDied(name); |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1729 | } |
Christopher Tate | 5d73b6d | 2017-10-06 16:15:34 -0700 | [diff] [blame] | 1730 | // If there is a new viable service, it is now connected. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1731 | if (service != null) { |
| 1732 | mConnection.onServiceConnected(name, service); |
Christopher Tate | 5d73b6d | 2017-10-06 16:15:34 -0700 | [diff] [blame] | 1733 | } else { |
| 1734 | // The binding machinery worked, but the remote returned null from onBind(). |
| 1735 | mConnection.onNullBinding(name); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | public void doDeath(ComponentName name, IBinder service) { |
Adrian Roos | a9b4318 | 2016-07-21 13:20:38 -0700 | [diff] [blame] | 1740 | synchronized (this) { |
| 1741 | ConnectionInfo old = mActiveConnections.get(name); |
| 1742 | if (old == null || old.binder != service) { |
| 1743 | // Death for someone different than who we last |
| 1744 | // reported... just ignore it. |
| 1745 | return; |
| 1746 | } |
| 1747 | mActiveConnections.remove(name); |
| 1748 | old.binder.unlinkToDeath(old.deathMonitor, 0); |
| 1749 | } |
| 1750 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1751 | mConnection.onServiceDisconnected(name); |
| 1752 | } |
| 1753 | |
| 1754 | private final class RunConnection implements Runnable { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1755 | RunConnection(ComponentName name, IBinder service, int command, boolean dead) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1756 | mName = name; |
| 1757 | mService = service; |
| 1758 | mCommand = command; |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1759 | mDead = dead; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | public void run() { |
| 1763 | if (mCommand == 0) { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1764 | doConnected(mName, mService, mDead); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1765 | } else if (mCommand == 1) { |
| 1766 | doDeath(mName, mService); |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | final ComponentName mName; |
| 1771 | final IBinder mService; |
| 1772 | final int mCommand; |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1773 | final boolean mDead; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | private final class DeathMonitor implements IBinder.DeathRecipient |
| 1777 | { |
| 1778 | DeathMonitor(ComponentName name, IBinder service) { |
| 1779 | mName = name; |
| 1780 | mService = service; |
| 1781 | } |
| 1782 | |
| 1783 | public void binderDied() { |
| 1784 | death(mName, mService); |
| 1785 | } |
| 1786 | |
| 1787 | final ComponentName mName; |
| 1788 | final IBinder mService; |
| 1789 | } |
| 1790 | } |
| 1791 | } |