package v;

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 v.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: e, reason: collision with root package name */
    protected boolean f5861e;

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

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

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

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

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    protected v.b f5868l;

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

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

        @Override // android.database.ContentObserver
        public void onChange(boolean z2) {
            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.f5861e = true;
            aVar.notifyDataSetChanged();
        }

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

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

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

    void b(Context context, Cursor cursor, int i3) {
        if ((i3 & 1) == 1) {
            i3 |= 2;
            this.f5862f = true;
        } else {
            this.f5862f = false;
        }
        boolean z2 = cursor != null;
        this.f5863g = cursor;
        this.f5861e = z2;
        this.f5864h = context;
        this.f5865i = z2 ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i3 & 2) == 2) {
            this.f5866j = new C0082a();
            this.f5867k = new b();
        } else {
            this.f5866j = null;
            this.f5867k = null;
        }
        if (z2) {
            C0082a c0082a = this.f5866j;
            if (c0082a != null) {
                cursor.registerContentObserver(c0082a);
            }
            DataSetObserver dataSetObserver = this.f5867k;
            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.f5862f || (cursor = this.f5863g) == null || cursor.isClosed()) {
            return;
        }
        this.f5861e = this.f5863g.requery();
    }

    public Cursor f(Cursor cursor) {
        Cursor cursor2 = this.f5863g;
        if (cursor == cursor2) {
            return null;
        }
        if (cursor2 != null) {
            C0082a c0082a = this.f5866j;
            if (c0082a != null) {
                cursor2.unregisterContentObserver(c0082a);
            }
            DataSetObserver dataSetObserver = this.f5867k;
            if (dataSetObserver != null) {
                cursor2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f5863g = cursor;
        if (cursor != null) {
            C0082a c0082a2 = this.f5866j;
            if (c0082a2 != null) {
                cursor.registerContentObserver(c0082a2);
            }
            DataSetObserver dataSetObserver2 = this.f5867k;
            if (dataSetObserver2 != null) {
                cursor.registerDataSetObserver(dataSetObserver2);
            }
            this.f5865i = cursor.getColumnIndexOrThrow("_id");
            this.f5861e = true;
            notifyDataSetChanged();
        } else {
            this.f5865i = -1;
            this.f5861e = false;
            notifyDataSetInvalidated();
        }
        return cursor2;
    }

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

    @Override // v.b.a
    public Cursor getCursor() {
        return this.f5863g;
    }

    @Override // android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i3, View view, ViewGroup viewGroup) {
        if (!this.f5861e) {
            return null;
        }
        this.f5863g.moveToPosition(i3);
        if (view == null) {
            view = c(this.f5864h, this.f5863g, viewGroup);
        }
        a(view, this.f5864h, this.f5863g);
        return view;
    }

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

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

    @Override // android.widget.Adapter
    public long getItemId(int i3) {
        Cursor cursor;
        if (this.f5861e && (cursor = this.f5863g) != null && cursor.moveToPosition(i3)) {
            return this.f5863g.getLong(this.f5865i);
        }
        return 0L;
    }

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