Search in sources :

Example 31 with TaskData

use of com.ichi2.async.DeckTask.TaskData in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundConfReset.

private TaskData doInBackgroundConfReset(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundConfReset");
    Object[] data = params[0].getObjArray();
    Collection col = (Collection) data[0];
    JSONObject conf = (JSONObject) data[1];
    col.getDecks().restoreToDefault(conf);
    return new TaskData(true);
}
Also used : JSONObject(org.json.JSONObject) Collection(com.ichi2.libanki.Collection) JSONObject(org.json.JSONObject)

Example 32 with TaskData

use of com.ichi2.async.DeckTask.TaskData 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 33 with TaskData

use of com.ichi2.async.DeckTask.TaskData 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)

Example 34 with TaskData

use of com.ichi2.async.DeckTask.TaskData in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundOpenCollection.

private TaskData doInBackgroundOpenCollection(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundOpenCollection");
    long time = Utils.intNow(1000);
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
    String collectionFile = params[0].getString();
    SharedPreferences prefs = AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext());
    // see, if a collection is still opened
    Collection oldCol = AnkiDroidApp.getCol();
    Collection col = null;
    publishProgress(new TaskData(res.getString(R.string.open_collection)));
    if (!(AnkiDroidApp.colIsOpen() && oldCol.getPath().equals(collectionFile))) {
        // android's delete db bug
        if (BackupManager.safetyBackupNeeded(collectionFile)) {
            publishProgress(new TaskData(res.getString(R.string.backup_collection)));
            BackupManager.performBackup(collectionFile);
        }
        publishProgress(new TaskData(res.getString(R.string.open_collection)));
        // load collection
        try {
            col = AnkiDroidApp.openCollection(collectionFile);
        } catch (RuntimeException e) {
            BackupManager.restoreCollectionIfMissing(collectionFile);
            Log.e(AnkiDroidApp.TAG, "doInBackgroundOpenCollection - RuntimeException on opening collection: " + e);
            AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundOpenCollection");
            return new TaskData(false);
        }
        // create tutorial deck if needed
        if (prefs.contains("createTutorial") && prefs.getBoolean("createTutorial", false)) {
            prefs.edit().remove("createTutorial").commit();
            publishProgress(new TaskData(res.getString(R.string.tutorial_load)));
            doInBackgroundLoadTutorial(new TaskData(col));
        }
    } else {
        Log.i(AnkiDroidApp.TAG, "doInBackgroundOpenCollection: collection still open - reusing it");
        col = oldCol;
    }
    Object[] counts = null;
    DeckTask.TaskData result = doInBackgroundLoadDeckCounts(new TaskData(col));
    if (result != null) {
        counts = result.getObjArray();
    }
    if (prefs.getBoolean("splashScreen", false)) {
        long millies = Utils.intNow(1000) - time;
        if (millies < 1000) {
            try {
                Thread.sleep(1200 - millies);
            } catch (InterruptedException e) {
            }
        }
    }
    return new TaskData(col, counts);
}
Also used : SharedPreferences(android.content.SharedPreferences) Collection(com.ichi2.libanki.Collection) JSONObject(org.json.JSONObject) Resources(android.content.res.Resources)

Example 35 with TaskData

use of com.ichi2.async.DeckTask.TaskData in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundDeleteDeck.

private TaskData doInBackgroundDeleteDeck(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundDeleteDeck");
    Collection col = params[0].getCollection();
    long did = params[0].getLong();
    col.getDecks().rem(did, true);
    return doInBackgroundLoadDeckCounts(new TaskData(col));
}
Also used : Collection(com.ichi2.libanki.Collection)

Aggregations

Collection (com.ichi2.libanki.Collection)24 JSONObject (org.json.JSONObject)13 JSONException (org.json.JSONException)8 TaskData (com.ichi2.async.DeckTask.TaskData)7 Resources (android.content.res.Resources)6 Sched (com.ichi2.libanki.Sched)6 AnkiDb (com.ichi2.anki.AnkiDb)5 Card (com.ichi2.libanki.Card)5 Note (com.ichi2.libanki.Note)5 File (java.io.File)5 DeckTask (com.ichi2.async.DeckTask)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 NotFoundException (android.content.res.Resources.NotFoundException)3 SQLException (android.database.SQLException)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 ZipFile (java.util.zip.ZipFile)3 DialogInterface (android.content.DialogInterface)2 OnCancelListener (android.content.DialogInterface.OnCancelListener)2