Search in sources :

Example 11 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class CardEditor method onCreateDialog.

@Override
protected Dialog onCreateDialog(int id) {
    StyledDialog dialog = null;
    Resources res = getResources();
    StyledDialog.Builder builder = new StyledDialog.Builder(this);
    switch(id) {
        case DIALOG_TAGS_SELECT:
            builder.setTitle(R.string.card_details_tags);
            builder.setPositiveButton(res.getString(R.string.select), new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mAddNote) {
                        try {
                            JSONArray ja = new JSONArray();
                            for (String t : selectedTags) {
                                ja.put(t);
                            }
                            mCol.getModels().current().put("tags", ja);
                            mCol.getModels().setChanged();
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                        mEditorNote.setTags(selectedTags);
                    }
                    mCurrentTags = selectedTags;
                    updateTags();
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            mNewTagEditText = (EditText) new EditText(this);
            mNewTagEditText.setHint(R.string.add_new_tag);
            InputFilter filter = new InputFilter() {

                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    for (int i = start; i < end; i++) {
                        if (source.charAt(i) == ' ' || source.charAt(i) == ',') {
                            return "";
                        }
                    }
                    return null;
                }
            };
            mNewTagEditText.setFilters(new InputFilter[] { filter });
            ImageView mAddTextButton = new ImageView(this);
            mAddTextButton.setImageResource(R.drawable.ic_addtag);
            mAddTextButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String tag = mNewTagEditText.getText().toString();
                    if (tag.length() != 0) {
                        if (mEditorNote.hasTag(tag)) {
                            mNewTagEditText.setText("");
                            return;
                        }
                        selectedTags.add(tag);
                        actualizeTagDialog(mTagsDialog);
                        mNewTagEditText.setText("");
                    }
                }
            });
            FrameLayout frame = new FrameLayout(this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
            params.rightMargin = 10;
            mAddTextButton.setLayoutParams(params);
            frame.addView(mNewTagEditText);
            frame.addView(mAddTextButton);
            builder.setView(frame, false, true);
            dialog = builder.create();
            mTagsDialog = dialog;
            break;
        case DIALOG_DECK_SELECT:
            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) {
                    throw new RuntimeException(e);
                }
            }
            // Convert to Array
            String[] items = new String[dialogDeckItems.size()];
            dialogDeckItems.toArray(items);
            builder.setItems(items, new DialogInterface.OnClickListener() {

                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) {
                                throw new RuntimeException(e);
                            }
                        }
                        mCurrentDid = newId;
                        updateDeck();
                    }
                }
            });
            dialog = builder.create();
            mDeckSelectDialog = dialog;
            break;
        case DIALOG_MODEL_SELECT:
            ArrayList<CharSequence> dialogItems = new ArrayList<CharSequence>();
            // Use this array to know which ID is associated with each
            // Item(name)
            final ArrayList<Long> dialogIds = new ArrayList<Long>();
            ArrayList<JSONObject> models = mCol.getModels().all();
            Collections.sort(models, new JSONNameComparator());
            builder.setTitle(R.string.note_type);
            for (JSONObject m : models) {
                try {
                    dialogItems.add(m.getString("name"));
                    dialogIds.add(m.getLong("id"));
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }
            // Convert to Array
            String[] items2 = new String[dialogItems.size()];
            dialogItems.toArray(items2);
            builder.setItems(items2, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int item) {
                    long oldModelId;
                    try {
                        oldModelId = mCol.getModels().current().getLong("id");
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    long newId = dialogIds.get(item);
                    if (oldModelId != newId) {
                        mCol.getModels().setCurrent(mCol.getModels().get(newId));
                        JSONObject cdeck = mCol.getDecks().current();
                        try {
                            cdeck.put("mid", newId);
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                        mCol.getDecks().save(cdeck);
                        int size = mEditFields.size();
                        String[] oldValues = new String[size];
                        for (int i = 0; i < size; i++) {
                            oldValues[i] = mEditFields.get(i).getText().toString();
                        }
                        setNote();
                        resetEditFields(oldValues);
                        mTimerHandler.removeCallbacks(checkDuplicatesRunnable);
                        duplicateCheck(false);
                    }
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_RESET_CARD:
            builder.setTitle(res.getString(R.string.reset_card_dialog_title));
            builder.setMessage(res.getString(R.string.reset_card_dialog_message));
            builder.setPositiveButton(res.getString(R.string.yes), new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                // for (long cardId :
                // mDeck.getCardsFromFactId(mEditorNote.getId())) {
                // mDeck.cardFromId(cardId).resetCard();
                // }
                // mDeck.reset();
                // setResult(Reviewer.RESULT_EDIT_CARD_RESET);
                // mCardReset = true;
                // Themes.showThemedToast(CardEditor.this,
                // getResources().getString(
                // R.string.reset_card_dialog_confirmation), true);
                }
            });
            builder.setNegativeButton(res.getString(R.string.no), null);
            builder.setCancelable(true);
            dialog = builder.create();
            break;
        case DIALOG_INTENT_INFORMATION:
            dialog = createDialogIntentInformation(builder, res);
    }
    return dialog;
}
Also used : DialogInterface(android.content.DialogInterface) Builder(com.ichi2.themes.StyledDialog.Builder) StyledDialog(com.ichi2.themes.StyledDialog) ArrayList(java.util.ArrayList) ImageView(android.widget.ImageView) EditText(android.widget.EditText) InputFilter(android.text.InputFilter) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Spanned(android.text.Spanned) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) OnClickListener(android.content.DialogInterface.OnClickListener) JSONObject(org.json.JSONObject) FrameLayout(android.widget.FrameLayout) Builder(com.ichi2.themes.StyledDialog.Builder) OnClickListener(android.content.DialogInterface.OnClickListener) Resources(android.content.res.Resources)

Example 12 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class Info method onPrepareDialog.

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    Resources res = getResources();
    StyledDialog ad = (StyledDialog) dialog;
    switch(id) {
        case DIALOG_UPGRADE_ERROR:
        case DIALOG_SYNC_LOG:
            ad.setMessage(mDialogMessage);
            break;
    }
}
Also used : StyledDialog(com.ichi2.themes.StyledDialog) Resources(android.content.res.Resources)

Example 13 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class CardEditor method createDialogIntentInformation.

private StyledDialog createDialogIntentInformation(Builder builder, Resources res) {
    builder.setTitle(res.getString(R.string.intent_add_saved_information));
    ListView listView = new ListView(this);
    mIntentInformationAdapter = new SimpleAdapter(this, mIntentInformation, R.layout.card_item, new String[] { "source", "target", "id" }, new int[] { R.id.card_sfld, R.id.card_tmpl, R.id.card_item });
    listView.setAdapter(mIntentInformationAdapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(CardEditor.this, CardEditor.class);
            intent.putExtra(EXTRA_CALLER, CALLER_CARDEDITOR_INTENT_ADD);
            HashMap<String, String> map = mIntentInformation.get(position);
            intent.putExtra(EXTRA_CONTENTS, map.get("fields"));
            intent.putExtra(EXTRA_ID, map.get("id"));
            startActivityForResult(intent, REQUEST_INTENT_ADD);
            if (AnkiDroidApp.SDK_VERSION > 4) {
                ActivityTransitionAnimation.slide(CardEditor.this, ActivityTransitionAnimation.FADE);
            }
            mIntentInformationDialog.dismiss();
        }
    });
    mCardItemBackground = Themes.getCardBrowserBackground()[0];
    mIntentInformationAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Object arg1, String text) {
            if (view.getId() == R.id.card_item) {
                view.setBackgroundResource(mCardItemBackground);
                return true;
            }
            return false;
        }
    });
    listView.setBackgroundColor(android.R.attr.colorBackground);
    listView.setDrawSelectorOnTop(true);
    listView.setFastScrollEnabled(true);
    Themes.setContentStyle(listView, Themes.CALLER_CARDEDITOR_INTENTDIALOG);
    builder.setView(listView, false, true);
    builder.setCancelable(true);
    builder.setPositiveButton(res.getString(R.string.intent_add_clear_all), new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int arg1) {
            MetaDB.resetIntentInformation(CardEditor.this);
            mIntentInformation.clear();
            dialog.dismiss();
        }
    });
    StyledDialog dialog = builder.create();
    mIntentInformationDialog = dialog;
    return dialog;
}
Also used : OnItemClickListener(android.widget.AdapterView.OnItemClickListener) HashMap(java.util.HashMap) DialogInterface(android.content.DialogInterface) StyledDialog(com.ichi2.themes.StyledDialog) SimpleAdapter(android.widget.SimpleAdapter) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ListView(android.widget.ListView) OnClickListener(android.content.DialogInterface.OnClickListener) JSONObject(org.json.JSONObject)

Example 14 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class StudyOptionsFragment method onCreateDialog.

protected StyledDialog onCreateDialog(int id) {
    StyledDialog dialog = null;
    Resources res = getResources();
    StyledDialog.Builder builder1 = new StyledDialog.Builder(this.getActivity());
    switch(id) {
        case DIALOG_STATISTIC_TYPE:
            dialog = ChartBuilder.getStatisticsDialog(getActivity(), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DeckTask.launchDeckTask(DeckTask.TASK_TYPE_LOAD_STATISTICS, mLoadStatisticsHandler, new DeckTask.TaskData(AnkiDroidApp.getCol(), which, false));
                }
            }, mFragmented);
            break;
        case DIALOG_CUSTOM_STUDY:
            builder1.setTitle(res.getString(R.string.custom_study));
            builder1.setIcon(android.R.drawable.ic_menu_sort_by_size);
            builder1.setItems(res.getStringArray(R.array.custom_study_options_labels), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mCustomDialogChoice = which;
                    if (which == CUSTOM_STUDY_TAGS - 1) {
                        /*
                    		 * There is a special Dialog for CUSTOM STUDY, where instead of only collecting
                    		 * a number, it is necessary to collect a list of tags. This case handles the
                    		 * creation of that Dialog.
                    		 */
                        showDialog(DIALOG_CUSTOM_STUDY_TAGS);
                        return;
                    }
                    showDialog(DIALOG_CUSTOM_STUDY_DETAILS);
                }
            });
            builder1.setCancelable(true);
            dialog = builder1.create();
            break;
        case DIALOG_CUSTOM_STUDY_TAGS:
            /*
            	  * This handles the case where we want to create a Custom Study Deck using tags.
            	  * This dialog needs to be different from the normal Custom Study dialogs, because
            	  * more information is required:
            	  * --List of Tags to select.
            	  * --Which cards to select 
            	  * 	--(New cards, Due cards, or all cards, as in the desktop version)
            	  */
            if (!AnkiDroidApp.colIsOpen()) {
            //TODO how should this error be handled?
            }
            Context context = getActivity().getBaseContext();
            /*
                 * The following RadioButtons and RadioGroup are to select the category of cards
                 * to select for the Custom Study Deck (New, Due or All cards).
                 */
            RadioGroup rg = formatRGCardType(context, res);
            mSelectWhichCards = rg;
            builder1.setView(rg, false, true);
            //Here we add the list of tags for the whole collection.
            Collection col;
            col = AnkiDroidApp.getCol();
            allTags = col.getTags().all();
            builder1.setTitle(R.string.studyoptions_limit_select_tags);
            builder1.setMultiChoiceItems(allTags, new boolean[allTags.length], new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String tag = allTags[which];
                    if (mSelectedTags.contains(tag)) {
                        Log.i(AnkiDroidApp.TAG, "unchecked tag: " + tag);
                        mSelectedTags.remove(tag);
                    } else {
                        Log.i(AnkiDroidApp.TAG, "checked tag: " + tag);
                        mSelectedTags.add(tag);
                    }
                }
            });
            /*
                 * Here's the method that gathers the final selection of tags, type of cards
                 * and generates the search screen for the custom study deck.
                 */
            builder1.setPositiveButton(res.getString(R.string.select), new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mCustomStudyEditText.setText("");
                    String tags = mSelectedTags.toString();
                    mCustomStudyEditText.setHint(getResources().getString(R.string.card_browser_tags_shown, tags.substring(1, tags.length() - 1)));
                    StringBuilder sb = new StringBuilder();
                    switch(mSelectedOption) {
                        case 1:
                            sb.append("is:new ");
                            break;
                        case 2:
                            sb.append("is:due ");
                            break;
                        default:
                            // Logging here might be appropriate : )
                            break;
                    }
                    int i = 0;
                    for (String tag : mSelectedTags) {
                        if (i != 0) {
                            sb.append("or ");
                        } else {
                            //Only if we really have selected tags
                            sb.append("(");
                        }
                        sb.append("tag:").append(tag).append(" ");
                        i++;
                    }
                    if (i > 0) {
                        //Only if we added anything to the tag list 
                        sb.append(")");
                    }
                    mSearchTerms = sb.toString();
                    createFilteredDeck(new JSONArray(), new Object[] { mSearchTerms, 9999, Sched.DYN_RANDOM }, false);
                }
            });
            builder1.setNegativeButton(res.getString(R.string.cancel), new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mSelectedTags.clear();
                }
            });
            builder1.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    mSelectedTags.clear();
                }
            });
            dialog = builder1.create();
            break;
        case DIALOG_CUSTOM_STUDY_DETAILS:
            /*
                 * This is the normal case for creating a custom study deck, where the dialog
                 * requires only a numeric input.
                 */
            builder1.setContentView(mCustomStudyDetailsView);
            builder1.setCancelable(true);
            builder1.setNegativeButton(R.string.cancel, null);
            builder1.setPositiveButton(R.string.ok, null);
            dialog = builder1.create();
            break;
        default:
            dialog = null;
            break;
    }
    dialog.setOwnerActivity(getActivity());
    return dialog;
}
Also used : Context(android.content.Context) RadioGroup(android.widget.RadioGroup) DialogInterface(android.content.DialogInterface) ChartBuilder(com.ichi2.charts.ChartBuilder) StyledDialog(com.ichi2.themes.StyledDialog) JSONArray(org.json.JSONArray) TaskData(com.ichi2.async.DeckTask.TaskData) DeckTask(com.ichi2.async.DeckTask) OnClickListener(android.content.DialogInterface.OnClickListener) OnClickListener(android.content.DialogInterface.OnClickListener) Collection(com.ichi2.libanki.Collection) JSONObject(org.json.JSONObject) Resources(android.content.res.Resources) OnCancelListener(android.content.DialogInterface.OnCancelListener)

Example 15 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class MultimediaCardEditorActivity method showModelSelectDialog.

private Dialog showModelSelectDialog() {
    StyledDialog dialog = null;
    StyledDialog.Builder builder = new StyledDialog.Builder(this);
    ArrayList<CharSequence> dialogItems = new ArrayList<CharSequence>();
    // Use this array to know which ID is associated with each
    // Item(name)
    final ArrayList<Long> dialogIds = new ArrayList<Long>();
    ArrayList<JSONObject> models = mCol.getModels().all();
    Collections.sort(models, new JSONNameComparator());
    builder.setTitle(R.string.note_type);
    for (JSONObject m : models) {
        try {
            dialogItems.add(m.getString("name"));
            dialogIds.add(m.getLong("id"));
        } catch (JSONException e) {
            Log.e("Multimedia Editor", e.getMessage());
        }
    }
    // Convert to Array
    String[] items2 = new String[dialogItems.size()];
    dialogItems.toArray(items2);
    builder.setItems(items2, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int item) {
            long oldModelId;
            try {
                oldModelId = mCol.getModels().current().getLong("id");
            } catch (JSONException e) {
                Log.e("Multimedia Editor", e.getMessage());
                return;
            }
            long newId = dialogIds.get(item);
            if (oldModelId != newId) {
                changeCurrentModel(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

StyledDialog (com.ichi2.themes.StyledDialog)14 DialogInterface (android.content.DialogInterface)12 Resources (android.content.res.Resources)10 JSONException (org.json.JSONException)7 JSONObject (org.json.JSONObject)7 OnClickListener (android.content.DialogInterface.OnClickListener)6 Intent (android.content.Intent)4 ArrayList (java.util.ArrayList)4 OnCancelListener (android.content.DialogInterface.OnCancelListener)3 View (android.view.View)3 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 TaskData (com.ichi2.async.DeckTask.TaskData)3 JSONArray (org.json.JSONArray)3 SuppressLint (android.annotation.SuppressLint)2 NotFoundException (android.content.res.Resources.NotFoundException)2 EditText (android.widget.EditText)2 ImageView (android.widget.ImageView)2 RadioGroup (android.widget.RadioGroup)2