Skip to content

Commit 91c6afd

Browse files
committed
增强PluginApplication
Signed-off-by: limpoxe <405977146@163.com>
1 parent 0ef81fd commit 91c6afd

File tree

3 files changed

+79
-31
lines changed

3 files changed

+79
-31
lines changed
Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,105 @@
11
package com.plugin.core;
22

3+
import com.plugin.core.ui.stub.PluginStubReceiver;
4+
import com.plugin.core.ui.stub.PluginStubService;
35
import com.plugin.util.LogUtil;
46
import com.plugin.util.RefInvoker;
57

68
import android.app.Application;
79
import android.app.Instrumentation;
10+
import android.content.ComponentName;
11+
import android.content.Intent;
812
import android.os.Handler;
913

1014
public class PluginApplication extends Application {
11-
15+
1216
private String mProcessName;
13-
17+
1418
public String getProcessName() {
1519
return mProcessName;
1620
}
17-
21+
1822
@Override
1923
public void onCreate() {
20-
super.onCreate();
24+
super.onCreate();
25+
2126
injectInstrumentation();
2227
injectClassLoader();
2328
}
24-
29+
2530
/**
2631
* 注入Instrumentation主要是为了支持Activity
2732
*/
2833
private void injectInstrumentation() {
2934
LogUtil.d("injectInstrumentation");
30-
31-
//从ThreadLocal中取出来的
35+
36+
// 从ThreadLocal中取出来的
3237
Object activityThread = RefInvoker.invokeStaticMethod("android.app.ActivityThread", "currentActivityThread",
33-
(Class[])null, (Object[])null);
34-
35-
mProcessName = (String)RefInvoker.invokeMethod(activityThread, "android.app.ActivityThread",
36-
"getProcessName", (Class[])null, (Object[])null);
37-
38-
//给Instrumentation添加一层代理,用来实现隐藏api的调用
39-
Instrumentation originalInstrumentation = (Instrumentation)RefInvoker.getFieldObject(activityThread, "android.app.ActivityThread", "mInstrumentation");
38+
(Class[]) null, (Object[]) null);
39+
40+
mProcessName = (String) RefInvoker.invokeMethod(activityThread, "android.app.ActivityThread", "getProcessName",
41+
(Class[]) null, (Object[]) null);
42+
43+
// 给Instrumentation添加一层代理,用来实现隐藏api的调用
44+
Instrumentation originalInstrumentation = (Instrumentation) RefInvoker.getFieldObject(activityThread,
45+
"android.app.ActivityThread", "mInstrumentation");
4046
RefInvoker.setFieldObject(activityThread, "android.app.ActivityThread", "mInstrumentation",
4147
new PluginInstrumentionWrapper(originalInstrumentation));
42-
43-
//getHandler
44-
Handler handler = (Handler)RefInvoker.getStaticFieldObject("android.app.ActivityThread", "sMainThreadHandler");
45-
46-
//给handler添加一个callback
48+
49+
// getHandler
50+
Handler handler = (Handler) RefInvoker.getStaticFieldObject("android.app.ActivityThread", "sMainThreadHandler");
51+
52+
// 给handler添加一个callback
4753
RefInvoker.setFieldObject(handler, Handler.class.getName(), "mCallback", new PluginAppTrace(handler));
48-
54+
4955
}
50-
56+
5157
/**
5258
* 注入classloader主要是为了支持Service和Receiver
5359
*/
5460
private void injectClassLoader() {
5561
//
5662
LogUtil.d("injectClassLoader");
5763
}
58-
64+
65+
@Override
66+
public void sendBroadcast(Intent intent) {
67+
LogUtil.d("sendBroadcast", intent.toUri(0));
68+
Intent realIntent = intent;
69+
if (PluginDispatcher.hackClassLoadForReceiverIfNeeded(intent)) {
70+
realIntent = new Intent();
71+
realIntent.setClass(PluginLoader.getApplicatoin(), PluginStubReceiver.class);
72+
realIntent.putExtra(PluginDispatcher.RECEIVER_ID_IN_PLUGIN, intent);
73+
}
74+
super.sendBroadcast(realIntent);
75+
}
76+
77+
@Override
78+
public ComponentName startService(Intent service) {
79+
LogUtil.d("startService", service.toUri(0));
80+
if (PluginDispatcher.hackClassLoadForServiceIfNeeded(service)) {
81+
service.setClass(PluginLoader.getApplicatoin(), PluginStubService.class);
82+
}
83+
return super.startService(service);
84+
}
85+
86+
@Override
87+
public boolean stopService(Intent name) {
88+
LogUtil.d("stopService", name.toUri(0));
89+
if (PluginDispatcher.hackClassLoadForServiceIfNeeded(name)) {
90+
name.setClass(PluginLoader.getApplicatoin(), PluginStubService.class);
91+
}
92+
return super.stopService(name);
93+
}
94+
95+
/**
96+
* startActivity有很多重载的方法,如有必要,可以相应的重写
97+
*/
98+
@Override
99+
public void startActivity(Intent intent) {
100+
LogUtil.d("startActivity", intent.toUri(0));
101+
PluginInstrumentionWrapper.resloveIntent(intent);
102+
super.startActivity(intent);
103+
}
59104

60105
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ public boolean stopService(Intent name) {
128128
return super.stopService(name);
129129
}
130130

131+
/**
132+
* startActivity有很多重载的方法,如有必要,可以相应的重写
133+
*/
131134
@Override
132135
public void startActivity(Intent intent) {
133136
LogUtil.d("startActivity", intent.toUri(0));

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ public static void startFragmentWithBuildInActivity(Context context, String targ
6868
/**
6969
* 放弃代理模式了。采用Activity免注册方式
7070
*/
71-
@Deprecated
72-
public static void startProxyActivity(Context context, String targetId) {
71+
// @Deprecated
72+
// public static void startProxyActivity(Context context, String targetId) {
7373

74-
// Intent pluginActivity = new Intent();
75-
// pluginActivity.setClass(context, PluginProxyActivity.class);
76-
// pluginActivity.putExtra(ACTIVITY_ID_IN_PLUGIN,
77-
// resloveTarget(targetId));
78-
// pluginActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
79-
// context.startActivity(pluginActivity);
74+
// Intent pluginActivity = new Intent();
75+
// pluginActivity.setClass(context, PluginProxyActivity.class);
76+
// pluginActivity.putExtra(ACTIVITY_ID_IN_PLUGIN,
77+
// resloveTarget(targetId));
78+
// pluginActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
79+
// context.startActivity(pluginActivity);
8080

81-
}
81+
// }
8282

8383
/**
8484
* 可以通过重写基类的startServie方法,会比较方便

0 commit comments

Comments
 (0)