package com.ifpdos.hotfix;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.FileUtils;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.util.Log;
import g.f.a.b;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class HotfixApplication extends Application {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public Object f1216f = null;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public String f1217m = null;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public String f1218n = null;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public String f1219o = null;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public String[] f1220p = null;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final BroadcastReceiver f1221q = new a();

    public class a extends BroadcastReceiver {
        public a() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            String string;
            String stringExtra = intent.getStringExtra("package");
            if (stringExtra == null || context.getPackageName().equals(stringExtra)) {
                if (intent.getAction().equals(HotfixApplication.this.f1219o)) {
                    boolean zDelete = HotfixApplication.a(HotfixApplication.this).delete();
                    StringBuilder sbF = g.a.a.a.a.F("delete ");
                    sbF.append(HotfixApplication.a(HotfixApplication.this).getAbsolutePath());
                    sbF.append(" : ");
                    sbF.append(zDelete);
                    string = sbF.toString();
                } else {
                    if (!intent.getAction().equals(HotfixApplication.this.f1218n)) {
                        return;
                    }
                    HotfixApplication hotfixApplication = HotfixApplication.this;
                    File fileB = hotfixApplication.b(hotfixApplication.f1220p);
                    if (fileB != null) {
                        if (intent.getBooleanExtra("save", false)) {
                            Log.d("Hotfix", "save hotfix apk");
                            try {
                                FileUtils.copy(new FileInputStream(fileB), new FileOutputStream(HotfixApplication.a(HotfixApplication.this)));
                            } catch (Throwable th) {
                                Log.e("Hotfix", "save hotfix apk failed", th);
                            }
                        }
                        Log.d("Hotfix", "restart to load hotfix apk");
                        System.exit(0);
                        throw new RuntimeException("restart to load hotfix apk");
                    }
                    string = "hotfix apk not found, not need to restart";
                }
                Log.d("Hotfix", string);
            }
        }
    }

    public static File a(HotfixApplication hotfixApplication) {
        Objects.requireNonNull(hotfixApplication);
        return new File(hotfixApplication.getCacheDir(), "hotfix.save.apk");
    }

    @Override // android.content.ContextWrapper
    public void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        Resources resources = null;
        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), 128);
            this.f1217m = applicationInfo.metaData.getString("HOTFIX_APPLICATION", null);
            this.f1218n = applicationInfo.metaData.getString("HOTFIX_LOAD_ACTION", "action.hotfix.load");
            this.f1219o = applicationInfo.metaData.getString("HOTFIX_CLEAN_ACTION", "action.hotfix.clean");
            this.f1220p = applicationInfo.metaData.getString("HOTFIX_DIRS", "/storage/emulated/0/Download,$USB/,$APP_DATA/cache").split(",");
            String string = applicationInfo.metaData.getString("HOTFIX_APK_NAME", getPackageName() + ".hotfix.apk");
            Log.d("Hotfix", "HOTFIX_APPLICATION is " + this.f1217m);
            Log.d("Hotfix", "HOTFIX_LOAD_ACTION is " + this.f1218n);
            int i2 = 0;
            while (true) {
                String[] strArr = this.f1220p;
                if (i2 >= strArr.length) {
                    break;
                }
                String str = strArr[i2];
                String str2 = File.separator;
                if (str.endsWith(str2)) {
                    this.f1220p[i2] = this.f1220p[i2] + string;
                } else {
                    this.f1220p[i2] = this.f1220p[i2] + str2 + string;
                }
                Log.d("Hotfix", "Add hotfix apk path : " + this.f1220p[i2]);
                i2++;
            }
        } catch (Throwable th) {
            Log.e("Hotfix", "loadApplicationLike failed", th);
        }
        File fileB = b(this.f1220p);
        if (fileB == null) {
            File file = new File(getCacheDir(), "hotfix.save.apk");
            if (file.exists()) {
                fileB = file;
            }
        }
        if (fileB == null) {
            String str3 = this.f1217m;
            if (str3 != null) {
                try {
                    this.f1216f = Class.forName(str3).newInstance();
                    d("setApplication", new Class[]{Application.class}, new Object[]{this});
                } catch (Throwable th2) {
                    Log.e("Hotfix", "initApplicationLikeNormal failed", th2);
                }
            }
            d("attachBaseContext", new Class[]{Context.class}, new Object[]{context});
            Log.d("Hotfix", "hotfix apk not found");
            return;
        }
        StringBuilder sbF = g.a.a.a.a.F("use hotfix apk : ");
        sbF.append(fileB.getAbsolutePath());
        Log.d("Hotfix", sbF.toString());
        b.d(this, fileB);
        try {
            Method declaredMethod = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
            AssetManager assetManager = (AssetManager) AssetManager.class.newInstance();
            declaredMethod.invoke(assetManager, fileB.getAbsolutePath());
            resources = new Resources(assetManager, getResources().getDisplayMetrics(), getResources().getConfiguration());
        } catch (Throwable th3) {
            Log.e("Hotfix", "loadHotfixResources failed ", th3);
        }
        b.a = resources;
        if (resources != null) {
            try {
                Field declaredField = ContextWrapper.class.getDeclaredField("mBase");
                declaredField.setAccessible(true);
                Object obj = declaredField.get(this);
                Field declaredField2 = Class.forName("android.app.ContextImpl").getDeclaredField("mResources");
                declaredField2.setAccessible(true);
                declaredField2.set(obj, b.a);
            } catch (Throwable th4) {
                Log.e("Hotfix", "replaceResource failed", th4);
            }
        }
        String str4 = this.f1217m;
        if (str4 != null) {
            try {
                this.f1216f = b.f4225c.loadClass(str4).newInstance();
                d("setApplication", new Class[]{Application.class}, new Object[]{this});
            } catch (Throwable th5) {
                Log.e("Hotfix", "initApplicationLikeHotfix failed", th5);
            }
        }
        d("attachBaseContext", new Class[]{Context.class}, new Object[]{context});
    }

    public final File b(String[] strArr) {
        File file;
        try {
            for (String str : strArr) {
                if (str.startsWith("$USB")) {
                    String strSubstring = str.substring(5);
                    Iterator it = ((ArrayList) c()).iterator();
                    while (it.hasNext()) {
                        file = new File((String) it.next(), strSubstring);
                        if (b.c(file)) {
                            break;
                        }
                    }
                    file = null;
                } else if (str.startsWith("$APP_DATA")) {
                    File dataDir = getDataDir();
                    if (dataDir == null) {
                        file = null;
                    } else {
                        File file2 = new File(str.replace("$APP_DATA", dataDir.getAbsolutePath()));
                        if (!b.c(file2)) {
                            file2 = null;
                        }
                        file = file2;
                    }
                } else {
                    if (str.startsWith("/")) {
                        File file3 = new File(str);
                        if (b.c(file3)) {
                            file = file3;
                        }
                    } else {
                        Log.w("Hotfix", "Unknown apk path : " + str);
                    }
                    file = null;
                }
                if (file != null) {
                    return file;
                }
            }
        } catch (Throwable th) {
            Log.e("Hotfix", "findHotfixApk failed", th);
        }
        return null;
    }

    public final List<String> c() {
        List list;
        ArrayList arrayList = new ArrayList();
        try {
            list = (List) StorageManager.class.getMethod("getVolumes", new Class[0]).invoke((StorageManager) getSystemService("storage"), new Object[0]);
        } catch (Throwable th) {
            Log.e("Hotfix", "getUsbPaths failed", th);
        }
        if (list != null && !list.isEmpty()) {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                arrayList.add(((VolumeInfo) it.next()).path);
            }
            return arrayList;
        }
        return arrayList;
    }

    public final void d(String str, Class<?>[] clsArr, Object[] objArr) {
        Object obj = this.f1216f;
        if (obj != null) {
            try {
                Method method = obj.getClass().getMethod(str, clsArr);
                method.setAccessible(true);
                method.invoke(this.f1216f, objArr);
            } catch (Throwable th) {
                Log.e("Hotfix", "invoke " + str + " failed", th);
            }
        }
    }

    @Override // android.app.Application, android.content.ComponentCallbacks
    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        d("onConfigurationChanged", new Class[]{Configuration.class}, new Object[]{configuration});
    }

    @Override // android.app.Application
    public void onCreate() {
        super.onCreate();
        d("onCreate", new Class[0], new Object[0]);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(this.f1218n);
        intentFilter.addAction(this.f1219o);
        registerReceiver(this.f1221q, intentFilter);
    }

    @Override // android.app.Application, android.content.ComponentCallbacks
    public void onLowMemory() {
        super.onLowMemory();
        d("onLowMemory", new Class[0], new Object[0]);
    }

    @Override // android.app.Application
    public void onTerminate() {
        super.onTerminate();
        d("onTerminate", new Class[0], new Object[0]);
    }

    @Override // android.app.Application, android.content.ComponentCallbacks2
    public void onTrimMemory(int i2) {
        super.onTrimMemory(i2);
        d("onTrimMemory", new Class[]{Integer.TYPE}, new Object[]{Integer.valueOf(i2)});
    }
}
