Search in sources :

Example 6 with AnkiDb

use of com.ichi2.anki.AnkiDb in project Anki-Android by Ramblurr.

the class Storage method check.

/*
     * Upgrading ************************************************************
     */
// public void upgrade(String path) {
// // mPath = path;
// // _openDB(path);
// // upgradeSchema();
// // _openCol();
// // _upgradeRest();
// // return mCol;
// }
/*
     * Integrity checking ************************************************************
     */
public static boolean check(String path) {
    AnkiDb db = AnkiDatabaseManager.getDatabase(path);
    // corrupt?
    try {
        if (!db.queryString("PRAGMA integrity_check").equalsIgnoreCase("ok")) {
            return false;
        }
    } catch (SQLException _) {
        return false;
    }
    // old version?
    if (db.queryScalar("SELECT version FROM decks") < 65) {
        return false;
    }
    // ensure we have indices for checks below
    db.execute("create index if not exists ix_cards_factId on cards (factId)");
    db.execute("create index if not exists ix_fields_factId on fieldModels (factId)");
    db.execute("analyze");
    // fields missing a field model?
    if (db.queryColumn(Integer.class, "select id from fields where fieldModelId not in (" + "select distinct id from fieldModels)", 0).size() > 0) {
        return false;
    }
    // facts missing a field?
    if (db.queryColumn(Integer.class, "select distinct facts.id from facts, fieldModels where " + "facts.modelId = fieldModels.modelId and fieldModels.id not in " + "(select fieldModelId from fields where factId = facts.id)", 0).size() > 0) {
        return false;
    }
    // cards missing a fact?
    if (db.queryColumn(Integer.class, "select id from cards where factId not in (select id from facts)", 0).size() > 0) {
        return false;
    }
    // cards missing a card model?
    if (db.queryColumn(Integer.class, "select id from cards where cardModelId not in (select id from cardModels)", 0).size() > 0) {
        return false;
    }
    // cards with a card model from the wrong model?
    if (db.queryColumn(Integer.class, "select id from cards where cardModelId not in (select cm.id from " + "cardModels cm, facts f where cm.modelId = f.modelId and " + "f.id = cards.factId)", 0).size() > 0) {
        return false;
    }
    // facts missing a card?
    if (db.queryColumn(Integer.class, "select facts.id from facts " + "where facts.id not in (select distinct factId from cards)", 0).size() > 0) {
        return false;
    }
    // dangling fields?
    if (db.queryColumn(Integer.class, "select id from fields where factId not in (select id from facts)", 0).size() > 0) {
        return false;
    }
    // fields without matching interval
    if (db.queryColumn(Integer.class, "select id from fields where ordinal != (select ordinal from fieldModels " + "where id = fieldModelId)", 0).size() > 0) {
        return false;
    }
    // incorrect types
    if (db.queryColumn(Integer.class, "select id from cards where relativeDelay != (case " + "when successive then 1 when reps then 0 else 2 end)", 0).size() > 0) {
        return false;
    }
    if (db.queryColumn(Integer.class, "select id from cards where type != (case " + "when type >= 0 then relativeDelay else relativeDelay - 3 end)", 0).size() > 0) {
        return false;
    }
    return true;
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) SQLException(android.database.SQLException)

Example 7 with AnkiDb

use of com.ichi2.anki.AnkiDb in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundAddNote.

private TaskData doInBackgroundAddNote(TaskData[] params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundAddNote");
    Note note = params[0].getNote();
    Collection col = note.getCol();
    try {
        AnkiDb ankiDB = col.getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            publishProgress(new TaskData(col.addNote(note)));
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundAddNote - RuntimeException on adding fact: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundAddNote");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection)

Example 8 with AnkiDb

use of com.ichi2.anki.AnkiDb in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundAnswerCard.

private TaskData doInBackgroundAnswerCard(TaskData... params) {
    Sched sched = params[0].getSched();
    Card oldCard = params[0].getCard();
    int ease = params[0].getInt();
    Card newCard = null;
    // TODO: proper leech handling
    int oldCardLeech = 0;
    // 0: normal; 1: leech; 2: leech & suspended
    try {
        AnkiDb ankiDB = sched.getCol().getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            if (oldCard != null) {
                sched.answerCard(oldCard, ease);
            }
            if (newCard == null) {
                newCard = getCard(sched);
            }
            if (newCard != null) {
                // render cards before locking database
                newCard._getQA(true);
            }
            publishProgress(new TaskData(newCard, oldCardLeech));
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundAnswerCard - RuntimeException on answering card: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundAnswerCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) Sched(com.ichi2.libanki.Sched) Card(com.ichi2.libanki.Card)

Example 9 with AnkiDb

use of com.ichi2.anki.AnkiDb in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundMarkCard.

private TaskData doInBackgroundMarkCard(TaskData... params) {
    Card card = params[0].getCard();
    Sched sched = params[0].getSched();
    try {
        AnkiDb ankiDB = sched.getCol().getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            if (card != null) {
                Note note = card.note();
                sched.getCol().markUndo(Collection.UNDO_MARK_NOTE, new Object[] { note.getId(), note.stringTags(), card.getId() });
                if (note.hasTag("marked")) {
                    note.delTag("marked");
                } else {
                    note.addTag("marked");
                }
                note.flush();
            }
            publishProgress(new TaskData(card));
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundMarkCard - RuntimeException on marking card: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundMarkCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) Sched(com.ichi2.libanki.Sched) Note(com.ichi2.libanki.Note) Card(com.ichi2.libanki.Card)

Aggregations

AnkiDb (com.ichi2.anki.AnkiDb)9 File (java.io.File)5 IOException (java.io.IOException)4 SQLiteDatabaseCorruptException (android.database.sqlite.SQLiteDatabaseCorruptException)3 Collection (com.ichi2.libanki.Collection)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 Card (com.ichi2.libanki.Card)2 Note (com.ichi2.libanki.Note)2 Sched (com.ichi2.libanki.Sched)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 HttpResponse (org.apache.http.HttpResponse)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Resources (android.content.res.Resources)1