package androidx.lifecycle;

import X.AnonymousClass0CB;
import X.AnonymousClass0CC;
import X.AnonymousClass0CH;
import X.AnonymousClass0CM;
import X.AnonymousClass188;
import X.AnonymousClass1BV;
import X.C004201o;
import X.C28451Bj;
import com.bytedance.covode.number.Covode;
import java.util.Iterator;
import java.util.Map;

public abstract class LiveData<T> {
    public static final Object NOT_SET = new Object();
    public int mActiveCount;
    public boolean mChangingActiveState;
    public volatile Object mData;
    public final Object mDataLock;
    public boolean mDispatchInvalidated;
    public boolean mDispatchingValue;
    public C004201o<AnonymousClass0CM<? super T>, LiveData<T>.b> mObservers;
    public volatile Object mPendingData;
    public final Runnable mPostValueRunnable;
    public int mVersion;

    public void onActive() {
    }

    public void onInactive() {
    }

    public boolean hasActiveObservers() {
        if (this.mActiveCount > 0) {
            return true;
        }
        return false;
    }

    public class LifecycleBoundObserver extends LiveData<T>.b implements C28451Bj {
        public final AnonymousClass0CH LIZ;

        static {
            Covode.recordClassIndex(1212);
        }

        public final void LIZIZ() {
            this.LIZ.getLifecycle().LIZIZ(this);
        }

        public final boolean LIZ() {
            return this.LIZ.getLifecycle().LIZ().isAtLeast(AnonymousClass0CC.STARTED);
        }

        public final boolean LIZ(AnonymousClass0CH r2) {
            if (this.LIZ == r2) {
                return true;
            }
            return false;
        }

        public void onStateChanged(AnonymousClass0CH r4, AnonymousClass0CB r5) {
            AnonymousClass0CC LIZ2 = this.LIZ.getLifecycle().LIZ();
            if (LIZ2 == AnonymousClass0CC.DESTROYED) {
                LiveData.this.removeObserver(this.LIZJ);
                return;
            }
            AnonymousClass0CC r1 = null;
            while (r1 != LIZ2) {
                LIZ(LIZ());
                r1 = LIZ2;
                LIZ2 = this.LIZ.getLifecycle().LIZ();
            }
        }

        public LifecycleBoundObserver(AnonymousClass0CH r2, AnonymousClass0CM<? super T> r3) {
            super(r3);
            this.LIZ = r2;
        }
    }

    static {
        Covode.recordClassIndex(1210);
    }

    public T getValue() {
        T t = this.mData;
        if (t != NOT_SET) {
            return t;
        }
        return null;
    }

    public boolean hasObservers() {
        if (this.mObservers.LJ > 0) {
            return true;
        }
        return false;
    }

    public LiveData() {
        this.mDataLock = new Object();
        this.mObservers = new C004201o<>();
        this.mActiveCount = 0;
        Object obj = NOT_SET;
        this.mPendingData = obj;
        this.mPostValueRunnable = new Runnable() {
            static {
                Covode.recordClassIndex(1211);
            }

            public final void run() {
                Object obj;
                synchronized (LiveData.this.mDataLock) {
                    obj = LiveData.this.mPendingData;
                    LiveData.this.mPendingData = LiveData.NOT_SET;
                }
                LiveData.this.setValue(obj);
            }
        };
        this.mData = obj;
        this.mVersion = -1;
    }

    public abstract class b {
        public final AnonymousClass0CM<? super T> LIZJ;
        public boolean LIZLLL;
        public int LJ = -1;

        static {
            Covode.recordClassIndex(1214);
        }

        public abstract boolean LIZ();

        public boolean LIZ(AnonymousClass0CH r2) {
            return false;
        }

        public void LIZIZ() {
        }

        public final void LIZ(boolean z) {
            int i;
            if (z != this.LIZLLL) {
                this.LIZLLL = z;
                LiveData liveData = LiveData.this;
                if (z) {
                    i = 1;
                } else {
                    i = -1;
                }
                liveData.changeActiveCounter(i);
                if (this.LIZLLL) {
                    LiveData.this.dispatchingValue(this);
                }
            }
        }

        public b(AnonymousClass0CM<? super T> r3) {
            this.LIZJ = r3;
        }
    }

    public void postValue(T t) {
        boolean z;
        synchronized (this.mDataLock) {
            if (this.mPendingData == NOT_SET) {
                z = true;
            } else {
                z = false;
            }
            this.mPendingData = t;
        }
        if (z) {
            AnonymousClass188.LIZ().LIZIZ(this.mPostValueRunnable);
        }
    }

    public void removeObserver(AnonymousClass0CM<? super T> r3) {
        assertMainThread("removeObserver");
        b LIZIZ = this.mObservers.LIZIZ(r3);
        if (LIZIZ != null) {
            LIZIZ.LIZIZ();
            LIZIZ.LIZ(false);
        }
    }

    public void setValue(T t) {
        assertMainThread("setValue");
        this.mVersion++;
        this.mData = t;
        dispatchingValue((LiveData<T>.b) null);
    }

    public static void assertMainThread(String str) {
        if (!AnonymousClass188.LIZ().LIZ.LIZIZ()) {
            throw new IllegalStateException("Cannot invoke " + str + " on a background thread");
        }
    }

    private void considerNotify(LiveData<T>.b bVar) {
        if (bVar.LIZLLL) {
            if (!bVar.LIZ()) {
                bVar.LIZ(false);
                return;
            }
            int i = bVar.LJ;
            int i2 = this.mVersion;
            if (i < i2) {
                bVar.LJ = i2;
                bVar.LIZJ.onChanged(this.mData);
            }
        }
    }

    public void changeActiveCounter(int i) {
        boolean z;
        int i2 = this.mActiveCount;
        this.mActiveCount = i + i2;
        if (!this.mChangingActiveState) {
            this.mChangingActiveState = true;
            while (true) {
                try {
                    int i3 = this.mActiveCount;
                    if (i2 != i3) {
                        if (i2 != 0) {
                            if (i2 <= 0 || i3 != 0) {
                                z = false;
                            } else {
                                z = true;
                            }
                            if (0 == 0) {
                                if (z) {
                                    onInactive();
                                }
                                i2 = i3;
                            }
                        } else if (i3 <= 0) {
                            i2 = i3;
                        }
                        onActive();
                        i2 = i3;
                    } else {
                        return;
                    }
                } finally {
                    this.mChangingActiveState = false;
                }
            }
        }
    }

    public void dispatchingValue(LiveData<T>.b bVar) {
        if (this.mDispatchingValue) {
            this.mDispatchInvalidated = true;
            return;
        }
        this.mDispatchingValue = true;
        do {
            this.mDispatchInvalidated = false;
            if (bVar == null) {
                C004201o<K, V>.d LIZ = this.mObservers.LIZ();
                while (LIZ.hasNext()) {
                    considerNotify((b) ((Map.Entry) LIZ.next()).getValue());
                    if (this.mDispatchInvalidated) {
                        break;
                    }
                }
            } else {
                considerNotify(bVar);
                bVar = null;
            }
        } while (this.mDispatchInvalidated);
        this.mDispatchingValue = false;
    }

    public void observeForever(AnonymousClass0CM<? super T> r4) {
        assertMainThread("observeForever");
        AnonymousClass1BV r2 = new AnonymousClass1BV(this, r4);
        LiveData<T>.b LIZ = this.mObservers.LIZ(r4, r2);
        if (LIZ instanceof LifecycleBoundObserver) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        } else if (LIZ == null) {
            r2.LIZ(true);
        }
    }

    public void removeObservers(AnonymousClass0CH r4) {
        assertMainThread("removeObservers");
        Iterator<Map.Entry<AnonymousClass0CM<? super T>, LiveData<T>.b>> it = this.mObservers.iterator();
        while (it.hasNext()) {
            Map.Entry next = it.next();
            if (((b) next.getValue()).LIZ(r4)) {
                removeObserver((AnonymousClass0CM) next.getKey());
            }
        }
    }

    public LiveData(T t) {
        this.mDataLock = new Object();
        this.mObservers = new C004201o<>();
        this.mActiveCount = 0;
        this.mPendingData = NOT_SET;
        this.mPostValueRunnable = new Runnable() {
            static {
                Covode.recordClassIndex(1211);
            }

            public final 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 void observe(AnonymousClass0CH r3, AnonymousClass0CM<? super T> r4) {
        assertMainThread("observe");
        if (r3.getLifecycle().LIZ() != AnonymousClass0CC.DESTROYED) {
            LifecycleBoundObserver lifecycleBoundObserver = new LifecycleBoundObserver(r3, r4);
            b LIZ = this.mObservers.LIZ(r4, lifecycleBoundObserver);
            if (LIZ == null) {
                r3.getLifecycle().LIZ(lifecycleBoundObserver);
            } else if (!LIZ.LIZ(r3)) {
                throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
            }
        }
    }
}
