package androidx.lifecycle;

import android.os.Looper;
import androidx.lifecycle.k;
import defpackage.i85;
import defpackage.lr;
import defpackage.vw2;
import defpackage.z98;
import defpackage.zs6;
import java.io.Serializable;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public abstract class v<T> {
    public static final Object k = new Object();
    public final Object a;
    public final z98 b;
    public int c;
    public boolean d;
    public volatile Object e;
    public volatile Object f;
    public int g;
    public boolean h;
    public boolean i;
    public final Runnable j;

    public class a implements Runnable {
        public a() {
        }

        @Override // java.lang.Runnable
        public final void run() {
            Object obj;
            synchronized (v.this.a) {
                obj = v.this.f;
                v.this.f = v.k;
            }
            v.this.l(obj);
        }
    }

    public class b extends v<T>.d {
        @Override // androidx.lifecycle.v.d
        public final boolean e() {
            return true;
        }
    }

    public class c extends v<T>.d implements p {
        public final i85 e;

        public c(i85 i85Var, zs6 zs6Var) {
            super(zs6Var);
            this.e = i85Var;
        }

        @Override // androidx.lifecycle.v.d
        public final void b() {
            this.e.getLifecycle().c(this);
        }

        @Override // androidx.lifecycle.v.d
        public final boolean c(i85 i85Var) {
            return this.e == i85Var;
        }

        @Override // androidx.lifecycle.v.d
        public final boolean e() {
            return this.e.getLifecycle().b().compareTo(k.b.d) >= 0;
        }

        @Override // androidx.lifecycle.p
        public final void onStateChanged(i85 i85Var, k.a aVar) {
            i85 i85Var2 = this.e;
            k.b bVarB = i85Var2.getLifecycle().b();
            if (bVarB == k.b.a) {
                v.this.k(this.a);
                return;
            }
            k.b bVar = null;
            while (bVar != bVarB) {
                a(e());
                bVar = bVarB;
                bVarB = i85Var2.getLifecycle().b();
            }
        }
    }

    public abstract class d {
        public final zs6 a;
        public boolean b;
        public int c = -1;

        public d(zs6 zs6Var) {
            this.a = zs6Var;
        }

        public final void a(boolean z) {
            if (z == this.b) {
                return;
            }
            this.b = z;
            int i = z ? 1 : -1;
            v vVar = v.this;
            int i2 = vVar.c;
            vVar.c = i + i2;
            if (!vVar.d) {
                vVar.d = true;
                while (true) {
                    try {
                        int i3 = vVar.c;
                        if (i2 == i3) {
                            break;
                        }
                        boolean z2 = i2 == 0 && i3 > 0;
                        boolean z3 = i2 > 0 && i3 == 0;
                        if (z2) {
                            vVar.h();
                        } else if (z3) {
                            vVar.i();
                        }
                        i2 = i3;
                    } catch (Throwable th) {
                        vVar.d = false;
                        throw th;
                    }
                }
                vVar.d = false;
            }
            if (this.b) {
                vVar.d(this);
            }
        }

        public void b() {
        }

        public boolean c(i85 i85Var) {
            return false;
        }

        public abstract boolean e();
    }

    public v(Serializable serializable) {
        this.a = new Object();
        this.b = new z98();
        this.c = 0;
        this.f = k;
        this.j = new a();
        this.e = serializable;
        this.g = 0;
    }

    public static void b(String str) {
        lr.a().a.getClass();
        if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
            throw new IllegalStateException(vw2.ab("Cannot invoke ", str, " on a background thread"));
        }
    }

    public final void c(d dVar) {
        if (dVar.b) {
            if (!dVar.e()) {
                dVar.a(false);
                return;
            }
            int i = dVar.c;
            int i2 = this.g;
            if (i >= i2) {
                return;
            }
            dVar.c = i2;
            dVar.a.a(this.e);
        }
    }

    public final void d(d dVar) {
        if (this.h) {
            this.i = true;
            return;
        }
        this.h = true;
        do {
            this.i = false;
            if (dVar != null) {
                c(dVar);
                dVar = null;
            } else {
                z98 z98Var = this.b;
                z98Var.getClass();
                z98.d dVar2 = new z98.d();
                z98Var.c.put(dVar2, Boolean.FALSE);
                while (dVar2.hasNext()) {
                    c((d) ((Map.Entry) dVar2.next()).getValue());
                    if (this.i) {
                        break;
                    }
                }
            }
        } while (this.i);
        this.h = false;
    }

    public final Object e() {
        Object obj = this.e;
        if (obj != k) {
            return obj;
        }
        return null;
    }

    public final void f(i85 i85Var, zs6 zs6Var) {
        b("observe");
        if (i85Var.getLifecycle().b() == k.b.a) {
            return;
        }
        c cVar = new c(i85Var, zs6Var);
        d dVar = (d) this.b.b(zs6Var, cVar);
        if (dVar != null && !dVar.c(i85Var)) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        }
        if (dVar != null) {
            return;
        }
        i85Var.getLifecycle().a(cVar);
    }

    public final void g(zs6 zs6Var) {
        b("observeForever");
        b bVar = new b(zs6Var);
        d dVar = (d) this.b.b(zs6Var, bVar);
        if (dVar instanceof c) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        }
        if (dVar != null) {
            return;
        }
        bVar.a(true);
    }

    public void h() {
    }

    public void i() {
    }

    public void j(Object obj) {
        boolean z;
        synchronized (this.a) {
            z = this.f == k;
            this.f = obj;
        }
        if (z) {
            lr.a().b(this.j);
        }
    }

    public void k(zs6 zs6Var) {
        b("removeObserver");
        d dVar = (d) this.b.c(zs6Var);
        if (dVar == null) {
            return;
        }
        dVar.b();
        dVar.a(false);
    }

    public void l(Object obj) {
        b("setValue");
        this.g++;
        this.e = obj;
        d(null);
    }

    public v() {
        this.a = new Object();
        this.b = new z98();
        this.c = 0;
        Object obj = k;
        this.f = obj;
        this.j = new a();
        this.e = obj;
        this.g = -1;
    }
}
