use of com.ichi2.libanki.DB in project Anki-Android by ankidroid.
the class DbUtils method performQuery.
/**
* performs a query on an unopened collection
*/
public static void performQuery(Context context, String query) {
if (Storage.isInMemory()) {
throw new IllegalStateException("cannot use performQuery in memory");
}
DB db = null;
try {
db = new DB(CollectionHelper.getCollectionPath(context));
db.executeScript(query);
} finally {
if (db != null) {
db.close();
}
}
}
use of com.ichi2.libanki.DB in project Anki-Android by ankidroid.
the class CollectionUtils method lockDatabase.
public static void lockDatabase(Collection collection) {
DB db = collection.getDb();
DB spy = spy(db);
doThrow(SQLiteDatabaseLockedException.class).when(spy).execute(any());
doThrow(SQLiteDatabaseLockedException.class).when(spy).execute(any(), any());
SupportSQLiteDatabase spiedDb = spy(spy.getDatabase());
when(spy.getDatabase()).thenReturn(spiedDb);
doThrow(SQLiteDatabaseLockedException.class).when(spiedDb).beginTransaction();
collection.setDb(spy);
}
use of com.ichi2.libanki.DB in project Anki-Android by ankidroid.
the class DeckPickerCheckDatabaseListenerTest method validResultWithLockedDatabaseWillShowLockedDialog.
@Test
public void validResultWithLockedDatabaseWillShowLockedDialog() {
CheckDatabaseResult lockedDb = lockedDatabase();
Pair<Boolean, Collection.CheckDatabaseResult> result = validResultWithData(lockedDb);
execute(result);
assertThat("Load Failed dialog should not be shown if invalid data is supplied", !mImpl.didDisplayDialogLoadFailed());
assertThat("Locked Database dialog should be shown if Db was locked", mImpl.didDisplayLockedDialog());
assertThat("Dialog should not be displayed", !mImpl.didDisplayMessage());
}
use of com.ichi2.libanki.DB in project Anki-Android by ankidroid.
the class CardContentProvider method answerCard.
private void answerCard(Collection col, AbstractSched sched, Card cardToAnswer, @Consts.BUTTON_TYPE int ease, long timeTaken) {
try {
DB db = col.getDb();
db.getDatabase().beginTransaction();
try {
if (cardToAnswer != null) {
if (timeTaken != -1) {
cardToAnswer.setTimerStarted(col.getTime().intTimeMS() - timeTaken);
}
sched.answerCard(cardToAnswer, ease);
}
db.getDatabase().setTransactionSuccessful();
} finally {
DB.safeEndInTransaction(db);
}
} catch (RuntimeException e) {
Timber.e(e, "answerCard - RuntimeException on answering card");
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundAnswerCard");
}
}
use of com.ichi2.libanki.DB in project Anki-Android by ankidroid.
the class Media method addFilesFromZip.
/**
* Extract zip data; return the number of files extracted. Unlike the python version, this method consumes a
* ZipFile stored on disk instead of a String buffer. Holding the entire downloaded data in memory is not feasible
* since some devices can have very limited heap space.
*
* This method closes the file before it returns.
*/
public int addFilesFromZip(ZipFile z) throws IOException {
try {
// get meta info first
JSONObject meta = new JSONObject(Utils.convertStreamToString(z.getInputStream(z.getEntry("_meta"))));
// then loop through all files
int cnt = 0;
ArrayList<? extends ZipEntry> zipEntries = Collections.list(z.entries());
List<Object[]> media = new ArrayList<>(zipEntries.size());
for (ZipEntry i : zipEntries) {
String fileName = i.getName();
if ("_meta".equals(fileName)) {
// ignore previously-retrieved meta
continue;
}
String name = meta.getString(fileName);
// normalize name for platform
name = Utils.nfcNormalized(name);
// save file
String destPath = (dir() + File.separator) + name;
try (InputStream zipInputStream = z.getInputStream(i)) {
Utils.writeToFile(zipInputStream, destPath);
}
String csum = Utils.fileChecksum(destPath);
// update db
media.add(new Object[] { name, csum, _mtime(destPath), 0 });
cnt += 1;
}
if (!media.isEmpty()) {
mDb.executeMany("insert or replace into media values (?,?,?,?)", media);
}
return cnt;
} finally {
z.close();
}
}
Aggregations