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

/* JADX INFO: compiled from: CursorAdapter.java */
/* JADX INFO: loaded from: classes.dex */
public abstract class a extends BaseAdapter implements Filterable, b.a {
    protected boolean b;
    protected boolean d;
    protected Cursor e;
    protected Context f;

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    protected g.f.a.b f509j;

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

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

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

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

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

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

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

    public void a(Cursor cursor) {
        Cursor cursorJ = j(cursor);
        if (cursorJ != null) {
            cursorJ.close();
        }
    }

    @Override // g.f.a.b.a
    public Cursor b() {
        return this.e;
    }

    public abstract CharSequence c(Cursor cursor);

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

    void f(Context context, Cursor cursor, int i2) {
        if ((i2 & 1) == 1) {
            i2 |= 2;
            this.d = true;
        } else {
            this.d = false;
        }
        boolean z = cursor != null;
        this.e = cursor;
        this.b = z;
        this.f = context;
        this.f506g = z ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i2 & 2) == 2) {
            this.f507h = new C0029a();
            this.f508i = new b();
        } else {
            this.f507h = null;
            this.f508i = null;
        }
        if (z) {
            C0029a c0029a = this.f507h;
            if (c0029a != null) {
                cursor.registerContentObserver(c0029a);
            }
            DataSetObserver dataSetObserver = this.f508i;
            if (dataSetObserver != null) {
                cursor.registerDataSetObserver(dataSetObserver);
            }
        }
    }

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

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

    @Override // android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i2, View view, ViewGroup viewGroup) {
        if (!this.b) {
            return null;
        }
        this.e.moveToPosition(i2);
        if (view == null) {
            view = g(this.f, this.e, viewGroup);
        }
        e(view, this.f, this.e);
        return view;
    }

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

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

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

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

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

    protected void i() {
        Cursor cursor;
        if (!this.d || (cursor = this.e) == null || cursor.isClosed()) {
            return;
        }
        this.b = this.e.requery();
    }

    public Cursor j(Cursor cursor) {
        Cursor cursor2 = this.e;
        if (cursor == cursor2) {
            return null;
        }
        if (cursor2 != null) {
            C0029a c0029a = this.f507h;
            if (c0029a != null) {
                cursor2.unregisterContentObserver(c0029a);
            }
            DataSetObserver dataSetObserver = this.f508i;
            if (dataSetObserver != null) {
                cursor2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.e = cursor;
        if (cursor != null) {
            C0029a c0029a2 = this.f507h;
            if (c0029a2 != null) {
                cursor.registerContentObserver(c0029a2);
            }
            DataSetObserver dataSetObserver2 = this.f508i;
            if (dataSetObserver2 != null) {
                cursor.registerDataSetObserver(dataSetObserver2);
            }
            this.f506g = cursor.getColumnIndexOrThrow("_id");
            this.b = true;
            notifyDataSetChanged();
        } else {
            this.f506g = -1;
            this.b = false;
            notifyDataSetInvalidated();
        }
        return cursor2;
    }
}
