package com.pgl.ssdk;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.text.TextUtils;
import com.pgl.ssdk.ces.out.PglSSCallBack;
import com.pgl.ssdk.ces.out.PglSSManager;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.json.JSONException;
import org.json.JSONObject;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static long f24764a = 28800;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static int f24765b = 5;

    public static long a(long j10, InputStream inputStream) throws IOException {
        if (j10 != -1) {
            return j10;
        }
        byte[] bArr = new byte[8192];
        long j11 = 0;
        while (true) {
            int i10 = inputStream.read(bArr);
            if (i10 == -1) {
                return j11;
            }
            j11 += (long) i10;
        }
    }

    public static long b() {
        Context contextA = z.a();
        if (contextA == null) {
            return 0L;
        }
        long jA = aw.a(contextA, "pgl_frt", 0L);
        if (jA != 0) {
            return jA;
        }
        long jCurrentTimeMillis = System.currentTimeMillis() / 1000;
        aw.b(contextA, "pgl_frt", jCurrentTimeMillis);
        return jCurrentTimeMillis;
    }

    /* JADX WARN: Unreachable blocks removed: 2, instructions: 2 */
    public static synchronized void c() {
        PglSSCallBack pglCallBack;
        try {
            if (!d()) {
                return;
            }
            String strE = v.e();
            if (TextUtils.isEmpty(strE)) {
                return;
            }
            File file = new File(strE);
            if (file.exists()) {
                JSONObject jSONObjectA = a(file, true);
                jSONObjectA.put("apk_info", v.c());
                jSONObjectA.put("app_name", ab.d(z.a()));
                PglSSManager pglSSManager = PglSSManager.getInstance();
                if (pglSSManager != null && (pglCallBack = pglSSManager.getPglCallBack()) != null) {
                    pglCallBack.reportSoftDecData("detailed_app_info", jSONObjectA.toString());
                }
            }
        } catch (Throwable unused) {
        }
    }

    private static boolean d() {
        if (aw.a(z.a(), "pgl_is_hit", 0) > 0) {
            return false;
        }
        if ((System.currentTimeMillis() / 1000) - b() < f24764a) {
            return false;
        }
        aw.b(z.a(), "pgl_is_hit", 1);
        return e();
    }

    private static boolean e() {
        boolean z10 = f24765b == 100;
        return !z10 ? ((int) ((Math.random() * 100.0d) + 1.0d)) <= f24765b : z10;
    }

    public static JSONObject a() throws JSONException {
        String[] strArr;
        Context contextA = z.a();
        ArrayList arrayList = new ArrayList();
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        if (contextA == null) {
            return jSONObject;
        }
        ApplicationInfo applicationInfo = contextA.getApplicationInfo();
        String str = applicationInfo.nativeLibraryDir;
        if (!TextUtils.isEmpty(str)) {
            File file = new File(str);
            if (file.exists() && file.isDirectory()) {
                File[] fileArrListFiles = file.listFiles();
                if (fileArrListFiles != null && fileArrListFiles.length > 0) {
                    for (File file2 : fileArrListFiles) {
                        arrayList.add(file2.getName());
                    }
                }
                a(jSONObject2, "arm64".equals(file.getName()) ? "v8a" : "v7a", arrayList, false);
                if (jSONObject2.length() > 0) {
                    jSONObject.put("lib", jSONObject2);
                }
            }
        }
        if (!arrayList.isEmpty() || (strArr = applicationInfo.splitPublicSourceDirs) == null) {
            return jSONObject;
        }
        for (String str2 : strArr) {
            if (str2.endsWith("armeabi-v7a.apk") || str2.endsWith("arm64_v8a.apk")) {
                File file3 = new File(str2);
                return file3.exists() ? a(file3, false) : jSONObject;
            }
        }
        return jSONObject;
    }

    /* JADX WARN: Can't wrap try/catch for region: R(8:0|2|(2:90|3)|(13:92|4|(4:7|(3:95|9|(8:97|13|(5:15|(1:17)|59|43|101)(1:18)|19|(4:24|(2:44|(2:49|(2:54|(1:58))(1:53))(1:48))(2:28|(3:34|(2:39|(1:41))(1:38)|42))|43|101)(1:23)|59|43|101)(1:100))(1:99)|98|5)|94|60|(1:62)|(1:66)|67|(1:69)|70|(1:74)|75|(1:79))|88|84|85|(1:(0))) */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static JSONObject a(File file, boolean z10) {
        ZipFile zipFile;
        ArrayList arrayList;
        String str;
        ArrayList arrayList2;
        String str2;
        long jA;
        JSONObject jSONObject = new JSONObject();
        ArrayList arrayList3 = new ArrayList();
        ArrayList arrayList4 = new ArrayList();
        ArrayList arrayList5 = new ArrayList();
        ArrayList arrayList6 = new ArrayList();
        ArrayList arrayList7 = new ArrayList();
        ArrayList arrayList8 = new ArrayList();
        JSONObject jSONObject2 = new JSONObject();
        try {
            zipFile = new ZipFile(file);
        } catch (Throwable unused) {
            zipFile = null;
        }
        try {
            Enumeration<? extends ZipEntry> enumerationEntries = zipFile.entries();
            while (enumerationEntries.hasMoreElements()) {
                ZipEntry zipEntryNextElement = enumerationEntries.nextElement();
                if (zipEntryNextElement != null) {
                    String name = zipEntryNextElement.getName();
                    if (!TextUtils.isEmpty(name) || !zipEntryNextElement.isDirectory()) {
                        String[] strArrSplit = name.split("/");
                        long size = zipEntryNextElement.getSize();
                        Enumeration<? extends ZipEntry> enumeration = enumerationEntries;
                        if (name.startsWith("res/drawable/")) {
                            arrayList = arrayList5;
                            if (name.length() > 13) {
                                str = strArrSplit[2];
                                arrayList2 = arrayList4;
                            }
                            arrayList2.add(str);
                            enumerationEntries = enumeration;
                            arrayList5 = arrayList;
                        } else {
                            arrayList = arrayList5;
                        }
                        if (name.startsWith("res/") && strArrSplit.length == 2) {
                            str = strArrSplit[1];
                            arrayList2 = arrayList;
                        } else {
                            if (name.startsWith("assets/") && strArrSplit.length >= 2) {
                                arrayList3.add(strArrSplit[1]);
                                if (name.startsWith("assets/assets/resources/native/") && strArrSplit.length >= 6 && jSONObject2.length() < 10) {
                                    if (!TextUtils.isEmpty(strArrSplit[4]) && !jSONObject2.has(strArrSplit[4])) {
                                        str2 = strArrSplit[4];
                                        jA = a(size, zipFile.getInputStream(zipEntryNextElement));
                                    } else if (jSONObject2.has(strArrSplit[4])) {
                                        long j10 = jSONObject2.getLong(strArrSplit[4]);
                                        str2 = strArrSplit[4];
                                        jA = j10 + a(size, zipFile.getInputStream(zipEntryNextElement));
                                    }
                                    jSONObject2.put(str2, jA);
                                }
                            } else if (name.startsWith("lib/armeabi/") && name.length() > 12) {
                                str = strArrSplit[2];
                                arrayList2 = arrayList6;
                            } else if (name.startsWith("lib/armeabi-v7a/") && name.length() > 16) {
                                str = strArrSplit[2];
                                arrayList2 = arrayList7;
                            } else if (name.startsWith("lib/arm64-v8a/") && name.length() > 14) {
                                str = strArrSplit[2];
                                arrayList2 = arrayList8;
                            }
                            enumerationEntries = enumeration;
                            arrayList5 = arrayList;
                        }
                        arrayList2.add(str);
                        enumerationEntries = enumeration;
                        arrayList5 = arrayList;
                    }
                }
            }
            ArrayList arrayList9 = arrayList5;
            JSONObject jSONObject3 = new JSONObject();
            a(jSONObject3, "eabi", arrayList6, false);
            a(jSONObject3, "v7a", arrayList7, false);
            a(jSONObject3, "v8a", arrayList8, false);
            if (jSONObject3.length() > 0) {
                jSONObject.put("lib", jSONObject3);
            }
            if (z10 && jSONObject3.length() <= 0) {
                jSONObject = a();
            }
            if (jSONObject2.length() > 0) {
                jSONObject.put("cocos", jSONObject2);
            }
            a(jSONObject, "assets", arrayList3, false);
            JSONObject jSONObjectA = a(arrayList4);
            if (jSONObjectA != null && jSONObjectA.length() > 0) {
                jSONObject.put("drawable", jSONObjectA);
            }
            JSONObject jSONObjectA2 = a(arrayList9);
            if (jSONObjectA2 != null && jSONObjectA2.length() > 0) {
                jSONObject.put("res", jSONObjectA2);
            }
        } catch (Throwable unused2) {
            if (zipFile != null) {
            }
            return jSONObject;
        }
        zipFile.close();
        return jSONObject;
    }

    private static JSONObject a(List<String> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        JSONObject jSONObject = new JSONObject();
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        ArrayList arrayList3 = new ArrayList();
        for (String str : list) {
            if (str.endsWith(".png")) {
                arrayList2.add(str.substring(0, str.lastIndexOf(".png")));
            } else if (str.endsWith(".xml")) {
                arrayList.add(str.substring(0, str.lastIndexOf(".xml")));
            } else {
                arrayList3.add(str);
            }
        }
        a(jSONObject, "xml", arrayList, true);
        a(jSONObject, "png", arrayList2, true);
        a(jSONObject, "other", arrayList3, true);
        return jSONObject;
    }

    public static void a(JSONObject jSONObject, String str, List<String> list, boolean z10) {
        if (jSONObject == null || list == null || list.isEmpty()) {
            return;
        }
        if (z10) {
            try {
                jSONObject.put(str + "_size", list.size());
            } catch (JSONException unused) {
                return;
            }
        }
        jSONObject.put(str, n.a(list));
    }
}
