Search in sources :

Example 41 with JSONException

use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.

the class MultimediaCardEditorActivity method changeCurrentModel.

/**
 * Change current model for the Note. Changes both MultimediaNote and the mEditorNote (Note Object) and copies
 * existing values to both.
 *
 * @param newId
 */
protected void changeCurrentModel(long newId) {
    try {
        JSONObject currentModel = mCol.getModels().get(newId);
        mCol.getModels().setCurrent(currentModel);
        JSONObject cdeck = mCol.getDecks().current();
        cdeck.put("mid", newId);
        mCol.getDecks().save(cdeck);
        int size = mNote.getNumberOfFields();
        String[] oldValues = new String[size];
        for (int i = 0; i < size; i++) {
            oldValues[i] = mNote.getField(i).getFormattedValue();
        }
        mEditorNote = new Note(mCol, currentModel);
        mEditorNote.model().put("did", mCurrentDid);
        MultimediaEditableNote newNote = NoteService.createEmptyNote(currentModel);
        for (int i = 0; i < newNote.getNumberOfFields(); i++) {
            if (i < mNote.getNumberOfFields()) {
                newNote.setField(i, mNote.getField(i));
            }
        }
        mNote = newNote;
    } catch (JSONException e) {
        Log.e("Multimedia Editor", e.getMessage());
    }
}
Also used : IMultimediaEditableNote(com.ichi2.anki.multimediacard.IMultimediaEditableNote) MultimediaEditableNote(com.ichi2.anki.multimediacard.impl.MultimediaEditableNote) JSONObject(org.json.JSONObject) Note(com.ichi2.libanki.Note) IMultimediaEditableNote(com.ichi2.anki.multimediacard.IMultimediaEditableNote) MultimediaEditableNote(com.ichi2.anki.multimediacard.impl.MultimediaEditableNote) JSONException(org.json.JSONException) SuppressLint(android.annotation.SuppressLint)

Example 42 with JSONException

use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.

the class MultimediaCardEditorActivity method createEditorUI.

/**
 * Creates the UI for editor area inside EditorLayout
 *
 * @param note
 */
private void createEditorUI(IMultimediaEditableNote note) {
    try {
        if (mNote == null) {
            finishCancel();
            return;
        } else {
            for (int i = 0; i < mNote.getNumberOfFields(); ++i) {
                IField f = mNote.getField(i);
                if (f == null) {
                    finishCancel();
                    return;
                }
            }
        }
        LinearLayout linearLayout = mEditorLayout;
        linearLayout.removeAllViews();
        for (int i = 0; i < note.getNumberOfFields(); ++i) {
            createNewViewer(linearLayout, note.getField(i), i);
        }
        mModelButton.setText(gtxt(R.string.multimedia_editor_activity_note_type) + " : " + mEditorNote.model().getString("name"));
        mDeckButton.setText(gtxt(R.string.multimedia_editor_activity_deck) + " : " + mCol.getDecks().get(mCurrentDid).getString("name"));
    } catch (JSONException e) {
        Log.e("Multimedia Editor", e.getMessage());
        return;
    }
}
Also used : JSONException(org.json.JSONException) IField(com.ichi2.anki.multimediacard.fields.IField) SuppressLint(android.annotation.SuppressLint) LinearLayout(android.widget.LinearLayout)

Example 43 with JSONException

use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.

the class Reviewer method onCreate.

// ----------------------------------------------------------------------------
// ANDROID METHODS
// ----------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
    Themes.applyTheme(this);
    super.onCreate(savedInstanceState);
    // 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);
        }
        // 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 : Collection(com.ichi2.libanki.Collection) JSONException(org.json.JSONException) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) IOException(java.io.IOException) DeckTask(com.ichi2.async.DeckTask)

Example 44 with JSONException

use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundLoadTutorial.

private TaskData doInBackgroundLoadTutorial(TaskData... params) {
    // Log.i(AnkiDroidApp.TAG, "doInBackgroundLoadTutorial");
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
    Collection col = params[0].getCollection();
    col.getDb().getDatabase().beginTransaction();
    String title = res.getString(R.string.help_tutorial);
    try {
        // get deck or create it
        long did = col.getDecks().id(title);
        // reset todays counts
        JSONObject d = col.getDecks().get(did);
        for (String t : new String[] { "new", "rev", "lrn", "time" }) {
            String k = t + "Today";
            JSONArray ja = new JSONArray();
            ja.put(col.getSched().getToday());
            ja.put(0);
            d.put(k, ja);
        }
        // save deck
        col.getDecks().save(d);
        if (col.getSched().cardCount("(" + did + ")") > 0) {
            // deck does already exist. Remove all cards and recreate them
            // to ensure the correct order
            col.remCards(col.getDecks().cids(did));
        }
        JSONObject model = col.getModels().byName(title);
        // }
        if (model == null) {
            model = col.getModels().addBasicModel(col, title);
        }
        model.put("did", did);
        String[] questions = res.getStringArray(R.array.tutorial_questions);
        String[] answers = res.getStringArray(R.array.tutorial_answers);
        String[] sampleQuestions = res.getStringArray(R.array.tutorial_capitals_questions);
        String[] sampleAnswers = res.getStringArray(R.array.tutorial_capitals_answers);
        int len = Math.min(questions.length, answers.length);
        for (int i = 0; i < len + Math.min(sampleQuestions.length, sampleAnswers.length); i++) {
            Note note = col.newNote(model);
            if (note.values().length < 2) {
                return new TaskData(false);
            }
            note.values()[0] = (i < len) ? questions[i] : sampleQuestions[i - len];
            note.values()[1] = (i < len) ? answers[i] : sampleAnswers[i - len];
            col.addNote(note);
        }
        // deck.setSessionTimeLimit(0);
        if (col.getSched().cardCount("(" + did + ")") == 0) {
            // error, delete deck
            col.getDecks().rem(did, true);
            return new TaskData(false);
        } else {
            col.save();
            col.getDecks().select(did);
            col.getDb().getDatabase().setTransactionSuccessful();
            return new TaskData(true);
        }
    } catch (SQLException e) {
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundLoadTutorial");
        return new DeckTask.TaskData(false);
    } catch (JSONException e) {
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundLoadTutorial");
        return new DeckTask.TaskData(false);
    } finally {
        col.getDb().getDatabase().endTransaction();
    }
}
Also used : SQLException(android.database.SQLException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Resources(android.content.res.Resources)

Example 45 with JSONException

use of com.ichi2.utils.JSONException 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

JSONException (com.ichi2.utils.JSONException)54 JSONObject (com.ichi2.utils.JSONObject)41 JSONException (org.json.JSONException)28 Collection (com.ichi2.libanki.Collection)25 ArrayList (java.util.ArrayList)25 JSONObject (org.json.JSONObject)22 JSONArray (com.ichi2.utils.JSONArray)21 SuppressLint (android.annotation.SuppressLint)18 IOException (java.io.IOException)17 File (java.io.File)16 Note (com.ichi2.libanki.Note)14 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)12 HashMap (java.util.HashMap)12 Bundle (android.os.Bundle)11 Deck (com.ichi2.libanki.Deck)11 Resources (android.content.res.Resources)10 Model (com.ichi2.libanki.Model)10 Map (java.util.Map)10 List (java.util.List)9 Context (android.content.Context)8