Search in sources :

Example 81 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class Models method scmhash.

/*
      Schema hash ***********************************************************************************************
     */
@NonNull
@Override
public String scmhash(Model m) {
    StringBuilder s = new StringBuilder();
    JSONArray flds = m.getJSONArray("flds");
    for (JSONObject fld : flds.jsonObjectIterable()) {
        s.append(fld.getString("name"));
    }
    JSONArray tmpls = m.getJSONArray("tmpls");
    for (JSONObject t : tmpls.jsonObjectIterable()) {
        s.append(t.getString("name"));
    }
    return Utils.checksum(s.toString());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) NonNull(androidx.annotation.NonNull)

Example 82 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class Storage method _upgradeClozeModel.

private static void _upgradeClozeModel(Collection col, Model m) throws ConfirmModSchemaException {
    m.put("type", Consts.MODEL_CLOZE);
    // convert first template
    JSONObject t = m.getJSONArray("tmpls").getJSONObject(0);
    for (String type : new String[] { "qfmt", "afmt" }) {
        // noinspection RegExpRedundantEscape            // In Android, } should be escaped
        t.put(type, t.getString(type).replaceAll("\\{\\{cloze:1:(.+?)\\}\\}", "{{cloze:$1}}"));
    }
    t.put("name", "Cloze");
    // delete non-cloze cards for the model
    JSONArray tmpls = m.getJSONArray("tmpls");
    ArrayList<JSONObject> rem = new ArrayList<>();
    for (JSONObject ta : tmpls.jsonObjectIterable()) {
        if (!ta.getString("afmt").contains("{{cloze:")) {
            rem.add(ta);
        }
    }
    for (JSONObject r : rem) {
        col.getModels().remTemplate(m, r);
    }
    JSONArray newTmpls = new JSONArray();
    newTmpls.put(tmpls.getJSONObject(0));
    m.put("tmpls", newTmpls);
    Models._updateTemplOrds(m);
    col.getModels().save(m);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) ArrayList(java.util.ArrayList)

Example 83 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class RemoteMediaServer method downloadFiles.

/**
 * args: files
 * <br>
 * This method returns a ZipFile with the OPEN_DELETE flag, ensuring that the file on disk will
 * be automatically deleted when the stream is closed.
 */
public ZipFile downloadFiles(List<String> top) throws UnknownHttpResponseException {
    Response resp = null;
    try {
        resp = super.req("downloadFiles", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("files", new JSONArray(top)))));
        String zipPath = mCol.getPath().replaceFirst("collection\\.anki2$", "tmpSyncFromServer.zip");
        // retrieve contents and save to file on disk:
        super.writeToFile(resp.body().byteStream(), zipPath);
        return new ZipFile(new File(zipPath), ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    } catch (IOException | NullPointerException e) {
        Timber.e(e, "Failed to download requested media files");
        throw new RuntimeException(e);
    } finally {
        if (resp != null && resp.body() != null) {
            resp.body().close();
        }
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) ZipFile(java.util.zip.ZipFile) JSONArray(com.ichi2.utils.JSONArray) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 84 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class RemoteMediaServer method mediaChanges.

// args: lastUsn
public JSONArray mediaChanges(int lastUsn) throws UnknownHttpResponseException, MediaSyncException {
    try {
        mPostVars = HashUtil.HashMapInit(1);
        mPostVars.put("sk", mSKey);
        Response resp = super.req("mediaChanges", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("lastUsn", lastUsn))));
        JSONObject jresp = new JSONObject(resp.body().string());
        return _dataOnly(jresp, JSONArray.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException)

Example 85 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class Syncer method chunk.

public JSONObject chunk() {
    JSONObject buf = new JSONObject();
    buf.put("done", false);
    int lim = 250;
    List<Integer> colTypes = null;
    while (!mTablesLeft.isEmpty() && lim > 0) {
        String curTable = mTablesLeft.getFirst();
        if (mCursor == null) {
            mCursor = cursorForTable(curTable);
        }
        colTypes = columnTypesForQuery(curTable);
        JSONArray rows = new JSONArray();
        int count = mCursor.getColumnCount();
        int fetched = 0;
        while (mCursor.moveToNext()) {
            JSONArray r = new JSONArray();
            for (int i = 0; i < count; i++) {
                switch(colTypes.get(i)) {
                    case TYPE_STRING:
                        r.put(mCursor.getString(i));
                        break;
                    case TYPE_FLOAT:
                        r.put(mCursor.getDouble(i));
                        break;
                    case TYPE_INTEGER:
                        r.put(mCursor.getLong(i));
                        break;
                }
            }
            rows.put(r);
            if (++fetched == lim) {
                break;
            }
        }
        if (fetched != lim) {
            // table is empty
            mTablesLeft.removeFirst();
            mCursor.close();
            mCursor = null;
            // if we're the client, mark the objects as having been sent
            if (!mCol.getServer()) {
                mCol.getDb().execute("UPDATE " + curTable + " SET usn=? WHERE usn=-1", mMaxUsn);
            }
        }
        buf.put(curTable, rows);
        lim -= fetched;
    }
    if (mTablesLeft.isEmpty()) {
        buf.put("done", true);
    }
    return buf;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Aggregations

JSONArray (com.ichi2.utils.JSONArray)188 JSONObject (com.ichi2.utils.JSONObject)97 Collection (com.ichi2.libanki.Collection)54 Test (org.junit.Test)44 ArrayList (java.util.ArrayList)42 Note (com.ichi2.libanki.Note)40 Card (com.ichi2.libanki.Card)39 DeckConfig (com.ichi2.libanki.DeckConfig)37 RobolectricTest (com.ichi2.anki.RobolectricTest)36 Deck (com.ichi2.libanki.Deck)19 Model (com.ichi2.libanki.Model)19 JSONException (com.ichi2.utils.JSONException)19 Cursor (android.database.Cursor)17 HashMap (java.util.HashMap)16 SuppressLint (android.annotation.SuppressLint)15 IOException (java.io.IOException)14 NonNull (androidx.annotation.NonNull)13 File (java.io.File)10 Response (okhttp3.Response)10 JSONArray (org.json.JSONArray)8