Skip to content

Commit 6e204b8

Browse files
committed
update core for 5.x and 2.x theme compat
Signed-off-by: Cai Liming <405977146@163.com>
1 parent eae4c75 commit 6e204b8

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.plugin.core;
2+
3+
import android.content.Context;
4+
import android.os.Build;
5+
6+
public class PluginCompat {
7+
8+
/**
9+
*
10+
* @param pluginContext
11+
* @param resId 宿主程序中定义的主题资源ID
12+
* @param clazz
13+
*/
14+
public static void applyHostTheme(Context pluginContext, int resId, @SuppressWarnings("rawtypes") Class clazz) {
15+
//使用主程序资源Id不需要区分版本
16+
pluginContext.setTheme(resId);
17+
}
18+
19+
/**
20+
*
21+
* @param pluginContext
22+
* @param resId 插件程序中定义的主题资源ID
23+
* @param clazz
24+
*/
25+
public static void applyPluginTheme(Context pluginContext, int resId, @SuppressWarnings("rawtypes") Class clazz) {
26+
//使用插件程序主题资源Id的时候需要区分版本
27+
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT <= 20) {
28+
pluginContext.setTheme(resId);
29+
} else {
30+
PluginDescriptor pd = PluginLoader.getPluginDescriptorByClassName(clazz.getName());
31+
((PluginContextTheme)pluginContext).mResources = PluginCreator.createPluginResourceFor5(PluginLoader.getApplicatoin(), pd.getInstalledPath());
32+
((PluginContextTheme)pluginContext).mTheme = null;
33+
pluginContext.setTheme(resId);
34+
}
35+
}
36+
37+
38+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
public class PluginContextTheme extends ContextWrapper {
1212
private int mThemeResource;
13-
private Resources.Theme mTheme;
13+
Resources.Theme mTheme;
1414
private LayoutInflater mInflater;
1515

16-
private Resources mResources;
16+
Resources mResources;
1717
private ClassLoader mClassLoader;
1818

1919
public PluginContextTheme(Context base, Resources resources, ClassLoader classLoader) {

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

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -221,32 +221,13 @@ public static Context getDefaultPluginContext(@SuppressWarnings("rawtypes") Clas
221221
* @return
222222
*/
223223
public static Context getNewPluginContext(@SuppressWarnings("rawtypes") Class clazz) {
224-
225224
Context pluginContext = getDefaultPluginContext(clazz);
226-
//为了使5.0支持主题, 这里做api版本区分
227-
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT <= 20 ) {
228-
229-
if (pluginContext != null) {
230-
pluginContext = PluginCreator.createPluginApplicationContext(sApplication,
231-
pluginContext.getResources(), (DexClassLoader)pluginContext.getClassLoader());
232-
pluginContext.setTheme(sApplication.getApplicationContext().getApplicationInfo().theme);
233-
}
234-
235-
return pluginContext;
236-
237-
} else {
238-
PluginDescriptor pd = getPluginDescriptorByClassName(clazz.getName());
239-
240-
Resources resFor5 = PluginCreator.createPluginResourceFor5(sApplication, pd.getInstalledPath());
241-
242-
if (pluginContext != null) {
243-
pluginContext = PluginCreator.createPluginApplicationContext(sApplication,
244-
resFor5, (DexClassLoader)pluginContext.getClassLoader());
245-
pluginContext.setTheme(sApplication.getApplicationContext().getApplicationInfo().theme);
246-
}
247-
return pluginContext;
225+
if (pluginContext != null) {
226+
pluginContext = PluginCreator.createPluginApplicationContext(sApplication,
227+
pluginContext.getResources(), (DexClassLoader)pluginContext.getClassLoader());
228+
pluginContext.setTheme(sApplication.getApplicationContext().getApplicationInfo().theme);
248229
}
249-
230+
return pluginContext;
250231
}
251232

252233
/**
@@ -358,7 +339,7 @@ public static PluginDescriptor getPluginDescriptorByPluginId(String pluginId) {
358339
return null;
359340
}
360341

361-
private static PluginDescriptor getPluginDescriptorByClassName(String clazzName) {
342+
public static PluginDescriptor getPluginDescriptorByClassName(String clazzName) {
362343
Iterator<PluginDescriptor> itr = sInstalledPlugins.values().iterator();
363344
while (itr.hasNext()) {
364345
PluginDescriptor descriptor = itr.next();

0 commit comments

Comments
 (0)