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);
}
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);
}
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" };
}
}
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;
}
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;
}
Aggregations