Search in sources :

Example 1 with Op

use of ee.ioc.phon.android.speechutils.editor.Op in project K6nele by Kaljurand.

the class SpeechInputMethodService method getSpeechInputViewListener.

private SpeechInputView.SpeechInputViewListener getSpeechInputViewListener(final String packageName) {
    return new AbstractSpeechInputViewListener() {

        // TODO: quick hack to add app to the matcher, not sure if we can access the
        // class name of the app
        private ComponentName app = new ComponentName(packageName, packageName);

        private void runOp(Op op) {
            mCommandEditor.runOp(op, false);
        }

        @Override
        public void onComboChange(String language, ComponentName service) {
            // TODO: name of the rewrites table configurable
            mCommandEditor.setRewriters(Utils.makeList(Utils.genRewriters(mPrefs, mRes, null, language, service, app)));
        }

        @Override
        public void onPartialResult(List<String> results) {
            if (mShowPartialResults) {
                mCommandEditor.commitPartialResult(getText(results));
            }
        }

        @Override
        public void onFinalResult(List<String> results, Bundle bundle) {
            CommandEditorResult editorResult = mCommandEditor.commitFinalResult(getText(results));
            if (editorResult != null && mInputView != null && editorResult.isCommand()) {
                mInputView.showMessage(editorResult.ppCommand(), editorResult.isSuccess());
            }
        }

        @Override
        public void onSwitchIme(boolean isAskUser) {
            switchIme(isAskUser);
        }

        @Override
        public void onSwitchToLastIme() {
            switchToLastIme();
        }

        @Override
        public void onAction(int editorAction, boolean hide) {
            if (hide) {
                closeSession();
            }
            runOp(mCommandEditor.imeAction(editorAction));
            if (hide) {
                requestHideSelf(0);
            }
        }

        @Override
        public void onDeleteLeftChar() {
            runOp(mCommandEditor.deleteLeftChars(1));
        }

        @Override
        public void onDeleteLastWord() {
            runOp(mCommandEditor.deleteLeftWord());
            // Show all of the current text. (Only on Watch.)
            if (getResources().getBoolean(R.bool.isWatch)) {
                mInputView.showMessage(mCommandEditor.getText());
            }
        }

        @Override
        public void onAddNewline() {
            runOp(mCommandEditor.replaceSel("\n"));
        }

        @Override
        public void goUp() {
            runOp(mCommandEditor.keyUp());
        }

        @Override
        public void goDown() {
            runOp(mCommandEditor.keyDown());
        }

        @Override
        public void moveRel(int numOfChars) {
            runOp(mCommandEditor.moveRel(numOfChars));
        }

        @Override
        public void moveRelSel(int numOfChars, int type) {
            runOp(mCommandEditor.moveRelSel(numOfChars, type));
        }

        @Override
        public void onExtendSel(String regex) {
            runOp(mCommandEditor.selectRe(regex, false));
        }

        @Override
        public void onAddSpace() {
            runOp(mCommandEditor.replaceSel(" "));
        }

        @Override
        public void onSelectAll() {
            // TODO: show ContextMenu
            runOp(mCommandEditor.selectAll());
        }

        @Override
        public void onReset() {
            // TODO: hide ContextMenu (if visible)
            runOp(mCommandEditor.moveRel(0));
        }

        @Override
        public void onStartListening() {
            Log.i("IME: onStartListening");
            mCommandEditor.reset();
        }

        @Override
        public void onStopListening() {
            Log.i("IME: onStopListening");
        }

        // TODO: add onCancel()
        @Override
        public void onError(int errorCode) {
            Log.i("IME: onError: " + errorCode);
            if (errorCode == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
                Intent intent = new Intent(SpeechInputMethodService.this, PermissionsRequesterActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                SpeechInputMethodService.this.startActivity(intent);
            }
        }
    };
}
Also used : Op(ee.ioc.phon.android.speechutils.editor.Op) Bundle(android.os.Bundle) AbstractSpeechInputViewListener(ee.ioc.phon.android.speak.view.AbstractSpeechInputViewListener) ComponentName(android.content.ComponentName) List(java.util.List) Intent(android.content.Intent) CommandEditorResult(ee.ioc.phon.android.speechutils.editor.CommandEditorResult)

Example 2 with Op

use of ee.ioc.phon.android.speechutils.editor.Op in project K6nele by Kaljurand.

the class SpeechInputMethodService method getSpeechInputViewListener.

private SpeechInputView.SpeechInputViewListener getSpeechInputViewListener(final Window window, final ComponentName app, RuleManager ruleManager) {
    return new AbstractSpeechInputViewListener() {

        final ComponentName mApp = app;

        final RuleManager mRuleManager = ruleManager;

        private void runOp(Op op) {
            mCommandEditor.runOp(op, false);
        }

        private void setKeepScreenOn(boolean b) {
            if (window != null) {
                if (b) {
                    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                } else {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                }
            }
        }

        private void commitResults(List<String> results) {
            String text = getText(results);
            CommandEditorResult editorResult = mCommandEditor.commitFinalResult(text);
            if (editorResult != null && mInputView != null && editorResult.isCommand()) {
                mInputView.showMessage(editorResult.getRewrite().ppCommand(), editorResult.isSuccess());
            }
            if (editorResult != null && mFlagPersonalizedLearning) {
                boolean isSelected = false;
                Rewrites rewritesRec = new Rewrites(mPrefs, mRes, REWRITES_NAME_RECENT);
                if (rewritesRec.isSelected()) {
                    isSelected = true;
                    UtteranceRewriter ur = mRuleManager.addRecent(editorResult, rewritesRec.getRewrites());
                    PreferenceUtils.putPrefMapEntry(mPrefs, mRes, R.string.keyRewritesMap, REWRITES_NAME_RECENT, ur.toTsv());
                }
                Rewrites rewritesFreq = new Rewrites(mPrefs, mRes, REWRITES_NAME_FREQUENT);
                if (rewritesFreq.isSelected()) {
                    isSelected = true;
                    UtteranceRewriter ur = mRuleManager.addFrequent(editorResult, rewritesFreq.getRewrites());
                    PreferenceUtils.putPrefMapEntry(mPrefs, mRes, R.string.keyRewritesMap, REWRITES_NAME_FREQUENT, ur.toTsv());
                }
                // Update rewriters because the tables have changed
                if (isSelected) {
                    mCommandEditor.setRewriters(Utils.makeList(Utils.genRewriters(mPrefs, mRes, null, mRuleManager.getCommandMatcher())));
                }
            }
        }

        @Override
        public void onComboChange(String language, ComponentName service) {
            mRuleManager.setMatchers(language, service, mApp);
            // TODO: name of the rewrites table configurable
            mCommandEditor.setRewriters(Utils.makeList(Utils.genRewriters(mPrefs, mRes, null, mRuleManager.getCommandMatcher())));
        }

        @Override
        public void onPartialResult(List<String> results, boolean isSemiFinal) {
            if (isSemiFinal) {
                commitResults(results);
            } else {
                if (mShowPartialResults) {
                    mCommandEditor.commitPartialResult(getText(results));
                }
            }
        }

        @Override
        public void onFinalResult(List<String> results, Bundle bundle) {
            commitResults(results);
            setKeepScreenOn(false);
        }

        @Override
        public void onCommand(String text) {
            Op op = mCommandEditor.getOpOrNull(text, false);
            if (op == null) {
                // TODO: maybe just clear the status bar
                if (mInputView != null) {
                    mInputView.showMessage(text, false);
                }
            } else {
                boolean success = mCommandEditor.runOp(op);
                if (mInputView != null) {
                    // TODO: show executed command or replacement text, not op.toString()
                    mInputView.showMessage(op.toString(), success);
                }
                setKeepScreenOn(false);
            }
        }

        @Override
        public void onSwitchIme(boolean isAskUser) {
            switchIme(isAskUser);
        }

        @Override
        public void onSwitchToLastIme() {
            switchToLastIme();
        }

        @Override
        public void onAction(int editorAction, boolean hide) {
            if (hide) {
                closeSession();
            }
            if (editorAction > 0) {
                runOp(mCommandEditor.imeAction(editorAction));
            }
            if (hide) {
                requestHideSelf(0);
            }
        }

        @Override
        public void onDeleteLeftChar() {
            // TODO: indicate somehow (e.g. vibration, different background color) that the Op failed
            runOp(mCommandEditor.deleteChars(-1));
        // TODO: might be better, i.e. able to delete non-text (checkboxes), but not undoable
        // runOp(mCommandEditor.keyCode(KeyEvent.KEYCODE_DEL));
        }

        @Override
        public void onDeleteLastWord() {
            runOp(mCommandEditor.deleteLeftWord());
        }

        @Override
        public void onAddNewline() {
            runOp(mCommandEditor.replaceSel("\n"));
        }

        @Override
        public void goUp() {
            runOp(mCommandEditor.keyUp());
        }

        @Override
        public void goDown() {
            runOp(mCommandEditor.keyDown());
        }

        @Override
        public void moveRel(int numOfChars) {
            runOp(mCommandEditor.moveRel(numOfChars));
        }

        @Override
        public void moveRelSel(int numOfChars, int type) {
            runOp(mCommandEditor.moveRelSel(numOfChars, type));
        }

        @Override
        public void onExtendSel(String regex) {
            runOp(mCommandEditor.selectRe(regex, false));
        }

        @Override
        public void onAddSpace() {
            runOp(mCommandEditor.replaceSel(" "));
        }

        @Override
        public void onSelectAll() {
            // TODO: show ContextMenu
            runOp(mCommandEditor.selectAll());
        }

        @Override
        public void onReset() {
            // TODO: hide ContextMenu (if visible)
            runOp(mCommandEditor.moveRel(0));
        }

        @Override
        public void onStartListening() {
            Log.i("IME: onStartListening");
            mCommandEditor.reset();
            setKeepScreenOn(true);
        }

        @Override
        public void onStopListening() {
            Log.i("IME: onStopListening");
            setKeepScreenOn(false);
        }

        // TODO: add onCancel()
        @Override
        public void onError(int errorCode) {
            setKeepScreenOn(false);
            Log.i("IME: onError: " + errorCode);
            if (errorCode == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
                Intent intent = new Intent(SpeechInputMethodService.this, PermissionsRequesterActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                SpeechInputMethodService.this.startActivity(intent);
            }
        }
    };
}
Also used : Op(ee.ioc.phon.android.speechutils.editor.Op) Bundle(android.os.Bundle) AbstractSpeechInputViewListener(ee.ioc.phon.android.speak.view.AbstractSpeechInputViewListener) RuleManager(ee.ioc.phon.android.speechutils.editor.RuleManager) Rewrites(ee.ioc.phon.android.speak.model.Rewrites) UtteranceRewriter(ee.ioc.phon.android.speechutils.editor.UtteranceRewriter) ComponentName(android.content.ComponentName) List(java.util.List) Intent(android.content.Intent) CommandEditorResult(ee.ioc.phon.android.speechutils.editor.CommandEditorResult)

Aggregations

ComponentName (android.content.ComponentName)2 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 AbstractSpeechInputViewListener (ee.ioc.phon.android.speak.view.AbstractSpeechInputViewListener)2 CommandEditorResult (ee.ioc.phon.android.speechutils.editor.CommandEditorResult)2 Op (ee.ioc.phon.android.speechutils.editor.Op)2 List (java.util.List)2 Rewrites (ee.ioc.phon.android.speak.model.Rewrites)1 RuleManager (ee.ioc.phon.android.speechutils.editor.RuleManager)1 UtteranceRewriter (ee.ioc.phon.android.speechutils.editor.UtteranceRewriter)1