use of com.ichi2.libanki.Note in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundAddNote.
private TaskData doInBackgroundAddNote(TaskData[] params) {
Log.i(AnkiDroidApp.TAG, "doInBackgroundAddNote");
Note note = params[0].getNote();
Collection col = note.getCol();
try {
AnkiDb ankiDB = col.getDb();
ankiDB.getDatabase().beginTransaction();
try {
publishProgress(new TaskData(col.addNote(note)));
ankiDB.getDatabase().setTransactionSuccessful();
} finally {
ankiDB.getDatabase().endTransaction();
}
} catch (RuntimeException e) {
Log.e(AnkiDroidApp.TAG, "doInBackgroundAddNote - RuntimeException on adding fact: " + e);
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundAddNote");
return new TaskData(false);
}
return new TaskData(true);
}
use of com.ichi2.libanki.Note in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundLoadTutorial.
private TaskData doInBackgroundLoadTutorial(TaskData... params) {
Log.i(AnkiDroidApp.TAG, "doInBackgroundLoadTutorial");
Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
Collection col = params[0].getCollection();
col.getDb().getDatabase().beginTransaction();
String title = res.getString(R.string.help_tutorial);
try {
// get deck or create it
long did = col.getDecks().id(title);
// reset todays counts
JSONObject d = col.getDecks().get(did);
for (String t : new String[] { "new", "rev", "lrn", "time" }) {
String k = t + "Today";
JSONArray ja = new JSONArray();
ja.put(col.getSched().getToday());
ja.put(0);
d.put(k, ja);
}
// save deck
col.getDecks().save(d);
if (col.getSched().cardCount("(" + did + ")") > 0) {
// deck does already exist. Remove all cards and recreate them
// to ensure the correct order
col.remCards(col.getDecks().cids(did));
}
JSONObject model = col.getModels().byName(title);
// }
if (model == null) {
model = col.getModels().addBasicModel(col, title);
}
model.put("did", did);
String[] questions = res.getStringArray(R.array.tutorial_questions);
String[] answers = res.getStringArray(R.array.tutorial_answers);
String[] sampleQuestions = res.getStringArray(R.array.tutorial_capitals_questions);
String[] sampleAnswers = res.getStringArray(R.array.tutorial_capitals_answers);
int len = Math.min(questions.length, answers.length);
for (int i = 0; i < len + Math.min(sampleQuestions.length, sampleAnswers.length); i++) {
Note note = col.newNote(model);
if (note.values().length < 2) {
return new TaskData(false);
}
note.values()[0] = (i < len) ? questions[i] : sampleQuestions[i - len];
note.values()[1] = (i < len) ? answers[i] : sampleAnswers[i - len];
col.addNote(note);
}
// deck.setSessionTimeLimit(0);
if (col.getSched().cardCount("(" + did + ")") == 0) {
// error, delete deck
col.getDecks().rem(did, true);
return new TaskData(false);
} else {
col.save();
col.getDecks().select(did);
col.getDb().getDatabase().setTransactionSuccessful();
return new TaskData(true);
}
} catch (SQLException e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundLoadTutorial");
return new DeckTask.TaskData(false);
} catch (JSONException e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundLoadTutorial");
return new DeckTask.TaskData(false);
} finally {
col.getDb().getDatabase().endTransaction();
}
}
use of com.ichi2.libanki.Note in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundMarkCard.
private TaskData doInBackgroundMarkCard(TaskData... params) {
Card card = params[0].getCard();
Sched sched = params[0].getSched();
try {
AnkiDb ankiDB = sched.getCol().getDb();
ankiDB.getDatabase().beginTransaction();
try {
if (card != null) {
Note note = card.note();
sched.getCol().markUndo(Collection.UNDO_MARK_NOTE, new Object[] { note.getId(), note.stringTags(), card.getId() });
if (note.hasTag("marked")) {
note.delTag("marked");
} else {
note.addTag("marked");
}
note.flush();
}
publishProgress(new TaskData(card));
ankiDB.getDatabase().setTransactionSuccessful();
} finally {
ankiDB.getDatabase().endTransaction();
}
} catch (RuntimeException e) {
Log.e(AnkiDroidApp.TAG, "doInBackgroundMarkCard - RuntimeException on marking card: " + e);
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundMarkCard");
return new TaskData(false);
}
return new TaskData(true);
}
Aggregations