diff --git a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/i/Factory.java b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/i/Factory.java index 60820efb..05a1299e 100644 --- a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/i/Factory.java +++ b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/i/Factory.java @@ -226,6 +226,15 @@ public static final ComponentList queryPluginComponentList(String name) { return sPluginManager.queryPluginComponentList(name); } + /** + * 警告:低层接口 + * 调用此接口会在当前进程加载全部插件(不加载代码和资源,只获取ComponentList) + * @return 插件的全部插件的ComponentList + */ + public static final List queryPluginComponentListAll() { + return sPluginManager.queryPluginComponentListAll(); + } + /** * 警告:低层接口 * 调用此接口会在当前进程加载插件(不启动App) diff --git a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/Loader.java b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/Loader.java index 622d4a3e..a6657e3c 100644 --- a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/Loader.java +++ b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/Loader.java @@ -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(mPackageInfo)); + Plugin.FILENAME_2_PACKAGE_INFO.put(mPath, mPackageInfo); } } @@ -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); } /* 只调整一次 */ @@ -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) { @@ -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) { diff --git a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/Plugin.java b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/Plugin.java index 15a1ab0a..c9594327 100644 --- a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/Plugin.java +++ b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/Plugin.java @@ -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; @@ -90,22 +91,22 @@ class Plugin { /** * */ - static final HashMap> FILENAME_2_DEX = new HashMap<>(); + static final HashMap FILENAME_2_DEX = new HashMap<>(); /** * */ - static final HashMap> FILENAME_2_RESOURCES = new HashMap<>(); + static final HashMap FILENAME_2_RESOURCES = new HashMap<>(); /** * */ - static final HashMap> FILENAME_2_PACKAGE_INFO = new HashMap<>(); + static final HashMap FILENAME_2_PACKAGE_INFO = new HashMap<>(); /** * */ - static final HashMap> FILENAME_2_COMPONENT_LIST = new HashMap<>(); + static final HashMap FILENAME_2_COMPONENT_LIST = new HashMap<>(); /** * 调试用 @@ -215,20 +216,15 @@ static final String queryCachedFilename(String name) { return filename; } + static final Map queryCachedFilenames() { + return PLUGIN_NAME_2_FILENAME; + } + static final ClassLoader queryCachedClassLoader(String filename) { ClassLoader dex = null; if (!TextUtils.isEmpty(filename)) { synchronized (FILENAME_2_DEX) { - WeakReference 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; @@ -238,16 +234,7 @@ static final Resources queryCachedResources(String filename) { Resources resources = null; if (!TextUtils.isEmpty(filename)) { synchronized (FILENAME_2_RESOURCES) { - WeakReference 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; @@ -257,38 +244,31 @@ static final PackageInfo queryCachedPackageInfo(String filename) { PackageInfo packageInfo = null; if (!TextUtils.isEmpty(filename)) { synchronized (FILENAME_2_PACKAGE_INFO) { - WeakReference 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 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 queryCachedComponentListAll() { + List 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) { @@ -298,50 +278,22 @@ static final void clearCachedPlugin(String filename) { ClassLoader dex = null; synchronized (FILENAME_2_DEX) { - WeakReference 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 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 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 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); - } - } } } diff --git a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PluginCommImpl.java b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PluginCommImpl.java index 42e6c014..82ed5ca8 100644 --- a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PluginCommImpl.java +++ b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PluginCommImpl.java @@ -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; @@ -297,6 +298,34 @@ public ComponentList queryPluginComponentList(String name) { return null; } + /** + * 获取全部插件的ComponentList,并在其中查找Intent Action + * @return 全部插件的ComponentList + */ + public List queryPluginComponentListAll() { + List result; + // 先从缓存获取全部插件 + result = Plugin.queryCachedComponentListAll(); + if (result != null && !result.isEmpty()) { + return result; + } + + result = new ArrayList<>(); + List 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) diff --git a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PluginContext.java b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PluginContext.java index dd7fee28..2be8f7bd 100644 --- a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PluginContext.java +++ b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PluginContext.java @@ -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); } diff --git a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PmBase.java b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PmBase.java index d5041940..8dbaf869 100644 --- a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PmBase.java +++ b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/loader2/PmBase.java @@ -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; @@ -1064,6 +1065,22 @@ final Plugin loadPackageInfoPlugin(String plugin, PluginCommImpl pm) { return loadPlugin(p, Plugin.LOAD_INFO, true); } + /** + * 获取全部插件信息 + * @param pm 宿主与插件、插件间的互通模块 + * @return 插件信息列表 + */ + final List loadPackageInfoPluginList(PluginCommImpl pm) { + List 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); diff --git a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/replugin/component/service/PluginServiceClient.java b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/replugin/component/service/PluginServiceClient.java index 668a18f0..2f7638ef 100644 --- a/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/replugin/component/service/PluginServiceClient.java +++ b/replugin-host-library/replugin-host-lib/src/main/java/com/qihoo360/replugin/component/service/PluginServiceClient.java @@ -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; @@ -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 pair = componentList.getServiceAndPluginByIntent(context, intent); - if (pair != null) { - return new ComponentName(pair.second, pair.first.name); + ComponentList currentComponentList; + Pair pair; + // 返回 全部插件的ComponentList + List 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 { @@ -351,5 +361,4 @@ private static ComponentName getServiceComponentFromIntent(Context context, Inte } return null; } -} - +} \ No newline at end of file diff --git a/replugin-sample/host/app/src/main/java/com/qihoo360/replugin/sample/host/MainActivity.java b/replugin-sample/host/app/src/main/java/com/qihoo360/replugin/sample/host/MainActivity.java index 3fc18d1c..0d210d5e 100644 --- a/replugin-sample/host/app/src/main/java/com/qihoo360/replugin/sample/host/MainActivity.java +++ b/replugin-sample/host/app/src/main/java/com/qihoo360/replugin/sample/host/MainActivity.java @@ -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; @@ -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 diff --git a/replugin-sample/host/app/src/main/res/layout/activity_main.xml b/replugin-sample/host/app/src/main/res/layout/activity_main.xml index 9338a5aa..64b8dfa9 100644 --- a/replugin-sample/host/app/src/main/res/layout/activity_main.xml +++ b/replugin-sample/host/app/src/main/res/layout/activity_main.xml @@ -35,6 +35,13 @@ android:layout_gravity="center" android:text="@string/startDemo1ForResult" /> +