Search in sources :

Example 41 with JSONArray

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

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=" + mMaxUsn + " WHERE usn=-1");
            }
        }
        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)

Example 42 with JSONArray

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

the class Syncer method getDecks.

/**
 * Decks ********************************************************************
 */
private JSONArray getDecks() {
    JSONArray result = new JSONArray();
    if (mCol.getServer()) {
        JSONArray decks = new JSONArray();
        for (Deck g : mCol.getDecks().all()) {
            if (g.getInt("usn") >= mMinUsn) {
                decks.put(g);
            }
        }
        JSONArray dconfs = new JSONArray();
        for (DeckConfig g : mCol.getDecks().allConf()) {
            if (g.getInt("usn") >= mMinUsn) {
                dconfs.put(g);
            }
        }
        result.put(decks);
        result.put(dconfs);
    } else {
        JSONArray decks = new JSONArray();
        for (Deck g : mCol.getDecks().all()) {
            if (g.getInt("usn") == -1) {
                g.put("usn", mMaxUsn);
                decks.put(g);
            }
        }
        JSONArray dconfs = new JSONArray();
        for (DeckConfig g : mCol.getDecks().allConf()) {
            if (g.getInt("usn") == -1) {
                g.put("usn", mMaxUsn);
                dconfs.put(g);
            }
        }
        mCol.getDecks().save();
        result.put(decks);
        result.put(dconfs);
    }
    return result;
}
Also used : JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 43 with JSONArray

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

the class AnkiChinaSyncer method getChangedColJson.

private <T extends JSONObject> JSONObject getChangedColJson(int type, List<T> newModels) {
    List<T> changedModel = new ArrayList<>();
    boolean deleted;
    Cursor cur = null;
    Map<Long, Long> oldModel = new HashMap<>();
    JSONObject modelsJson = new JSONObject();
    try {
        cur = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance()).getDb().getDatabase().query("SELECT id,mod FROM synclog WHERE type = " + type, null);
        while (cur.moveToNext()) {
            oldModel.put(cur.getLong(0), cur.getLong(1));
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    if (oldModel.isEmpty()) {
        changedModel.addAll(newModels);
    } else {
        StringBuilder deletedModelSb = new StringBuilder();
        for (Map.Entry<Long, Long> entry : oldModel.entrySet()) {
            // System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            deleted = true;
            for (T model : newModels) {
                if (!changedModel.contains(model) && (oldModel.get(model.getLong("id")) == null || oldModel.get(model.getLong("id")) < model.getLong("mod"))) {
                    // 新增或修改过的deck
                    changedModel.add(model);
                } else if (model.getLong("id") == entry.getKey()) {
                    // 未被删除的deck
                    deleted = false;
                }
            }
            if (deleted) {
                if (deletedModelSb.length() != 0) {
                    deletedModelSb.append(",");
                }
                deletedModelSb.append(entry.getKey());
            }
        }
        if (deletedModelSb.length() > 0) {
            modelsJson.put("delete", new JSONArray(deletedModelSb.toString().split(",")));
        // modelsJson.put("delete", strArray2jsonArray(deletedModelSb.toString().split(",")));
        }
    }
    JSONArray changedModelJson = new JSONArray();
    for (T item : changedModel) {
        changedModelJson.put(item);
    }
    modelsJson.put("replace", changedModelJson);
    return modelsJson;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) Cursor(android.database.Cursor) JSONObject(com.ichi2.utils.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 44 with JSONArray

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

the class StepsPreference method convertToJSON.

/**
 * Convert steps format. For better usability, rounded floats are converted to integers (e.g., 1.0 is converted to
 * 1).
 *
 * @param steps String representation of steps.
 * @return The steps as a JSONArray or null if the steps are not valid.
 */
public static JSONArray convertToJSON(String steps) {
    JSONArray stepsAr = new JSONArray();
    steps = steps.trim();
    if (TextUtils.isEmpty(steps)) {
        return stepsAr;
    }
    try {
        for (String s : steps.split("\\s+")) {
            float f = Float.parseFloat(s);
            // 0 or less is not a valid step.
            if (f <= 0) {
                return null;
            }
            // Use whole numbers if we can (but still allow decimals)
            int i = (int) f;
            if (i == f) {
                stepsAr.put(i);
            } else {
                stepsAr.put(f);
            }
        }
    } catch (NumberFormatException e) {
        return null;
    } catch (JSONException e) {
        // Can't serialize float. Value likely too big/small.
        return null;
    }
    return stepsAr;
}
Also used : JSONArray(com.ichi2.utils.JSONArray) JSONException(com.ichi2.utils.JSONException)

Example 45 with JSONArray

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

the class AbstractSchedTest method ensureUndoCorrectCounts.

@Test
public void ensureUndoCorrectCounts() {
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    Deck deck = col.getDecks().get(1);
    DeckConfig dconf = col.getDecks().getConf(1);
    dconf.getJSONObject("new").put("perDay", 10);
    JSONArray newCount = deck.getJSONArray("newToday");
    for (int i = 0; i < 20; i++) {
        Note note = col.newNote();
        note.setField(0, "a");
        col.addNote(note);
    }
    col.reset();
    assertThat(col.cardCount(), is(20));
    assertThat(sched.newCount(), is(10));
    Card card = sched.getCard();
    assertThat(sched.newCount(), is(9));
    assertThat(sched.counts(card)[0], is(10));
    sched.answerCard(card, sched.getGoodNewButton());
    sched.getCard();
    nonTaskUndo(col);
    card.load();
    assertThat(sched.newCount(), is(9));
    assertThat(sched.counts(card)[0], is(10));
}
Also used : Note(com.ichi2.libanki.Note) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

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