package com.ifpdos.hotfix;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.os.FileUtils;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private Object f3167a = null;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private String f3168b = null;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private String f3169c = null;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private String f3170d = null;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private String[] f3171e = null;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final BroadcastReceiver f3172f = new a();

    class a extends BroadcastReceiver {
        a() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            String stringExtra = intent.getStringExtra("package");
            if (stringExtra == null || context.getPackageName().equals(stringExtra)) {
                if (intent.getAction().equals(HotfixApplication.this.f3170d)) {
                    Log.d("Hotfix", "delete " + HotfixApplication.this.j().getAbsolutePath() + " : " + HotfixApplication.this.j().delete());
                    return;
                }
                if (intent.getAction().equals(HotfixApplication.this.f3169c)) {
                    HotfixApplication hotfixApplication = HotfixApplication.this;
                    File fileF = hotfixApplication.f(hotfixApplication.f3171e);
                    if (fileF == null) {
                        Log.d("Hotfix", "hotfix apk not found, not need to restart");
                        return;
                    }
                    if (intent.getBooleanExtra("save", false)) {
                        Log.d("Hotfix", "save hotfix apk");
                        try {
                            FileUtils.copy(new FileInputStream(fileF), new FileOutputStream(HotfixApplication.this.j()));
                        } 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");
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public File f(String[] strArr) {
        int i2;
        File fileH;
        try {
        } catch (Throwable th) {
            Log.e("Hotfix", "findHotfixApk failed", th);
        }
        for (String str : strArr) {
            if (str.startsWith("$USB")) {
                fileH = i(str);
            } else if (str.startsWith("$APP_DATA")) {
                fileH = g(str);
            } else if (str.startsWith("/")) {
                fileH = h(str);
            } else {
                Log.w("Hotfix", "Unknown apk path : " + str);
                fileH = null;
            }
            if (fileH != null) {
                if (com.ifpdos.hotfix.a.a(this, fileH)) {
                    return fileH;
                }
                Log.w("Hotfix", "checkApkSignature failed : " + str);
                return null;
            }
            return null;
        }
        return null;
    }

    private File g(String str) {
        File dataDir = getDataDir();
        if (dataDir == null) {
            return null;
        }
        File file = new File(str.replace("$APP_DATA", dataDir.getAbsolutePath()));
        if (com.ifpdos.hotfix.a.d(file)) {
            return file;
        }
        return null;
    }

    private File h(String str) {
        File file = new File(str);
        if (com.ifpdos.hotfix.a.d(file)) {
            return file;
        }
        return null;
    }

    private File i(String str) {
        String strSubstring = str.substring(5);
        Iterator it = k().iterator();
        while (it.hasNext()) {
            File file = new File((String) it.next(), strSubstring);
            if (com.ifpdos.hotfix.a.d(file)) {
                return file;
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public File j() {
        return new File(getCacheDir(), "hotfix.save.apk");
    }

    private List k() {
        List list;
        ArrayList arrayList = new ArrayList();
        try {
            list = (List) StorageManager.class.getMethod("getVolumes", null).invoke((StorageManager) getSystemService("storage"), null);
        } 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;
    }

    private void l() {
        String str = this.f3168b;
        if (str != null) {
            try {
                this.f3167a = com.ifpdos.hotfix.a.e(str).newInstance();
                n("setApplication", new Class[]{Application.class}, new Object[]{this});
            } catch (Throwable th) {
                Log.e("Hotfix", "initApplicationLikeHotfix failed", th);
            }
        }
    }

    private void m() {
        String str = this.f3168b;
        if (str != null) {
            try {
                this.f3167a = Class.forName(str).newInstance();
                n("setApplication", new Class[]{Application.class}, new Object[]{this});
            } catch (Throwable th) {
                Log.e("Hotfix", "initApplicationLikeNormal failed", th);
            }
        }
    }

    private void n(String str, Class[] clsArr, Object[] objArr) {
        Object obj = this.f3167a;
        if (obj != null) {
            try {
                Method method = obj.getClass().getMethod(str, clsArr);
                method.setAccessible(true);
                method.invoke(this.f3167a, objArr);
            } catch (Throwable th) {
                Log.e("Hotfix", "invoke " + str + " failed", th);
            }
        }
    }

    private void o() {
        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), 128);
            this.f3168b = applicationInfo.metaData.getString("HOTFIX_APPLICATION", null);
            this.f3169c = applicationInfo.metaData.getString("HOTFIX_LOAD_ACTION", "action.hotfix.load");
            this.f3170d = applicationInfo.metaData.getString("HOTFIX_CLEAN_ACTION", "action.hotfix.clean");
            this.f3171e = 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.f3168b);
            Log.d("Hotfix", "HOTFIX_LOAD_ACTION is " + this.f3169c);
            int i2 = 0;
            while (true) {
                String[] strArr = this.f3171e;
                if (i2 >= strArr.length) {
                    return;
                }
                String str = strArr[i2];
                String str2 = File.separator;
                if (str.endsWith(str2)) {
                    this.f3171e[i2] = this.f3171e[i2] + string;
                } else {
                    this.f3171e[i2] = this.f3171e[i2] + str2 + string;
                }
                Log.d("Hotfix", "Add hotfix apk path : " + this.f3171e[i2]);
                i2++;
            }
        } catch (Throwable th) {
            Log.e("Hotfix", "loadApplicationLike failed", th);
        }
    }

    @Override // android.content.ContextWrapper
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        o();
        File fileF = f(this.f3171e);
        if (fileF == null) {
            File fileJ = j();
            if (fileJ.exists()) {
                fileF = fileJ;
            }
        }
        if (fileF == null) {
            m();
            n("attachBaseContext", new Class[]{Context.class}, new Object[]{context});
            Log.d("Hotfix", "hotfix apk not found");
            return;
        }
        Log.d("Hotfix", "use hotfix apk : " + fileF.getAbsolutePath());
        com.ifpdos.hotfix.a.f(this, fileF);
        l();
        n("attachBaseContext", new Class[]{Context.class}, new Object[]{context});
    }

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

    @Override // android.app.Application
    public void onCreate() {
        super.onCreate();
        n("onCreate", new Class[0], new Object[0]);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(this.f3169c);
        intentFilter.addAction(this.f3170d);
        registerReceiver(this.f3172f, intentFilter, 2);
    }

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

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

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