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"));
}
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;
}
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;
}
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;
}
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());
}
}
});
}
Aggregations