Search in sources :

Example 66 with JSONException

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);
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 67 with JSONException

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"));
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Note(com.ichi2.libanki.Note) Test(org.junit.Test)

Example 68 with JSONException

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;
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONException(com.ichi2.utils.JSONException)

Example 69 with JSONException

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);
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) AbstractSched(com.ichi2.libanki.sched.AbstractSched) JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) JSONException(com.ichi2.utils.JSONException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 70 with JSONException

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;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Aggregations

JSONException (com.ichi2.utils.JSONException)54 JSONObject (com.ichi2.utils.JSONObject)41 JSONException (org.json.JSONException)28 Collection (com.ichi2.libanki.Collection)25 ArrayList (java.util.ArrayList)25 JSONObject (org.json.JSONObject)22 JSONArray (com.ichi2.utils.JSONArray)21 SuppressLint (android.annotation.SuppressLint)18 IOException (java.io.IOException)17 File (java.io.File)16 Note (com.ichi2.libanki.Note)14 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)12 HashMap (java.util.HashMap)12 Bundle (android.os.Bundle)11 Deck (com.ichi2.libanki.Deck)11 Resources (android.content.res.Resources)10 Model (com.ichi2.libanki.Model)10 Map (java.util.Map)10 List (java.util.List)9 Context (android.content.Context)8