package androidx.lifecycle; import androidx.lifecycle.e; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import k.b; /* loaded from: classes.dex */ public class i extends e { /* renamed from: b, reason: collision with root package name */ private k.a f2081b; /* renamed from: c, reason: collision with root package name */ private e.c f2082c; /* renamed from: d, reason: collision with root package name */ private final WeakReference f2083d; /* renamed from: e, reason: collision with root package name */ private int f2084e; /* renamed from: f, reason: collision with root package name */ private boolean f2085f; /* renamed from: g, reason: collision with root package name */ private boolean f2086g; /* renamed from: h, reason: collision with root package name */ private ArrayList f2087h; /* renamed from: i, reason: collision with root package name */ private final boolean f2088i; static class a { /* renamed from: a, reason: collision with root package name */ e.c f2089a; /* renamed from: b, reason: collision with root package name */ g f2090b; a(h hVar, e.c cVar) { this.f2090b = k.f(hVar); this.f2089a = cVar; } void a(LifecycleOwner lifecycleOwner, e.b bVar) { e.c b10 = bVar.b(); this.f2089a = i.k(this.f2089a, b10); this.f2090b.onStateChanged(lifecycleOwner, bVar); this.f2089a = b10; } } public i(LifecycleOwner lifecycleOwner) { this(lifecycleOwner, true); } private void d(LifecycleOwner lifecycleOwner) { Iterator descendingIterator = this.f2081b.descendingIterator(); while (descendingIterator.hasNext() && !this.f2086g) { Map.Entry entry = (Map.Entry) descendingIterator.next(); a aVar = (a) entry.getValue(); while (aVar.f2089a.compareTo(this.f2082c) > 0 && !this.f2086g && this.f2081b.contains((h) entry.getKey())) { e.b a10 = e.b.a(aVar.f2089a); if (a10 == null) { throw new IllegalStateException("no event down from " + aVar.f2089a); } n(a10.b()); aVar.a(lifecycleOwner, a10); m(); } } } private e.c e(h hVar) { Map.Entry h10 = this.f2081b.h(hVar); e.c cVar = null; e.c cVar2 = h10 != null ? ((a) h10.getValue()).f2089a : null; if (!this.f2087h.isEmpty()) { cVar = (e.c) this.f2087h.get(r0.size() - 1); } return k(k(this.f2082c, cVar2), cVar); } private void f(String str) { if (!this.f2088i || j.a.e().b()) { return; } throw new IllegalStateException("Method " + str + " must be called on the main thread"); } private void g(LifecycleOwner lifecycleOwner) { b.d c10 = this.f2081b.c(); while (c10.hasNext() && !this.f2086g) { Map.Entry entry = (Map.Entry) c10.next(); a aVar = (a) entry.getValue(); while (aVar.f2089a.compareTo(this.f2082c) < 0 && !this.f2086g && this.f2081b.contains((h) entry.getKey())) { n(aVar.f2089a); e.b c11 = e.b.c(aVar.f2089a); if (c11 == null) { throw new IllegalStateException("no event up from " + aVar.f2089a); } aVar.a(lifecycleOwner, c11); m(); } } } private boolean i() { if (this.f2081b.size() == 0) { return true; } e.c cVar = ((a) this.f2081b.a().getValue()).f2089a; e.c cVar2 = ((a) this.f2081b.d().getValue()).f2089a; return cVar == cVar2 && this.f2082c == cVar2; } static e.c k(e.c cVar, e.c cVar2) { return (cVar2 == null || cVar2.compareTo(cVar) >= 0) ? cVar : cVar2; } private void l(e.c cVar) { e.c cVar2 = this.f2082c; if (cVar2 == cVar) { return; } if (cVar2 == e.c.INITIALIZED && cVar == e.c.DESTROYED) { throw new IllegalStateException("no event down from " + this.f2082c); } this.f2082c = cVar; if (this.f2085f || this.f2084e != 0) { this.f2086g = true; return; } this.f2085f = true; p(); this.f2085f = false; if (this.f2082c == e.c.DESTROYED) { this.f2081b = new k.a(); } } private void m() { this.f2087h.remove(r0.size() - 1); } private void n(e.c cVar) { this.f2087h.add(cVar); } private void p() { LifecycleOwner lifecycleOwner = (LifecycleOwner) this.f2083d.get(); if (lifecycleOwner == null) { throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is alreadygarbage collected. It is too late to change lifecycle state."); } while (!i()) { this.f2086g = false; if (this.f2082c.compareTo(((a) this.f2081b.a().getValue()).f2089a) < 0) { d(lifecycleOwner); } Map.Entry d10 = this.f2081b.d(); if (!this.f2086g && d10 != null && this.f2082c.compareTo(((a) d10.getValue()).f2089a) > 0) { g(lifecycleOwner); } } this.f2086g = false; } @Override // androidx.lifecycle.e public void a(h hVar) { LifecycleOwner lifecycleOwner; f("addObserver"); e.c cVar = this.f2082c; e.c cVar2 = e.c.DESTROYED; if (cVar != cVar2) { cVar2 = e.c.INITIALIZED; } a aVar = new a(hVar, cVar2); if (((a) this.f2081b.f(hVar, aVar)) == null && (lifecycleOwner = (LifecycleOwner) this.f2083d.get()) != null) { boolean z10 = this.f2084e != 0 || this.f2085f; e.c e10 = e(hVar); this.f2084e++; while (aVar.f2089a.compareTo(e10) < 0 && this.f2081b.contains(hVar)) { n(aVar.f2089a); e.b c10 = e.b.c(aVar.f2089a); if (c10 == null) { throw new IllegalStateException("no event up from " + aVar.f2089a); } aVar.a(lifecycleOwner, c10); m(); e10 = e(hVar); } if (!z10) { p(); } this.f2084e--; } } @Override // androidx.lifecycle.e public e.c b() { return this.f2082c; } @Override // androidx.lifecycle.e public void c(h hVar) { f("removeObserver"); this.f2081b.g(hVar); } public void h(e.b bVar) { f("handleLifecycleEvent"); l(bVar.b()); } public void j(e.c cVar) { f("markState"); o(cVar); } public void o(e.c cVar) { f("setCurrentState"); l(cVar); } private i(LifecycleOwner lifecycleOwner, boolean z10) { this.f2081b = new k.a(); this.f2084e = 0; this.f2085f = false; this.f2086g = false; this.f2087h = new ArrayList(); this.f2083d = new WeakReference(lifecycleOwner); this.f2082c = e.c.INITIALIZED; this.f2088i = z10; } }