package androidx.lifecycle;

import c.l.j;
import c.l.l;
import c.l.n;
import c.l.t;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public abstract class LiveData<T> {
    public static final Object a = new Object();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object f376b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public c.c.a.b.b<t<? super T>, LiveData<T>.b> f377c;

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

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

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

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

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

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

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

    public class LifecycleBoundObserver extends LiveData<T>.b implements l {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final n f385e;

        public LifecycleBoundObserver(n nVar, t<? super T> tVar) {
            super(tVar);
            this.f385e = nVar;
        }

        @Override // c.l.l
        public void g(n nVar, j.a aVar) {
            j.b bVarB = this.f385e.getLifecycle().b();
            if (bVarB == j.b.DESTROYED) {
                LiveData.this.h(this.a);
                return;
            }
            j.b bVar = null;
            while (bVar != bVarB) {
                h(this.f385e.getLifecycle().b().isAtLeast(j.b.STARTED));
                bVar = bVarB;
                bVarB = this.f385e.getLifecycle().b();
            }
        }

        @Override // androidx.lifecycle.LiveData.b
        public void i() {
            this.f385e.getLifecycle().c(this);
        }

        @Override // androidx.lifecycle.LiveData.b
        public boolean j(n nVar) {
            return this.f385e == nVar;
        }

        @Override // androidx.lifecycle.LiveData.b
        public boolean k() {
            return this.f385e.getLifecycle().b().isAtLeast(j.b.STARTED);
        }
    }

    public class a extends LiveData<T>.b {
        public a(LiveData liveData, t<? super T> tVar) {
            super(tVar);
        }

        @Override // androidx.lifecycle.LiveData.b
        public boolean k() {
            return true;
        }
    }

    public abstract class b {
        public final t<? super T> a;

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public int f388c = -1;

        public b(t<? super T> tVar) {
            this.a = tVar;
        }

        public void h(boolean z) {
            if (z == this.f387b) {
                return;
            }
            this.f387b = z;
            LiveData liveData = LiveData.this;
            int i2 = z ? 1 : -1;
            int i3 = liveData.f378d;
            liveData.f378d = i2 + i3;
            if (!liveData.f379e) {
                liveData.f379e = true;
                while (true) {
                    try {
                        int i4 = liveData.f378d;
                        if (i3 == i4) {
                            break;
                        }
                        boolean z2 = i3 == 0 && i4 > 0;
                        boolean z3 = i3 > 0 && i4 == 0;
                        if (z2) {
                            liveData.f();
                        } else if (z3) {
                            liveData.g();
                        }
                        i3 = i4;
                    } finally {
                        liveData.f379e = false;
                    }
                }
            }
            if (this.f387b) {
                LiveData.this.c(this);
            }
        }

        public void i() {
        }

        public boolean j(n nVar) {
            return false;
        }

        public abstract boolean k();
    }

    public LiveData(T t) {
        this.f376b = new Object();
        this.f377c = new c.c.a.b.b<>();
        this.f378d = 0;
        this.f381g = a;
        this.f380f = t;
        this.f382h = 0;
    }

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

    public final void b(LiveData<T>.b bVar) {
        if (bVar.f387b) {
            if (!bVar.k()) {
                bVar.h(false);
                return;
            }
            int i2 = bVar.f388c;
            int i3 = this.f382h;
            if (i2 >= i3) {
                return;
            }
            bVar.f388c = i3;
            bVar.a.d((Object) this.f380f);
        }
    }

    public void c(LiveData<T>.b bVar) {
        if (this.f383i) {
            this.f384j = true;
            return;
        }
        this.f383i = true;
        do {
            this.f384j = false;
            if (bVar != null) {
                b(bVar);
                bVar = null;
            } else {
                c.c.a.b.b<t<? super T>, LiveData<T>.b>.d dVarB = this.f377c.b();
                while (dVarB.hasNext()) {
                    b((b) ((Map.Entry) dVarB.next()).getValue());
                    if (this.f384j) {
                        break;
                    }
                }
            }
        } while (this.f384j);
        this.f383i = false;
    }

    public T d() {
        T t = (T) this.f380f;
        if (t != a) {
            return t;
        }
        return null;
    }

    public void e(n nVar, t<? super T> tVar) {
        a("observe");
        if (nVar.getLifecycle().b() == j.b.DESTROYED) {
            return;
        }
        LifecycleBoundObserver lifecycleBoundObserver = new LifecycleBoundObserver(nVar, tVar);
        LiveData<T>.b bVarD = this.f377c.d(tVar, lifecycleBoundObserver);
        if (bVarD != null && !bVarD.j(nVar)) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        }
        if (bVarD != null) {
            return;
        }
        nVar.getLifecycle().a(lifecycleBoundObserver);
    }

    public void f() {
    }

    public void g() {
    }

    public void h(t<? super T> tVar) {
        a("removeObserver");
        LiveData<T>.b bVarE = this.f377c.e(tVar);
        if (bVarE == null) {
            return;
        }
        bVarE.i();
        bVarE.h(false);
    }

    public LiveData() {
        this.f376b = new Object();
        this.f377c = new c.c.a.b.b<>();
        this.f378d = 0;
        Object obj = a;
        this.f381g = obj;
        this.f380f = obj;
        this.f382h = -1;
    }
}
