Search in sources :

Example 26 with Collection

use of com.ichi2.libanki.Collection 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 27 with Collection

use of com.ichi2.libanki.Collection 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 28 with Collection

use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.

the class FullSyncer method download.

@Override
public Object[] download() {
    InputStream cont;
    try {
        HttpResponse ret = super.req("download");
        if (ret == null) {
            return null;
        }
        cont = ret.getEntity().getContent();
    } catch (IllegalStateException e1) {
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        return null;
    }
    String path = AnkiDroidApp.getCollectionPath();
    if (mCol != null) {
        mCol.close(false);
        mCol = null;
    }
    String tpath = path + ".tmp";
    if (!super.writeToFile(cont, tpath)) {
        return new Object[] { "sdAccessError" };
    }
    // first check, if account needs upgrade (from 1.2)
    try {
        FileInputStream fis = new FileInputStream(tpath);
        if (super.stream2String(fis, 15).equals("upgradeRequired")) {
            return new Object[] { "upgradeRequired" };
        }
    } catch (FileNotFoundException e1) {
        throw new RuntimeException(e1);
    }
    // check the received file is ok
    mCon.publishProgress(R.string.sync_check_download_file);
    try {
        AnkiDb d = AnkiDatabaseManager.getDatabase(tpath);
        if (!d.queryString("PRAGMA integrity_check").equalsIgnoreCase("ok")) {
            Log.e(AnkiDroidApp.TAG, "Full sync - downloaded file corrupt");
            return new Object[] { "remoteDbError" };
        }
    } catch (SQLiteDatabaseCorruptException e) {
        Log.e(AnkiDroidApp.TAG, "Full sync - downloaded file corrupt");
        return new Object[] { "remoteDbError" };
    } finally {
        AnkiDatabaseManager.closeDatabase(tpath);
    }
    // overwrite existing collection
    File newFile = new File(tpath);
    if (newFile.renameTo(new File(path))) {
        return new Object[] { "success" };
    } else {
        return new Object[] { "overwriteError" };
    }
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) HttpResponse(org.apache.http.HttpResponse) SQLiteDatabaseCorruptException(android.database.sqlite.SQLiteDatabaseCorruptException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 29 with Collection

use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.

the class Storage method Collection.

public static Collection Collection(String path, boolean server) {
    assert path.endsWith(".anki2");
    File dbFile = new File(path);
    boolean create = !dbFile.exists();
    if (create) {
        AnkiDroidApp.createDirectoryIfMissing(dbFile.getParentFile());
    }
    // connect
    AnkiDb db = AnkiDatabaseManager.getDatabase(path);
    int ver;
    if (create) {
        ver = _createDB(db);
    } else {
        ver = _upgradeSchema(db);
    }
    db.execute("PRAGMA temp_store = memory");
    // LIBANKI: sync, journal_mode --> in AnkiDroid done in AnkiDb
    // add db to col and do any remaining upgrades
    Collection col = new Collection(db, path, server);
    if (ver < Collection.SCHEMA_VERSION) {
        _upgrade(col, ver);
    } else if (create) {
        // add in reverse order so basic is default
        Models.addClozeModel(col);
        Models.addForwardOptionalReverse(col);
        Models.addForwardReverse(col);
        Models.addBasicModel(col);
        col.save();
    }
    return col;
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) File(java.io.File)

Example 30 with Collection

use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.

the class DiskUtil method getStoringDirectory.

public static File getStoringDirectory() {
    Collection col = AnkiDroidApp.getCol();
    String mediaDir = col.getMedia().getDir() + "/";
    File mediaDirFile = new File(mediaDir);
    return mediaDirFile;
}
Also used : Collection(com.ichi2.libanki.Collection) File(java.io.File)

Aggregations

Collection (com.ichi2.libanki.Collection)40 JSONObject (org.json.JSONObject)20 File (java.io.File)13 JSONException (org.json.JSONException)12 IOException (java.io.IOException)11 Resources (android.content.res.Resources)10 DialogInterface (android.content.DialogInterface)6 DeckTask (com.ichi2.async.DeckTask)6 TaskData (com.ichi2.async.DeckTask.TaskData)6 StyledDialog (com.ichi2.themes.StyledDialog)6 AnkiDb (com.ichi2.anki.AnkiDb)5 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 Intent (android.content.Intent)4 Note (com.ichi2.libanki.Note)4 FileNotFoundException (java.io.FileNotFoundException)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 JSONArray (org.json.JSONArray)4 OnCancelListener (android.content.DialogInterface.OnCancelListener)3