Search in sources :

Example 16 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class AnkiActivity method importReplace.

@Override
public void importReplace(String importPath) {
    try {
        AnkiDroidApp.getSharedPrefs(this).edit().remove(Consts.KEY_SYNC_CHINA_SESSION).apply();
        CollectionTask.launchCollectionTask(IMPORT_REPLACE, importReplaceListener(), new TaskData(importPath));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : JSONException(com.ichi2.utils.JSONException) IOException(java.io.IOException) ActivityNotFoundException(android.content.ActivityNotFoundException) DeckRenameException(com.ichi2.anki.exception.DeckRenameException) TaskData(com.ichi2.async.TaskData)

Example 17 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackgroundAnswerCard.

private TaskData doInBackgroundAnswerCard(TaskData param) {
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    Card oldCard = param.getCard();
    @Consts.BUTTON_TYPE int ease = param.getInt();
    Card newCard = null;
    Timber.i(oldCard != null ? "Answering card" : "Obtaining card");
    try {
        DB db = col.getDb();
        db.getDatabase().beginTransaction();
        try {
            if (oldCard != null) {
                Timber.i("Answering card %d", oldCard.getId());
                sched.answerCard(oldCard, ease);
            }
            newCard = sched.getCard();
            if (newCard != null) {
                // render cards before locking database
                newCard._getQA(true);
            }
            publishProgress(new TaskData(newCard));
            db.getDatabase().setTransactionSuccessful();
        } finally {
            db.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Timber.e(e, "doInBackgroundAnswerCard - RuntimeException on answering card");
        AnkiDroidApp.sendExceptionReport(e, "doInBackgroundAnswerCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AbstractSched(com.ichi2.libanki.sched.AbstractSched) Collection(com.ichi2.libanki.Collection) DB(com.ichi2.libanki.DB) Card(com.ichi2.libanki.Card)

Example 18 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackgroundUpdateNote.

private TaskData doInBackgroundUpdateNote(TaskData param) {
    Timber.d("doInBackgroundUpdateNote");
    // Save the note
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    Card editCard = param.getCard();
    Note editNote = editCard.note();
    boolean fromReviewer = param.getBoolean();
    try {
        col.getDb().getDatabase().beginTransaction();
        try {
            // TODO: undo integration
            editNote.flush();
            // flush card too, in case, did has been changed
            editCard.flush();
            if (fromReviewer) {
                Card newCard;
                if (col.getDecks().active().contains(editCard.getDid())) {
                    newCard = editCard;
                    newCard.load();
                    // reload qa-cache
                    newCard.q(true);
                } else {
                    newCard = sched.getCard();
                }
                publishProgress(new TaskData(newCard));
            } else {
                publishProgress(new TaskData(editCard, editNote.stringTags()));
            }
            col.getDb().getDatabase().setTransactionSuccessful();
        } finally {
            col.getDb().getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Timber.e(e, "doInBackgroundUpdateNote - RuntimeException on updating note");
        AnkiDroidApp.sendExceptionReport(e, "doInBackgroundUpdateNote");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AbstractSched(com.ichi2.libanki.sched.AbstractSched) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Card(com.ichi2.libanki.Card)

Example 19 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackgroundCheckDatabase.

private TaskData doInBackgroundCheckDatabase() {
    Timber.d("doInBackgroundCheckDatabase");
    Collection col = getCol();
    // Don't proceed if collection closed
    if (col == null) {
        Timber.e("doInBackgroundCheckDatabase :: supplied collection was null");
        return new TaskData(false);
    }
    Collection.CheckDatabaseResult result = col.fixIntegrity(new ProgressCallback(this, AnkiDroidApp.getAppResources()));
    if (result.getFailed()) {
        // we can fail due to a locked database, which requires knowledge of the failure.
        return new TaskData(false, new Object[] { result });
    } else {
        // Close the collection and we restart the app to reload
        CollectionHelper.getInstance().closeCollection(true, "Check Database Completed");
        return new TaskData(true, new Object[] { result });
    }
}
Also used : Collection(com.ichi2.libanki.Collection)

Example 20 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackgroundEmptyCram.

private TaskData doInBackgroundEmptyCram() {
    Timber.d("doInBackgroundEmptyCram");
    Collection col = getCol();
    col.getSched().emptyDyn(col.getDecks().selected());
    return doInBackgroundUpdateValuesFromDeck(new TaskData(new Object[] { true }));
}
Also used : Collection(com.ichi2.libanki.Collection) JSONObject(com.ichi2.utils.JSONObject)

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