package b.g.a;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import b.g.a.b;

/* JADX INFO: loaded from: classes.dex */
public abstract class a extends BaseAdapter implements Filterable, b.a {

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

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public C0018a f1064f;
    public DataSetObserver g;
    public b.g.a.b h;

    /* JADX INFO: renamed from: b.g.a.a$a, reason: collision with other inner class name */
    public class C0018a extends ContentObserver {
        public C0018a() {
            super(new Handler());
        }

        @Override // android.database.ContentObserver
        public boolean deliverSelfNotifications() {
            return true;
        }

        @Override // android.database.ContentObserver
        public void onChange(boolean z) {
            Cursor cursor;
            a aVar = a.this;
            if (!aVar.f1060b || (cursor = aVar.f1061c) == null || cursor.isClosed()) {
                return;
            }
            aVar.f1059a = aVar.f1061c.requery();
        }
    }

    public class b extends DataSetObserver {
        public b() {
        }

        @Override // android.database.DataSetObserver
        public void onChanged() {
            a aVar = a.this;
            aVar.f1059a = true;
            aVar.notifyDataSetChanged();
        }

        @Override // android.database.DataSetObserver
        public void onInvalidated() {
            a aVar = a.this;
            aVar.f1059a = false;
            aVar.notifyDataSetInvalidated();
        }
    }

    public a(Context context, Cursor cursor, boolean z) {
        b bVar;
        int i = z ? 1 : 2;
        if ((i & 1) == 1) {
            i |= 2;
            this.f1060b = true;
        } else {
            this.f1060b = false;
        }
        boolean z2 = cursor != null;
        this.f1061c = cursor;
        this.f1059a = z2;
        this.f1062d = context;
        this.f1063e = z2 ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i & 2) == 2) {
            this.f1064f = new C0018a();
            bVar = new b();
        } else {
            bVar = null;
            this.f1064f = null;
        }
        this.g = bVar;
        if (z2) {
            C0018a c0018a = this.f1064f;
            if (c0018a != null) {
                cursor.registerContentObserver(c0018a);
            }
            DataSetObserver dataSetObserver = this.g;
            if (dataSetObserver != null) {
                cursor.registerDataSetObserver(dataSetObserver);
            }
        }
    }

    public abstract void a(View view, Context context, Cursor cursor);

    public void b(Cursor cursor) {
        Cursor cursor2 = this.f1061c;
        if (cursor == cursor2) {
            cursor2 = null;
        } else {
            if (cursor2 != null) {
                C0018a c0018a = this.f1064f;
                if (c0018a != null) {
                    cursor2.unregisterContentObserver(c0018a);
                }
                DataSetObserver dataSetObserver = this.g;
                if (dataSetObserver != null) {
                    cursor2.unregisterDataSetObserver(dataSetObserver);
                }
            }
            this.f1061c = cursor;
            if (cursor != null) {
                C0018a c0018a2 = this.f1064f;
                if (c0018a2 != null) {
                    cursor.registerContentObserver(c0018a2);
                }
                DataSetObserver dataSetObserver2 = this.g;
                if (dataSetObserver2 != null) {
                    cursor.registerDataSetObserver(dataSetObserver2);
                }
                this.f1063e = cursor.getColumnIndexOrThrow("_id");
                this.f1059a = true;
                notifyDataSetChanged();
            } else {
                this.f1063e = -1;
                this.f1059a = false;
                notifyDataSetInvalidated();
            }
        }
        if (cursor2 != null) {
            cursor2.close();
        }
    }

    public abstract CharSequence c(Cursor cursor);

    public abstract View d(Context context, Cursor cursor, ViewGroup viewGroup);

    @Override // android.widget.Adapter
    public int getCount() {
        Cursor cursor;
        if (!this.f1059a || (cursor = this.f1061c) == null) {
            return 0;
        }
        return cursor.getCount();
    }

    @Override // android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i, View view, ViewGroup viewGroup) {
        if (!this.f1059a) {
            return null;
        }
        this.f1061c.moveToPosition(i);
        if (view == null) {
            c cVar = (c) this;
            view = cVar.k.inflate(cVar.j, viewGroup, false);
        }
        a(view, this.f1062d, this.f1061c);
        return view;
    }

    @Override // android.widget.Filterable
    public Filter getFilter() {
        if (this.h == null) {
            this.h = new b.g.a.b(this);
        }
        return this.h;
    }

    @Override // android.widget.Adapter
    public Object getItem(int i) {
        Cursor cursor;
        if (!this.f1059a || (cursor = this.f1061c) == null) {
            return null;
        }
        cursor.moveToPosition(i);
        return this.f1061c;
    }

    @Override // android.widget.Adapter
    public long getItemId(int i) {
        Cursor cursor;
        if (this.f1059a && (cursor = this.f1061c) != null && cursor.moveToPosition(i)) {
            return this.f1061c.getLong(this.f1063e);
        }
        return 0L;
    }

    @Override // android.widget.Adapter
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (!this.f1059a) {
            throw new IllegalStateException("this should only be called when the cursor is valid");
        }
        if (this.f1061c.moveToPosition(i)) {
            if (view == null) {
                view = d(this.f1062d, this.f1061c, viewGroup);
            }
            a(view, this.f1062d, this.f1061c);
            return view;
        }
        throw new IllegalStateException("couldn't move cursor to position " + i);
    }
}
