Search in sources :

Example 16 with Reviewer

use of com.ichi2.anki.Reviewer 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 17 with Reviewer

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

the class ReviewerTest method testMultipleCards.

@Test
public synchronized void testMultipleCards() throws ConfirmModSchemaException, InterruptedException {
    addNoteWithThreeCards();
    Collection col = getCol();
    JSONObject nw = col.getDecks().confForDid(1).getJSONObject("new");
    MockTime time = getCollectionTime();
    nw.put("delays", new JSONArray(new int[] { 1, 10, 60, 120 }));
    waitForAsyncTasksToComplete();
    Reviewer reviewer = startReviewer();
    waitForAsyncTasksToComplete();
    assertCounts(reviewer, 3, 0, 0);
    // card 1 is shown
    answerCardOrdinalAsGood(reviewer, 1);
    // card get scheduler in [10, 12.5] minutes
    time.addM(3);
    // We wait 3 minutes to ensure card 2 is scheduled after card 1
    // card 2 is shown
    answerCardOrdinalAsGood(reviewer, 2);
    // Same as above
    time.addM(3);
    // card 3 is shown
    answerCardOrdinalAsGood(reviewer, 3);
    undo(reviewer);
    assertCurrentOrdIs(reviewer, 3);
    // card 3 is shown
    answerCardOrdinalAsGood(reviewer, 3);
    // Anki Desktop shows "1"
    assertCurrentOrdIsNot(reviewer, 3);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) MockTime(com.ichi2.testutils.MockTime) Test(org.junit.Test)

Example 18 with Reviewer

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

the class SchedV2 method getCard.

/**
 * Pop the next card from the queue. null if finished.
 */
@Nullable
public Card getCard() {
    _checkDay();
    // check day deal with cutoff if required. No need to do it in resets
    if (!mHaveCounts) {
        resetCounts(false);
    }
    if (!mHaveQueues) {
        resetQueues(false);
    }
    @Nullable Card card = _getCard();
    if (card != null) {
        mCol.log(card);
        incrReps();
        // In upstream, counts are decremented when the card is
        // gotten; i.e. in _getLrnCard, _getRevCard and
        // _getNewCard. This can not be done anymore since we use
        // those methods to pre-fetch the next card. Instead we
        // decrement the counts here, when the card is returned to
        // the reviewer.
        decrementCounts(card);
        setCurrentCard(card);
        card.startTimer();
    } else {
        discardCurrentCard();
    }
    return card;
}
Also used : Nullable(androidx.annotation.Nullable) Card(com.ichi2.libanki.Card) Nullable(androidx.annotation.Nullable)

Example 19 with Reviewer

use of com.ichi2.anki.Reviewer in project Anki-Android by ankidroid.

the class SchedV2 method counts.

/**
 * Same as counts(), but also count `card`. In practice, we use it because `card` is in the reviewer and that is the
 * number we actually want.
 * Overridden: left / 1000 in V1
 */
@NonNull
public Counts counts(@NonNull Card card) {
    Counts counts = counts();
    Queue idx = countIdx(card);
    counts.changeCount(idx, 1);
    return counts;
}
Also used : Queue(com.ichi2.libanki.sched.Counts.Queue) NonNull(androidx.annotation.NonNull)

Example 20 with Reviewer

use of com.ichi2.anki.Reviewer in project Anki-Android by ankidroid.

the class SchedV2 method getCard.

/**
 * Pop the next card from the queue. null if finished.
 */
@Nullable
public Card getCard() {
    _checkDay();
    if (!mHaveQueues) {
        resetQueues(false);
    }
    @Nullable Card card = _getCard();
    if (card == null && !mHaveCounts) {
        // maybe we didn't refill queues because counts were not
        // set. This could only occur if the only card is a buried
        // sibling. So let's try to set counts and check again.
        reset();
        card = _getCard();
    }
    if (card != null) {
        mCol.log(card);
        incrReps();
        // In upstream, counts are decremented when the card is
        // gotten; i.e. in _getLrnCard, _getRevCard and
        // _getNewCard. This can not be done anymore since we use
        // those methods to pre-fetch the next card. Instead we
        // decrement the counts here, when the card is returned to
        // the reviewer.
        decrementCounts(card);
        setCurrentCard(card);
        card.startTimer();
    } else {
        discardCurrentCard();
    }
    if (!mHaveCounts) {
        // Need to reset queues once counts are reset
        TaskManager.launchCollectionTask(new CollectionTask.Reset());
    }
    return card;
}
Also used : CollectionTask(com.ichi2.async.CollectionTask) Nullable(androidx.annotation.Nullable) Card(com.ichi2.libanki.Card) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)11 Collection (com.ichi2.libanki.Collection)10 Card (com.ichi2.libanki.Card)8 JSONObject (com.ichi2.utils.JSONObject)5 Intent (android.content.Intent)4 NonNull (androidx.annotation.NonNull)4 Nullable (androidx.annotation.Nullable)4 Model (com.ichi2.libanki.Model)4 Note (com.ichi2.libanki.Note)4 JSONArray (com.ichi2.utils.JSONArray)4 Resources (android.content.res.Resources)3 View (android.view.View)3 WindowManager (android.view.WindowManager)3 VisibleForTesting (androidx.annotation.VisibleForTesting)3 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)3 TaskData (com.ichi2.async.TaskData)3 ArrayList (java.util.ArrayList)3 Context (android.content.Context)2 SharedPreferences (android.content.SharedPreferences)2 Uri (android.net.Uri)2