Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决宿主无法通过Action启动插件Service的问题 #473

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ public static final ComponentList queryPluginComponentList(String name) {
return sPluginManager.queryPluginComponentList(name);
}

/**
* 警告:低层接口
* 调用此接口会在当前进程加载全部插件(不加载代码和资源,只获取ComponentList)
* @return 插件的全部插件的ComponentList
*/
public static final List<ComponentList> queryPluginComponentListAll() {
return sPluginManager.queryPluginComponentListAll();
}

/**
* 警告:低层接口
* 调用此接口会在当前进程加载插件(不启动App)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ final boolean loadDex(ClassLoader parent, int load) {

// 缓存表: fileName -> PackageInfo
synchronized (Plugin.FILENAME_2_PACKAGE_INFO) {
Plugin.FILENAME_2_PACKAGE_INFO.put(mPath, new WeakReference<PackageInfo>(mPackageInfo));
Plugin.FILENAME_2_PACKAGE_INFO.put(mPath, mPackageInfo);
}
}

Expand All @@ -244,7 +244,7 @@ final boolean loadDex(ClassLoader parent, int load) {

// 缓存表:ComponentList
synchronized (Plugin.FILENAME_2_COMPONENT_LIST) {
Plugin.FILENAME_2_COMPONENT_LIST.put(mPath, new WeakReference<>(mComponents));
Plugin.FILENAME_2_COMPONENT_LIST.put(mPath, mComponents);
}

/* 只调整一次 */
Expand Down Expand Up @@ -289,7 +289,7 @@ final boolean loadDex(ClassLoader parent, int load) {

// 缓存表: Resources
synchronized (Plugin.FILENAME_2_RESOURCES) {
Plugin.FILENAME_2_RESOURCES.put(mPath, new WeakReference<>(mPkgResources));
Plugin.FILENAME_2_RESOURCES.put(mPath, mPkgResources);
}
}
if (load == Plugin.LOAD_RESOURCES) {
Expand Down Expand Up @@ -326,7 +326,7 @@ final boolean loadDex(ClassLoader parent, int load) {

// 缓存表:ClassLoader
synchronized (Plugin.FILENAME_2_DEX) {
Plugin.FILENAME_2_DEX.put(mPath, new WeakReference<>(mClassLoader));
Plugin.FILENAME_2_DEX.put(mPath, mClassLoader);
}
}
if (load == Plugin.LOAD_DEX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.qihoo360.replugin.helper.LogDebug.LOG;
import static com.qihoo360.replugin.helper.LogDebug.MAIN_TAG;
Expand Down Expand Up @@ -90,22 +91,22 @@ class Plugin {
/**
*
*/
static final HashMap<String, WeakReference<ClassLoader>> FILENAME_2_DEX = new HashMap<>();
static final HashMap<String, ClassLoader> FILENAME_2_DEX = new HashMap<>();

/**
*
*/
static final HashMap<String, WeakReference<Resources>> FILENAME_2_RESOURCES = new HashMap<>();
static final HashMap<String, Resources> FILENAME_2_RESOURCES = new HashMap<>();

/**
*
*/
static final HashMap<String, WeakReference<PackageInfo>> FILENAME_2_PACKAGE_INFO = new HashMap<>();
static final HashMap<String, PackageInfo> FILENAME_2_PACKAGE_INFO = new HashMap<>();

/**
*
*/
static final HashMap<String, WeakReference<ComponentList>> FILENAME_2_COMPONENT_LIST = new HashMap<>();
static final HashMap<String, ComponentList> FILENAME_2_COMPONENT_LIST = new HashMap<>();

/**
* 调试用
Expand Down Expand Up @@ -215,20 +216,15 @@ static final String queryCachedFilename(String name) {
return filename;
}

static final Map<String, String> queryCachedFilenames() {
return PLUGIN_NAME_2_FILENAME;
}

static final ClassLoader queryCachedClassLoader(String filename) {
ClassLoader dex = null;
if (!TextUtils.isEmpty(filename)) {
synchronized (FILENAME_2_DEX) {
WeakReference<ClassLoader> ref = FILENAME_2_DEX.get(filename);
if (ref != null) {
dex = ref.get();
if (dex == null) {
FILENAME_2_DEX.remove(filename);
}
if (LOG) {
LogDebug.d(PLUGIN_TAG, "cached Dex " + filename + " -> " + dex);
}
}
return FILENAME_2_DEX.get(filename);
}
}
return dex;
Expand All @@ -238,16 +234,7 @@ static final Resources queryCachedResources(String filename) {
Resources resources = null;
if (!TextUtils.isEmpty(filename)) {
synchronized (FILENAME_2_RESOURCES) {
WeakReference<Resources> ref = FILENAME_2_RESOURCES.get(filename);
if (ref != null) {
resources = ref.get();
if (resources == null) {
FILENAME_2_RESOURCES.remove(filename);
}
if (LOG) {
LogDebug.d(PLUGIN_TAG, "cached Resources " + filename + " -> " + resources);
}
}
return FILENAME_2_RESOURCES.get(filename);
}
}
return resources;
Expand All @@ -257,38 +244,31 @@ static final PackageInfo queryCachedPackageInfo(String filename) {
PackageInfo packageInfo = null;
if (!TextUtils.isEmpty(filename)) {
synchronized (FILENAME_2_PACKAGE_INFO) {
WeakReference<PackageInfo> ref = FILENAME_2_PACKAGE_INFO.get(filename);
if (ref != null) {
packageInfo = ref.get();
if (packageInfo == null) {
FILENAME_2_PACKAGE_INFO.remove(filename);
}
if (LOG) {
LogDebug.d(PLUGIN_TAG, "cached packageInfo " + filename + " -> " + packageInfo);
}
}
return FILENAME_2_PACKAGE_INFO.get(filename);
}
}
return packageInfo;
}

static final ComponentList queryCachedComponentList(String filename) {
ComponentList cl = null;
if (!TextUtils.isEmpty(filename)) {
synchronized (FILENAME_2_COMPONENT_LIST) {
WeakReference<ComponentList> ref = FILENAME_2_COMPONENT_LIST.get(filename);
if (ref != null) {
cl = ref.get();
if (cl == null) {
FILENAME_2_COMPONENT_LIST.remove(filename);
}
if (LOG) {
LogDebug.d(PLUGIN_TAG, "cached componentList " + filename + " -> " + cl);
}
}
return FILENAME_2_COMPONENT_LIST.get(filename);
}
} else {
return null;
}
}


static final List<ComponentList> queryCachedComponentListAll() {
List<ComponentList> result = new ArrayList<>();
synchronized (FILENAME_2_COMPONENT_LIST) {
for (String key : FILENAME_2_COMPONENT_LIST.keySet()) {
result.add(FILENAME_2_COMPONENT_LIST.get(key));
}
}
return cl;
return result;
}

static final void clearCachedPlugin(String filename) {
Expand All @@ -298,50 +278,22 @@ static final void clearCachedPlugin(String filename) {

ClassLoader dex = null;
synchronized (FILENAME_2_DEX) {
WeakReference<ClassLoader> ref = FILENAME_2_DEX.get(filename);
if (ref != null) {
dex = ref.get();
FILENAME_2_DEX.remove(filename);
if (LOG) {
LogDebug.d(PLUGIN_TAG, "clear Cached Dex " + filename + " -> " + dex);
}
}
}

Resources resources = null;
synchronized (FILENAME_2_RESOURCES) {
WeakReference<Resources> ref = FILENAME_2_RESOURCES.get(filename);
if (ref != null) {
resources = ref.get();
FILENAME_2_RESOURCES.remove(filename);
if (LOG) {
LogDebug.d(PLUGIN_TAG, "clear Cached Resources " + filename + " -> " + resources);
}
}
}

PackageInfo packageInfo = null;
synchronized (FILENAME_2_PACKAGE_INFO) {
WeakReference<PackageInfo> ref = FILENAME_2_PACKAGE_INFO.get(filename);
if (ref != null) {
packageInfo = ref.get();
FILENAME_2_PACKAGE_INFO.remove(filename);
if (LOG) {
LogDebug.d(PLUGIN_TAG, "clear Cached packageInfo " + filename + " -> " + packageInfo);
}
}
}

ComponentList cl = null;
synchronized (FILENAME_2_COMPONENT_LIST) {
WeakReference<ComponentList> ref = FILENAME_2_COMPONENT_LIST.get(filename);
if (ref != null) {
cl = ref.get();
FILENAME_2_COMPONENT_LIST.remove(filename);
if (LOG) {
LogDebug.d(PLUGIN_TAG, "clear Cached componentList " + filename + " -> " + cl);
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.qihoo360.replugin.helper.LogRelease;
import com.qihoo360.replugin.model.PluginInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -297,6 +298,34 @@ public ComponentList queryPluginComponentList(String name) {
return null;
}

/**
* 获取全部插件的ComponentList,并在其中查找Intent Action
* @return 全部插件的ComponentList
*/
public List<ComponentList> queryPluginComponentListAll() {
List<ComponentList> result;
// 先从缓存获取全部插件
result = Plugin.queryCachedComponentListAll();
if (result != null && !result.isEmpty()) {
return result;
}

result = new ArrayList<>();
List<Plugin> list = mPluginMgr.loadPackageInfoPluginList(this);
if (list != null && !list.isEmpty()) {
for (Plugin p : list) {
result.add(p.mLoader.mComponents);
}
return result;
}

if (LOG) {
LogDebug.d(PLUGIN_TAG, "not found plugin");
}

return null;
}

/**
* 警告:低层接口
* 调用此接口会在当前进程加载插件(不启动App)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ private final View handleCreateView(String name, Context context, AttributeSet a
// 找构造器
try {
construct = c.getConstructor(Context.class, AttributeSet.class);
construct.setAccessible(true);
if (LOG) {
LogDebug.d(PLUGIN_TAG, "layout.cache: new constructor. plugin=" + mPlugin + " name=" + name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -1064,6 +1065,22 @@ final Plugin loadPackageInfoPlugin(String plugin, PluginCommImpl pm) {
return loadPlugin(p, Plugin.LOAD_INFO, true);
}

/**
* 获取全部插件信息
* @param pm 宿主与插件、插件间的互通模块
* @return 插件信息列表
*/
final List<Plugin> loadPackageInfoPluginList(PluginCommImpl pm) {
List<Plugin> list = new ArrayList<>();
Plugin p, result;
for (String key : mPlugins.keySet()) {
p = Plugin.cloneAndReattach(mContext, mPlugins.get(key), mClassLoader, pm);
result = loadPlugin(p, Plugin.LOAD_INFO, true);
list.add(result);
}
return list;
}

final Plugin loadResourcePlugin(String plugin, PluginCommImpl pm) {
Plugin p = Plugin.cloneAndReattach(mContext, mPlugins.get(plugin), mClassLoader, pm);
return loadPlugin(p, Plugin.LOAD_RESOURCES, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import com.qihoo360.replugin.helper.LogDebug;
import com.qihoo360.replugin.helper.LogRelease;

import java.util.List;

import static com.qihoo360.replugin.helper.LogDebug.LOG;
import static com.qihoo360.replugin.helper.LogDebug.PLUGIN_TAG;
import static com.qihoo360.replugin.helper.LogRelease.LOGR;
Expand Down Expand Up @@ -335,12 +337,20 @@ private static ComponentName getServiceComponentFromIntent(Context context, Inte
} else {
/* Intent 中已指定 Action,根据 action 解析出 ServiceInfo */
if (!TextUtils.isEmpty(intent.getAction())) {
ComponentList componentList = Factory.queryPluginComponentList(plugin);
if (componentList != null) {
// 返回 ServiceInfo 和 Service 所在的插件
Pair<ServiceInfo, String> pair = componentList.getServiceAndPluginByIntent(context, intent);
if (pair != null) {
return new ComponentName(pair.second, pair.first.name);
ComponentList currentComponentList;
Pair<ServiceInfo, String> pair;
// 返回 全部插件的ComponentList
List<ComponentList> componentListAll = Factory.queryPluginComponentListAll();
if (componentListAll != null && !componentListAll.isEmpty()) {
for (int index = 0; index < componentListAll.size(); index++) {
currentComponentList = componentListAll.get(index);
if (currentComponentList != null) {
pair = currentComponentList.getServiceAndPluginByIntent(context, intent);
if (pair != null) {
// 返回 ServiceInfo 和 Service 所在的插件
return new ComponentName(pair.second, pair.first.name);
}
}
}
}
} else {
Expand All @@ -351,5 +361,4 @@ private static ComponentName getServiceComponentFromIntent(Context context, Inte
}
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.widget.Toast;

import com.qihoo360.replugin.RePlugin;
import com.qihoo360.replugin.component.service.PluginServiceClient;
import com.qihoo360.replugin.model.PluginInfo;
import com.qihoo360.replugin.utils.FileUtils;

Expand Down Expand Up @@ -62,6 +63,16 @@ public void onClick(View v) {
}
});

findViewById(R.id.btn_start_plugin_from_action).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setPackage("com.qihoo360.replugin.sample.demo1");
intent.setAction("com.qihoo360.replugin.sample.demo1.action.XXXX");
PluginServiceClient.startService(MainActivity.this, intent);
}
});

findViewById(R.id.btn_load_fragment_from_demo1).setOnClickListener(new View.OnClickListener() {

@Override
Expand Down
Loading