fix old android crash accenting

This commit is contained in:
gdlbo
2023-07-04 23:23:18 +03:00
parent c70ab388c1
commit cdaf7909d4
3 changed files with 15 additions and 33 deletions
@@ -124,7 +124,7 @@ public class ThemesManager {
ResourcesLoader.load(app, modApk.getAbsolutePath(), false);
}
} catch (Exception e) {
Log.e("ThemesManager", e.getMessage());
e.printStackTrace();
}
}
@@ -152,8 +152,7 @@ public class ThemesManager {
return validated && modApk.exists() && ThemesUtils.getReservedAccent() != Color.TRANSPARENT;
}
public static void generateModApk(int accentColor)
throws Throwable {
public static void generateModApk(int accentColor) throws Throwable {
try (var apk = new ZipFile(baseApkPath)) {
var arscEntry = apk.getEntry("resources.arsc");
var arscBis = new BufferedInputStream(apk.getInputStream(arscEntry));
@@ -2,6 +2,7 @@ package ru.vtosters.lite.themes.loaders;
import android.annotation.SuppressLint;
import android.content.res.AssetManager;
import android.util.Log;
import ru.vtosters.lite.utils.ReflectionUtils;
import java.lang.reflect.InvocationTargetException;
@@ -9,24 +10,11 @@ import java.lang.reflect.Method;
@SuppressLint("PrivateApi")
public class AssetManagerHelper {
// methods
private static final Method getAssetPathMtd;
private static final Method getApkAssetsMtd;
static {
try {
getAssetPathMtd = ReflectionUtils.findMethod(Class.forName("android.content.res.ApkAssets"), "getAssetPath");
getApkAssetsMtd = ReflectionUtils.findMethod(AssetManager.class, "getApkAssets");
} catch (ClassNotFoundException | NoSuchMethodException e) {
throw new RuntimeException();
}
}
public static String getApkPath(Object assetManager, String packagename) throws InvocationTargetException, IllegalAccessException {
Object[] apkAssetsArr = (Object[]) getAssetPathMtd.invoke(assetManager);
public static String getApkPath(Object assetManager, String packagename) throws InvocationTargetException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException {
Object[] apkAssetsArr = (Object[]) ReflectionUtils.findMethod(Class.forName("android.content.res.ApkAssets"), "getAssetPath").invoke(assetManager);
// By default, base apk is the last element of array if is not, for execution speed, we should iterate array from the end to the beginning
for (int i = apkAssetsArr.length - 1; i >= 0; i--) {
String pathname = (String) getApkAssetsMtd.invoke(apkAssetsArr[i]);
String pathname = (String) ReflectionUtils.findMethod(AssetManager.class, "getApkAssets").invoke(apkAssetsArr[i]);
if (pathname != null && pathname.contains(packagename)) return pathname;
}
@@ -34,12 +22,8 @@ public class AssetManagerHelper {
}
//region Uses for adding own assets or getting existing cookies
public static int addAssetPath(AssetManager manager, String path) {
try {
return (int) AssetManager.class.getMethod("addAssetPath", String.class).invoke(manager, path);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
public static void addAssetPath(AssetManager manager, String path) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
manager.getClass().getMethod("addAssetPath", String.class).invoke(manager, path);
}
public static int addAssetPathAsSharedLibrary(AssetManager manager, String path) {
@@ -14,7 +14,9 @@ import ru.vtosters.lite.utils.ReflectionUtils;
import java.io.File;
import java.lang.ref.WeakReference;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
@@ -25,7 +27,6 @@ public class ResourcesLoader {
private static Collection<WeakReference<Resources>> sResourceReferences = null;
private static Object sCurrentActivityThread = null;
private static AssetManager sAssetManager = null;
// methods
private static Method ensureStringBlocksMtd = null;
@@ -89,8 +90,7 @@ public class ResourcesLoader {
}
}
public static void init(Context context)
throws Exception {
public static void init(Context context) throws Exception {
sCurrentActivityThread = ReflectionUtils.getActivityThread(context, null);
//pre-N
@@ -110,8 +110,7 @@ public class ResourcesLoader {
}
}
public static void load(Context context, String resPath, boolean reinject)
throws Exception {
public static void load(Context context, String resPath, boolean reinject) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException {
ApplicationInfo appInfo = context.getApplicationInfo();
Field[] packagesFlds = Build.VERSION.SDK_INT < Build.VERSION_CODES.O_MR1
@@ -135,10 +134,10 @@ public class ResourcesLoader {
return;
}
sAssetManager = AssetManager.class.newInstance();
// Create a new AssetManager instance and point it to the resources installed under
if (AssetManagerHelper.addAssetPath(sAssetManager, resPath) == 0)
throw new IllegalStateException("Could not create new AssetManager");
AssetManager sAssetManager = AssetManager.class.newInstance();
AssetManagerHelper.addAssetPath(sAssetManager, resPath);
// Add SharedLibraries to AssetManager for resolve system resources not found issue
// This influence SharedLibrary Package ID