Match components which are direct boot unaware

This commit is contained in:
vvb2060 2022-03-02 23:16:47 +08:00 committed by John Wu
parent 80dd37ee31
commit 000a163beb

View File

@ -106,12 +106,15 @@ public class DynLoad {
if (Build.VERSION.SDK_INT < 29) if (Build.VERSION.SDK_INT < 29)
replaceClassLoader(context); replaceClassLoader(context);
// noinspection InlinedApi
int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES
| PackageManager.GET_PROVIDERS | PackageManager.GET_RECEIVERS; | PackageManager.GET_PROVIDERS | PackageManager.GET_RECEIVERS
| PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
var pm = context.getPackageManager(); var pm = context.getPackageManager();
final PackageInfo info; final PackageInfo info;
try { try {
// noinspection WrongConstant
info = pm.getPackageInfo(context.getPackageName(), flags); info = pm.getPackageInfo(context.getPackageName(), flags);
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
// Impossible // Impossible
@ -122,6 +125,7 @@ public class DynLoad {
final var cl = loadApk(context); final var cl = loadApk(context);
if (cl != null) try { if (cl != null) try {
// noinspection WrongConstant
var pkgInfo = pm.getPackageArchiveInfo(apk.getPath(), flags); var pkgInfo = pm.getPackageArchiveInfo(apk.getPath(), flags);
cl.updateComponentMap(info, pkgInfo); cl.updateComponentMap(info, pkgInfo);
@ -162,7 +166,6 @@ public class DynLoad {
} }
// Replace LoadedApk mClassLoader // Replace LoadedApk mClassLoader
@SuppressWarnings("ConstantConditions")
private static void replaceClassLoader(Context context) { private static void replaceClassLoader(Context context) {
// Get ContextImpl // Get ContextImpl
while (context instanceof ContextWrapper) { while (context instanceof ContextWrapper) {
@ -173,6 +176,7 @@ public class DynLoad {
Field mInfo = context.getClass().getDeclaredField("mPackageInfo"); Field mInfo = context.getClass().getDeclaredField("mPackageInfo");
mInfo.setAccessible(true); mInfo.setAccessible(true);
Object loadedApk = mInfo.get(context); Object loadedApk = mInfo.get(context);
assert loadedApk != null;
Field mcl = loadedApk.getClass().getDeclaredField("mClassLoader"); Field mcl = loadedApk.getClass().getDeclaredField("mClassLoader");
mcl.setAccessible(true); mcl.setAccessible(true);
mcl.set(loadedApk, new DelegateClassLoader()); mcl.set(loadedApk, new DelegateClassLoader());