package androidx.lifecycle; import android.annotation.SuppressLint; import androidx.annotation.MainThread; import androidx.annotation.VisibleForTesting; import androidx.arch.core.executor.ArchTaskExecutor; 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; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; /* loaded from: classes.dex */ public class LifecycleRegistry extends Lifecycle { public static final Companion Companion = new Companion(null); private int addingObserverCounter; private final boolean enforceMainThread; private boolean handlingEvent; private final WeakReference lifecycleOwner; private boolean newEventOccurred; private FastSafeIterableMap observerMap; private ArrayList parentStates; private Lifecycle.State state; public /* synthetic */ LifecycleRegistry(LifecycleOwner lifecycleOwner, boolean z, DefaultConstructorMarker defaultConstructorMarker) { this(lifecycleOwner, z); } @VisibleForTesting public static final LifecycleRegistry createUnsafe(LifecycleOwner lifecycleOwner) { return Companion.createUnsafe(lifecycleOwner); } @Override // androidx.lifecycle.Lifecycle public Lifecycle.State getCurrentState() { return this.state; } private LifecycleRegistry(LifecycleOwner lifecycleOwner, boolean z) { this.enforceMainThread = z; this.observerMap = new FastSafeIterableMap<>(); this.state = Lifecycle.State.INITIALIZED; this.parentStates = new ArrayList<>(); this.lifecycleOwner = new WeakReference<>(lifecycleOwner); } /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */ public LifecycleRegistry(LifecycleOwner provider) { this(provider, true); Intrinsics.checkNotNullParameter(provider, "provider"); } @MainThread public void markState(Lifecycle.State state) { Intrinsics.checkNotNullParameter(state, "state"); enforceMainThreadIfNeeded("markState"); setCurrentState(state); } public void setCurrentState(Lifecycle.State state) { Intrinsics.checkNotNullParameter(state, "state"); enforceMainThreadIfNeeded("setCurrentState"); moveToState(state); } public void handleLifecycleEvent(Lifecycle.Event event) { Intrinsics.checkNotNullParameter(event, "event"); enforceMainThreadIfNeeded("handleLifecycleEvent"); moveToState(event.getTargetState()); } private final void moveToState(Lifecycle.State state) { Lifecycle.State state2 = this.state; if (state2 == state) { return; } if (state2 == Lifecycle.State.INITIALIZED && state == Lifecycle.State.DESTROYED) { throw new IllegalStateException(("no event down from " + this.state + " in component " + this.lifecycleOwner.get()).toString()); } this.state = state; if (this.handlingEvent || this.addingObserverCounter != 0) { this.newEventOccurred = true; return; } this.handlingEvent = true; sync(); this.handlingEvent = false; if (this.state == Lifecycle.State.DESTROYED) { this.observerMap = new FastSafeIterableMap<>(); } } private final boolean isSynced() { if (this.observerMap.size() == 0) { return true; } Map.Entry eldest = this.observerMap.eldest(); Intrinsics.checkNotNull(eldest); Lifecycle.State state = eldest.getValue().getState(); Map.Entry newest = this.observerMap.newest(); Intrinsics.checkNotNull(newest); Lifecycle.State state2 = newest.getValue().getState(); return state == state2 && this.state == state2; } private final Lifecycle.State calculateTargetState(LifecycleObserver lifecycleObserver) { ObserverWithState value; Map.Entry ceil = this.observerMap.ceil(lifecycleObserver); Lifecycle.State state = null; Lifecycle.State state2 = (ceil == null || (value = ceil.getValue()) == null) ? null : value.getState(); if (!this.parentStates.isEmpty()) { state = this.parentStates.get(r0.size() - 1); } Companion companion = Companion; return companion.min$lifecycle_runtime_release(companion.min$lifecycle_runtime_release(this.state, state2), state); } @Override // androidx.lifecycle.Lifecycle public void addObserver(LifecycleObserver observer) { LifecycleOwner lifecycleOwner; Intrinsics.checkNotNullParameter(observer, "observer"); enforceMainThreadIfNeeded("addObserver"); Lifecycle.State state = this.state; Lifecycle.State state2 = Lifecycle.State.DESTROYED; if (state != state2) { state2 = Lifecycle.State.INITIALIZED; } ObserverWithState observerWithState = new ObserverWithState(observer, state2); if (this.observerMap.putIfAbsent(observer, observerWithState) == null && (lifecycleOwner = this.lifecycleOwner.get()) != null) { boolean z = this.addingObserverCounter != 0 || this.handlingEvent; Lifecycle.State calculateTargetState = calculateTargetState(observer); this.addingObserverCounter++; while (observerWithState.getState().compareTo(calculateTargetState) < 0 && this.observerMap.contains(observer)) { pushParentState(observerWithState.getState()); Lifecycle.Event upFrom = Lifecycle.Event.Companion.upFrom(observerWithState.getState()); if (upFrom == null) { throw new IllegalStateException("no event up from " + observerWithState.getState()); } observerWithState.dispatchEvent(lifecycleOwner, upFrom); popParentState(); calculateTargetState = calculateTargetState(observer); } if (!z) { sync(); } this.addingObserverCounter--; } } private final void popParentState() { this.parentStates.remove(r0.size() - 1); } private final void pushParentState(Lifecycle.State state) { this.parentStates.add(state); } @Override // androidx.lifecycle.Lifecycle public void removeObserver(LifecycleObserver observer) { Intrinsics.checkNotNullParameter(observer, "observer"); enforceMainThreadIfNeeded("removeObserver"); this.observerMap.remove(observer); } public int getObserverCount() { enforceMainThreadIfNeeded("getObserverCount"); return this.observerMap.size(); } private final void forwardPass(LifecycleOwner lifecycleOwner) { SafeIterableMap.IteratorWithAdditions iteratorWithAdditions = this.observerMap.iteratorWithAdditions(); Intrinsics.checkNotNullExpressionValue(iteratorWithAdditions, "observerMap.iteratorWithAdditions()"); while (iteratorWithAdditions.hasNext() && !this.newEventOccurred) { Map.Entry next = iteratorWithAdditions.next(); LifecycleObserver lifecycleObserver = (LifecycleObserver) next.getKey(); ObserverWithState observerWithState = (ObserverWithState) next.getValue(); while (observerWithState.getState().compareTo(this.state) < 0 && !this.newEventOccurred && this.observerMap.contains(lifecycleObserver)) { pushParentState(observerWithState.getState()); Lifecycle.Event upFrom = Lifecycle.Event.Companion.upFrom(observerWithState.getState()); if (upFrom == null) { throw new IllegalStateException("no event up from " + observerWithState.getState()); } observerWithState.dispatchEvent(lifecycleOwner, upFrom); popParentState(); } } } private final void backwardPass(LifecycleOwner lifecycleOwner) { Iterator> descendingIterator = this.observerMap.descendingIterator(); Intrinsics.checkNotNullExpressionValue(descendingIterator, "observerMap.descendingIterator()"); while (descendingIterator.hasNext() && !this.newEventOccurred) { Map.Entry next = descendingIterator.next(); Intrinsics.checkNotNullExpressionValue(next, "next()"); LifecycleObserver key = next.getKey(); ObserverWithState value = next.getValue(); while (value.getState().compareTo(this.state) > 0 && !this.newEventOccurred && this.observerMap.contains(key)) { Lifecycle.Event downFrom = Lifecycle.Event.Companion.downFrom(value.getState()); if (downFrom == null) { throw new IllegalStateException("no event down from " + value.getState()); } pushParentState(downFrom.getTargetState()); value.dispatchEvent(lifecycleOwner, downFrom); popParentState(); } } } private final void sync() { LifecycleOwner lifecycleOwner = this.lifecycleOwner.get(); if (lifecycleOwner == null) { throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is already garbage collected. It is too late to change lifecycle state."); } while (!isSynced()) { this.newEventOccurred = false; Lifecycle.State state = this.state; Map.Entry eldest = this.observerMap.eldest(); Intrinsics.checkNotNull(eldest); if (state.compareTo(eldest.getValue().getState()) < 0) { backwardPass(lifecycleOwner); } Map.Entry newest = this.observerMap.newest(); if (!this.newEventOccurred && newest != null && this.state.compareTo(newest.getValue().getState()) > 0) { forwardPass(lifecycleOwner); } } this.newEventOccurred = false; } @SuppressLint({"RestrictedApi"}) private final void enforceMainThreadIfNeeded(String str) { if (!this.enforceMainThread || ArchTaskExecutor.getInstance().isMainThread()) { return; } throw new IllegalStateException(("Method " + str + " must be called on the main thread").toString()); } public static final class ObserverWithState { private LifecycleEventObserver lifecycleObserver; private Lifecycle.State state; public final LifecycleEventObserver getLifecycleObserver() { return this.lifecycleObserver; } public final Lifecycle.State getState() { return this.state; } public final void setLifecycleObserver(LifecycleEventObserver lifecycleEventObserver) { Intrinsics.checkNotNullParameter(lifecycleEventObserver, ""); this.lifecycleObserver = lifecycleEventObserver; } public final void setState(Lifecycle.State state) { Intrinsics.checkNotNullParameter(state, ""); this.state = state; } public ObserverWithState(LifecycleObserver lifecycleObserver, Lifecycle.State initialState) { Intrinsics.checkNotNullParameter(initialState, "initialState"); Intrinsics.checkNotNull(lifecycleObserver); this.lifecycleObserver = Lifecycling.lifecycleEventObserver(lifecycleObserver); this.state = initialState; } public final void dispatchEvent(LifecycleOwner lifecycleOwner, Lifecycle.Event event) { Intrinsics.checkNotNullParameter(event, "event"); Lifecycle.State targetState = event.getTargetState(); this.state = LifecycleRegistry.Companion.min$lifecycle_runtime_release(this.state, targetState); LifecycleEventObserver lifecycleEventObserver = this.lifecycleObserver; Intrinsics.checkNotNull(lifecycleOwner); lifecycleEventObserver.onStateChanged(lifecycleOwner, event); this.state = targetState; } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } @VisibleForTesting public final LifecycleRegistry createUnsafe(LifecycleOwner owner) { Intrinsics.checkNotNullParameter(owner, "owner"); return new LifecycleRegistry(owner, false, null); } public final Lifecycle.State min$lifecycle_runtime_release(Lifecycle.State state1, Lifecycle.State state) { Intrinsics.checkNotNullParameter(state1, "state1"); return (state == null || state.compareTo(state1) >= 0) ? state1 : state; } } }