248 lines
11 KiB
Java
248 lines
11 KiB
Java
package androidx.lifecycle;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import androidx.annotation.MainThread;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
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 b.d.b.a.a;
|
|
import java.lang.ref.WeakReference;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
public class LifecycleRegistry extends Lifecycle {
|
|
private int mAddingObserverCounter;
|
|
private final boolean mEnforceMainThread;
|
|
private boolean mHandlingEvent;
|
|
private final WeakReference<LifecycleOwner> mLifecycleOwner;
|
|
private boolean mNewEventOccurred;
|
|
private FastSafeIterableMap<LifecycleObserver, ObserverWithState> mObserverMap;
|
|
private ArrayList<Lifecycle.State> mParentStates;
|
|
private Lifecycle.State mState;
|
|
|
|
public static class ObserverWithState {
|
|
public LifecycleEventObserver mLifecycleObserver;
|
|
public Lifecycle.State mState;
|
|
|
|
public ObserverWithState(LifecycleObserver lifecycleObserver, Lifecycle.State state) {
|
|
this.mLifecycleObserver = Lifecycling.lifecycleEventObserver(lifecycleObserver);
|
|
this.mState = state;
|
|
}
|
|
|
|
public void dispatchEvent(LifecycleOwner lifecycleOwner, Lifecycle.Event event) {
|
|
Lifecycle.State targetState = event.getTargetState();
|
|
this.mState = LifecycleRegistry.min(this.mState, targetState);
|
|
this.mLifecycleObserver.onStateChanged(lifecycleOwner, event);
|
|
this.mState = targetState;
|
|
}
|
|
}
|
|
|
|
public LifecycleRegistry(@NonNull LifecycleOwner lifecycleOwner) {
|
|
this(lifecycleOwner, true);
|
|
}
|
|
|
|
private LifecycleRegistry(@NonNull LifecycleOwner lifecycleOwner, boolean z2) {
|
|
this.mObserverMap = new FastSafeIterableMap<>();
|
|
this.mAddingObserverCounter = 0;
|
|
this.mHandlingEvent = false;
|
|
this.mNewEventOccurred = false;
|
|
this.mParentStates = new ArrayList<>();
|
|
this.mLifecycleOwner = new WeakReference<>(lifecycleOwner);
|
|
this.mState = Lifecycle.State.INITIALIZED;
|
|
this.mEnforceMainThread = z2;
|
|
}
|
|
|
|
private void backwardPass(LifecycleOwner lifecycleOwner) {
|
|
Iterator<Map.Entry<LifecycleObserver, ObserverWithState>> descendingIterator = this.mObserverMap.descendingIterator();
|
|
while (descendingIterator.hasNext() && !this.mNewEventOccurred) {
|
|
Map.Entry<LifecycleObserver, ObserverWithState> next = descendingIterator.next();
|
|
ObserverWithState value = next.getValue();
|
|
while (value.mState.compareTo(this.mState) > 0 && !this.mNewEventOccurred && this.mObserverMap.contains(next.getKey())) {
|
|
Lifecycle.Event downFrom = Lifecycle.Event.downFrom(value.mState);
|
|
if (downFrom != null) {
|
|
pushParentState(downFrom.getTargetState());
|
|
value.dispatchEvent(lifecycleOwner, downFrom);
|
|
popParentState();
|
|
} else {
|
|
StringBuilder R = a.R("no event down from ");
|
|
R.append(value.mState);
|
|
throw new IllegalStateException(R.toString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private Lifecycle.State calculateTargetState(LifecycleObserver lifecycleObserver) {
|
|
Map.Entry<LifecycleObserver, ObserverWithState> ceil = this.mObserverMap.ceil(lifecycleObserver);
|
|
Lifecycle.State state = null;
|
|
Lifecycle.State state2 = ceil != null ? ceil.getValue().mState : null;
|
|
if (!this.mParentStates.isEmpty()) {
|
|
ArrayList<Lifecycle.State> arrayList = this.mParentStates;
|
|
state = arrayList.get(arrayList.size() - 1);
|
|
}
|
|
return min(min(this.mState, state2), state);
|
|
}
|
|
|
|
@NonNull
|
|
@VisibleForTesting
|
|
public static LifecycleRegistry createUnsafe(@NonNull LifecycleOwner lifecycleOwner) {
|
|
return new LifecycleRegistry(lifecycleOwner, false);
|
|
}
|
|
|
|
@SuppressLint({"RestrictedApi"})
|
|
private void enforceMainThreadIfNeeded(String str) {
|
|
if (this.mEnforceMainThread && !ArchTaskExecutor.getInstance().isMainThread()) {
|
|
throw new IllegalStateException(a.w("Method ", str, " must be called on the main thread"));
|
|
}
|
|
}
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r3v3, resolved type: androidx.arch.core.internal.FastSafeIterableMap<androidx.lifecycle.LifecycleObserver, androidx.lifecycle.LifecycleRegistry$ObserverWithState> */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
private void forwardPass(LifecycleOwner lifecycleOwner) {
|
|
SafeIterableMap<LifecycleObserver, ObserverWithState>.IteratorWithAdditions iteratorWithAdditions = this.mObserverMap.iteratorWithAdditions();
|
|
while (iteratorWithAdditions.hasNext() && !this.mNewEventOccurred) {
|
|
Map.Entry next = iteratorWithAdditions.next();
|
|
ObserverWithState observerWithState = (ObserverWithState) next.getValue();
|
|
while (observerWithState.mState.compareTo(this.mState) < 0 && !this.mNewEventOccurred && this.mObserverMap.contains(next.getKey())) {
|
|
pushParentState(observerWithState.mState);
|
|
Lifecycle.Event upFrom = Lifecycle.Event.upFrom(observerWithState.mState);
|
|
if (upFrom != null) {
|
|
observerWithState.dispatchEvent(lifecycleOwner, upFrom);
|
|
popParentState();
|
|
} else {
|
|
StringBuilder R = a.R("no event up from ");
|
|
R.append(observerWithState.mState);
|
|
throw new IllegalStateException(R.toString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean isSynced() {
|
|
if (this.mObserverMap.size() == 0) {
|
|
return true;
|
|
}
|
|
Lifecycle.State state = this.mObserverMap.eldest().getValue().mState;
|
|
Lifecycle.State state2 = this.mObserverMap.newest().getValue().mState;
|
|
return state == state2 && this.mState == state2;
|
|
}
|
|
|
|
public static Lifecycle.State min(@NonNull Lifecycle.State state, @Nullable Lifecycle.State state2) {
|
|
return (state2 == null || state2.compareTo(state) >= 0) ? state : state2;
|
|
}
|
|
|
|
private void moveToState(Lifecycle.State state) {
|
|
if (this.mState != state) {
|
|
this.mState = state;
|
|
if (this.mHandlingEvent || this.mAddingObserverCounter != 0) {
|
|
this.mNewEventOccurred = true;
|
|
return;
|
|
}
|
|
this.mHandlingEvent = true;
|
|
sync();
|
|
this.mHandlingEvent = false;
|
|
}
|
|
}
|
|
|
|
private void popParentState() {
|
|
ArrayList<Lifecycle.State> arrayList = this.mParentStates;
|
|
arrayList.remove(arrayList.size() - 1);
|
|
}
|
|
|
|
private void pushParentState(Lifecycle.State state) {
|
|
this.mParentStates.add(state);
|
|
}
|
|
|
|
private void sync() {
|
|
LifecycleOwner lifecycleOwner = this.mLifecycleOwner.get();
|
|
if (lifecycleOwner != null) {
|
|
while (!isSynced()) {
|
|
this.mNewEventOccurred = false;
|
|
if (this.mState.compareTo(this.mObserverMap.eldest().getValue().mState) < 0) {
|
|
backwardPass(lifecycleOwner);
|
|
}
|
|
Map.Entry<LifecycleObserver, ObserverWithState> newest = this.mObserverMap.newest();
|
|
if (!this.mNewEventOccurred && newest != null && this.mState.compareTo(newest.getValue().mState) > 0) {
|
|
forwardPass(lifecycleOwner);
|
|
}
|
|
}
|
|
this.mNewEventOccurred = false;
|
|
return;
|
|
}
|
|
throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is alreadygarbage collected. It is too late to change lifecycle state.");
|
|
}
|
|
|
|
@Override // androidx.lifecycle.Lifecycle
|
|
public void addObserver(@NonNull LifecycleObserver lifecycleObserver) {
|
|
LifecycleOwner lifecycleOwner;
|
|
enforceMainThreadIfNeeded("addObserver");
|
|
Lifecycle.State state = this.mState;
|
|
Lifecycle.State state2 = Lifecycle.State.DESTROYED;
|
|
if (state != state2) {
|
|
state2 = Lifecycle.State.INITIALIZED;
|
|
}
|
|
ObserverWithState observerWithState = new ObserverWithState(lifecycleObserver, state2);
|
|
if (this.mObserverMap.putIfAbsent(lifecycleObserver, observerWithState) == null && (lifecycleOwner = this.mLifecycleOwner.get()) != null) {
|
|
boolean z2 = this.mAddingObserverCounter != 0 || this.mHandlingEvent;
|
|
Lifecycle.State calculateTargetState = calculateTargetState(lifecycleObserver);
|
|
this.mAddingObserverCounter++;
|
|
while (observerWithState.mState.compareTo(calculateTargetState) < 0 && this.mObserverMap.contains(lifecycleObserver)) {
|
|
pushParentState(observerWithState.mState);
|
|
Lifecycle.Event upFrom = Lifecycle.Event.upFrom(observerWithState.mState);
|
|
if (upFrom != null) {
|
|
observerWithState.dispatchEvent(lifecycleOwner, upFrom);
|
|
popParentState();
|
|
calculateTargetState = calculateTargetState(lifecycleObserver);
|
|
} else {
|
|
StringBuilder R = a.R("no event up from ");
|
|
R.append(observerWithState.mState);
|
|
throw new IllegalStateException(R.toString());
|
|
}
|
|
}
|
|
if (!z2) {
|
|
sync();
|
|
}
|
|
this.mAddingObserverCounter--;
|
|
}
|
|
}
|
|
|
|
@Override // androidx.lifecycle.Lifecycle
|
|
@NonNull
|
|
public Lifecycle.State getCurrentState() {
|
|
return this.mState;
|
|
}
|
|
|
|
public int getObserverCount() {
|
|
enforceMainThreadIfNeeded("getObserverCount");
|
|
return this.mObserverMap.size();
|
|
}
|
|
|
|
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
|
|
enforceMainThreadIfNeeded("handleLifecycleEvent");
|
|
moveToState(event.getTargetState());
|
|
}
|
|
|
|
@MainThread
|
|
@Deprecated
|
|
public void markState(@NonNull Lifecycle.State state) {
|
|
enforceMainThreadIfNeeded("markState");
|
|
setCurrentState(state);
|
|
}
|
|
|
|
@Override // androidx.lifecycle.Lifecycle
|
|
public void removeObserver(@NonNull LifecycleObserver lifecycleObserver) {
|
|
enforceMainThreadIfNeeded("removeObserver");
|
|
this.mObserverMap.remove(lifecycleObserver);
|
|
}
|
|
|
|
@MainThread
|
|
public void setCurrentState(@NonNull Lifecycle.State state) {
|
|
enforceMainThreadIfNeeded("setCurrentState");
|
|
moveToState(state);
|
|
}
|
|
}
|