Skip to content

Commit ec269d3

Browse files
committed
隔离插件SharedPerference、DataBase、Application
1 parent e51bfcb commit ec269d3

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.plugin.core;
22

33
import android.content.Context;
4+
import android.content.SharedPreferences;
45
import android.content.res.AssetManager;
56
import android.content.res.Resources;
7+
import android.database.DatabaseErrorHandler;
8+
import android.database.sqlite.SQLiteDatabase;
69
import android.view.LayoutInflater;
710

811
import com.plugin.content.PluginDescriptor;
@@ -101,21 +104,61 @@ public String getPackageName() {
101104
//如果是独立插件 返回插件本身的packageName
102105
//但是只返回插件本身的packageName可能会引起其他问题
103106
//例如1、会导致toast无法弹出,原因是toast弹出时会检查packageName是否时当前用户的
104-
// 2、导致宿主的Application获取的sharepreference和插件Activity获取的shareperference不在同一个xml里面
105107
//if (mPluginDescriptor.isStandalone()) {
106108
// return mPluginDescriptor.getPackageName();
107109
//} else {
108110
return super.getPackageName();
109111
//}
110112
}
111113

114+
/**
115+
* 隔离插件间的SharedPreferences
116+
* @param name
117+
* @param mode
118+
* @return
119+
*/
120+
@Override
121+
public SharedPreferences getSharedPreferences(String name, int mode) {
122+
return super.getSharedPreferences(mPluginDescriptor.getPackageName() + "_" + name, mode);
123+
}
124+
125+
/**
126+
* 隔离插件间的Database
127+
* @param name
128+
* @param mode
129+
* @param factory
130+
* @return
131+
*/
132+
@Override
133+
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
134+
return super.openOrCreateDatabase(mPluginDescriptor.getPackageName() + "_" + name, mode, factory);
135+
}
136+
137+
/**
138+
* 隔离插件间的Database
139+
* @param name
140+
* @param mode
141+
* @param factory
142+
* @return
143+
*/
144+
@Override
145+
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
146+
return super.openOrCreateDatabase(mPluginDescriptor.getPackageName() + "_" + name, mode, factory, errorHandler);
147+
}
148+
149+
@Override
150+
public Context getApplicationContext() {
151+
return super.getApplicationContext();
152+
}
153+
112154
@Override
113155
public String getPackageCodePath() {
114-
//if (mPluginDescriptor.isStandalone()) {
115-
// return mPluginDescriptor.getInstalledPath();
116-
//} else {
117-
return super.getPackageCodePath();
118-
//}
156+
return mPluginDescriptor.getInstalledPath();
157+
}
158+
159+
@Override
160+
public String getPackageResourcePath() {
161+
return mPluginDescriptor.getInstalledPath();
119162
}
120163

121164
public PluginDescriptor getPluginDescriptor() {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ static void injectActivityContext(Activity activity) {
154154
} else if (!TextUtils.isEmpty(fragmentContainer.fragmentId())) {
155155
String classId = null;
156156
try {
157-
classId = activity.getIntent().getStringExtra(fragmentContainer.fragmentId());
157+
//TODO
158+
classId = intent.getStringExtra(fragmentContainer.fragmentId());
158159
} catch (Exception e) {
159160
LogUtil.printException("这里的Intent如果包含来自插件的VO对象实例," +
160161
"会产生ClassNotFound异常", e);
@@ -175,11 +176,18 @@ static void injectActivityContext(Activity activity) {
175176
}
176177

177178
} else {
178-
179179
//是打开插件中的activity
180180
pd = PluginLoader.getPluginDescriptorByClassName(activity.getClass().getName());
181181
pluginContext = PluginLoader.getNewPluginContext(activity.getClass());
182182

183+
//获取插件Application对象
184+
Application pluginApp = pd.getPluginApplication();
185+
if (pluginApp != null) {
186+
//重设mApplication
187+
RefInvoker.setFieldObject(activity, Activity.class.getName(),
188+
"mApplication", pluginApp);
189+
}
190+
183191
}
184192

185193
PluginActivityInfo pluginActivityInfo = pd.getActivityInfos().get(activity.getClass().getName());
144 Bytes
Binary file not shown.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.plugintest.activity;
22

33
import android.app.Activity;
4+
import android.content.Intent;
45
import android.os.Bundle;
56
import android.util.Log;
67
import android.view.LayoutInflater;
@@ -12,7 +13,10 @@
1213
import android.widget.Button;
1314
import android.widget.Toast;
1415

16+
import com.example.plugintest.PluginTestApplication;
1517
import com.example.plugintest.R;
18+
import com.example.plugintest.receiver.PluginTestReceiver;
19+
import com.example.plugintest.vo.ParamVO;
1620

1721
/**
1822
* 完整生命周期模式 不使用反射、也不使用代理,真真正证实现activity无需在Manifest中注册!
@@ -37,6 +41,17 @@ public void onCreate(Bundle savedInstanceState) {
3741
initViews();
3842

3943
setContentView(scrollview);
44+
45+
Toast.makeText(this, ""+ ((PluginTestApplication) getApplication()).getApplicationContext().toString(), Toast.LENGTH_LONG).show();
46+
47+
//测试动态注册的插件广播
48+
Intent intent = new Intent();
49+
intent.setClassName(this, PluginTestReceiver.class.getName());
50+
intent.putExtra("str1", "打开PluginTestReceiver——————");
51+
ParamVO pvo = new ParamVO();
52+
pvo.name = "打开PluginTestReceiver";
53+
intent.putExtra("paramvo", pvo);
54+
getApplication().sendBroadcast(intent);
4055
}
4156

4257
@Override

0 commit comments

Comments
 (0)