|
14 | 14 | import android.view.LayoutInflater;
|
15 | 15 |
|
16 | 16 | public class PluginContextTheme extends ContextWrapper {
|
17 |
| - private int mThemeResource; |
18 |
| - Resources.Theme mTheme; |
19 |
| - private LayoutInflater mInflater; |
20 |
| - |
21 |
| - Resources mResources; |
22 |
| - private ClassLoader mClassLoader; |
23 |
| - |
24 |
| - public PluginContextTheme(Context base, Resources resources, ClassLoader classLoader) { |
25 |
| - super(base); |
26 |
| - mResources = resources; |
27 |
| - mClassLoader = classLoader; |
28 |
| - } |
29 |
| - |
30 |
| - @Override protected void attachBaseContext(Context newBase) { |
31 |
| - super.attachBaseContext(newBase); |
32 |
| - } |
33 |
| - |
34 |
| - @Override |
35 |
| - public ClassLoader getClassLoader() { |
36 |
| - return mClassLoader; |
37 |
| - } |
38 |
| - |
39 |
| - @Override |
40 |
| - public AssetManager getAssets() { |
41 |
| - return mResources.getAssets(); |
42 |
| - } |
43 |
| - |
44 |
| - @Override |
45 |
| - public Resources getResources() { |
46 |
| - return mResources; |
47 |
| - } |
48 |
| - |
49 |
| - /** |
50 |
| - * 传0表示使用系统默认主题,最终的现实样式和客户端程序的minSdk应该有关系。 |
51 |
| - * 即系统针对不同的minSdk设置了不同的默认主题样式 |
52 |
| - * 传非0的话表示传过来什么主题就显示什么主题 |
53 |
| - */ |
54 |
| - @Override public void setTheme(int resid) { |
55 |
| - mThemeResource = resid; |
56 |
| - initializeTheme(); |
57 |
| - } |
58 |
| - |
59 |
| - @Override public Resources.Theme getTheme() { |
60 |
| - if (mTheme != null) { |
61 |
| - return mTheme; |
62 |
| - } |
63 |
| - |
64 |
| - Object result = RefInvoker.invokeStaticMethod(Resources.class.getName(), "selectDefaultTheme", |
65 |
| - new Class[]{int.class,int.class}, |
66 |
| - new Object[]{mThemeResource, getBaseContext().getApplicationInfo().targetSdkVersion}); |
67 |
| - if (result != null) { |
68 |
| - mThemeResource = (Integer)result; |
69 |
| - } |
70 |
| - |
71 |
| - initializeTheme(); |
72 |
| - |
73 |
| - return mTheme; |
74 |
| - } |
75 |
| - |
76 |
| - @Override public Object getSystemService(String name) { |
77 |
| - if (LAYOUT_INFLATER_SERVICE.equals(name)) { |
78 |
| - if (mInflater == null) { |
79 |
| - mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this); |
80 |
| - } |
81 |
| - return mInflater; |
82 |
| - } |
83 |
| - return getBaseContext().getSystemService(name); |
84 |
| - } |
85 |
| - |
86 |
| - private void initializeTheme() { |
87 |
| - final boolean first = mTheme == null; |
88 |
| - if (first) { |
89 |
| - mTheme = getResources().newTheme(); |
90 |
| - Resources.Theme theme = getBaseContext().getTheme(); |
91 |
| - if (theme != null) { |
92 |
| - mTheme.setTo(theme); |
93 |
| - } |
94 |
| - } |
95 |
| - mTheme.applyStyle(mThemeResource, true); |
96 |
| - } |
97 |
| - |
98 |
| - @Override |
| 17 | + private int mThemeResource; |
| 18 | + Resources.Theme mTheme; |
| 19 | + private LayoutInflater mInflater; |
| 20 | + |
| 21 | + Resources mResources; |
| 22 | + private final ClassLoader mClassLoader; |
| 23 | + |
| 24 | + public PluginContextTheme(Context base, Resources resources, ClassLoader classLoader) { |
| 25 | + super(base); |
| 26 | + mResources = resources; |
| 27 | + mClassLoader = classLoader; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void attachBaseContext(Context newBase) { |
| 32 | + super.attachBaseContext(newBase); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public ClassLoader getClassLoader() { |
| 37 | + return mClassLoader; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public AssetManager getAssets() { |
| 42 | + return mResources.getAssets(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Resources getResources() { |
| 47 | + return mResources; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * 传0表示使用系统默认主题,最终的现实样式和客户端程序的minSdk应该有关系。 即系统针对不同的minSdk设置了不同的默认主题样式 |
| 52 | + * 传非0的话表示传过来什么主题就显示什么主题 |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public void setTheme(int resid) { |
| 56 | + mThemeResource = resid; |
| 57 | + initializeTheme(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Resources.Theme getTheme() { |
| 62 | + if (mTheme != null) { |
| 63 | + return mTheme; |
| 64 | + } |
| 65 | + |
| 66 | + Object result = RefInvoker.invokeStaticMethod(Resources.class.getName(), "selectDefaultTheme", new Class[] { |
| 67 | + int.class, int.class }, new Object[] { mThemeResource, |
| 68 | + getBaseContext().getApplicationInfo().targetSdkVersion }); |
| 69 | + if (result != null) { |
| 70 | + mThemeResource = (Integer) result; |
| 71 | + } |
| 72 | + |
| 73 | + initializeTheme(); |
| 74 | + |
| 75 | + return mTheme; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Object getSystemService(String name) { |
| 80 | + if (LAYOUT_INFLATER_SERVICE.equals(name)) { |
| 81 | + if (mInflater == null) { |
| 82 | + mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this); |
| 83 | + } |
| 84 | + return mInflater; |
| 85 | + } |
| 86 | + return getBaseContext().getSystemService(name); |
| 87 | + } |
| 88 | + |
| 89 | + private void initializeTheme() { |
| 90 | + final boolean first = mTheme == null; |
| 91 | + if (first) { |
| 92 | + mTheme = getResources().newTheme(); |
| 93 | + Resources.Theme theme = getBaseContext().getTheme(); |
| 94 | + if (theme != null) { |
| 95 | + mTheme.setTo(theme); |
| 96 | + } |
| 97 | + } |
| 98 | + mTheme.applyStyle(mThemeResource, true); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
99 | 102 | public void sendBroadcast(Intent intent) {
|
100 |
| - LogUtil.d("sendBroadcast", intent.toUri(0)); |
101 |
| - Intent realIntent = intent; |
| 103 | + LogUtil.d("sendBroadcast", intent.toUri(0)); |
| 104 | + Intent realIntent = intent; |
102 | 105 | if (PluginDispatcher.hackClassLoadForReceiverIfNeeded(intent)) {
|
103 | 106 | realIntent = new Intent();
|
104 | 107 | realIntent.setClass(PluginLoader.getApplicatoin(), PluginStubReceiver.class);
|
105 | 108 | realIntent.putExtra(PluginDispatcher.RECEIVER_ID_IN_PLUGIN, intent);
|
106 | 109 | }
|
107 | 110 | super.sendBroadcast(realIntent);
|
108 | 111 | }
|
109 |
| - |
110 |
| - @Override |
111 |
| - public ComponentName startService(Intent service) { |
112 |
| - LogUtil.d("startService", service.toUri(0)); |
113 |
| - if (PluginDispatcher.hackClassLoadForServiceIfNeeded(service)) { |
114 |
| - service.setClass(PluginLoader.getApplicatoin(), PluginStubService.class); |
| 112 | + |
| 113 | + @Override |
| 114 | + public ComponentName startService(Intent service) { |
| 115 | + LogUtil.d("startService", service.toUri(0)); |
| 116 | + if (PluginDispatcher.hackClassLoadForServiceIfNeeded(service)) { |
| 117 | + service.setClass(PluginLoader.getApplicatoin(), PluginStubService.class); |
115 | 118 | }
|
116 |
| - return super.startService(service); |
117 |
| - } |
118 |
| - |
119 |
| - @Override |
120 |
| - public void startActivity(Intent intent) { |
121 |
| - LogUtil.d("startActivity", intent.toUri(0)); |
122 |
| - PluginInstrumentionWrapper.resloveIntent(intent); |
123 |
| - super.startActivity(intent); |
124 |
| - } |
125 |
| - |
126 |
| -// @Override |
127 |
| -// public Context getApplicationContext() { |
128 |
| -// if (getBaseContext() instanceof Activity) { |
129 |
| -// return super.getApplicationContext(); |
130 |
| -// } else { |
131 |
| -// return this; |
132 |
| -// } |
133 |
| -// } |
134 |
| -} |
| 119 | + return super.startService(service); |
| 120 | + } |
135 | 121 |
|
| 122 | + @Override |
| 123 | + public boolean stopService(Intent name) { |
| 124 | + LogUtil.d("stopService", name.toUri(0)); |
| 125 | + if (PluginDispatcher.hackClassLoadForServiceIfNeeded(name)) { |
| 126 | + name.setClass(PluginLoader.getApplicatoin(), PluginStubService.class); |
| 127 | + } |
| 128 | + return super.stopService(name); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void startActivity(Intent intent) { |
| 133 | + LogUtil.d("startActivity", intent.toUri(0)); |
| 134 | + PluginInstrumentionWrapper.resloveIntent(intent); |
| 135 | + super.startActivity(intent); |
| 136 | + } |
| 137 | + |
| 138 | + // @Override |
| 139 | + // public Context getApplicationContext() { |
| 140 | + // if (getBaseContext() instanceof Activity) { |
| 141 | + // return super.getApplicationContext(); |
| 142 | + // } else { |
| 143 | + // return this; |
| 144 | + // } |
| 145 | + // } |
| 146 | +} |
0 commit comments