Search in sources :

Example 1 with EntangledPair

use of ai.saiy.android.processing.EntangledPair in project Saiy-PS by brandall76.

the class BingTranslate method getResponse.

/**
 * Resolve the translation request and return the {@link Outcome}
 *
 * @return the created {@link Outcome}
 */
public Outcome getResponse() {
    final CommandTranslateValues ctv = (CommandTranslateValues) cr.getVariableData();
    final TranslationLanguageBing language = resolveLanguage(ctv.getLanguage());
    if (language != TranslationLanguageBing.AUTO_DETECT) {
        String translationRequest = ctv.getText();
        if (DEBUG) {
            MyLog.d(CLS_NAME, "language: " + language.name());
            MyLog.d(CLS_NAME, "request: " + translationRequest);
        }
        if (ClipboardHelper.isClipboard(mContext, translationRequest)) {
            try {
                Thread.sleep(CLIPBOARD_DELAY);
            } catch (final InterruptedException e) {
                if (DEBUG) {
                    MyLog.w(CLS_NAME, "InterruptedException");
                    e.printStackTrace();
                }
            }
            final Pair<Boolean, String> clipboardPair = ClipboardHelper.getClipboardContentPair(mContext, sl);
            if (clipboardPair.first) {
                translationRequest = clipboardPair.second;
            } else {
                outcome.setUtterance(clipboardPair.second);
                outcome.setOutcome(Outcome.FAILURE);
                return outcome;
            }
        }
        if (!tooLong(translationRequest)) {
            final Pair<Boolean, String> translationResult = execute(mContext, language, translationRequest);
            if (translationResult.first) {
                outcome.setUtterance(translationResult.second);
                outcome.setOutcome(Outcome.SUCCESS);
                final EntangledPair entangledPair = new EntangledPair(Position.TOAST_LONG, CC.COMMAND_TRANSLATE);
                entangledPair.setToastContent(translationResult.second);
                outcome.setEntangledPair(entangledPair);
                final Qubit qubit = new Qubit();
                qubit.setTranslatedText(translationResult.second);
                outcome.setQubit(qubit);
                outcome.setTTSLocale(language.getLocale());
            } else {
                outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate, PersonalityHelper.getUserNameOrNot(mContext)));
                outcome.setOutcome(Outcome.FAILURE);
                return outcome;
            }
        } else {
            outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate_length, PersonalityHelper.getUserNameOrNot(mContext)));
            outcome.setOutcome(Outcome.FAILURE);
            return outcome;
        }
    } else {
        outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate_unsupported, PersonalityHelper.getUserNameOrNot(mContext)));
        outcome.setOutcome(Outcome.FAILURE);
        return outcome;
    }
    return outcome;
}
Also used : CommandTranslateValues(ai.saiy.android.command.translate.CommandTranslateValues) Qubit(ai.saiy.android.processing.Qubit) EntangledPair(ai.saiy.android.processing.EntangledPair) UtilsString(ai.saiy.android.utils.UtilsString)

Example 2 with EntangledPair

use of ai.saiy.android.processing.EntangledPair in project Saiy-PS by brandall76.

the class GoogleTranslate method getResponse.

/**
 * Resolve the translation request and return the {@link Outcome}
 *
 * @return the created {@link Outcome}
 */
public Outcome getResponse() {
    final CommandTranslateValues ctv = (CommandTranslateValues) cr.getVariableData();
    final TranslationLanguageGoogle language = resolveLanguage(ctv.getLanguage());
    if (language != TranslationLanguageGoogle.AUTO_DETECT) {
        String translationRequest = ctv.getText();
        if (DEBUG) {
            MyLog.d(CLS_NAME, "language: " + language.name());
            MyLog.d(CLS_NAME, "request: " + translationRequest);
        }
        if (ClipboardHelper.isClipboard(mContext, translationRequest)) {
            try {
                Thread.sleep(CLIPBOARD_DELAY);
            } catch (final InterruptedException e) {
                if (DEBUG) {
                    MyLog.w(CLS_NAME, "InterruptedException");
                    e.printStackTrace();
                }
            }
            final Pair<Boolean, String> clipboardPair = ClipboardHelper.getClipboardContentPair(mContext, sl);
            if (clipboardPair.first) {
                translationRequest = clipboardPair.second;
            } else {
                outcome.setUtterance(clipboardPair.second);
                outcome.setOutcome(Outcome.FAILURE);
                return outcome;
            }
        }
        if (!tooLong(translationRequest)) {
            final Pair<Boolean, String> translationResult = execute(mContext, language, translationRequest);
            if (translationResult.first) {
                outcome.setUtterance(translationResult.second);
                outcome.setOutcome(Outcome.SUCCESS);
                final EntangledPair entangledPair = new EntangledPair(Position.TOAST_LONG, CC.COMMAND_TRANSLATE);
                entangledPair.setToastContent(translationResult.second);
                outcome.setEntangledPair(entangledPair);
                final Qubit qubit = new Qubit();
                qubit.setTranslatedText(translationResult.second);
                outcome.setQubit(qubit);
                outcome.setTTSLocale(UtilsLocale.stringToLocale(language.getLanguage()));
            } else {
                outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate, PersonalityHelper.getUserNameOrNot(mContext)));
                outcome.setOutcome(Outcome.FAILURE);
                return outcome;
            }
        } else {
            outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate_length, PersonalityHelper.getUserNameOrNot(mContext)));
            outcome.setOutcome(Outcome.FAILURE);
            return outcome;
        }
    } else {
        outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate_unsupported, PersonalityHelper.getUserNameOrNot(mContext)));
        outcome.setOutcome(Outcome.FAILURE);
        return outcome;
    }
    return outcome;
}
Also used : CommandTranslateValues(ai.saiy.android.command.translate.CommandTranslateValues) Qubit(ai.saiy.android.processing.Qubit) EntangledPair(ai.saiy.android.processing.EntangledPair) UtilsString(ai.saiy.android.utils.UtilsString)

Aggregations

CommandTranslateValues (ai.saiy.android.command.translate.CommandTranslateValues)2 EntangledPair (ai.saiy.android.processing.EntangledPair)2 Qubit (ai.saiy.android.processing.Qubit)2 UtilsString (ai.saiy.android.utils.UtilsString)2