package androidx.lifecycle;

import android.annotation.SuppressLint;
import androidx.lifecycle.g;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/* JADX INFO: compiled from: LifecycleRegistry.java */
/* JADX INFO: loaded from: classes.dex */
public class l extends g {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private h.a<j, a> f2557a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private g.c f2558b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final WeakReference<k> f2559c;

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private ArrayList<g.c> f2563g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final boolean f2564h;

    /* JADX INFO: compiled from: LifecycleRegistry.java */
    static class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        g.c f2565a;

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

        a(j jVar, g.c cVar) {
            this.f2566b = n.f(jVar);
            this.f2565a = cVar;
        }

        void a(k kVar, g.b bVar) {
            g.c targetState = bVar.getTargetState();
            this.f2565a = l.k(this.f2565a, targetState);
            this.f2566b.g(kVar, bVar);
            this.f2565a = targetState;
        }
    }

    public l(k kVar) {
        this(kVar, true);
    }

    private void d(k kVar) {
        Iterator<Map.Entry<j, a>> itA = this.f2557a.a();
        while (itA.hasNext() && !this.f2562f) {
            Map.Entry<j, a> next = itA.next();
            a value = next.getValue();
            while (value.f2565a.compareTo(this.f2558b) > 0 && !this.f2562f && this.f2557a.contains(next.getKey())) {
                g.b bVarDownFrom = g.b.downFrom(value.f2565a);
                if (bVarDownFrom == null) {
                    throw new IllegalStateException("no event down from " + value.f2565a);
                }
                n(bVarDownFrom.getTargetState());
                value.a(kVar, bVarDownFrom);
                m();
            }
        }
    }

    private g.c e(j jVar) {
        Map.Entry<j, a> entryI = this.f2557a.i(jVar);
        g.c cVar = null;
        g.c cVar2 = entryI != null ? entryI.getValue().f2565a : null;
        if (!this.f2563g.isEmpty()) {
            cVar = this.f2563g.get(r0.size() - 1);
        }
        return k(k(this.f2558b, cVar2), cVar);
    }

    @SuppressLint({"RestrictedApi"})
    private void f(String str) {
        if (!this.f2564h || g.a.d().b()) {
            return;
        }
        throw new IllegalStateException("Method " + str + " must be called on the main thread");
    }

    private void g(k kVar) {
        h.b<j, a>.d dVarD = this.f2557a.d();
        while (dVarD.hasNext() && !this.f2562f) {
            Map.Entry next = dVarD.next();
            a aVar = (a) next.getValue();
            while (aVar.f2565a.compareTo(this.f2558b) < 0 && !this.f2562f && this.f2557a.contains((j) next.getKey())) {
                n(aVar.f2565a);
                g.b bVarUpFrom = g.b.upFrom(aVar.f2565a);
                if (bVarUpFrom == null) {
                    throw new IllegalStateException("no event up from " + aVar.f2565a);
                }
                aVar.a(kVar, bVarUpFrom);
                m();
            }
        }
    }

    private boolean i() {
        if (this.f2557a.size() == 0) {
            return true;
        }
        g.c cVar = this.f2557a.b().getValue().f2565a;
        g.c cVar2 = this.f2557a.e().getValue().f2565a;
        return cVar == cVar2 && this.f2558b == cVar2;
    }

    static g.c k(g.c cVar, g.c cVar2) {
        return (cVar2 == null || cVar2.compareTo(cVar) >= 0) ? cVar : cVar2;
    }

    private void l(g.c cVar) {
        g.c cVar2 = this.f2558b;
        if (cVar2 == cVar) {
            return;
        }
        if (cVar2 == g.c.INITIALIZED && cVar == g.c.DESTROYED) {
            throw new IllegalStateException("no event down from " + this.f2558b);
        }
        this.f2558b = cVar;
        if (this.f2561e || this.f2560d != 0) {
            this.f2562f = true;
            return;
        }
        this.f2561e = true;
        p();
        this.f2561e = false;
        if (this.f2558b == g.c.DESTROYED) {
            this.f2557a = new h.a<>();
        }
    }

    private void m() {
        this.f2563g.remove(r1.size() - 1);
    }

    private void n(g.c cVar) {
        this.f2563g.add(cVar);
    }

    private void p() {
        k kVar = this.f2559c.get();
        if (kVar == null) {
            throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is alreadygarbage collected. It is too late to change lifecycle state.");
        }
        while (!i()) {
            this.f2562f = false;
            if (this.f2558b.compareTo(this.f2557a.b().getValue().f2565a) < 0) {
                d(kVar);
            }
            Map.Entry<j, a> entryE = this.f2557a.e();
            if (!this.f2562f && entryE != null && this.f2558b.compareTo(entryE.getValue().f2565a) > 0) {
                g(kVar);
            }
        }
        this.f2562f = false;
    }

    @Override // androidx.lifecycle.g
    public void a(j jVar) {
        k kVar;
        f("addObserver");
        g.c cVar = this.f2558b;
        g.c cVar2 = g.c.DESTROYED;
        if (cVar != cVar2) {
            cVar2 = g.c.INITIALIZED;
        }
        a aVar = new a(jVar, cVar2);
        if (this.f2557a.g(jVar, aVar) == null && (kVar = this.f2559c.get()) != null) {
            boolean z2 = this.f2560d != 0 || this.f2561e;
            g.c cVarE = e(jVar);
            this.f2560d++;
            while (aVar.f2565a.compareTo(cVarE) < 0 && this.f2557a.contains(jVar)) {
                n(aVar.f2565a);
                g.b bVarUpFrom = g.b.upFrom(aVar.f2565a);
                if (bVarUpFrom == null) {
                    throw new IllegalStateException("no event up from " + aVar.f2565a);
                }
                aVar.a(kVar, bVarUpFrom);
                m();
                cVarE = e(jVar);
            }
            if (!z2) {
                p();
            }
            this.f2560d--;
        }
    }

    @Override // androidx.lifecycle.g
    public g.c b() {
        return this.f2558b;
    }

    @Override // androidx.lifecycle.g
    public void c(j jVar) {
        f("removeObserver");
        this.f2557a.h(jVar);
    }

    public void h(g.b bVar) {
        f("handleLifecycleEvent");
        l(bVar.getTargetState());
    }

    @Deprecated
    public void j(g.c cVar) {
        f("markState");
        o(cVar);
    }

    public void o(g.c cVar) {
        f("setCurrentState");
        l(cVar);
    }

    private l(k kVar, boolean z2) {
        this.f2557a = new h.a<>();
        this.f2560d = 0;
        this.f2561e = false;
        this.f2562f = false;
        this.f2563g = new ArrayList<>();
        this.f2559c = new WeakReference<>(kVar);
        this.f2558b = g.c.INITIALIZED;
        this.f2564h = z2;
    }
}
