Search in sources :

Example 6 with AnkiActivity

use of com.ichi2.anki.AnkiActivity in project AnkiChinaAndroid by ankichinateam.

the class StudyOptionsFragment method onMenuItemClick.

@SuppressWarnings("deprecation")
@Override
public boolean onMenuItemClick(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_browser:
            {
                Timber.i("DeckPicker:: Old browser button pressed");
                getAnkiActivity().openOldCardBrowser();
                return true;
            }
        case R.id.action_undo:
            Timber.i("StudyOptionsFragment:: Undo button pressed");
            CollectionTask.launchCollectionTask(UNDO, undoListener);
            return true;
        case R.id.action_deck_options:
            Timber.i("StudyOptionsFragment:: Deck options button pressed");
            if (getCol().getDecks().isDyn(getCol().getDecks().selected())) {
                openFilteredDeckOptions();
            } else {
                Intent i = new Intent(getActivity(), DeckOptions.class);
                getActivity().startActivityForResult(i, DECK_OPTIONS);
                ActivityTransitionAnimation.slide(getActivity(), ActivityTransitionAnimation.FADE);
            }
            return true;
        case R.id.action_setting:
            Timber.i("StudyOptionsFragment:: Deck setting button pressed");
            Intent i = new Intent(getActivity(), StudySettingActivity.class);
            getActivity().startActivityForResult(i, DECK_OPTIONS);
            ActivityTransitionAnimation.slide(getActivity(), ActivityTransitionAnimation.FADE);
            return true;
        case R.id.action_custom_study:
            Timber.i("StudyOptionsFragment:: custom study button pressed");
            showCustomStudyContextMenu();
            return true;
        case R.id.action_unbury:
            Timber.i("StudyOptionsFragment:: unbury button pressed");
            getCol().getSched().unburyCardsForDeck();
            refreshInterfaceAndDecklist(true);
            item.setVisible(false);
            return true;
        case R.id.action_rebuild:
            Timber.i("StudyOptionsFragment:: rebuild cram deck button pressed");
            mProgressDialog = StyledProgressDialog.show(getActivity(), "", getResources().getString(R.string.rebuild_filtered_deck), true);
            CollectionTask.launchCollectionTask(REBUILD_CRAM, getCollectionTaskListener(true));
            return true;
        case R.id.action_empty:
            Timber.i("StudyOptionsFragment:: empty cram deck button pressed");
            mProgressDialog = StyledProgressDialog.show(getActivity(), "", getResources().getString(R.string.empty_filtered_deck), false);
            CollectionTask.launchCollectionTask(EMPTY_CRAM, getCollectionTaskListener(true));
            return true;
        case R.id.action_rename:
            ((AnkiActivity) getActivity()).renameDeckDialog(getCol().getDecks().selected());
            return true;
        case R.id.action_delete:
            getAnkiActivity().mContextMenuDid = getCol().getDecks().selected();
            ((AnkiActivity) getActivity()).confirmDeckDeletion(getCol().getDecks().selected());
            return true;
        case R.id.action_export:
            ((AnkiActivity) getActivity()).exportDeck(getCol().getDecks().selected());
            return true;
        case R.id.action_add_card:
            addNote();
            return true;
        case R.id.create_deck:
            getAnkiActivity().mContextMenuDid = getCol().getDecks().selected();
            ((AnkiActivity) getActivity()).createSubdeckDialog();
            return true;
        case // 停止/恢复
        R.id.action_suspend:
            mDeckIsStopped = deckIsStopped();
            if (mDeckIsStopped) {
                customDialog = new CustomStyleDialog.Builder(getAnkiActivity()).setTitle("恢复学习").setMessage("恢复学习后将每天新卡数和复习数值调整成默认值,请确认是否开始学习").setPositiveButton("确定", (dialog, which) -> {
                    resumeDeckStudy();
                    dialog.dismiss();
                }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).create();
            } else {
                customDialog = new CustomStyleDialog.Builder(getAnkiActivity()).setTitle("停止学习").setMessage("暂停学习将会自动把每天新卡数和复习数调整为零,适用于有事暂停一两周不学习,若该记忆库学习的时长不超过2个月,又暂停了30天未学习,建议重设进度,重新开始学习").setPositiveButton("确定", (dialog, which) -> {
                    stopDeckStudy();
                    dialog.dismiss();
                }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).create();
            }
            customDialog.show();
            return true;
        case // 重设学习进度
        R.id.action_reset_card_progress:
            customDialog = new CustomStyleDialog.Builder(getAnkiActivity()).setTitle("重设学习进度").setMessage("重设学习进度后,该记忆库的所有卡片学习记录都会被清除,请谨慎操作").setPositiveButton("确定", (dialog, which) -> {
                CollectionTask.launchCollectionTask(RESET_DECK, new ResetCardHandler(StudyOptionsFragment.this), new TaskData(new Object[] { "deck:\"" + getCol().getDecks().current().getString("name") + "\" " }));
                dialog.dismiss();
            }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).create();
            customDialog.show();
            return true;
        default:
            return false;
    }
}
Also used : DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) Bundle(android.os.Bundle) TypeToken(com.google.gson.reflect.TypeToken) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) Maps(java8.util.Maps) KEY_STOPPED(com.ichi2.anki.StudySettingActivity.KEY_STOPPED) DeckInfoListAdapter(com.ichi2.anki.widgets.DeckInfoListAdapter) Decks(com.ichi2.libanki.Decks) Gson(com.google.gson.Gson) Locale(java.util.Locale) Handler(android.os.Handler) DeckConfig(com.ichi2.libanki.DeckConfig) Map(java.util.Map) Fragment(androidx.fragment.app.Fragment) View(android.view.View) Button(android.widget.Button) TaskData(com.ichi2.async.TaskData) RecyclerView(androidx.recyclerview.widget.RecyclerView) REQUEST_BROWSE_CARDS(com.ichi2.anki.DeckPicker.REQUEST_BROWSE_CARDS) TabLayout(com.google.android.material.tabs.TabLayout) TYPE_LIFE(com.ichi2.libanki.stats.Stats.AxisType.TYPE_LIFE) SHOW_STUDYOPTIONS(com.ichi2.anki.DeckPicker.SHOW_STUDYOPTIONS) Set(java.util.Set) FragmentTransaction(androidx.fragment.app.FragmentTransaction) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) DeckPickerContextMenu(com.ichi2.anki.dialogs.DeckPickerContextMenu) List(java.util.List) Nullable(androidx.annotation.Nullable) TextView(android.widget.TextView) NextCardHandler(com.ichi2.anki.AbstractFlashcardViewer.NextCardHandler) ALL_DECKS_ID(com.ichi2.libanki.stats.Stats.ALL_DECKS_ID) CompatHelper(com.ichi2.compat.CompatHelper) Toolbar(androidx.appcompat.widget.Toolbar) RelativeLayout(android.widget.RelativeLayout) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Window(android.view.Window) Snackbar(com.google.android.material.snackbar.Snackbar) DialogFragment(androidx.fragment.app.DialogFragment) KEY_SHOW_TTS_ICON(com.ichi2.libanki.Consts.KEY_SHOW_TTS_ICON) Context(android.content.Context) TaskListener(com.ichi2.async.TaskListener) Dialog(android.app.Dialog) Intent(android.content.Intent) StepsPreference.convertToJSON(com.ichi2.preferences.StepsPreference.convertToJSON) HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) PixelFormat(android.graphics.PixelFormat) Editable(android.text.Editable) TypedArray(android.content.res.TypedArray) MenuItem(android.view.MenuItem) TASK_TYPE(com.ichi2.async.CollectionTask.TASK_TYPE) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) Menu(android.view.Menu) Utils(com.ichi2.libanki.Utils) AbstractDeckTreeNode(com.ichi2.libanki.sched.AbstractDeckTreeNode) Stats(com.ichi2.libanki.stats.Stats) LayoutInflater(android.view.LayoutInflater) CollectionTask(com.ichi2.async.CollectionTask) Color(android.graphics.Color) Gravity(android.view.Gravity) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) TreeMap(java.util.TreeMap) KEY_MIND_MODE(com.ichi2.anki.StudySettingActivity.KEY_MIND_MODE) CustomStudyDialog(com.ichi2.anki.dialogs.CustomStudyDialog) DeckPicker.fadeOut(com.ichi2.anki.DeckPicker.fadeOut) Activity(android.app.Activity) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) DeckPicker.fadeIn(com.ichi2.anki.DeckPicker.fadeIn) STUDY_SETTING(com.ichi2.anki.StudySettingActivity.STUDY_SETTING) EditText(android.widget.EditText) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) Resources(android.content.res.Resources) Intent(android.content.Intent) TaskData(com.ichi2.async.TaskData)

Example 7 with AnkiActivity

use of com.ichi2.anki.AnkiActivity in project AnkiChinaAndroid by ankichinateam.

the class ReadText method initializeTts.

public static String initializeTts(Context context, boolean vipSpeak, boolean showToast, ReadTextListener listener) {
    // Store weak reference to Activity to prevent memory leak
    mReviewer = new WeakReference<>(context);
    // Create new TTS object and setup its onInit Listener
    mTts = new TextToSpeech(context, status -> {
        if (status == TextToSpeech.SUCCESS) {
            // build list of available languages
            buildAvailableLanguages(TextToSpeech.LANG_AVAILABLE);
            if (availableTtsLocales.size() > 0) {
                // notify the reviewer that TTS has been initialized
                Timber.d("TTS initialized and available languages found");
                if (listener != null) {
                    listener.ttsInitialized();
                }
            // ((AbstractFlashcardViewer) mReviewer.get()).ttsInitialized();
            } else {
                if (showToast) {
                    Toast.makeText(mReviewer.get(), mReviewer.get().getString(R.string.no_tts_available_message), Toast.LENGTH_LONG).show();
                }
                Timber.w("TTS initialized but no available languages found");
            }
            mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {

                @Override
                public void onDone(String arg0) {
                    if (ReadText.sTextQueue.size() > 0) {
                        String[] text = ReadText.sTextQueue.remove(0);
                        ReadText.speak(text[0], text[1], TextToSpeech.QUEUE_FLUSH);
                    }
                    if (listener != null) {
                        listener.onDone();
                    }
                }

                @Override
                @Deprecated
                public void onError(String utteranceId) {
                    Timber.v("Andoid TTS failed. Check logcat for error. Indicates a problem with Android TTS engine.");
                    final Uri helpUrl = Uri.parse(mReviewer.get().getString(R.string.link_faq_tts));
                    final AnkiActivity ankiActivity = (AnkiActivity) mReviewer.get();
                    ankiActivity.mayOpenUrl(helpUrl);
                    if (showToast) {
                        UIUtils.showSnackbar(ankiActivity, R.string.no_tts_available_message, false, R.string.help, v -> openTtsHelpUrl(helpUrl), ankiActivity.findViewById(R.id.root_layout), new Snackbar.Callback());
                    }
                }

                @Override
                public void onStart(String arg0) {
                // no nothing
                }
            });
        } else {
            if (showToast) {
                Toast.makeText(mReviewer.get(), mReviewer.get().getString(R.string.no_tts_available_message), Toast.LENGTH_LONG).show();
            }
            Timber.w("TTS not successfully initialized");
        }
    });
    // Show toast that it's getting initialized, as it can take a while before the sound plays the first time
    if (showToast) {
        Toast.makeText(context, context.getString(R.string.initializing_tts), Toast.LENGTH_LONG).show();
    }
    return mTts.getDefaultEngine();
}
Also used : Context(android.content.Context) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) WindowManager(android.view.WindowManager) UtteranceProgressListener(android.speech.tts.UtteranceProgressListener) Compat(com.ichi2.compat.Compat) Timber(timber.log.Timber) ArrayList(java.util.ArrayList) TextToSpeech(android.speech.tts.TextToSpeech) Nullable(androidx.annotation.Nullable) Locale(java.util.Locale) Handler(android.os.Handler) Toast(android.widget.Toast) CompatHelper(com.ichi2.compat.CompatHelper) View(android.view.View) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) WeakReference(java.lang.ref.WeakReference) Snackbar(com.google.android.material.snackbar.Snackbar) VisibleForTesting(androidx.annotation.VisibleForTesting) Sound(com.ichi2.libanki.Sound) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) Resources(android.content.res.Resources) UtteranceProgressListener(android.speech.tts.UtteranceProgressListener) TextToSpeech(android.speech.tts.TextToSpeech) Uri(android.net.Uri) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 8 with AnkiActivity

use of com.ichi2.anki.AnkiActivity in project AnkiChinaAndroid by ankichinateam.

the class StudyOptionsFragment method showCustomStudyContextMenu.

/**
 * Show the context menu for the custom study options
 */
private void showCustomStudyContextMenu() {
    CustomStudyDialog d = CustomStudyDialog.newInstance(CustomStudyDialog.CONTEXT_MENU_STANDARD, getCol().getDecks().selected(), (CustomStudyDialog.CustomStudyListener) getActivity());
    ((AnkiActivity) getActivity()).showDialogFragment(d);
}
Also used : CustomStudyDialog(com.ichi2.anki.dialogs.CustomStudyDialog)

Example 9 with AnkiActivity

use of com.ichi2.anki.AnkiActivity in project AnkiChinaAndroid by ankichinateam.

the class AbstractFlashcardViewer method restoreCollectionPreferences.

protected void restoreCollectionPreferences() {
    // These are preferences we pull out of the collection instead of SharedPreferences
    try {
        mShowNextReviewTime = getCol().getConf().getBoolean("estTimes");
        // Dynamic don't have review options; attempt to get deck-specific auto-advance options
        // but be prepared to go with all default if it's a dynamic deck
        JSONObject revOptions = new JSONObject();
        long selectedDid = getCol().getDecks().selected();
        if (!getCol().getDecks().isDyn(selectedDid)) {
            revOptions = getCol().getDecks().confForDid(selectedDid).getJSONObject("rev");
        }
        mOptUseGeneralTimerSettings = revOptions.optBoolean("useGeneralTimeoutSettings", true);
        mOptUseTimer = revOptions.optBoolean("timeoutAnswer", false);
        mOptWaitAnswerSecond = revOptions.optInt("timeoutAnswerSeconds", 20);
        mOptWaitQuestionSecond = revOptions.optInt("timeoutQuestionSeconds", 60);
    } catch (JSONException e) {
        Timber.e(e, "Unable to restoreCollectionPreferences");
        throw new RuntimeException(e);
    } catch (NullPointerException npe) {
        // NPE on collection only happens if the Collection is broken, follow AnkiActivity example
        Intent deckPicker = new Intent(this, DeckPicker.class);
        // don't currently do anything with this
        deckPicker.putExtra("collectionLoadError", true);
        deckPicker.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivityWithAnimation(deckPicker, ActivityTransitionAnimation.LEFT);
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONException(com.ichi2.utils.JSONException) Intent(android.content.Intent)

Example 10 with AnkiActivity

use of com.ichi2.anki.AnkiActivity in project AnkiChinaAndroid by ankichinateam.

the class CustomStudyDialog method buildContextMenu.

/**
 * Build a context menu for custom study
 *
 * @param id
 * @return
 */
private MaterialDialog buildContextMenu(int id) {
    int[] listIds = getListIds(id);
    final boolean jumpToReviewer = getArguments().getBoolean("jumpToReviewer");
    return new MaterialDialog.Builder(this.getActivity()).title(R.string.custom_study).cancelable(true).itemsIds(listIds).items(ContextMenuHelper.getValuesFromKeys(getKeyValueMap(), listIds)).itemsCallback(new MaterialDialog.ListCallback() {

        @Override
        public void onSelection(MaterialDialog materialDialog, View view, int which, CharSequence charSequence) {
            AnkiActivity activity = getAnkiActivity();
            switch(view.getId()) {
                case DECK_OPTIONS:
                    {
                        // User asked to permanently change the deck options
                        Intent i = new Intent(activity, DeckOptions.class);
                        i.putExtra("did", getArguments().getLong("did"));
                        getActivity().startActivity(i);
                        break;
                    }
                case MORE_OPTIONS:
                    {
                        // User asked to see all custom study options
                        CustomStudyDialog d = CustomStudyDialog.newInstance(CONTEXT_MENU_STANDARD, getArguments().getLong("did"), jumpToReviewer, listener);
                        activity.showDialogFragment(d);
                        break;
                    }
                case CUSTOM_STUDY_TAGS:
                    {
                        /*
                                 * This 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.
                                 */
                        long currentDeck = getArguments().getLong("did");
                        TagsDialog dialogFragment = TagsDialog.newInstance(TagsDialog.TYPE_CUSTOM_STUDY_TAGS, new ArrayList<String>(), new ArrayList<>(activity.getCol().getTags().byDeck(currentDeck, true)));
                        dialogFragment.setTagsDialogListener(CustomStudyDialog.this::customStudyFromTags);
                        activity.showDialogFragment(dialogFragment);
                        break;
                    }
                default:
                    {
                        // User asked for a standard custom study option
                        CustomStudyDialog d = CustomStudyDialog.newInstance(view.getId(), getArguments().getLong("did"), jumpToReviewer, listener);
                        getAnkiActivity().showDialogFragment(d);
                    }
            }
        }
    }).build();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ArrayList(java.util.ArrayList) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) AnkiActivity(com.ichi2.anki.AnkiActivity) DeckOptions(com.ichi2.anki.DeckOptions)

Aggregations

MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)5 AnkiActivity (com.ichi2.anki.AnkiActivity)5 Intent (android.content.Intent)4 Resources (android.content.res.Resources)4 ArrayList (java.util.ArrayList)4 Bundle (android.os.Bundle)3 View (android.view.View)3 Timber (timber.log.Timber)3 Dialog (android.app.Dialog)2 Context (android.content.Context)2 Uri (android.net.Uri)2 Handler (android.os.Handler)2 TypedValue (android.util.TypedValue)2 WindowManager (android.view.WindowManager)2 TextView (android.widget.TextView)2 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 Snackbar (com.google.android.material.snackbar.Snackbar)2 CustomStudyDialog (com.ichi2.anki.dialogs.CustomStudyDialog)2 TaskListener (com.ichi2.async.TaskListener)2