package com.apm.insight.b;

import android.os.Looper;
import android.os.Process;
import android.os.SystemClock;
import android.text.TextUtils;
import com.apm.insight.runtime.p;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import xyz.stream.download.config.InnerConstant;

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private static int f5051r = 2;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private int f5053b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private int f5055d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private int f5056e;

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

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private int f5060i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private long f5061j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private String f5062k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private String f5063l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private com.apm.insight.b.e f5064m;

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

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

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private final p f5067p;

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

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

    public static class a {

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

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

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

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

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

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        StackTraceElement[] f5083f;

        private a() {
        }

        public /* synthetic */ a(byte b10) {
            this();
        }
    }

    public static class b {

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

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private int f5085b;

        public final void a(a aVar) {
            throw null;
        }
    }

    public interface c {
    }

    public static class d {
    }

    public static class e {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public long f5086a;

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

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

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

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

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

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

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

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        public String f5094i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        private String f5095j;

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        private d f5096k;

        public final JSONObject a() {
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("msg", g.a(this.f5093h));
                jSONObject.put("cpuDuration", this.f5092g);
                jSONObject.put("duration", this.f5091f);
                jSONObject.put("type", this.f5089d);
                jSONObject.put("count", this.f5090e);
                jSONObject.put("messageCount", this.f5090e);
                jSONObject.put("lastDuration", this.f5087b - this.f5088c);
                jSONObject.put("start", this.f5086a);
                jSONObject.put("end", this.f5087b);
                jSONObject.put("block_uuid", (Object) null);
                jSONObject.put("sblock_uuid", (Object) null);
                jSONObject.put("belong_frame", false);
            } catch (JSONException unused) {
            }
            return jSONObject;
        }

        public final void b() {
            this.f5089d = -1;
            this.f5090e = -1;
            this.f5091f = -1L;
            this.f5093h = null;
            this.f5095j = null;
            this.f5096k = null;
            this.f5094i = null;
        }
    }

    public g() {
        this((byte) 0);
    }

    public static /* synthetic */ b c() {
        return null;
    }

    public static /* synthetic */ p e() {
        return null;
    }

    public static class f {

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

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private int f5098b;

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

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

        public f(int i10) {
            this.f5097a = i10;
        }

        public final e a(int i10) {
            e eVar = this.f5099c;
            if (eVar != null) {
                eVar.f5089d = i10;
                this.f5099c = null;
                return eVar;
            }
            e eVar2 = new e();
            eVar2.f5089d = i10;
            return eVar2;
        }

        public final void a(e eVar) {
            int size = this.f5100d.size();
            int i10 = this.f5097a;
            if (size < i10) {
                this.f5100d.add(eVar);
                this.f5098b = this.f5100d.size();
                return;
            }
            int i11 = this.f5098b % i10;
            this.f5098b = i11;
            e eVar2 = this.f5100d.set(i11, eVar);
            eVar2.b();
            this.f5099c = eVar2;
            this.f5098b++;
        }

        public final List<e> a() {
            ArrayList arrayList = new ArrayList();
            int i10 = 0;
            if (this.f5100d.size() == this.f5097a) {
                for (int i11 = this.f5098b; i11 < this.f5100d.size(); i11++) {
                    arrayList.add(this.f5100d.get(i11));
                }
                while (i10 < this.f5098b - 1) {
                    arrayList.add(this.f5100d.get(i10));
                    i10++;
                }
            } else {
                while (i10 < this.f5100d.size()) {
                    arrayList.add(this.f5100d.get(i10));
                    i10++;
                }
            }
            return arrayList;
        }
    }

    private g(byte b10) {
        this.f5053b = 0;
        this.f5054c = 0;
        this.f5055d = 100;
        this.f5056e = 200;
        this.f5058g = -1L;
        this.f5059h = -1L;
        this.f5060i = -1;
        this.f5061j = -1L;
        this.f5065n = false;
        this.f5066o = false;
        this.f5068q = false;
        this.f5069s = new Runnable() { // from class: com.apm.insight.b.g.2

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

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

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

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

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

            @Override // java.lang.Runnable
            public final void run() {
                long jUptimeMillis = SystemClock.uptimeMillis();
                if (g.c().f5084a != null) {
                    throw null;
                }
                a aVar = new a((byte) 0);
                if (this.f5073c == g.this.f5054c) {
                    this.f5074d++;
                } else {
                    this.f5074d = 0;
                    this.f5075e = 0;
                    this.f5072b = jUptimeMillis;
                }
                this.f5073c = g.this.f5054c;
                int i10 = this.f5074d;
                if (i10 > 0 && i10 - this.f5075e >= g.f5051r && this.f5071a != 0 && jUptimeMillis - this.f5072b > 700 && g.this.f5068q) {
                    aVar.f5083f = Looper.getMainLooper().getThread().getStackTrace();
                    this.f5075e = this.f5074d;
                }
                aVar.f5081d = g.this.f5068q;
                aVar.f5080c = (jUptimeMillis - this.f5071a) - 300;
                aVar.f5078a = jUptimeMillis;
                long jUptimeMillis2 = SystemClock.uptimeMillis();
                this.f5071a = jUptimeMillis2;
                aVar.f5079b = jUptimeMillis2 - jUptimeMillis;
                aVar.f5082e = g.this.f5054c;
                g.e().a(g.this.f5069s, 300L);
                g.c().a(aVar);
            }
        };
        this.f5052a = new c() { // from class: com.apm.insight.b.g.1
        };
        this.f5067p = null;
    }

    public static /* synthetic */ int d(g gVar) {
        int i10 = gVar.f5053b;
        gVar.f5053b = i10 + 1;
        return i10;
    }

    public final JSONArray b() {
        JSONArray jSONArray = new JSONArray();
        try {
            int i10 = 0;
            for (e eVar : this.f5057f.a()) {
                if (eVar != null) {
                    i10++;
                    jSONArray.put(eVar.a().put(InnerConstant.Db.f36069id, i10));
                }
            }
        } catch (Throwable unused) {
        }
        return jSONArray;
    }

    public final void a() {
        if (this.f5065n) {
            return;
        }
        this.f5065n = true;
        this.f5055d = 100;
        this.f5056e = 300;
        this.f5057f = new f(100);
        this.f5064m = new com.apm.insight.b.e() { // from class: com.apm.insight.b.g.3
            @Override // com.apm.insight.b.e
            public final boolean a() {
                return true;
            }

            @Override // com.apm.insight.b.e
            public final void b(String str) {
                super.b(str);
                g.d(g.this);
                g.a(g.this, false, com.apm.insight.b.e.f5045a);
                g gVar = g.this;
                gVar.f5062k = gVar.f5063l;
                g.this.f5063l = "no message running";
                g.this.f5068q = false;
            }

            @Override // com.apm.insight.b.e
            public final void a(String str) {
                g.this.f5068q = true;
                g.this.f5063l = str;
                super.a(str);
                g.a(g.this, true, com.apm.insight.b.e.f5045a);
            }
        };
        h.a();
        h.a(this.f5064m);
        j.a(j.a());
    }

    private void a(int i10, long j10, String str) {
        a(i10, j10, str, true);
    }

    private void a(int i10, long j10, String str, boolean z10) {
        this.f5066o = true;
        e eVarA = this.f5057f.a(i10);
        eVarA.f5091f = j10 - this.f5058g;
        if (z10) {
            long jCurrentThreadTimeMillis = SystemClock.currentThreadTimeMillis();
            eVarA.f5092g = jCurrentThreadTimeMillis - this.f5061j;
            this.f5061j = jCurrentThreadTimeMillis;
        } else {
            eVarA.f5092g = -1L;
        }
        eVarA.f5090e = this.f5053b;
        eVarA.f5093h = str;
        eVarA.f5094i = this.f5062k;
        eVarA.f5086a = this.f5058g;
        eVarA.f5087b = j10;
        eVarA.f5088c = this.f5059h;
        this.f5057f.a(eVarA);
        this.f5053b = 0;
        this.f5058g = j10;
    }

    public final e a(long j10) {
        e eVar = new e();
        eVar.f5093h = this.f5063l;
        eVar.f5094i = this.f5062k;
        eVar.f5091f = j10 - this.f5059h;
        eVar.f5092g = 0 - this.f5061j;
        eVar.f5090e = this.f5053b;
        return eVar;
    }

    public static String a(String str) {
        String str2;
        if (TextUtils.isEmpty(str)) {
            return "unknown message";
        }
        try {
            String[] strArrSplit = str.split(":");
            String str3 = strArrSplit.length == 2 ? strArrSplit[1] : "";
            if (str.contains("{") && str.contains("}")) {
                str2 = str.split("\\{")[0];
                try {
                    str = str2 + str.split("\\}")[1];
                } catch (Throwable unused) {
                    return str2;
                }
            } else {
                str2 = str;
            }
            if (str.contains("@")) {
                String[] strArrSplit2 = str.split("@");
                if (strArrSplit2.length > 1) {
                    str = strArrSplit2[0];
                }
            }
            if (str.contains("(") && str.contains(")") && !str.endsWith(" null")) {
                String[] strArrSplit3 = str.split("\\(");
                if (strArrSplit3.length > 1) {
                    str = strArrSplit3[1];
                }
                str = str.replace(")", "");
            }
            if (str.startsWith(" ")) {
                str = str.replace(" ", "");
            }
            return str + str3;
        } catch (Throwable unused2) {
            return str;
        }
    }

    public static /* synthetic */ void a(g gVar, boolean z10, long j10) {
        int i10 = gVar.f5054c + 1;
        gVar.f5054c = i10;
        gVar.f5054c = i10 & 65535;
        gVar.f5066o = false;
        if (gVar.f5058g < 0) {
            gVar.f5058g = j10;
        }
        if (gVar.f5059h < 0) {
            gVar.f5059h = j10;
        }
        if (gVar.f5060i < 0) {
            gVar.f5060i = Process.myTid();
            gVar.f5061j = SystemClock.currentThreadTimeMillis();
        }
        long j11 = j10 - gVar.f5058g;
        int i11 = gVar.f5056e;
        if (j11 > i11) {
            long j12 = gVar.f5059h;
            if (j10 - j12 <= i11) {
                gVar.a(9, j10, gVar.f5063l);
            } else if (z10) {
                if (gVar.f5053b == 0) {
                    gVar.a(1, j10, "no message running");
                } else {
                    gVar.a(9, j12, gVar.f5062k);
                    gVar.a(1, j10, "no message running", false);
                }
            } else if (gVar.f5053b == 0) {
                gVar.a(8, j10, gVar.f5063l, true);
            } else {
                gVar.a(9, j12, gVar.f5062k, false);
                gVar.a(8, j10, gVar.f5063l, true);
            }
        }
        gVar.f5059h = j10;
    }
}
