Search in sources :

Example 16 with Time

use of com.ichi2.libanki.utils.Time in project AnkiChinaAndroid by ankichinateam.

the class SchedV2 method _updateCutoff.

/**
 * Daily cutoff ************************************************************* **********************************
 * This function uses GregorianCalendar so as to be sensitive to leap years, daylight savings, etc.
 */
/* Overriden: other way to count time*/
public void _updateCutoff() {
    Integer oldToday = mToday == null ? 0 : mToday;
    // days since col created
    mToday = _daysSinceCreation();
    // end of day cutoff
    mDayCutoff = _dayCutoff();
    if (oldToday != mToday) {
        mCol.log(mToday, mDayCutoff);
    }
    // instead
    for (Deck deck : mCol.getDecks().all()) {
        update(deck);
    }
    // unbury if the day has rolled over
    int unburied = mCol.getConf().optInt("lastUnburied", 0);
    if (unburied < mToday) {
        SyncStatus.ignoreDatabaseModification(this::unburyCards);
        mCol.getConf().put("lastUnburied", mToday);
    }
}
Also used : Deck(com.ichi2.libanki.Deck)

Example 17 with Time

use of com.ichi2.libanki.utils.Time in project AnkiChinaAndroid by ankichinateam.

the class SchedV2 method _answerLrnCard.

// Overriden
protected void _answerLrnCard(@NonNull Card card, @Consts.BUTTON_TYPE int ease) {
    JSONObject conf = _lrnConf(card);
    @Consts.CARD_TYPE int type;
    if (card.getType() == Consts.CARD_TYPE_REV || card.getType() == Consts.CARD_TYPE_RELEARNING) {
        type = Consts.CARD_TYPE_REV;
    } else {
        type = Consts.CARD_TYPE_NEW;
    }
    // lrnCount was decremented once when card was fetched
    int lastLeft = card.getLeft();
    boolean leaving = false;
    // immediate graduate?
    if (ease == Consts.BUTTON_FOUR) {
        _rescheduleAsRev(card, conf, true);
        leaving = true;
    // next step?
    } else if (ease == Consts.BUTTON_THREE) {
        // graduation time?
        if ((card.getLeft() % 1000) - 1 <= 0) {
            _rescheduleAsRev(card, conf, false);
            leaving = true;
        } else {
            _moveToNextStep(card, conf);
        }
    } else if (ease == Consts.BUTTON_TWO) {
        _repeatStep(card, conf);
    } else {
        // move back to first step
        _moveToFirstStep(card, conf);
    }
    _logLrn(card, ease, conf, leaving, type, lastLeft);
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 18 with Time

use of com.ichi2.libanki.utils.Time in project AnkiChinaAndroid by ankichinateam.

the class Syncer method sync.

/**
 * Returns 'noChanges', 'fullSync', 'success', etc
 */
// public Object[] sync() throws UnknownHttpResponseException {
// return sync(null);
// }
public Object[] sync(Connection con, long restSpace) throws UnknownHttpResponseException, NoEnoughServerSpaceException {
    mSyncMsg = "";
    setRestSpace(restSpace);
    // if the deck has any pending changes, flush them first and bump mod time
    mCol.getSched()._updateCutoff();
    mCol.save();
    // step 1: login & metadata
    Response ret = mServer.meta();
    if (ret == null) {
        return null;
    }
    int returntype = ret.code();
    if (returntype == 403) {
        return new Object[] { "badAuth" };
    }
    try {
        mCol.getDb().getDatabase().beginTransaction();
        try {
            Timber.i("Sync: getting meta data from server");
            JSONObject rMeta = new JSONObject(ret.body().string());
            mCol.log("rmeta", rMeta);
            mSyncMsg = rMeta.getString("msg");
            if (!rMeta.getBoolean("cont")) {
                // Don't add syncMsg; it can be fetched by UI code using the accessor
                return new Object[] { "serverAbort" };
            } else {
            // don't abort, but ui should show messages after sync finishes
            // and require confirmation if it's non-empty
            }
            throwExceptionIfCancelled(con);
            long rscm = rMeta.getLong("scm");
            int rts = rMeta.getInt("ts");
            mRMod = rMeta.getLong("mod");
            mMaxUsn = rMeta.getInt("usn");
            // skip uname, AnkiDroid already stores and shows it
            trySetHostNum(rMeta);
            Timber.i("Sync: building local meta data");
            JSONObject lMeta = meta();
            mCol.log("lmeta", lMeta);
            mLMod = lMeta.getLong("mod");
            mMinUsn = lMeta.getInt("usn");
            long lscm = lMeta.getLong("scm");
            int lts = lMeta.getInt("ts");
            long diff = Math.abs(rts - lts);
            if (diff > 300) {
                mCol.log("clock off");
                return new Object[] { "clockOff", diff };
            }
            if (mLMod == mRMod) {
                Timber.i("Sync: no changes - returning");
                mCol.log("no changes");
                return new Object[] { "noChanges" };
            } else if (lscm != rscm) {
                Timber.i("Sync: full sync necessary - returning");
                mCol.log("schema diff");
                return new Object[] { "fullSync" };
            }
            mLNewer = mLMod > mRMod;
            // step 1.5: check collection is valid
            if (!mCol.basicCheck()) {
                mCol.log("basic check");
                return new Object[] { "basicCheckFailed" };
            }
            throwExceptionIfCancelled(con);
            // step 2: deletions
            publishProgress(con, R.string.sync_deletions_message);
            Timber.i("Sync: collection removed data");
            JSONObject lrem = removed();
            JSONObject o = new JSONObject();
            o.put("minUsn", mMinUsn);
            o.put("lnewer", mLNewer);
            o.put("graves", lrem);
            Timber.i("Sync: sending and receiving removed data");
            JSONObject rrem = mServer.start(o);
            Timber.i("Sync: applying removed data");
            throwExceptionIfCancelled(con);
            remove(rrem);
            // ... and small objects
            publishProgress(con, R.string.sync_small_objects_message);
            Timber.i("Sync: collection small changes");
            JSONObject lchg = changes();
            JSONObject sch = new JSONObject();
            sch.put("changes", lchg);
            Timber.i("Sync: sending and receiving small changes");
            long needSize = sch.toString().length();
            Timber.i("Sync: sending and receiving small changes size:%d", needSize);
            if (needSize > restSpace && Consts.loginAnkiChina()) {
                throwExceptionIfNoSpace(needSize, restSpace);
            } else {
                restSpace -= needSize;
                setRestSpace(restSpace);
                Timber.i("Sync: remain size %d after for small changes ", restSpace);
            }
            JSONObject rchg = mServer.applyChanges(sch);
            throwExceptionIfCancelled(con);
            Timber.i("Sync: merging small changes");
            try {
                mergeChanges(lchg, rchg);
            } catch (UnexpectedSchemaChange e) {
                mServer.abort();
                _forceFullSync();
            }
            // step 3: stream large tables from server
            publishProgress(con, R.string.sync_download_chunk);
            while (true) {
                throwExceptionIfCancelled(con);
                Timber.i("Sync: downloading chunked data");
                JSONObject chunk = mServer.chunk();
                mCol.log("server chunk", chunk);
                Timber.i("Sync: applying chunked data");
                applyChunk(chunk);
                if (chunk.getBoolean("done")) {
                    break;
                }
            }
            // step 4: stream to server
            publishProgress(con, R.string.sync_upload_chunk);
            List<JSONObject> sechs = new ArrayList<>();
            long chunkSize = 0;
            while (true) {
                throwExceptionIfCancelled(con);
                Timber.i("Sync: collecting chunked data");
                JSONObject chunk = chunk();
                mCol.log("client chunk", chunk);
                JSONObject sech = new JSONObject();
                sech.put("chunk", chunk);
                chunkSize += sech.toString().length();
                sechs.add(sech);
                if (chunk.getBoolean("done")) {
                    break;
                }
            }
            Timber.i("Sync: sending chunked data:%d", chunkSize);
            if (chunkSize > restSpace && Consts.loginAnkiChina()) {
                throwExceptionIfNoSpace(chunkSize, restSpace);
            } else {
                restSpace -= chunkSize;
                setRestSpace(restSpace);
            }
            for (JSONObject object : sechs) {
                mServer.applyChunk(object);
            }
            // step 5: sanity check
            JSONObject c = sanityCheck();
            JSONObject sanity = mServer.sanityCheck2(c);
            if (sanity == null || !"ok".equals(sanity.optString("status", "bad"))) {
                mCol.log("sanity check failed", c, sanity);
                return _forceFullSync();
            }
            // finalize
            publishProgress(con, R.string.sync_finish_message);
            Timber.i("Sync: sending finish command");
            long mod = mServer.finish();
            if (mod == 0) {
                return new Object[] { "finishError" };
            }
            Timber.i("Sync: finishing");
            finish(mod);
            publishProgress(con, R.string.sync_writing_db);
            mCol.getDb().getDatabase().setTransactionSuccessful();
        } catch (NoEnoughServerSpaceException e) {
            // mCol.getDb().getDatabase().endTransaction();
            throw new NoEnoughServerSpaceException(e.rest, e.need);
        // e.printStackTrace();
        // mCol.getDb().getDatabase().endTransaction();
        // return new Object[] {"noServerSpace", e.rest, e.need};
        } finally {
            mCol.getDb().getDatabase().endTransaction();
        }
    } catch (NoEnoughServerSpaceException e) {
        Timber.e("NoEnoughServerSpaceException ");
        throw new NoEnoughServerSpaceException(e.rest, e.need);
    // e.printStackTrace();
    // mCol.getDb().getDatabase().endTransaction();
    // return new Object[] {"noServerSpace", e.rest, e.need};
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (OutOfMemoryError e) {
        AnkiDroidApp.sendExceptionReport(e, "Syncer-sync");
        return new Object[] { "OutOfMemoryError" };
    } catch (IOException e) {
        AnkiDroidApp.sendExceptionReport(e, "Syncer-sync");
        return new Object[] { "IOException" };
    }
    return new Object[] { "success", restSpace };
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) JSONObject(com.ichi2.utils.JSONObject) NoEnoughServerSpaceException(com.ichi2.anki.exception.NoEnoughServerSpaceException)

Example 19 with Time

use of com.ichi2.libanki.utils.Time in project AnkiChinaAndroid by ankichinateam.

the class SchedTest method test_reviewsV1.

@Test
public void test_reviewsV1() throws Exception {
    Collection col = getColV1();
    // add a note
    Note note = col.newNote();
    note.setItem("Front", "one");
    note.setItem("Back", "two");
    col.addNote(note);
    // set the card up as a review card, due 8 days ago
    Card c = note.cards().get(0);
    c.setType(CARD_TYPE_REV);
    c.setQueue(QUEUE_TYPE_REV);
    c.setDue(col.getSched().getToday() - 8);
    c.setFactor(STARTING_FACTOR);
    c.setReps(3);
    c.setLapses(1);
    c.setIvl(100);
    c.startTimer();
    c.flush();
    // save it for later use as well
    Card cardcopy = c.clone();
    // failing it should put it in the learn queue with the default options
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    // different delay to new
    col.reset();
    DeckConfig conf = col.getSched()._cardConf(c);
    conf.getJSONObject("lapse").put("delays", new JSONArray(new double[] { 2, 20 }));
    col.getDecks().save(conf);
    col.getSched().answerCard(c, 1);
    assertEquals(QUEUE_TYPE_LRN, c.getQueue());
    // it should be due tomorrow, with an interval of 1
    assertEquals(col.getSched().getToday() + 1, c.getODue());
    assertEquals(1, c.getIvl());
    // but because it's in the learn queue, its current due time should be in
    // the future
    assertThat(c.getDue(), is(greaterThanOrEqualTo(col.getTime().intTime())));
    assertThat(c.getDue() - col.getTime().intTime(), is(greaterThan(118L)));
    // factor should have been decremented
    assertEquals(2300, c.getFactor());
    // check counters
    assertEquals(2, c.getLapses());
    assertEquals(4, c.getReps());
    // check ests.
    assertEquals(120, col.getSched().nextIvl(c, 1));
    assertEquals(20 * 60, col.getSched().nextIvl(c, 2));
    // try again with an ease of 2 instead
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    c = cardcopy.clone();
    c.flush();
    col.getSched().answerCard(c, 2);
    assertEquals(QUEUE_TYPE_REV, c.getQueue());
    // the new interval should be (100 + 8/4) * 1.2 = 122
    assertTrue(checkRevIvl(col, c, 122));
    assertEquals(col.getSched().getToday() + c.getIvl(), c.getDue());
    // factor should have been decremented
    assertEquals(2350, c.getFactor());
    // check counters
    assertEquals(1, c.getLapses());
    assertEquals(4, c.getReps());
    // ease 3
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    c = cardcopy.clone();
    c.flush();
    col.getSched().answerCard(c, 3);
    // the new interval should be (100 + 8/2) * 2.5 = 260
    assertTrue(checkRevIvl(col, c, 260));
    assertEquals(col.getSched().getToday() + c.getIvl(), c.getDue());
    // factor should have been left alone
    assertEquals(STARTING_FACTOR, c.getFactor());
    // ease 4
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    c = cardcopy.clone();
    c.flush();
    col.getSched().answerCard(c, 4);
    // the new interval should be (100 + 8) * 2.5 * 1.3 = 351
    assertTrue(checkRevIvl(col, c, 351));
    assertEquals(col.getSched().getToday() + c.getIvl(), c.getDue());
    // factor should have been increased
    assertEquals(2650, c.getFactor());
}
Also used : Note(com.ichi2.libanki.Note) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 20 with Time

use of com.ichi2.libanki.utils.Time in project AnkiChinaAndroid by ankichinateam.

the class SchedTest method test_learnV1.

@Test
public void test_learnV1() throws Exception {
    Collection col = getColV1();
    // add a note
    Note note = col.newNote();
    note.setItem("Front", "one");
    note.setItem("Back", "two");
    col.addNote(note);
    // set as a learn card and rebuild queues
    col.getDb().execute("update cards set queue=0, type=0");
    col.reset();
    // sched.getCard should return it, since it's due in the past
    Card c = col.getSched().getCard();
    assertNotNull(c);
    DeckConfig conf = col.getSched()._cardConf(c);
    conf.getJSONObject("new").put("delays", new JSONArray(new double[] { 0.5, 3, 10 }));
    col.getDecks().save(conf);
    // fail it
    col.getSched().answerCard(c, 1);
    // it should have three reps left to graduation
    assertEquals(3, c.getLeft() % 1000);
    assertEquals(3, c.getLeft() / 1000);
    // it should be due in 30 seconds
    long t = Math.round(c.getDue() - col.getTime().intTime());
    assertThat(t, is(greaterThanOrEqualTo(25L)));
    assertThat(t, is(lessThanOrEqualTo(40L)));
    // pass it once
    col.getSched().answerCard(c, 2);
    // it should be due in 3 minutes
    assertEquals(Math.round(c.getDue() - col.getTime().intTime()), 179, 1);
    assertEquals(2, c.getLeft() % 1000);
    assertEquals(2, c.getLeft() / 1000);
    // check log is accurate
    Cursor log = col.getDb().getDatabase().query("select * from revlog order by id desc");
    assertTrue(log.moveToFirst());
    assertEquals(2, log.getInt(3));
    assertEquals(-180, log.getInt(4));
    assertEquals(-30, log.getInt(5));
    // pass again
    col.getSched().answerCard(c, 2);
    // it should be due in 10 minutes
    assertEquals(c.getDue() - col.getTime().intTime(), 599, 1);
    assertEquals(1, c.getLeft() % 1000);
    assertEquals(1, c.getLeft() / 1000);
    // the next pass should graduate the card
    assertEquals(QUEUE_TYPE_LRN, c.getQueue());
    assertEquals(CARD_TYPE_LRN, c.getType());
    col.getSched().answerCard(c, 2);
    assertEquals(QUEUE_TYPE_REV, c.getQueue());
    assertEquals(CARD_TYPE_REV, c.getType());
    // should be due tomorrow, with an interval of 1
    assertEquals(col.getSched().getToday() + 1, c.getDue());
    assertEquals(1, c.getIvl());
    // or normal removal
    c.setType(CARD_TYPE_NEW);
    c.setQueue(QUEUE_TYPE_LRN);
    col.getSched().answerCard(c, 3);
    assertEquals(CARD_TYPE_REV, c.getType());
    assertEquals(QUEUE_TYPE_REV, c.getQueue());
    assertTrue(checkRevIvl(col, c, 4));
    // revlog should have been updated each time
    assertEquals(5, col.getDb().queryScalar("select count() from revlog where type = 0"));
    // now failed card handling
    c.setType(CARD_TYPE_REV);
    c.setQueue(QUEUE_TYPE_LRN);
    c.setODue(123);
    col.getSched().answerCard(c, 3);
    assertEquals(123, c.getDue());
    assertEquals(CARD_TYPE_REV, c.getType());
    assertEquals(QUEUE_TYPE_REV, c.getQueue());
    // we should be able to remove manually, too
    c.setType(CARD_TYPE_REV);
    c.setQueue(QUEUE_TYPE_LRN);
    c.setODue(321);
    c.flush();
    ((Sched) col.getSched()).removeLrn();
    c.load();
    assertEquals(QUEUE_TYPE_REV, c.getQueue());
    assertEquals(321, c.getDue());
}
Also used : Note(com.ichi2.libanki.Note) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) Cursor(android.database.Cursor) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)27 Collection (com.ichi2.libanki.Collection)26 JSONObject (com.ichi2.utils.JSONObject)26 RobolectricTest (com.ichi2.anki.RobolectricTest)19 Card (com.ichi2.libanki.Card)19 Note (com.ichi2.libanki.Note)15 JSONArray (com.ichi2.utils.JSONArray)15 Deck (com.ichi2.libanki.Deck)11 ArrayList (java.util.ArrayList)11 DeckConfig (com.ichi2.libanki.DeckConfig)10 IOException (java.io.IOException)10 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)9 HashMap (java.util.HashMap)9 Cursor (android.database.Cursor)8 Nullable (androidx.annotation.Nullable)8 JSONException (com.ichi2.utils.JSONException)8 SharedPreferences (android.content.SharedPreferences)7 Resources (android.content.res.Resources)7 File (java.io.File)7 View (android.view.View)5