Skip to content

Commit

Permalink
feat(core): 支持新增API33新增的getPackageInfo方法
Browse files Browse the repository at this point in the history
androidx.profileinstaller.ProfileVerifier中有使用。

#1277
  • Loading branch information
aprz512 authored and shifujun committed Nov 23, 2024
1 parent 7650e36 commit 000ae0c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 5 deletions.
2 changes: 1 addition & 1 deletion buildScripts/gradle/versions.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
COMPILE_SDK_VERSION=31
COMPILE_SDK_VERSION=33
MIN_SDK_VERSION=14
TARGET_SDK_VERSION=28
VERSION_CODE=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,18 @@ internal class PluginPackageManagerImpl(
hostPackageManager.getApplicationInfo(packageName, flags)
}

override fun getPackageInfo(packageName: String, flags: Int): PackageInfo? {
val hostPackageInfo = hostPackageManager.getPackageInfo(packageName, flags)
/**
* 所有插件中的各种方法签名的getPackageInfo方法汇总到这里。
* 如果包名是插件的,优先返回插件的PackageInfo。
* 否则返回从宿主(系统)查询到的PackageInfo。
* 直接由getPackageArchiveInfo构造的PackageInfo和从getPackageInfo得到的正常的PackageInfo不完全一致。
* 在这里修改它使其尽可能像系统返回的。
*/
private fun getPluginPackageInfoIfPossible(
packageName: String,
flags: Int,
hostPackageInfo: PackageInfo,
): PackageInfo? {
return if (packageName.isPlugin()) {
val packageInfo = hostPackageManager.getPackageArchiveInfo(pluginArchiveFilePath, flags)
if (packageInfo != null) {
Expand All @@ -53,6 +63,42 @@ internal class PluginPackageManagerImpl(
}
}

@Suppress("DEPRECATION")
override fun getPackageInfo(packageName: String, flags: Int) =
getPluginPackageInfoIfPossible(
packageName,
flags,
hostPackageManager.getPackageInfo(packageName, flags)
)

@Suppress("DEPRECATION")
@SuppressLint("NewApi")
override fun getPackageInfo(versionedPackage: VersionedPackage, flags: Int) =
getPluginPackageInfoIfPossible(
versionedPackage.packageName,
flags,
hostPackageManager.getPackageInfo(versionedPackage, flags)
)

@SuppressLint("NewApi")
override fun getPackageInfo(packageName: String, flags: PackageManager.PackageInfoFlags) =
getPluginPackageInfoIfPossible(
packageName,
flags.value.toInt(),//FIXME 这里会丢失flags升级到Long新增的标志位
hostPackageManager.getPackageInfo(packageName, flags)
)

@SuppressLint("NewApi")
override fun getPackageInfo(
versionedPackage: VersionedPackage,
flags: PackageManager.PackageInfoFlags
) =
getPluginPackageInfoIfPossible(
versionedPackage.packageName,
flags.value.toInt(),//FIXME 这里会丢失flags升级到Long新增的标志位
hostPackageManager.getPackageInfo(versionedPackage, flags)
)

override fun getActivityInfo(component: ComponentName, flags: Int): ActivityInfo? =
getComponentInfo(
component,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,19 @@ 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 {
return getPluginPackageManager(classLoaderOfInvokeCode).getPackageInfo(versionedPackage.getPackageName(), flags);
return getPluginPackageManager(classLoaderOfInvokeCode).getPackageInfo(versionedPackage, flags);
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
public static PackageInfo getPackageInfo(ClassLoader classLoaderOfInvokeCode, String packageName,
PackageManager.PackageInfoFlags flags) throws PackageManager.NameNotFoundException {
return getPluginPackageManager(classLoaderOfInvokeCode).getPackageInfo(packageName, flags);
}

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

public static ProviderInfo resolveContentProvider(ClassLoader classLoaderOfInvokeCode, String name, int flags) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.tencent.shadow.core.runtime;

import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.VersionedPackage;
import android.os.Build;

import java.util.List;

Expand All @@ -22,6 +26,14 @@ public interface PluginPackageManager {

PackageInfo getPackageInfo(String packageName, int flags);

PackageInfo getPackageInfo(VersionedPackage versionedPackage, int flags);

@TargetApi(Build.VERSION_CODES.TIRAMISU)
PackageInfo getPackageInfo(VersionedPackage versionedPackage, PackageManager.PackageInfoFlags flags);

@TargetApi(Build.VERSION_CODES.TIRAMISU)
PackageInfo getPackageInfo(String packageName, PackageManager.PackageInfoFlags flags);

ProviderInfo resolveContentProvider(String name, int flags);

List<ProviderInfo> queryContentProviders(String processName, int uid, int flags);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.tencent.shadow.test.cases.plugin_main;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import androidx.test.core.app.ApplicationProvider;

Expand Down Expand Up @@ -55,4 +58,21 @@ public void testGetServiceInfoName() {
public void testGetServiceInfoPackageName() {
matchTextWithViewTag("getServiceInfo/packageName", "com.tencent.shadow.test.hostapp");
}

@Test
public void testGetPackageInfoVersionName() throws PackageManager.NameNotFoundException {
Context applicationContext = ApplicationProvider.getApplicationContext();
String packageName = applicationContext.getPackageName();
PackageInfo packageInfo = applicationContext.getPackageManager().getPackageInfo(packageName, 0);
matchTextWithViewTag("getPackageInfo/versionName", packageInfo.versionName);
}

@Test
public void testGetPackageInfoVersionCode() throws PackageManager.NameNotFoundException {
Context applicationContext = ApplicationProvider.getApplicationContext();
String packageName = applicationContext.getPackageName();
PackageInfo packageInfo = applicationContext.getPackageManager().getPackageInfo(packageName, 0);
matchTextWithViewTag("getPackageInfo/versionCode", Integer.toString(packageInfo.versionCode));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.VersionedPackage;
import android.os.Build;
import android.os.Bundle;
import android.view.ViewGroup;

Expand Down Expand Up @@ -151,7 +153,21 @@ private void getPackageInfo(ViewGroup viewGroup) {
String versionName;
String versionCode;
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
PackageManager packageManager = getPackageManager();
String packageName = getPackageName();
PackageInfo packageInfo;

//'getPackageInfo(java.lang.String, int)' is deprecated as of API 33 ("Tiramisu"; Android 13.0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageInfo = packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0));

//再测试调用一下传VersionedPackage的方法
VersionedPackage versionedPackage = new VersionedPackage(packageInfo.packageName, packageInfo.getLongVersionCode());
packageInfo = packageManager.getPackageInfo(versionedPackage, PackageManager.PackageInfoFlags.of(0));
} else {
packageInfo = packageManager.getPackageInfo(packageName, 0);
}

versionName = packageInfo.versionName;
versionCode = Integer.toString(packageInfo.versionCode);
} catch (PackageManager.NameNotFoundException e) {
Expand Down

0 comments on commit 000ae0c

Please sign in to comment.