use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class CollectionTask method doInBackgroundExportApkg.
private TaskData doInBackgroundExportApkg(TaskData param) {
Timber.d("doInBackgroundExportApkg");
Object[] data = param.getObjArray();
Collection col = (Collection) data[0];
String apkgPath = (String) data[1];
Long did = (Long) data[2];
boolean includeSched = (Boolean) data[3];
boolean includeMedia = (Boolean) data[4];
boolean exportApkg = (Boolean) data[5];
boolean exportCard = (Boolean) data[6];
try {
AnkiPackageExporter exporter = new AnkiPackageExporter(col);
exporter.setIncludeSched(includeSched);
exporter.setIncludeMedia(includeMedia);
exporter.setExportCard(exportCard);
exporter.setExportApkg(exportApkg);
exporter.setDid(did);
exporter.exportInto(apkgPath, mContext);
} catch (FileNotFoundException e) {
Timber.e(e, "FileNotFoundException in doInBackgroundExportApkg");
return new TaskData(false);
} catch (IOException e) {
Timber.e(e, "IOException in doInBackgroundExportApkg");
return new TaskData(false);
} catch (JSONException e) {
Timber.e(e, "JSOnException in doInBackgroundExportApkg");
return new TaskData(false);
} catch (ImportExportException e) {
Timber.e(e, "ImportExportException in doInBackgroundExportApkg");
return new TaskData(e.getMessage(), true);
}
return new TaskData(exportCard ? apkgPath.replace(".apkg", ".card") : apkgPath);
}
use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class CollectionTask method doInBackgroundConfChange.
private TaskData doInBackgroundConfChange(TaskData param) {
Timber.d("doInBackgroundConfChange");
Collection col = getCol();
Object[] data = param.getObjArray();
Deck deck = (Deck) data[0];
DeckConfig conf = (DeckConfig) data[1];
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);
col.save();
return new TaskData(true);
} catch (JSONException e) {
return new TaskData(false);
}
}
use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class NoteService method createEmptyNote.
/**
* Creates an empty Note from given Model
*
* @param model the model in JSOBObject format
* @return a new note instance
*/
public static MultimediaEditableNote createEmptyNote(JSONObject model) {
try {
JSONArray fieldsArray = model.getJSONArray("flds");
int numOfFields = fieldsArray.length();
if (numOfFields > 0) {
MultimediaEditableNote note = new MultimediaEditableNote();
note.setNumFields(numOfFields);
for (int i = 0; i < numOfFields; i++) {
JSONObject fieldObject = fieldsArray.getJSONObject(i);
TextField uiTextField = new TextField();
uiTextField.setName(fieldObject.getString("name"));
uiTextField.setText(fieldObject.getString("name"));
note.setField(i, uiTextField);
}
note.setModelId(model.getLong("id"));
return note;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class ZipFile method _exportMedia.
private JSONObject _exportMedia(ZipFile z, File[] files, ValidateFiles validateFiles) throws IOException {
int c = 0;
JSONObject media = new JSONObject();
for (File file : files) {
// todo: deflate SVG files, as in dae/anki@a5b0852360b132c0d04094f5ca8f1933f64d7c7e
if (validateFiles == ValidateFiles.VALIDATE && !file.exists()) {
// Anki 2.1.30 does the same
Timber.d("Skipping missing file %s", file);
continue;
}
z.write(file.getPath(), Integer.toString(c));
try {
media.put(Integer.toString(c), file.getName());
c++;
} catch (JSONException e) {
e.printStackTrace();
}
}
return media;
}
use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class ZipFile method exportFiltered.
private JSONObject exportFiltered(ZipFile z, String path, Context context) throws IOException, JSONException, ImportExportException {
// export into the anki2 file
String colfile = path.replace(".apkg", ".anki2");
super.exportInto(colfile, context);
z.write(colfile, CollectionHelper.COLLECTION_FILENAME);
// and media
prepareMedia();
JSONObject media = _exportMedia(z, mMediaFiles, mCol.getMedia().dir());
// tidy up intermediate files
SQLiteDatabase.deleteDatabase(new File(colfile));
SQLiteDatabase.deleteDatabase(new File(path.replace(".apkg", ".media.ad.db2")));
String tempPath = path.replace(".apkg", ".media");
File file = new File(tempPath);
if (file.exists()) {
String deleteCmd = "rm -r " + tempPath;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) {
}
}
return media;
}
Aggregations