%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD.apk(点击下载) / f.java


package com.baidu.techain.core;

import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;
import com.baidu.techain.TechainReceiver;
import com.baidu.techain.ac.U;
import com.baidu.techain.b;
import com.baidu.techain.e;
import com.baidu.techain.i.c;
import com.baidu.techain.i.n;
import com.meizu.cloud.pushsdk.constants.PushConstants;
import com.meizu.cloud.pushsdk.notification.model.NotifyType;
import com.umeng.analytics.pro.ak;
import com.xiaomi.mipush.sdk.Constants;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.json.JSONArray;
import org.json.JSONObject;

/* compiled from: PluginloaderHub */
public final class f {
    public static List<Integer> b = new ArrayList();
    private static f e;
    private static Application f;
    private static Random g = new Random();
    public Map<String, ApkInfo> a = new ConcurrentHashMap();
    private String c;
    private boolean d;
    private Map<String, ApkInfo> h = new ConcurrentHashMap();
    private Map<String, TechainReceiver> i = new HashMap();

    public static synchronized f a(Context context) {
        f fVar;
        synchronized (f.class) {
            if (e == null) {
                f = (Application) context.getApplicationContext();
                e = new f();
            }
            fVar = e;
        }
        return fVar;
    }

    public static f a() {
        return e;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:39:?, code lost:
        com.baidu.techain.i.c.a();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:41:0x008f, code lost:
        return;
     */
    /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
    /* JADX WARNING: Missing exception handler attribute for start block: B:38:0x008b */
    public final synchronized void a(g gVar) {
        if (gVar.d != null) {
            ApkInfo apkInfo = this.a.get(gVar.a);
            if (apkInfo != null) {
                if (apkInfo.intentFilters == null) {
                    apkInfo.intentFilters = new ArrayList();
                }
                for (int i2 = 0; i2 < apkInfo.intentFilters.size(); i2++) {
                    if (gVar.a(apkInfo.intentFilters.get(i2))) {
                        return;
                    }
                }
                apkInfo.intentFilters.add(gVar);
                String a2 = a(gVar.d);
                if (!TextUtils.isEmpty(a2) && !a2.equals("____")) {
                    if (!this.i.keySet().contains(a2)) {
                        TechainReceiver techainReceiver = new TechainReceiver();
                        if (!c.a(f, techainReceiver, gVar.d)) {
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException unused) {
                                c.a();
                            }
                            c.a(f, techainReceiver, gVar.d);
                        }
                        this.i.put(a2, techainReceiver);
                    }
                }
            }
        }
    }

    private static String a(IntentFilter intentFilter) {
        if (intentFilter == null) {
            return "";
        }
        try {
            StringBuilder sb = new StringBuilder();
            int countActions = intentFilter.countActions();
            if (countActions > 0) {
                ArrayList<String> arrayList = new ArrayList();
                for (int i2 = 0; i2 < countActions; i2++) {
                    try {
                        String action = intentFilter.getAction(i2);
                        if (!TextUtils.isEmpty(action)) {
                            arrayList.add(action);
                        }
                    } catch (Throwable unused) {
                        c.a();
                    }
                }
                if (arrayList.size() > 0) {
                    Collections.sort(arrayList);
                    for (String str : arrayList) {
                        sb.append(str);
                    }
                } else {
                    sb.append("_");
                }
            } else {
                sb.append("_");
            }
            int countCategories = intentFilter.countCategories();
            if (countCategories > 0) {
                ArrayList<String> arrayList2 = new ArrayList();
                for (int i3 = 0; i3 < countCategories; i3++) {
                    try {
                        String category = intentFilter.getCategory(i3);
                        if (!TextUtils.isEmpty(category)) {
                            arrayList2.add(category);
                        }
                    } catch (Throwable unused2) {
                        c.a();
                    }
                }
                if (arrayList2.size() > 0) {
                    Collections.sort(arrayList2);
                    for (String str2 : arrayList2) {
                        sb.append(str2);
                    }
                } else {
                    sb.append("_");
                }
            } else {
                sb.append("_");
            }
            if (intentFilter.countDataTypes() > 0) {
                ArrayList<String> arrayList3 = new ArrayList();
                for (int i4 = 0; i4 < countCategories; i4++) {
                    try {
                        String dataType = intentFilter.getDataType(i4);
                        if (!TextUtils.isEmpty(dataType)) {
                            arrayList3.add(dataType);
                        }
                    } catch (Throwable unused3) {
                        c.a();
                    }
                }
                if (arrayList3.size() > 0) {
                    Collections.sort(arrayList3);
                    for (String str3 : arrayList3) {
                        sb.append(str3);
                    }
                } else {
                    sb.append("_");
                }
            } else {
                sb.append("_");
            }
            int countDataSchemes = intentFilter.countDataSchemes();
            if (countDataSchemes > 0) {
                ArrayList<String> arrayList4 = new ArrayList();
                for (int i5 = 0; i5 < countDataSchemes; i5++) {
                    try {
                        String dataScheme = intentFilter.getDataScheme(i5);
                        if (!TextUtils.isEmpty(dataScheme)) {
                            arrayList4.add(dataScheme);
                        }
                    } catch (Throwable unused4) {
                        c.a();
                    }
                }
                if (arrayList4.size() > 0) {
                    Collections.sort(arrayList4);
                    for (String str4 : arrayList4) {
                        sb.append(str4);
                    }
                } else {
                    sb.append("_");
                }
            } else {
                sb.append("_");
            }
            return sb.toString();
        } catch (Throwable unused5) {
            return "";
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:58:?, code lost:
        com.baidu.techain.i.c.a();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:60:0x00de, code lost:
        return;
     */
    /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
    /* JADX WARNING: Missing exception handler attribute for start block: B:57:0x00da */
    public final synchronized void b(g gVar) {
        if (gVar.d != null) {
            ApkInfo apkInfo = this.a.get(gVar.a);
            if (!(apkInfo == null || apkInfo.intentFilters == null)) {
                ArrayList<Integer> arrayList = new ArrayList();
                for (int i2 = 0; i2 < apkInfo.intentFilters.size(); i2++) {
                    if (gVar.a(apkInfo.intentFilters.get(i2))) {
                        arrayList.add(Integer.valueOf(i2));
                    }
                }
                for (Integer num : arrayList) {
                    int intValue = num.intValue();
                    if (!(apkInfo == null || apkInfo.intentFilters == null)) {
                        try {
                            apkInfo.intentFilters.remove(intValue);
                        } catch (Throwable unused) {
                            c.a();
                        }
                    }
                }
                if (!(apkInfo == null || apkInfo.intentFilters == null || apkInfo.intentFilters.size() != 0)) {
                    apkInfo.intentFilters = null;
                }
            }
            String a2 = a(gVar.d);
            if (!TextUtils.isEmpty(a2)) {
                for (ApkInfo apkInfo2 : b()) {
                    if (apkInfo2.intentFilters != null && apkInfo2.intentFilters.size() > 0) {
                        for (g gVar2 : apkInfo2.intentFilters) {
                            String a3 = a(gVar2.d);
                            if (!TextUtils.isEmpty(a3) && a3.equals(a2)) {
                                return;
                            }
                        }
                        continue;
                    }
                }
                f.unregisterReceiver(this.i.get(a2));
                this.i.remove(a2);
            }
        }
    }

    public final List<ApkInfo> b() {
        try {
            ArrayList arrayList = new ArrayList();
            for (String str : this.a.keySet()) {
                arrayList.add(this.a.get(str));
            }
            return arrayList;
        } catch (Throwable unused) {
            c.a();
            return null;
        }
    }

    public final boolean a(ApkInfo apkInfo, boolean z) {
        System.currentTimeMillis();
        this.d = z;
        this.c = apkInfo.versionName;
        return a(apkInfo);
    }

    private synchronized boolean a(ApkInfo apkInfo) {
        boolean z;
        if (apkInfo != null) {
            if (!TextUtils.isEmpty(apkInfo.pkgPath)) {
                ApkInfo apkInfo2 = this.h.get(apkInfo.pkgPath);
                if (apkInfo2 != null) {
                    if (apkInfo2.versionName.equals(apkInfo.versionName)) {
                        return true;
                    }
                    a(apkInfo.pkgPath);
                }
                if (!c.a(new File(apkInfo.pkgPath))) {
                    HashMap hashMap = new HashMap();
                    hashMap.put(PushConstants.PUSH_TYPE_NOTIFY, 1);
                    hashMap.put("1", Integer.valueOf(apkInfo.key));
                    hashMap.put(PushConstants.PUSH_TYPE_UPLOAD_LOG, apkInfo.versionName);
                    c.a(f.getApplicationContext(), "1003117", (Map<String, Object>) hashMap, false);
                    return false;
                }
                try {
                    apkInfo.hostContext = f;
                    if (apkInfo.apkParseSuc == 1) {
                        try {
                            if (TextUtils.isEmpty(apkInfo.packageName) || TextUtils.isEmpty(apkInfo.pkgPath)) {
                                throw new RuntimeException("packageName or pkgPath miss");
                            } else if (!a(apkInfo.key, apkInfo.versionName, apkInfo.apkMD5, apkInfo.pkgPath)) {
                                return false;
                            } else {
                                apkInfo.dataDir = f.getFilesDir().getCanonicalPath() + "/.techain_" + apkInfo.key;
                                String str = apkInfo.dataDir + "/dex";
                                String str2 = (apkInfo.dataDir + "/lib/" + this.c) + "/" + g.nextInt();
                                f(str);
                                c.a(str, false);
                                f(str2);
                                a(apkInfo, str2, str);
                                this.h.put(apkInfo.pkgPath, apkInfo);
                                this.a.put(apkInfo.packageName, apkInfo);
                                a(apkInfo, apkInfo.className);
                            }
                        } catch (Throwable unused) {
                            a(apkInfo.pkgPath);
                            c.a();
                            z = true;
                        }
                    }
                    z = false;
                    if (apkInfo.apkParseSuc != 1 || z) {
                        PackageInfo packageInfo = apkInfo.cloudPkgInfo;
                        if ((packageInfo == null || TextUtils.isEmpty(packageInfo.packageName) || TextUtils.isEmpty(packageInfo.versionName)) && (((packageInfo = f.getPackageManager().getPackageArchiveInfo(apkInfo.pkgPath, 1)) == null || TextUtils.isEmpty(packageInfo.packageName) || TextUtils.isEmpty(packageInfo.versionName)) && ((packageInfo = a(apkInfo.packageName, apkInfo.apkMD5)) == null || TextUtils.isEmpty(packageInfo.packageName) || TextUtils.isEmpty(packageInfo.versionName)))) {
                            throw new Exception("requestCloudPackageInfo failed");
                        } else if (TextUtils.isEmpty(packageInfo.packageName) || !packageInfo.packageName.startsWith("com.baidu.techain")) {
                            throw new Exception("package name check failed");
                        } else if (!a(apkInfo.key, packageInfo.versionName, apkInfo.apkMD5, apkInfo.pkgPath)) {
                            return false;
                        } else {
                            apkInfo.packageName = packageInfo.packageName;
                            apkInfo.className = packageInfo.applicationInfo.className;
                            apkInfo.versionName = packageInfo.versionName;
                            apkInfo.activities = packageInfo.activities;
                            apkInfo.applicationTheme = packageInfo.applicationInfo.theme;
                            apkInfo.dataDir = f.getFilesDir().getCanonicalPath() + "/.techain_" + apkInfo.key;
                            String str3 = apkInfo.dataDir + "/dex";
                            String str4 = (apkInfo.dataDir + "/lib/" + this.c) + "/" + g.nextInt();
                            f(str3);
                            c.a(str3, false);
                            f(str4);
                            a(apkInfo, str4, str3);
                            this.h.put(apkInfo.pkgPath, apkInfo);
                            this.a.put(apkInfo.packageName, apkInfo);
                            a(apkInfo, packageInfo.applicationInfo.className);
                        }
                    }
                    return true;
                } catch (Throwable unused2) {
                    c.a();
                }
            }
        }
        return false;
        return false;
    }

    private static PackageInfo a(String str, String str2) {
        try {
            JSONArray jSONArray = new JSONArray();
            JSONObject jSONObject = new JSONObject();
            jSONObject.put(PushConstants.URI_PACKAGE_NAME, str);
            jSONObject.put("m", str2);
            jSONArray.put(jSONObject);
            JSONArray jSONArray2 = new JSONArray(com.baidu.techain.i.f.a(f, c.b() + "p/1/pdl", jSONArray.toString(), false, true));
            if (jSONArray2.length() <= 0) {
                return null;
            }
            JSONObject optJSONObject = jSONArray2.optJSONObject(0);
            PackageInfo packageInfo = new PackageInfo();
            packageInfo.packageName = optJSONObject.optString(ak.ax);
            packageInfo.versionName = optJSONObject.optString("v");
            ApplicationInfo applicationInfo = new ApplicationInfo();
            applicationInfo.className = optJSONObject.optString("n");
            if (!TextUtils.isEmpty(applicationInfo.className) && applicationInfo.className.startsWith(".")) {
                applicationInfo.className = packageInfo.packageName + applicationInfo.className;
            }
            applicationInfo.theme = optJSONObject.optInt(ak.aH);
            packageInfo.applicationInfo = applicationInfo;
            JSONArray optJSONArray = optJSONObject.optJSONArray(ak.av);
            if (optJSONArray != null && optJSONArray.length() > 0) {
                ArrayList arrayList = new ArrayList();
                for (int i2 = 0; i2 < optJSONArray.length(); i2++) {
                    try {
                        JSONObject jSONObject2 = optJSONArray.getJSONObject(i2);
                        if (jSONObject2 != null) {
                            ActivityInfo activityInfo = new ActivityInfo();
                            activityInfo.name = jSONObject2.optString("n");
                            if (!TextUtils.isEmpty(activityInfo.name) && activityInfo.name.startsWith(".")) {
                                activityInfo.name = packageInfo.packageName + activityInfo.name;
                            }
                            activityInfo.packageName = packageInfo.packageName;
                            activityInfo.theme = jSONObject2.optInt(ak.aH);
                            activityInfo.labelRes = jSONObject2.optInt(NotifyType.LIGHTS);
                            if (!TextUtils.isEmpty(activityInfo.name)) {
                                arrayList.add(activityInfo);
                            }
                        }
                    } catch (Throwable unused) {
                        c.a();
                    }
                }
                if (arrayList.size() > 0) {
                    packageInfo.activities = (ActivityInfo[]) arrayList.toArray(new ActivityInfo[arrayList.size()]);
                }
            }
            return packageInfo;
        } catch (Throwable unused2) {
            c.a();
            return null;
        }
    }

    private static boolean a(int i2, String str, String str2, String str3) {
        Pair pair;
        String str4 = i2 + str;
        if (U.sRealtimeMd5Map != null) {
            String str5 = U.sRealtimeMd5Map.get(str4);
            if (!TextUtils.isEmpty(str5)) {
                str2 = str5;
            }
        }
        if (TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3)) {
            pair = new Pair(false, "");
        } else {
            File file = new File(str3);
            if (!c.a(file)) {
                pair = new Pair(false, "");
            } else {
                String a2 = n.a(file);
                if (TextUtils.isEmpty(a2)) {
                    pair = new Pair(false, "");
                } else if (!a2.equalsIgnoreCase(str2)) {
                    pair = new Pair(false, a2);
                } else {
                    pair = new Pair(true, "");
                }
            }
        }
        if (((Boolean) pair.first).booleanValue()) {
            return true;
        }
        HashMap hashMap = new HashMap();
        hashMap.put(PushConstants.PUSH_TYPE_NOTIFY, 3);
        hashMap.put("1", Integer.valueOf(i2));
        hashMap.put(PushConstants.PUSH_TYPE_UPLOAD_LOG, str);
        hashMap.put(PushConstants.PUSH_FLYME_3_CHANGE_VERSION_START, Base64.encodeToString(((String) pair.second).getBytes(), 0).replace("\n", "").replace("\t", "").replace("\r", ""));
        c.a(f.getApplicationContext(), "1003117", (Map<String, Object>) hashMap, false);
        return false;
    }

    private static boolean a(ApkInfo apkInfo, String str) {
        try {
            if (TextUtils.isEmpty(str)) {
                return true;
            }
            Application application = (Application) apkInfo.classLoader.loadClass(str).newInstance();
            e.a(Application.class, f, application);
            Application application2 = f;
            Class<?> cls = application2.getClass();
            for (Class<Application> cls2 = Application.class; cls2 != null; cls2 = cls2.getSuperclass()) {
                try {
                    Field declaredField = cls2.getDeclaredField("mBase");
                    declaredField.setAccessible(true);
                    if (declaredField.getType().isAssignableFrom(cls)) {
                        declaredField.set(application, application2);
                    }
                } catch (Throwable unused) {
                    c.a();
                }
            }
            application.onCreate();
            return true;
        } catch (Throwable unused2) {
            return false;
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:34:?, code lost:
        com.baidu.techain.i.c.d(new java.io.File(r20, "apkDex").getAbsolutePath());
        r2 = new java.io.File(r20, "dexDex").getAbsolutePath();
        com.baidu.techain.i.c.d(r2);
        f(r2);
        r9 = a(r18, r19, false);
        r18.classLoader = new com.baidu.techain.core.e(r9, r2, r6, r7);
        r1 = r18.classLoader.loadClass("com.baidu.techain.engine.EngineImpl");
        r2 = new java.io.File(r9);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:35:0x010a, code lost:
        if (r2.exists() != false) goto L_0x010c;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:36:0x010c, code lost:
        r2.delete();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:37:0x010f, code lost:
        if (r1 == null) goto L_0x011c;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:40:0x011b, code lost:
        return;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:42:0x0121, code lost:
        throw new java.lang.Exception("class ForHostApp.ENGINE_IMPL_CLASS_FULL_PATH loaded is null");
     */
    /* JADX WARNING: Code restructure failed: missing block: B:43:0x0122, code lost:
        if (r18 != null) goto L_0x0124;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:46:?, code lost:
        com.baidu.techain.i.c.d(r18.dataDir);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:47:0x012b, code lost:
        if (com.baidu.techain.core.f.f != null) goto L_0x012d;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:48:0x012d, code lost:
        com.baidu.techain.i.c.d(com.baidu.techain.core.f.f.getFileStreamPath(r18.packageName).getAbsolutePath());
     */
    /* JADX WARNING: Code restructure failed: missing block: B:49:0x013d, code lost:
        com.baidu.techain.i.c.a();
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:33:0x00d2 */
    private void a(ApkInfo apkInfo, String str, String str2) throws Throwable {
        f.getFilesDir();
        String a2 = a(apkInfo, str, true);
        String property = System.getProperty("java.library.path");
        if (Build.VERSION.SDK_INT >= 25 || TextUtils.isEmpty(property)) {
            property = "";
        }
        if (TextUtils.isEmpty(a2)) {
            a2 = property;
        } else if (!TextUtils.isEmpty(property)) {
            a2 = a2 + Constants.COLON_SEPARATOR + property;
        }
        apkInfo.libPath = a2;
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(str2, "apkDex");
        String absolutePath = file.getAbsolutePath();
        f(absolutePath);
        if (Build.VERSION.SDK_INT == 21 || Build.VERSION.SDK_INT == 22) {
            long freeSpace = file.getFreeSpace();
            File file2 = new File(apkInfo.pkgPath);
            if (file2.exists() && file2.isFile()) {
                long length = file2.length();
                StringBuilder sb = new StringBuilder("V5 freeSpace=");
                sb.append(freeSpace);
                sb.append(",fileSize=");
                sb.append(length);
                b.a();
                if (freeSpace < length * 2) {
                    throw new Exception("Have no space to load plugin.");
                }
            }
        }
        apkInfo.classLoader = new e(apkInfo.pkgPath, absolutePath, a2, classLoader);
        Class<?> loadClass = apkInfo.classLoader.loadClass("com.baidu.techain.engine.EngineImpl");
        if (loadClass == null || TextUtils.isEmpty(loadClass.getName())) {
            throw new Exception("class ForHostApp.ENGINE_IMPL_CLASS_FULL_PATH loaded is null");
        }
        return;
        if (apkInfo != null) {
            throw new RuntimeException("can't load EngineImpl by both dexFile:" + "" + " and ZipFile:" + apkInfo.pkgPath);
        }
        throw new RuntimeException("can't load EngineImpl by both dexFile:" + "" + " and ZipFile:null");
    }

    public final boolean a(String str) {
        ApkInfo apkInfo = this.h.get(str);
        if (apkInfo == null) {
            return false;
        }
        this.h.remove(str);
        this.a.remove(apkInfo.packageName);
        com.baidu.techain.mutiprocess.b.b(apkInfo.packageName);
        c.d(apkInfo.dataDir);
        Application application = f;
        if (application == null) {
            return true;
        }
        c.d(application.getFileStreamPath(apkInfo.packageName).getAbsolutePath());
        return true;
    }

    public final boolean b(String str) {
        ApkInfo apkInfo = this.a.get(str);
        if (apkInfo == null) {
            return false;
        }
        this.h.remove(apkInfo.pkgPath);
        this.a.remove(str);
        com.baidu.techain.mutiprocess.b.b(str);
        c.d(apkInfo.dataDir);
        Application application = f;
        if (application == null) {
            return true;
        }
        c.d(application.getFileStreamPath(apkInfo.packageName).getAbsolutePath());
        return true;
    }

    public final ApkInfo c(String str) {
        try {
            return this.h.get(str);
        } catch (Throwable unused) {
            c.a();
            return null;
        }
    }

    public final ApkInfo d(String str) {
        try {
            return this.a.get(str);
        } catch (Throwable unused) {
            c.a();
            return null;
        }
    }

    private static boolean f(String str) {
        try {
            File file = new File(str);
            if (file.exists() && !file.isDirectory()) {
                file.delete();
            }
            if (file.exists()) {
                return true;
            }
            file.mkdirs();
            return true;
        } catch (Throwable unused) {
            c.a();
            return false;
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:112:0x01f3, code lost:
        r0 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:113:0x01f4, code lost:
        r10 = r0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:114:0x01f7, code lost:
        r10 = r0;
        r7 = r7;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:116:0x01fe, code lost:
        r10 = r0;
        r7 = r7;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:129:0x0271, code lost:
        if (r10 != null) goto L_0x0273;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:12:0x0030, code lost:
        r0 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:131:?, code lost:
        r10.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:132:0x0277, code lost:
        com.baidu.techain.i.c.a();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:139:?, code lost:
        r10.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0031, code lost:
        r10 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:143:0x0289, code lost:
        r10.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:144:0x028d, code lost:
        com.baidu.techain.i.c.a();
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:112:0x01f3 A[ExcHandler: all (th java.lang.Throwable), Splitter:B:28:0x009c] */
    /* JADX WARNING: Removed duplicated region for block: B:12:0x0030 A[ExcHandler: all (th java.lang.Throwable), Splitter:B:19:0x003d] */
    /* JADX WARNING: Removed duplicated region for block: B:130:0x0273 A[SYNTHETIC, Splitter:B:130:0x0273] */
    /* JADX WARNING: Removed duplicated region for block: B:138:0x0280 A[SYNTHETIC, Splitter:B:138:0x0280] */
    /* JADX WARNING: Removed duplicated region for block: B:143:0x0289 A[Catch:{ all -> 0x028d }] */
    /* JADX WARNING: Removed duplicated region for block: B:148:0x0294  */
    /* JADX WARNING: Removed duplicated region for block: B:150:0x0299 A[RETURN] */
    private static String a(ApkInfo apkInfo, String str, boolean z) throws Throwable {
        String str2;
        boolean z2;
        String str3;
        ZipFile zipFile;
        ZipFile zipFile2;
        Throwable th;
        String str4;
        String message;
        ZipFile zipFile3;
        String str5;
        File file;
        String str6 = "";
        if (apkInfo == null) {
            return str6;
        }
        String replace = (!z || TextUtils.isEmpty(apkInfo.versionName)) ? str6 : apkInfo.versionName.replace(".", str6);
        try {
            zipFile3 = new ZipFile(apkInfo.pkgPath);
        } catch (FileNotFoundException e2) {
            try {
                message = e2.getMessage();
                File file2 = new File(apkInfo.hostContext.getFilesDir(), ".b_techain");
                if (file2.exists()) {
                    File file3 = new File(file2, apkInfo.key + Constants.ACCEPT_TIME_SEPARATOR_SERVER + apkInfo.versionName);
                    if (file3.exists()) {
                        File file4 = new File(apkInfo.pkgPath);
                        c.a(file3, file4);
                        c.a(apkInfo.pkgPath, true);
                        com.baidu.techain.c.a(file4);
                        com.baidu.techain.c.a(apkInfo.hostContext, apkInfo.key, file4, file3);
                        if (file4.exists()) {
                            zipFile3 = new ZipFile(apkInfo.pkgPath);
                        } else {
                            throw new FileNotFoundException(message + "--file not exists after copy");
                        }
                    } else {
                        throw new FileNotFoundException(message + "--backupFile not exists");
                    }
                } else {
                    throw new FileNotFoundException(message + "--backupDir not exists");
                }
            } catch (ZipException unused) {
                str4 = str6;
                str3 = str4;
                zipFile = null;
                if (zipFile != null) {
                }
                str2 = str3;
                z2 = true;
                if (z2) {
                }
            } catch (EOFException unused2) {
                str4 = str6;
                str3 = str4;
                zipFile2 = null;
                if (zipFile2 != null) {
                }
                str2 = str3;
                z2 = true;
                if (z2) {
                }
            } catch (Throwable th2) {
            }
        } catch (ZipException unused3) {
            str3 = str4;
            zipFile = null;
            if (zipFile != null) {
            }
            str2 = str3;
            z2 = true;
            if (z2) {
            }
        } catch (EOFException unused4) {
            str3 = str4;
            zipFile2 = null;
            if (zipFile2 != null) {
            }
            str2 = str3;
            z2 = true;
            if (z2) {
            }
        } catch (Throwable th22) {
        }
        try {
            HashMap hashMap = new HashMap();
            Enumeration<? extends ZipEntry> entries = zipFile3.entries();
            str2 = str6;
            while (entries.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) entries.nextElement();
                String name = zipEntry.getName();
                if (name.contains("..")) {
                    try {
                        zipFile3.close();
                    } catch (Throwable unused5) {
                        c.a();
                    }
                    return str6;
                }
                if (TextUtils.isEmpty(name) || !name.startsWith("lib/") || zipEntry.isDirectory() || !z) {
                    str5 = str6;
                } else {
                    String[] split = name.split("/");
                    str5 = str6;
                    if (split.length == 3) {
                        String str7 = split[1];
                        if (!TextUtils.isEmpty(str7)) {
                            List list = (List) hashMap.get(str7);
                            if (list == null) {
                                list = new ArrayList();
                                hashMap.put(str7, list);
                            }
                            list.add(zipEntry);
                        }
                    }
                    str6 = str5;
                }
                try {
                    if (name.endsWith(".dex") && !zipEntry.isDirectory() && !z) {
                        String str8 = apkInfo.dataDir;
                        f(str8);
                        file = new File(str8, apkInfo.key + Constants.ACCEPT_TIME_SEPARATOR_SERVER + apkInfo.versionName + ".dex");
                        try {
                            if (a(zipFile3, zipEntry, file)) {
                                str2 = file.getAbsolutePath();
                            } else if (file.exists()) {
                                file.delete();
                            }
                        } catch (Throwable unused6) {
                            if (file != null && file.exists()) {
                                file.delete();
                            }
                            str6 = str5;
                        }
                    }
                } catch (Throwable unused7) {
                    file = null;
                    file.delete();
                    str6 = str5;
                }
                str6 = str5;
            }
            if (z && hashMap.size() > 0) {
                String a2 = a(apkInfo.hostContext, hashMap.keySet());
                if (!TextUtils.isEmpty(a2)) {
                    File file5 = new File(str, a2);
                    f(file5.getAbsolutePath());
                    List<ZipEntry> list2 = (List) hashMap.get(a2);
                    if (list2 != null && list2.size() > 0) {
                        for (ZipEntry zipEntry2 : list2) {
                            String name2 = zipEntry2.getName();
                            String substring = name2.substring(name2.lastIndexOf("/") + 1);
                            if (!a(zipFile3, zipEntry2, new File(file5, substring.replace(".so", replace + ".so")))) {
                                throw new RuntimeException("UnZip so failed.Report space left.");
                            }
                        }
                    }
                    str2 = file5.getAbsolutePath();
                }
            }
            try {
                zipFile3.close();
            } catch (Throwable unused8) {
                c.a();
            }
            z2 = false;
        } catch (ZipException unused9) {
            zipFile = zipFile3;
            str3 = str2;
            if (zipFile != null) {
            }
            str2 = str3;
            z2 = true;
            if (z2) {
            }
        } catch (EOFException unused10) {
            zipFile2 = zipFile3;
            str3 = str2;
            if (zipFile2 != null) {
            }
            str2 = str3;
            z2 = true;
            if (z2) {
            }
        } catch (Throwable th3) {
        }
        return z2 ? a(apkInfo, str, replace, z) : str2;
        throw th;
    }

    private static String a(Context context, Collection<String> collection) {
        String[] strArr;
        if (Build.VERSION.SDK_INT >= 21) {
            String p = c.p(context);
            if (!TextUtils.isEmpty(p) && collection.contains(p)) {
                return p;
            }
            boolean d2 = c.d();
            if (d2) {
                strArr = Build.SUPPORTED_64_BIT_ABIS;
            } else {
                strArr = Build.SUPPORTED_32_BIT_ABIS;
            }
            if (strArr != null && strArr.length > 0) {
                for (String str : strArr) {
                    if (collection.contains(str)) {
                        return str;
                    }
                }
            }
            if (d2 || !collection.contains("armeabi") || Build.SUPPORTED_ABIS == null || Build.SUPPORTED_ABIS.length <= 0 || "mips".equals(Build.SUPPORTED_ABIS[0])) {
                return "";
            }
            return "armeabi";
        } else if (collection.contains(Build.CPU_ABI)) {
            return Build.CPU_ABI;
        } else {
            if (collection.contains(Build.CPU_ABI2)) {
                return Build.CPU_ABI2;
            }
            if (!collection.contains("armeabi") || "mips".equals(Build.CPU_ABI)) {
                return "";
            }
            return "armeabi";
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:34:0x0041 A[SYNTHETIC, Splitter:B:34:0x0041] */
    /* JADX WARNING: Removed duplicated region for block: B:39:0x004a A[SYNTHETIC, Splitter:B:39:0x004a] */
    private static boolean a(ZipFile zipFile, ZipEntry zipEntry, File file) {
        FileOutputStream fileOutputStream;
        if (zipEntry == null) {
            return false;
        }
        InputStream inputStream = null;
        try {
            InputStream inputStream2 = zipFile.getInputStream(zipEntry);
            try {
                fileOutputStream = new FileOutputStream(file);
            } catch (Throwable unused) {
                fileOutputStream = null;
                inputStream = inputStream2;
                try {
                    c.a();
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (Throwable unused2) {
                            c.a();
                        }
                    }
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (Throwable unused3) {
                            c.a();
                        }
                    }
                    return false;
                } catch (Throwable unused4) {
                    c.a();
                }
            }
            try {
                byte[] bArr = new byte[com.heytap.mcssdk.a.b.a];
                while (true) {
                    int read = inputStream2.read(bArr);
                    if (read <= 0) {
                        break;
                    }
                    fileOutputStream.write(bArr, 0, read);
                }
                fileOutputStream.flush();
                c.a(file.getAbsolutePath(), true);
                if (inputStream2 != null) {
                    try {
                        inputStream2.close();
                    } catch (Throwable unused5) {
                        c.a();
                    }
                }
                try {
                    fileOutputStream.close();
                } catch (Throwable unused6) {
                    c.a();
                }
                return true;
            } catch (Throwable unused7) {
                inputStream = inputStream2;
                c.a();
                if (inputStream != null) {
                }
                if (fileOutputStream != null) {
                }
                return false;
            }
        } catch (Throwable unused8) {
            fileOutputStream = null;
            c.a();
            if (inputStream != null) {
            }
            if (fileOutputStream != null) {
            }
            return false;
        }
        throw th;
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        throw th;
    }

    private static String a(ApkInfo apkInfo, String str, String str2, boolean z) throws Throwable {
        if (apkInfo == null) {
            return "";
        }
        ArrayList arrayList = new ArrayList();
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(apkInfo.pkgPath));
        String str3 = "";
        while (true) {
            try {
                ZipEntry nextEntry = zipInputStream.getNextEntry();
                if (nextEntry != null) {
                    String name = nextEntry.getName();
                    if (name.contains("..")) {
                        try {
                            zipInputStream.close();
                        } catch (Throwable unused) {
                            c.a();
                        }
                        return "";
                    }
                    if (name.startsWith("lib/") && !nextEntry.isDirectory() && z) {
                        String[] split = name.split("/");
                        if (split.length != 3) {
                            zipInputStream.closeEntry();
                        } else {
                            String str4 = split[1];
                            if (TextUtils.isEmpty(str4)) {
                                zipInputStream.closeEntry();
                            } else if (!arrayList.contains(str4)) {
                                arrayList.add(str4);
                            }
                        }
                    }
                    File file = null;
                    try {
                        if (name.endsWith(".dex") && !nextEntry.isDirectory() && !z) {
                            String str5 = apkInfo.dataDir;
                            f(str5);
                            File file2 = new File(str5, apkInfo.key + Constants.ACCEPT_TIME_SEPARATOR_SERVER + apkInfo.versionName + ".dex");
                            try {
                                file2.delete();
                                file2.createNewFile();
                                if (a(file2, zipInputStream)) {
                                    str3 = file2.getAbsolutePath();
                                } else if (file2.exists()) {
                                    file2.delete();
                                }
                            } catch (Throwable unused2) {
                                file = file2;
                                if (file != null && file.exists()) {
                                    file.delete();
                                }
                                zipInputStream.closeEntry();
                            }
                        }
                    } catch (Throwable unused3) {
                        file.delete();
                        zipInputStream.closeEntry();
                    }
                    zipInputStream.closeEntry();
                } else {
                    try {
                        zipInputStream.close();
                    } catch (Throwable unused4) {
                        c.a();
                    }
                    if (z && arrayList.size() > 0) {
                        String a2 = a(apkInfo.hostContext, arrayList);
                        if (!TextUtils.isEmpty(a2)) {
                            File file3 = new File(str, a2);
                            f(file3.getAbsolutePath());
                            ZipInputStream zipInputStream2 = new ZipInputStream(new FileInputStream(apkInfo.pkgPath));
                            while (true) {
                                try {
                                    ZipEntry nextEntry2 = zipInputStream2.getNextEntry();
                                    if (nextEntry2 != null) {
                                        String name2 = nextEntry2.getName();
                                        if (name2.startsWith("lib/") && name2.endsWith(".so") && !nextEntry2.isDirectory()) {
                                            String[] split2 = name2.split("/");
                                            if (split2.length != 3) {
                                                zipInputStream2.closeEntry();
                                            } else if (a2.equals(split2[1])) {
                                                String substring = name2.substring(name2.lastIndexOf("/") + 1);
                                                File file4 = new File(file3, substring.replace(".so", str2 + ".so"));
                                                file4.delete();
                                                file4.createNewFile();
                                                if (a(file4, zipInputStream2)) {
                                                    zipInputStream2.closeEntry();
                                                } else {
                                                    throw new RuntimeException("Stream UnZip so failed.Report space left.");
                                                }
                                            }
                                        }
                                        zipInputStream2.closeEntry();
                                    } else {
                                        str3 = file3.getAbsolutePath();
                                        try {
                                            zipInputStream2.close();
                                            break;
                                        } catch (Throwable unused5) {
                                            c.a();
                                        }
                                    }
                                } catch (Throwable unused6) {
                                    c.a();
                                }
                            }
                        }
                    }
                    return str3;
                }
            } catch (Throwable unused7) {
                c.a();
            }
        }
        throw th;
        throw th;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:18:?, code lost:
        com.baidu.techain.i.c.a();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:19:0x002b, code lost:
        if (r1 != null) goto L_0x002d;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:21:?, code lost:
        r1.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:22:0x0031, code lost:
        com.baidu.techain.i.c.a();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:24:0x0034, code lost:
        return false;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:25:0x0035, code lost:
        r4 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:26:0x0036, code lost:
        if (r1 != null) goto L_0x0038;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:28:?, code lost:
        r1.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:29:0x003c, code lost:
        com.baidu.techain.i.c.a();
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:17:0x0028 */
    private static boolean a(File file, ZipInputStream zipInputStream) {
        FileOutputStream fileOutputStream;
        try {
            fileOutputStream = new FileOutputStream(file);
            byte[] bArr = new byte[com.heytap.mcssdk.a.b.a];
            while (true) {
                int read = zipInputStream.read(bArr);
                if (read <= 0) {
                    break;
                }
                fileOutputStream.write(bArr, 0, read);
            }
            fileOutputStream.flush();
            c.a(file.getAbsolutePath(), true);
            try {
                fileOutputStream.close();
            } catch (Throwable unused) {
                c.a();
            }
            return true;
        } catch (Throwable unused2) {
            fileOutputStream = null;
        }
        throw th;
    }

    public final boolean e(String str) {
        try {
            ApkInfo apkInfo = this.a.get(str);
            if (apkInfo == null) {
                return false;
            }
            try {
                Class<?> a2 = ((e) apkInfo.classLoader).a("com.baidu.techain.engine.EngineImpl");
                Object invoke = a2.getDeclaredMethod("getInstance", Context.class).invoke(a2, f);
                if (invoke != null) {
                    c.a(invoke, "unload", (Class<?>[]) null, new Object[0]);
                }
            } catch (Throwable unused) {
                c.a();
            }
            this.h.remove(apkInfo.pkgPath);
            this.a.remove(str);
            return true;
        } catch (Throwable unused2) {
            c.a();
            return false;
        }
    }
}