Search in sources :

Example 21 with DB

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();
        }
    }
}
Also used : DB(com.ichi2.libanki.DB)

Example 22 with DB

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);
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) DB(com.ichi2.libanki.DB)

Example 23 with DB

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());
}
Also used : CheckDatabaseResult(com.ichi2.libanki.Collection.CheckDatabaseResult) Test(org.junit.Test)

Example 24 with DB

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");
    }
}
Also used : DB(com.ichi2.libanki.DB)

Example 25 with DB

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();
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList)

Aggregations

File (java.io.File)19 JSONObject (com.ichi2.utils.JSONObject)18 Collection (com.ichi2.libanki.Collection)14 IOException (java.io.IOException)14 DB (com.ichi2.libanki.DB)13 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)11 JSONArray (com.ichi2.utils.JSONArray)10 Test (org.junit.Test)10 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)9 ContentValues (android.content.ContentValues)8 Resources (android.content.res.Resources)8 Uri (android.net.Uri)8 JSONException (com.ichi2.utils.JSONException)8 Cursor (android.database.Cursor)7 Model (com.ichi2.libanki.Model)7 ContentResolver (android.content.ContentResolver)6 FileInputStream (java.io.FileInputStream)6 SQLException (android.database.SQLException)4 BufferedInputStream (java.io.BufferedInputStream)4