package androidx.lifecycle;

import android.os.Looper;
import java.util.Iterator;
import java.util.Map;
import n.C0899a;
import n.C0901c;
import o.C0919b;
import o.C0921d;
import p0.AbstractC0928a;

/* JADX INFO: loaded from: classes.dex */
public abstract class C {
    static final Object NOT_SET = new Object();
    static final int START_VERSION = -1;
    private boolean mChangingActiveState;
    private volatile Object mData;
    private boolean mDispatchInvalidated;
    private boolean mDispatchingValue;
    volatile Object mPendingData;
    private final Runnable mPostValueRunnable;
    private int mVersion;
    final Object mDataLock = new Object();
    private o.f mObservers = new o.f();
    int mActiveCount = 0;

    public C() {
        Object obj = NOT_SET;
        this.mPendingData = obj;
        this.mPostValueRunnable = new RunnableC0317y(this);
        this.mData = obj;
        this.mVersion = -1;
    }

    public static void assertMainThread(String str) {
        C0899a.x().f9314e.getClass();
        if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
            throw new IllegalStateException(AbstractC0928a.l("Cannot invoke ", str, " on a background thread"));
        }
    }

    public final void a(B b2) {
        if (b2.f4413l) {
            if (!b2.e()) {
                b2.b(false);
                return;
            }
            int i = b2.f4414m;
            int i5 = this.mVersion;
            if (i >= i5) {
                return;
            }
            b2.f4414m = i5;
            b2.f4412k.a(this.mData);
        }
    }

    public void changeActiveCounter(int i) {
        int i5 = this.mActiveCount;
        this.mActiveCount = i + i5;
        if (this.mChangingActiveState) {
            return;
        }
        this.mChangingActiveState = true;
        while (true) {
            try {
                int i6 = this.mActiveCount;
                if (i5 == i6) {
                    this.mChangingActiveState = false;
                    return;
                }
                boolean z5 = i5 == 0 && i6 > 0;
                boolean z6 = i5 > 0 && i6 == 0;
                if (z5) {
                    onActive();
                } else if (z6) {
                    onInactive();
                }
                i5 = i6;
            } catch (Throwable th) {
                this.mChangingActiveState = false;
                throw th;
            }
        }
    }

    public void dispatchingValue(B b2) {
        if (this.mDispatchingValue) {
            this.mDispatchInvalidated = true;
            return;
        }
        this.mDispatchingValue = true;
        do {
            this.mDispatchInvalidated = false;
            if (b2 != null) {
                a(b2);
                b2 = null;
            } else {
                o.f fVar = this.mObservers;
                fVar.getClass();
                C0921d c0921d = new C0921d(fVar);
                fVar.f9375m.put(c0921d, Boolean.FALSE);
                while (c0921d.hasNext()) {
                    a((B) ((Map.Entry) c0921d.next()).getValue());
                    if (this.mDispatchInvalidated) {
                        break;
                    }
                }
            }
        } while (this.mDispatchInvalidated);
        this.mDispatchingValue = false;
    }

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

    public int getVersion() {
        return this.mVersion;
    }

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

    public boolean hasObservers() {
        return this.mObservers.f9376n > 0;
    }

    public boolean isInitialized() {
        return this.mData != NOT_SET;
    }

    public void observe(InterfaceC0313u interfaceC0313u, E e2) {
        assertMainThread("observe");
        if (((C0315w) interfaceC0313u.getLifecycle()).f4487c == EnumC0308o.f4477k) {
            return;
        }
        A a5 = new A(this, interfaceC0313u, e2);
        B b2 = (B) this.mObservers.b(e2, a5);
        if (b2 != null && !b2.d(interfaceC0313u)) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        }
        if (b2 != null) {
            return;
        }
        interfaceC0313u.getLifecycle().a(a5);
    }

    public void observeForever(E e2) {
        assertMainThread("observeForever");
        C0318z c0318z = new C0318z(this, e2);
        B b2 = (B) this.mObservers.b(e2, c0318z);
        if (b2 instanceof A) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        }
        if (b2 != null) {
            return;
        }
        c0318z.b(true);
    }

    public void onActive() {
    }

    public void onInactive() {
    }

    public void postValue(Object obj) {
        boolean z5;
        synchronized (this.mDataLock) {
            z5 = this.mPendingData == NOT_SET;
            this.mPendingData = obj;
        }
        if (z5) {
            C0899a c0899aX = C0899a.x();
            Runnable runnable = this.mPostValueRunnable;
            C0901c c0901c = c0899aX.f9314e;
            if (c0901c.f9318g == null) {
                synchronized (c0901c.f9316e) {
                    try {
                        if (c0901c.f9318g == null) {
                            c0901c.f9318g = C0901c.x(Looper.getMainLooper());
                        }
                    } finally {
                    }
                }
            }
            c0901c.f9318g.post(runnable);
        }
    }

    public void removeObserver(E e2) {
        assertMainThread("removeObserver");
        B b2 = (B) this.mObservers.c(e2);
        if (b2 == null) {
            return;
        }
        b2.c();
        b2.b(false);
    }

    public void removeObservers(InterfaceC0313u interfaceC0313u) {
        assertMainThread("removeObservers");
        Iterator it = this.mObservers.iterator();
        while (true) {
            C0919b c0919b = (C0919b) it;
            if (!c0919b.hasNext()) {
                return;
            }
            Map.Entry entry = (Map.Entry) c0919b.next();
            if (((B) entry.getValue()).d(interfaceC0313u)) {
                removeObserver((E) entry.getKey());
            }
        }
    }

    public void setValue(Object obj) {
        assertMainThread("setValue");
        this.mVersion++;
        this.mData = obj;
        dispatchingValue(null);
    }
}
