package androidx.lifecycle;

import androidx.lifecycle.e;

/* JADX INFO: loaded from: classes.dex */
public abstract class LiveData<T> {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    static final Object f291i = new Object();
    final Object a = new Object();
    private g.b.a.b.b<m<? super T>, LiveData<T>.a> b = new g.b.a.b.b<>();
    int c = 0;
    private volatile Object d = f291i;
    volatile Object e = f291i;
    private int f = -1;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private boolean f292g;

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

    class LifecycleBoundObserver extends LiveData<T>.a implements d {
        final g e;

        LifecycleBoundObserver(g gVar, m<? super T> mVar) {
            super(mVar);
            this.e = gVar;
        }

        @Override // androidx.lifecycle.LiveData.a
        void e() {
            this.e.b().c(this);
        }

        @Override // androidx.lifecycle.d
        public void f(g gVar, e.a aVar) {
            if (this.e.b().b() == e.b.DESTROYED) {
                LiveData.this.i(this.a);
            } else {
                a(j());
            }
        }

        @Override // androidx.lifecycle.LiveData.a
        boolean i(g gVar) {
            return this.e == gVar;
        }

        @Override // androidx.lifecycle.LiveData.a
        boolean j() {
            return this.e.b().b().isAtLeast(e.b.STARTED);
        }
    }

    private abstract class a {
        final m<? super T> a;
        boolean b;
        int c = -1;

        a(m<? super T> mVar) {
            this.a = mVar;
        }

        void a(boolean z) {
            if (z == this.b) {
                return;
            }
            this.b = z;
            boolean z2 = LiveData.this.c == 0;
            LiveData.this.c += this.b ? 1 : -1;
            if (z2 && this.b) {
                LiveData.this.g();
            }
            LiveData liveData = LiveData.this;
            if (liveData.c == 0 && !this.b) {
                liveData.h();
            }
            if (this.b) {
                LiveData.this.c(this);
            }
        }

        void e() {
        }

        boolean i(g gVar) {
            return false;
        }

        abstract boolean j();
    }

    private static void a(String str) {
        if (g.b.a.a.a.b().a()) {
            return;
        }
        throw new IllegalStateException("Cannot invoke " + str + " on a background thread");
    }

    private void b(LiveData<T>.a aVar) {
        if (aVar.b) {
            if (!aVar.j()) {
                aVar.a(false);
                return;
            }
            int i2 = aVar.c;
            int i3 = this.f;
            if (i2 >= i3) {
                return;
            }
            aVar.c = i3;
            aVar.a.a((Object) this.d);
        }
    }

    void c(LiveData<T>.a aVar) {
        if (this.f292g) {
            this.f293h = true;
            return;
        }
        this.f292g = true;
        do {
            this.f293h = false;
            if (aVar != null) {
                b(aVar);
                aVar = null;
            } else {
                g.b.a.b.b<m<? super T>, LiveData<T>.a>.d dVarD = this.b.d();
                while (dVarD.hasNext()) {
                    b((a) dVarD.next().getValue());
                    if (this.f293h) {
                        break;
                    }
                }
            }
        } while (this.f293h);
        this.f292g = false;
    }

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

    public boolean e() {
        return this.c > 0;
    }

    public void f(g gVar, m<? super T> mVar) {
        a("observe");
        if (gVar.b().b() == e.b.DESTROYED) {
            return;
        }
        LifecycleBoundObserver lifecycleBoundObserver = new LifecycleBoundObserver(gVar, mVar);
        LiveData<T>.a aVarG = this.b.g(mVar, lifecycleBoundObserver);
        if (aVarG != null && !aVarG.i(gVar)) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        }
        if (aVarG != null) {
            return;
        }
        gVar.b().a(lifecycleBoundObserver);
    }

    protected void g() {
    }

    protected void h() {
    }

    public void i(m<? super T> mVar) {
        a("removeObserver");
        LiveData<T>.a aVarH = this.b.h(mVar);
        if (aVarH == null) {
            return;
        }
        aVarH.e();
        aVarH.a(false);
    }

    protected void j(T t) {
        a("setValue");
        this.f++;
        this.d = t;
        c(null);
    }
}
