package androidx.cursoradapter.widget; 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 androidx.cursoradapter.widget.CursorFilter; import com.liulishuo.filedownloader.model.FileDownloadModel; /* loaded from: classes.dex */ public abstract class CursorAdapter extends BaseAdapter implements Filterable, CursorFilter.CursorFilterClient { protected boolean a; protected boolean b; protected Cursor c; protected Context d; protected int e; protected ChangeObserver f; protected DataSetObserver g; protected CursorFilter h; private class ChangeObserver extends ContentObserver { ChangeObserver() { super(new Handler()); } @Override // android.database.ContentObserver public boolean deliverSelfNotifications() { return true; } @Override // android.database.ContentObserver public void onChange(boolean z) { CursorAdapter.this.b(); } } private class MyDataSetObserver extends DataSetObserver { MyDataSetObserver() { } @Override // android.database.DataSetObserver public void onChanged() { CursorAdapter cursorAdapter = CursorAdapter.this; cursorAdapter.a = true; cursorAdapter.notifyDataSetChanged(); } @Override // android.database.DataSetObserver public void onInvalidated() { CursorAdapter cursorAdapter = CursorAdapter.this; cursorAdapter.a = false; cursorAdapter.notifyDataSetInvalidated(); } } public CursorAdapter(Context context, Cursor cursor, boolean z) { a(context, cursor, z ? 1 : 2); } public abstract View a(Context context, Cursor cursor, ViewGroup viewGroup); void a(Context context, Cursor cursor, int i) { if ((i & 1) == 1) { i |= 2; this.b = true; } else { this.b = false; } boolean z = cursor != null; this.c = cursor; this.a = z; this.d = context; this.e = z ? cursor.getColumnIndexOrThrow(FileDownloadModel.ID) : -1; if ((i & 2) == 2) { this.f = new ChangeObserver(); this.g = new MyDataSetObserver(); } else { this.f = null; this.g = null; } if (z) { ChangeObserver changeObserver = this.f; if (changeObserver != null) { cursor.registerContentObserver(changeObserver); } DataSetObserver dataSetObserver = this.g; if (dataSetObserver != null) { cursor.registerDataSetObserver(dataSetObserver); } } } public abstract void a(View view, Context context, Cursor cursor); public abstract View b(Context context, Cursor cursor, ViewGroup viewGroup); public abstract CharSequence b(Cursor cursor); protected void b() { Cursor cursor; if (!this.b || (cursor = this.c) == null || cursor.isClosed()) { return; } this.a = this.c.requery(); } public Cursor c(Cursor cursor) { Cursor cursor2 = this.c; if (cursor == cursor2) { return null; } if (cursor2 != null) { ChangeObserver changeObserver = this.f; if (changeObserver != null) { cursor2.unregisterContentObserver(changeObserver); } DataSetObserver dataSetObserver = this.g; if (dataSetObserver != null) { cursor2.unregisterDataSetObserver(dataSetObserver); } } this.c = cursor; if (cursor != null) { ChangeObserver changeObserver2 = this.f; if (changeObserver2 != null) { cursor.registerContentObserver(changeObserver2); } DataSetObserver dataSetObserver2 = this.g; if (dataSetObserver2 != null) { cursor.registerDataSetObserver(dataSetObserver2); } this.e = cursor.getColumnIndexOrThrow(FileDownloadModel.ID); this.a = true; notifyDataSetChanged(); } else { this.e = -1; this.a = false; notifyDataSetInvalidated(); } return cursor2; } @Override // android.widget.Adapter public int getCount() { Cursor cursor; if (!this.a || (cursor = this.c) == null) { return 0; } return cursor.getCount(); } @Override // android.widget.BaseAdapter, android.widget.SpinnerAdapter public View getDropDownView(int i, View view, ViewGroup viewGroup) { if (!this.a) { return null; } this.c.moveToPosition(i); if (view == null) { view = a(this.d, this.c, viewGroup); } a(view, this.d, this.c); return view; } @Override // android.widget.Filterable public Filter getFilter() { if (this.h == null) { this.h = new CursorFilter(this); } return this.h; } @Override // android.widget.Adapter public Object getItem(int i) { Cursor cursor; if (!this.a || (cursor = this.c) == null) { return null; } cursor.moveToPosition(i); return this.c; } @Override // android.widget.Adapter public long getItemId(int i) { Cursor cursor; if (this.a && (cursor = this.c) != null && cursor.moveToPosition(i)) { return this.c.getLong(this.e); } return 0L; } @Override // android.widget.Adapter public View getView(int i, View view, ViewGroup viewGroup) { if (!this.a) { throw new IllegalStateException("this should only be called when the cursor is valid"); } if (this.c.moveToPosition(i)) { if (view == null) { view = b(this.d, this.c, viewGroup); } a(view, this.d, this.c); return view; } throw new IllegalStateException("couldn't move cursor to position " + i); } @Override // androidx.cursoradapter.widget.CursorFilter.CursorFilterClient public Cursor a() { return this.c; } public void a(Cursor cursor) { Cursor c = c(cursor); if (c != null) { c.close(); } } }