Search in sources :

Example 51 with JSONObject

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

the class Decks method update.

/**
 * Add or update an existing deck. Used for syncing and merging.
 */
public void update(Deck g) {
    long id = g.getLong("id");
    JSONObject oldDeck = get(id, false);
    if (oldDeck != null) {
        // In case where another update got the name
        // `oldName`, it would be a mistake to remove it from nameMap
        mNameMap.remove(oldDeck.getString("name"), oldDeck);
    }
    mNameMap.add(g);
    mDecks.put(g.getLong("id"), g);
    maybeAddToActive();
    // mark registry changed, but don't bump mod time
    save();
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 52 with JSONObject

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

the class Decks method flush.

public void flush() {
    ContentValues values = new ContentValues();
    if (mChanged) {
        JSONObject decksarray = new JSONObject();
        for (Map.Entry<Long, Deck> d : mDecks.entrySet()) {
            decksarray.put(Long.toString(d.getKey()), d.getValue());
        }
        values.put("decks", Utils.jsonToString(decksarray));
        JSONObject confarray = new JSONObject();
        for (Map.Entry<Long, DeckConfig> d : mDconf.entrySet()) {
            confarray.put(Long.toString(d.getKey()), d.getValue());
        }
        values.put("dconf", Utils.jsonToString(confarray));
        mCol.getDb().update("col", values);
        mChanged = false;
    }
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 53 with JSONObject

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

the class AnkiChinaSyncer method uploadLocalMediaFileInfo.

/**
 * 上传媒体文件信息给服务端,服务端比对完后下发文件的同步结果
 */
private void uploadLocalMediaFileInfo(String token) {
    // 获取本地media文件夹的目录
    updateDialogProgress(SYNCING_MEDIA, "", 1);
    File mediaDir = new File(Media.getCollectionMediaPath(CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance()).getPath()));
    // if (!mediaDir.exists()) {
    // return;
    // }
    File[] files = mediaDir.listFiles();
    // if (files.length == 0) {
    // return;
    // }
    String[] localFiles = new String[files.length];
    for (int i = 0; i < files.length; i++) {
        localFiles[i] = files[i].getName();
    }
    JSONArray filesJson = new JSONArray(localFiles);
    // Timber.i("local file list:%s", filesJson);
    RequestBody formBody = new FormBody.Builder().add("file_list", filesJson.toString()).build();
    updateDialogProgress(SYNCING_MEDIA, "", 5);
    if (mCancel) {
        return;
    }
    OKHttpUtil.post(Consts.ANKI_CHINA_BASE + Consts.API_VERSION + "napi/sync/postFileInfo", formBody, token, "", new OKHttpUtil.MyCallBack() {

        @Override
        public void onFailure(Call call, IOException e) {
            updateDialogMessage(SYNCING_ERROR, ERROR_NETWORK);
        }

        @Override
        public void onResponse(Call call, String token, Object arg1, Response response) throws IOException {
            if (response.isSuccessful()) {
                Timber.e("upload media file info succeed!");
                try {
                    final JSONObject object = new JSONObject(response.body().string());
                    Timber.e("fetch media sync info from server:%s", object.toString());
                    if (object.getInt("status_code") != 0) {
                        updateDialogMessage(SYNCING_ERROR, object.getString("message"));
                        return;
                    }
                    final JSONObject item = object.getJSONObject("data");
                    updateDialogProgress(SYNCING_MEDIA, "", 10);
                    handleMediaSync(item, token);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Timber.e("upload media file info error, code %d", response.code());
            }
        }
    });
}
Also used : Call(okhttp3.Call) OKHttpUtil(com.ichi2.utils.OKHttpUtil) JSONArray(com.ichi2.utils.JSONArray) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) FileNotFoundException(java.io.FileNotFoundException) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) IOException(java.io.IOException) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) JSONObject(com.ichi2.utils.JSONObject) ZipFile(java.util.zip.ZipFile) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 54 with JSONObject

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

the class SchedV2 method _checkLeech.

/**
 * Leeches ****************************************************************** *****************************
 */
/**
 * Leech handler. True if card was a leech.
 *        Overridden: in V1, due and did are changed
 */
protected boolean _checkLeech(@NonNull Card card, @NonNull JSONObject conf) {
    int lf;
    lf = conf.getInt("leechFails");
    if (lf == 0) {
        return false;
    }
    // if over threshold or every half threshold reps after that
    if (card.getLapses() >= lf && (card.getLapses() - lf) % Math.max(lf / 2, 1) == 0) {
        // add a leech tag
        Note n = card.note();
        n.addTag("leech");
        n.flush();
        // handle
        if (conf.getInt("leechAction") == Consts.LEECH_SUSPEND) {
            card.setQueue(Consts.QUEUE_TYPE_SUSPENDED);
        }
        // notify UI
        if (mContextReference != null) {
            Activity context = mContextReference.get();
            leech(card, context);
        }
        return true;
    }
    return false;
}
Also used : Note(com.ichi2.libanki.Note) Activity(android.app.Activity)

Example 55 with JSONObject

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

the class SchedV2 method _burySiblings.

/**
 * Sibling spacing
 * ********************
 */
protected void _burySiblings(@NonNull Card card) {
    ArrayList<Long> toBury = new ArrayList<>();
    JSONObject nconf = _newConf(card);
    boolean buryNew = nconf.optBoolean("bury", true);
    JSONObject rconf = _revConf(card);
    boolean buryRev = rconf.optBoolean("bury", true);
    // loop through and remove from queues
    Cursor cur = null;
    try {
        cur = mCol.getDb().query("select id, queue from cards where nid=? and id!=? " + "and (queue=" + Consts.QUEUE_TYPE_NEW + " or (queue=" + Consts.QUEUE_TYPE_REV + " and due<=?))", card.getNid(), card.getId(), mToday);
        while (cur.moveToNext()) {
            long cid = cur.getLong(0);
            int queue = cur.getInt(1);
            SimpleCardQueue queue_object;
            if (queue == Consts.QUEUE_TYPE_REV) {
                queue_object = mRevQueue;
                if (buryRev) {
                    toBury.add(cid);
                }
            } else {
                queue_object = mNewQueue;
                if (buryNew) {
                    toBury.add(cid);
                }
            }
            // even if burying disabled, we still discard to give
            // same-day spacing
            queue_object.remove(cid);
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    // then bury
    if (!toBury.isEmpty()) {
        buryCards(Utils.collection2Array(toBury), false);
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)272 JSONArray (com.ichi2.utils.JSONArray)110 ArrayList (java.util.ArrayList)59 Test (org.junit.Test)55 Collection (com.ichi2.libanki.Collection)46 IOException (java.io.IOException)40 RobolectricTest (com.ichi2.anki.RobolectricTest)34 JSONException (com.ichi2.utils.JSONException)34 Note (com.ichi2.libanki.Note)33 Model (com.ichi2.libanki.Model)31 HashMap (java.util.HashMap)31 SuppressLint (android.annotation.SuppressLint)29 JSONObject (org.json.JSONObject)27 Response (okhttp3.Response)25 JSONException (org.json.JSONException)24 NonNull (androidx.annotation.NonNull)23 Card (com.ichi2.libanki.Card)21 File (java.io.File)21 Map (java.util.Map)20 Intent (android.content.Intent)19