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 */
    protected boolean f1501b;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    protected boolean f1502f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    protected Cursor f1503g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    protected Context f1504h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    protected int f1505i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    protected C0035a f1506j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    protected DataSetObserver f1507k;
    protected c.g.a.b l;

    /* JADX INFO: renamed from: c.g.a.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: CursorAdapter.java */
    private class C0035a extends ContentObserver {
        C0035a() {
            super(new Handler());
        }

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

        @Override // android.database.ContentObserver
        public void onChange(boolean z) {
            a.this.e();
        }
    }

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

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

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

    public a(Context context, Cursor cursor, boolean z) {
        b(context, cursor, z ? 1 : 2);
    }

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

    void b(Context context, Cursor cursor, int i2) {
        if ((i2 & 1) == 1) {
            i2 |= 2;
            this.f1502f = true;
        } else {
            this.f1502f = false;
        }
        boolean z = cursor != null;
        this.f1503g = cursor;
        this.f1501b = z;
        this.f1504h = context;
        this.f1505i = z ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i2 & 2) == 2) {
            this.f1506j = new C0035a();
            this.f1507k = new b();
        } else {
            this.f1506j = null;
            this.f1507k = null;
        }
        if (z) {
            C0035a c0035a = this.f1506j;
            if (c0035a != null) {
                cursor.registerContentObserver(c0035a);
            }
            DataSetObserver dataSetObserver = this.f1507k;
            if (dataSetObserver != null) {
                cursor.registerDataSetObserver(dataSetObserver);
            }
        }
    }

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

    public void changeCursor(Cursor cursor) {
        Cursor cursorF = f(cursor);
        if (cursorF != null) {
            cursorF.close();
        }
    }

    public abstract CharSequence convertToString(Cursor cursor);

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

    protected void e() {
        Cursor cursor;
        if (!this.f1502f || (cursor = this.f1503g) == null || cursor.isClosed()) {
            return;
        }
        this.f1501b = this.f1503g.requery();
    }

    public Cursor f(Cursor cursor) {
        Cursor cursor2 = this.f1503g;
        if (cursor == cursor2) {
            return null;
        }
        if (cursor2 != null) {
            C0035a c0035a = this.f1506j;
            if (c0035a != null) {
                cursor2.unregisterContentObserver(c0035a);
            }
            DataSetObserver dataSetObserver = this.f1507k;
            if (dataSetObserver != null) {
                cursor2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f1503g = cursor;
        if (cursor != null) {
            C0035a c0035a2 = this.f1506j;
            if (c0035a2 != null) {
                cursor.registerContentObserver(c0035a2);
            }
            DataSetObserver dataSetObserver2 = this.f1507k;
            if (dataSetObserver2 != null) {
                cursor.registerDataSetObserver(dataSetObserver2);
            }
            this.f1505i = cursor.getColumnIndexOrThrow("_id");
            this.f1501b = true;
            notifyDataSetChanged();
        } else {
            this.f1505i = -1;
            this.f1501b = false;
            notifyDataSetInvalidated();
        }
        return cursor2;
    }

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

    @Override // c.g.a.b.a
    public Cursor getCursor() {
        return this.f1503g;
    }

    @Override // android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i2, View view, ViewGroup viewGroup) {
        if (!this.f1501b) {
            return null;
        }
        this.f1503g.moveToPosition(i2);
        if (view == null) {
            view = c(this.f1504h, this.f1503g, viewGroup);
        }
        a(view, this.f1504h, this.f1503g);
        return view;
    }

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

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

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

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