Search in sources :

Example 11 with Collection

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

the class DeckTask method doInBackgroundConfChange.

private TaskData doInBackgroundConfChange(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundConfChange");
    Object[] data = params[0].getObjArray();
    Collection col = (Collection) data[0];
    JSONObject deck = (JSONObject) data[1];
    JSONObject conf = (JSONObject) data[2];
    try {
        long newConfId = conf.getLong("id");
        // If new config has a different sorting order, reorder the cards
        int oldOrder = col.getDecks().getConf(deck.getLong("conf")).getJSONObject("new").getInt("order");
        int newOrder = col.getDecks().getConf(newConfId).getJSONObject("new").getInt("order");
        if (oldOrder != newOrder) {
            switch(newOrder) {
                case 0:
                    col.getSched().randomizeCards(deck.getLong("id"));
                    break;
                case 1:
                    col.getSched().orderCards(deck.getLong("id"));
                    break;
            }
        }
        col.getDecks().setConf(deck, newConfId);
        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)

Example 12 with Collection

use of com.ichi2.libanki.Collection 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)

Example 13 with Collection

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

the class Reviewer method onCreate.

// ----------------------------------------------------------------------------
// ANDROID METHODS
// ----------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
    // Create the extensions as early as possible, so that they can be offered events.
    mExtensions = new ReviewerExtRegistry(getBaseContext());
    Themes.applyTheme(this);
    super.onCreate(savedInstanceState);
    Log.i(AnkiDroidApp.TAG, "Reviewer - onCreate");
    // Remove the status bar and title bar
    if (mPrefFullscreenReview) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // Do not hide the title bar in Honeycomb, since it contains the action bar.
        if (AnkiDroidApp.SDK_VERSION <= 11) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
    }
    mChangeBorderStyle = Themes.getTheme() == Themes.THEME_ANDROID_LIGHT || Themes.getTheme() == Themes.THEME_ANDROID_DARK;
    // The hardware buttons should control the music volume while reviewing.
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    Collection col = AnkiDroidApp.getCol();
    if (col == null) {
        reloadCollection(savedInstanceState);
        return;
    } else {
        mSched = col.getSched();
        mCollectionFilename = col.getPath();
        mBaseUrl = Utils.getBaseUrl(col.getMedia().getDir());
        restorePreferences();
        setFullScreen(mPrefFullscreenReview);
        registerExternalStorageListener();
        if (mNightMode) {
            mCurrentBackgroundColor = Themes.getNightModeCardBackground(this);
        } else {
            mCurrentBackgroundColor = Color.WHITE;
        }
        mUseQuickUpdate = shouldUseQuickUpdate();
        initLayout(R.layout.flashcard);
        try {
            String[] title = mSched.getCol().getDecks().current().getString("name").split("::");
            AnkiDroidApp.getCompat().setTitle(this, title[title.length - 1], mInvertedColors);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        AnkiDroidApp.getCompat().setSubtitle(this, "", mInvertedColors);
        if (mPrefTextSelection) {
            clipboardSetText("");
        }
        // Load the template for the card
        try {
            mCardTemplate = Utils.convertStreamToString(getAssets().open("card_template.html"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Initialize text-to-speech. This is an asynchronous operation.
        if (mSpeakText) {
            ReadText.initializeTts(this);
            mTTSProgressDialog = new ProgressDialog(this);
            new AsyncTask<String, Void, Void>() {

                @Override
                protected void onPreExecute() {
                    mTTSProgressDialog.setMessage("Init TTS");
                    mTTSProgressDialog.show();
                }

                @Override
                protected Void doInBackground(String... params) {
                    while (ReadText.mTTSInitDone == false) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void res) {
                    mTTSProgressDialog.dismiss();
                    // Load the first card and start reviewing. Uses the answer card
                    // task to load a card, but since we send null
                    // as the card to answer, no card will be answered.
                    DeckTask.launchDeckTask(DeckTask.TASK_TYPE_ANSWER_CARD, mAnswerCardHandler, new DeckTask.TaskData(mSched, null, 0));
                }
            }.execute();
        }
        // Get last whiteboard state
        if (mPrefWhiteboard && mCurrentCard != null && MetaDB.getWhiteboardState(this, mCurrentCard.getDid()) == 1) {
            mShowWhiteboard = true;
            mWhiteboard.setVisibility(View.VISIBLE);
        }
        // Load the first card and start reviewing. Uses the answer card
        // task to load a card, but since we send null
        // as the card to answer, no card will be answered.
        DeckTask.launchDeckTask(DeckTask.TASK_TYPE_ANSWER_CARD, mAnswerCardHandler, new DeckTask.TaskData(mSched, null, 0));
        // Since we aren't actually answering a card, decrement the rep count
        mSched.setReps(mSched.getReps() - 1);
    }
}
Also used : JSONException(org.json.JSONException) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) IOException(java.io.IOException) ProgressDialog(android.app.ProgressDialog) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) DeckTask(com.ichi2.async.DeckTask) Collection(com.ichi2.libanki.Collection) ReviewerExtRegistry(com.ichi2.anki.reviewer.ReviewerExtRegistry)

Example 14 with Collection

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

the class ImageField method setFormattedString.

@Override
public void setFormattedString(String value) {
    Pattern p = Pattern.compile(PATH_REGEX);
    Matcher m = p.matcher(value);
    String res = "";
    if (m.find()) {
        res = m.group(1);
    }
    Collection col = AnkiDroidApp.getCol();
    String mediaDir = col.getMedia().getDir() + "/";
    setImagePath(mediaDir + res);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Collection(com.ichi2.libanki.Collection)

Example 15 with Collection

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

the class SdCardReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)) {
        Log.i(AnkiDroidApp.TAG, "media eject detected - closing collection and sending broadcast");
        Intent i = new Intent();
        i.setAction(MEDIA_EJECT);
        context.sendBroadcast(i);
        Collection col = AnkiDroidApp.getCol();
        if (col != null) {
            col.close();
        }
    } else if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
        Log.i(AnkiDroidApp.TAG, "media mount detected - sending broadcast");
        Intent i = new Intent();
        i.setAction(MEDIA_MOUNT);
        context.sendBroadcast(i);
    }
}
Also used : Collection(com.ichi2.libanki.Collection) Intent(android.content.Intent)

Aggregations

Collection (com.ichi2.libanki.Collection)40 JSONObject (org.json.JSONObject)20 File (java.io.File)13 JSONException (org.json.JSONException)12 IOException (java.io.IOException)11 Resources (android.content.res.Resources)10 DialogInterface (android.content.DialogInterface)6 DeckTask (com.ichi2.async.DeckTask)6 TaskData (com.ichi2.async.DeckTask.TaskData)6 StyledDialog (com.ichi2.themes.StyledDialog)6 AnkiDb (com.ichi2.anki.AnkiDb)5 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 Intent (android.content.Intent)4 Note (com.ichi2.libanki.Note)4 FileNotFoundException (java.io.FileNotFoundException)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 JSONArray (org.json.JSONArray)4 OnCancelListener (android.content.DialogInterface.OnCancelListener)3