Skip to content

Commit c8a950e

Browse files
committed
Update PackageManager
1 parent effb193 commit c8a950e

File tree

3 files changed

+134
-14
lines changed

3 files changed

+134
-14
lines changed

PluginCore/src/com/plugin/core/app/ActivityThread.java

+119
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
import android.app.Instrumentation;
44
import android.content.Context;
5+
import android.content.pm.ApplicationInfo;
6+
import android.content.pm.PackageManager;
57
import android.content.pm.ProviderInfo;
68
import android.os.Handler;
79

10+
import com.plugin.content.PluginDescriptor;
811
import com.plugin.core.PluginAppTrace;
912
import com.plugin.core.PluginInstrumentionWrapper;
13+
import com.plugin.core.PluginLoader;
1014
import com.plugin.util.LogUtil;
1115
import com.plugin.util.RefInvoker;
1216

1317
import java.util.List;
18+
import java.util.Map;
1419

1520
public class ActivityThread {
1621

@@ -47,6 +52,14 @@ public synchronized static Object currentActivityThread() {
4752
return sCurrentActivityThread;
4853
}
4954

55+
public static Object getResCompatibilityInfo() {
56+
return RefInvoker.getFieldObject(currentActivityThread(), android_app_ActivityThread, "mResCompatibilityInfo");
57+
}
58+
59+
public static void enableLog() {
60+
RefInvoker.setFieldObject(currentActivityThread(), android_app_ActivityThread, "localLOGV", true);
61+
}
62+
5063
public static void installContentProviders(Context context, List<ProviderInfo> providers) {
5164
RefInvoker.invokeMethod(currentActivityThread(),
5265
clazz(), android_app_ActivityThread_installContentProviders,
@@ -60,6 +73,7 @@ public static void wrapHandler() {
6073
RefInvoker.setFieldObject(handler, Handler.class.getName(), android_os_Handler_mCallback,
6174
new PluginAppTrace(handler));
6275
}
76+
6377
public static void wrapInstrumentation() {
6478
Instrumentation originalInstrumentation = (Instrumentation) RefInvoker.getFieldObject(currentActivityThread(),
6579
clazz(), android_app_ActivityThread_mInstrumentation);;
@@ -68,4 +82,109 @@ public static void wrapInstrumentation() {
6882
new PluginInstrumentionWrapper(originalInstrumentation));
6983
}
7084

85+
//getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
86+
// ClassLoader baseLoader, boolean securityViolation, boolean includeCode)
87+
88+
89+
// synchronized (mResourcesManager) {
90+
// 1834 WeakReference<LoadedApk> ref;
91+
// 1835 if (includeCode) {
92+
// 1836 ref = mPackages.get(aInfo.packageName);
93+
// 1837 } else {
94+
// 1838 ref = mResourcePackages.get(aInfo.packageName);
95+
// 1839 }
96+
// 1840 LoadedApk packageInfo = ref != null ? ref.get() : null;
97+
// 1841 if (packageInfo == null || (packageInfo.mResources != null
98+
// 1842 && !packageInfo.mResources.getAssets().isUpToDate())) {
99+
// 1843 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
100+
// 1844 : "Loading resource-only package ") + aInfo.packageName
101+
// 1845 + " (in " + (mBoundApplication != null
102+
// 1846 ? mBoundApplication.processName : null)
103+
// 1847 + ")");
104+
// 1848 packageInfo =
105+
// 1849 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
106+
// 1850 securityViolation, includeCode &&
107+
// 1851 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
108+
// 1852 if (includeCode) {
109+
// 1853 mPackages.put(aInfo.packageName,
110+
// 1854 new WeakReference<LoadedApk>(packageInfo));
111+
// 1855 } else {
112+
// 1856 mResourcePackages.put(aInfo.packageName,
113+
// 1857 new WeakReference<LoadedApk>(packageInfo));
114+
// 1858 }
115+
// 1859 }
116+
// 1860 return packageInfo;
117+
// 1861 }
118+
119+
120+
//LoadedApk 构造器
121+
// mActivityThread = activityThread;
122+
// 117 mApplicationInfo = aInfo;
123+
// 118 mPackageName = aInfo.packageName;
124+
// 119 mAppDir = aInfo.sourceDir;
125+
// 120 final int myUid = Process.myUid();
126+
// 121 mResDir = aInfo.uid == myUid ? aInfo.sourceDir
127+
// 122 : aInfo.publicSourceDir;
128+
// 123 if (!UserHandle.isSameUser(aInfo.uid, myUid) && !Process.isIsolated()) {
129+
// 124 aInfo.dataDir = PackageManager.getDataDirForUser(UserHandle.getUserId(myUid),
130+
// 125 mPackageName);
131+
// 126 }
132+
// 127 mSharedLibraries = aInfo.sharedLibraryFiles;
133+
// 128 mDataDir = aInfo.dataDir;
134+
// 129 mDataDirFile = mDataDir != null ? new File(mDataDir) : null;
135+
// 130 mLibDir = aInfo.nativeLibraryDir;
136+
// 131 mBaseClassLoader = baseLoader;
137+
// 132 mSecurityViolation = securityViolation;
138+
// 133 mIncludeCode = includeCode;
139+
// 134 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
140+
// 135
141+
// 136 if (mAppDir == null) {
142+
// 137 if (ActivityThread.mSystemContext == null) {
143+
// 138 ActivityThread.mSystemContext =
144+
// 139 ContextImpl.createSystemContext(mainThread);
145+
// 140 ResourcesManager resourcesManager = ResourcesManager.getInstance();
146+
// 141 ActivityThread.mSystemContext.getResources().updateConfiguration(
147+
// 142 resourcesManager.getConfiguration(),
148+
// 143 resourcesManager.getDisplayMetricsLocked(
149+
// 144 Display.DEFAULT_DISPLAY, mDisplayAdjustments), compatInfo);
150+
// 145 //Slog.i(TAG, "Created system resources "
151+
// 146 // + mSystemContext.getResources() + ": "
152+
// 147 // + mSystemContext.getResources().getConfiguration());
153+
// 148 }
154+
// 149 mClassLoader = ActivityThread.mSystemContext.getClassLoader();
155+
// 150 mResources = ActivityThread.mSystemContext.getResources();
156+
// 151 }
157+
//
158+
// ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent();
159+
// 39
160+
// 40 synchronized (mLoaders) {
161+
// 41 if (parent == null) {
162+
// 42 parent = baseParent;
163+
// 43 }
164+
//
165+
public static void getPackageInfo(Context hostContext, String pluginId) throws ClassNotFoundException {
166+
enableLog();
167+
Object applicationLoaders = RefInvoker.invokeStaticMethod("android.app.ApplicationLoaders", "getDefault", (Class[]) null, (Object[]) null);
168+
Map mLoaders = (Map)RefInvoker.getFieldObject(applicationLoaders, "android.app.ApplicationLoaders", "mLoaders");
169+
PluginDescriptor pd = PluginLoader.initPluginByPluginId(pluginId);
170+
mLoaders.put(pd.getInstalledPath(), pd.getPluginClassLoader());
171+
try {
172+
ApplicationInfo info = hostContext.getPackageManager().getApplicationInfo(pluginId, PackageManager.GET_SHARED_LIBRARY_FILES);
173+
Object compatibilityInfo = getResCompatibilityInfo();//Not Sure
174+
ClassLoader baseLoader = null;//MUST
175+
boolean securityViolation = false;
176+
boolean includeCode = true;
177+
//先保存
178+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
179+
//会触发替换
180+
RefInvoker.invokeMethod(currentActivityThread(), android_app_ActivityThread, "getPackageInfo",
181+
new Class[]{ApplicationInfo.class, Class.forName("android.content.res.CompatibilityInfo"), ClassLoader.class, boolean.class, boolean.class},
182+
new Object[]{info, compatibilityInfo, baseLoader, securityViolation, includeCode});
183+
//再还原
184+
Thread.currentThread().setContextClassLoader(classLoader);
185+
} catch (PackageManager.NameNotFoundException e) {
186+
e.printStackTrace();
187+
}
188+
}
189+
71190
}

PluginCore/src/com/plugin/core/systemservice/AndroidAppIPackageManager.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ public static void installProxy(PackageManager manager) {
5757
sMethods.put("queryIntentServices", new queryIntentServices());
5858
sMethods.put("resolveActivity", new resolveActivity());
5959
sMethods.put("resolveActivityAsUser", new resolveActivityAsUser());
60+
sMethods.put("resolveService", new resolveService());
6061
}
6162

6263
public static class getPackageInfo extends MethodDelegate {
6364
@Override
6465
public Object beforeInvoke(Object target, Method method, Object[] args) {
65-
LogUtil.e("beforeInvoke", method.getName());
66+
LogUtil.d("beforeInvoke", method.getName());
6667
PluginDescriptor pd = PluginLoader.getPluginDescriptorByPluginId(args[0].toString());
6768
if (pd != null) {
6869
return PluginLoader.getApplicatoin().getPackageManager().getPackageArchiveInfo(pd.getInstalledPath(), (int) args[1]);
@@ -75,7 +76,7 @@ public static class getInstalledPackages extends MethodDelegate {
7576

7677
@Override
7778
public Object afterInvoke(Object target, Method method, Object[] args, Object beforeResult, Object invokeResult) {
78-
LogUtil.e("afterInvoke", method.getName());
79+
LogUtil.d("afterInvoke", method.getName());
7980

8081
List<PackageInfo> result = (List<PackageInfo> )RefInvoker.invokeMethod(invokeResult, "android.content.pm.ParceledListSlice", "getList", (Class[])null, (Object[])null);
8182

@@ -97,7 +98,7 @@ public Object afterInvoke(Object target, Method method, Object[] args, Object be
9798
public static class queryIntentActivities extends MethodDelegate {
9899
@Override
99100
public Object beforeInvoke(Object target, Method method, Object[] args) {
100-
LogUtil.e("beforeInvoke", method.getName());
101+
LogUtil.d("beforeInvoke", method.getName());
101102
ArrayList<String> classNames = PluginLoader.matchPlugin((Intent) args[0], PluginDescriptor.ACTIVITY);
102103
if (classNames != null && classNames.size() > 0) {
103104
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(classNames.get(0));
@@ -115,7 +116,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
115116
public static class getApplicationInfo extends MethodDelegate {
116117
@Override
117118
public Object beforeInvoke(Object target, Method method, Object[] args) {
118-
LogUtil.e("beforeInvoke", method.getName());
119+
LogUtil.d("beforeInvoke", method.getName());
119120
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByPluginId(args[0].toString());
120121
if (pluginDescriptor != null) {
121122
return getApplicationInfo(pluginDescriptor);
@@ -127,7 +128,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
127128
public static class getActivityInfo extends MethodDelegate {
128129
@Override
129130
public Object beforeInvoke(Object target, Method method, Object[] args) {
130-
LogUtil.e("beforeInvoke", method.getName());
131+
LogUtil.d("beforeInvoke", method.getName());
131132
String className = ((ComponentName)args[0]).getClassName();
132133
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(className);
133134
if (pluginDescriptor != null) {
@@ -141,7 +142,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
141142
public static class getReceiverInfo extends MethodDelegate {
142143
@Override
143144
public Object beforeInvoke(Object target, Method method, Object[] args) {
144-
LogUtil.e("beforeInvoke", method.getName());
145+
LogUtil.d("beforeInvoke", method.getName());
145146
String className = ((ComponentName)args[0]).getClassName();
146147
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(className);
147148
if (pluginDescriptor != null) {
@@ -155,7 +156,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
155156
public static class getServiceInfo extends MethodDelegate {
156157
@Override
157158
public Object beforeInvoke(Object target, Method method, Object[] args) {
158-
LogUtil.e("beforeInvoke", method.getName());
159+
LogUtil.d("beforeInvoke", method.getName());
159160
String className = ((ComponentName)args[0]).getClassName();
160161
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(className);
161162
if (pluginDescriptor != null) {
@@ -169,7 +170,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
169170
public static class getProviderInfo extends MethodDelegate {
170171
@Override
171172
public Object beforeInvoke(Object target, Method method, Object[] args) {
172-
LogUtil.e("beforeInvoke", method.getName());
173+
LogUtil.d("beforeInvoke", method.getName());
173174
String className = ((ComponentName)args[0]).getClassName();
174175
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(className);
175176
if (pluginDescriptor != null) {
@@ -192,7 +193,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
192193
public static class queryIntentServices extends MethodDelegate {
193194
@Override
194195
public Object beforeInvoke(Object target, Method method, Object[] args) {
195-
LogUtil.e("beforeInvoke", method.getName());
196+
LogUtil.d("beforeInvoke", method.getName());
196197
ArrayList<String> classNames = PluginLoader.matchPlugin((Intent) args[0], PluginDescriptor.SERVICE);
197198
if (classNames != null && classNames.size() > 0) {
198199
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(classNames.get(0));
@@ -209,7 +210,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
209210
public static class resolveIntent extends MethodDelegate {
210211
@Override
211212
public Object beforeInvoke(Object target, Method method, Object[] args) {
212-
LogUtil.e("beforeInvoke", method.getName());
213+
LogUtil.d("beforeInvoke", method.getName());
213214
ArrayList<String> classNames = PluginLoader.matchPlugin((Intent) args[0], PluginDescriptor.ACTIVITY);
214215
if (classNames != null && classNames.size() > 0) {
215216
List<ResolveInfo> result = new ArrayList<>();
@@ -226,7 +227,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
226227
public static class resolveService extends MethodDelegate {
227228
@Override
228229
public Object beforeInvoke(Object target, Method method, Object[] args) {
229-
LogUtil.e("beforeInvoke", method.getName());
230+
LogUtil.d("beforeInvoke", method.getName());
230231
ArrayList<String> classNames = PluginLoader.matchPlugin((Intent) args[0], PluginDescriptor.SERVICE);
231232
if (classNames != null && classNames.size() > 0) {
232233
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(classNames.get(0));
@@ -242,7 +243,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
242243
public static class resolveActivity extends MethodDelegate {
243244
@Override
244245
public Object beforeInvoke(Object target, Method method, Object[] args) {
245-
LogUtil.e("beforeInvoke", method.getName());
246+
LogUtil.d("beforeInvoke", method.getName());
246247
ArrayList<String> classNames = PluginLoader.matchPlugin((Intent) args[0], PluginDescriptor.ACTIVITY);
247248
if (classNames != null && classNames.size() > 0) {
248249
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(classNames.get(0));
@@ -258,7 +259,7 @@ public Object beforeInvoke(Object target, Method method, Object[] args) {
258259
public static class resolveActivityAsUser extends MethodDelegate {
259260
@Override
260261
public Object beforeInvoke(Object target, Method method, Object[] args) {
261-
LogUtil.e("beforeInvoke", method.getName());
262+
LogUtil.d("beforeInvoke", method.getName());
262263
ArrayList<String> classNames = PluginLoader.matchPlugin((Intent) args[0], PluginDescriptor.ACTIVITY);
263264
if (classNames != null && classNames.size() > 0) {
264265
PluginDescriptor pluginDescriptor = PluginLoader.getPluginDescriptorByClassName(classNames.get(0));

PluginCore/src/com/plugin/util/RefInvoker.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static Object invokeMethod(Object target, String className, String method
3232
public static Object invokeMethod(Object target, Class clazz, String methodName, Class[] paramTypes,
3333
Object[] paramValues) {
3434
try {
35-
LogUtil.e("Method", methodName);
35+
//LogUtil.e("Method", methodName);
3636
Method method = clazz.getDeclaredMethod(methodName, paramTypes);
3737
method.setAccessible(true);
3838
return method.invoke(target, paramValues);

0 commit comments

Comments
 (0)