Search in sources :

Example 16 with DB

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

the class Storage method _setColVars.

private static void _setColVars(DB db, @NonNull Time time) {
    JSONObject g = new JSONObject(Decks.defaultDeck);
    g.put("id", 1);
    g.put("name", "Default");
    g.put("conf", 1);
    g.put("mod", time.intTime());
    JSONObject gc = new JSONObject(Decks.defaultConf);
    gc.put("id", 1);
    JSONObject ag = new JSONObject();
    ag.put("1", g);
    JSONObject agc = new JSONObject();
    agc.put("1", gc);
    ContentValues values = new ContentValues();
    values.put("conf", Collection.defaultConf);
    values.put("decks", Utils.jsonToString(ag));
    values.put("dconf", Utils.jsonToString(agc));
    db.update("col", values);
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject)

Example 17 with DB

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

the class AnkiChinaSyncer method compareSyncTable.

private JSONObject compareSyncTable() {
    mCol = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
    CollectionHelper.getInstance().lockCollection();
    DB db = mCol.getDb();
    db.execute("create index if not exists ix_synclog_id on synclog (id);)");
    int num = db.queryScalar("SELECT id FROM synclog");
    if (num == 0) {
        // 表是空的,则直接同步所有内容到该表
        Timber.w("no record in synclog table!");
    }
    JSONObject root = new JSONObject();
    JSONObject colJson = new JSONObject();
    JSONObject colJsonReplace = new JSONObject();
    // JSONObject decksJson = new JSONObject();
    // JSONObject modelsJson = new JSONObject();
    // JSONObject dconfJson = new JSONObject();
    colJsonReplace.put("crt", mCol.getCrt());
    colJsonReplace.put("mod", mCol.getMod());
    colJsonReplace.put("scm", mCol.getScm());
    colJsonReplace.put("ver", mCol.getVer());
    colJsonReplace.put("dty", mCol.getDirty() ? 1 : 0);
    colJsonReplace.put("usn", mCol.getUsnForSync());
    colJsonReplace.put("ls", mCol.getLs());
    colJsonReplace.put("conf", mCol.getConf());
    colJsonReplace.put("tags", mCol.getTagsJson());
    colJson.put("replace", colJsonReplace);
    root.put("col", colJson);
    root.put("decks", getChangedColJson(SYNC_LOG_TYPE_DECKS, mCol.getDecks().all()));
    root.put("dconf", getChangedColJson(SYNC_LOG_TYPE_DCONF, mCol.getDecks().allConf()));
    root.put("models", getChangedColJson(SYNC_LOG_TYPE_MODELS, mCol.getModels().all()));
    updateDialogProgress(SYNCING_DATA, "整理数据中", 6);
    root.put("cards", getChangedCardsOrNote(SYNC_LOG_TYPE_CARD));
    root.put("notes", getChangedCardsOrNote(SYNC_LOG_TYPE_NOTE));
    root.put("revlog", getAddedRevLog());
    CollectionHelper.getInstance().unlockCollection();
    // root.put("revlog", getChangedCardsOrNote(SYNC_LOG_TYPE_REVLOG));
    return root;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) DB(com.ichi2.libanki.DB) SuppressLint(android.annotation.SuppressLint)

Example 18 with DB

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

the class FullSyncer method download.

@Override
public Object[] download() throws UnknownHttpResponseException {
    InputStream cont;
    ResponseBody body = null;
    try {
        Response ret = super.req("download");
        if (ret == null || ret.body() == null) {
            return null;
        }
        body = ret.body();
        cont = body.byteStream();
    } catch (IllegalArgumentException e1) {
        if (body != null) {
            body.close();
        }
        throw new RuntimeException(e1);
    }
    String path;
    if (mCol != null) {
        Timber.i("Closing collection for full sync");
        // Usual case where collection is non-null
        path = mCol.getPath();
        mCol.close();
        mCol = null;
    } else {
        // Allow for case where collection is completely unreadable
        Timber.w("Collection was unexpectedly null when doing full sync download");
        path = CollectionHelper.getCollectionPath(AnkiDroidApp.getInstance());
    }
    String tpath = path + ".tmp";
    try {
        super.writeToFile(cont, tpath);
        Timber.d("Full Sync - Downloaded temp file");
        FileInputStream fis = new FileInputStream(tpath);
        if ("upgradeRequired".equals(super.stream2String(fis, 15))) {
            Timber.w("Full Sync - 'Upgrade Required' message received");
            return new Object[] { "upgradeRequired" };
        }
    } catch (FileNotFoundException e) {
        Timber.e(e, "Failed to create temp file when downloading collection.");
        throw new RuntimeException(e);
    } catch (IOException e) {
        Timber.e(e, "Full sync failed to download collection.");
        return new Object[] { "sdAccessError" };
    } finally {
        body.close();
    }
    // check the received file is ok
    mCon.publishProgress(R.string.sync_check_download_file);
    DB tempDb = null;
    try {
        tempDb = new DB(tpath);
        if (!"ok".equalsIgnoreCase(tempDb.queryString("PRAGMA integrity_check"))) {
            Timber.e("Full sync - downloaded file corrupt");
            return new Object[] { "remoteDbError" };
        }
    } catch (SQLiteDatabaseCorruptException e) {
        Timber.e("Full sync - downloaded file corrupt");
        return new Object[] { "remoteDbError" };
    } finally {
        if (tempDb != null) {
            tempDb.close();
        }
    }
    Timber.d("Full Sync: Downloaded file was not corrupt");
    // overwrite existing collection
    File newFile = new File(tpath);
    if (newFile.renameTo(new File(path))) {
        Timber.i("Full Sync Success: Overwritten collection with downloaded file");
        return new Object[] { "success" };
    } else {
        Timber.w("Full Sync: Error overwriting collection with downloaded file");
        return new Object[] { "overwriteError" };
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SQLiteDatabaseCorruptException(android.database.sqlite.SQLiteDatabaseCorruptException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) File(java.io.File) DB(com.ichi2.libanki.DB)

Example 19 with DB

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

the class DeckPickerCheckDatabaseListenerTest method validResultWithFailedDatabaseWillShowFailedDialog.

@Test
public void validResultWithFailedDatabaseWillShowFailedDialog() {
    CheckDatabaseResult failedDb = failedDatabase();
    TaskData result = validResultWithData(failedDb);
    execute(result);
    assertThat("Load Failed dialog should be shown if failed data is supplied", impl.didDisplayDialogLoadFailed());
    assertThat("Locked Database dialog should be shown if Db was locked", !impl.didDisplayLockedDialog());
    assertThat("Dialog should not be displayed", !impl.didDisplayMessage());
}
Also used : CheckDatabaseResult(com.ichi2.libanki.Collection.CheckDatabaseResult) TaskData(com.ichi2.async.TaskData) Test(org.junit.Test)

Example 20 with DB

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

the class CollectionUtils method lockDatabase.

public static void lockDatabase(Collection collection) {
    DB db = collection.getDb();
    DB spy = spy(db);
    doThrow(SQLiteDatabaseLockedException.class).when(spy).execute(any());
    doThrow(SQLiteDatabaseLockedException.class).when(spy).execute(any(), any());
    SupportSQLiteDatabase spiedDb = spy(spy.getDatabase());
    when(spy.getDatabase()).thenReturn(spiedDb);
    doThrow(SQLiteDatabaseLockedException.class).when(spiedDb).beginTransaction();
    collection.setDb(spy);
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) DB(com.ichi2.libanki.DB)

Aggregations

File (java.io.File)19 JSONObject (com.ichi2.utils.JSONObject)18 Collection (com.ichi2.libanki.Collection)14 IOException (java.io.IOException)14 DB (com.ichi2.libanki.DB)13 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)11 JSONArray (com.ichi2.utils.JSONArray)10 Test (org.junit.Test)10 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)9 ContentValues (android.content.ContentValues)8 Resources (android.content.res.Resources)8 Uri (android.net.Uri)8 JSONException (com.ichi2.utils.JSONException)8 Cursor (android.database.Cursor)7 Model (com.ichi2.libanki.Model)7 ContentResolver (android.content.ContentResolver)6 FileInputStream (java.io.FileInputStream)6 SQLException (android.database.SQLException)4 BufferedInputStream (java.io.BufferedInputStream)4