Search in sources :

Example 6 with JSONException

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);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JSONException(com.ichi2.utils.JSONException) IOException(java.io.IOException) AnkiPackageExporter(com.ichi2.libanki.AnkiPackageExporter) ImportExportException(com.ichi2.anki.exception.ImportExportException) Collection(com.ichi2.libanki.Collection) JSONObject(com.ichi2.utils.JSONObject)

Example 7 with JSONException

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);
    }
}
Also used : Collection(com.ichi2.libanki.Collection) Deck(com.ichi2.libanki.Deck) JSONException(com.ichi2.utils.JSONException) JSONObject(com.ichi2.utils.JSONObject) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 8 with JSONException

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;
}
Also used : MultimediaEditableNote(com.ichi2.anki.multimediacard.impl.MultimediaEditableNote) IMultimediaEditableNote(com.ichi2.anki.multimediacard.IMultimediaEditableNote) JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) TextField(com.ichi2.anki.multimediacard.fields.TextField) JSONException(com.ichi2.utils.JSONException)

Example 9 with JSONException

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;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONException(com.ichi2.utils.JSONException) File(java.io.File)

Example 10 with JSONException

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;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException) File(java.io.File)

Aggregations

JSONException (com.ichi2.utils.JSONException)54 JSONObject (com.ichi2.utils.JSONObject)41 JSONException (org.json.JSONException)28 Collection (com.ichi2.libanki.Collection)25 ArrayList (java.util.ArrayList)25 JSONObject (org.json.JSONObject)22 JSONArray (com.ichi2.utils.JSONArray)21 SuppressLint (android.annotation.SuppressLint)18 IOException (java.io.IOException)17 File (java.io.File)16 Note (com.ichi2.libanki.Note)14 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)12 HashMap (java.util.HashMap)12 Bundle (android.os.Bundle)11 Deck (com.ichi2.libanki.Deck)11 Resources (android.content.res.Resources)10 Model (com.ichi2.libanki.Model)10 Map (java.util.Map)10 List (java.util.List)9 Context (android.content.Context)8