2021-07-24 02:37:17 +00:00
|
|
|
package androidx.lifecycle;
|
|
|
|
|
|
|
|
import androidx.annotation.MainThread;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.arch.core.executor.ArchTaskExecutor;
|
|
|
|
import androidx.arch.core.internal.SafeIterableMap;
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
|
|
|
import c.d.b.a.a;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
|
|
|
public abstract class LiveData<T> {
|
|
|
|
public static final Object NOT_SET = new Object();
|
|
|
|
public static final int START_VERSION = -1;
|
|
|
|
public int mActiveCount;
|
|
|
|
private boolean mChangingActiveState;
|
|
|
|
private volatile Object mData;
|
|
|
|
public final Object mDataLock;
|
|
|
|
private boolean mDispatchInvalidated;
|
|
|
|
private boolean mDispatchingValue;
|
|
|
|
private SafeIterableMap<Observer<? super T>, LiveData<T>.ObserverWrapper> mObservers;
|
|
|
|
public volatile Object mPendingData;
|
|
|
|
private final Runnable mPostValueRunnable;
|
|
|
|
private int mVersion;
|
|
|
|
|
|
|
|
/* renamed from: androidx.lifecycle.LiveData$1 reason: invalid class name */
|
|
|
|
public class AnonymousClass1 implements Runnable {
|
|
|
|
public AnonymousClass1() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r0v2, resolved type: androidx.lifecycle.LiveData */
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
|
|
@Override // java.lang.Runnable
|
|
|
|
public void run() {
|
|
|
|
Object obj;
|
|
|
|
synchronized (LiveData.this.mDataLock) {
|
|
|
|
obj = LiveData.this.mPendingData;
|
|
|
|
LiveData.this.mPendingData = LiveData.NOT_SET;
|
|
|
|
}
|
|
|
|
LiveData.this.setValue(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class AlwaysActiveObserver extends LiveData<T>.ObserverWrapper {
|
|
|
|
public AlwaysActiveObserver(Observer<? super T> observer) {
|
|
|
|
super(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.lifecycle.LiveData.ObserverWrapper
|
|
|
|
public boolean shouldBeActive() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class LifecycleBoundObserver extends LiveData<T>.ObserverWrapper implements LifecycleEventObserver {
|
|
|
|
@NonNull
|
|
|
|
public final LifecycleOwner mOwner;
|
|
|
|
|
|
|
|
public LifecycleBoundObserver(@NonNull LifecycleOwner lifecycleOwner, Observer<? super T> observer) {
|
|
|
|
super(observer);
|
|
|
|
this.mOwner = lifecycleOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.lifecycle.LiveData.ObserverWrapper
|
|
|
|
public void detachObserver() {
|
|
|
|
this.mOwner.getLifecycle().removeObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.lifecycle.LiveData.ObserverWrapper
|
|
|
|
public boolean isAttachedTo(LifecycleOwner lifecycleOwner) {
|
|
|
|
return this.mOwner == lifecycleOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.lifecycle.LifecycleEventObserver
|
|
|
|
public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event) {
|
|
|
|
Lifecycle.State currentState = this.mOwner.getLifecycle().getCurrentState();
|
|
|
|
if (currentState == Lifecycle.State.DESTROYED) {
|
|
|
|
LiveData.this.removeObserver(this.mObserver);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Lifecycle.State state = null;
|
|
|
|
while (state != currentState) {
|
|
|
|
activeStateChanged(shouldBeActive());
|
|
|
|
state = currentState;
|
|
|
|
currentState = this.mOwner.getLifecycle().getCurrentState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.lifecycle.LiveData.ObserverWrapper
|
|
|
|
public boolean shouldBeActive() {
|
|
|
|
return this.mOwner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class ObserverWrapper {
|
|
|
|
public boolean mActive;
|
|
|
|
public int mLastVersion = -1;
|
|
|
|
public final Observer<? super T> mObserver;
|
|
|
|
|
|
|
|
public ObserverWrapper(Observer<? super T> observer) {
|
|
|
|
this.mObserver = observer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void activeStateChanged(boolean z2) {
|
|
|
|
if (z2 != this.mActive) {
|
|
|
|
this.mActive = z2;
|
|
|
|
LiveData.this.changeActiveCounter(z2 ? 1 : -1);
|
|
|
|
if (this.mActive) {
|
|
|
|
LiveData.this.dispatchingValue(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void detachObserver() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAttachedTo(LifecycleOwner lifecycleOwner) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract boolean shouldBeActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData() {
|
|
|
|
this.mDataLock = new Object();
|
|
|
|
this.mObservers = new SafeIterableMap<>();
|
|
|
|
this.mActiveCount = 0;
|
|
|
|
Object obj = NOT_SET;
|
|
|
|
this.mPendingData = obj;
|
|
|
|
this.mPostValueRunnable = new AnonymousClass1();
|
|
|
|
this.mData = obj;
|
|
|
|
this.mVersion = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData(T t) {
|
|
|
|
this.mDataLock = new Object();
|
|
|
|
this.mObservers = new SafeIterableMap<>();
|
|
|
|
this.mActiveCount = 0;
|
|
|
|
this.mPendingData = NOT_SET;
|
|
|
|
this.mPostValueRunnable = new AnonymousClass1();
|
|
|
|
this.mData = t;
|
|
|
|
this.mVersion = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void assertMainThread(String str) {
|
|
|
|
if (!ArchTaskExecutor.getInstance().isMainThread()) {
|
2021-11-05 06:48:17 +00:00
|
|
|
throw new IllegalStateException(a.w("Cannot invoke ", str, " on a background thread"));
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void considerNotify(LiveData<T>.ObserverWrapper observerWrapper) {
|
|
|
|
if (observerWrapper.mActive) {
|
|
|
|
if (!observerWrapper.shouldBeActive()) {
|
|
|
|
observerWrapper.activeStateChanged(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int i = observerWrapper.mLastVersion;
|
|
|
|
int i2 = this.mVersion;
|
|
|
|
if (i < i2) {
|
|
|
|
observerWrapper.mLastVersion = i2;
|
|
|
|
observerWrapper.mObserver.onChanged((Object) this.mData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
public void changeActiveCounter(int i) {
|
|
|
|
int i2 = this.mActiveCount;
|
|
|
|
this.mActiveCount = i + i2;
|
|
|
|
if (!this.mChangingActiveState) {
|
|
|
|
this.mChangingActiveState = true;
|
|
|
|
while (true) {
|
|
|
|
boolean z2 = false;
|
|
|
|
try {
|
|
|
|
int i3 = this.mActiveCount;
|
|
|
|
if (i2 != i3) {
|
|
|
|
boolean z3 = i2 == 0 && i3 > 0;
|
|
|
|
boolean z4 = i2 > 0 && i3 == 0;
|
|
|
|
if (z3) {
|
|
|
|
onActive();
|
|
|
|
} else if (z4) {
|
|
|
|
onInactive();
|
|
|
|
}
|
|
|
|
i2 = i3;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this.mChangingActiveState = z2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void dispatchingValue(@Nullable LiveData<T>.ObserverWrapper observerWrapper) {
|
|
|
|
if (this.mDispatchingValue) {
|
|
|
|
this.mDispatchInvalidated = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.mDispatchingValue = true;
|
|
|
|
do {
|
|
|
|
this.mDispatchInvalidated = false;
|
|
|
|
if (observerWrapper == null) {
|
|
|
|
SafeIterableMap<Observer<? super T>, LiveData<T>.ObserverWrapper>.IteratorWithAdditions iteratorWithAdditions = this.mObservers.iteratorWithAdditions();
|
|
|
|
while (iteratorWithAdditions.hasNext()) {
|
|
|
|
considerNotify((ObserverWrapper) iteratorWithAdditions.next().getValue());
|
|
|
|
if (this.mDispatchInvalidated) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
considerNotify(observerWrapper);
|
|
|
|
observerWrapper = null;
|
|
|
|
}
|
|
|
|
} while (this.mDispatchInvalidated);
|
|
|
|
this.mDispatchingValue = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public T getValue() {
|
|
|
|
T t = (T) this.mData;
|
|
|
|
if (t != NOT_SET) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getVersion() {
|
|
|
|
return this.mVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasActiveObservers() {
|
|
|
|
return this.mActiveCount > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasObservers() {
|
|
|
|
return this.mObservers.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
public void observe(@NonNull LifecycleOwner lifecycleOwner, @NonNull Observer<? super T> observer) {
|
|
|
|
assertMainThread("observe");
|
|
|
|
if (lifecycleOwner.getLifecycle().getCurrentState() != Lifecycle.State.DESTROYED) {
|
|
|
|
LifecycleBoundObserver lifecycleBoundObserver = new LifecycleBoundObserver(lifecycleOwner, observer);
|
|
|
|
LiveData<T>.ObserverWrapper putIfAbsent = this.mObservers.putIfAbsent(observer, lifecycleBoundObserver);
|
|
|
|
if (putIfAbsent != null && !putIfAbsent.isAttachedTo(lifecycleOwner)) {
|
|
|
|
throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
|
|
|
|
} else if (putIfAbsent == null) {
|
|
|
|
lifecycleOwner.getLifecycle().addObserver(lifecycleBoundObserver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
public void observeForever(@NonNull Observer<? super T> observer) {
|
|
|
|
assertMainThread("observeForever");
|
|
|
|
AlwaysActiveObserver alwaysActiveObserver = new AlwaysActiveObserver(observer);
|
|
|
|
LiveData<T>.ObserverWrapper putIfAbsent = this.mObservers.putIfAbsent(observer, alwaysActiveObserver);
|
|
|
|
if (putIfAbsent instanceof LifecycleBoundObserver) {
|
|
|
|
throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
|
|
|
|
} else if (putIfAbsent == null) {
|
|
|
|
alwaysActiveObserver.activeStateChanged(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onActive() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onInactive() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void postValue(T t) {
|
|
|
|
boolean z2;
|
|
|
|
synchronized (this.mDataLock) {
|
|
|
|
z2 = this.mPendingData == NOT_SET;
|
|
|
|
this.mPendingData = t;
|
|
|
|
}
|
|
|
|
if (z2) {
|
|
|
|
ArchTaskExecutor.getInstance().postToMainThread(this.mPostValueRunnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
public void removeObserver(@NonNull Observer<? super T> observer) {
|
|
|
|
assertMainThread("removeObserver");
|
|
|
|
LiveData<T>.ObserverWrapper remove = this.mObservers.remove(observer);
|
|
|
|
if (remove != null) {
|
|
|
|
remove.detachObserver();
|
|
|
|
remove.activeStateChanged(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
public void removeObservers(@NonNull LifecycleOwner lifecycleOwner) {
|
|
|
|
assertMainThread("removeObservers");
|
|
|
|
Iterator<Map.Entry<Observer<? super T>, LiveData<T>.ObserverWrapper>> it = this.mObservers.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry<Observer<? super T>, LiveData<T>.ObserverWrapper> next = it.next();
|
|
|
|
if (next.getValue().isAttachedTo(lifecycleOwner)) {
|
|
|
|
removeObserver(next.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
public void setValue(T t) {
|
|
|
|
assertMainThread("setValue");
|
|
|
|
this.mVersion++;
|
|
|
|
this.mData = t;
|
|
|
|
dispatchingValue(null);
|
|
|
|
}
|
|
|
|
}
|