package d.i.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 d.i.a.b;

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

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

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

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

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

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

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public C0037a f2839q;

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public d.i.a.b f2841s;

    /* JADX INFO: renamed from: d.i.a.a$a, reason: collision with other inner class name */
    public class C0037a extends ContentObserver {
        public C0037a() {
            super(new Handler());
        }

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

        @Override // android.database.ContentObserver
        public void onChange(boolean z2) {
            Cursor cursor;
            a aVar = a.this;
            if (!aVar.f2835m || (cursor = aVar.f2836n) == null || cursor.isClosed()) {
                return;
            }
            aVar.f2834f = aVar.f2836n.requery();
        }
    }

    public class b extends DataSetObserver {
        public b() {
        }

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

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

    public a(Context context, Cursor cursor, boolean z2) {
        b bVar;
        int i2 = z2 ? 1 : 2;
        if ((i2 & 1) == 1) {
            i2 |= 2;
            this.f2835m = true;
        } else {
            this.f2835m = false;
        }
        boolean z3 = cursor != null;
        this.f2836n = cursor;
        this.f2834f = z3;
        this.f2837o = context;
        this.f2838p = z3 ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i2 & 2) == 2) {
            this.f2839q = new C0037a();
            bVar = new b();
        } else {
            bVar = null;
            this.f2839q = null;
        }
        this.f2840r = bVar;
        if (z3) {
            C0037a c0037a = this.f2839q;
            if (c0037a != null) {
                cursor.registerContentObserver(c0037a);
            }
            DataSetObserver dataSetObserver = this.f2840r;
            if (dataSetObserver != null) {
                cursor.registerDataSetObserver(dataSetObserver);
            }
        }
    }

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

    public void b(Cursor cursor) {
        Cursor cursor2 = this.f2836n;
        if (cursor == cursor2) {
            cursor2 = null;
        } else {
            if (cursor2 != null) {
                C0037a c0037a = this.f2839q;
                if (c0037a != null) {
                    cursor2.unregisterContentObserver(c0037a);
                }
                DataSetObserver dataSetObserver = this.f2840r;
                if (dataSetObserver != null) {
                    cursor2.unregisterDataSetObserver(dataSetObserver);
                }
            }
            this.f2836n = cursor;
            if (cursor != null) {
                C0037a c0037a2 = this.f2839q;
                if (c0037a2 != null) {
                    cursor.registerContentObserver(c0037a2);
                }
                DataSetObserver dataSetObserver2 = this.f2840r;
                if (dataSetObserver2 != null) {
                    cursor.registerDataSetObserver(dataSetObserver2);
                }
                this.f2838p = cursor.getColumnIndexOrThrow("_id");
                this.f2834f = true;
                notifyDataSetChanged();
            } else {
                this.f2838p = -1;
                this.f2834f = 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.f2834f || (cursor = this.f2836n) == null) {
            return 0;
        }
        return cursor.getCount();
    }

    @Override // android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i2, View view, ViewGroup viewGroup) {
        if (!this.f2834f) {
            return null;
        }
        this.f2836n.moveToPosition(i2);
        if (view == null) {
            c cVar = (c) this;
            view = cVar.f2844v.inflate(cVar.f2843u, viewGroup, false);
        }
        a(view, this.f2837o, this.f2836n);
        return view;
    }

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

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

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

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