Search in sources :

Example 36 with JSONArray

use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.

the class RemoteMediaServer method uploadChanges.

public JSONArray uploadChanges(File zip) throws UnknownHttpResponseException, MediaSyncException {
    try {
        // no compression, as we compress the zip file instead
        Response resp = super.req("uploadChanges", new FileInputStream(zip), 0);
        JSONObject jresp = new JSONObject(resp.body().string());
        return _dataOnly(jresp, JSONArray.class);
    } catch (IOException | NullPointerException e) {
        throw new RuntimeException(e);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 37 with JSONArray

use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.

the class Syncer method mergeModels.

private void mergeModels(JSONArray rchg) throws UnexpectedSchemaChange {
    for (int i = 0; i < rchg.length(); i++) {
        Model r = new Model(rchg.getJSONObject(i));
        Model l = mCol.getModels().get(r.getLong("id"));
        // if missing locally or server is newer, update
        if (l == null || r.getLong("mod") > l.getLong("mod")) {
            // syncing algorithm should handle this in a better way.
            if (l != null) {
                if (l.getJSONArray("flds").length() != r.getJSONArray("flds").length()) {
                    throw new UnexpectedSchemaChange();
                }
                if (l.getJSONArray("tmpls").length() != r.getJSONArray("tmpls").length()) {
                    throw new UnexpectedSchemaChange();
                }
            }
            mCol.getModels().update(r);
        }
    }
}
Also used : Model(com.ichi2.libanki.Model)

Example 38 with JSONArray

use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.

the class Syncer method newerRows.

private ArrayList<Object[]> newerRows(JSONArray data, String table, int modIdx) {
    long[] ids = new long[data.length()];
    for (int i = 0; i < data.length(); i++) {
        ids[i] = data.getJSONArray(i).getLong(0);
    }
    HashMap<Long, Long> lmods = new HashMap<>();
    Cursor cur = null;
    try {
        cur = mCol.getDb().getDatabase().query("SELECT id, mod FROM " + table + " WHERE id IN " + Utils.ids2str(ids) + " AND " + usnLim(), null);
        while (cur.moveToNext()) {
            lmods.put(cur.getLong(0), cur.getLong(1));
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    ArrayList<Object[]> update = new ArrayList<>();
    for (int i = 0; i < data.length(); i++) {
        JSONArray r = data.getJSONArray(i);
        if (!lmods.containsKey(r.getLong(0)) || lmods.get(r.getLong(0)) < r.getLong(modIdx)) {
            update.add(Utils.jsonArray2Objects(r));
        }
    }
    mCol.log(table, data);
    return update;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) Cursor(android.database.Cursor)

Example 39 with JSONArray

use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.

the class Syncer method getModels.

/**
 * Models ********************************************************************
 */
private JSONArray getModels() {
    JSONArray result = new JSONArray();
    if (mCol.getServer()) {
        for (JSONObject m : mCol.getModels().all()) {
            if (m.getInt("usn") >= mMinUsn) {
                result.put(m);
            }
        }
    } else {
        for (JSONObject m : mCol.getModels().all()) {
            if (m.getInt("usn") == -1) {
                m.put("usn", mMaxUsn);
                result.put(m);
            }
        }
        mCol.getModels().save();
    }
    return result;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 40 with JSONArray

use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.

the class Syncer method mergeDecks.

private void mergeDecks(JSONArray rchg) {
    JSONArray decks = rchg.getJSONArray(0);
    for (int i = 0; i < decks.length(); i++) {
        Deck r = new Deck(decks.getJSONObject(i));
        Deck l = mCol.getDecks().get(r.getLong("id"), false);
        // if missing locally or server is newer, update
        if (l == null || r.getLong("mod") > l.getLong("mod")) {
            mCol.getDecks().update(r);
        }
    }
    JSONArray confs = rchg.getJSONArray(1);
    for (int i = 0; i < confs.length(); i++) {
        DeckConfig r = new DeckConfig(confs.getJSONObject(i));
        DeckConfig l = mCol.getDecks().getConf(r.getLong("id"));
        // if missing locally or server is newer, update
        if (l == null || r.getLong("mod") > l.getLong("mod")) {
            mCol.getDecks().updateConf(r);
        }
    }
}
Also used : JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig)

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