Search in sources :

Example 1 with RuleManager

use of ee.ioc.phon.android.speechutils.editor.RuleManager 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)

Example 2 with RuleManager

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

the class SpeechInputMethodService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    Log.i("onCreate");
    mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    mCommandEditor = new InputConnectionCommandEditor(getApplicationContext());
    mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    mRes = getResources();
    mRuleManager = new RuleManager();
    Rewrites rewritesClip = new Rewrites(mPrefs, mRes, REWRITES_NAME_CLIP);
    if (rewritesClip.isSelected()) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // TODO: remove the listener onFinish
        clipboard.addPrimaryClipChangedListener(() -> {
            ClipData clipData = clipboard.getPrimaryClip();
            if (clipData != null) {
                String clip = clipData.getItemAt(0).getText().toString();
                // Empty strings make less sense as clips
                if (!clip.isEmpty()) {
                    UtteranceRewriter ur = mRuleManager.addRecent(clip, rewritesClip.getRewrites());
                    PreferenceUtils.putPrefMapEntry(mPrefs, mRes, R.string.keyRewritesMap, REWRITES_NAME_CLIP, ur.toTsv());
                    mCommandEditor.setRewriters(Utils.makeList(Utils.genRewriters(mPrefs, mRes, null, mRuleManager.getCommandMatcher())));
                }
            }
        });
    }
}
Also used : ClipboardManager(android.content.ClipboardManager) RuleManager(ee.ioc.phon.android.speechutils.editor.RuleManager) Rewrites(ee.ioc.phon.android.speak.model.Rewrites) UtteranceRewriter(ee.ioc.phon.android.speechutils.editor.UtteranceRewriter) InputConnectionCommandEditor(ee.ioc.phon.android.speechutils.editor.InputConnectionCommandEditor) ClipData(android.content.ClipData)

Aggregations

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