Skip to content

Commit eb10f65

Browse files
committed
Update Core
1 parent 990ed57 commit eb10f65

9 files changed

+199
-191
lines changed

PluginCore/AndroidManifest.xml

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,99 +10,115 @@
1010

1111
<application>
1212

13-
<receiver android:name="com.plugin.core.stub.PluginStubReceiver">
13+
<!--
14+
对Receiver的类全名没有要求,可随意指定,框架中是通过action来查询。
15+
-->
16+
<receiver android:name="com.plugin.core.stub.PluginStubReceiver"
17+
android:exported="true">
18+
<intent-filter>
19+
<action
20+
android:name="com.plugin.core.STUB_DEFAULT" />
21+
<category
22+
android:name="android.intent.category.DEFAULT" />
23+
</intent-filter>
1424
</receiver>
1525

1626
<!--
17-
所有的插件中共有多少个Service, 这里就注册多少个对应的虚拟Service,这里的虚拟Service并不需要真实存在,
27+
所有的插件中共有多少个Service, 这里就注册多少个对应的虚拟Service,
28+
这里的虚拟Service并不需要真实存在,
1829
只是占个位置,运行实动态绑定到实际的插件Service
1930
对service的类全名没有要求,可随意指定,框架中是通过action来查询。
2031
-->
21-
<service android:name="com.plugin.core.stub.PluginProxyService1">
32+
<service android:name="com.plugin.core.stub.PluginStubService1">
2233
<intent-filter>
2334
<action
24-
android:name="com.plugin.core.STUB_SERVICE" />
35+
android:name="com.plugin.core.STUB_DEFAULT" />
2536
<category
2637
android:name="android.intent.category.DEFAULT" />
2738
</intent-filter>
2839
</service>
2940

30-
<service android:name="com.plugin.core.stub.PluginProxyService2">
41+
<service android:name="com.plugin.core.stub.PluginStubService2">
3142
<intent-filter>
3243
<action
33-
android:name="com.plugin.core.STUB_SERVICE" />
44+
android:name="com.plugin.core.STUB_DEFAULT" />
3445
<category
3546
android:name="android.intent.category.DEFAULT" />
3647
</intent-filter>
3748
</service>
3849

39-
<service android:name="com.plugin.core.stub.PluginProxyService3">
50+
<service android:name="com.plugin.core.stub.PluginStubService3">
4051
<intent-filter>
4152
<action
42-
android:name="com.plugin.core.STUB_SERVICE" />
53+
android:name="com.plugin.core.STUB_DEFAULT" />
4354
<category
4455
android:name="android.intent.category.DEFAULT" />
4556
</intent-filter>
4657
</service>
4758

4859
<activity
49-
android:name="com.plugin.core.stub.ui.PluginStubActivity">
60+
android:name="com.plugin.core.stub.PluginStubActivity0"
61+
android:exported="true">
62+
<intent-filter>
63+
<action
64+
android:name="com.plugin.core.STUB_DEFAULT" />
65+
<category
66+
android:name="android.intent.category.DEFAULT" />
67+
</intent-filter>
5068
</activity>
5169

5270
<!--///////////////////////////////////////////////////////////////////-->
5371
<!--///////////////////////////////////////////////////////////////////-->
5472
<!--为了支持非standard的launchMode,
55-
所有的插件中共有多少个非standard模式的Activity,就在这里注册多少个对应的虚拟的Activity,这些虚拟Activity并不需要真实存在,
56-
(实际情况中注册的虚拟activity个数可以比插件中的非standard的activity个数少。因为不太可能所有的插件activity逗同时被唤起)
57-
58-
虚Activity的命名需遵守如下约定:和PluginStubActivity在同一个包下。
59-
60-
原因是插件框架会先通过action进行查询,再通过PluginStubActivity这个类所在的包名进行判断
61-
73+
所有的插件中共有多少个非standard模式的Activity,就在这里注册多少个对应的虚拟的Activity(standard的只需要1个),
74+
这些虚拟Activity并不需要真实存在,
75+
(实际情况中注册的虚拟activity个数可以比插件中的非standard的activity个数少。
76+
因为不太可能所有的插件activity都同时被唤起)
77+
对Activity的类全名没有要求,可随意指定,框架中是通过action来查询。
6278
-->
6379
<activity
64-
android:name="com.plugin.core.stub.ui.PluginStubActivity1"
65-
android:exported="false"
80+
android:name="com.plugin.core.stub.PluginStubActivity1"
81+
android:exported="true"
6682
android:launchMode="singleTask">
6783
<intent-filter>
6884
<action
69-
android:name="com.plugin.core.LAUNCH_MODE" />
85+
android:name="com.plugin.core.STUB_DEFAULT" />
7086
<category
7187
android:name="android.intent.category.DEFAULT" />
7288
</intent-filter>
7389
</activity>
7490

7591
<activity
76-
android:name="com.plugin.core.stub.ui.PluginStubActivity2"
77-
android:exported="false"
92+
android:name="com.plugin.core.stub.PluginStubActivity2"
93+
android:exported="true"
7894
android:launchMode="singleTask">
7995
<intent-filter>
8096
<action
81-
android:name="com.plugin.core.LAUNCH_MODE" />
97+
android:name="com.plugin.core.STUB_DEFAULT" />
8298
<category
8399
android:name="android.intent.category.DEFAULT" />
84100
</intent-filter>
85101
</activity>
86102

87103
<activity
88-
android:name="com.plugin.core.stub.ui.PluginStubActivity3"
89-
android:exported="false"
104+
android:name="com.plugin.core.stub.PluginStubActivity3"
105+
android:exported="true"
90106
android:launchMode="singleTop">
91107
<intent-filter>
92108
<action
93-
android:name="com.plugin.core.LAUNCH_MODE" />
109+
android:name="com.plugin.core.STUB_DEFAULT" />
94110
<category
95111
android:name="android.intent.category.DEFAULT" />
96112
</intent-filter>
97113
</activity>
98114

99115
<activity
100-
android:name="com.plugin.core.stub.ui.PluginStubActivity4"
101-
android:exported="false"
116+
android:name="com.plugin.core.stub.PluginStubActivity4"
117+
android:exported="true"
102118
android:launchMode="singleInstance">
103119
<intent-filter>
104120
<action
105-
android:name="com.plugin.core.LAUNCH_MODE" />
121+
android:name="com.plugin.core.STUB_DEFAULT" />
106122
<category
107123
android:name="android.intent.category.DEFAULT" />
108124
</intent-filter>

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public String findLibrary(String name) {
2727
protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
2828

2929
//Just for Receiver and service
30-
if (className.startsWith(PluginIntentResolver.prefix)) {
31-
String realName = className.replace(PluginIntentResolver.prefix, "");
30+
if (className.startsWith(PluginIntentResolver.CLASS_PREFIX)) {
31+
String realName = className.replace(PluginIntentResolver.CLASS_PREFIX, "");
3232
LogUtil.d("className ", className, "target", realName);
3333
Class clazz = PluginLoader.loadPluginClassByName(realName);
3434
if (clazz != null) {
@@ -42,13 +42,8 @@ protected Class<?> loadClass(String className, boolean resolve) throws ClassNotF
4242
if (clazz != null) {
4343
return clazz;
4444
}
45-
} else if (PluginStubBinding.isExact(className)) {
46-
LogUtil.d("className ", className, "target", className);
47-
Class clazz = PluginLoader.loadPluginClassByName(className);
48-
if (clazz != null) {
49-
return clazz;
50-
}
5145
}
46+
5247
return super.loadClass(className, resolve);
5348
}
5449

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ static void injectActivityContext(Activity activity) {
137137
Intent intent = activity.getIntent();
138138
FragmentContainer fragmentContainer = AnnotationProcessor.getFragmentContainer(activity.getClass());
139139
// 如果是打开插件中的activity, 或者是打开的用来显示插件fragment的宿主activity
140-
if (fragmentContainer != null || (intent.getComponent() != null
141-
&& (intent.getComponent().getClassName().startsWith(PluginStubBinding.STUB_ACTIVITY_PRE)))) {
140+
if (fragmentContainer != null ||
141+
PluginStubBinding.isStubActivity(intent.getComponent().getClassName())) {
142142
// 为了不需要重写插件Activity的attachBaseContext方法为:
143143
// 我们在activityoncreate之前去完成attachBaseContext的事情
144144

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ public boolean onException(Object obj, Throwable e) {
5151
public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException,
5252
IllegalAccessException, ClassNotFoundException {
5353
// 将PluginStubActivity替换成插件中的activity
54-
if (className.startsWith(PluginStubBinding.STUB_ACTIVITY_PRE)) {
54+
if (PluginStubBinding.isStubActivity(className)) {
5555

5656
String action = intent.getAction();
57+
if (action != null && action.contains(PluginIntentResolver.CLASS_SEPARATOR)) {
5758

58-
//
59-
if (action != null && action.contains(PluginIntentResolver.ACTIVITY_ACTION_IN_PLUGIN)) {
60-
61-
String[] targetClassName = action.split(PluginIntentResolver.ACTIVITY_ACTION_IN_PLUGIN);
59+
String[] targetClassName = action.split(PluginIntentResolver.CLASS_SEPARATOR);
6260

6361
LogUtil.d(className, action, targetClassName[0]);
6462

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.plugin.content.PluginActivityInfo;
1010
import com.plugin.content.PluginDescriptor;
1111
import com.plugin.content.PluginReceiverIntent;
12-
import com.plugin.core.stub.PluginStubReceiver;
1312
import com.plugin.util.ClassLoaderUtil;
1413
import com.plugin.util.LogUtil;
1514
import com.plugin.util.RefInvoker;
@@ -18,19 +17,16 @@
1817

1918
public class PluginIntentResolver {
2019

21-
static final String ACTIVITY_ACTION_IN_PLUGIN = "_ACTIVITY_ACTION_IN_PLUGIN_";
22-
23-
static final String prefix = "plugin_receiver_or_service_prefix.";
24-
25-
private static String RECEIVER_ACTION_IN_PLUGIN = "_RECEIVER_ACTION_IN_PLUGIN_";
20+
static final String CLASS_SEPARATOR = "_CLASS_SEPARATOR_IN_ACTION_";
21+
static final String CLASS_PREFIX = "_CLASS_PREFIX_";
2622

2723
/* package */static void resolveService(Intent service) {
2824
ArrayList<String> classNameList = PluginLoader.matchPlugin(service, PluginDescriptor.SERVICE);
2925
if (classNameList != null && classNameList.size() > 0) {
3026
ClassLoaderUtil.hackHostClassLoaderIfNeeded();
31-
String stubActivityName = PluginStubBinding.bindStubService(classNameList.get(0));
32-
if (stubActivityName != null) {
33-
service.setClassName(PluginLoader.getApplicatoin(), stubActivityName);
27+
String stubServiceName = PluginStubBinding.bindStubService(classNameList.get(0));
28+
if (stubServiceName != null) {
29+
service.setClassName(PluginLoader.getApplicatoin(), stubServiceName);
3430
}
3531
}
3632
}
@@ -46,9 +42,9 @@ public class PluginIntentResolver {
4642
for(String className: classNameList) {
4743
Intent newIntent = new Intent(intent);
4844
newIntent.setComponent(new ComponentName(PluginLoader.getApplicatoin().getPackageName(),
49-
PluginStubReceiver.class.getName()));
45+
PluginStubBinding.bindStubReceiver()));
5046
//hackReceiverForClassLoader检测到这个标记后会进行替换
51-
newIntent.setAction(className + RECEIVER_ACTION_IN_PLUGIN + (intent.getAction() == null ? "" : intent.getAction()));
47+
newIntent.setAction(className + CLASS_SEPARATOR + (intent.getAction() == null ? "" : intent.getAction()));
5248
result.add(newIntent);
5349
}
5450
} else {
@@ -59,11 +55,11 @@ public class PluginIntentResolver {
5955

6056
/* package */static Class hackReceiverForClassLoader(Object msgObj) {
6157
Intent intent = (Intent) RefInvoker.getFieldObject(msgObj, "android.app.ActivityThread$ReceiverData", "intent");
62-
if (intent.getComponent().getClassName().equals(PluginStubReceiver.class.getName())) {
58+
if (intent.getComponent().getClassName().equals(PluginStubBinding.bindStubReceiver())) {
6359
String action = intent.getAction();
6460
LogUtil.d("action", action);
6561
if (action != null) {
66-
String[] targetClassName = action.split(RECEIVER_ACTION_IN_PLUGIN);
62+
String[] targetClassName = action.split(CLASS_SEPARATOR);
6763
@SuppressWarnings("rawtypes")
6864
Class clazz = PluginLoader.loadPluginClassByName(targetClassName[0]);
6965
if (clazz != null) {
@@ -77,7 +73,7 @@ public class PluginIntentResolver {
7773
}
7874
// PluginClassLoader检测到这个特殊标记后会进行替换
7975
intent.setComponent(new ComponentName(intent.getComponent().getPackageName(),
80-
prefix + targetClassName[0]));
76+
CLASS_PREFIX + targetClassName[0]));
8177

8278
if (Build.VERSION.SDK_INT >= 21) {
8379
if (intent.getExtras() != null) {
@@ -100,7 +96,7 @@ public class PluginIntentResolver {
10096
LogUtil.d("hackServiceName", info.name, info.packageName, info.processName, "targetClassName", targetClassName);
10197

10298
if (targetClassName != null) {
103-
info.name = prefix + targetClassName;
99+
info.name = CLASS_PREFIX + targetClassName;
104100
} else {
105101
LogUtil.e("hackServiceName 没有找到映射关系, 说明是宿主service(也可能是映射表出了异常)", info.name);
106102
}
@@ -117,12 +113,12 @@ public class PluginIntentResolver {
117113

118114
PluginActivityInfo pluginActivityInfo = pd.getActivityInfos().get(className);
119115

120-
String stubActivityName = PluginStubBinding.bindLaunchModeStubActivity(className, Integer.parseInt(pluginActivityInfo.getLaunchMode()));
116+
String stubActivityName = PluginStubBinding.bindStubActivity(className, Integer.parseInt(pluginActivityInfo.getLaunchMode()));
121117

122118
intent.setComponent(
123119
new ComponentName(PluginLoader.getApplicatoin().getPackageName(), stubActivityName));
124120
//PluginInstrumentationWrapper检测到这个标记后会进行替换
125-
intent.setAction(className + ACTIVITY_ACTION_IN_PLUGIN + (intent.getAction()==null?"":intent.getAction()));
121+
intent.setAction(className + CLASS_SEPARATOR + (intent.getAction()==null?"":intent.getAction()));
126122
}
127123
}
128124

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public static ArrayList<String> matchPlugin(Intent intent, int type) {
524524
clazzName = intent.getComponent().getClassName();
525525
result = new ArrayList<String>(1);
526526
result.add(clazzName);
527-
return result;//暂时不考虑不同的插件中配置了相同名称的组件的问题
527+
return result;//暂时不考虑不同的插件中配置了相同名称的组件的问题,先到先得
528528
}
529529
} else {
530530
// 如果是通过IntentFilter进行匹配的
@@ -535,7 +535,7 @@ public static ArrayList<String> matchPlugin(Intent intent, int type) {
535535
if (list != null && list.size() >0) {
536536
result = new ArrayList<String>(1);
537537
result.add(list.get(0));
538-
return result;//暂时不考虑多个Activity配置了相同的Intent的问题
538+
return result;//暂时不考虑多个Activity配置了相同的Intent的问题,先到先得
539539
}
540540

541541
} else if (type == PluginDescriptor.SERVICE) {

0 commit comments

Comments
 (0)