use of com.ichi2.async.TaskData in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundConfReset.
private TaskData doInBackgroundConfReset(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundConfReset");
Object[] data = params[0].getObjArray();
Collection col = (Collection) data[0];
JSONObject conf = (JSONObject) data[1];
col.getDecks().restoreToDefault(conf);
return new TaskData(true);
}
use of com.ichi2.async.TaskData 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.async.TaskData in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundLoadStatistics.
private TaskData doInBackgroundLoadStatistics(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundLoadStatistics");
Collection col = params[0].getCollection();
int type = params[0].getInt();
boolean wholeCollection = params[0].getBoolean();
Stats stats = new Stats(col, wholeCollection);
switch(type) {
default:
case Stats.TYPE_FORECAST:
return new TaskData(stats.calculateDue(AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).getInt("statsType", Stats.TYPE_MONTH)));
case Stats.TYPE_REVIEW_COUNT:
return new TaskData(stats.calculateDone(AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).getInt("statsType", Stats.TYPE_MONTH), true));
case Stats.TYPE_REVIEW_TIME:
return new TaskData(stats.calculateDone(AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).getInt("statsType", Stats.TYPE_MONTH), false));
}
}
use of com.ichi2.async.TaskData 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.async.TaskData in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundAnswerCard.
private TaskData doInBackgroundAnswerCard(TaskData... params) {
Sched sched = params[0].getSched();
Card oldCard = params[0].getCard();
int ease = params[0].getInt();
Card newCard = null;
// TODO: proper leech handling
int oldCardLeech = 0;
// 0: normal; 1: leech; 2: leech & suspended
try {
AnkiDb ankiDB = sched.getCol().getDb();
ankiDB.getDatabase().beginTransaction();
try {
if (oldCard != null) {
sched.answerCard(oldCard, ease);
}
if (newCard == null) {
newCard = getCard(sched);
}
if (newCard != null) {
// render cards before locking database
newCard._getQA(true);
}
publishProgress(new TaskData(newCard, oldCardLeech));
ankiDB.getDatabase().setTransactionSuccessful();
} finally {
ankiDB.getDatabase().endTransaction();
}
} catch (RuntimeException e) {
Log.e(AnkiDroidApp.TAG, "doInBackgroundAnswerCard - RuntimeException on answering card: " + e);
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundAnswerCard");
return new TaskData(false);
}
return new TaskData(true);
}
Aggregations