Search in sources :

Example 36 with JSONException

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

the class StudyOptionsActivity method onOptionsItemSelected.

/**
 * Handles item selections
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Resources res = this.getResources();
    switch(item.getItemId()) {
        case android.R.id.home:
            closeStudyOptions();
            return true;
        case MENU_PREFERENCES:
            startActivityForResult(new Intent(this, Preferences.class), StudyOptionsFragment.PREFERENCES_UPDATE);
            if (AnkiDroidApp.SDK_VERSION > 4) {
                ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.FADE);
            }
            return true;
        case MENU_ROTATE:
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else {
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            return true;
        case MENU_NIGHT:
            SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(this);
            if (preferences.getBoolean("invertedColors", false)) {
                preferences.edit().putBoolean("invertedColors", false).commit();
                item.setIcon(R.drawable.ic_menu_night);
            } else {
                preferences.edit().putBoolean("invertedColors", true).commit();
                item.setIcon(R.drawable.ic_menu_night_checked);
            }
            return true;
        case DeckPicker.MENU_CREATE_DYNAMIC_DECK:
            StyledDialog.Builder builder = new StyledDialog.Builder(StudyOptionsActivity.this);
            builder.setTitle(res.getString(R.string.new_deck));
            mDialogEditText = new EditText(StudyOptionsActivity.this);
            ArrayList<String> names = AnkiDroidApp.getCol().getDecks().allNames();
            int n = 1;
            String cramDeckName = "Cram 1";
            while (names.contains(cramDeckName)) {
                n++;
                cramDeckName = "Cram " + n;
            }
            mDialogEditText.setText(cramDeckName);
            // mDialogEditText.setFilters(new InputFilter[] { mDeckNameFilter });
            builder.setView(mDialogEditText, false, false);
            builder.setPositiveButton(res.getString(R.string.create), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    long id;
                    Bundle initialConfig = new Bundle();
                    try {
                        initialConfig.putString("searchSuffix", "'deck:" + AnkiDroidApp.getCol().getDecks().current().getString("name") + "'");
                        id = AnkiDroidApp.getCol().getDecks().newDyn(mDialogEditText.getText().toString());
                        AnkiDroidApp.getCol().getDecks().get(id);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    loadContent(false, initialConfig);
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            builder.create().show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Also used : EditText(android.widget.EditText) SharedPreferences(android.content.SharedPreferences) DialogInterface(android.content.DialogInterface) Bundle(android.os.Bundle) StyledDialog(com.ichi2.themes.StyledDialog) JSONException(org.json.JSONException) Intent(android.content.Intent) Resources(android.content.res.Resources) SharedPreferences(android.content.SharedPreferences)

Example 37 with JSONException

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

the class StudyOptionsFragment method onPrepareDialog.

private void onPrepareDialog(int id, StyledDialog styledDialog) {
    Resources res = getResources();
    switch(id) {
        case DIALOG_CUSTOM_STUDY_DETAILS:
            styledDialog.setTitle(res.getStringArray(R.array.custom_study_options_labels)[mCustomDialogChoice]);
            switch(mCustomDialogChoice + 1) {
                case CUSTOM_STUDY_NEW:
                    if (AnkiDroidApp.colIsOpen()) {
                        Collection col = AnkiDroidApp.getCol();
                        mCustomStudyTextView1.setText(res.getString(R.string.custom_study_new_total_new, col.getSched().totalNewForCurrentDeck()));
                    }
                    mCustomStudyTextView2.setText(res.getString(R.string.custom_study_new_extend));
                    mCustomStudyEditText.setText(Integer.toString(AnkiDroidApp.getSharedPrefs(getActivity()).getInt("extendNew", 10)));
                    styledDialog.setButtonOnClickListener(Dialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (AnkiDroidApp.colIsOpen()) {
                                try {
                                    int n = Integer.parseInt(mCustomStudyEditText.getText().toString());
                                    AnkiDroidApp.getSharedPrefs(getActivity()).edit().putInt("extendNew", n).commit();
                                    Collection col = AnkiDroidApp.getCol();
                                    JSONObject deck = col.getDecks().current();
                                    deck.put("extendNew", n);
                                    col.getDecks().save(deck);
                                    col.getSched().extendLimits(n, 0);
                                    resetAndUpdateValuesFromDeck();
                                    finishCongrats();
                                } catch (NumberFormatException e) {
                                    // ignore non numerical values
                                    Themes.showThemedToast(getActivity().getBaseContext(), getResources().getString(R.string.custom_study_invalid_number), false);
                                } catch (JSONException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
                    });
                    break;
                case CUSTOM_STUDY_REV:
                    if (AnkiDroidApp.colIsOpen()) {
                        Collection col = AnkiDroidApp.getCol();
                        mCustomStudyTextView1.setText(res.getString(R.string.custom_study_rev_total_rev, col.getSched().totalRevForCurrentDeck()));
                    }
                    mCustomStudyTextView2.setText(res.getString(R.string.custom_study_rev_extend));
                    mCustomStudyEditText.setText(Integer.toString(AnkiDroidApp.getSharedPrefs(getActivity()).getInt("extendRev", 10)));
                    styledDialog.setButtonOnClickListener(Dialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (AnkiDroidApp.colIsOpen()) {
                                try {
                                    int n = Integer.parseInt(mCustomStudyEditText.getText().toString());
                                    AnkiDroidApp.getSharedPrefs(getActivity()).edit().putInt("extendRev", n).commit();
                                    Collection col = AnkiDroidApp.getCol();
                                    JSONObject deck = col.getDecks().current();
                                    deck.put("extendRev", n);
                                    col.getDecks().save(deck);
                                    col.getSched().extendLimits(0, n);
                                    resetAndUpdateValuesFromDeck();
                                    finishCongrats();
                                } catch (NumberFormatException e) {
                                    // ignore non numerical values
                                    Themes.showThemedToast(getActivity().getBaseContext(), getResources().getString(R.string.custom_study_invalid_number), false);
                                } catch (JSONException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
                    });
                    break;
                case CUSTOM_STUDY_FORGOT:
                    mCustomStudyTextView1.setText("");
                    mCustomStudyTextView2.setText(res.getString(R.string.custom_study_forgotten));
                    mCustomStudyEditText.setText(Integer.toString(AnkiDroidApp.getSharedPrefs(getActivity()).getInt("forgottenDays", 2)));
                    styledDialog.setButtonOnClickListener(Dialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            JSONArray ar = new JSONArray();
                            try {
                                int forgottenDays = Integer.parseInt(((EditText) mCustomStudyEditText).getText().toString());
                                ar.put(0, 1);
                                createFilteredDeck(ar, new Object[] { String.format(Locale.US, "rated:%d:1", forgottenDays), 9999, Sched.DYN_RANDOM }, false);
                            } catch (NumberFormatException e) {
                                // ignore non numerical values
                                Themes.showThemedToast(getActivity().getBaseContext(), getResources().getString(R.string.custom_study_invalid_number), false);
                            } catch (JSONException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    });
                    break;
                case CUSTOM_STUDY_AHEAD:
                    mCustomStudyTextView1.setText("");
                    mCustomStudyTextView2.setText(res.getString(R.string.custom_study_ahead));
                    mCustomStudyEditText.setText(Integer.toString(AnkiDroidApp.getSharedPrefs(getActivity()).getInt("aheadDays", 1)));
                    styledDialog.setButtonOnClickListener(Dialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                int days = Integer.parseInt(((EditText) mCustomStudyEditText).getText().toString());
                                createFilteredDeck(new JSONArray(), new Object[] { String.format(Locale.US, "prop:due<=%d", days), 9999, Sched.DYN_DUE }, true);
                            } catch (NumberFormatException e) {
                                // ignore non numerical values
                                Themes.showThemedToast(getActivity().getBaseContext(), getResources().getString(R.string.custom_study_invalid_number), false);
                            }
                        }
                    });
                    break;
                case CUSTOM_STUDY_RANDOM:
                    mCustomStudyTextView1.setText("");
                    mCustomStudyTextView2.setText(res.getString(R.string.custom_study_random));
                    mCustomStudyEditText.setText(Integer.toString(AnkiDroidApp.getSharedPrefs(getActivity()).getInt("randomCards", 100)));
                    styledDialog.setButtonOnClickListener(Dialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                int randomCards = Integer.parseInt(((EditText) mCustomStudyEditText).getText().toString());
                                createFilteredDeck(new JSONArray(), new Object[] { "", randomCards, Sched.DYN_RANDOM }, true);
                            } catch (NumberFormatException e) {
                                // ignore non numerical values
                                Themes.showThemedToast(getActivity().getBaseContext(), getResources().getString(R.string.custom_study_invalid_number), false);
                            }
                        }
                    });
                    break;
                case CUSTOM_STUDY_PREVIEW:
                    mCustomStudyTextView1.setText("");
                    mCustomStudyTextView2.setText(res.getString(R.string.custom_study_preview));
                    mCustomStudyEditText.setText(Integer.toString(AnkiDroidApp.getSharedPrefs(getActivity()).getInt("previewDays", 1)));
                    styledDialog.setButtonOnClickListener(Dialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String previewDays = ((EditText) mCustomStudyEditText).getText().toString();
                            createFilteredDeck(new JSONArray(), new Object[] { "is:new added:" + previewDays, 9999, Sched.DYN_OLDEST }, false);
                        }
                    });
                    break;
                case CUSTOM_STUDY_TAGS:
                    mCustomStudyTextView1.setText("");
                    mCustomStudyTextView2.setText(res.getString(R.string.custom_study_tags));
                    mCustomStudyEditText.setText(AnkiDroidApp.getSharedPrefs(getActivity()).getString("customTags", ""));
                    styledDialog.setButtonOnClickListener(Dialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String tags = ((EditText) mCustomStudyEditText).getText().toString();
                            createFilteredDeck(new JSONArray(), new Object[] { "(is:new or is:due) " + tags, 9999, Sched.DYN_RANDOM }, true);
                        }
                    });
                    break;
            }
    }
}
Also used : EditText(android.widget.EditText) DialogInterface(android.content.DialogInterface) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) OnClickListener(android.content.DialogInterface.OnClickListener) JSONObject(org.json.JSONObject) Collection(com.ichi2.libanki.Collection) JSONObject(org.json.JSONObject) Resources(android.content.res.Resources)

Example 38 with JSONException

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

the class StudyOptionsFragment method updateValuesFromDeck.

private void updateValuesFromDeck(boolean reset) {
    String fullName;
    if (!AnkiDroidApp.colIsOpen()) {
        return;
    }
    JSONObject deck = AnkiDroidApp.getCol().getDecks().current();
    try {
        fullName = deck.getString("name");
        String[] name = fullName.split("::");
        StringBuilder nameBuilder = new StringBuilder();
        if (name.length > 0) {
            nameBuilder.append(name[0]);
        }
        if (name.length > 1) {
            nameBuilder.append("\n").append(name[1]);
        }
        if (name.length > 3) {
            nameBuilder.append("...");
        }
        if (name.length > 2) {
            nameBuilder.append("\n").append(name[name.length - 1]);
        }
        mTextDeckName.setText(nameBuilder.toString());
        // open cram deck option if deck is opened for the first time
        if (mCramInitialConfig != null) {
            openCramDeckOptions(mCramInitialConfig);
            return;
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    if (!mFragmented) {
        getActivity().setTitle(fullName);
    }
    String desc;
    try {
        if (deck.getInt("dyn") == 0) {
            desc = AnkiDroidApp.getCol().getDecks().getActualDescription();
            mTextDeckDescription.setMaxLines(3);
        } else {
            desc = getResources().getString(R.string.dyn_deck_desc);
            mTextDeckDescription.setMaxLines(5);
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    if (desc.length() > 0) {
        mTextDeckDescription.setText(Html.fromHtml(desc));
        mTextDeckDescription.setVisibility(View.VISIBLE);
    } else {
        mTextDeckDescription.setVisibility(View.GONE);
    }
    DeckTask.launchDeckTask(DeckTask.TASK_TYPE_UPDATE_VALUES_FROM_DECK, mUpdateValuesFromDeckListener, new DeckTask.TaskData(AnkiDroidApp.getCol(), new Object[] { reset, mSmallChart != null }));
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) TaskData(com.ichi2.async.DeckTask.TaskData) DeckTask(com.ichi2.async.DeckTask)

Example 39 with JSONException

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

the class StudyOptionsFragment method createFilteredDeck.

private void createFilteredDeck(JSONArray delays, Object[] terms, Boolean resched) {
    JSONObject dyn;
    if (AnkiDroidApp.colIsOpen()) {
        Collection col = AnkiDroidApp.getCol();
        try {
            String deckName = col.getDecks().current().getString("name");
            String customStudyDeck = getResources().getString(R.string.custom_study_deck_name);
            JSONObject cur = col.getDecks().byName(customStudyDeck);
            if (cur != null) {
                if (cur.getInt("dyn") != 1) {
                    StyledDialog.Builder builder = new StyledDialog.Builder(getActivity());
                    builder.setMessage(R.string.custom_study_deck_exists);
                    builder.setNegativeButton(getResources().getString(R.string.cancel), new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        // 
                        }
                    });
                    builder.create().show();
                    return;
                } else {
                    // safe to empty
                    col.getSched().emptyDyn(cur.getLong("id"));
                    // reuse; don't delete as it may have children
                    dyn = cur;
                    col.getDecks().select(cur.getLong("id"));
                }
            } else {
                long did = col.getDecks().newDyn(customStudyDeck);
                dyn = col.getDecks().get(did);
            }
            // and then set various options
            dyn.put("delays", delays);
            JSONArray ar = dyn.getJSONArray("terms");
            ar.getJSONArray(0).put(0, new StringBuilder("deck:\"").append(deckName).append("\" ").append(terms[0]).toString());
            ar.getJSONArray(0).put(1, terms[1]);
            ar.getJSONArray(0).put(2, terms[2]);
            dyn.put("resched", resched);
            if (mFragmented) {
                Bundle config = new Bundle();
                config.putString("searchSuffix", "'deck:" + dyn.getString("name") + "'");
                initAllContentViews(getLayoutInflater(config));
                finishCongrats();
            } else {
                // Load a new fragment with the filtered deck view. The config passed is null, so it uses the
                // current deck. The deck we just created is internally set as the current deck.
                ((StudyOptionsActivity) getActivity()).loadContent(false, null);
            }
            // Initial rebuild
            mProgressDialog = StyledProgressDialog.show(getActivity(), "", getResources().getString(R.string.rebuild_custom_study_deck), true);
            DeckTask.launchDeckTask(DeckTask.TASK_TYPE_REBUILD_CRAM, mRebuildCustomStudyListener, new DeckTask.TaskData(AnkiDroidApp.getCol(), AnkiDroidApp.getCol().getDecks().selected(), mFragmented));
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : DialogInterface(android.content.DialogInterface) Bundle(android.os.Bundle) ChartBuilder(com.ichi2.charts.ChartBuilder) StyledDialog(com.ichi2.themes.StyledDialog) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) TaskData(com.ichi2.async.DeckTask.TaskData) DeckTask(com.ichi2.async.DeckTask) JSONObject(org.json.JSONObject) Collection(com.ichi2.libanki.Collection) OnClickListener(android.content.DialogInterface.OnClickListener)

Example 40 with JSONException

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

the class MultimediaCardEditorActivity method showDeckSelectDialog.

private Dialog showDeckSelectDialog() {
    StyledDialog dialog = null;
    StyledDialog.Builder builder = new StyledDialog.Builder(this);
    ArrayList<CharSequence> dialogDeckItems = new ArrayList<CharSequence>();
    // Use this array to know which ID is associated with each
    // Item(name)
    final ArrayList<Long> dialogDeckIds = new ArrayList<Long>();
    ArrayList<JSONObject> decks = mCol.getDecks().all();
    Collections.sort(decks, new JSONNameComparator());
    builder.setTitle(R.string.deck);
    for (JSONObject d : decks) {
        try {
            if (d.getInt("dyn") == 0) {
                dialogDeckItems.add(d.getString("name"));
                dialogDeckIds.add(d.getLong("id"));
            }
        } catch (JSONException e) {
            Log.e("Multimedia Editor", e.getMessage());
        }
    }
    // Convert to Array
    String[] items = new String[dialogDeckItems.size()];
    dialogDeckItems.toArray(items);
    builder.setItems(items, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int item) {
            long newId = dialogDeckIds.get(item);
            if (mCurrentDid != newId) {
                if (mAddNote) {
                    try {
                        // TODO: mEditorNote.setDid(newId);
                        mEditorNote.model().put("did", newId);
                        mCol.getModels().setChanged();
                    } catch (JSONException e) {
                        Log.e("Multimedia Editor", e.getMessage());
                    }
                }
                mCurrentDid = newId;
                createEditorUI(mNote);
            }
        }
    });
    dialog = builder.create();
    return dialog;
}
Also used : JSONNameComparator(com.ichi2.utils.JSONNameComparator) DialogInterface(android.content.DialogInterface) StyledDialog(com.ichi2.themes.StyledDialog) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) SuppressLint(android.annotation.SuppressLint) 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