package androidx.core.app;

import android.app.Activity;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.SparseIntArray;
import android.view.FrameMetrics;
import android.view.Window;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;

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

    /* renamed from: a, reason: collision with root package name */
    private final b f3541a;

    /* loaded from: classes.dex */
    private static class a extends b {

        /* renamed from: e, reason: collision with root package name */
        private static HandlerThread f3542e;

        /* renamed from: f, reason: collision with root package name */
        private static Handler f3543f;

        /* renamed from: a, reason: collision with root package name */
        int f3544a;

        /* renamed from: b, reason: collision with root package name */
        SparseIntArray[] f3545b = new SparseIntArray[9];

        /* renamed from: c, reason: collision with root package name */
        private final ArrayList f3546c = new ArrayList();

        /* renamed from: d, reason: collision with root package name */
        Window.OnFrameMetricsAvailableListener f3547d = new WindowOnFrameMetricsAvailableListenerC0090a();

        /* renamed from: androidx.core.app.g$a$a, reason: collision with other inner class name */
        /* loaded from: classes.dex */
        class WindowOnFrameMetricsAvailableListenerC0090a implements Window.OnFrameMetricsAvailableListener {
            WindowOnFrameMetricsAvailableListenerC0090a() {
            }

            @Override // android.view.Window.OnFrameMetricsAvailableListener
            public void onFrameMetricsAvailable(Window window, FrameMetrics frameMetrics, int i10) {
                a aVar = a.this;
                if ((aVar.f3544a & 1) != 0) {
                    aVar.e(aVar.f3545b[0], frameMetrics.getMetric(8));
                }
                a aVar2 = a.this;
                if ((aVar2.f3544a & 2) != 0) {
                    aVar2.e(aVar2.f3545b[1], frameMetrics.getMetric(1));
                }
                a aVar3 = a.this;
                if ((aVar3.f3544a & 4) != 0) {
                    aVar3.e(aVar3.f3545b[2], frameMetrics.getMetric(3));
                }
                a aVar4 = a.this;
                if ((aVar4.f3544a & 8) != 0) {
                    aVar4.e(aVar4.f3545b[3], frameMetrics.getMetric(4));
                }
                a aVar5 = a.this;
                if ((aVar5.f3544a & 16) != 0) {
                    aVar5.e(aVar5.f3545b[4], frameMetrics.getMetric(5));
                }
                a aVar6 = a.this;
                if ((aVar6.f3544a & 64) != 0) {
                    aVar6.e(aVar6.f3545b[6], frameMetrics.getMetric(7));
                }
                a aVar7 = a.this;
                if ((aVar7.f3544a & 32) != 0) {
                    aVar7.e(aVar7.f3545b[5], frameMetrics.getMetric(6));
                }
                a aVar8 = a.this;
                if ((aVar8.f3544a & 128) != 0) {
                    aVar8.e(aVar8.f3545b[7], frameMetrics.getMetric(0));
                }
                a aVar9 = a.this;
                if ((aVar9.f3544a & 256) != 0) {
                    aVar9.e(aVar9.f3545b[8], frameMetrics.getMetric(2));
                }
            }
        }

        a(int i10) {
            this.f3544a = i10;
        }

        @Override // androidx.core.app.g.b
        public void a(Activity activity) {
            if (f3542e == null) {
                HandlerThread handlerThread = new HandlerThread("FrameMetricsAggregator");
                f3542e = handlerThread;
                handlerThread.start();
                f3543f = new Handler(f3542e.getLooper());
            }
            for (int i10 = 0; i10 <= 8; i10++) {
                SparseIntArray[] sparseIntArrayArr = this.f3545b;
                if (sparseIntArrayArr[i10] == null && (this.f3544a & (1 << i10)) != 0) {
                    sparseIntArrayArr[i10] = new SparseIntArray();
                }
            }
            activity.getWindow().addOnFrameMetricsAvailableListener(this.f3547d, f3543f);
            this.f3546c.add(new WeakReference(activity));
        }

        @Override // androidx.core.app.g.b
        public SparseIntArray[] b() {
            return this.f3545b;
        }

        @Override // androidx.core.app.g.b
        public SparseIntArray[] c(Activity activity) {
            Iterator it = this.f3546c.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                WeakReference weakReference = (WeakReference) it.next();
                if (weakReference.get() == activity) {
                    this.f3546c.remove(weakReference);
                    break;
                }
            }
            activity.getWindow().removeOnFrameMetricsAvailableListener(this.f3547d);
            return this.f3545b;
        }

        @Override // androidx.core.app.g.b
        public SparseIntArray[] d() {
            SparseIntArray[] sparseIntArrayArr = this.f3545b;
            this.f3545b = new SparseIntArray[9];
            return sparseIntArrayArr;
        }

        void e(SparseIntArray sparseIntArray, long j10) {
            if (sparseIntArray != null) {
                int i10 = (int) ((500000 + j10) / 1000000);
                if (j10 >= 0) {
                    sparseIntArray.put(i10, sparseIntArray.get(i10) + 1);
                }
            }
        }
    }

    /* loaded from: classes.dex */
    private static class b {
        b() {
        }

        public abstract void a(Activity activity);

        public abstract SparseIntArray[] b();

        public abstract SparseIntArray[] c(Activity activity);

        public abstract SparseIntArray[] d();
    }

    public g() {
        this(1);
    }

    public void a(Activity activity) {
        this.f3541a.a(activity);
    }

    public SparseIntArray[] b() {
        return this.f3541a.b();
    }

    public SparseIntArray[] c(Activity activity) {
        return this.f3541a.c(activity);
    }

    public SparseIntArray[] d() {
        return this.f3541a.d();
    }

    public g(int i10) {
        this.f3541a = new a(i10);
    }
}
