package c.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 c.g.a.b;

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public c.g.a.b f1509k;

    /* JADX INFO: renamed from: c.g.a.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: CursorAdapter.java */
    public class C0012a extends ContentObserver {
        public C0012a() {
            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.f1503e || (cursor = aVar.f1504f) == null || cursor.isClosed()) {
                return;
            }
            aVar.f1502b = aVar.f1504f.requery();
        }
    }

    /* JADX INFO: compiled from: CursorAdapter.java */
    public class b extends DataSetObserver {
        public b() {
        }

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

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

    public a(Context context, Cursor cursor, boolean z) {
        int i2 = z ? 1 : 2;
        if ((i2 & 1) == 1) {
            i2 |= 2;
            this.f1503e = true;
        } else {
            this.f1503e = false;
        }
        boolean z2 = cursor != null;
        this.f1504f = cursor;
        this.f1502b = z2;
        this.f1505g = context;
        this.f1506h = z2 ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i2 & 2) == 2) {
            this.f1507i = new C0012a();
            this.f1508j = new b();
        } else {
            this.f1507i = null;
            this.f1508j = null;
        }
        if (z2) {
            C0012a c0012a = this.f1507i;
            if (c0012a != null) {
                cursor.registerContentObserver(c0012a);
            }
            DataSetObserver dataSetObserver = this.f1508j;
            if (dataSetObserver != null) {
                cursor.registerDataSetObserver(dataSetObserver);
            }
        }
    }

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

    public void b(Cursor cursor) {
        Cursor cursor2 = this.f1504f;
        if (cursor == cursor2) {
            cursor2 = null;
        } else {
            if (cursor2 != null) {
                C0012a c0012a = this.f1507i;
                if (c0012a != null) {
                    cursor2.unregisterContentObserver(c0012a);
                }
                DataSetObserver dataSetObserver = this.f1508j;
                if (dataSetObserver != null) {
                    cursor2.unregisterDataSetObserver(dataSetObserver);
                }
            }
            this.f1504f = cursor;
            if (cursor != null) {
                C0012a c0012a2 = this.f1507i;
                if (c0012a2 != null) {
                    cursor.registerContentObserver(c0012a2);
                }
                DataSetObserver dataSetObserver2 = this.f1508j;
                if (dataSetObserver2 != null) {
                    cursor.registerDataSetObserver(dataSetObserver2);
                }
                this.f1506h = cursor.getColumnIndexOrThrow("_id");
                this.f1502b = true;
                notifyDataSetChanged();
            } else {
                this.f1506h = -1;
                this.f1502b = 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.f1502b || (cursor = this.f1504f) == null) {
            return 0;
        }
        return cursor.getCount();
    }

    @Override // android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i2, View view, ViewGroup viewGroup) {
        if (!this.f1502b) {
            return null;
        }
        this.f1504f.moveToPosition(i2);
        if (view == null) {
            c cVar = (c) this;
            view = cVar.n.inflate(cVar.m, viewGroup, false);
        }
        a(view, this.f1505g, this.f1504f);
        return view;
    }

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

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

    @Override // android.widget.Adapter
    public long getItemId(int i2) {
        Cursor cursor;
        if (this.f1502b && (cursor = this.f1504f) != null && cursor.moveToPosition(i2)) {
            return this.f1504f.getLong(this.f1506h);
        }
        return 0L;
    }

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