Search in sources :

Example 1 with UtteranceRewriter

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

the class Rewrites method getRules.

public String[] getRules() {
    String rewrites = PreferenceUtils.getPrefMapEntry(mPrefs, mRes, R.string.keyRewritesMap, mId);
    UtteranceRewriter ur = new UtteranceRewriter(rewrites);
    return ur.toStringArray();
}
Also used : UtteranceRewriter(ee.ioc.phon.android.speechutils.editor.UtteranceRewriter)

Example 2 with UtteranceRewriter

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

the class Utils method genRewriters.

/**
 * Generates rewriters based on the list of names of rewrite tables.
 * If a name does not resolve to a rewrite table then generates null.
 * If the given list is null, then the default rewriter is returned (currently at most one).
 * Passing an empty list effectively turns off rewriting.
 */
public static Iterable<UtteranceRewriter> genRewriters(final SharedPreferences prefs, final Resources resources, String[] rewritesByName, String language, ComponentName service, ComponentName app) {
    final String[] names;
    if (rewritesByName == null) {
        Set<String> defaults = PreferenceUtils.getPrefStringSet(prefs, resources, R.string.defaultRewriteTables);
        if (defaults.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        names = defaults.toArray(new String[defaults.size()]);
        // TODO: defaults should be a list (not a set that needs to be sorted)
        Arrays.sort(names);
    } else {
        names = rewritesByName;
    }
    final int length = names.length;
    if (length == 0) {
        return Collections.EMPTY_LIST;
    }
    final CommandMatcher commandMatcher = CommandMatcherFactory.createCommandFilter(language, service, app);
    return new Iterable<UtteranceRewriter>() {

        @Override
        public Iterator<UtteranceRewriter> iterator() {
            return new Iterator<UtteranceRewriter>() {

                private int mCurrent = 0;

                @Override
                public boolean hasNext() {
                    return mCurrent < length;
                }

                @Override
                public UtteranceRewriter next() {
                    String rewritesAsStr = PreferenceUtils.getPrefMapEntry(prefs, resources, R.string.keyRewritesMap, names[mCurrent++]);
                    if (rewritesAsStr == null) {
                        return null;
                    }
                    return new UtteranceRewriter(rewritesAsStr, commandMatcher);
                }
            };
        }
    };
}
Also used : CommandMatcher(ee.ioc.phon.android.speechutils.editor.CommandMatcher) Iterator(java.util.Iterator) UtteranceRewriter(ee.ioc.phon.android.speechutils.editor.UtteranceRewriter) ExecutableString(ee.ioc.phon.android.speak.ExecutableString) SpannableString(android.text.SpannableString) Paint(android.graphics.Paint)

Example 3 with UtteranceRewriter

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

the class IntentUtils method rewriteResultWithExtras.

public static String rewriteResultWithExtras(Context context, Bundle extras, String result) {
    String defaultResultUtterance = null;
    String defaultResultCommand = null;
    String defaultResultArg1 = null;
    if (extras.getBoolean(Extras.EXTRA_RESULT_LAUNCH_AS_ACTIVITY)) {
        defaultResultUtterance = "(.+)";
        defaultResultCommand = "activity";
        defaultResultArg1 = "$1";
    }
    String resultUtterance = extras.getString(Extras.EXTRA_RESULT_UTTERANCE, defaultResultUtterance);
    if (resultUtterance != null) {
        String resultReplacement = extras.getString(Extras.EXTRA_RESULT_REPLACEMENT, null);
        String resultCommand = extras.getString(Extras.EXTRA_RESULT_COMMAND, defaultResultCommand);
        String resultArg1 = extras.getString(Extras.EXTRA_RESULT_ARG1, defaultResultArg1);
        String resultArg2 = extras.getString(Extras.EXTRA_RESULT_ARG2, null);
        String[] resultArgs;
        if (resultArg1 == null) {
            resultArgs = null;
        } else if (resultArg2 == null) {
            resultArgs = new String[] { resultArg1 };
        } else {
            resultArgs = new String[] { resultArg1, resultArg2 };
        }
        List<Command> commands = new ArrayList<>();
        commands.add(new Command(resultUtterance, resultReplacement, resultCommand, resultArgs));
        result = launchIfIntent(context, new UtteranceRewriter(commands), result);
    }
    if (result != null) {
        String rewritesAsStr = extras.getString(Extras.EXTRA_RESULT_REWRITES_AS_STR, null);
        if (rewritesAsStr != null) {
            result = launchIfIntent(context, new UtteranceRewriter(rewritesAsStr), result);
        }
    }
    return result;
}
Also used : Command(ee.ioc.phon.android.speechutils.editor.Command) ArrayList(java.util.ArrayList) UtteranceRewriter(ee.ioc.phon.android.speechutils.editor.UtteranceRewriter)

Example 4 with UtteranceRewriter

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

the class IntentUtils method launchIfIntent.

/**
 * Rewrites the text. If the result is a command then executes it (only "activity" is currently
 * supported). Otherwise returns the rewritten string.
 * Errors that occur during the execution of "activity" are communicated via toasts.
 * The possible errors are: syntax error in JSON, nobody responded to the intent, no permission to launch
 * the intent.
 */
public static String launchIfIntent(Context context, Iterable<UtteranceRewriter> urs, String text) {
    String newText = text;
    for (UtteranceRewriter ur : urs) {
        // Skip null, i.e. a case where a rewrites name did not resolve to a table.
        if (ur == null) {
            continue;
        }
        UtteranceRewriter.Rewrite rewrite = ur.getRewrite(newText);
        if (rewrite.isCommand() && rewrite.mArgs != null && rewrite.mArgs.length > 0) {
            // There can be other commands in the future.
            try {
                Intent intent = JsonUtils.createIntent(rewrite.mArgs[0]);
                switch(rewrite.mId) {
                    case "activity":
                        startActivityIfAvailable(context, intent);
                        break;
                    case "service":
                        // TODO
                        break;
                    case "broadcast":
                        // TODO
                        break;
                    default:
                        break;
                }
            } catch (JSONException e) {
                Log.i("launchIfIntent: JSON: " + e.getMessage());
                showMessage(context, e.getLocalizedMessage());
            }
            return null;
        }
        newText = rewrite.mStr;
    }
    return newText;
}
Also used : UtteranceRewriter(ee.ioc.phon.android.speechutils.editor.UtteranceRewriter) JSONException(org.json.JSONException) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RecognizerIntent(android.speech.RecognizerIntent)

Example 5 with UtteranceRewriter

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

the class Rewrites method getIntentSendBase64.

public Intent getIntentSendBase64() {
    UtteranceRewriter ur = new UtteranceRewriter(getRewrites());
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_SUBJECT, mId);
    intent.putExtra(Intent.EXTRA_TEXT, "k6://" + Base64.encodeToString(ur.toTsv().getBytes(), Base64.NO_WRAP | Base64.URL_SAFE));
    intent.setType("text/plain");
    return intent;
}
Also used : UtteranceRewriter(ee.ioc.phon.android.speechutils.editor.UtteranceRewriter) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent)

Aggregations

UtteranceRewriter (ee.ioc.phon.android.speechutils.editor.UtteranceRewriter)11 Intent (android.content.Intent)5 RecognizerIntent (android.speech.RecognizerIntent)3 Bundle (android.os.Bundle)2 SpannableString (android.text.SpannableString)2 ExecutableString (ee.ioc.phon.android.speak.ExecutableString)2 Rewrites (ee.ioc.phon.android.speak.model.Rewrites)2 RuleManager (ee.ioc.phon.android.speechutils.editor.RuleManager)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 PendingIntent (android.app.PendingIntent)1 ClipData (android.content.ClipData)1 ClipboardManager (android.content.ClipboardManager)1 ComponentName (android.content.ComponentName)1 SharedPreferences (android.content.SharedPreferences)1 Resources (android.content.res.Resources)1 Paint (android.graphics.Paint)1 Uri (android.net.Uri)1 ArrayAdapter (android.widget.ArrayAdapter)1 AbstractSpeechInputViewListener (ee.ioc.phon.android.speak.view.AbstractSpeechInputViewListener)1