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