use of com.ichi2.async.TaskData in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundConfChange.
private TaskData doInBackgroundConfChange(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundConfChange");
Object[] data = params[0].getObjArray();
Collection col = (Collection) data[0];
JSONObject deck = (JSONObject) data[1];
JSONObject conf = (JSONObject) data[2];
try {
long newConfId = conf.getLong("id");
// If new config has a different sorting order, reorder the cards
int oldOrder = col.getDecks().getConf(deck.getLong("conf")).getJSONObject("new").getInt("order");
int newOrder = col.getDecks().getConf(newConfId).getJSONObject("new").getInt("order");
if (oldOrder != newOrder) {
switch(newOrder) {
case 0:
col.getSched().randomizeCards(deck.getLong("id"));
break;
case 1:
col.getSched().orderCards(deck.getLong("id"));
break;
}
}
col.getDecks().setConf(deck, newConfId);
return new TaskData(true);
} catch (JSONException e) {
return new TaskData(false);
}
}
use of com.ichi2.async.TaskData in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundCheckDatabase.
private TaskData doInBackgroundCheckDatabase(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundCheckDatabase");
Collection col = params[0].getCollection();
long result = col.fixIntegrity();
if (result == -1) {
return new TaskData(false);
} else {
return new TaskData(0, result, true);
}
}
use of com.ichi2.async.TaskData in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundReorder.
private TaskData doInBackgroundReorder(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundReorder");
Object[] data = params[0].getObjArray();
Collection col = (Collection) data[0];
JSONObject conf = (JSONObject) data[1];
col.getSched().resortConf(conf);
return new TaskData(true);
}
use of com.ichi2.async.TaskData in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundCloseCollection.
private TaskData doInBackgroundCloseCollection(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundCloseCollection");
Collection col = params[0].getCollection();
if (col != null) {
try {
WidgetStatus.waitToFinish();
String path = col.getPath();
AnkiDroidApp.closeCollection(true);
BackupManager.performBackup(path);
} catch (RuntimeException e) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundCloseCollection: error occurred - collection not properly closed");
}
}
return null;
}
use of com.ichi2.async.TaskData in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundExportApkg.
private TaskData doInBackgroundExportApkg(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundExportApkg");
Object[] data = params[0].getObjArray();
String colPath = (String) data[0];
String apkgPath = (String) data[1];
boolean includeMedia = (Boolean) data[2];
byte[] buf = new byte[1024];
try {
try {
AnkiDb d = AnkiDatabaseManager.getDatabase(colPath);
} catch (SQLiteDatabaseCorruptException e) {
// collection is invalid
return new TaskData(false);
} finally {
AnkiDatabaseManager.closeDatabase(colPath);
}
// export collection
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(apkgPath));
FileInputStream colFin = new FileInputStream(colPath);
ZipEntry ze = new ZipEntry("collection.anki2");
zos.putNextEntry(ze);
int len;
while ((len = colFin.read(buf)) >= 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
colFin.close();
// export media
JSONObject media = new JSONObject();
if (includeMedia) {
File mediaDir = new File(AnkiDroidApp.getCurrentAnkiDroidMediaDir());
if (mediaDir.exists() && mediaDir.isDirectory()) {
File[] mediaFiles = mediaDir.listFiles();
int c = 0;
for (File f : mediaFiles) {
FileInputStream mediaFin = new FileInputStream(f);
ze = new ZipEntry(Integer.toString(c));
zos.putNextEntry(ze);
while ((len = mediaFin.read(buf)) >= 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
media.put(Integer.toString(c), f.getName());
}
}
}
ze = new ZipEntry("media");
zos.putNextEntry(ze);
InputStream mediaIn = new ByteArrayInputStream(Utils.jsonToString(media).getBytes("UTF-8"));
while ((len = mediaIn.read(buf)) >= 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
zos.close();
} catch (FileNotFoundException e) {
return new TaskData(false);
} catch (IOException e) {
return new TaskData(false);
} catch (JSONException e) {
return new TaskData(false);
}
return new TaskData(true);
}
Aggregations