Search in sources :

Example 56 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackgroundConfSetSubdecks.

private TaskData doInBackgroundConfSetSubdecks(TaskData param) {
    Timber.d("doInBackgroundConfSetSubdecks");
    Collection col = getCol();
    Object[] data = param.getObjArray();
    Deck deck = (Deck) data[0];
    DeckConfig conf = (DeckConfig) data[1];
    try {
        TreeMap<String, Long> children = col.getDecks().children(deck.getLong("id"));
        for (Map.Entry<String, Long> entry : children.entrySet()) {
            Deck child = col.getDecks().get(entry.getValue());
            if (child.getInt("dyn") == 1) {
                continue;
            }
            TaskData newParam = new TaskData(new Object[] { child, conf });
            boolean changed = doInBackgroundConfChange(newParam).getBoolean();
            if (!changed) {
                return new TaskData(false);
            }
        }
        return new TaskData(true);
    } catch (JSONException e) {
        return new TaskData(false);
    }
}
Also used : Deck(com.ichi2.libanki.Deck) JSONException(com.ichi2.utils.JSONException) Collection(com.ichi2.libanki.Collection) JSONObject(com.ichi2.utils.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 57 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackgroundConfReset.

private TaskData doInBackgroundConfReset(TaskData param) {
    Timber.d("doInBackgroundConfReset");
    Collection col = getCol();
    Object[] data = param.getObjArray();
    DeckConfig conf = (DeckConfig) data[0];
    col.getDecks().restoreToDefault(conf);
    col.save();
    return new TaskData(true);
}
Also used : Collection(com.ichi2.libanki.Collection) JSONObject(com.ichi2.utils.JSONObject) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 58 with DeckConfig

use of com.ichi2.libanki.DeckConfig 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 59 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

the class Anki2Importer method _did.

/**
 * Decks
 * ***********************************************************
 */
/**
 * Given did in src col, return local id.
 */
private long _did(long did) {
    // already converted?
    if (mDecks.containsKey(did)) {
        return mDecks.get(did);
    }
    // get the name in src
    Deck g = mSrc.getDecks().get(did);
    String name = g.getString("name");
    // if there's a prefix, replace the top level deck
    if (!TextUtils.isEmpty(mDeckPrefix)) {
        List<String> parts = Arrays.asList(Decks.path(name));
        String tmpname = TextUtils.join("::", parts.subList(1, parts.size()));
        name = mDeckPrefix;
        if (!TextUtils.isEmpty(tmpname)) {
            name += "::" + tmpname;
        }
    }
    // Manually create any parents so we can pull in descriptions
    String head = "";
    List<String> parents = Arrays.asList(Decks.path(name));
    for (String parent : parents.subList(0, parents.size() - 1)) {
        if (!TextUtils.isEmpty(head)) {
            head += "::";
        }
        head += parent;
        long idInSrc = mSrc.getDecks().id(head);
        _did(idInSrc);
    }
    // create in local
    long newid = mDst.getDecks().id(name);
    // pull conf over
    if (g.has("conf") && g.getLong("conf") != 1) {
        DeckConfig conf = mSrc.getDecks().getConf(g.getLong("conf"));
        mDst.getDecks().save(conf);
        mDst.getDecks().updateConf(conf);
        Deck g2 = mDst.getDecks().get(newid);
        g2.put("conf", g.getLong("conf"));
        mDst.getDecks().save(g2);
    }
    // save desc
    Deck deck = mDst.getDecks().get(newid);
    deck.put("desc", g.getString("desc"));
    mDst.getDecks().save(deck);
    // add to deck map and return
    mDecks.put(did, newid);
    return newid;
}
Also used : Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 60 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

the class SchedV2 method undoReview.

@Override
public void undoReview(@NonNull Card oldCardData, boolean wasLeech) {
    // remove leech tag if it didn't have it before
    if (!wasLeech && oldCardData.note().hasTag("leech")) {
        oldCardData.note().delTag("leech");
        oldCardData.note().flush();
    }
    Timber.i("Undo Review of card %d, leech: %b", oldCardData.getId(), wasLeech);
    // write old data
    oldCardData.flush(false);
    DeckConfig conf = _cardConf(oldCardData);
    boolean previewing = conf.getInt("dyn") != 0 && !conf.getBoolean("resched");
    if (!previewing) {
        // and delete revlog entry
        long last = mCol.getDb().queryLongScalar("SELECT id FROM revlog WHERE cid = ? ORDER BY id DESC LIMIT 1", oldCardData.getId());
        mCol.getDb().execute("DELETE FROM revlog WHERE id = " + last);
    }
    // restore any siblings
    mCol.getDb().execute("update cards set queue=type,mod=?,usn=? where queue=" + Consts.QUEUE_TYPE_SIBLING_BURIED + " and nid=?", getTime().intTime(), mCol.usn(), oldCardData.getNid());
    // and finally, update daily count
    @Consts.CARD_QUEUE int n = oldCardData.getQueue() == Consts.QUEUE_TYPE_DAY_LEARN_RELEARN ? Consts.QUEUE_TYPE_LRN : oldCardData.getQueue();
    String type = (new String[] { "new", "lrn", "rev" })[n];
    _updateStats(oldCardData, type, -1);
    decrReps();
}
Also used : DeckConfig(com.ichi2.libanki.DeckConfig)

Aggregations

DeckConfig (com.ichi2.libanki.DeckConfig)85 Collection (com.ichi2.libanki.Collection)57 Card (com.ichi2.libanki.Card)50 Test (org.junit.Test)49 RobolectricTest (com.ichi2.anki.RobolectricTest)48 Note (com.ichi2.libanki.Note)47 JSONArray (com.ichi2.utils.JSONArray)42 JSONObject (com.ichi2.utils.JSONObject)27 Deck (com.ichi2.libanki.Deck)14 NonNull (androidx.annotation.NonNull)12 Cursor (android.database.Cursor)5 JSONException (com.ichi2.utils.JSONException)5 ArrayList (java.util.ArrayList)5 Model (com.ichi2.libanki.Model)4 HashMap (java.util.HashMap)4 SuppressLint (android.annotation.SuppressLint)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 ContentValues (android.content.ContentValues)2 Intent (android.content.Intent)2