179 lines
6.8 KiB
Java
179 lines
6.8 KiB
Java
|
package androidx.room.paging;
|
||
|
|
||
|
import android.database.Cursor;
|
||
|
import androidx.annotation.NonNull;
|
||
|
import androidx.annotation.RestrictTo;
|
||
|
import androidx.paging.PositionalDataSource;
|
||
|
import androidx.room.InvalidationTracker;
|
||
|
import androidx.room.RoomDatabase;
|
||
|
import androidx.room.RoomSQLiteQuery;
|
||
|
import androidx.sqlite.db.SupportSQLiteQuery;
|
||
|
import c.d.b.a.a;
|
||
|
import java.util.Collections;
|
||
|
import java.util.List;
|
||
|
import java.util.Set;
|
||
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
||
|
public abstract class LimitOffsetDataSource<T> extends PositionalDataSource<T> {
|
||
|
private final String mCountQuery;
|
||
|
private final RoomDatabase mDb;
|
||
|
private final boolean mInTransaction;
|
||
|
private final String mLimitOffsetQuery;
|
||
|
private final InvalidationTracker.Observer mObserver;
|
||
|
private final RoomSQLiteQuery mSourceQuery;
|
||
|
|
||
|
/* renamed from: androidx.room.paging.LimitOffsetDataSource$1 reason: invalid class name */
|
||
|
public class AnonymousClass1 extends InvalidationTracker.Observer {
|
||
|
public AnonymousClass1(String[] strArr) {
|
||
|
super(strArr);
|
||
|
}
|
||
|
|
||
|
@Override // androidx.room.InvalidationTracker.Observer
|
||
|
public void onInvalidated(@NonNull Set<String> set) {
|
||
|
LimitOffsetDataSource.this.invalidate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public LimitOffsetDataSource(RoomDatabase roomDatabase, RoomSQLiteQuery roomSQLiteQuery, boolean z2, String... strArr) {
|
||
|
this.mDb = roomDatabase;
|
||
|
this.mSourceQuery = roomSQLiteQuery;
|
||
|
this.mInTransaction = z2;
|
||
|
StringBuilder L = a.L("SELECT COUNT(*) FROM ( ");
|
||
|
L.append(roomSQLiteQuery.getSql());
|
||
|
L.append(" )");
|
||
|
this.mCountQuery = L.toString();
|
||
|
StringBuilder L2 = a.L("SELECT * FROM ( ");
|
||
|
L2.append(roomSQLiteQuery.getSql());
|
||
|
L2.append(" ) LIMIT ? OFFSET ?");
|
||
|
this.mLimitOffsetQuery = L2.toString();
|
||
|
AnonymousClass1 r3 = new AnonymousClass1(strArr);
|
||
|
this.mObserver = r3;
|
||
|
roomDatabase.getInvalidationTracker().addWeakObserver(r3);
|
||
|
}
|
||
|
|
||
|
public LimitOffsetDataSource(RoomDatabase roomDatabase, SupportSQLiteQuery supportSQLiteQuery, boolean z2, String... strArr) {
|
||
|
this(roomDatabase, RoomSQLiteQuery.copyFrom(supportSQLiteQuery), z2, strArr);
|
||
|
}
|
||
|
|
||
|
private RoomSQLiteQuery getSQLiteQuery(int i, int i2) {
|
||
|
RoomSQLiteQuery acquire = RoomSQLiteQuery.acquire(this.mLimitOffsetQuery, this.mSourceQuery.getArgCount() + 2);
|
||
|
acquire.copyArgumentsFrom(this.mSourceQuery);
|
||
|
acquire.bindLong(acquire.getArgCount() - 1, (long) i2);
|
||
|
acquire.bindLong(acquire.getArgCount(), (long) i);
|
||
|
return acquire;
|
||
|
}
|
||
|
|
||
|
public abstract List<T> convertRows(Cursor cursor);
|
||
|
|
||
|
public int countItems() {
|
||
|
RoomSQLiteQuery acquire = RoomSQLiteQuery.acquire(this.mCountQuery, this.mSourceQuery.getArgCount());
|
||
|
acquire.copyArgumentsFrom(this.mSourceQuery);
|
||
|
Cursor query = this.mDb.query(acquire);
|
||
|
try {
|
||
|
if (query.moveToFirst()) {
|
||
|
return query.getInt(0);
|
||
|
}
|
||
|
query.close();
|
||
|
acquire.release();
|
||
|
return 0;
|
||
|
} finally {
|
||
|
query.close();
|
||
|
acquire.release();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public boolean isInvalid() {
|
||
|
this.mDb.getInvalidationTracker().refreshVersionsSync();
|
||
|
return LimitOffsetDataSource.super.isInvalid();
|
||
|
}
|
||
|
|
||
|
/* JADX WARNING: Removed duplicated region for block: B:20:0x004b */
|
||
|
/* JADX WARNING: Removed duplicated region for block: B:23:0x0055 */
|
||
|
public void loadInitial(@NonNull PositionalDataSource.LoadInitialParams loadInitialParams, @NonNull PositionalDataSource.LoadInitialCallback<T> loadInitialCallback) {
|
||
|
Throwable th;
|
||
|
RoomSQLiteQuery roomSQLiteQuery;
|
||
|
int i;
|
||
|
RoomSQLiteQuery roomSQLiteQuery2;
|
||
|
List<T> emptyList = Collections.emptyList();
|
||
|
this.mDb.beginTransaction();
|
||
|
Cursor cursor = null;
|
||
|
try {
|
||
|
int countItems = countItems();
|
||
|
if (countItems != 0) {
|
||
|
int computeInitialLoadPosition = computeInitialLoadPosition(loadInitialParams, countItems);
|
||
|
roomSQLiteQuery = getSQLiteQuery(computeInitialLoadPosition, computeInitialLoadSize(loadInitialParams, computeInitialLoadPosition, countItems));
|
||
|
try {
|
||
|
cursor = this.mDb.query(roomSQLiteQuery);
|
||
|
List<T> convertRows = convertRows(cursor);
|
||
|
this.mDb.setTransactionSuccessful();
|
||
|
roomSQLiteQuery2 = roomSQLiteQuery;
|
||
|
i = computeInitialLoadPosition;
|
||
|
emptyList = convertRows;
|
||
|
} catch (Throwable th2) {
|
||
|
th = th2;
|
||
|
if (cursor != null) {
|
||
|
cursor.close();
|
||
|
}
|
||
|
this.mDb.endTransaction();
|
||
|
if (roomSQLiteQuery != null) {
|
||
|
roomSQLiteQuery.release();
|
||
|
}
|
||
|
throw th;
|
||
|
}
|
||
|
} else {
|
||
|
i = 0;
|
||
|
roomSQLiteQuery2 = null;
|
||
|
}
|
||
|
if (cursor != null) {
|
||
|
cursor.close();
|
||
|
}
|
||
|
this.mDb.endTransaction();
|
||
|
if (roomSQLiteQuery2 != null) {
|
||
|
roomSQLiteQuery2.release();
|
||
|
}
|
||
|
loadInitialCallback.onResult(emptyList, i, countItems);
|
||
|
} catch (Throwable th3) {
|
||
|
th = th3;
|
||
|
roomSQLiteQuery = null;
|
||
|
if (cursor != null) {
|
||
|
}
|
||
|
this.mDb.endTransaction();
|
||
|
if (roomSQLiteQuery != null) {
|
||
|
}
|
||
|
throw th;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@NonNull
|
||
|
public List<T> loadRange(int i, int i2) {
|
||
|
RoomSQLiteQuery sQLiteQuery = getSQLiteQuery(i, i2);
|
||
|
if (this.mInTransaction) {
|
||
|
this.mDb.beginTransaction();
|
||
|
Cursor cursor = null;
|
||
|
try {
|
||
|
cursor = this.mDb.query(sQLiteQuery);
|
||
|
List<T> convertRows = convertRows(cursor);
|
||
|
this.mDb.setTransactionSuccessful();
|
||
|
return convertRows;
|
||
|
} finally {
|
||
|
if (cursor != null) {
|
||
|
cursor.close();
|
||
|
}
|
||
|
this.mDb.endTransaction();
|
||
|
sQLiteQuery.release();
|
||
|
}
|
||
|
} else {
|
||
|
Cursor query = this.mDb.query(sQLiteQuery);
|
||
|
try {
|
||
|
return convertRows(query);
|
||
|
} finally {
|
||
|
query.close();
|
||
|
sQLiteQuery.release();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void loadRange(@NonNull PositionalDataSource.LoadRangeParams loadRangeParams, @NonNull PositionalDataSource.LoadRangeCallback<T> loadRangeCallback) {
|
||
|
loadRangeCallback.onResult(loadRange(loadRangeParams.startPosition, loadRangeParams.loadSize));
|
||
|
}
|
||
|
}
|