Search in sources :

Example 41 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class AnkiChinaSyncer method handleServerData.

// 35%+25%
private void handleServerData(JSONObject item) {
    mCol = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
    CollectionHelper.getInstance().lockCollection();
    updateDialogProgress(SYNCING_DATA, "更新全局配置中", mCurrentProgress + 1);
    DB db = mCol.getDb();
    try {
        JSONObject remoteCol = item.getJSONObject("col").getJSONObject("replace");
        // db.execute("update col set id ="+remoteCol.getInt("id")+","+"set crt =\"+remoteCol.getLong(\"crt\")");
        // db.execute("update col set crt ="+remoteCol.getLong("crt"));
        // db.execute("update col set mod ="+remoteCol.getLong("mod"));
        // db.execute("update col set scm ="+remoteCol.getLong("scm"));
        // db.execute("update col set ver ="+remoteCol.getInt("ver"));
        // db.execute("update col set dty ="+remoteCol.getInt("dty"));
        // db.execute("update col set usn ="+remoteCol.getInt("usn"));
        // db.execute("update col set ls ="+remoteCol.getLong("ls"));
        // db.execute("update col set conf ="+remoteCol.getString("conf"));
        // db.execute("update col set tags ="+remoteCol.getString("tags"));
        // db.execute("update col set tags ="+remoteCol.getString("tags"));
        // db.execute("update col set id = %d,");
        Timber.i("remote col config:%s", remoteCol.toString());
        @SuppressLint("DefaultLocale") String sql = String.format("update col set id = %d,crt = %d,mod=%d,scm=%d,ver=%d,dty=%d,usn=%d,ls=%d,conf='%s',tags='%s'", remoteCol.getInt("id"), remoteCol.getLong("crt"), remoteCol.getLong("mod"), remoteCol.getLong("scm"), remoteCol.getInt("ver"), remoteCol.getInt("dty"), remoteCol.getInt("usn"), remoteCol.getLong("ls"), remoteCol.getString("conf"), remoteCol.getString("tags") == null || !remoteCol.getString("tags").startsWith("{") ? "{}" : remoteCol.getString("tags"));
        db.execute(sql);
        mCol.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Decks currentDecks = mCol.getDecks();
    // 删除多余的内容
    try {
        JSONArray deletedDecks = item.getJSONObject("decks").getJSONArray("delete");
        if (deletedDecks.length() > 0) {
            double percent = 2.0 / deletedDecks.length();
            for (int i = 0; i < deletedDecks.length(); i++) {
                String deckID = deletedDecks.getString(i);
                currentDecks.rem(Long.parseLong(deckID));
                updateDialogProgress(SYNCING_DATA, "删除多余牌组中", mCurrentProgress + percent);
            }
            mCol.save();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    try {
        JSONArray deletedDConf = item.getJSONObject("dconf").getJSONArray("delete");
        if (deletedDConf.length() > 0) {
            double percent = 2.0 / deletedDConf.length();
            for (int i = 0; i < deletedDConf.length(); i++) {
                String id = deletedDConf.getString(i);
                mCol.getDecks().remConf(Long.parseLong(id));
                updateDialogProgress(SYNCING_DATA, "删除多余牌组配置中", mCurrentProgress + percent);
            }
            mCol.save();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    try {
        JSONArray deletedModel = item.getJSONObject("models").getJSONArray("delete");
        if (deletedModel.length() > 0) {
            double percent = 2.0 / deletedModel.length();
            for (int i = 0; i < deletedModel.length(); i++) {
                String id = deletedModel.getString(i);
                mCol.getModels().rem(mCol.getModels().get(Long.parseLong(id)));
                updateDialogProgress(SYNCING_DATA, "删除多余模板中", mCurrentProgress + percent);
            }
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    updateDialogProgress(SYNCING_DATA, "删除多余卡牌中", mCurrentProgress + 2);
    try {
        JSONArray deletedCards = item.getJSONObject("cards").getJSONArray("delete");
        List<Long> sids = ids2longList(deletedCards);
        Timber.e("need delete cards num:%s", sids.size());
        // db.execute("DELETE FROM cards WHERE id IN " + sids);
        mCol.remCards(sids);
        mCol.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
    updateDialogProgress(SYNCING_DATA, "删除多余笔记中", mCurrentProgress + 2);
    try {
        JSONArray deletedNotes = item.getJSONObject("notes").getJSONArray("delete");
        long[] sids = ids2longArray(deletedNotes);
        // db.execute("DELETE FROM notes WHERE id IN " + sids);
        mCol.remNotes(sids);
        mCol.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        JSONObject replaceDecks = item.getJSONObject("decks").getJSONObject("replace");
        if (replaceDecks.length() > 0) {
            double percent = 2.0 / replaceDecks.length();
            Iterator<String> it = replaceDecks.keys();
            while (it.hasNext()) {
                String next = it.next();
                try {
                    // mCol.getDecks().getDecks().put(Long.parseLong(next), new Deck(replaceDecks.getJSONObject(next)));
                    mCol.getDecks().update(new Deck(replaceDecks.getJSONObject(next)));
                    updateDialogProgress(SYNCING_DATA, "同步牌组数据中", mCurrentProgress + percent);
                } catch (Exception e) {
                // 只遍历model id
                }
            }
            mCol.save();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    db.getDatabase().beginTransaction();
    try {
        JSONArray replace = item.getJSONObject("revlog").getJSONArray("replace");
        if (replace.length() > 0) {
            for (int i = 0; i < replace.length(); i++) {
                log(replace.getJSONArray(i).get(0), replace.getJSONArray(i).get(1), replace.getJSONArray(i).get(2), replace.getJSONArray(i).get(3), replace.getJSONArray(i).get(4), replace.getJSONArray(i).get(5), replace.getJSONArray(i).get(6), replace.getJSONArray(i).get(7), replace.getJSONArray(i).get(8));
            }
            db.getDatabase().setTransactionSuccessful();
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.getDatabase().endTransaction();
    }
    // Timber.e("看看是null还是null:%s,%s", (item.getJSONObject("dconf").get("replace")==JSONObject.NULL), (item.getJSONObject("dconf").getString("replace").equals("null")));
    try {
        JSONObject replaceDConf = item.getJSONObject("dconf").getJSONObject("replace");
        if (replaceDConf.length() > 0) {
            double percent = 2.0 / replaceDConf.length();
            Iterator<String> it = replaceDConf.keys();
            while (it.hasNext()) {
                String next = it.next();
                try {
                    Long.parseLong(next);
                    mCol.getDecks().updateConf(new DeckConfig(replaceDConf.getJSONObject(next)));
                    updateDialogProgress(SYNCING_DATA, "同步牌组配置数据中", mCurrentProgress + percent);
                } catch (Exception e) {
                // 只遍历model id
                }
            }
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        JSONObject replaceModels = item.getJSONObject("models").getJSONObject("replace");
        if (replaceModels.length() > 0) {
            double percent = 5.0 / replaceModels.length();
            Iterator<String> it = replaceModels.keys();
            while (it.hasNext()) {
                String next = it.next();
                try {
                    Long.parseLong(next);
                    mCol.getModels().update(new Model(replaceModels.getJSONObject(next)));
                    updateDialogProgress(SYNCING_DATA, "同步模板数据中", mCurrentProgress + percent);
                } catch (Exception e) {
                // 只遍历model id
                }
            }
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    db.getDatabase().beginTransaction();
    try {
        JSONArray replace = item.getJSONObject("notes").getJSONArray("replace");
        if (replace.length() > 0) {
            double percent = mPullNotesPerPercent / replace.length();
            for (int i = 0; i < replace.length(); i++) {
                // String values = replace.getJSONArray(i).toString().replace("[", "").replace("]", "").replaceAll("\"","'").replaceAll("\u001f","\u001f");
                // String sql = "replace into notes(id,guid,mid,mod,usn,tags,flds,sfld,csum,flags,data) values ( " + values + ")";
                // 
                db.execute("insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)", replace.getJSONArray(i).get(0), replace.getJSONArray(i).get(1), replace.getJSONArray(i).get(2), replace.getJSONArray(i).get(3), replace.getJSONArray(i).get(4), replace.getJSONArray(i).get(5), replace.getJSONArray(i).get(6), replace.getJSONArray(i).get(7), replace.getJSONArray(i).get(8), replace.getJSONArray(i).get(9), replace.getJSONArray(i).get(10));
                updateDialogProgress(SYNCING_DATA, "同步笔记数据中", mCurrentProgress + percent);
            }
            db.getDatabase().setTransactionSuccessful();
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.getDatabase().endTransaction();
    }
    db.getDatabase().beginTransaction();
    try {
        JSONArray replace = item.getJSONObject("cards").getJSONArray("replace");
        if (replace.length() > 0) {
            double percent = 5.0 / replace.length();
            for (int i = 0; i < replace.length(); i++) {
                // String values = replace.getJSONArray(i).toString().replace("[", "").replace("]", "").replaceAll("\"","'").replaceAll("\u001f","\u001f");
                // String sql = "replace into cards(id,nid,did,ord,mod,usn,type,queue,due,ivl,factor,reps,lapses,left,odue,odid,flags,data) values ( " + values + ")";
                // Timber.i("update dialog progress:%d", percent);
                db.execute("insert or replace into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", replace.getJSONArray(i).get(0), replace.getJSONArray(i).get(1), replace.getJSONArray(i).get(2), replace.getJSONArray(i).get(3), replace.getJSONArray(i).get(4), replace.getJSONArray(i).get(5), replace.getJSONArray(i).get(6), replace.getJSONArray(i).get(7), replace.getJSONArray(i).get(8), replace.getJSONArray(i).get(9), replace.getJSONArray(i).get(10), replace.getJSONArray(i).get(11), replace.getJSONArray(i).get(12), replace.getJSONArray(i).get(13), replace.getJSONArray(i).get(14), replace.getJSONArray(i).get(15), replace.getJSONArray(i).get(16), replace.getJSONArray(i).get(17));
                updateDialogProgress(SYNCING_DATA, "同步卡牌数据中", mCurrentProgress + percent);
            }
            db.getDatabase().setTransactionSuccessful();
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.getDatabase().endTransaction();
    }
    CollectionHelper.getInstance().unlockCollection();
}
Also used : Decks(com.ichi2.libanki.Decks) JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) FileNotFoundException(java.io.FileNotFoundException) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) SuppressLint(android.annotation.SuppressLint) DB(com.ichi2.libanki.DB) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 42 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS 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 43 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class Syncer method getTags.

/**
 * Tags ********************************************************************
 */
private JSONArray getTags() {
    JSONArray result = new JSONArray();
    if (mCol.getServer()) {
        for (Map.Entry<String, Integer> t : mCol.getTags().allItems()) {
            if (t.getValue() >= mMinUsn) {
                result.put(t.getKey());
            }
        }
    } else {
        for (Map.Entry<String, Integer> t : mCol.getTags().allItems()) {
            if (t.getValue() == -1) {
                String tag = t.getKey();
                mCol.getTags().add(t.getKey(), mMaxUsn);
                result.put(tag);
            }
        }
        mCol.getTags().save();
    }
    return result;
}
Also used : JSONArray(com.ichi2.utils.JSONArray) HashMap(java.util.HashMap) Map(java.util.Map)

Example 44 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class Syncer method changes.

/**
 * Bundle up small objects.
 */
public JSONObject changes() {
    JSONObject o = new JSONObject();
    o.put("models", getModels());
    o.put("decks", getDecks());
    o.put("tags", getTags());
    if (mLNewer) {
        o.put("conf", getConf());
        o.put("crt", mCol.getCrt());
    }
    return o;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 45 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class Models method newModel.

/**
 * Create a new model, save it in the registry, and return it.
 */
// Called `new` in Anki's code. New is a reserved word in java,
// not in python. Thus the method has to be renamed.
public Model newModel(String name) {
    // caller should call save() after modifying
    Model m;
    m = new Model(defaultModel);
    m.put("name", name);
    m.put("mod", mCol.getTime().intTime());
    m.put("flds", new JSONArray());
    m.put("tmpls", new JSONArray());
    m.put("tags", new JSONArray());
    m.put("id", 0);
    return m;
}
Also used : JSONArray(com.ichi2.utils.JSONArray)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)27 Note (com.ichi2.libanki.Note)18 JSONArray (com.ichi2.utils.JSONArray)18 ArrayList (java.util.ArrayList)13 Card (com.ichi2.libanki.Card)12 Collection (com.ichi2.libanki.Collection)12 Model (com.ichi2.libanki.Model)11 Map (java.util.Map)10 HashMap (java.util.HashMap)9 SuppressLint (android.annotation.SuppressLint)8 Resources (android.content.res.Resources)8 Deck (com.ichi2.libanki.Deck)8 JSONException (com.ichi2.utils.JSONException)8 JSONException (org.json.JSONException)8 Intent (android.content.Intent)7 View (android.view.View)7 TextView (android.widget.TextView)7 Pair (android.util.Pair)5 ContentValues (android.content.ContentValues)4 Uri (android.net.Uri)4