|
1 | 1 | package com.plugin.core;
|
2 | 2 |
|
| 3 | +import com.plugin.core.ui.stub.PluginStubReceiver; |
| 4 | +import com.plugin.core.ui.stub.PluginStubService; |
3 | 5 | import com.plugin.util.LogUtil;
|
4 | 6 | import com.plugin.util.RefInvoker;
|
5 | 7 |
|
6 | 8 | import android.app.Application;
|
7 | 9 | import android.app.Instrumentation;
|
| 10 | +import android.content.ComponentName; |
| 11 | +import android.content.Intent; |
8 | 12 | import android.os.Handler;
|
9 | 13 |
|
10 | 14 | public class PluginApplication extends Application {
|
11 |
| - |
| 15 | + |
12 | 16 | private String mProcessName;
|
13 |
| - |
| 17 | + |
14 | 18 | public String getProcessName() {
|
15 | 19 | return mProcessName;
|
16 | 20 | }
|
17 |
| - |
| 21 | + |
18 | 22 | @Override
|
19 | 23 | public void onCreate() {
|
20 |
| - super.onCreate(); |
| 24 | + super.onCreate(); |
| 25 | + |
21 | 26 | injectInstrumentation();
|
22 | 27 | injectClassLoader();
|
23 | 28 | }
|
24 |
| - |
| 29 | + |
25 | 30 | /**
|
26 | 31 | * 注入Instrumentation主要是为了支持Activity
|
27 | 32 | */
|
28 | 33 | private void injectInstrumentation() {
|
29 | 34 | LogUtil.d("injectInstrumentation");
|
30 |
| - |
31 |
| - //从ThreadLocal中取出来的 |
| 35 | + |
| 36 | + // 从ThreadLocal中取出来的 |
32 | 37 | 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"); |
40 | 46 | RefInvoker.setFieldObject(activityThread, "android.app.ActivityThread", "mInstrumentation",
|
41 | 47 | 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 |
47 | 53 | RefInvoker.setFieldObject(handler, Handler.class.getName(), "mCallback", new PluginAppTrace(handler));
|
48 |
| - |
| 54 | + |
49 | 55 | }
|
50 |
| - |
| 56 | + |
51 | 57 | /**
|
52 | 58 | * 注入classloader主要是为了支持Service和Receiver
|
53 | 59 | */
|
54 | 60 | private void injectClassLoader() {
|
55 | 61 | //
|
56 | 62 | LogUtil.d("injectClassLoader");
|
57 | 63 | }
|
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 | + } |
59 | 104 |
|
60 | 105 | }
|
0 commit comments