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