package androidx.lifecycle;

import b.c.a.b.b;
import b.j.d;
import b.j.e;
import b.j.g;
import b.j.h;
import b.j.m;
import java.util.Map;

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

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public b<m<? super T>, LiveData<T>.a> f174c = new b<>();

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public volatile Object f177f;
    public volatile Object g;
    public int h;
    public boolean i;
    public boolean j;

    public class LifecycleBoundObserver extends LiveData<T>.a implements e {

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

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final /* synthetic */ LiveData f179f;

        @Override // b.j.e
        public void g(g gVar, d.a aVar) {
            d.b bVar = ((h) this.f178e.a()).f1291b;
            if (bVar == d.b.DESTROYED) {
                this.f179f.f(this.f180a);
                return;
            }
            d.b bVar2 = null;
            while (bVar2 != bVar) {
                h(j());
                bVar2 = bVar;
                bVar = ((h) this.f178e.a()).f1291b;
            }
        }

        @Override // androidx.lifecycle.LiveData.a
        public void i() {
            h hVar = (h) this.f178e.a();
            hVar.c("removeObserver");
            hVar.f1290a.e(this);
        }

        @Override // androidx.lifecycle.LiveData.a
        public boolean j() {
            return ((h) this.f178e.a()).f1291b.compareTo(d.b.STARTED) >= 0;
        }
    }

    public abstract class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final m<? super T> f180a;

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

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

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final /* synthetic */ LiveData f183d;

        public void h(boolean z) {
            if (z == this.f181b) {
                return;
            }
            this.f181b = z;
            LiveData liveData = this.f183d;
            int i = z ? 1 : -1;
            int i2 = liveData.f175d;
            liveData.f175d = i + i2;
            if (!liveData.f176e) {
                liveData.f176e = true;
                while (true) {
                    try {
                        int i3 = liveData.f175d;
                        if (i2 == i3) {
                            break;
                        }
                        boolean z2 = i2 == 0 && i3 > 0;
                        boolean z3 = i2 > 0 && i3 == 0;
                        if (z2) {
                            liveData.d();
                        } else if (z3) {
                            liveData.e();
                        }
                        i2 = i3;
                    } finally {
                        liveData.f176e = false;
                    }
                }
            }
            if (this.f181b) {
                this.f183d.c(this);
            }
        }

        public void i() {
        }

        public abstract boolean j();
    }

    public LiveData() {
        Object obj = f172a;
        this.g = obj;
        this.f177f = obj;
        this.h = -1;
    }

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

    public final void b(LiveData<T>.a aVar) {
        if (aVar.f181b) {
            if (!aVar.j()) {
                aVar.h(false);
                return;
            }
            int i = aVar.f182c;
            int i2 = this.h;
            if (i >= i2) {
                return;
            }
            aVar.f182c = i2;
            aVar.f180a.a((Object) this.f177f);
        }
    }

    public void c(LiveData<T>.a aVar) {
        if (this.i) {
            this.j = true;
            return;
        }
        this.i = true;
        do {
            this.j = false;
            if (aVar != null) {
                b(aVar);
                aVar = null;
            } else {
                b<m<? super T>, LiveData<T>.a>.d dVarB = this.f174c.b();
                while (dVarB.hasNext()) {
                    b((a) ((Map.Entry) dVarB.next()).getValue());
                    if (this.j) {
                        break;
                    }
                }
            }
        } while (this.j);
        this.i = false;
    }

    public void d() {
    }

    public void e() {
    }

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