package com.ifpdos.hotfix;

import android.app.Application;
import android.app.LoadedApk;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.util.Log;
import c0.C0403a;
import java.io.File;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public abstract class a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static Resources f3174a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static LoadedApk f3175b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static ClassLoader f3176c;

    static boolean a(Context context, File file) {
        Signature[] signatureArr;
        Signature[] signatureArr2;
        if (file != null) {
            try {
                if (file.exists() && file.canRead()) {
                    PackageManager packageManager = context.getPackageManager();
                    PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 64);
                    PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(file.getAbsolutePath(), 64);
                    if (packageArchiveInfo != null && (signatureArr = packageArchiveInfo.signatures) != null && signatureArr.length != 0 && (signatureArr2 = packageInfo.signatures) != null && signatureArr2.length != 0) {
                        Signature signature = signatureArr2[0];
                        for (Signature signature2 : signatureArr) {
                            if (signature.equals(signature2)) {
                                return true;
                            }
                        }
                    }
                    return false;
                }
            } catch (Throwable th) {
                Log.e("Hotfix", "checkApkSignature failed", th);
            }
        }
        return false;
    }

    private static Field b(Class cls, String str) throws NoSuchFieldException {
        Class superclass = cls;
        while (true) {
            try {
                Field declaredField = superclass.getDeclaredField(str);
                declaredField.setAccessible(true);
                return declaredField;
            } catch (Throwable unused) {
                if (superclass == Object.class) {
                    throw new NoSuchFieldException("Cannot find field " + str + " in class " + cls.getName() + " and its super classes.");
                }
                superclass = superclass.getSuperclass();
            }
        }
    }

    private static List c() {
        try {
            ClassLoader classLoader = a.class.getClassLoader();
            Object obj = b(Class.forName("dalvik.system.BaseDexClassLoader", false, classLoader), "pathList").get(classLoader);
            return (List) b(obj.getClass(), "nativeLibraryDirectories").get(obj);
        } catch (Throwable th) {
            Log.e("Hotfix", "getOriginNativeLibraryDirectories failed", th);
            return new ArrayList();
        }
    }

    public static boolean d(File file) {
        return file.exists() && file.canRead() && file.isFile();
    }

    public static Class e(String str) {
        return f3176c.loadClass(str);
    }

    public static void f(Application application, File file) {
        g(application, file);
        f3174a = h(application, file);
        i(application);
    }

    private static void g(Application application, File file) {
        try {
            List<File> listC = c();
            listC.add(0, file.getParentFile());
            StringBuilder sb = new StringBuilder();
            boolean z2 = true;
            for (File file2 : listC) {
                if (file2 != null) {
                    if (z2) {
                        z2 = false;
                    } else {
                        sb.append(File.pathSeparator);
                    }
                    sb.append(file2.getAbsolutePath());
                }
            }
            f3176c = new C0403a(file.getAbsolutePath(), sb.toString(), a.class.getClassLoader());
            Class<?> cls = Class.forName("android.app.ActivityThread");
            Field declaredField = cls.getDeclaredField("sCurrentActivityThread");
            declaredField.setAccessible(true);
            Object obj = declaredField.get(null);
            Field declaredField2 = cls.getDeclaredField("mPackages");
            declaredField2.setAccessible(true);
            f3175b = (LoadedApk) ((WeakReference) ((Map) declaredField2.get(obj)).get(application.getPackageName())).get();
            Field declaredField3 = LoadedApk.class.getDeclaredField("mClassLoader");
            declaredField3.setAccessible(true);
            declaredField3.set(f3175b, f3176c);
        } catch (Throwable th) {
            Log.e("Hotfix", "loadHotfixClassLoader failed", th);
        }
    }

    private static Resources h(Context context, File file) {
        try {
            Method declaredMethod = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
            AssetManager assetManager = (AssetManager) AssetManager.class.newInstance();
            declaredMethod.invoke(assetManager, file.getAbsolutePath());
            return new Resources(assetManager, context.getResources().getDisplayMetrics(), context.getResources().getConfiguration());
        } catch (Throwable th) {
            Log.e("Hotfix", "loadHotfixResources failed ", th);
            return null;
        }
    }

    private static void i(Application application) {
        if (f3174a == null) {
            return;
        }
        try {
            Field declaredField = ContextWrapper.class.getDeclaredField("mBase");
            declaredField.setAccessible(true);
            Object obj = declaredField.get(application);
            Field declaredField2 = Class.forName("android.app.ContextImpl").getDeclaredField("mResources");
            declaredField2.setAccessible(true);
            declaredField2.set(obj, f3174a);
        } catch (Throwable th) {
            Log.e("Hotfix", "replaceResource failed", th);
        }
    }
}
