Search in sources :

Example 1 with CommandMatcher

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

Aggregations

Paint (android.graphics.Paint)1 SpannableString (android.text.SpannableString)1 ExecutableString (ee.ioc.phon.android.speak.ExecutableString)1 CommandMatcher (ee.ioc.phon.android.speechutils.editor.CommandMatcher)1 UtteranceRewriter (ee.ioc.phon.android.speechutils.editor.UtteranceRewriter)1 Iterator (java.util.Iterator)1