package androidx.lifecycle; import androidx.arch.core.executor.ArchTaskExecutor; import androidx.arch.core.internal.SafeIterableMap; import androidx.lifecycle.Lifecycle; import java.util.Iterator; import java.util.Map; /* loaded from: classes2.dex */ public abstract class LiveData { static final Object NOT_SET = new Object(); static final int START_VERSION = -1; int mActiveCount; private boolean mChangingActiveState; private volatile Object mData; final Object mDataLock; private boolean mDispatchInvalidated; private boolean mDispatchingValue; private SafeIterableMap, LiveData.ObserverWrapper> mObservers; volatile Object mPendingData; private final Runnable mPostValueRunnable; private int mVersion; public T getValue() { T t = (T) this.mData; if (t != NOT_SET) { return t; } return null; } /* JADX INFO: Access modifiers changed from: package-private */ public int getVersion() { return this.mVersion; } public boolean hasActiveObservers() { return this.mActiveCount > 0; } public boolean isInitialized() { return this.mData != NOT_SET; } /* JADX INFO: Access modifiers changed from: protected */ public void onActive() { } /* JADX INFO: Access modifiers changed from: protected */ public void onInactive() { } public LiveData(T t) { this.mDataLock = new Object(); this.mObservers = new SafeIterableMap<>(); this.mActiveCount = 0; this.mPendingData = NOT_SET; this.mPostValueRunnable = new Runnable() { // from class: androidx.lifecycle.LiveData.1 /* 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); } }; this.mData = t; this.mVersion = 0; } public LiveData() { this.mDataLock = new Object(); this.mObservers = new SafeIterableMap<>(); this.mActiveCount = 0; Object obj = NOT_SET; this.mPendingData = obj; this.mPostValueRunnable = new Runnable() { // from class: androidx.lifecycle.LiveData.1 /* JADX WARN: Multi-variable type inference failed */ @Override // java.lang.Runnable public void run() { Object obj2; synchronized (LiveData.this.mDataLock) { obj2 = LiveData.this.mPendingData; LiveData.this.mPendingData = LiveData.NOT_SET; } LiveData.this.setValue(obj2); } }; this.mData = obj; this.mVersion = -1; } private void considerNotify(LiveData.ObserverWrapper observerWrapper) { if (observerWrapper.mActive) { if (!observerWrapper.shouldBeActive()) { observerWrapper.activeStateChanged(false); return; } int i = observerWrapper.mLastVersion; int i2 = this.mVersion; if (i >= i2) { return; } observerWrapper.mLastVersion = i2; observerWrapper.mObserver.onChanged((Object) this.mData); } } void dispatchingValue(LiveData.ObserverWrapper observerWrapper) { if (this.mDispatchingValue) { this.mDispatchInvalidated = true; return; } this.mDispatchingValue = true; do { this.mDispatchInvalidated = false; if (observerWrapper != null) { considerNotify(observerWrapper); observerWrapper = null; } else { SafeIterableMap, LiveData.ObserverWrapper>.IteratorWithAdditions iteratorWithAdditions = this.mObservers.iteratorWithAdditions(); while (iteratorWithAdditions.hasNext()) { considerNotify((ObserverWrapper) iteratorWithAdditions.next().getValue()); if (this.mDispatchInvalidated) { break; } } } } while (this.mDispatchInvalidated); this.mDispatchingValue = false; } public void observe(LifecycleOwner lifecycleOwner, Observer observer) { assertMainThread("observe"); if (lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) { return; } LifecycleBoundObserver lifecycleBoundObserver = new LifecycleBoundObserver(lifecycleOwner, observer); LiveData.ObserverWrapper putIfAbsent = this.mObservers.putIfAbsent(observer, lifecycleBoundObserver); if (putIfAbsent != null && !putIfAbsent.isAttachedTo(lifecycleOwner)) { throw new IllegalArgumentException("Cannot add the same observer with different lifecycles"); } if (putIfAbsent != null) { return; } lifecycleOwner.getLifecycle().addObserver(lifecycleBoundObserver); } public void observeForever(Observer observer) { assertMainThread("observeForever"); AlwaysActiveObserver alwaysActiveObserver = new AlwaysActiveObserver(observer); LiveData.ObserverWrapper putIfAbsent = this.mObservers.putIfAbsent(observer, alwaysActiveObserver); if (putIfAbsent instanceof LifecycleBoundObserver) { throw new IllegalArgumentException("Cannot add the same observer with different lifecycles"); } if (putIfAbsent != null) { return; } alwaysActiveObserver.activeStateChanged(true); } public void removeObserver(Observer observer) { assertMainThread("removeObserver"); LiveData.ObserverWrapper remove = this.mObservers.remove(observer); if (remove == null) { return; } remove.detachObserver(); remove.activeStateChanged(false); } public void removeObservers(LifecycleOwner lifecycleOwner) { assertMainThread("removeObservers"); Iterator, LiveData.ObserverWrapper>> it = this.mObservers.iterator(); while (it.hasNext()) { Map.Entry, LiveData.ObserverWrapper> next = it.next(); if (next.getValue().isAttachedTo(lifecycleOwner)) { removeObserver(next.getKey()); } } } /* JADX INFO: Access modifiers changed from: protected */ public void postValue(T t) { boolean z; synchronized (this.mDataLock) { z = this.mPendingData == NOT_SET; this.mPendingData = t; } if (z) { ArchTaskExecutor.getInstance().postToMainThread(this.mPostValueRunnable); } } /* JADX INFO: Access modifiers changed from: protected */ public void setValue(T t) { assertMainThread("setValue"); this.mVersion++; this.mData = t; dispatchingValue(null); } public boolean hasObservers() { return this.mObservers.size() > 0; } void changeActiveCounter(int i) { int i2 = this.mActiveCount; this.mActiveCount = i + i2; if (this.mChangingActiveState) { return; } this.mChangingActiveState = true; while (true) { try { int i3 = this.mActiveCount; if (i2 == i3) { return; } boolean z = i2 == 0 && i3 > 0; boolean z2 = i2 > 0 && i3 == 0; if (z) { onActive(); } else if (z2) { onInactive(); } i2 = i3; } finally { this.mChangingActiveState = false; } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class LifecycleBoundObserver extends LiveData.ObserverWrapper implements LifecycleEventObserver { final LifecycleOwner mOwner; @Override // androidx.lifecycle.LiveData.ObserverWrapper boolean isAttachedTo(LifecycleOwner lifecycleOwner) { return this.mOwner == lifecycleOwner; } LifecycleBoundObserver(LifecycleOwner lifecycleOwner, Observer observer) { super(observer); this.mOwner = lifecycleOwner; } @Override // androidx.lifecycle.LiveData.ObserverWrapper boolean shouldBeActive() { return this.mOwner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED); } @Override // androidx.lifecycle.LifecycleEventObserver public void onStateChanged(LifecycleOwner lifecycleOwner, 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 void detachObserver() { this.mOwner.getLifecycle().removeObserver(this); } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public abstract class ObserverWrapper { boolean mActive; int mLastVersion = -1; final Observer mObserver; void detachObserver() { } boolean isAttachedTo(LifecycleOwner lifecycleOwner) { return false; } abstract boolean shouldBeActive(); ObserverWrapper(Observer observer) { this.mObserver = observer; } void activeStateChanged(boolean z) { if (z == this.mActive) { return; } this.mActive = z; LiveData.this.changeActiveCounter(z ? 1 : -1); if (this.mActive) { LiveData.this.dispatchingValue(this); } } } /* loaded from: classes2.dex */ private class AlwaysActiveObserver extends LiveData.ObserverWrapper { @Override // androidx.lifecycle.LiveData.ObserverWrapper boolean shouldBeActive() { return true; } AlwaysActiveObserver(Observer observer) { super(observer); } } static void assertMainThread(String str) { if (!ArchTaskExecutor.getInstance().isMainThread()) { throw new IllegalStateException("Cannot invoke " + str + " on a background thread"); } } }