package com.apm.insight.b;

import android.content.Context;
import android.os.Process;
import android.os.SystemClock;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.dash.DashMediaSource;
import com.apm.insight.CrashType;
import com.apm.insight.ICrashCallback;
import com.apm.insight.Npth;
import com.apm.insight.entity.Header;
import com.apm.insight.entity.b;
import com.apm.insight.l.c;
import com.apm.insight.l.m;
import com.apm.insight.nativecrash.NativeImpl;
import com.apm.insight.runtime.l;
import com.apm.insight.runtime.n;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.google.android.gms.measurement.internal.u1;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    private static volatile boolean f5004z = true;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Context f5006b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private volatile boolean f5007c;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private JSONObject f5011g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private JSONObject f5012h;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private JSONArray f5017m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private JSONObject f5018n;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private JSONArray f5021q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private JSONArray f5022r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    private JSONObject f5023s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private boolean f5024t;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private volatile boolean f5026v;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private long f5008d = -1;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private File f5009e = null;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private boolean f5010f = true;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private String f5013i = "unknown";

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private String f5014j = "unknown";

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private String f5015k = "unknown";

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private String f5016l = "npth_inner_default";

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private int f5019o = 0;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private long f5020p = -1;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private final Object f5025u = new Object();

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private long f5027w = -1;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private long f5028x = 0;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private final Runnable f5029y = new Runnable() { // from class: com.apm.insight.b.b.1
        @Override // java.lang.Runnable
        public final void run() {
            try {
                b.this.g();
            } catch (Throwable th) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
            }
        }
    };
    private int A = 0;
    private List<Pattern> B = null;
    private Pattern C = null;
    private File D = null;

    public b(Context context) {
        this.f5006b = context;
    }

    private static String a(float f10) {
        return f10 <= 0.0f ? "0%" : f10 <= 0.1f ? "0% - 10%" : f10 <= 0.3f ? "10% - 30%" : f10 <= 0.6f ? "30% - 60%" : f10 <= 0.9f ? "60% - 90%" : "90% - 100%";
    }

    private static boolean h() {
        boolean z10 = !com.apm.insight.l.a.a();
        if (!z10 || com.apm.insight.runtime.a.b.d().e() > ExoPlayer.DEFAULT_DETACH_SURFACE_TIMEOUT_MS) {
            return z10;
        }
        return false;
    }

    private File i() {
        if (this.D == null) {
            this.D = new File(this.f5006b.getFilesDir(), "has_anr_signal_" + com.apm.insight.l.a.b().replaceAll(":", "_"));
        }
        return this.D;
    }

    public final void b() {
        if (this.f5007c) {
            this.f5007c = false;
            c cVar = this.f5005a;
            if (cVar != null) {
                cVar.b();
            }
            this.f5005a = null;
        }
    }

    public final void c() {
        c cVar = this.f5005a;
        if (cVar != null) {
            cVar.a();
        }
    }

    public final void d() {
        synchronized (this.f5025u) {
        }
        this.f5029y.run();
    }

    public final void e() {
        if (NativeImpl.h()) {
            try {
                com.apm.insight.l.f.a(i(), String.valueOf(this.A + 1), false);
            } catch (Throwable th) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
            }
        }
        this.f5027w = SystemClock.uptimeMillis();
        this.f5026v = true;
    }

    public final void f() {
        File fileI = i();
        try {
            int iIntValue = Integer.decode(com.apm.insight.l.f.a(fileI.getAbsolutePath(), "\n")).intValue();
            this.A = iIntValue;
            if (iIntValue >= 2) {
                NativeImpl.a(false);
            } else {
                NativeImpl.a(true);
            }
        } catch (IOException unused) {
            NativeImpl.a(true);
        } catch (Throwable unused2) {
            com.apm.insight.l.f.a(fileI);
        }
    }

    public final boolean g() {
        String str;
        String str2;
        JSONArray jSONArrayB;
        JSONArray jSONArrayA;
        JSONObject jSONObject;
        boolean z10;
        JSONObject jSONObjectA;
        boolean z11;
        String str3;
        String str4;
        JSONArray jSONArray;
        final long j10;
        JSONObject jSONObjectA2;
        JSONObject jSONObject2;
        JSONObject jSONObject3;
        JSONObject jSONObject4;
        boolean zB = com.apm.insight.runtime.i.a().b();
        long jUptimeMillis = SystemClock.uptimeMillis();
        a(jUptimeMillis);
        String strA = d.a(this.f5006b);
        long jCurrentTimeMillis = System.currentTimeMillis();
        String str5 = "normal";
        TextUtils.isEmpty(strA);
        if (TextUtils.isEmpty(strA)) {
            str = "unknown";
            str2 = "unknown";
            jSONArrayB = null;
            jSONArrayA = null;
            jSONObject = null;
            z10 = false;
            jSONObjectA = null;
            z11 = false;
            str3 = "normal";
            str4 = "unknown";
            jSONArray = null;
            j10 = jCurrentTimeMillis;
            jSONObjectA2 = null;
        } else {
            synchronized (this.f5025u) {
            }
            if (this.f5011g == null || System.currentTimeMillis() - this.f5008d > 20000) {
                if (this.f5026v) {
                    this.f5026v = false;
                    str5 = "trace_after";
                }
                b(jUptimeMillis);
            } else {
                str5 = "trace_last";
            }
            JSONObject jSONObject5 = this.f5011g;
            String str6 = this.f5013i;
            String str7 = this.f5014j;
            String str8 = this.f5015k;
            JSONArray jSONArray2 = this.f5017m;
            JSONArray jSONArray3 = this.f5022r;
            JSONArray jSONArray4 = this.f5021q;
            JSONObject jSONObject6 = this.f5023s;
            JSONObject jSONObject7 = this.f5012h;
            z11 = this.f5024t;
            long j11 = this.f5020p;
            this.f5011g = null;
            this.f5017m = null;
            this.f5021q = null;
            this.f5012h = null;
            this.f5022r = null;
            this.f5013i = "unknown";
            this.f5014j = "unknown";
            this.f5015k = "unknown";
            this.f5019o = 0;
            jSONArrayA = jSONArray4;
            str2 = str8;
            str4 = str6;
            jSONArray = jSONArray2;
            jSONObjectA = jSONObject7;
            jSONObject = jSONObject6;
            jSONArrayB = jSONArray3;
            str = str7;
            str3 = str5;
            j10 = j11;
            jSONObjectA2 = jSONObject5;
            z10 = true;
        }
        if (TextUtils.isEmpty(strA)) {
            if (this.f5011g == null || System.currentTimeMillis() - this.f5008d <= 20000) {
                if (this.f5011g == null || System.currentTimeMillis() - this.f5008d <= ExoPlayer.DEFAULT_DETACH_SURFACE_TIMEOUT_MS || !NativeImpl.h()) {
                    return false;
                }
                com.apm.insight.l.f.a(i());
                return false;
            }
            this.f5011g = null;
            this.f5017m = null;
            this.f5021q = null;
            this.f5012h = null;
            this.f5022r = null;
            this.f5013i = "unknown";
            this.f5014j = "unknown";
            this.f5015k = "unknown";
            this.f5019o = 0;
            this.f5009e = null;
            return false;
        }
        if (jSONObjectA2 == null) {
            if (jSONArrayA == null) {
                try {
                    jSONArrayB = f.b().b();
                    jSONArrayA = j.a(jUptimeMillis);
                    jSONObject3 = jSONObjectA2;
                    try {
                        jSONObjectA = f.b().a(jUptimeMillis).a();
                        jSONObject4 = new JSONObject();
                    } catch (Throwable unused) {
                    }
                    try {
                        com.apm.insight.l.a.a(this.f5006b, jSONObject4);
                        jSONObject = jSONObject4;
                    } catch (Throwable unused2) {
                        jSONObject = jSONObject4;
                        jSONObjectA2 = jSONObject3;
                    }
                } catch (Throwable unused3) {
                    jSONObject3 = jSONObjectA2;
                }
            } else {
                jSONObject3 = jSONObjectA2;
            }
            jSONObjectA2 = d.a();
        }
        JSONObject jSONObject8 = jSONObjectA;
        if (jSONObjectA2 != null && jSONObjectA2.length() > 0) {
            try {
                jSONObjectA2.put("pid", Process.myPid());
                jSONObjectA2.put("package", this.f5006b.getPackageName());
                jSONObjectA2.put("is_remote_process", 0);
                jSONObjectA2.put("is_new_stack", 10);
                com.apm.insight.entity.a aVar = new com.apm.insight.entity.a(new JSONObject());
                String str9 = str2;
                aVar.a("data", (Object) jSONObjectA2.toString());
                JSONObject jSONObject9 = jSONObjectA2;
                aVar.a("is_anr", (Object) 1);
                aVar.a("anrType", (Object) str3);
                if (com.apm.insight.e.x()) {
                    aVar.a("history_message", (Object) jSONArrayB);
                    aVar.a("current_message", jSONObject8);
                    aVar.a("pending_messages", (Object) jSONArrayA);
                }
                aVar.a("anr_time", Long.valueOf(System.currentTimeMillis()));
                aVar.a("crash_time", Long.valueOf(j10));
                com.apm.insight.c.a.b();
                aVar.c(jSONObject);
                aVar.a("anr_info", (Object) strA);
                if (jSONArray != null) {
                    aVar.a("dump_trace", (Object) jSONArray);
                }
                JSONObject jSONObject10 = this.f5018n;
                aVar.a("all_thread_stacks", (jSONObject10 == null || jSONObject10.length() == 0) ? m.b((String) null) : this.f5018n);
                com.apm.insight.runtime.a.f fVarA = com.apm.insight.runtime.a.f.a();
                CrashType crashType = CrashType.ANR;
                com.apm.insight.entity.a aVarA = fVarA.a(crashType, aVar);
                aVarA.a("is_background", Boolean.valueOf(z11));
                aVarA.a("logcat", (Object) com.apm.insight.runtime.h.a(com.apm.insight.e.f()));
                aVarA.a("has_dump", (Object) "true");
                aVarA.a("crash_uuid", (Object) com.apm.insight.e.a(j10, crashType, false, false));
                JSONObject jSONObjectOptJSONObject = aVarA.c().optJSONObject("filters");
                com.apm.insight.entity.d.b(aVarA.c());
                if (jSONObjectOptJSONObject == null) {
                    try {
                        jSONObject2 = new JSONObject();
                        try {
                            aVarA.a("filters", jSONObject2);
                        } catch (Throwable unused4) {
                            jSONObjectOptJSONObject = jSONObject2;
                            jSONObject2 = jSONObjectOptJSONObject;
                        }
                    } catch (Throwable unused5) {
                        jSONObject2 = jSONObjectOptJSONObject;
                    }
                } else {
                    jSONObject2 = jSONObjectOptJSONObject;
                }
                jSONObject2.put("anrType", str3);
                jSONObject2.put("max_utm_thread", str4);
                jSONObject2.put("max_stm_thread", str);
                jSONObject2.put("max_utm_stm_thread", str9);
                jSONObject2.put("max_utm_thread_version", this.f5016l);
                long j12 = j10 - com.apm.insight.e.j();
                jSONObject2.put("crash_length", j12 < DashMediaSource.DEFAULT_FALLBACK_TARGET_LIVE_OFFSET_MS ? "0 - 30s" : j12 < 60000 ? "30s - 1min" : j12 < 120000 ? "1min - 2min" : j12 < 300000 ? "2min - 5min" : j12 < TTAdConstant.AD_MAX_EVENT_TIME ? "5min - 10min" : j12 < 1800000 ? "10min - 30min" : j12 < 3600000 ? "30min - 1h" : "1h - ");
                jSONObject2.put("disable_looper_monitor", String.valueOf(com.apm.insight.runtime.a.c()));
                jSONObject2.put("npth_force_apm_crash", String.valueOf(com.apm.insight.c.a.b()));
                jSONObject2.put("sdk_version", "2.0.0.8");
                jSONObject2.put("has_logcat", String.valueOf(aVarA.a()));
                jSONObject2.put("memory_leak", String.valueOf(com.apm.insight.entity.a.a(com.apm.insight.e.f())));
                jSONObject2.put("fd_leak", String.valueOf(com.apm.insight.entity.a.b(com.apm.insight.e.f())));
                jSONObject2.put("threads_leak", String.valueOf(com.apm.insight.entity.a.c(com.apm.insight.e.f())));
                jSONObject2.put("is_64_devices", String.valueOf(Header.a()));
                jSONObject2.put("is_64_runtime", String.valueOf(NativeImpl.f()));
                jSONObject2.put("is_x86_devices", String.valueOf(Header.b()));
                jSONObject2.put("has_meminfo_file", String.valueOf(com.apm.insight.l.j.d(com.apm.insight.e.f()).exists()));
                jSONObject2.put("is_root", com.apm.insight.nativecrash.a.k() ? "true" : "false");
                jSONObject2.put("anr_normal_trace", String.valueOf(!this.f5026v));
                jSONObject2.put("anr_no_run", String.valueOf(zB));
                jSONObject2.put("crash_after_crash", Npth.hasCrash() ? "true" : "false");
                jSONObject2.put("from_file", String.valueOf(d.b()));
                jSONObject2.put("has_dump", "true");
                jSONObject2.put("from_kill", "false");
                jSONObject2.put("last_resume_activity", com.apm.insight.runtime.a.b.d().h());
                int i10 = this.f5019o;
                if (i10 > 0) {
                    jSONObject2.put("may_have_stack_overflow", String.valueOf(i10));
                }
                try {
                    a(strA, jSONObject2);
                } catch (Throwable th) {
                    com.apm.insight.c.a();
                    com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                }
                com.apm.insight.a.a.a();
                CrashType crashType2 = CrashType.LAUNCH;
                com.apm.insight.e.e();
                try {
                    if (com.apm.insight.a.e().length() > 1024) {
                        aVarA.a("has_system_traces", "true");
                    }
                } catch (Throwable unused6) {
                }
                try {
                    JSONArray jSONArrayA2 = com.apm.insight.nativecrash.b.a(com.apm.insight.l.j.e(com.apm.insight.e.f()), com.apm.insight.l.j.f(com.apm.insight.e.f()));
                    jSONObject2.put("leak_threads_count", String.valueOf(jSONArrayA2.length()));
                    if (jSONArrayA2.length() > 0) {
                        com.apm.insight.l.f.a(com.apm.insight.l.j.g(com.apm.insight.e.f()), jSONArrayA2);
                    }
                } catch (Throwable unused7) {
                }
                String strOptString = jSONObject9.optString("mainStackFromTrace");
                JSONArray jSONArrayA3 = com.apm.insight.entity.b.a(strOptString);
                com.apm.insight.entity.b.a(aVarA.c(), jSONArrayA3, new b.a() { // from class: com.apm.insight.b.b.2
                    @Override // com.apm.insight.entity.b.a
                    public final void a(JSONObject jSONObject11) {
                        com.apm.insight.k.d.a().a(jSONObject11, j10, b.this.f5010f);
                    }
                });
                b(strOptString, jSONArrayA3);
            } catch (Throwable th2) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th2, "NPTH_CATCH");
            }
        }
        return z10;
    }

    private void b(long j10) {
        if (this.f5028x != this.f5027w) {
            try {
                this.f5020p = System.currentTimeMillis();
                if (com.apm.insight.e.x()) {
                    this.f5022r = f.b().b();
                    this.f5021q = j.a(j10);
                    this.f5012h = f.b().a(j10).a();
                }
                JSONObject jSONObject = new JSONObject();
                this.f5023s = jSONObject;
                com.apm.insight.l.a.a(this.f5006b, jSONObject);
                this.f5024t = h();
                this.f5010f = !Npth.hasCrash();
            } catch (Throwable unused) {
            }
            try {
                this.f5008d = this.f5020p;
                String strB = com.apm.insight.l.j.b();
                File file = new File(new File(com.apm.insight.l.j.f(this.f5006b), strB), "trace_" + com.apm.insight.l.a.b().replace(':', '_') + ".txt");
                file.getParentFile().mkdirs();
                com.apm.insight.l.f.a(file, com.apm.insight.l.b.a().format(new Date(System.currentTimeMillis())) + "\n", false);
                n.a("anr_trace", strB);
                NativeImpl.i(file.getAbsolutePath());
                try {
                    JSONArray jSONArrayA = com.apm.insight.l.f.a(file.getAbsolutePath());
                    this.f5017m = jSONArrayA;
                    a(jSONArrayA);
                } catch (IOException unused2) {
                } catch (Throwable th) {
                    com.apm.insight.c.a();
                    com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                }
                if (this.f5011g == null) {
                    this.f5011g = d.a();
                }
            } catch (Throwable th2) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th2, "NPTH_CATCH");
            }
            com.apm.insight.a.d();
        } else {
            try {
                this.f5008d = this.f5020p;
                String strB2 = com.apm.insight.l.j.b();
                File file2 = new File(new File(com.apm.insight.l.j.f(this.f5006b), strB2), "trace" + com.apm.insight.l.a.b().replace(':', '_') + ".txt");
                file2.getParentFile().mkdirs();
                com.apm.insight.l.f.a(file2, com.apm.insight.l.b.a().format(new Date(System.currentTimeMillis())) + "\n", false);
                n.a("anr_trace", strB2);
                NativeImpl.i(file2.getAbsolutePath());
                try {
                    JSONArray jSONArrayA2 = com.apm.insight.l.f.a(file2.getAbsolutePath());
                    this.f5017m = jSONArrayA2;
                    a(jSONArrayA2);
                } catch (IOException unused3) {
                } catch (Throwable th3) {
                    com.apm.insight.c.a();
                    com.apm.insight.runtime.j.a(th3, "NPTH_CATCH");
                }
                if (this.f5011g == null) {
                    this.f5011g = d.a();
                }
            } catch (Throwable th4) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th4, "NPTH_CATCH");
            }
        }
        long j11 = this.f5027w;
        this.f5028x = j11;
        this.f5027w = -1L;
        if (j11 == -1) {
            this.f5028x = (-1) - 1;
        }
    }

    private JSONObject c(@NonNull JSONArray jSONArray) {
        JSONObject jSONObject = new JSONObject();
        JSONArray jSONArrayB = com.apm.insight.a.b(jSONArray);
        if (jSONArrayB.length() != jSONArray.length()) {
            this.f5019o++;
        }
        try {
            jSONObject.put("thread_number", 1);
            StringBuilder sb = new StringBuilder();
            for (int i10 = 0; i10 < jSONArrayB.length(); i10++) {
                sb.append(jSONArrayB.getString(i10));
                sb.append('\n');
            }
            jSONObject.put("mainStackFromTrace", sb.toString());
            return jSONObject;
        } catch (JSONException unused) {
            return null;
        }
    }

    public final void a() {
        if (this.f5007c) {
            return;
        }
        this.f5005a = new c(this);
        this.f5008d = com.apm.insight.e.j();
        this.f5007c = true;
    }

    private boolean a(long j10) {
        if (this.f5026v) {
            this.f5026v = false;
            b(j10);
        }
        return false;
    }

    private void a(JSONArray jSONArray) {
        int[] iArrB;
        int[] iArrB2;
        if (jSONArray == null) {
            return;
        }
        this.f5011g = null;
        this.f5018n = null;
        this.f5019o = 0;
        JSONArray jSONArray2 = new JSONArray();
        JSONArray jSONArray3 = new JSONArray();
        JSONArray jSONArray4 = new JSONArray();
        this.f5013i = "unknown";
        this.f5014j = "unknown";
        this.f5015k = "unknown";
        int[] iArr = {0, 0, 0};
        JSONArray jSONArray5 = jSONArray4;
        String string = null;
        boolean z10 = false;
        for (int i10 = 0; i10 < jSONArray.length(); i10++) {
            String strOptString = jSONArray.optString(i10);
            if (TextUtils.isEmpty(strOptString)) {
                if (jSONArray5.length() > 0 && !TextUtils.isEmpty(string)) {
                    if (this.f5011g == null && "main".equals(string)) {
                        this.f5011g = c(jSONArray5);
                    } else {
                        jSONArray2.put(a(string, jSONArray5));
                    }
                    try {
                        if (!"main".equals(string)) {
                            string = string.substring(0, string.indexOf(40)).trim();
                        }
                    } catch (Throwable unused) {
                    }
                    String str = string;
                    if (!a(str)) {
                        try {
                            iArrB2 = b(jSONArray5);
                        } catch (IllegalArgumentException e10) {
                            com.apm.insight.c.a();
                            com.apm.insight.runtime.j.a(e10, "NPTH_CATCH");
                            iArrB2 = null;
                        } catch (Throwable unused2) {
                            iArrB2 = null;
                        }
                        if (iArrB2 != null) {
                            int i11 = iArrB2[0];
                            if (i11 > iArr[0]) {
                                iArr[0] = i11;
                                this.f5013i = str;
                            }
                            int i12 = iArrB2[1];
                            if (i12 > iArr[1]) {
                                iArr[1] = i12;
                                this.f5014j = str;
                            }
                            int i13 = iArrB2[2];
                            if (i13 > iArr[2]) {
                                iArr[2] = i13;
                                this.f5015k = str;
                            }
                        }
                    }
                }
                if (jSONArray5.length() > 0) {
                    jSONArray5 = new JSONArray();
                }
                string = null;
            } else if (!z10) {
                if (strOptString.startsWith("DALVIK THREADS") || strOptString.startsWith("suspend") || strOptString.startsWith("\"")) {
                    z10 = true;
                }
                jSONArray3.put(strOptString);
            } else if (z10) {
                if (strOptString.contains(" prio=")) {
                    if (jSONArray5.length() > 0 && !TextUtils.isEmpty(string)) {
                        if (this.f5011g == null && "main".equals(string)) {
                            this.f5011g = c(jSONArray5);
                        } else {
                            jSONArray2.put(a(string, jSONArray5));
                        }
                        try {
                            if (!"main".equals(string)) {
                                string = string.substring(0, string.indexOf(40)).trim();
                            }
                        } catch (Throwable unused3) {
                        }
                        String str2 = string;
                        if (!a(str2)) {
                            try {
                                iArrB = b(jSONArray5);
                            } catch (IllegalArgumentException e11) {
                                com.apm.insight.c.a();
                                com.apm.insight.runtime.j.a(e11, "NPTH_CATCH");
                                iArrB = null;
                            } catch (Throwable unused4) {
                                iArrB = null;
                            }
                            if (iArrB != null) {
                                int i14 = iArrB[0];
                                if (i14 > iArr[0]) {
                                    iArr[0] = i14;
                                    this.f5013i = str2;
                                }
                                int i15 = iArrB[1];
                                if (i15 > iArr[1]) {
                                    iArr[1] = i15;
                                    this.f5014j = str2;
                                }
                                int i16 = iArrB[2];
                                if (i16 > iArr[2]) {
                                    iArr[2] = i16;
                                    this.f5015k = str2;
                                }
                            }
                        }
                    }
                    String strSubstring = strOptString.substring(1, strOptString.indexOf(34, 1));
                    if ("main".equals(strSubstring)) {
                        string = strSubstring;
                    } else {
                        StringBuilder sbD = u.a.d(strSubstring, "  (");
                        sbD.append(strOptString.substring(strOptString.indexOf(34, 2) + 1));
                        sbD.append(" )");
                        string = sbD.toString();
                    }
                    if (jSONArray5.length() > 0) {
                        jSONArray5 = new JSONArray();
                    }
                    jSONArray5.put(strOptString);
                } else if (TextUtils.isEmpty(string)) {
                    jSONArray3.put(strOptString);
                } else {
                    jSONArray5.put(strOptString);
                }
            }
        }
        if (jSONArray2.length() > 0) {
            this.f5017m = jSONArray3;
            try {
                JSONObject jSONObject = new JSONObject();
                this.f5018n = jSONObject;
                jSONObject.put("thread_all_count", jSONArray2.length());
                this.f5018n.put("thread_stacks", jSONArray2);
            } catch (JSONException unused5) {
            }
        }
    }

    @Nullable
    private int[] b(@NonNull JSONArray jSONArray) throws IllegalArgumentException {
        for (int i10 = 0; i10 < jSONArray.length(); i10++) {
            String strOptString = jSONArray.optString(i10);
            int iIndexOf = (strOptString == null || strOptString.isEmpty()) ? -1 : strOptString.indexOf("utm=");
            if (iIndexOf > 0) {
                if (this.C == null) {
                    this.C = Pattern.compile("[^0-9]+");
                }
                String[] strArrSplit = this.C.split(strOptString.substring(iIndexOf));
                if (strArrSplit == null || strArrSplit.length < 2) {
                    return null;
                }
                int iIntValue = Integer.decode(strArrSplit[1]).intValue();
                int iIntValue2 = Integer.decode(strArrSplit[2]).intValue();
                return new int[]{iIntValue, iIntValue2, iIntValue + iIntValue2};
            }
        }
        return null;
    }

    private static String b(float f10) {
        return a(f10 / 100.0f);
    }

    private static void b(String str, JSONArray jSONArray) {
        for (ICrashCallback iCrashCallback : l.a().e()) {
            try {
                if (iCrashCallback instanceof com.apm.insight.b) {
                    ((com.apm.insight.b) iCrashCallback).a(CrashType.ANR, str, (Thread) null, jSONArray);
                } else {
                    iCrashCallback.onCrash(CrashType.ANR, str, null);
                }
            } catch (Throwable th) {
                com.apm.insight.c.a();
                com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
            }
        }
    }

    private boolean a(String str) {
        if (this.B == null) {
            JSONArray jSONArrayB = com.apm.insight.runtime.a.b();
            if (jSONArrayB != null) {
                this.B = new LinkedList();
                this.f5016l = jSONArrayB.optString(0);
                for (int i10 = 1; i10 < jSONArrayB.length(); i10++) {
                    try {
                        this.B.add(Pattern.compile(jSONArrayB.optString(i10)));
                    } catch (Throwable unused) {
                    }
                }
            }
            if (this.B == null) {
                LinkedList linkedList = new LinkedList();
                this.B = linkedList;
                linkedList.add(Pattern.compile("^main$"));
                this.B.add(Pattern.compile("^default_npth_thread$"));
                this.B.add(Pattern.compile("^RenderThread$"));
                this.B.add(Pattern.compile("^Jit thread pool worker thread.*$"));
            }
        }
        Iterator<Pattern> it = this.B.iterator();
        while (it.hasNext()) {
            if (it.next().matcher(str).matches()) {
                return true;
            }
        }
        return false;
    }

    private JSONObject a(String str, JSONArray jSONArray) {
        JSONObject jSONObject = new JSONObject();
        JSONArray jSONArrayB = com.apm.insight.a.b(jSONArray);
        if (jSONArrayB.length() != jSONArray.length()) {
            this.f5019o++;
        }
        try {
            jSONObject.put("thread_name", str);
            jSONObject.put("thread_stack", jSONArrayB);
            return jSONObject;
        } catch (JSONException unused) {
            return null;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:111:0x01ed, code lost:
    
        if (r8 != 5) goto L135;
     */
    /* JADX WARN: Removed duplicated region for block: B:100:0x01da  */
    /* JADX WARN: Removed duplicated region for block: B:125:0x0223  */
    /* JADX WARN: Removed duplicated region for block: B:126:0x0226 A[PHI: r28
      0x0226: PHI (r28v3 char) = (r28v2 char), (r28v18 char) binds: [B:124:0x0221, B:114:0x01f4] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:128:0x0230  */
    /* JADX WARN: Removed duplicated region for block: B:129:0x0233 A[PHI: r28
      0x0233: PHI (r28v4 char) = (r28v3 char), (r28v19 char) binds: [B:127:0x022e, B:113:0x01f1] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:131:0x023b  */
    /* JADX WARN: Removed duplicated region for block: B:132:0x023d A[PHI: r28
      0x023d: PHI (r28v15 char) = (r28v4 char), (r28v20 char) binds: [B:130:0x0239, B:111:0x01ed] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:137:0x024c  */
    /* JADX WARN: Removed duplicated region for block: B:151:0x029a  */
    /* JADX WARN: Removed duplicated region for block: B:153:0x02a3 A[LOOP:2: B:98:0x01d7->B:153:0x02a3, LOOP_END] */
    /* JADX WARN: Removed duplicated region for block: B:181:0x0341  */
    /* JADX WARN: Removed duplicated region for block: B:184:0x0352  */
    /* JADX WARN: Removed duplicated region for block: B:237:0x03b3 A[EDGE_INSN: B:237:0x03b3->B:202:0x03b3 BREAK  A[LOOP:0: B:3:0x0038->B:204:0x03c5], SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:239:0x03c5 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:241:0x037b A[EDGE_INSN: B:241:0x037b->B:192:0x037b BREAK  A[LOOP:2: B:98:0x01d7->B:153:0x02a3], SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private void a(String str, JSONObject jSONObject) throws JSONException {
        HashMap map;
        HashMap map2;
        String[] strArr;
        int i10;
        String str2;
        char c6;
        char c10;
        char c11;
        int i11;
        boolean z10;
        String str3;
        String strTrim;
        String string;
        HashMap map3;
        String str4;
        int i12;
        char c12;
        String[] strArr2;
        String str5;
        char c13;
        b bVar = this;
        SystemClock.uptimeMillis();
        String[] strArrSplit = str.split("\n");
        float[] fArr = {-1.0f, -1.0f, -1.0f};
        HashMap map4 = new HashMap();
        HashMap map5 = new HashMap();
        HashMap map6 = new HashMap();
        HashMap map7 = new HashMap();
        HashMap map8 = new HashMap();
        int length = strArrSplit.length;
        String str6 = "unknown";
        String strTrim2 = "unknown";
        String str7 = strTrim2;
        int i13 = 0;
        char c14 = 0;
        boolean z11 = false;
        while (true) {
            if (i13 >= length) {
                map = map7;
                map2 = map8;
                break;
            }
            String strTrim3 = strArrSplit[i13];
            if (!TextUtils.isEmpty(strTrim3)) {
                strArr = strArrSplit;
                i10 = length;
                if (c14 != 0) {
                    str2 = str6;
                    if (c14 == 1) {
                        map = map7;
                        map2 = map8;
                        c10 = c14;
                        i11 = 0;
                        strTrim3 = strTrim3.trim();
                        String lowerCase = strTrim3.toLowerCase();
                        if (lowerCase.startsWith("shortmsg")) {
                            strTrim3.substring(strTrim3.indexOf(58));
                            z10 = false;
                        } else if (lowerCase.startsWith("reason:")) {
                            strTrim3.substring(strTrim3.indexOf(58));
                            z10 = true;
                        } else {
                            c6 = 3;
                            if (lowerCase.contains("appfreeze")) {
                                str7 = "AppFreeze";
                                c14 = '\n';
                            } else {
                                c14 = c10;
                            }
                            c11 = 4;
                        }
                        if (lowerCase.contains("input dispatch")) {
                            str3 = "Input dispatching timed out";
                        } else if (lowerCase.contains("broadcast of intent")) {
                            str3 = "Broadcast of Intent";
                        } else {
                            if (lowerCase.contains("executing service")) {
                                if ("null".equalsIgnoreCase(strTrim2)) {
                                    strTrim2 = strTrim3.substring(strTrim3.indexOf("service ") + 8).trim();
                                }
                                str7 = "executing service";
                            } else if (lowerCase.contains("service.startforeground")) {
                                str3 = "not call Service.startForeground";
                            } else {
                                str7 = str2;
                            }
                            if (z10) {
                                c11 = 4;
                                c6 = 3;
                                c14 = 2;
                            }
                            strTrim = strTrim3.trim();
                            if (strTrim.startsWith("Load:")) {
                            }
                            c6 = 3;
                            c14 = c10;
                            c11 = 4;
                        }
                        str7 = str3;
                        if (z10) {
                        }
                        strTrim = strTrim3.trim();
                        if (strTrim.startsWith("Load:")) {
                        }
                        c6 = 3;
                        c14 = c10;
                        c11 = 4;
                    } else if (c14 == 2) {
                        map = map7;
                        map2 = map8;
                        c10 = c14;
                        i11 = 0;
                        strTrim = strTrim3.trim();
                        if (strTrim.startsWith("Load:")) {
                            String[] strArrSplit2 = strTrim.replace("Load:", "").trim().split("/");
                            c6 = 3;
                            if (3 == strArrSplit2.length) {
                                for (int i14 = i11; i14 < strArrSplit2.length; i14++) {
                                    fArr[i14] = Float.valueOf(strArrSplit2[i14]).floatValue();
                                }
                            }
                            c14 = 3;
                            c11 = 4;
                        }
                        c6 = 3;
                        c14 = c10;
                        c11 = 4;
                    } else if (c14 != 3) {
                        map = map7;
                        map2 = map8;
                        c6 = 3;
                        c10 = c14;
                        c14 = c10;
                        c11 = 4;
                    } else {
                        String[] strArrSplit3 = strTrim3.split("\\s");
                        c10 = c14;
                        if (strArrSplit3.length >= 2) {
                            if ("CPU".equalsIgnoreCase(strArrSplit3[0]) && "usage".equalsIgnoreCase(strArrSplit3[1])) {
                                if (strTrim3.contains("ago")) {
                                    z11 = true;
                                }
                                if (map4.isEmpty() && map5.isEmpty() && map6.isEmpty() && map8.isEmpty() && map7.isEmpty()) {
                                    map = map7;
                                    map2 = map8;
                                    c14 = c10;
                                    c11 = 4;
                                    c6 = 3;
                                }
                            } else if (map4.isEmpty() || map5.isEmpty() || map6.isEmpty() || map8.isEmpty() || map7.isEmpty()) {
                                if (map4.isEmpty() && strArrSplit3[1].equalsIgnoreCase("TOTAL:")) {
                                    string = "";
                                    map3 = map4;
                                } else if (strTrim3.contains(bVar.f5006b.getPackageName())) {
                                    string = "";
                                    int i15 = 0;
                                    while (i15 < strArrSplit3.length) {
                                        if (strArrSplit3[i15].contains(bVar.f5006b.getPackageName())) {
                                            StringBuilder sb = new StringBuilder();
                                            String str8 = strArrSplit3[i15];
                                            sb.append(str8.substring(str8.indexOf(47) + 1, strArrSplit3[i15].length() - 1));
                                            sb.append('_');
                                            string = sb.toString();
                                        }
                                        i15++;
                                        bVar = this;
                                    }
                                    map3 = map6;
                                } else if (map5.isEmpty() && strTrim3.contains("system_server:")) {
                                    string = "";
                                    map3 = map5;
                                } else if (map8.isEmpty() && strTrim3.contains("kswapd")) {
                                    string = "";
                                    map3 = map8;
                                } else if (map7.isEmpty() && strTrim3.contains("dex2oat")) {
                                    string = "";
                                    map3 = map7;
                                } else {
                                    string = "";
                                    map3 = null;
                                }
                                if (map3 != null) {
                                    int i16 = 0;
                                    try {
                                        do {
                                            str4 = "%";
                                            if (!strArrSplit3[i16].contains("%")) {
                                                i16++;
                                            }
                                            break;
                                        } while (i16 < strArrSplit3.length);
                                        break;
                                        float fFloatValue = Float.valueOf(strArrSplit3[i16].replace("%", "")).floatValue();
                                        map = map7;
                                        try {
                                            String str9 = string + "total";
                                            if (map3 == map4) {
                                                map2 = map8;
                                            } else {
                                                map2 = map8;
                                                try {
                                                    fFloatValue /= com.apm.insight.l.d.d();
                                                } catch (Throwable unused) {
                                                    map3.put(u1.j(string, "total"), Float.valueOf(-1.0f));
                                                }
                                            }
                                            map3.put(str9, Float.valueOf(fFloatValue));
                                        } catch (Throwable unused2) {
                                            map2 = map8;
                                            map3.put(u1.j(string, "total"), Float.valueOf(-1.0f));
                                            i12 = i16 + 3;
                                            char c15 = 0;
                                            while (i12 < strArrSplit3.length) {
                                            }
                                            c6 = 3;
                                            c14 = c10;
                                            c11 = 4;
                                            if (c14 < c11) {
                                            }
                                        }
                                    } catch (Throwable unused3) {
                                        map = map7;
                                    }
                                    i12 = i16 + 3;
                                    char c152 = 0;
                                    while (i12 < strArrSplit3.length) {
                                        String str10 = "softirq";
                                        if (c152 == 0) {
                                            c12 = c152;
                                            if ("user".equalsIgnoreCase(strArrSplit3[i12])) {
                                                str10 = "user";
                                                c152 = 1;
                                            }
                                            if (str10 != null) {
                                            }
                                            c13 = 6;
                                            if (c152 >= c13) {
                                            }
                                        } else if (c152 == 1) {
                                            c12 = c152;
                                        } else if (c152 == 2) {
                                            c12 = c152;
                                            if (!"iowait".equalsIgnoreCase(strArrSplit3[i12])) {
                                                str10 = "iowait";
                                                c152 = 3;
                                            } else if (!"irq".equalsIgnoreCase(strArrSplit3[i12])) {
                                            }
                                            if (str10 != null) {
                                            }
                                            c13 = 6;
                                            if (c152 >= c13) {
                                            }
                                        } else if (c152 == 3) {
                                            c12 = c152;
                                            if (!"irq".equalsIgnoreCase(strArrSplit3[i12])) {
                                                str10 = "irq";
                                                c152 = 4;
                                            } else if (!"softirq".equalsIgnoreCase(strArrSplit3[i12])) {
                                            }
                                            if (str10 != null) {
                                            }
                                            c13 = 6;
                                            if (c152 >= c13) {
                                            }
                                        } else if (c152 != 4) {
                                            c12 = c152;
                                        } else {
                                            c12 = c152;
                                            if (!"softirq".equalsIgnoreCase(strArrSplit3[i12])) {
                                                c152 = 5;
                                            } else if ("softirq".equalsIgnoreCase(strArrSplit3[i12])) {
                                                c152 = 6;
                                            } else {
                                                c152 = c12;
                                                str10 = null;
                                            }
                                            if (str10 != null) {
                                                try {
                                                    float fFloatValue2 = Float.valueOf(strArrSplit3[i12 - 1].replace(str4, "")).floatValue();
                                                    strArr2 = strArrSplit3;
                                                    try {
                                                        String str11 = string + str10;
                                                        if (map3 == map4) {
                                                            str5 = str4;
                                                        } else {
                                                            str5 = str4;
                                                            try {
                                                                fFloatValue2 /= com.apm.insight.l.d.d();
                                                            } catch (Throwable unused4) {
                                                                map3.put(u1.j(string, str10), Float.valueOf(-1.0f));
                                                                c13 = 6;
                                                            }
                                                        }
                                                        map3.put(str11, Float.valueOf(fFloatValue2));
                                                        c13 = 6;
                                                    } catch (Throwable unused5) {
                                                        str5 = str4;
                                                        map3.put(u1.j(string, str10), Float.valueOf(-1.0f));
                                                        c13 = 6;
                                                        if (c152 >= c13) {
                                                        }
                                                    }
                                                } catch (Throwable unused6) {
                                                    strArr2 = strArrSplit3;
                                                }
                                                if (c152 >= c13) {
                                                    break;
                                                }
                                                i12 += 3;
                                                strArrSplit3 = strArr2;
                                                str4 = str5;
                                            } else {
                                                strArr2 = strArrSplit3;
                                                str5 = str4;
                                            }
                                            c13 = 6;
                                            if (c152 >= c13) {
                                            }
                                        }
                                        if ("kernel".equalsIgnoreCase(strArrSplit3[i12])) {
                                            str10 = "kernel";
                                            c152 = 2;
                                        } else if (!"iowait".equalsIgnoreCase(strArrSplit3[i12])) {
                                        }
                                        if (str10 != null) {
                                        }
                                        c13 = 6;
                                        if (c152 >= c13) {
                                        }
                                    }
                                } else {
                                    map = map7;
                                    map2 = map8;
                                }
                            }
                            map = map7;
                            map2 = map8;
                            c11 = 4;
                            c6 = 3;
                            c14 = 4;
                        } else {
                            map = map7;
                            map2 = map8;
                        }
                        c6 = 3;
                        c14 = c10;
                        c11 = 4;
                    }
                } else {
                    map = map7;
                    map2 = map8;
                    str2 = str6;
                    c10 = c14;
                    c6 = 3;
                    String strTrim4 = strTrim3.trim();
                    if (strTrim4.startsWith("tag:")) {
                        strTrim2 = strTrim4.replace("tag:", "").trim();
                        c11 = 4;
                        c14 = 1;
                    }
                    c14 = c10;
                    c11 = 4;
                }
                if (c14 < c11) {
                    break;
                }
            } else {
                strArr = strArrSplit;
                map = map7;
                map2 = map8;
                i10 = length;
                str2 = str6;
                c6 = 3;
            }
            i13++;
            bVar = this;
            strArrSplit = strArr;
            length = i10;
            str6 = str2;
            map7 = map;
            map8 = map2;
        }
        String str12 = str7;
        jSONObject.put("anr_tag", strTrim2);
        jSONObject.put("anr_has_ago", String.valueOf(z11));
        jSONObject.put("anr_reason", str12);
        a(map6, jSONObject, "app");
        a(map4, jSONObject, "total");
        if (map5.isEmpty()) {
            jSONObject.put("npth_anr_systemserver_total", "not found");
        } else {
            jSONObject.put("npth_anr_systemserver_total", b(c.AnonymousClass1.a(map5).floatValue()));
        }
        if (map2.isEmpty()) {
            jSONObject.put("npth_anr_kswapd_total", "not found");
        } else {
            jSONObject.put("npth_anr_kswapd_total", b(c.AnonymousClass1.a(map2).floatValue()));
        }
        if (map.isEmpty()) {
            jSONObject.put("npth_anr_dex2oat_total", "not found");
        } else {
            jSONObject.put("npth_anr_dex2oat_total", b(c.AnonymousClass1.a(map).floatValue()));
        }
    }

    private static void a(HashMap<String, Float> map, JSONObject jSONObject, String str) throws JSONException {
        String strConcat = "npth_anr_".concat(String.valueOf(str));
        if (map.isEmpty()) {
            jSONObject.put(strConcat + "_total", "not found");
            return;
        }
        float fFloatValue = 0.0f;
        float fFloatValue2 = 0.0f;
        float fFloatValue3 = 0.0f;
        float fFloatValue4 = 0.0f;
        float fFloatValue5 = 0.0f;
        for (Map.Entry<String, Float> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.endsWith("user")) {
                fFloatValue += entry.getValue().floatValue();
            } else if (key.endsWith("kernel")) {
                fFloatValue2 += entry.getValue().floatValue();
            } else if (key.endsWith("iowait")) {
                fFloatValue3 += entry.getValue().floatValue();
            } else if (key.endsWith("irq")) {
                fFloatValue4 += entry.getValue().floatValue();
            } else if (key.endsWith("softirq")) {
                fFloatValue5 = entry.getValue().floatValue() + fFloatValue5;
            }
        }
        float f10 = fFloatValue + fFloatValue2 + fFloatValue3 + fFloatValue4 + fFloatValue5;
        jSONObject.put(u1.j(strConcat, "_total"), b(f10));
        jSONObject.put(strConcat + "_kernel_user_ratio", a(fFloatValue2, f10));
        jSONObject.put(strConcat + "_iowait_user_ratio", a(fFloatValue3, f10));
    }

    private static String a(float f10, float f11) {
        if (f11 > 0.0f) {
            return a(f10 / f11);
        }
        return f10 > 0.0f ? "100%" : "0%";
    }
}
