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();
}
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);
}
};
}
};
}
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;
}
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;
}
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;
}
Aggregations