Search in sources :

Example 16 with TaskData

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

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

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

the class DeckTask method doInBackgroundUpdateNote.

private TaskData doInBackgroundUpdateNote(TaskData[] params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundUpdateNote");
    // Save the note
    Sched sched = params[0].getSched();
    Collection col = sched.getCol();
    Card editCard = params[0].getCard();
    Note editNote = editCard.note();
    boolean fromReviewer = params[0].getBoolean();
    // mark undo
    col.markUndo(Collection.UNDO_EDIT_NOTE, new Object[] { col.getNote(editNote.getId()), editCard.getId(), fromReviewer });
    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.getQuestion(true);
                } else {
                    newCard = getCard(sched);
                }
                publishProgress(new TaskData(newCard));
            } else {
                publishProgress(new TaskData(editCard, editNote.stringTags()));
            }
            col.getDb().getDatabase().setTransactionSuccessful();
        } finally {
            col.getDb().getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundUpdateNote - RuntimeException on updating fact: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundUpdateNote");
        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 19 with TaskData

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

the class DeckPicker method onStop.

@Override
protected void onStop() {
    Log.i(AnkiDroidApp.TAG, "DeckPicker - onStop");
    super.onStop();
    if (!mDontSaveOnStop) {
        if (isFinishing()) {
            DeckTask.launchDeckTask(DeckTask.TASK_TYPE_CLOSE_DECK, mCloseCollectionHandler, new TaskData(AnkiDroidApp.getCol()));
        } else {
            StudyOptionsFragment frag = getFragment();
            if (!(frag != null && !frag.dbSaveNecessary())) {
                UIUtils.saveCollectionInBackground();
            }
        }
    }
}
Also used : TaskData(com.ichi2.async.DeckTask.TaskData)

Example 20 with TaskData

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

the class DeckPicker method showStartupScreensAndDialogs.

private void showStartupScreensAndDialogs(SharedPreferences preferences, int skip) {
    if (skip < 1 && preferences.getLong("lastTimeOpened", 0) == 0) {
        Intent infoIntent = new Intent(this, Info.class);
        infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_WELCOME);
        startActivityForResult(infoIntent, SHOW_INFO_WELCOME);
        if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) {
            ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT);
        }
    } else if (skip < 2 && !preferences.getString("lastVersion", "").equals(AnkiDroidApp.getPkgVersionName())) {
        preferences.edit().putBoolean("showBroadcastMessageToday", true).commit();
        Intent infoIntent = new Intent(this, Info.class);
        infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_NEW_VERSION);
        startActivityForResult(infoIntent, SHOW_INFO_NEW_VERSION);
        if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) {
            ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT);
        }
    } else if (skip < 3 && upgradeNeeded()) {
        // Note that the "upgrade needed" refers to upgrading Anki 1.x decks, not to newer
        // versions of AnkiDroid.
        AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).edit().putInt("lastUpgradeVersion", AnkiDroidApp.getPkgVersionCode()).commit();
        showUpgradeScreen(skip != 0, Info.UPGRADE_SCREEN_BASIC1);
    } else if (skip < 4 && hasErrorFiles()) {
        Intent i = new Intent(this, Feedback.class);
        startActivityForResult(i, REPORT_ERROR);
        if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) {
            ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT);
        }
    } else if (!AnkiDroidApp.isSdCardMounted()) {
        showDialog(DIALOG_SD_CARD_NOT_MOUNTED);
    } else if (!BackupManager.enoughDiscSpace(mPrefDeckPath)) {
        // && !preferences.getBoolean("dontShowLowMemory",
        // false)) {
        showDialog(DIALOG_NO_SPACE_LEFT);
    } else if (preferences.getBoolean("noSpaceLeft", false)) {
        showDialog(DIALOG_BACKUP_NO_SPACE_LEFT);
        preferences.edit().putBoolean("noSpaceLeft", false).commit();
    } else if (mImportPath != null && AnkiDroidApp.colIsOpen()) {
        showDialog(DIALOG_IMPORT);
    } else {
        // AnkiDroid is being updated and a collection already exists. We check if we are upgrading
        // to a version that contains additions to the database integrity check routine that we would
        // like to run on all collections. A missing version number is assumed to be a fresh
        // installation of AnkiDroid and we don't run the check.
        int current = AnkiDroidApp.getPkgVersionCode();
        // a non-final variable, for intermediate calculations
        int previousTemp;
        if (!preferences.contains("lastUpgradeVersion")) {
            // Fresh install
            previousTemp = current;
        } else {
            try {
                previousTemp = preferences.getInt("lastUpgradeVersion", current);
            } catch (ClassCastException e) {
                // Previous versions stored this as a string.
                String s = preferences.getString("lastUpgradeVersion", "");
                // check.
                if (s.equals("2.0.2")) {
                    previousTemp = 40;
                } else {
                    previousTemp = 0;
                }
            }
        }
        final int previous = previousTemp;
        preferences.edit().putInt("lastUpgradeVersion", current).commit();
        if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION || previous < AnkiDroidApp.CHECK_PREFERENCES_AT_VERSION) {
            DeckTask.launchDeckTask(DeckTask.TASK_TYPE_OPEN_COLLECTION, new Listener() {

                @Override
                public void onPostExecute(DeckTask task, TaskData result) {
                    mOpenCollectionHandler.onPostExecute(result);
                    if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION) {
                        integrityCheck();
                    }
                    if (previous < AnkiDroidApp.CHECK_PREFERENCES_AT_VERSION) {
                        upgradePreferences(previous);
                    }
                }

                @Override
                public void onPreExecute(DeckTask task) {
                }

                @Override
                public void onProgressUpdate(DeckTask task, TaskData... values) {
                }
            }, new DeckTask.TaskData(AnkiDroidApp.getCollectionPath()));
        } else {
            loadCollection();
        }
    }
}
Also used : OnGlobalLayoutListener(android.view.ViewTreeObserver.OnGlobalLayoutListener) OnCancelListener(android.content.DialogInterface.OnCancelListener) SimpleOnGestureListener(android.view.GestureDetector.SimpleOnGestureListener) Listener(com.ichi2.async.DeckTask.Listener) OnClickListener(android.view.View.OnClickListener) Intent(android.content.Intent) ActivityInfo(android.content.pm.ActivityInfo) ContextMenuInfo(android.view.ContextMenu.ContextMenuInfo) DeckTask(com.ichi2.async.DeckTask) TaskData(com.ichi2.async.DeckTask.TaskData)

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