Skip to content

Commit

Permalink
feat(core): 支持PackageManager#getProviderInfo方法
Browse files Browse the repository at this point in the history
由于更多测试插件的加入,比如androidx引入了新的Provider,
queryContentProviders测试数据可能会变动,因此改为只判断是否大于0.
  • Loading branch information
shifujun committed Apr 7, 2022
1 parent 72ce18c commit e41153d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ abstract class ComponentManager : PluginComponentLauncher {
fun getArchiveFilePathForService(className: String) =
getArchiveFilePath(className, PluginManifest::getServices)

fun getArchiveFilePathForProvider(action: String?): kotlin.Pair<String?, String?> {
fun getArchiveFilePathForProviderByAction(action: String?): kotlin.Pair<String?, String?> {
for ((pluginManifest, archiveFilePath) in allLoadedPlugin) {
val providers = pluginManifest.providers
if (providers != null) {
Expand All @@ -320,6 +320,20 @@ abstract class ComponentManager : PluginComponentLauncher {
return null to null
}

fun getArchiveFilePathForProviderByClassName(className: String): kotlin.Pair<String?, String?> {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -102,6 +101,7 @@ class PackageManagerTransform : SpecificTransform() {
"getApplicationInfo",
"getActivityInfo",
"getServiceInfo",
"getProviderInfo",
"getPackageInfo",
"resolveContentProvider",
"queryContentProviders",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -185,18 +182,8 @@ private void queryContentProviders(ViewGroup viewGroup) {
int uid = applicationInfo.uid;
List<ProviderInfo> providerInfos = packageManager.queryContentProviders(processName, uid, PackageManager.MATCH_ALL);

String size = Integer.toString(providerInfos.size());
String name;
if (providerInfos.isEmpty()) {
name = "";
} else {
ArrayList<String> 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,
Expand All @@ -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) {
Expand Down

0 comments on commit e41153d

Please sign in to comment.