package android.support.v4.widget;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Handler;
import android.support.v4.widget.e;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;

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

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    protected int f701f;
    protected a g;
    protected DataSetObserver h;
    protected e i;

    /* JADX INFO: compiled from: CursorAdapter.java */
    private class a extends ContentObserver {
        a() {
            super(new Handler());
        }

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

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

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

        @Override // android.database.DataSetObserver
        public void onChanged() {
            d dVar = d.this;
            dVar.f697b = true;
            dVar.notifyDataSetChanged();
        }

        @Override // android.database.DataSetObserver
        public void onInvalidated() {
            d dVar = d.this;
            dVar.f697b = false;
            dVar.notifyDataSetInvalidated();
        }
    }

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

    @Override // android.support.v4.widget.e.a
    public abstract CharSequence a(Cursor cursor);

    @Override // android.support.v4.widget.e.a
    public void b(Cursor cursor) {
        Cursor cursorJ = j(cursor);
        if (cursorJ != null) {
            cursorJ.close();
        }
    }

    @Override // android.support.v4.widget.e.a
    public Cursor c() {
        return this.f699d;
    }

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

    void f(Context context, Cursor cursor, int i) {
        if ((i & 1) == 1) {
            i |= 2;
            this.f698c = true;
        } else {
            this.f698c = false;
        }
        boolean z = cursor != null;
        this.f699d = cursor;
        this.f697b = z;
        this.f700e = context;
        this.f701f = z ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i & 2) == 2) {
            this.g = new a();
            this.h = new b();
        } else {
            this.g = null;
            this.h = null;
        }
        if (z) {
            a aVar = this.g;
            if (aVar != null) {
                cursor.registerContentObserver(aVar);
            }
            DataSetObserver dataSetObserver = this.h;
            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.f697b || (cursor = this.f699d) == null) {
            return 0;
        }
        return cursor.getCount();
    }

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

    @Override // android.widget.Filterable
    public Filter getFilter() {
        if (this.i == null) {
            this.i = new e(this);
        }
        return this.i;
    }

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

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

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

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

    protected void i() {
        Cursor cursor;
        if (!this.f698c || (cursor = this.f699d) == null || cursor.isClosed()) {
            return;
        }
        this.f697b = this.f699d.requery();
    }

    public Cursor j(Cursor cursor) {
        Cursor cursor2 = this.f699d;
        if (cursor == cursor2) {
            return null;
        }
        if (cursor2 != null) {
            a aVar = this.g;
            if (aVar != null) {
                cursor2.unregisterContentObserver(aVar);
            }
            DataSetObserver dataSetObserver = this.h;
            if (dataSetObserver != null) {
                cursor2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f699d = cursor;
        if (cursor != null) {
            a aVar2 = this.g;
            if (aVar2 != null) {
                cursor.registerContentObserver(aVar2);
            }
            DataSetObserver dataSetObserver2 = this.h;
            if (dataSetObserver2 != null) {
                cursor.registerDataSetObserver(dataSetObserver2);
            }
            this.f701f = cursor.getColumnIndexOrThrow("_id");
            this.f697b = true;
            notifyDataSetChanged();
        } else {
            this.f701f = -1;
            this.f697b = false;
            notifyDataSetInvalidated();
        }
        return cursor2;
    }
}
