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

/* 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 */
    public boolean f5367b;

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public C0074a f5372g;

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

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

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

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

        @Override // android.database.ContentObserver
        public void onChange(boolean z) {
            Cursor cursor;
            a aVar = a.this;
            if (!aVar.f5368c || (cursor = aVar.f5369d) == null || cursor.isClosed()) {
                return;
            }
            aVar.f5367b = aVar.f5369d.requery();
        }
    }

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

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

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

    public a(Context context, Cursor cursor, boolean z) {
        b bVar;
        int i2 = z ? 1 : 2;
        if ((i2 & 1) == 1) {
            i2 |= 2;
            this.f5368c = true;
        } else {
            this.f5368c = false;
        }
        boolean z2 = cursor != null;
        this.f5369d = cursor;
        this.f5367b = z2;
        this.f5370e = context;
        this.f5371f = z2 ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i2 & 2) == 2) {
            this.f5372g = new C0074a();
            bVar = new b();
        } else {
            bVar = null;
            this.f5372g = null;
        }
        this.f5373h = bVar;
        if (z2) {
            C0074a c0074a = this.f5372g;
            if (c0074a != null) {
                cursor.registerContentObserver(c0074a);
            }
            DataSetObserver dataSetObserver = this.f5373h;
            if (dataSetObserver != null) {
                cursor.registerDataSetObserver(dataSetObserver);
            }
        }
    }

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

    public void b(Cursor cursor) {
        Cursor cursor2 = this.f5369d;
        if (cursor == cursor2) {
            cursor2 = null;
        } else {
            if (cursor2 != null) {
                C0074a c0074a = this.f5372g;
                if (c0074a != null) {
                    cursor2.unregisterContentObserver(c0074a);
                }
                DataSetObserver dataSetObserver = this.f5373h;
                if (dataSetObserver != null) {
                    cursor2.unregisterDataSetObserver(dataSetObserver);
                }
            }
            this.f5369d = cursor;
            if (cursor != null) {
                C0074a c0074a2 = this.f5372g;
                if (c0074a2 != null) {
                    cursor.registerContentObserver(c0074a2);
                }
                DataSetObserver dataSetObserver2 = this.f5373h;
                if (dataSetObserver2 != null) {
                    cursor.registerDataSetObserver(dataSetObserver2);
                }
                this.f5371f = cursor.getColumnIndexOrThrow("_id");
                this.f5367b = true;
                notifyDataSetChanged();
            } else {
                this.f5371f = -1;
                this.f5367b = 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.f5367b || (cursor = this.f5369d) == null) {
            return 0;
        }
        return cursor.getCount();
    }

    @Override // android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i2, View view, ViewGroup viewGroup) {
        if (!this.f5367b) {
            return null;
        }
        this.f5369d.moveToPosition(i2);
        if (view == null) {
            c cVar = (c) this;
            view = cVar.l.inflate(cVar.k, viewGroup, false);
        }
        a(view, this.f5370e, this.f5369d);
        return view;
    }

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

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

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

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