Search in sources :

Example 26 with JSONArray

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

the class Models method moveTemplate.

public void moveTemplate(Model m, JSONObject template, int idx) {
    JSONArray tmpls = m.getJSONArray("tmpls");
    int oldidx = -1;
    ArrayList<JSONObject> l = new ArrayList<>();
    HashMap<Integer, Integer> oldidxs = new HashMap<>();
    for (int i = 0; i < tmpls.length(); ++i) {
        if (tmpls.getJSONObject(i).equals(template)) {
            oldidx = i;
            if (idx == oldidx) {
                return;
            }
        }
        JSONObject t = tmpls.getJSONObject(i);
        oldidxs.put(t.hashCode(), t.getInt("ord"));
        l.add(t);
    }
    l.remove(oldidx);
    l.add(idx, template);
    m.put("tmpls", new JSONArray(l));
    _updateTemplOrds(m);
    // generate change map - We use StringBuilder
    StringBuilder sb = new StringBuilder();
    tmpls = m.getJSONArray("tmpls");
    for (int i = 0; i < tmpls.length(); ++i) {
        JSONObject t = tmpls.getJSONObject(i);
        sb.append("when ord = ").append(oldidxs.get(t.hashCode())).append(" then ").append(t.getInt("ord"));
        if (i != tmpls.length() - 1) {
            sb.append(" ");
        }
    }
    // apply
    save(m);
    mCol.getDb().execute("update cards set ord = (case " + sb.toString() + " end),usn=?,mod=? where nid in (select id from notes where mid = ?)", mCol.usn(), mCol.getTime().intTime(), m.getLong("id"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) JSONArray(com.ichi2.utils.JSONArray) ArrayList(java.util.ArrayList)

Example 27 with JSONArray

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

the class Models method getTemplateNames.

public HashMap<Long, HashMap<Integer, String>> getTemplateNames() {
    HashMap<Long, HashMap<Integer, String>> result = new HashMap<>();
    for (Model m : mModels.values()) {
        JSONArray templates;
        templates = m.getJSONArray("tmpls");
        HashMap<Integer, String> names = new HashMap<>();
        for (int i = 0; i < templates.length(); i++) {
            JSONObject t = templates.getJSONObject(i);
            names.put(t.getInt("ord"), t.getString("name"));
        }
        result.put(m.getLong("id"), names);
    }
    return result;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) JSONArray(com.ichi2.utils.JSONArray)

Example 28 with JSONArray

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

the class Decks method load.

public void load(String decks, String dconf) {
    mDecks = new HashMap<>();
    mDconf = new HashMap<>();
    JSONObject decksarray = new JSONObject(decks);
    JSONArray ids = decksarray.names();
    for (int i = 0; i < ids.length(); i++) {
        String id = ids.getString(i);
        try {
            long longId = Long.parseLong(id);
            Deck o = new Deck(decksarray.getJSONObject(id));
            mDecks.put(longId, o);
        } catch (NumberFormatException e) {
            Timber.w("id is not valid");
        }
    }
    mNameMap = NameMap.constructor(mDecks.values());
    JSONObject confarray = new JSONObject(dconf);
    ids = confarray.names();
    for (int i = 0; ids != null && i < ids.length(); i++) {
        String id = ids.getString(i);
        try {
            mDconf.put(Long.parseLong(id), new DeckConfig(confarray.getJSONObject(id)));
        } catch (NumberFormatException e) {
            Timber.w("id is not valid");
        }
    }
    mChanged = false;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 29 with JSONArray

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

the class Decks method active.

/**
 * Deck selection
 * ***********************************************************
 */
/**
 * The currently active dids. Make sure to copy before modifying.
 */
public LinkedList<Long> active() {
    JSONArray activeDecks = mCol.getConf().getJSONArray("activeDecks");
    LinkedList<Long> result = new LinkedList<>();
    for (int i = 0; i < activeDecks.length(); i++) {
        result.add(activeDecks.getLong(i));
    }
    return result;
}
Also used : JSONArray(com.ichi2.utils.JSONArray) LinkedList(java.util.LinkedList)

Example 30 with JSONArray

use of com.ichi2.utils.JSONArray 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)

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