Skip to content

Commit 04fbd53

Browse files
committed
增强对插件主题的支持。默认使用插件Manifest文件中Application节点指定的主题;更新测试demo
1 parent 1ed362e commit 04fbd53

File tree

9 files changed

+58
-34
lines changed

9 files changed

+58
-34
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.plugin.content.PluginDescriptor;
44

55
import android.content.Context;
6-
import android.os.Build;
76

87
public class PluginCompat {
98

@@ -20,7 +19,12 @@ public class PluginCompat {
2019
* @param clazz
2120
*/
2221
public static void setTheme(Context pluginContext, int resId, @SuppressWarnings("rawtypes") Class clazz) {
23-
22+
setTheme(pluginContext, resId, PluginLoader.getPluginDescriptorByClassName(clazz.getName()));
23+
}
24+
25+
public static void setTheme(Context pluginContext, int resId, PluginDescriptor pd) {
26+
27+
/**
2428
boolean isThemeInHostResouce = false;
2529
try {
2630
//如果使用public.xml,采用下面的判断方式
@@ -36,7 +40,7 @@ public static void setTheme(Context pluginContext, int resId, @SuppressWarnings(
3640
}
3741
} catch (Exception e) {
3842
}
39-
43+
4044
if (isThemeInHostResouce) {
4145
//使用主程序资源Id不需要区分版本
4246
pluginContext.setTheme(resId);
@@ -45,12 +49,14 @@ public static void setTheme(Context pluginContext, int resId, @SuppressWarnings(
4549
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT <= 20) {
4650
pluginContext.setTheme(resId);
4751
} else {
48-
PluginDescriptor pd = PluginLoader.getPluginDescriptorByClassName(clazz.getName());
4952
((PluginContextTheme)pluginContext).mResources = PluginCreator.createPluginResourceFor5(PluginLoader.getApplicatoin(), pd.getInstalledPath());
5053
((PluginContextTheme)pluginContext).mTheme = null;
5154
pluginContext.setTheme(resId);
5255
}
53-
}
56+
}*/
57+
58+
((PluginContextTheme)pluginContext).mTheme = null;
59+
pluginContext.setTheme(resId);
5460
}
5561

5662
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ private static String[] buildAssetPath(boolean isStandalone, String app, String
101101

102102
}
103103

104+
/**
105+
* 未使用
106+
*/
104107
/* package */static Resources createPluginResourceFor5(Application application, String absolutePluginApkPath) {
105108
try {
106109
AssetManager assetMgr = AssetManager.class.newInstance();

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,7 @@ static void injectActivityContext(Activity activity) {
163163
RefInvoker.invokeMethod(activity, ContextThemeWrapper.class.getName(), android_content_ContextThemeWrapper_attachBaseContext,
164164
new Class[]{Context.class }, new Object[] { pluginContext });
165165

166-
// 重设LayoutInflater
167-
LogUtil.d(activity.getWindow().getClass().getName());
168-
RefInvoker.setFieldObject(activity.getWindow(), activity.getWindow().getClass().getName(),
169-
"mLayoutInflater", LayoutInflater.from(pluginContext));
170-
171-
// 如果api>=11,还要重设factory2
172-
if (Build.VERSION.SDK_INT >= 11) {
173-
RefInvoker.invokeMethod(activity.getWindow().getLayoutInflater(), LayoutInflater.class.getName(),
174-
"setPrivateFactory", new Class[] { LayoutInflater.Factory2.class }, new Object[] { activity });
175-
}
176-
177-
// 由于在attach的时候Resource已经被初始化了,所以还需要重置Resource
166+
// 由于在attach的时候Resource已经被初始化了,所以需要重置Resource
178167
RefInvoker.setFieldObject(activity, ContextThemeWrapper.class.getName(), android_content_ContextThemeWrapper_mResources, null);
179168

180169
// 重设theme
@@ -189,6 +178,21 @@ static void injectActivityContext(Activity activity) {
189178
activity.setTheme(pluginAppTheme);
190179
}
191180

181+
// 重设theme
182+
((PluginContextTheme)pluginContext).mTheme = null;
183+
pluginContext.setTheme(pluginAppTheme);
184+
185+
// 重设LayoutInflater
186+
LogUtil.d(activity.getWindow().getClass().getName());
187+
RefInvoker.setFieldObject(activity.getWindow(), activity.getWindow().getClass().getName(),
188+
"mLayoutInflater", LayoutInflater.from(pluginContext));
189+
190+
// 如果api>=11,还要重设factory2
191+
if (Build.VERSION.SDK_INT >= 11) {
192+
RefInvoker.invokeMethod(activity.getWindow().getLayoutInflater(), LayoutInflater.class.getName(),
193+
"setPrivateFactory", new Class[]{LayoutInflater.Factory2.class}, new Object[]{activity});
194+
}
195+
192196
//如果是独立插件,由于没有合并资源,这里还需要替换掉 mActivityInfo, 避免activity试图通过ActivityInfo中的资源id来读取资源时失败
193197
activityInfo.icon = pd.getApplicationIcon();
194198
activityInfo.logo = pd.getApplicationLogo();
209 Bytes
Binary file not shown.

PluginTest/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
<application
1515
android:name="com.example.plugintest.PluginTestApplication"
16-
android:label="包含UI的插件demo">
16+
android:theme="@style/PluginTheme2">
1717

1818
<!-- for fragment -->
1919
<meta-data android:name="fragment_id_fragmentTest1" android:value="com.example.plugintest.fragment.PluginSpecFragment"/>
2020
<!-- for fragment -->
2121
<meta-data android:name="fragment_id_fragmentTest2" android:value="com.example.plugintest.fragment.PluginNormalFragment"/>
22-
22+
23+
<meta-data android:name="test_meta_1" android:value="123"/>
24+
2325
<activity
2426
android:name="com.example.plugintest.activity.PluginTestActivity" >
2527
<intent-filter>

PluginTest/res/values/styles.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
<item name="android:gravity">center_vertical|center_horizontal</item>
55
<item name="android:textColor">@android:color/black</item>
66
</style>
7+
8+
<style name="MyButton2">
9+
<item name="android:background">#00ffA3</item>
10+
<item name="android:gravity">center_vertical|center_horizontal</item>
11+
<item name="android:textColor">#e3a438</item>
12+
</style>
13+
714
<!--
815
Base application theme, dependent on API level. This theme is replaced
916
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
@@ -22,4 +29,9 @@
2229
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
2330
</style>
2431

32+
<!-- Application theme. -->
33+
<style name="PluginTheme2" parent="AppBaseTheme">
34+
<item name="android:buttonStyle">@style/MyButton2</item>
35+
</style>
36+
2537
</resources>

PluginTest/src/com/example/plugintest/activity/PluginForOppoAndVivoActivity.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,23 @@
1414

1515
import com.example.plugintest.R;
1616

17-
/**
18-
* 完整生命周期模式 不使用反射、也不使用代理,真真正证实现activity无需在Manifest中注册!
19-
*
20-
* @author cailiming
21-
*
22-
*/
2317
public class PluginForOppoAndVivoActivity extends Activity implements OnClickListener {
2418

2519
private ViewGroup mRoot;
20+
2621
private LayoutInflater mInflater;
2722

2823
@Override
2924
public void onCreate(Bundle savedInstanceState) {
3025
super.onCreate(savedInstanceState);
31-
setTitle("测试插件中拥有真正生命周期的Activity");
32-
mInflater = getLayoutInflater();
33-
View scrollview = mInflater.inflate(R.layout.plugin_layout, null);
26+
setTitle("测试插件主题");
3427

35-
mRoot = (ViewGroup) scrollview.findViewById(R.id.content);
28+
setContentView(R.layout.plugin_layout);
3629

3730
initViews();
3831

39-
setContentView(scrollview);
32+
mInflater = getLayoutInflater();
33+
4034
}
4135

4236
@Override
@@ -53,6 +47,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
5347
}
5448

5549
public void initViews() {
50+
mRoot = (ViewGroup) findViewById(R.id.content);
5651

5752
Button btn1 = (Button) mRoot.findViewById(R.id.plugin_test_btn1);
5853
btn1.setOnClickListener(this);

PluginTest/src/com/example/plugintest/fragment/PluginNormalFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import android.widget.Toast;
1414

1515
/**
16-
* 这个fragment会被嵌在 插件 提供的Activity中展示
16+
* 此fragment没有使用特定的context,因此只能在插件中的activity,或者宿主中的特定activity中展示
1717
*/
1818
public class PluginNormalFragment extends Fragment implements OnClickListener {
1919

@@ -28,7 +28,7 @@ public void onCreate(Bundle savedInstanceState) {
2828
@Override
2929
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
3030

31-
getActivity().setTitle("测试插件中非自由模式的Fragment");
31+
getActivity().setTitle("测试插件中的Fragment,使用插件默认主题");
3232

3333
mInflater = inflater;
3434
View scrollview = mInflater.inflate(R.layout.plugin_layout, null);

PluginTest/src/com/example/plugintest/fragment/PluginSpecFragment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import com.plugin.core.PluginLoader;
1717

1818
/**
19-
* 这个fragment会被嵌在 宿主程序 提供的activity中展示
19+
* 此fragment使用了特定的context,因此可以在在插件中的activity,或者宿主中的特定activity、
20+
* 或者宿主中的非特定activity中展示
2021
*/
22+
2123
public class PluginSpecFragment extends Fragment implements OnClickListener {
2224

2325
private ViewGroup mRoot;
@@ -28,7 +30,7 @@ public class PluginSpecFragment extends Fragment implements OnClickListener {
2830
public void onCreate(Bundle savedInstanceState) {
2931
super.onCreate(savedInstanceState);
3032

31-
getActivity().setTitle("测试插件自由模式的Fragment");
33+
getActivity().setTitle("测试插件中的Fragment,使用插件中指定的PluginTheme主题");
3234

3335
// 默认是宿主程序Application主题
3436
pluginContext = PluginLoader.getNewPluginContext(PluginSpecFragment.class);

0 commit comments

Comments
 (0)