Search in sources :

Example 1 with Anki2Importer

use of com.ichi2.libanki.importer.Anki2Importer in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundImportAdd.

private TaskData doInBackgroundImportAdd(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundImportAdd");
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
    Collection col = params[0].getCollection();
    String path = params[0].getString();
    boolean sharedDeckImport = params[0].getBoolean();
    ProgressCallback pc = null;
    // don't report progress on shared deck import (or maybe should we?)
    if (!sharedDeckImport) {
        pc = new ProgressCallback(this, res);
    }
    int addedCount = -1;
    try {
        Anki2Importer imp = new Anki2Importer(col, path, pc);
        AnkiDb ankiDB = col.getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            addedCount = imp.run();
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
            if (sharedDeckImport) {
                File tmpFile = new File(path);
                tmpFile.delete();
            }
        }
        if (addedCount >= 0) {
            ankiDB.execute("VACUUM");
            ankiDB.execute("ANALYZE");
        }
        publishProgress(new TaskData(res.getString(R.string.import_update_counts)));
        // Update the counts
        DeckTask.TaskData result = doInBackgroundLoadDeckCounts(new TaskData(col));
        if (result == null) {
            return null;
        }
        return new TaskData(addedCount, result.getObjArray(), true);
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportAdd - RuntimeException on importing cards: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportAdd");
        return new TaskData(false);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportAdd - IOException on importing cards: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportAdd");
        return new TaskData(false);
    }
}
Also used : Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) AnkiDb(com.ichi2.anki.AnkiDb) IOException(java.io.IOException) Collection(com.ichi2.libanki.Collection) Resources(android.content.res.Resources) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

Resources (android.content.res.Resources)1 AnkiDb (com.ichi2.anki.AnkiDb)1 Collection (com.ichi2.libanki.Collection)1 Anki2Importer (com.ichi2.libanki.importer.Anki2Importer)1 File (java.io.File)1 IOException (java.io.IOException)1 ZipFile (java.util.zip.ZipFile)1