Skip to content

Commit 596d0bb

Browse files
committed
1、修改插件的配置方式,插件信息完全从插件Manifest文件中提取
2、增强对Instrumentation的代理
1 parent b105403 commit 596d0bb

18 files changed

Lines changed: 2333 additions & 394 deletions

PluginCore/src/com/plugin/core/PluginAppTrace.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package com.plugin.core;
22

3+
import java.util.concurrent.BlockingQueue;
4+
import java.util.concurrent.LinkedBlockingQueue;
5+
6+
import com.plugin.core.ui.PluginStubService;
7+
import com.plugin.util.RefInvoker;
8+
9+
import dalvik.system.DexClassLoader;
10+
import android.app.Application;
11+
import android.content.Intent;
12+
import android.content.pm.ServiceInfo;
313
import android.os.Handler;
414
import android.os.Message;
515
import android.util.Log;
@@ -31,7 +41,7 @@ public boolean handleMessage(Message msg) {
3141
}
3242
return true;
3343
}
34-
44+
3545
private static class CodeConst {
3646
public static final int LAUNCH_ACTIVITY = 100;
3747
public static final int PAUSE_ACTIVITY = 101;

PluginCore/src/com/plugin/core/PluginApplication.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.plugin.util.RefInvoker;
44

55
import android.app.Application;
6+
import android.app.Instrumentation;
67
import android.os.Handler;
78

89
public class PluginApplication extends Application {
@@ -23,8 +24,10 @@ private void inject() {
2324
mProcessName = (String)RefInvoker.invokeMethod(activityThread, "android.app.ActivityThread",
2425
"getProcessName", (Class[])null, (Object[])null);
2526

27+
//给Instrumentation添加一层代理,用来实现隐藏api的调用
28+
Instrumentation originalInstrumentation = (Instrumentation)RefInvoker.getFieldObject(activityThread, "android.app.ActivityThread", "mInstrumentation");
2629
RefInvoker.setFieldObject(activityThread, "android.app.ActivityThread", "mInstrumentation",
27-
new PluginInstrumention());
30+
new PluginInstrumentionWrapper(originalInstrumentation));
2831

2932
//getHandler
3033
Handler handler = (Handler)RefInvoker.getStaticFieldObject("android.app.ActivityThread", "sMainThreadHandler");

PluginCore/src/com/plugin/core/PluginDescriptor.java

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,43 @@
11
package com.plugin.core;
22

33
import java.io.Serializable;
4+
import java.util.ArrayList;
45
import java.util.HashMap;
56

7+
import android.app.Application;
68
import android.content.Context;
9+
import android.content.IntentFilter;
10+
import android.content.pm.PackageInfo;
711
import android.util.Log;
812
import dalvik.system.DexClassLoader;
913

1014
/**
1115
* <Pre>
12-
* {
13-
* "id":123,
14-
* "version":"1.0.1",
15-
* "description":"插件介绍",
16-
* "application":"com.example.plugintest.PluginApplication",
17-
* "fragments":{
18-
* "test1":"com.example.plugintest.PluginMustRunInSpec",
19-
* "test2":"com.example.plugintest.PluginRunEasy"
20-
* },
21-
* "activities":{
22-
* "test3":"com.example.plugintest.PluginTextActivity",
23-
* },
24-
* "services":{
25-
* "test4":"com.example.plugintest.PluginTextService"
26-
* }
27-
* }
2816
* @author cailiming
2917
* </Pre>
3018
*/
3119
public class PluginDescriptor implements Serializable {
3220

3321
private static final long serialVersionUID = -7545734825911798344L;
3422

35-
private String id;
23+
private String packageName;
3624

3725
private String version;
3826

3927
private String description;
4028

4129
private boolean isEnabled;
4230

43-
private String application;
31+
private String applicationName;
4432

45-
private HashMap<String, String> fragments;
33+
private HashMap<String, String> fragments = new HashMap<String, String>();
4634

47-
private HashMap<String, String> activities;
35+
private HashMap<String, ArrayList<IntentFilter>> components;
4836

49-
private HashMap<String, String> services;
50-
5137
private String installedPath;
5238

39+
private transient Application pluginApplication;
40+
5341
private transient DexClassLoader pluginClassLoader;
5442

5543
private transient Context pluginContext;
@@ -70,12 +58,12 @@ public void setPluginClassLoader(DexClassLoader pluginLoader) {
7058
this.pluginClassLoader = pluginLoader;
7159
}
7260

73-
public String getId() {
74-
return id;
61+
public String getPackageName() {
62+
return packageName;
7563
}
7664

77-
public void setId(String id) {
78-
this.id = id;
65+
public void setPackageName(String packageName) {
66+
this.packageName = packageName;
7967
}
8068

8169
public String getVersion() {
@@ -102,12 +90,12 @@ public void setInstalledPath(String installedPath) {
10290
this.installedPath = installedPath;
10391
}
10492

105-
public HashMap<String, String> getActivities() {
106-
return activities;
93+
public HashMap<String, ArrayList<IntentFilter>> getComponents() {
94+
return components;
10795
}
10896

109-
public void setActivities(HashMap<String, String> activities) {
110-
this.activities = activities;
97+
public void setComponents(HashMap<String, ArrayList<IntentFilter>> activities) {
98+
this.components = activities;
11199
}
112100

113101
public String getDescription() {
@@ -118,20 +106,12 @@ public void setDescription(String description) {
118106
this.description = description;
119107
}
120108

121-
public String getApplication() {
122-
return application;
109+
public String getApplicationName() {
110+
return applicationName;
123111
}
124112

125-
public void setApplication(String application) {
126-
this.application = application;
127-
}
128-
129-
public HashMap<String, String> getServices() {
130-
return services;
131-
}
132-
133-
public void setServices(HashMap<String, String> services) {
134-
this.services = services;
113+
public void setApplicationName(String applicationName) {
114+
this.applicationName = applicationName;
135115
}
136116

137117
public boolean isEnabled() {
@@ -142,15 +122,14 @@ public void setEnabled(boolean isEnabled) {
142122
this.isEnabled = isEnabled;
143123
}
144124

125+
/**
126+
* 需要根据id查询的只有fragment
127+
* @param clazzId
128+
* @return
129+
*/
145130
public String getPluginClassNameById(String clazzId) {
146131
String clazzName = getFragments().get(clazzId);
147-
if (clazzName == null) {
148-
clazzName = getActivities().get(clazzId);
149-
}
150-
if (clazzName == null) {
151-
clazzName = getServices().get(clazzId);
152-
}
153-
132+
154133
if (clazzName == null) {
155134
Log.d("PluginDescriptor", "clazzName not found for classId " + clazzId);
156135
} else {
@@ -159,23 +138,35 @@ public String getPluginClassNameById(String clazzId) {
159138
return clazzName;
160139
}
161140

162-
public boolean containsId(String clazzId) {
141+
public Application getPluginApplication() {
142+
return pluginApplication;
143+
}
144+
145+
public void setPluginApplication(Application pluginApplication) {
146+
this.pluginApplication = pluginApplication;
147+
}
148+
149+
/**
150+
* 需要根据Id查询的只有fragment
151+
* @param clazzId
152+
* @return
153+
*/
154+
public boolean containsFragment(String clazzId) {
163155
if (getFragments().containsKey(clazzId) && isEnabled()) {
164156
return true;
165-
} else if (getActivities().containsKey(clazzId) && isEnabled()) {
166-
return true;
167-
} else if (getServices().containsKey(clazzId) && isEnabled()) {
168-
return true;
169157
}
170158
return false;
171159
}
172160

161+
/**
162+
* 根据className查询
163+
* @param clazzName
164+
* @return
165+
*/
173166
public boolean containsName(String clazzName) {
174167
if (getFragments().containsValue(clazzName) && isEnabled()) {
175168
return true;
176-
} else if (getActivities().containsValue(clazzName) && isEnabled()) {
177-
return true;
178-
} else if (getServices().containsValue(clazzName) && isEnabled()) {
169+
} else if (getComponents().containsKey(clazzName) && isEnabled()) {
179170
return true;
180171
}
181172
return false;

PluginCore/src/com/plugin/core/PluginInstrumention.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ public Activity newActivity(ClassLoader cl, String className, Intent intent)
2828
ClassNotFoundException {
2929

3030
String targetClassName = intent.getStringExtra("className");
31-
String targetId = intent.getStringExtra("classId");
32-
31+
3332
Log.d(LOG_TAG, intent.toUri(0));
34-
Log.d(LOG_TAG, "className = " + className + ", targetClassName = " + targetClassName + ", targetId = " + targetId);
3533

3634
if (className.equals(PluginStubActivity.class.getName())) {
3735

@@ -41,12 +39,6 @@ public Activity newActivity(ClassLoader cl, String className, Intent intent)
4139
if (clazz != null) {
4240
return (Activity)clazz.newInstance();
4341
}
44-
} else if (targetId != null) {
45-
@SuppressWarnings("rawtypes")
46-
Class clazz = PluginLoader.loadPluginClassById(targetId);
47-
if (clazz != null) {
48-
return (Activity)clazz.newInstance();
49-
}
5042
}
5143
}
5244

0 commit comments

Comments
 (0)