package androidx.lifecycle;

import androidx.annotation.Nullable;
import com.google.android.gms.measurement.internal.u1;
import java.util.Iterator;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public abstract class v {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final Object f2707k = new Object();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Object f2708a = new Object();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final n.g f2709b = new n.g();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f2710c = 0;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public boolean f2711d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public volatile Object f2712e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public volatile Object f2713f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int f2714g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f2715h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public boolean f2716i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final e.s0 f2717j;

    public v() {
        Object obj = f2707k;
        this.f2713f = obj;
        this.f2717j = new e.s0(this, 10);
        this.f2712e = obj;
        this.f2714g = -1;
    }

    public static void a(String str) {
        if (!m.b.d().f30272a.b()) {
            throw new IllegalStateException(u1.k("Cannot invoke ", str, " on a background thread"));
        }
    }

    public final void b(u uVar) {
        if (uVar.mActive) {
            if (!uVar.shouldBeActive()) {
                uVar.activeStateChanged(false);
                return;
            }
            int i10 = uVar.mLastVersion;
            int i11 = this.f2714g;
            if (i10 >= i11) {
                return;
            }
            uVar.mLastVersion = i11;
            uVar.mObserver.onChanged(this.f2712e);
        }
    }

    public final void c(p pVar, x xVar) {
        Object obj;
        a("observe");
        if (((r) pVar.getLifecycle()).f2692c == Lifecycle$State.f2631a) {
            return;
        }
        LiveData$LifecycleBoundObserver liveData$LifecycleBoundObserver = new LiveData$LifecycleBoundObserver(this, pVar, xVar);
        n.g gVar = this.f2709b;
        n.c cVar = gVar.get(xVar);
        if (cVar != null) {
            obj = cVar.f30705b;
        } else {
            n.c cVar2 = new n.c(xVar, liveData$LifecycleBoundObserver);
            gVar.f30716d++;
            n.c cVar3 = gVar.f30714b;
            if (cVar3 == null) {
                gVar.f30713a = cVar2;
                gVar.f30714b = cVar2;
            } else {
                cVar3.f30706c = cVar2;
                cVar2.f30707d = cVar3;
                gVar.f30714b = cVar2;
            }
            obj = null;
        }
        u uVar = (u) obj;
        if (uVar != null && !uVar.isAttachedTo(pVar)) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        }
        if (uVar != null) {
            return;
        }
        pVar.getLifecycle().a(liveData$LifecycleBoundObserver);
    }

    public final void d(androidx.fragment.app.s sVar) {
        Object obj;
        a("observeForever");
        t tVar = new t(this, sVar);
        n.g gVar = this.f2709b;
        n.c cVar = gVar.get(sVar);
        if (cVar != null) {
            obj = cVar.f30705b;
        } else {
            n.c cVar2 = new n.c(sVar, tVar);
            gVar.f30716d++;
            n.c cVar3 = gVar.f30714b;
            if (cVar3 == null) {
                gVar.f30713a = cVar2;
                gVar.f30714b = cVar2;
            } else {
                cVar3.f30706c = cVar2;
                cVar2.f30707d = cVar3;
                gVar.f30714b = cVar2;
            }
            obj = null;
        }
        u uVar = (u) obj;
        if (uVar instanceof LiveData$LifecycleBoundObserver) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        }
        if (uVar != null) {
            return;
        }
        tVar.activeStateChanged(true);
    }

    public void dispatchingValue(@Nullable u uVar) {
        if (this.f2715h) {
            this.f2716i = true;
            return;
        }
        this.f2715h = true;
        do {
            this.f2716i = false;
            if (uVar != null) {
                b(uVar);
                uVar = null;
            } else {
                n.g gVar = this.f2709b;
                gVar.getClass();
                n.d dVar = new n.d(gVar);
                gVar.f30715c.put(dVar, Boolean.FALSE);
                while (dVar.hasNext()) {
                    b((u) ((Map.Entry) dVar.next()).getValue());
                    if (this.f2716i) {
                        break;
                    }
                }
            }
        } while (this.f2716i);
        this.f2715h = false;
    }

    public void e() {
    }

    public void f() {
    }

    public void g(x xVar) {
        a("removeObserver");
        u uVar = (u) this.f2709b.c(xVar);
        if (uVar == null) {
            return;
        }
        uVar.detachObserver();
        uVar.activeStateChanged(false);
    }

    @Nullable
    public Object getValue() {
        Object obj = this.f2712e;
        if (obj != f2707k) {
            return obj;
        }
        return null;
    }

    public final void h(p pVar) {
        a("removeObservers");
        Iterator it = this.f2709b.iterator();
        while (true) {
            n.e eVar = (n.e) it;
            if (!eVar.hasNext()) {
                return;
            }
            Map.Entry entry = (Map.Entry) eVar.next();
            if (((u) entry.getValue()).isAttachedTo(pVar)) {
                g((x) entry.getKey());
            }
        }
    }

    public abstract void i(Object obj);
}
