package com.apm.insight.runtime.a;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static boolean f5417b = false;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static boolean f5418c = false;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static int f5419d = 1;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static boolean f5420e = false;

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    private static volatile b f5422z;
    private int B;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private LinkedList<a> f5429m = new LinkedList<>();

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private boolean f5440x = false;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private long f5441y = -1;
    private int A = 50;

    public static class a {

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

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

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

        public a(String str, String str2, long j10) {
            this.f5444b = str2;
            this.f5445c = j10;
            this.f5443a = str;
        }

        public final String toString() {
            return com.apm.insight.l.b.a().format(new Date(this.f5445c)) + " : " + this.f5443a + ' ' + this.f5444b;
        }
    }

    private b(@NonNull Application application) {
        this.f5424h = application;
        this.f5423g = application;
        if (application != null) {
            try {
                this.f5423g.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { // from class: com.apm.insight.runtime.a.b.1
                    @Override // android.app.Application.ActivityLifecycleCallbacks
                    public final void onActivityCreated(Activity activity, Bundle bundle) {
                        b.this.f5430n = activity.getClass().getName();
                        b.this.f5431o = System.currentTimeMillis();
                        boolean unused = b.f5417b = bundle != null;
                        boolean unused2 = b.f5418c = true;
                        b.this.f5425i.add(b.this.f5430n);
                        b.this.f5426j.add(Long.valueOf(b.this.f5431o));
                        b bVar = b.this;
                        b.a(bVar, bVar.f5430n, b.this.f5431o, "onCreate");
                    }

                    @Override // android.app.Application.ActivityLifecycleCallbacks
                    public final void onActivityDestroyed(Activity activity) {
                        String name = activity.getClass().getName();
                        int iIndexOf = b.this.f5425i.indexOf(name);
                        if (iIndexOf >= 0 && iIndexOf < b.this.f5425i.size()) {
                            b.this.f5425i.remove(iIndexOf);
                            b.this.f5426j.remove(iIndexOf);
                        }
                        b.this.f5427k.add(name);
                        long jCurrentTimeMillis = System.currentTimeMillis();
                        b.this.f5428l.add(Long.valueOf(jCurrentTimeMillis));
                        b.a(b.this, name, jCurrentTimeMillis, "onDestroy");
                    }

                    @Override // android.app.Application.ActivityLifecycleCallbacks
                    public final void onActivityPaused(Activity activity) {
                        b.this.f5436t = activity.getClass().getName();
                        b.this.f5437u = System.currentTimeMillis();
                        b.l(b.this);
                        if (b.this.B == 0) {
                            b.this.f5440x = false;
                            boolean unused = b.f5418c = false;
                            b.this.f5441y = SystemClock.uptimeMillis();
                        } else if (b.this.B < 0) {
                            b.n(b.this);
                            b.this.f5440x = false;
                            boolean unused2 = b.f5418c = false;
                            b.this.f5441y = SystemClock.uptimeMillis();
                        }
                        b bVar = b.this;
                        b.a(bVar, bVar.f5436t, b.this.f5437u, "onPause");
                    }

                    @Override // android.app.Application.ActivityLifecycleCallbacks
                    public final void onActivityResumed(Activity activity) {
                        b.this.f5434r = activity.getClass().getName();
                        b.this.f5435s = System.currentTimeMillis();
                        b.g(b.this);
                        if (!b.this.f5440x) {
                            if (b.f5416a) {
                                b.k();
                                int unused = b.f5419d = 1;
                                long unused2 = b.f5421f = b.this.f5435s;
                            }
                            if (!b.this.f5434r.equals(b.this.f5436t)) {
                                return;
                            }
                            if (b.f5418c && !b.f5417b) {
                                int unused3 = b.f5419d = 4;
                                long unused4 = b.f5421f = b.this.f5435s;
                                return;
                            } else if (!b.f5418c) {
                                int unused5 = b.f5419d = 3;
                                long unused6 = b.f5421f = b.this.f5435s;
                                return;
                            }
                        }
                        b.this.f5440x = true;
                        b bVar = b.this;
                        b.a(bVar, bVar.f5434r, b.this.f5435s, "onResume");
                    }

                    @Override // android.app.Application.ActivityLifecycleCallbacks
                    public final void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
                    }

                    @Override // android.app.Application.ActivityLifecycleCallbacks
                    public final void onActivityStarted(Activity activity) {
                        b.this.f5432p = activity.getClass().getName();
                        b.this.f5433q = System.currentTimeMillis();
                        b bVar = b.this;
                        b.a(bVar, bVar.f5432p, b.this.f5433q, "onStart");
                    }

                    @Override // android.app.Application.ActivityLifecycleCallbacks
                    public final void onActivityStopped(Activity activity) {
                        b.this.f5438v = activity.getClass().getName();
                        b.this.f5439w = System.currentTimeMillis();
                        b bVar = b.this;
                        b.a(bVar, bVar.f5438v, b.this.f5439w, "onStop");
                    }
                });
            } catch (Throwable unused) {
            }
        }
    }

    public static int b() {
        int i10 = f5419d;
        return i10 == 1 ? f5420e ? 2 : 1 : i10;
    }

    public static long c() {
        return f5421f;
    }

    public static /* synthetic */ int g(b bVar) {
        int i10 = bVar.B;
        bVar.B = i10 + 1;
        return i10;
    }

    public static /* synthetic */ boolean k() {
        f5416a = false;
        return false;
    }

    public static /* synthetic */ int n(b bVar) {
        bVar.B = 0;
        return 0;
    }

    public final boolean f() {
        return this.f5440x;
    }

    public static /* synthetic */ int l(b bVar) {
        int i10 = bVar.B;
        bVar.B = i10 - 1;
        return i10;
    }

    private JSONArray n() {
        JSONArray jSONArray = new JSONArray();
        List<String> list = this.f5425i;
        if (list != null && !list.isEmpty()) {
            for (int i10 = 0; i10 < this.f5425i.size(); i10++) {
                try {
                    jSONArray.put(a(this.f5425i.get(i10), this.f5426j.get(i10).longValue()));
                } catch (Throwable unused) {
                }
            }
        }
        return jSONArray;
    }

    private JSONArray o() {
        JSONArray jSONArray = new JSONArray();
        List<String> list = this.f5427k;
        if (list != null && !list.isEmpty()) {
            for (int i10 = 0; i10 < this.f5427k.size(); i10++) {
                try {
                    jSONArray.put(a(this.f5427k.get(i10), this.f5428l.get(i10).longValue()));
                } catch (Throwable unused) {
                }
            }
        }
        return jSONArray;
    }

    public final JSONObject g() {
        JSONObject jSONObject = new JSONObject();
        if (com.apm.insight.e.w()) {
            try {
                jSONObject.put("last_create_activity", a(this.f5430n, this.f5431o));
                jSONObject.put("last_start_activity", a(this.f5432p, this.f5433q));
                jSONObject.put("last_resume_activity", a(this.f5434r, this.f5435s));
                jSONObject.put("last_pause_activity", a(this.f5436t, this.f5437u));
                jSONObject.put("last_stop_activity", a(this.f5438v, this.f5439w));
                jSONObject.put("alive_activities", n());
                jSONObject.put("finish_activities", o());
            } catch (JSONException unused) {
            }
        }
        return jSONObject;
    }

    @NonNull
    public final String h() {
        return String.valueOf(this.f5434r);
    }

    public final JSONArray i() {
        JSONArray jSONArray = new JSONArray();
        Iterator it = new ArrayList(this.f5429m).iterator();
        while (it.hasNext()) {
            jSONArray.put(((a) it.next()).toString());
        }
        return jSONArray;
    }

    public static void a() {
        f5420e = true;
    }

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

    public final long e() {
        return SystemClock.uptimeMillis() - this.f5441y;
    }

    private static JSONObject a(String str, long j10) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("name", str);
            jSONObject.put("time", j10);
        } catch (JSONException unused) {
        }
        return jSONObject;
    }

    public static /* synthetic */ void a(b bVar, String str, long j10, String str2) {
        a aVar;
        if (com.apm.insight.e.w()) {
            try {
                if (bVar.f5429m.size() >= bVar.A) {
                    aVar = bVar.f5429m.poll();
                    if (aVar != null) {
                        bVar.f5429m.add(aVar);
                    }
                } else {
                    aVar = null;
                }
                if (aVar == null) {
                    aVar = new a(str, str2, j10);
                    bVar.f5429m.add(aVar);
                }
                aVar.f5444b = str2;
                aVar.f5443a = str;
                aVar.f5445c = j10;
            } catch (Throwable unused) {
            }
        }
    }
}
