Search in sources :

Example 11 with Decks

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

the class AnkiDroidApp method onCreate.

/**
     * On application creation.
     */
@Override
public void onCreate() {
    super.onCreate();
    if (isNookHdPlus() && AnkiDroidApp.SDK_VERSION == 15) {
        mCompat = new CompatV15NookHdPlus();
    } else if (isNook()) {
        mCompat = new CompatV4();
    } else if (AnkiDroidApp.SDK_VERSION >= 16) {
        mCompat = new CompatV16();
    } else if (AnkiDroidApp.SDK_VERSION >= 15) {
        mCompat = new CompatV15();
    } else if (AnkiDroidApp.SDK_VERSION >= 11) {
        mCompat = new CompatV11();
    } else if (AnkiDroidApp.SDK_VERSION >= 9) {
        mCompat = new CompatV9();
    } else if (AnkiDroidApp.SDK_VERSION >= 5) {
        mCompat = new CompatV5();
    } else {
        mCompat = new CompatV4();
    }
    sInstance = this;
    Connection.setContext(getApplicationContext());
    // Error Reporter
    CustomExceptionHandler customExceptionHandler = CustomExceptionHandler.getInstance();
    customExceptionHandler.init(sInstance.getApplicationContext());
    Thread.setDefaultUncaughtExceptionHandler(customExceptionHandler);
    SharedPreferences preferences = getSharedPrefs(this);
    sInstance.mHooks = new Hooks(preferences);
    setLanguage(preferences.getString("language", ""));
    // Assign some default settings if necessary
    if (!preferences.contains("deckPath")) {
        Editor editor = preferences.edit();
        // Create the folder "AnkiDroid", if not exists, where the decks
        // will be stored by default
        String deckPath = getDefaultAnkiDroidDirectory();
        createDirectoryIfMissing(new File(deckPath));
        // Put the base path in preferences pointing to the default "AnkiDroid" folder
        editor.putString("deckPath", deckPath);
        // Using commit instead of apply even though we don't need a return value.
        // Reason: apply() not available on Android 1.5
        editor.commit();
    }
}
Also used : CompatV15NookHdPlus(com.ichi2.compat.CompatV15NookHdPlus) SharedPreferences(android.content.SharedPreferences) CompatV15(com.ichi2.compat.CompatV15) CompatV16(com.ichi2.compat.CompatV16) CompatV11(com.ichi2.compat.CompatV11) CompatV4(com.ichi2.compat.CompatV4) Hooks(com.ichi2.libanki.hooks.Hooks) CompatV5(com.ichi2.compat.CompatV5) Editor(android.content.SharedPreferences.Editor) File(java.io.File) CompatV9(com.ichi2.compat.CompatV9)

Example 12 with Decks

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

the class CustomFontsReviewerExt method getCustomFontsStyle.

/**
     * Returns the CSS used to handle custom fonts.
     * <p>
     * Custom fonts live in fonts directory in the directory used to store decks.
     * <p>
     * Each font is mapped to the font family by the same name as the name of the font without the extension.
     */
private static String getCustomFontsStyle(Map<String, AnkiFont> customFontsMap) {
    StringBuilder builder = new StringBuilder();
    for (AnkiFont font : customFontsMap.values()) {
        builder.append(font.getDeclaration());
        builder.append('\n');
    }
    return builder.toString();
}
Also used : AnkiFont(com.ichi2.anki.AnkiFont)

Example 13 with Decks

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

the class Connection method doInBackgroundSync.

private Payload doInBackgroundSync(Payload data) {
    // for for doInBackgroundLoadDeckCounts if any
    DeckTask.waitToFinish();
    String hkey = (String) data.data[0];
    boolean media = (Boolean) data.data[1];
    String conflictResolution = (String) data.data[2];
    int mediaUsn = (Integer) data.data[3];
    boolean colCorruptFullSync = false;
    Collection col = AnkiDroidApp.getCol();
    if (!AnkiDroidApp.colIsOpen()) {
        if (conflictResolution != null && conflictResolution.equals("download")) {
            colCorruptFullSync = true;
        } else {
            data.success = false;
            data.result = new Object[] { "genericError" };
            return data;
        }
    }
    String path = AnkiDroidApp.getCollectionPath();
    BasicHttpSyncer server = new RemoteServer(this, hkey);
    Syncer client = new Syncer(col, server);
    // run sync and check state
    boolean noChanges = false;
    if (conflictResolution == null) {
        Log.i(AnkiDroidApp.TAG, "Sync - starting sync");
        publishProgress(R.string.sync_prepare_syncing);
        Object[] ret = client.sync(this);
        data.message = client.getSyncMsg();
        mediaUsn = client.getmMediaUsn();
        if (ret == null) {
            data.success = false;
            data.result = new Object[] { "genericError" };
            return data;
        }
        String retCode = (String) ret[0];
        if (!retCode.equals("noChanges") && !retCode.equals("success")) {
            data.success = false;
            data.result = ret;
            // note mediaUSN for later
            data.data = new Object[] { mediaUsn };
            return data;
        }
        // save and note success state
        if (retCode.equals("noChanges")) {
            // publishProgress(R.string.sync_no_changes_message);
            noChanges = true;
        } else {
        // publishProgress(R.string.sync_database_success);
        }
    } else {
        try {
            server = new FullSyncer(col, hkey, this);
            if (conflictResolution.equals("upload")) {
                Log.i(AnkiDroidApp.TAG, "Sync - fullsync - upload collection");
                publishProgress(R.string.sync_preparing_full_sync_message);
                Object[] ret = server.upload();
                if (ret == null) {
                    data.success = false;
                    data.result = new Object[] { "genericError" };
                    AnkiDroidApp.openCollection(path);
                    return data;
                }
                if (!((String) ret[0]).equals(BasicHttpSyncer.ANKIWEB_STATUS_OK)) {
                    data.success = false;
                    data.result = ret;
                    AnkiDroidApp.openCollection(path);
                    return data;
                }
            } else if (conflictResolution.equals("download")) {
                Log.i(AnkiDroidApp.TAG, "Sync - fullsync - download collection");
                publishProgress(R.string.sync_downloading_message);
                Object[] ret = server.download();
                if (ret == null) {
                    data.success = false;
                    data.result = new Object[] { "genericError" };
                    AnkiDroidApp.openCollection(path);
                    return data;
                }
                if (!((String) ret[0]).equals("success")) {
                    data.success = false;
                    data.result = ret;
                    if (!colCorruptFullSync) {
                        AnkiDroidApp.openCollection(path);
                    }
                    return data;
                }
            }
            col = AnkiDroidApp.openCollection(path);
        } catch (OutOfMemoryError e) {
            AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-fullSync");
            data.success = false;
            data.result = new Object[] { "OutOfMemoryError" };
            data.data = new Object[] { mediaUsn };
            return data;
        } catch (RuntimeException e) {
            AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-fullSync");
            data.success = false;
            data.result = new Object[] { "IOException" };
            data.data = new Object[] { mediaUsn };
            return data;
        }
    }
    // clear undo to avoid non syncing orphans (because undo resets usn too 
    if (!noChanges) {
        col.clearUndo();
    }
    // then move on to media sync
    boolean noMediaChanges = false;
    String mediaError = null;
    if (media) {
        server = new RemoteMediaServer(hkey, this);
        MediaSyncer mediaClient = new MediaSyncer(col, (RemoteMediaServer) server);
        String ret;
        try {
            ret = mediaClient.sync(mediaUsn, this);
            if (ret == null) {
                mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
            } else {
                if (ret.equals("noChanges")) {
                    publishProgress(R.string.sync_media_no_changes);
                    noMediaChanges = true;
                }
                if (ret.equals("sanityFailed")) {
                    mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
                } else {
                    publishProgress(R.string.sync_media_success);
                }
            }
        } catch (RuntimeException e) {
            AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-mediaSync");
            mediaError = e.getLocalizedMessage();
        }
    }
    if (noChanges && noMediaChanges) {
        data.success = false;
        data.result = new Object[] { "noChanges" };
        return data;
    } else {
        data.success = true;
        TreeSet<Object[]> decks = col.getSched().deckDueTree();
        int[] counts = new int[] { 0, 0, 0 };
        for (Object[] deck : decks) {
            if (((String[]) deck[0]).length == 1) {
                counts[0] += (Integer) deck[2];
                counts[1] += (Integer) deck[3];
                counts[2] += (Integer) deck[4];
            }
        }
        Object[] dc = col.getSched().deckCounts();
        data.result = dc[0];
        data.data = new Object[] { conflictResolution, col, dc[1], dc[2], mediaError };
        return data;
    }
}
Also used : FullSyncer(com.ichi2.libanki.sync.FullSyncer) OutOfMemoryError(java.lang.OutOfMemoryError) BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) FullSyncer(com.ichi2.libanki.sync.FullSyncer) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) Syncer(com.ichi2.libanki.sync.Syncer) Collection(com.ichi2.libanki.Collection) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) JSONObject(org.json.JSONObject) RemoteServer(com.ichi2.libanki.sync.RemoteServer) RemoteMediaServer(com.ichi2.libanki.sync.RemoteMediaServer)

Example 14 with Decks

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

the class DeckTask method doInBackgroundConfRemove.

private TaskData doInBackgroundConfRemove(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundConfRemove");
    Object[] data = params[0].getObjArray();
    Collection col = (Collection) data[0];
    JSONObject conf = (JSONObject) data[1];
    try {
        // When a conf is deleted, all decks using it revert to the default conf.
        // Cards must be reordered according to the default conf.
        int order = conf.getJSONObject("new").getInt("order");
        int defaultOrder = col.getDecks().getConf(1).getJSONObject("new").getInt("order");
        if (order != defaultOrder) {
            conf.getJSONObject("new").put("order", defaultOrder);
            col.getSched().resortConf(conf);
        }
        col.getDecks().remConf(conf.getLong("id"));
        return new TaskData(true);
    } catch (JSONException e) {
        return new TaskData(false);
    }
}
Also used : JSONObject(org.json.JSONObject) Collection(com.ichi2.libanki.Collection) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Aggregations

JSONObject (org.json.JSONObject)6 File (java.io.File)4 JSONException (org.json.JSONException)4 Collection (com.ichi2.libanki.Collection)3 DialogInterface (android.content.DialogInterface)2 SharedPreferences (android.content.SharedPreferences)2 Cursor (android.database.Cursor)2 SQLiteException (android.database.sqlite.SQLiteException)2 AnkiDb (com.ichi2.anki.AnkiDb)2 SharedDeckDownload (com.ichi2.anki.SharedDeckDownload)2 BasicHttpSyncer (com.ichi2.libanki.sync.BasicHttpSyncer)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1