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