package androidx.lifecycle; import android.util.Log; import androidx.arch.core.internal.FastSafeIterableMap; import androidx.arch.core.internal.SafeIterableMap; import androidx.lifecycle.Lifecycle; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; /* loaded from: classes.dex */ public class LifecycleRegistry extends Lifecycle { private final WeakReference c; private FastSafeIterableMap a = new FastSafeIterableMap<>(); private int d = 0; private boolean e = false; private boolean f = false; private ArrayList g = new ArrayList<>(); private Lifecycle.State b = Lifecycle.State.INITIALIZED; /* renamed from: androidx.lifecycle.LifecycleRegistry$1, reason: invalid class name */ static /* synthetic */ class AnonymousClass1 { static final /* synthetic */ int[] a; static final /* synthetic */ int[] b = new int[Lifecycle.State.values().length]; static { try { b[Lifecycle.State.INITIALIZED.ordinal()] = 1; } catch (NoSuchFieldError unused) { } try { b[Lifecycle.State.CREATED.ordinal()] = 2; } catch (NoSuchFieldError unused2) { } try { b[Lifecycle.State.STARTED.ordinal()] = 3; } catch (NoSuchFieldError unused3) { } try { b[Lifecycle.State.RESUMED.ordinal()] = 4; } catch (NoSuchFieldError unused4) { } try { b[Lifecycle.State.DESTROYED.ordinal()] = 5; } catch (NoSuchFieldError unused5) { } a = new int[Lifecycle.Event.values().length]; try { a[Lifecycle.Event.ON_CREATE.ordinal()] = 1; } catch (NoSuchFieldError unused6) { } try { a[Lifecycle.Event.ON_STOP.ordinal()] = 2; } catch (NoSuchFieldError unused7) { } try { a[Lifecycle.Event.ON_START.ordinal()] = 3; } catch (NoSuchFieldError unused8) { } try { a[Lifecycle.Event.ON_PAUSE.ordinal()] = 4; } catch (NoSuchFieldError unused9) { } try { a[Lifecycle.Event.ON_RESUME.ordinal()] = 5; } catch (NoSuchFieldError unused10) { } try { a[Lifecycle.Event.ON_DESTROY.ordinal()] = 6; } catch (NoSuchFieldError unused11) { } try { a[Lifecycle.Event.ON_ANY.ordinal()] = 7; } catch (NoSuchFieldError unused12) { } } } static class ObserverWithState { Lifecycle.State a; GenericLifecycleObserver b; ObserverWithState(LifecycleObserver lifecycleObserver, Lifecycle.State state) { this.b = Lifecycling.a(lifecycleObserver); this.a = state; } void a(LifecycleOwner lifecycleOwner, Lifecycle.Event event) { Lifecycle.State b = LifecycleRegistry.b(event); this.a = LifecycleRegistry.a(this.a, b); this.b.a(lifecycleOwner, event); this.a = b; } } public LifecycleRegistry(LifecycleOwner lifecycleOwner) { this.c = new WeakReference<>(lifecycleOwner); } private boolean b() { if (this.a.size() == 0) { return true; } Lifecycle.State state = this.a.a().getValue().a; Lifecycle.State state2 = this.a.d().getValue().a; return state == state2 && this.b == state2; } private void c(Lifecycle.State state) { if (this.b == state) { return; } this.b = state; if (this.e || this.d != 0) { this.f = true; return; } this.e = true; d(); this.e = false; } private void d(Lifecycle.State state) { this.g.add(state); } private static Lifecycle.Event e(Lifecycle.State state) { int i = AnonymousClass1.b[state.ordinal()]; if (i != 1) { if (i == 2) { return Lifecycle.Event.ON_START; } if (i == 3) { return Lifecycle.Event.ON_RESUME; } if (i == 4) { throw new IllegalArgumentException(); } if (i != 5) { throw new IllegalArgumentException("Unexpected state value " + state); } } return Lifecycle.Event.ON_CREATE; } public void a(Lifecycle.State state) { c(state); } private void d() { LifecycleOwner lifecycleOwner = this.c.get(); if (lifecycleOwner == null) { Log.w("LifecycleRegistry", "LifecycleOwner is garbage collected, you shouldn't try dispatch new events from it."); return; } while (!b()) { this.f = false; if (this.b.compareTo(this.a.a().getValue().a) < 0) { a(lifecycleOwner); } Map.Entry d = this.a.d(); if (!this.f && d != null && this.b.compareTo(d.getValue().a) > 0) { b(lifecycleOwner); } } this.f = false; } public void a(Lifecycle.Event event) { c(b(event)); } @Override // androidx.lifecycle.Lifecycle public void a(LifecycleObserver lifecycleObserver) { LifecycleOwner lifecycleOwner; Lifecycle.State state = this.b; Lifecycle.State state2 = Lifecycle.State.DESTROYED; if (state != state2) { state2 = Lifecycle.State.INITIALIZED; } ObserverWithState observerWithState = new ObserverWithState(lifecycleObserver, state2); if (this.a.b(lifecycleObserver, observerWithState) == null && (lifecycleOwner = this.c.get()) != null) { boolean z = this.d != 0 || this.e; Lifecycle.State c = c(lifecycleObserver); this.d++; while (observerWithState.a.compareTo(c) < 0 && this.a.contains(lifecycleObserver)) { d(observerWithState.a); observerWithState.a(lifecycleOwner, e(observerWithState.a)); c(); c = c(lifecycleObserver); } if (!z) { d(); } this.d--; } } @Override // androidx.lifecycle.Lifecycle public void b(LifecycleObserver lifecycleObserver) { this.a.remove(lifecycleObserver); } static Lifecycle.State b(Lifecycle.Event event) { switch (AnonymousClass1.a[event.ordinal()]) { case 1: case 2: return Lifecycle.State.CREATED; case 3: case 4: return Lifecycle.State.STARTED; case 5: return Lifecycle.State.RESUMED; case 6: return Lifecycle.State.DESTROYED; default: throw new IllegalArgumentException("Unexpected event value " + event); } } private Lifecycle.State c(LifecycleObserver lifecycleObserver) { Map.Entry b = this.a.b(lifecycleObserver); Lifecycle.State state = null; Lifecycle.State state2 = b != null ? b.getValue().a : null; if (!this.g.isEmpty()) { state = this.g.get(r0.size() - 1); } return a(a(this.b, state2), state); } private static Lifecycle.Event b(Lifecycle.State state) { int i = AnonymousClass1.b[state.ordinal()]; if (i == 1) { throw new IllegalArgumentException(); } if (i == 2) { return Lifecycle.Event.ON_DESTROY; } if (i == 3) { return Lifecycle.Event.ON_STOP; } if (i == 4) { return Lifecycle.Event.ON_PAUSE; } if (i != 5) { throw new IllegalArgumentException("Unexpected state value " + state); } throw new IllegalArgumentException(); } private void c() { this.g.remove(r0.size() - 1); } /* JADX WARN: Multi-variable type inference failed */ private void b(LifecycleOwner lifecycleOwner) { SafeIterableMap.IteratorWithAdditions b = this.a.b(); while (b.hasNext() && !this.f) { Map.Entry next = b.next(); ObserverWithState observerWithState = (ObserverWithState) next.getValue(); while (observerWithState.a.compareTo(this.b) < 0 && !this.f && this.a.contains(next.getKey())) { d(observerWithState.a); observerWithState.a(lifecycleOwner, e(observerWithState.a)); c(); } } } @Override // androidx.lifecycle.Lifecycle public Lifecycle.State a() { return this.b; } private void a(LifecycleOwner lifecycleOwner) { Iterator> descendingIterator = this.a.descendingIterator(); while (descendingIterator.hasNext() && !this.f) { Map.Entry next = descendingIterator.next(); ObserverWithState value = next.getValue(); while (value.a.compareTo(this.b) > 0 && !this.f && this.a.contains(next.getKey())) { Lifecycle.Event b = b(value.a); d(b(b)); value.a(lifecycleOwner, b); c(); } } } static Lifecycle.State a(Lifecycle.State state, Lifecycle.State state2) { return (state2 == null || state2.compareTo(state) >= 0) ? state : state2; } }