use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.
the class Models method scmhash.
/*
Schema hash ***********************************************************************************************
*/
@NonNull
@Override
public String scmhash(Model m) {
StringBuilder s = new StringBuilder();
JSONArray flds = m.getJSONArray("flds");
for (JSONObject fld : flds.jsonObjectIterable()) {
s.append(fld.getString("name"));
}
JSONArray tmpls = m.getJSONArray("tmpls");
for (JSONObject t : tmpls.jsonObjectIterable()) {
s.append(t.getString("name"));
}
return Utils.checksum(s.toString());
}
use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.
the class Storage method _upgradeClozeModel.
private static void _upgradeClozeModel(Collection col, Model m) throws ConfirmModSchemaException {
m.put("type", Consts.MODEL_CLOZE);
// convert first template
JSONObject t = m.getJSONArray("tmpls").getJSONObject(0);
for (String type : new String[] { "qfmt", "afmt" }) {
// noinspection RegExpRedundantEscape // In Android, } should be escaped
t.put(type, t.getString(type).replaceAll("\\{\\{cloze:1:(.+?)\\}\\}", "{{cloze:$1}}"));
}
t.put("name", "Cloze");
// delete non-cloze cards for the model
JSONArray tmpls = m.getJSONArray("tmpls");
ArrayList<JSONObject> rem = new ArrayList<>();
for (JSONObject ta : tmpls.jsonObjectIterable()) {
if (!ta.getString("afmt").contains("{{cloze:")) {
rem.add(ta);
}
}
for (JSONObject r : rem) {
col.getModels().remTemplate(m, r);
}
JSONArray newTmpls = new JSONArray();
newTmpls.put(tmpls.getJSONObject(0));
m.put("tmpls", newTmpls);
Models._updateTemplOrds(m);
col.getModels().save(m);
}
use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.
the class RemoteMediaServer method downloadFiles.
/**
* args: files
* <br>
* This method returns a ZipFile with the OPEN_DELETE flag, ensuring that the file on disk will
* be automatically deleted when the stream is closed.
*/
public ZipFile downloadFiles(List<String> top) throws UnknownHttpResponseException {
Response resp = null;
try {
resp = super.req("downloadFiles", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("files", new JSONArray(top)))));
String zipPath = mCol.getPath().replaceFirst("collection\\.anki2$", "tmpSyncFromServer.zip");
// retrieve contents and save to file on disk:
super.writeToFile(resp.body().byteStream(), zipPath);
return new ZipFile(new File(zipPath), ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
} catch (IOException | NullPointerException e) {
Timber.e(e, "Failed to download requested media files");
throw new RuntimeException(e);
} finally {
if (resp != null && resp.body() != null) {
resp.body().close();
}
}
}
use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.
the class RemoteMediaServer method mediaChanges.
// args: lastUsn
public JSONArray mediaChanges(int lastUsn) throws UnknownHttpResponseException, MediaSyncException {
try {
mPostVars = HashUtil.HashMapInit(1);
mPostVars.put("sk", mSKey);
Response resp = super.req("mediaChanges", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("lastUsn", lastUsn))));
JSONObject jresp = new JSONObject(resp.body().string());
return _dataOnly(jresp, JSONArray.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.
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=? WHERE usn=-1", mMaxUsn);
}
}
buf.put(curTable, rows);
lim -= fetched;
}
if (mTablesLeft.isEmpty()) {
buf.put("done", true);
}
return buf;
}
Aggregations