package com.apm.insight.k;

import android.content.Context;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.apm.insight.CrashType;
import com.apm.insight.Npth;
import com.apm.insight.entity.Header;
import com.apm.insight.entity.b;
import com.apm.insight.k.c;
import com.apm.insight.l.n;
import com.apm.insight.nativecrash.NativeImpl;
import com.apm.insight.runtime.m;
import com.apm.insight.runtime.o;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

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

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private Context f5254a;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private C0004b f5258f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private HashMap<String, C0004b> f5259g;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private List<File> f5255c = new ArrayList();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private List<File> f5256d = new ArrayList();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private int f5257e = -1;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private volatile boolean f5260h = false;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private Runnable f5261i = new Runnable() { // from class: com.apm.insight.k.b.1
        @Override // java.lang.Runnable
        public final void run() throws Throwable {
            b.this.f();
        }
    };

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private Runnable f5262j = new Runnable() { // from class: com.apm.insight.k.b.2
        @Override // java.lang.Runnable
        public final void run() throws Throwable {
            b.this.d();
        }
    };

    /* JADX INFO: renamed from: com.apm.insight.k.b$b, reason: collision with other inner class name */
    public static class C0004b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        String f5277a;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        a f5280d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        a f5281e;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        List<a> f5278b = new ArrayList();

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        List<a> f5279c = new ArrayList();

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        boolean f5282f = false;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        int f5283g = 0;

        public C0004b(String str) {
            this.f5277a = str;
        }
    }

    private b(Context context) {
        this.f5254a = context;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void d() throws Throwable {
        if (this.f5260h || this.f5259g == null) {
            return;
        }
        if (!com.apm.insight.l.k.b(this.f5254a)) {
            e();
        }
        if (this.f5257e == -1) {
            if (com.apm.insight.runtime.a.a() && com.apm.insight.runtime.a.f()) {
                this.f5257e = 1;
            } else {
                this.f5257e = 0;
            }
        }
        boolean z10 = this.f5257e == 1;
        com.apm.insight.runtime.f fVar = new com.apm.insight.runtime.f(this.f5254a);
        Iterator<C0004b> it = this.f5259g.values().iterator();
        while (it.hasNext()) {
            b(it.next(), z10, fVar);
        }
        Iterator<C0004b> it2 = this.f5259g.values().iterator();
        while (it2.hasNext()) {
            a(it2.next(), z10, fVar);
        }
        Iterator<C0004b> it3 = this.f5259g.values().iterator();
        while (it3.hasNext()) {
            com.apm.insight.l.f.a(com.apm.insight.l.j.a(this.f5254a, it3.next().f5277a));
        }
        fVar.a();
        com.apm.insight.runtime.b.a();
        e();
    }

    private void e() {
        this.f5260h = true;
        this.f5259g = null;
        NativeImpl.j();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void f() throws Throwable {
        if (this.f5260h) {
            return;
        }
        if (!com.apm.insight.l.k.b(this.f5254a) || (System.currentTimeMillis() - com.apm.insight.e.j() <= 5000 && com.apm.insight.e.i().isApmExists() && !Npth.hasCrash())) {
            m.a().a(this.f5261i, 5000L);
        } else {
            d();
        }
    }

    private void g() {
        File[] fileArrListFiles = com.apm.insight.l.j.i(this.f5254a).listFiles();
        if (fileArrListFiles == null) {
            return;
        }
        for (int i10 = 0; i10 < fileArrListFiles.length && i10 < 5; i10++) {
            File file = fileArrListFiles[i10];
            if (file.getName().endsWith(".atmp")) {
                com.apm.insight.a.a.a();
                file.getAbsolutePath();
            } else {
                try {
                    com.apm.insight.f.b bVarC = com.apm.insight.l.f.c(file.getAbsolutePath());
                    if (bVarC != null) {
                        if (bVarC.b() != null) {
                            bVarC.b().put("upload_scene", "launch_scan");
                        }
                        if (e.a(e.d(), bVarC.e(), bVarC.d(), bVarC.f(), bVarC.g())) {
                            com.apm.insight.l.f.a(file);
                            String strC = bVarC.c();
                            if (!TextUtils.isEmpty(strC)) {
                                com.apm.insight.l.f.a(new File(strC));
                            }
                        }
                    } else {
                        com.apm.insight.l.f.a(file);
                    }
                } catch (Throwable th) {
                    com.apm.insight.c.a();
                    com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                }
            }
        }
    }

    public final boolean c() {
        return this.f5260h;
    }

    public static class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        File f5272a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        long f5273b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        long f5274c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        @Nullable
        CrashType f5275d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        String f5276e;

        public a(File file, @Nullable CrashType crashType) {
            this.f5273b = -1L;
            this.f5274c = -1L;
            this.f5272a = file;
            this.f5275d = crashType;
            this.f5276e = file.getName();
        }

        public a(File file, long j10, @Nullable CrashType crashType) {
            this.f5274c = -1L;
            this.f5272a = file;
            this.f5273b = j10;
            this.f5275d = crashType;
            this.f5276e = file.getName();
        }
    }

    public static b a() {
        if (f5253b == null) {
            synchronized (b.class) {
                try {
                    if (f5253b == null) {
                        f5253b = new b(com.apm.insight.e.g());
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        return f5253b;
    }

    public final void b() {
        try {
            if (!this.f5260h && com.apm.insight.l.a.b(com.apm.insight.e.g())) {
                m.a().a(this.f5262j);
            }
        } catch (Throwable unused) {
        }
    }

    private void b(HashMap<String, C0004b> map) {
        File[] fileArrListFiles = com.apm.insight.l.j.d(this.f5254a).listFiles();
        if (fileArrListFiles == null || fileArrListFiles.length == 0) {
            return;
        }
        for (int i10 = 0; i10 < fileArrListFiles.length && i10 < 5; i10++) {
            File file = fileArrListFiles[i10];
            try {
                if (!file.isDirectory()) {
                    com.apm.insight.l.f.a(file);
                } else if (file.getName().endsWith("G")) {
                    String name = file.getName();
                    C0004b c0004b = map.get(name);
                    if (c0004b == null) {
                        c0004b = new C0004b(name);
                        map.put(name, c0004b);
                    }
                    c0004b.f5279c.add(new a(file, CrashType.NATIVE));
                } else {
                    com.apm.insight.l.f.a(file);
                }
            } catch (Throwable th) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                com.apm.insight.l.f.a(file);
            }
        }
    }

    public final void a(boolean z10) throws Throwable {
        if (!Npth.isStopUpload() && z10) {
            if (this.f5258f == null) {
                this.f5258f = new C0004b("old_uuid");
                HashMap<String, C0004b> map = new HashMap<>();
                this.f5259g = map;
                a(map);
                a(this.f5259g, this.f5258f);
                com.apm.insight.l.f.a(com.apm.insight.l.j.b(this.f5254a));
                b(this.f5259g);
                b(this.f5258f, true, null);
                a(this.f5258f, true, null);
                this.f5258f = null;
                if (this.f5259g.isEmpty()) {
                    e();
                } else {
                    f();
                }
            }
            g();
            com.apm.insight.a.c();
        }
    }

    private void a(HashMap<String, C0004b> map) {
        File[] fileArrListFiles = com.apm.insight.l.j.f(this.f5254a).listFiles();
        if (fileArrListFiles == null || fileArrListFiles.length == 0) {
            return;
        }
        for (int i10 = 0; i10 < fileArrListFiles.length && i10 < 5; i10++) {
            File file = fileArrListFiles[i10];
            try {
                if (!file.isDirectory()) {
                    com.apm.insight.l.f.a(file);
                } else if (file.getName().endsWith("G")) {
                    String name = file.getName();
                    C0004b c0004b = map.get(name);
                    if (c0004b == null) {
                        c0004b = new C0004b(name);
                        map.put(name, c0004b);
                    }
                    JSONArray jSONArrayA = com.apm.insight.nativecrash.b.a(com.apm.insight.l.j.l(file), com.apm.insight.l.j.m(file));
                    int length = jSONArrayA.length();
                    c0004b.f5283g = length;
                    if (length > 0) {
                        try {
                            com.apm.insight.l.f.a(com.apm.insight.l.j.n(file), jSONArrayA);
                        } catch (Throwable unused) {
                        }
                    }
                } else {
                    com.apm.insight.l.f.a(file);
                }
            } catch (Throwable th) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                com.apm.insight.l.f.a(file);
            }
        }
    }

    private void b(final C0004b c0004b, boolean z10, @Nullable com.apm.insight.runtime.f fVar) {
        Iterator<a> it;
        boolean z11;
        JSONObject jSONObjectA;
        if (c0004b.f5279c.size() <= 1 && c0004b.f5279c.isEmpty()) {
            c0004b.f5281e = c0004b.f5280d;
            return;
        }
        boolean zB = com.apm.insight.l.k.b(this.f5254a);
        c0004b.f5281e = c0004b.f5280d;
        com.apm.insight.nativecrash.a aVar = new com.apm.insight.nativecrash.a(this.f5254a);
        Iterator<a> it2 = c0004b.f5279c.iterator();
        while (it2.hasNext()) {
            a next = it2.next();
            final File file = next.f5272a;
            try {
                aVar.a(file);
                jSONObjectA = a(aVar);
            } catch (Throwable th) {
                th = th;
                it = it2;
            }
            if (jSONObjectA != null && jSONObjectA.length() != 0) {
                if (jSONObjectA.length() != 0) {
                    if (!z10) {
                        long jOptLong = jSONObjectA.optLong("crash_time");
                        try {
                            a aVar2 = c0004b.f5281e;
                            if (aVar2 == null) {
                                c0004b.f5281e = next;
                                c0004b.f5282f = true;
                                if (fVar == null || fVar.a("default")) {
                                    it = it2;
                                    z11 = zB;
                                } else {
                                    aVar.i();
                                }
                            } else {
                                it = it2;
                                try {
                                    if (c0004b.f5282f) {
                                        z11 = zB;
                                    } else {
                                        z11 = zB;
                                        try {
                                            if (jOptLong < aVar2.f5273b) {
                                                c0004b.f5281e = next;
                                                if (fVar != null && !fVar.a("default")) {
                                                    aVar.i();
                                                    zB = z11;
                                                    it2 = it;
                                                } else {
                                                    a(file);
                                                    c0004b.f5282f = true;
                                                }
                                            }
                                        } catch (Throwable th2) {
                                            th = th2;
                                            com.apm.insight.c.a();
                                            com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                                            com.apm.insight.l.f.a(file);
                                            zB = z11;
                                            it2 = it;
                                        }
                                    }
                                    com.apm.insight.entity.a.a(jSONObjectA, "filters", "aid", String.valueOf(jSONObjectA.optJSONObject("header").opt("aid")));
                                    jSONObjectA.optJSONObject("header").put("aid", 2010);
                                } catch (Throwable th3) {
                                    th = th3;
                                    z11 = zB;
                                }
                            }
                        } catch (Throwable th4) {
                            th = th4;
                            it = it2;
                            z11 = zB;
                            com.apm.insight.c.a();
                            com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                            com.apm.insight.l.f.a(file);
                            zB = z11;
                            it2 = it;
                        }
                    } else {
                        it = it2;
                        z11 = zB;
                        if (fVar != null && !fVar.a("default")) {
                            aVar.i();
                        }
                    }
                    com.apm.insight.entity.a.a(jSONObjectA, "filters", "start_uuid", c0004b.f5277a);
                    com.apm.insight.entity.a.a(jSONObjectA, "filters", "crash_thread_name", jSONObjectA.optString("crash_thread_name", "unknown"));
                    if (z11) {
                        try {
                            c.a aVar3 = new c.a(jSONObjectA, CrashType.NATIVE);
                            com.apm.insight.entity.b.a(jSONObjectA, com.apm.insight.entity.b.a(aVar3.c(), aVar3.a(), o.a().b(aVar3.b() == -1 ? System.currentTimeMillis() : aVar3.b())), new b.a() { // from class: com.apm.insight.k.b.4
                                @Override // com.apm.insight.entity.b.a
                                public final void a(JSONObject jSONObject) {
                                    d.a();
                                    d.a(jSONObject, file, com.apm.insight.l.j.a(b.this.f5254a, c0004b.f5277a));
                                }
                            });
                            if (!aVar.i()) {
                                aVar.f();
                            }
                        } catch (Throwable th5) {
                            th = th5;
                            com.apm.insight.c.a();
                            com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                            com.apm.insight.l.f.a(file);
                        }
                    }
                    c.a(CrashType.NATIVE, jSONObjectA);
                } else {
                    it = it2;
                    z11 = zB;
                }
                zB = z11;
                it2 = it;
            } else {
                it = it2;
                z11 = zB;
                aVar.i();
                zB = z11;
                it2 = it;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:47:0x00c4  */
    /* JADX WARN: Removed duplicated region for block: B:49:0x00c7  */
    /* JADX WARN: Removed duplicated region for block: B:56:0x00d5 A[Catch: all -> 0x0036, TryCatch #1 {all -> 0x0036, blocks: (B:9:0x0023, B:11:0x0031, B:14:0x0039, B:17:0x0041, B:20:0x0051, B:22:0x0057, B:23:0x005c, B:25:0x0068, B:27:0x0073, B:31:0x008d, B:57:0x00d7, B:59:0x00df, B:60:0x00e7, B:62:0x00f2, B:70:0x010a, B:65:0x00fc, B:67:0x0100, B:69:0x0108, B:54:0x00cf, B:55:0x00d2, B:56:0x00d5, B:38:0x00a6, B:41:0x00b0, B:44:0x00ba, B:71:0x0110, B:72:0x012d, B:29:0x007f), top: B:79:0x0023, inners: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00df A[Catch: all -> 0x0036, TryCatch #1 {all -> 0x0036, blocks: (B:9:0x0023, B:11:0x0031, B:14:0x0039, B:17:0x0041, B:20:0x0051, B:22:0x0057, B:23:0x005c, B:25:0x0068, B:27:0x0073, B:31:0x008d, B:57:0x00d7, B:59:0x00df, B:60:0x00e7, B:62:0x00f2, B:70:0x010a, B:65:0x00fc, B:67:0x0100, B:69:0x0108, B:54:0x00cf, B:55:0x00d2, B:56:0x00d5, B:38:0x00a6, B:41:0x00b0, B:44:0x00ba, B:71:0x0110, B:72:0x012d, B:29:0x007f), top: B:79:0x0023, inners: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:62:0x00f2 A[Catch: all -> 0x0036, TryCatch #1 {all -> 0x0036, blocks: (B:9:0x0023, B:11:0x0031, B:14:0x0039, B:17:0x0041, B:20:0x0051, B:22:0x0057, B:23:0x005c, B:25:0x0068, B:27:0x0073, B:31:0x008d, B:57:0x00d7, B:59:0x00df, B:60:0x00e7, B:62:0x00f2, B:70:0x010a, B:65:0x00fc, B:67:0x0100, B:69:0x0108, B:54:0x00cf, B:55:0x00d2, B:56:0x00d5, B:38:0x00a6, B:41:0x00b0, B:44:0x00ba, B:71:0x0110, B:72:0x012d, B:29:0x007f), top: B:79:0x0023, inners: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:64:0x00fa A[ADDED_TO_REGION] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private void a(HashMap<String, C0004b> map, C0004b c0004b) {
        byte b10;
        CrashType crashType;
        C0004b c0004b2;
        a aVar;
        a aVar2;
        File[] fileArrListFiles = com.apm.insight.l.j.a(this.f5254a).listFiles();
        if (fileArrListFiles == null) {
            return;
        }
        Arrays.sort(fileArrListFiles, Collections.reverseOrder());
        char c6 = 0;
        int i10 = 0;
        while (i10 < fileArrListFiles.length) {
            File file = fileArrListFiles[i10];
            try {
                if (com.apm.insight.e.a.a().a(file.getAbsolutePath())) {
                    com.apm.insight.l.f.a(file);
                } else if (!com.apm.insight.l.f.e(file) && !com.apm.insight.g.a.a().b(file.getName())) {
                    if (file.isFile()) {
                        com.apm.insight.l.f.a(file);
                    } else {
                        String name = file.getName();
                        if (name.endsWith("G")) {
                            String[] strArrSplit = name.split("_");
                            if (strArrSplit.length < 5) {
                                c0004b.f5278b.add(new a(file, null));
                            } else {
                                try {
                                    long j10 = Long.parseLong(strArrSplit[c6]);
                                    long j11 = Long.parseLong(strArrSplit[4]);
                                    String str = strArrSplit[2];
                                    String str2 = strArrSplit[1];
                                    int iHashCode = str2.hashCode();
                                    if (iHashCode == -1109843021) {
                                        if (str2.equals("launch")) {
                                            b10 = 0;
                                        }
                                        if (b10 == 0) {
                                        }
                                        c0004b2 = map.get(str);
                                        if (c0004b2 == null) {
                                        }
                                        aVar = new a(file, j10, crashType);
                                        aVar.f5274c = j11;
                                        aVar2 = c0004b2.f5280d;
                                        if (aVar2 == null) {
                                        }
                                    } else if (iHashCode != 96741) {
                                        b10 = (iHashCode == 3254818 && str2.equals("java")) ? (byte) 1 : (byte) -1;
                                        if (b10 == 0) {
                                            crashType = CrashType.LAUNCH;
                                        } else if (b10 != 1) {
                                            crashType = b10 != 2 ? null : CrashType.ANR;
                                        } else {
                                            crashType = CrashType.JAVA;
                                        }
                                        c0004b2 = map.get(str);
                                        if (c0004b2 == null) {
                                            c0004b2 = new C0004b(str);
                                            map.put(str, c0004b2);
                                        }
                                        aVar = new a(file, j10, crashType);
                                        aVar.f5274c = j11;
                                        aVar2 = c0004b2.f5280d;
                                        if ((aVar2 == null || aVar2.f5273b > aVar.f5273b) && crashType != null && crashType != CrashType.ANR && !name.contains("ignore")) {
                                            c0004b2.f5280d = aVar;
                                        }
                                        c0004b2.f5278b.add(aVar);
                                    } else {
                                        if (str2.equals("anr")) {
                                            b10 = 2;
                                        }
                                        if (b10 == 0) {
                                        }
                                        c0004b2 = map.get(str);
                                        if (c0004b2 == null) {
                                        }
                                        aVar = new a(file, j10, crashType);
                                        aVar.f5274c = j11;
                                        aVar2 = c0004b2.f5280d;
                                        if (aVar2 == null) {
                                            c0004b2.f5280d = aVar;
                                            c0004b2.f5278b.add(aVar);
                                        } else {
                                            c0004b2.f5280d = aVar;
                                            c0004b2.f5278b.add(aVar);
                                        }
                                    }
                                } catch (Throwable unused) {
                                    c0004b.f5278b.add(new a(file, null));
                                    com.apm.insight.c.a();
                                    com.apm.insight.runtime.j.a(new RuntimeException("err format crashTime:".concat(name)), "NPTH_CATCH");
                                }
                            }
                        } else {
                            com.apm.insight.l.f.a(file);
                        }
                    }
                }
            } catch (Throwable th) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
            }
            i10++;
            c6 = 0;
        }
    }

    private static JSONObject a(com.apm.insight.nativecrash.a aVar) {
        JSONObject jSONObjectB = aVar.b();
        if (jSONObjectB != null && jSONObjectB.length() != 0) {
            return jSONObjectB;
        }
        if (com.apm.insight.e.d()) {
            aVar.j();
        }
        if (!aVar.a()) {
            aVar.i();
            return null;
        }
        if (!aVar.d()) {
            aVar.i();
            return null;
        }
        if (aVar.e()) {
            aVar.i();
            return null;
        }
        aVar.c();
        return aVar.h();
    }

    private void a(final C0004b c0004b, boolean z10, @Nullable com.apm.insight.runtime.f fVar) {
        Iterator<a> it;
        final File file;
        CrashType crashType;
        if (c0004b.f5278b.isEmpty()) {
            return;
        }
        if (c0004b.f5281e == null) {
            c0004b.f5281e = c0004b.f5280d;
        }
        for (Iterator<a> it2 = c0004b.f5278b.iterator(); it2.hasNext(); it2 = it) {
            a next = it2.next();
            try {
                File file2 = next.f5272a;
                CrashType crashType2 = next.f5275d;
                it = it2;
                try {
                    final com.apm.insight.f.b bVarA = a(file2, crashType2, c0004b.f5277a, next.f5273b, next.f5274c);
                    if (bVarA == null) {
                        com.apm.insight.l.f.a(file2);
                    } else {
                        JSONObject jSONObjectB = bVarA.b();
                        if (jSONObjectB == null) {
                            com.apm.insight.l.f.a(file2);
                        } else {
                            JSONObject jSONObjectOptJSONObject = jSONObjectB.optJSONObject("header");
                            if (jSONObjectOptJSONObject == null) {
                                com.apm.insight.l.f.a(file2);
                            } else {
                                if (crashType2 == null) {
                                    file = file2;
                                    if (new File(file, file2.getName()).exists() || file.getName().split("_").length < 5) {
                                        if (e.b(bVarA.a(), jSONObjectB.toString()).a()) {
                                            com.apm.insight.l.f.a(file);
                                        }
                                    }
                                } else {
                                    file = file2;
                                }
                                File fileA = com.apm.insight.entity.b.a(file);
                                if (!fileA.exists()) {
                                    com.apm.insight.l.f.a(file);
                                } else {
                                    try {
                                        JSONArray jSONArray = new JSONArray(com.apm.insight.l.f.a(fileA, "\n"));
                                        JSONObject jSONObjectOptJSONObject2 = crashType2 == CrashType.LAUNCH ? ((JSONArray) jSONObjectB.opt("data")).optJSONObject(0) : jSONObjectB;
                                        if ((!z10 && c0004b.f5281e != next) || next.f5276e.contains("ignore")) {
                                            try {
                                                com.apm.insight.entity.a.a(jSONObjectOptJSONObject2, "filters", "aid", String.valueOf(jSONObjectOptJSONObject.opt("aid")));
                                                crashType = crashType2;
                                                try {
                                                    com.apm.insight.entity.a.a(jSONObjectOptJSONObject2, "filters", "has_ignore", String.valueOf(next.f5276e.contains("ignore")));
                                                    jSONObjectOptJSONObject.put("aid", 2010);
                                                } catch (Throwable th) {
                                                    th = th;
                                                    com.apm.insight.c.a();
                                                    com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                                                }
                                            } catch (Throwable th2) {
                                                th = th2;
                                                crashType = crashType2;
                                            }
                                        } else {
                                            crashType = crashType2;
                                            if (fVar != null && !fVar.a(jSONObjectOptJSONObject2.optString("crash_md5", "default"))) {
                                                com.apm.insight.l.f.a(next.f5272a);
                                            }
                                        }
                                        com.apm.insight.entity.a.a(jSONObjectOptJSONObject2, "filters", "start_uuid", c0004b.f5277a);
                                        com.apm.insight.entity.a.a(jSONObjectOptJSONObject2, "filters", "leak_threads_count", String.valueOf(c0004b.f5283g));
                                        com.apm.insight.entity.a.a(jSONObjectOptJSONObject2, "filters", "crash_thread_name", jSONObjectOptJSONObject2.optString("crash_thread_name", "unknown"));
                                        try {
                                            com.apm.insight.entity.b.a(jSONObjectB, jSONArray, new b.a() { // from class: com.apm.insight.k.b.3
                                                @Override // com.apm.insight.entity.b.a
                                                public final void a(JSONObject jSONObject) {
                                                    e.a(bVarA.a(), jSONObject.toString(), new File(file, "logZip"), com.apm.insight.l.j.a(b.this.f5254a, c0004b.f5277a));
                                                }
                                            });
                                            if (!com.apm.insight.l.f.a(file)) {
                                                com.apm.insight.e.a.a().a(com.apm.insight.d.a.b(file.getAbsolutePath()));
                                            }
                                            c.a(crashType, jSONObjectB);
                                        } catch (Throwable th3) {
                                            th = th3;
                                            com.apm.insight.c.a();
                                            com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                                            com.apm.insight.l.f.a(next.f5272a);
                                        }
                                    } catch (Throwable unused) {
                                    }
                                }
                            }
                        }
                    }
                } catch (Throwable th4) {
                    th = th4;
                    com.apm.insight.c.a();
                    com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                    com.apm.insight.l.f.a(next.f5272a);
                }
            } catch (Throwable th5) {
                th = th5;
                it = it2;
            }
        }
    }

    private static boolean a(File file) {
        String[] list = file.list();
        if (list == null) {
            return false;
        }
        for (String str : list) {
            if (!TextUtils.isEmpty(str) && str.endsWith("")) {
                return true;
            }
        }
        return false;
    }

    @Nullable
    private com.apm.insight.f.b a(File file, CrashType crashType, String str, long j10, long j11) {
        com.apm.insight.f.b bVar;
        try {
            try {
                if (file.isFile()) {
                    com.apm.insight.l.f.a(file);
                    return null;
                }
                boolean z10 = crashType == CrashType.LAUNCH;
                if (crashType == null) {
                    try {
                        return com.apm.insight.l.f.b(new File(file, file.getName()).getAbsolutePath());
                    } catch (Throwable th) {
                        th = th;
                        bVar = null;
                        com.apm.insight.l.f.a(file);
                        com.apm.insight.c.a();
                        com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                        return bVar;
                    }
                }
                com.apm.insight.f.b bVarA = com.apm.insight.l.f.a(file, crashType);
                try {
                    JSONObject jSONObjectB = bVarA.b();
                    try {
                        if (bVarA.b() != null) {
                            if (crashType == CrashType.ANR) {
                                return bVarA;
                            }
                            jSONObjectB.put("crash_time", j10);
                            jSONObjectB.put("app_start_time", j11);
                            JSONObject jSONObjectOptJSONObject = jSONObjectB.optJSONObject("header");
                            if (jSONObjectOptJSONObject == null) {
                                jSONObjectOptJSONObject = Header.a(j10).f();
                            } else if (z10) {
                                jSONObjectB.remove("header");
                            }
                            String strOptString = jSONObjectOptJSONObject.optString("sdk_version_name", null);
                            if (strOptString == null) {
                                strOptString = "2.0.0.8";
                            }
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "sdk_version", strOptString);
                            JSONArray jSONArrayOptJSONArray = jSONObjectB.optJSONArray("logcat");
                            if (jSONArrayOptJSONArray == null || jSONArrayOptJSONArray.length() == 0) {
                                jSONObjectB.put("logcat", com.apm.insight.runtime.h.a(str));
                            }
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "has_dump", "true");
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "has_logcat", String.valueOf(!com.apm.insight.a.a(jSONObjectB, "logcat")));
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "memory_leak", String.valueOf(com.apm.insight.entity.a.a(str)));
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "fd_leak", String.valueOf(com.apm.insight.entity.a.b(str)));
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "threads_leak", String.valueOf(com.apm.insight.entity.a.c(str)));
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "is_64_devices", String.valueOf(Header.a()));
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "is_64_runtime", String.valueOf(NativeImpl.f()));
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "is_x86_devices", String.valueOf(Header.b()));
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "has_meminfo_file", String.valueOf(com.apm.insight.l.j.d(str).exists()));
                            com.apm.insight.entity.a.a(jSONObjectB, "filters", "is_root", String.valueOf(com.apm.insight.nativecrash.a.k()));
                            jSONObjectB.put("launch_did", com.apm.insight.i.a.a(this.f5254a));
                            jSONObjectB.put("crash_uuid", file.getName());
                            try {
                                long j12 = Long.parseLong(com.apm.insight.runtime.b.a(j10, str));
                                com.apm.insight.entity.a.a(jSONObjectB, "filters", "lastAliveTime", Math.abs(j12 - j10) < 60000 ? "< 60s" : "> 60s");
                                jSONObjectB.put("lastAliveTime", String.valueOf(j12));
                            } catch (Throwable unused) {
                                jSONObjectB.put("lastAliveTime", "unknown");
                                com.apm.insight.entity.a.a(jSONObjectB, "filters", "lastAliveTime", "unknown");
                            }
                            jSONObjectB.put("has_dump", "true");
                            if (jSONObjectB.opt("storage") == null) {
                                com.apm.insight.e.g();
                                com.apm.insight.entity.a.a(jSONObjectB, n.a());
                            }
                            if (Header.b(jSONObjectOptJSONObject)) {
                                com.apm.insight.entity.a.a(jSONObjectB, "filters", "unauthentic_version", "unauthentic_version");
                            }
                            com.apm.insight.entity.d.b(jSONObjectB);
                            bVarA.b().put("upload_scene", "launch_scan");
                            if (z10) {
                                JSONObject jSONObject = new JSONObject();
                                jSONObjectB.put("event_type", "start_crash");
                                jSONObjectB.put("stack", jSONObjectB.remove("data"));
                                jSONObject.put("data", new JSONArray().put(jSONObjectB));
                                jSONObject.put("header", jSONObjectOptJSONObject);
                                bVarA.a(jSONObject);
                            } else {
                                jSONObjectB.put("isJava", 1);
                            }
                        } else {
                            com.apm.insight.l.f.a(file);
                        }
                        return bVarA;
                    } catch (Throwable th2) {
                        th = th2;
                        bVar = bVarA;
                        com.apm.insight.l.f.a(file);
                        com.apm.insight.c.a();
                        com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                        return bVar;
                    }
                } catch (Throwable th3) {
                    th = th3;
                }
            } catch (Throwable th4) {
                th = th4;
            }
        } catch (Throwable th5) {
            th = th5;
            bVar = null;
        }
        com.apm.insight.l.f.a(file);
        com.apm.insight.c.a();
        com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
        return bVar;
    }
}
