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