Search in sources :

Example 46 with TaskData

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

the class DeckTask method doInBackgroundRepairDeck.

private TaskData doInBackgroundRepairDeck(TaskData... params) {
    // Log.i(AnkiDroidApp.TAG, "doInBackgroundRepairDeck");
    String deckPath = params[0].getString();
    Collection col = params[0].getCollection();
    if (col != null) {
        col.close(false);
    }
    return new TaskData(BackupManager.repairDeck(deckPath));
}
Also used : Collection(com.ichi2.libanki.Collection)

Example 47 with TaskData

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

the class DeckTask method doInBackgroundRebuildCram.

private TaskData doInBackgroundRebuildCram(TaskData... params) {
    // Log.i(AnkiDroidApp.TAG, "doInBackgroundRebuildCram");
    Collection col = params[0].getCollection();
    boolean fragmented = params[0].getBoolean();
    long did = params[0].getLong();
    col.getSched().rebuildDyn(did);
    return doInBackgroundUpdateValuesFromDeck(new DeckTask.TaskData(col, new Object[] { true, fragmented }));
}
Also used : Collection(com.ichi2.libanki.Collection) JSONObject(org.json.JSONObject)

Example 48 with TaskData

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

the class DeckTask method doInBackgroundRestoreDeck.

private TaskData doInBackgroundRestoreDeck(TaskData... params) {
    // Log.i(AnkiDroidApp.TAG, "doInBackgroundRestoreDeck");
    Object[] data = params[0].getObjArray();
    Collection col = (Collection) data[0];
    if (col != null) {
        col.close(false);
    }
    return new TaskData(BackupManager.restoreBackup((String) data[1], (String) data[2]));
}
Also used : Collection(com.ichi2.libanki.Collection) JSONObject(org.json.JSONObject)

Example 49 with TaskData

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

the class DeckTask method doInBackgroundDismissNote.

private TaskData doInBackgroundDismissNote(TaskData... params) {
    Sched sched = params[0].getSched();
    Collection col = sched.getCol();
    Card card = params[0].getCard();
    Note note = card.note();
    int type = params[0].getInt();
    try {
        col.getDb().getDatabase().beginTransaction();
        try {
            switch(type) {
                case 4:
                    // collect undo information
                    col.markUndo(Collection.UNDO_BURY_CARD, new Object[] { col.getDirty(), note.cards(), card.getId() });
                    // then bury
                    sched.buryCards(new long[] { card.getId() });
                    sHadCardQueue = true;
                    break;
                case 0:
                    // collect undo information
                    col.markUndo(Collection.UNDO_BURY_NOTE, new Object[] { col.getDirty(), note.cards(), card.getId() });
                    // then bury
                    sched.buryNote(note.getId());
                    sHadCardQueue = true;
                    break;
                case 1:
                    // collect undo information
                    col.markUndo(Collection.UNDO_SUSPEND_CARD, new Object[] { card });
                    // suspend card
                    if (card.getQueue() == -1) {
                        sched.unsuspendCards(new long[] { card.getId() });
                    } else {
                        sched.suspendCards(new long[] { card.getId() });
                    }
                    sHadCardQueue = true;
                    break;
                case 2:
                    // collect undo information
                    ArrayList<Card> cards = note.cards();
                    long[] cids = new long[cards.size()];
                    for (int i = 0; i < cards.size(); i++) {
                        cids[i] = cards.get(i).getId();
                    }
                    col.markUndo(Collection.UNDO_SUSPEND_NOTE, new Object[] { cards, card.getId() });
                    // suspend note
                    sched.suspendCards(cids);
                    sHadCardQueue = true;
                    break;
                case 3:
                    // collect undo information
                    ArrayList<Card> allCs = note.cards();
                    long[] cardIds = new long[allCs.size()];
                    for (int i = 0; i < allCs.size(); i++) {
                        cardIds[i] = allCs.get(i).getId();
                    }
                    col.markUndo(Collection.UNDO_DELETE_NOTE, new Object[] { note, allCs, card.getId() });
                    // delete note
                    col.remNotes(new long[] { note.getId() });
                    sHadCardQueue = true;
                    break;
            }
            publishProgress(new TaskData(getCard(col.getSched()), 0));
            col.getDb().getDatabase().setTransactionSuccessful();
        } finally {
            col.getDb().getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundSuspendCard - RuntimeException on suspending card: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSuspendCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : Sched(com.ichi2.libanki.Sched) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Card(com.ichi2.libanki.Card)

Example 50 with TaskData

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

the class DeckTask method doInBackgroundImportAdd.

private TaskData doInBackgroundImportAdd(TaskData... params) {
    // Log.i(AnkiDroidApp.TAG, "doInBackgroundImportAdd");
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
    Collection col = params[0].getCollection();
    String path = params[0].getString();
    boolean sharedDeckImport = params[0].getBoolean();
    ProgressCallback pc = null;
    // don't report progress on shared deck import (or maybe should we?)
    if (!sharedDeckImport) {
        pc = new ProgressCallback(this, res);
    }
    int addedCount = -1;
    try {
        Anki2Importer imp = new Anki2Importer(col, path, pc);
        AnkiDb ankiDB = col.getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            addedCount = imp.run();
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
            if (sharedDeckImport) {
                File tmpFile = new File(path);
                tmpFile.delete();
            }
        }
        if (addedCount >= 0) {
            ankiDB.execute("VACUUM");
            ankiDB.execute("ANALYZE");
        }
        publishProgress(new TaskData(res.getString(R.string.import_update_counts)));
        // Update the counts
        DeckTask.TaskData result = doInBackgroundLoadDeckCounts(new TaskData(col));
        if (result == null) {
            return null;
        }
        return new TaskData(addedCount, result.getObjArray(), true);
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportAdd - RuntimeException on importing cards: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportAdd");
        return new TaskData(false);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportAdd - IOException on importing cards: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportAdd");
        return new TaskData(false);
    }
}
Also used : Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) AnkiDb(com.ichi2.anki.AnkiDb) IOException(java.io.IOException) Collection(com.ichi2.libanki.Collection) Resources(android.content.res.Resources) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

Collection (com.ichi2.libanki.Collection)67 TaskData (com.ichi2.async.TaskData)46 JSONObject (com.ichi2.utils.JSONObject)35 Card (com.ichi2.libanki.Card)16 JSONException (com.ichi2.utils.JSONException)15 ArrayList (java.util.ArrayList)14 JSONObject (org.json.JSONObject)13 Resources (android.content.res.Resources)12 HashMap (java.util.HashMap)12 SharedPreferences (android.content.SharedPreferences)11 Deck (com.ichi2.libanki.Deck)11 Intent (android.content.Intent)10 View (android.view.View)9 TextView (android.widget.TextView)9 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)9 TaskListener (com.ichi2.async.TaskListener)9 Map (java.util.Map)9 VisibleForTesting (androidx.annotation.VisibleForTesting)8 CollectionTask (com.ichi2.async.CollectionTask)8 IOException (java.io.IOException)8