use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class CardContentProvider method getTemplateFromUri.
private JSONObject getTemplateFromUri(Uri uri, Collection col) throws JSONException {
JSONObject model = col.getModels().get(getModelIdFromUri(uri, col));
Integer ord = Integer.parseInt(uri.getLastPathSegment());
return model.getJSONArray("tmpls").getJSONObject(ord);
}
use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method testAnki2DiffmodelTemplates.
@Test
public void testAnki2DiffmodelTemplates() throws IOException, JSONException, ImportExportException {
// different from the above as this one tests only the template text being
// changed, not the number of cards/fields
// import the first version of the model
String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-1.apkg");
AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
imp.setDupeOnSchemaChange(true);
imp.run();
// then the version with updated template
tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-2.apkg");
imp = new AnkiPackageImporter(testCol, tmp);
imp.setDupeOnSchemaChange(true);
imp.run();
// collection should contain the note we imported
assertEquals(1, testCol.noteCount());
// the front template should contain the text added in the 2nd package
Long tcid = testCol.findCards("").get(0);
Note tnote = testCol.getCard(tcid).note();
assertTrue(testCol.findTemplates(tnote).get(0).getString("qfmt").contains("Changed Front Template"));
}
use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class RemoteServer method hostKey.
/**
* Returns hkey or null if user/pw incorrect.
*/
@Override
public Response hostKey(String user, String pw) throws UnknownHttpResponseException {
try {
mPostVars = new HashMap<>();
JSONObject credentials = new JSONObject();
credentials.put("u", user);
credentials.put("p", pw);
return super.req("hostKey", HttpSyncer.getInputStream(Utils.jsonToString(credentials)));
} catch (JSONException e) {
return null;
}
}
use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class Syncer method sanityCheck.
public JSONObject sanityCheck() {
JSONObject result = new JSONObject();
try {
if (mCol.getDb().queryScalar("SELECT count() FROM cards WHERE nid NOT IN (SELECT id FROM notes)") != 0) {
Timber.e("Sync - SanityCheck: there are cards without mother notes");
result.put("client", "missing notes");
return result;
}
if (mCol.getDb().queryScalar("SELECT count() FROM notes WHERE id NOT IN (SELECT DISTINCT nid FROM cards)") != 0) {
Timber.e("Sync - SanityCheck: there are notes without cards");
result.put("client", "missing cards");
return result;
}
if (mCol.getDb().queryScalar("SELECT count() FROM cards WHERE usn = -1") != 0) {
Timber.e("Sync - SanityCheck: there are unsynced cards");
result.put("client", "cards had usn = -1");
return result;
}
if (mCol.getDb().queryScalar("SELECT count() FROM notes WHERE usn = -1") != 0) {
Timber.e("Sync - SanityCheck: there are unsynced notes");
result.put("client", "notes had usn = -1");
return result;
}
if (mCol.getDb().queryScalar("SELECT count() FROM revlog WHERE usn = -1") != 0) {
Timber.e("Sync - SanityCheck: there are unsynced revlogs");
result.put("client", "revlog had usn = -1");
return result;
}
if (mCol.getDb().queryScalar("SELECT count() FROM graves WHERE usn = -1") != 0) {
Timber.e("Sync - SanityCheck: there are unsynced graves");
result.put("client", "graves had usn = -1");
return result;
}
for (Deck g : mCol.getDecks().all()) {
if (g.getInt("usn") == -1) {
Timber.e("Sync - SanityCheck: unsynced deck: " + g.getString("name"));
result.put("client", "deck had usn = -1");
return result;
}
}
for (Map.Entry<String, Integer> tag : mCol.getTags().allItems()) {
if (tag.getValue() == -1) {
Timber.e("Sync - SanityCheck: there are unsynced tags");
result.put("client", "tag had usn = -1");
return result;
}
}
boolean found = false;
for (JSONObject m : mCol.getModels().all()) {
if (mCol.getServer()) {
// the web upgrade was mistakenly setting usn
if (m.getInt("usn") < 0) {
m.put("usn", 0);
found = true;
}
} else {
if (m.getInt("usn") == -1) {
Timber.e("Sync - SanityCheck: unsynced model: " + m.getString("name"));
result.put("client", "model had usn = -1");
return result;
}
}
}
if (found) {
mCol.getModels().save();
}
// check for missing parent decks
mCol.getSched().deckDueList();
// return summary of deck
JSONArray check = new JSONArray();
JSONArray counts = new JSONArray();
// #5666 - not in libAnki
// We modified mReportLimit inside the scheduler, and this causes issues syncing dynamic decks.
AbstractSched syncScheduler = mCol.createScheduler(SYNC_SCHEDULER_REPORT_LIMIT);
for (int c : syncScheduler.recalculateCounts()) {
counts.put(c);
}
check.put(counts);
check.put(mCol.getDb().queryScalar("SELECT count() FROM cards"));
check.put(mCol.getDb().queryScalar("SELECT count() FROM notes"));
check.put(mCol.getDb().queryScalar("SELECT count() FROM revlog"));
check.put(mCol.getDb().queryScalar("SELECT count() FROM graves"));
check.put(mCol.getModels().all().size());
check.put(mCol.getDecks().all().size());
check.put(mCol.getDecks().allConf().size());
result.put("client", check);
return result;
} catch (JSONException e) {
Timber.e(e, "Syncer.sanityCheck()");
throw new RuntimeException(e);
}
}
use of com.ichi2.utils.JSONException in project AnkiChinaAndroid by ankichinateam.
the class Syncer method meta.
public JSONObject meta() throws JSONException {
JSONObject j = new JSONObject();
j.put("mod", mCol.getMod());
j.put("scm", mCol.getScm());
j.put("usn", mCol.getUsnForSync());
j.put("ts", mCol.getTime().intTime());
j.put("musn", 0);
j.put("msg", "");
j.put("cont", true);
return j;
}
Aggregations