From e41153daa0f2d48d2724bf4c5fb8748ea19cce20 Mon Sep 17 00:00:00 2001 From: shifujun Date: Thu, 7 Apr 2022 17:47:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E6=94=AF=E6=8C=81PackageManager#?= =?UTF-8?q?getProviderInfo=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由于更多测试插件的加入,比如androidx引入了新的Provider, queryContentProviders测试数据可能会变动,因此改为只判断是否大于0. --- .../core/loader/managers/ComponentManager.kt | 16 +++++++++++- .../managers/PluginPackageManagerImpl.kt | 21 +++++++++++++++- .../runtime/PackageManagerInvokeRedirect.java | 4 +++ .../core/runtime/PluginPackageManager.java | 2 ++ .../specific/PackageManagerTransform.kt | 2 +- .../cases/plugin_main/PackageManagerTest.java | 3 +-- .../general_cases/app/PackageManagerTest.java | 3 +-- .../TestPackageManagerActivity.java | 25 ++----------------- 8 files changed, 46 insertions(+), 30 deletions(-) diff --git a/projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/managers/ComponentManager.kt b/projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/managers/ComponentManager.kt index ea5f698b0..0b8a1e78d 100644 --- a/projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/managers/ComponentManager.kt +++ b/projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/managers/ComponentManager.kt @@ -306,7 +306,7 @@ abstract class ComponentManager : PluginComponentLauncher { fun getArchiveFilePathForService(className: String) = getArchiveFilePath(className, PluginManifest::getServices) - fun getArchiveFilePathForProvider(action: String?): kotlin.Pair { + fun getArchiveFilePathForProviderByAction(action: String?): kotlin.Pair { for ((pluginManifest, archiveFilePath) in allLoadedPlugin) { val providers = pluginManifest.providers if (providers != null) { @@ -320,6 +320,20 @@ abstract class ComponentManager : PluginComponentLauncher { return null to null } + fun getArchiveFilePathForProviderByClassName(className: String): kotlin.Pair { + for ((pluginManifest, archiveFilePath) in allLoadedPlugin) { + val providers = pluginManifest.providers + if (providers != null) { + for (provider in providers) { + if (className == provider.className) { + return provider.className to archiveFilePath + } + } + } + } + return null to null + } + fun getAllArchiveFilePaths() = allLoadedPlugin.map { it.second }.toList() private fun getArchiveFilePath( diff --git a/projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/managers/PluginPackageManagerImpl.kt b/projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/managers/PluginPackageManagerImpl.kt index 513153088..2f96ada18 100644 --- a/projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/managers/PluginPackageManagerImpl.kt +++ b/projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/managers/PluginPackageManagerImpl.kt @@ -72,6 +72,23 @@ internal class PluginPackageManagerImpl( PackageManager::getServiceInfo ) + override fun getProviderInfo(component: ComponentName, flags: Int): ProviderInfo { + val (className, archiveFilePath) + = componentManager.getArchiveFilePathForProviderByClassName(component.className) + if (archiveFilePath != null) { + val packageInfo = hostPackageManager.getPackageArchiveInfo( + archiveFilePath, PackageManager.GET_PROVIDERS or flags + ) + val componentInfo = packageInfo?.providers?.find { + it.name == className + } + if (componentInfo != null) { + return componentInfo + } + } + return hostPackageManager.getProviderInfo(component, flags) + } + override fun resolveActivity(intent: Intent, flags: Int): ResolveInfo? { val component = intent.component if (component != null) { @@ -127,7 +144,9 @@ internal class PluginPackageManagerImpl( } override fun resolveContentProvider(name: String, flags: Int): ProviderInfo? { - val (className, archiveFilePath) = componentManager.getArchiveFilePathForProvider(name) + val (className, archiveFilePath) = componentManager.getArchiveFilePathForProviderByAction( + name + ) if (archiveFilePath != null) { val packageInfo = hostPackageManager.getPackageArchiveInfo( archiveFilePath, PackageManager.GET_PROVIDERS or flags diff --git a/projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PackageManagerInvokeRedirect.java b/projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PackageManagerInvokeRedirect.java index d153bec22..208fb900c 100644 --- a/projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PackageManagerInvokeRedirect.java +++ b/projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PackageManagerInvokeRedirect.java @@ -56,6 +56,10 @@ public static ServiceInfo getServiceInfo(ClassLoader classLoaderOfInvokeCode, Co return getPluginPackageManager(classLoaderOfInvokeCode).getServiceInfo(component, flags); } + public static ProviderInfo getProviderInfo(ClassLoader classLoaderOfInvokeCode, ComponentName component, int flags) throws PackageManager.NameNotFoundException { + return getPluginPackageManager(classLoaderOfInvokeCode).getProviderInfo(component, flags); + } + public static PackageInfo getPackageInfo(ClassLoader classLoaderOfInvokeCode, String packageName, int flags) throws PackageManager.NameNotFoundException { return getPluginPackageManager(classLoaderOfInvokeCode).getPackageInfo(packageName, flags); } diff --git a/projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PluginPackageManager.java b/projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PluginPackageManager.java index 6abb8b7b0..1fbea4787 100644 --- a/projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PluginPackageManager.java +++ b/projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PluginPackageManager.java @@ -18,6 +18,8 @@ public interface PluginPackageManager { ServiceInfo getServiceInfo(ComponentName component, int flags); + ProviderInfo getProviderInfo(ComponentName component, int flags); + PackageInfo getPackageInfo(String packageName, int flags); ProviderInfo resolveContentProvider(String name, int flags); diff --git a/projects/sdk/core/transform/src/main/kotlin/com/tencent/shadow/core/transform/specific/PackageManagerTransform.kt b/projects/sdk/core/transform/src/main/kotlin/com/tencent/shadow/core/transform/specific/PackageManagerTransform.kt index 0a03f011d..1cb08201c 100644 --- a/projects/sdk/core/transform/src/main/kotlin/com/tencent/shadow/core/transform/specific/PackageManagerTransform.kt +++ b/projects/sdk/core/transform/src/main/kotlin/com/tencent/shadow/core/transform/specific/PackageManagerTransform.kt @@ -22,7 +22,6 @@ import com.tencent.shadow.core.transform.ShadowTransform.Companion.SelfClassName import com.tencent.shadow.core.transform_kit.SpecificTransform import com.tencent.shadow.core.transform_kit.TransformStep import javassist.* -import java.util.* class PackageManagerTransform : SpecificTransform() { companion object { @@ -102,6 +101,7 @@ class PackageManagerTransform : SpecificTransform() { "getApplicationInfo", "getActivityInfo", "getServiceInfo", + "getProviderInfo", "getPackageInfo", "resolveContentProvider", "queryContentProviders", diff --git a/projects/test/dynamic/host/test-dynamic-host/src/androidTest/java/com/tencent/shadow/test/cases/plugin_main/PackageManagerTest.java b/projects/test/dynamic/host/test-dynamic-host/src/androidTest/java/com/tencent/shadow/test/cases/plugin_main/PackageManagerTest.java index 284956e49..5b6b0c844 100644 --- a/projects/test/dynamic/host/test-dynamic-host/src/androidTest/java/com/tencent/shadow/test/cases/plugin_main/PackageManagerTest.java +++ b/projects/test/dynamic/host/test-dynamic-host/src/androidTest/java/com/tencent/shadow/test/cases/plugin_main/PackageManagerTest.java @@ -38,8 +38,7 @@ public void testGetApplicationInfoMetaData() { @Test public void testQueryContentProvidersName() { - matchTextWithViewTag("queryContentProviders/size", "2"); - matchTextWithViewTag("queryContentProviders/name", "[android.support.v4.content.FileProvider, com.tencent.shadow.test.plugin.general_cases.lib.usecases.provider.TestProvider]"); + matchTextWithViewTag("queryContentProviders/size", ">0"); } @Test diff --git a/projects/test/plugin/general-cases/test-plugin-general-cases/src/androidTest/java/com/tencent/shadow/test/plugin/general_cases/app/PackageManagerTest.java b/projects/test/plugin/general-cases/test-plugin-general-cases/src/androidTest/java/com/tencent/shadow/test/plugin/general_cases/app/PackageManagerTest.java index 8ad40fc34..34c40df5f 100644 --- a/projects/test/plugin/general-cases/test-plugin-general-cases/src/androidTest/java/com/tencent/shadow/test/plugin/general_cases/app/PackageManagerTest.java +++ b/projects/test/plugin/general-cases/test-plugin-general-cases/src/androidTest/java/com/tencent/shadow/test/plugin/general_cases/app/PackageManagerTest.java @@ -62,8 +62,7 @@ public void testGetPackageInfoVersionCode() { @Test public void testQueryContentProvidersName() { - matchTextWithViewTag("queryContentProviders/size", "2"); - matchTextWithViewTag("queryContentProviders/name", "[android.support.v4.content.FileProvider, com.tencent.shadow.test.plugin.general_cases.lib.usecases.provider.TestProvider]"); + matchTextWithViewTag("queryContentProviders/size", ">0"); } @Test diff --git a/projects/test/plugin/general-cases/test-plugin-general-cases/src/main/java/com/tencent/shadow/test/plugin/general_cases/lib/usecases/packagemanager/TestPackageManagerActivity.java b/projects/test/plugin/general-cases/test-plugin-general-cases/src/main/java/com/tencent/shadow/test/plugin/general_cases/lib/usecases/packagemanager/TestPackageManagerActivity.java index 20d32672e..ef715a4bf 100644 --- a/projects/test/plugin/general-cases/test-plugin-general-cases/src/main/java/com/tencent/shadow/test/plugin/general_cases/lib/usecases/packagemanager/TestPackageManagerActivity.java +++ b/projects/test/plugin/general-cases/test-plugin-general-cases/src/main/java/com/tencent/shadow/test/plugin/general_cases/lib/usecases/packagemanager/TestPackageManagerActivity.java @@ -36,9 +36,6 @@ import com.tencent.shadow.test.plugin.general_cases.lib.gallery.util.UiUtil; import com.tencent.shadow.test.plugin.general_cases.lib.usecases.service.TestService; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.List; public class TestPackageManagerActivity extends Activity { @@ -185,18 +182,8 @@ private void queryContentProviders(ViewGroup viewGroup) { int uid = applicationInfo.uid; List providerInfos = packageManager.queryContentProviders(processName, uid, PackageManager.MATCH_ALL); - String size = Integer.toString(providerInfos.size()); - String name; - if (providerInfos.isEmpty()) { - name = ""; - } else { - ArrayList names = new ArrayList<>(providerInfos.size()); - for (ProviderInfo providerInfo : providerInfos) { - names.add(providerInfo.name); - } - Collections.sort(names); - name = Arrays.toString(names.toArray()); - } + String size = providerInfos.size() > 0 ? ">0" : "0"; + viewGroup.addView( UiUtil.makeItem( this, @@ -205,14 +192,6 @@ private void queryContentProviders(ViewGroup viewGroup) { size ) ); - viewGroup.addView( - UiUtil.makeItem( - this, - "queryContentProviders/name", - "queryContentProviders/name", - name - ) - ); } private void resolveActivityByExplicitIntent(ViewGroup viewGroup) {