Skip to content

Commit

Permalink
feat(core): 支持queryContentProviders
Browse files Browse the repository at this point in the history
插件的applicationInfo的processName和uid都从宿主复制一下填充。

支持插件内调用支持queryContentProviders方法。
processName传入null时返回正常查询结果加上所有插件的Provider的集合。

#464
  • Loading branch information
shifujun committed Jan 13, 2021
1 parent f7b8dfa commit 807ecd5
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ object LoadPluginBloc {

packageArchiveInfo.applicationInfo.nativeLibraryDir = installedApk.libraryPath
packageArchiveInfo.applicationInfo.dataDir = dataDir.absolutePath
packageArchiveInfo.applicationInfo.processName = hostAppContext.applicationInfo.processName
packageArchiveInfo.applicationInfo.uid = hostAppContext.applicationInfo.uid

lock.withLock { pluginPackageInfoSet.add(packageArchiveInfo) }
packageArchiveInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ internal class PluginPackageManagerImpl(private val hostPackageManager: PackageM

return hostPackageManager.resolveContentProvider(name, flags)
}

override fun queryContentProviders(processName: String?, uid: Int, flags: Int) =
if (processName == null) {
val allNormalProviders = hostPackageManager.queryContentProviders(null, 0, flags)
val allPluginProviders = allPluginPackageInfo()
.flatMap { it.providers.asIterable() }
listOf(allNormalProviders, allPluginProviders).flatten()
} else {
allPluginPackageInfo().filter {
it.applicationInfo.processName == processName
&& it.applicationInfo.uid == uid
}.flatMap { it.providers.asIterable() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import android.content.pm.VersionedPackage;
import android.os.Build;

import java.util.List;

/**
* PackageManagerTransform必须把对PackageManager的调用转到到这个处于Runtime层不被Transform作用的类上来,
* 而不能把这个类实现的方法直接写在Transform生成的方法体中,是为了避免这个类实现的代码再次被Transform,
Expand All @@ -53,13 +55,16 @@ public static PackageInfo getPackageInfo(ClassLoader classLoaderOfInvokeCode, St

@TargetApi(Build.VERSION_CODES.O)
public static PackageInfo getPackageInfo(ClassLoader classLoaderOfInvokeCode, VersionedPackage versionedPackage,
int flags) throws PackageManager.NameNotFoundException{
int flags) throws PackageManager.NameNotFoundException {
return getPluginPackageManager(classLoaderOfInvokeCode).getPackageInfo(versionedPackage.getPackageName(), flags);
}

public static ProviderInfo resolveContentProvider(ClassLoader classLoaderOfInvokeCode, String name, int flags) {
return getPluginPackageManager(classLoaderOfInvokeCode).resolveContentProvider(name, flags);
}

public static List<ProviderInfo> queryContentProviders(ClassLoader classLoaderOfInvokeCode, String processName, int uid, int flags) {
return getPluginPackageManager(classLoaderOfInvokeCode).queryContentProviders(processName, uid, flags);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.content.pm.PackageInfo;
import android.content.pm.ProviderInfo;

import java.util.List;

public interface PluginPackageManager {
ApplicationInfo getApplicationInfo(String packageName, int flags);

Expand All @@ -14,4 +16,6 @@ public interface PluginPackageManager {
PackageInfo getPackageInfo(String packageName, int flags);

ProviderInfo resolveContentProvider(String name, int flags);

List<ProviderInfo> queryContentProviders(String processName, int uid, int flags);
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class PackageManagerTransform : SpecificTransform() {
"getApplicationInfo",
"getActivityInfo",
"getPackageInfo",
"resolveContentProvider"
"resolveContentProvider",
"queryContentProviders",
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public void testGetApplicationInfoMetaData() {
matchTextWithViewTag("getApplicationInfo/metaData", "test_value");
}

@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]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ public void testGetPackageInfoVersionName() {
public void testGetPackageInfoVersionCode() {
matchTextWithViewTag("getPackageInfo/versionCode", "1");
}

@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]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.os.Bundle;
import android.view.ViewGroup;

import com.tencent.shadow.test.plugin.general_cases.lib.gallery.util.UiUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static android.content.pm.PackageManager.GET_META_DATA;

public class TestPackageManagerActivity extends Activity {
Expand All @@ -41,6 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
getApplicationInfo(viewGroup);
getActivityInfo(viewGroup);
getPackageInfo(viewGroup);
queryContentProviders(viewGroup);
}

private void getApplicationInfo(ViewGroup viewGroup) {
Expand Down Expand Up @@ -137,4 +144,41 @@ private void getPackageInfo(ViewGroup viewGroup) {
)
);
}

private void queryContentProviders(ViewGroup viewGroup) {
PackageManager packageManager = getPackageManager();
ApplicationInfo applicationInfo = getApplicationInfo();
String processName = applicationInfo.processName;
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());
}
viewGroup.addView(
UiUtil.makeItem(
this,
"queryContentProviders/size",
"queryContentProviders/size",
size
)
);
viewGroup.addView(
UiUtil.makeItem(
this,
"queryContentProviders/name",
"queryContentProviders/name",
name
)
);
}
}

0 comments on commit 807ecd5

Please sign in to comment.