Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dependencies {
implementation project(':speechutils:app')
// TODO: use Android's JSON library instead
implementation 'com.googlecode.json-simple:json-simple:1.1'
implementation 'com.koushikdutta.async:androidasync:2.2.1'
implementation 'com.koushikdutta.async:androidasync:3.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.preference:preference:1.1.1'
Expand All @@ -26,8 +26,8 @@ android {
applicationId 'ee.ioc.phon.android.speak'
minSdkVersion 16
targetSdkVersion 30
versionCode 1750
versionName '1.7.50'
versionCode 1751
versionName '1.7.51'
vectorDrawables.useSupportLibrary = true
// Keep only en and et resources
resConfigs "en", "et"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ public void onCreate() {
mRes = getResources();
mRuleManager = new RuleManager();

String rewritesClip = getRewrites(REWRITES_NAME_CLIP);
if (rewritesClip != null) {
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(() -> {
CharSequence clip = clipboard.getPrimaryClip().getItemAt(0).getText();
if (clip != null) {
UtteranceRewriter ur = mRuleManager.addRecent(clip.toString(), rewritesClip);
UtteranceRewriter ur = mRuleManager.addRecent(clip.toString(), rewritesClip.getRewrites());
PreferenceUtils.putPrefMapEntry(mPrefs, mRes, R.string.keyRewritesMap, REWRITES_NAME_CLIP, ur.toTsv());
mCommandEditor.setRewriters(
Utils.makeList(
Expand Down Expand Up @@ -218,11 +218,6 @@ public void onStartInputView(EditorInfo editorInfo, boolean restarting) {
}
}

private String getRewrites(String name) {
Rewrites rewrites = new Rewrites(mPrefs, mRes, name);
return rewrites.getRewrites();
}

/**
* Called when the input view is being hidden from the user.
* This will be called either prior to hiding the window,
Expand Down Expand Up @@ -336,8 +331,8 @@ private static Bundle makeExtras() {
private SpeechInputView.SpeechInputViewListener getSpeechInputViewListener(final Window window, final ComponentName app, RuleManager ruleManager) {
return new AbstractSpeechInputViewListener() {

ComponentName mApp = app;
RuleManager mRuleManager = ruleManager;
final ComponentName mApp = app;
final RuleManager mRuleManager = ruleManager;

private void runOp(Op op) {
mCommandEditor.runOp(op, false);
Expand All @@ -360,18 +355,18 @@ private void commitResults(List<String> results) {
mInputView.showMessage(editorResult.getRewrite().ppCommand(), editorResult.isSuccess());
}
if (editorResult != null && mFlagPersonalizedLearning) {
String rewritesRec = getRewrites(REWRITES_NAME_RECENT);
if (rewritesRec != null) {
UtteranceRewriter ur = mRuleManager.addRecent(editorResult, rewritesRec);
Rewrites rewritesRec = new Rewrites(mPrefs, mRes, REWRITES_NAME_RECENT);
if (rewritesRec.isSelected()) {
UtteranceRewriter ur = mRuleManager.addRecent(editorResult, rewritesRec.getRewrites());
PreferenceUtils.putPrefMapEntry(mPrefs, mRes, R.string.keyRewritesMap, REWRITES_NAME_RECENT, ur.toTsv());
}
String rewritesFreq = getRewrites(REWRITES_NAME_FREQUENT);
if (rewritesFreq != null) {
UtteranceRewriter ur = mRuleManager.addFrequent(editorResult, rewritesFreq);
Rewrites rewritesFreq = new Rewrites(mPrefs, mRes, REWRITES_NAME_FREQUENT);
if (rewritesFreq.isSelected()) {
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 (rewritesRec != null || rewritesFreq != null) {
if (rewritesRec.isSelected() || rewritesFreq.isSelected()) {
mCommandEditor.setRewriters(
Utils.makeList(
Utils.genRewriters(mPrefs, mRes, null, mRuleManager.getCommandMatcher())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public AdaptationState parseAdaptationState() throws WebSocketResponseException
public static class Result {
private final JSONObject mResult;

public Result(JSONObject result) throws JSONException {
public Result(JSONObject result) {
mResult = result;
}

Expand Down Expand Up @@ -124,7 +124,7 @@ public boolean isFinal() {
public static class Message {
private final String mMessage;

public Message(String message) throws JSONException {
public Message(String message) {
mMessage = message;
}

Expand All @@ -135,7 +135,7 @@ public String getMessage() {


public static class AdaptationState {
public AdaptationState(JSONObject result) throws JSONException {
public AdaptationState(JSONObject result) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -56,10 +55,6 @@ public class SpeechInputView extends LinearLayoutCompat {

private static final String[] EMPTY_STRING_ARRAY = {};

// TODO: get the colors from speechutils
private static final int COLOR_RECORDING = Color.argb(255, 204, 0, 0);
private static final int COLOR_TRANSCRIBING = Color.argb(255, 153, 51, 204);

private View mCentralButtons;
private MicButton mBImeStartStop;
private ImageButton mBImeKeyboard;
Expand Down Expand Up @@ -94,6 +89,7 @@ public class SpeechInputView extends LinearLayoutCompat {
private final static String DASH_CUR = "――――――――――――――――――――";
private final static String DASH_SEL = "■■■■■■■■■■■■■■■■■■■■";
private final static int DASH_LENGTH = DASH_CUR.length();
private final static String NEW_TAB_LABEL = "+";

public interface SpeechInputViewListener {

Expand Down Expand Up @@ -621,8 +617,14 @@ private void updateClipboard(Context context, String language, ComponentName ser
@Override
public void onTabSelected(TabLayout.Tab tab) {
String name = tab.getText().toString();
mRvClipboard.setAdapter(getClipboardAdapter(prefs, res, name, commandMatcher));
setTabName(prefs, res, appId, name);
if (NEW_TAB_LABEL.equals(tab.getTag())) {
Intent intent = new Intent(getContext(), RewritesSelectorActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
mRvClipboard.setAdapter(getClipboardAdapter(prefs, res, name, commandMatcher));
setTabName(prefs, res, appId, name);
}
}

@Override
Expand All @@ -640,7 +642,8 @@ public void onTabReselected(TabLayout.Tab tab) {
tabs.addTab(tab, tabName.equals(selectedTabName));
}
TabLayout.Tab tab = tabs.newTab();
tab.setText("+");
tab.setText(NEW_TAB_LABEL);
tab.setTag(NEW_TAB_LABEL);
tabs.addTab(tab, false);

// If the previously selected rewrites table is not among the defaults anymore then
Expand All @@ -650,6 +653,7 @@ public void onTabReselected(TabLayout.Tab tab) {
}

LinearLayout tabStrip = (LinearLayout) tabs.getChildAt(0);
// We exclude the NEW_TAB_LABEL
for (int i = 0; i < tabStrip.getChildCount() - 1; i++) {
String name = tabs.getTabAt(i).getText().toString();
// Long click loads the rewrites view (without populating the tab)
Expand All @@ -664,12 +668,6 @@ public void onTabReselected(TabLayout.Tab tab) {
return false;
});
}

tabStrip.getChildAt(tabStrip.getChildCount() - 1).setOnClickListener(v -> {
Intent intent = new Intent(getContext(), RewritesSelectorActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
});
}

private String getTabName(SharedPreferences prefs, Resources res, String appId) {
Expand Down Expand Up @@ -848,6 +846,9 @@ private class SpeechInputRecognitionListener implements RecognitionListener {
public void onReadyForSpeech(Bundle params) {
Log.i("onReadyForSpeech: state = " + mState);
setGuiState(MicButton.State.LISTENING);
if (mBUiMode != null) {
mBUiMode.setColorFilter(MicButton.COLOR_LISTENING);
}
mBtnType = "R";
setText(mTvInstruction, R.string.buttonImeStop);
showMessage("");
Expand All @@ -859,9 +860,6 @@ public void onBeginningOfSpeech() {
setGuiState(MicButton.State.RECORDING);
mBtnType = "R";
setVisibility(findViewById(R.id.rlKeyButtons), View.INVISIBLE);
if (mBUiMode != null) {
mBUiMode.setColorFilter(COLOR_RECORDING);
}
}

@Override
Expand All @@ -873,7 +871,7 @@ public void onEndOfSpeech() {
if (mState == MicButton.State.RECORDING) {
setGuiState(MicButton.State.TRANSCRIBING);
if (mBUiMode != null) {
mBUiMode.setColorFilter(COLOR_TRANSCRIBING);
mBUiMode.setColorFilter(MicButton.COLOR_TRANSCRIBING);
}
setText(mTvInstruction, R.string.statusImeTranscribing);
}
Expand Down
72 changes: 42 additions & 30 deletions docs/adb-pref.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Usage:

adb-pref.py prefs_developer.yml prefs_user_guide_rewrites.yml prefs_private.yml | sh
adb-pref.py prefs_private.yml | fgrep "#Recent" | sh
adb-pref.py prefs_private.yml | fgrep "#r" | sh

"""

Expand All @@ -29,80 +29,92 @@
import re
import yaml

DEFAULT_ADB_COMMAND = 'shell'
#DEFAULT_ADB_COMMAND = 'exec-out'
DEFAULT_ADB_COMMAND = "shell"
# DEFAULT_ADB_COMMAND = 'exec-out'

DEFAULT_PREF_DISABLE_CONFIRMATION = {
'key': 'keyGetPutPrefSkipUi',
'val': True
}
DEFAULT_PREF_DISABLE_CONFIRMATION = {"key": "keyGetPutPrefSkipUi", "val": True}

# Escape for sh.
# TODO: incomplete + should escape for adb as well (e.g. the comma)
RE_SUB_ESCAPE_ESA = [
(';', '\\;'),
('#', '\\#'),
(";", "\\;"),
("#", "\\#"),
]

RE_SUB_ESCAPE_S = [
('\n', '\\\n'),
('\t', '\\\t'),
(' ', '\\ '),
(r"\n", r"\\n"),
(r"\t", r"\\t"),
(" ", "\\ "),
]


def apply_sub(rewriter, text):
"""Applies the given sequence of regex replacements to the given text."""
for x, y in rewriter:
text = re.sub(x, y, text)
return text


def get_args():
"""Get command line arguments"""
parser = argparse.ArgumentParser(description='Converts a YAML list of preferences to ADB calls that set these preferences using GetPutPreferenceActivity.')
parser.add_argument('fns', metavar='FILE', type=str, nargs='*',
help='preference file')
parser.add_argument('--disable-confirmation', action='store_true', dest='disable_confirmation')
parser.add_argument('-v', '--version', action='version', version='%(prog)s v0.0.3')
parser = argparse.ArgumentParser(
description="Converts a YAML list of preferences to ADB calls that set these preferences using GetPutPreferenceActivity."
)
parser.add_argument(
"fns", metavar="FILE", type=str, nargs="*", help="preference file"
)
parser.add_argument(
"--disable-confirmation", action="store_true", dest="disable_confirmation"
)
parser.add_argument("-v", "--version", action="version", version="%(prog)s v0.1.0")
return parser.parse_args()


def create_adb(pref):
"""Return the preference as an ADB call"""

def escape_esa(text):
return apply_sub(RE_SUB_ESCAPE_ESA, text)

def escape_s(text):
return apply_sub(RE_SUB_ESCAPE_S, text)

key = escape_s(pref['key'])
val = pref.get('val')
key = escape_s(pref["key"])
val = pref.get("val")
if val is None:
val_str = '--esn val'
elif type(val) is list:
val_str = '--esa val "{}"'.format(','.join(escape_esa(text) for text in val))
elif type(val) is bool:
val_str = '--ez val {}'.format(val)
val_str = "--esn val"
elif isinstance(val, list):
val_str = '--esa val "{}"'.format(",".join(escape_esa(text) for text in val))
elif isinstance(val, bool):
val_str = "--ez val {}".format(val)
elif isinstance(val, int):
val_str = "--ei val {}".format(val)
else:
val_str = '-e val "{}"'.format(escape_s(val))
if pref.get('is_url'):
val_str += ' --ez is_url true'
return 'adb {0} am start -n ee.ioc.phon.android.speak/.activity.GetPutPreferenceActivity -e key "{1}" {2}'.format(DEFAULT_ADB_COMMAND, key, val_str)
val_str = "-e val '{}'".format(escape_s(val))
if pref.get("is_url"):
val_str += " --ez is_url true"
return f'adb {DEFAULT_ADB_COMMAND} am start -n ee.ioc.phon.android.speak/.activity.GetPutPreferenceActivity -e key "{key}" {val_str}'


def process(prefs):
"""Output preferences as ADB calls"""
for pref in prefs:
print(create_adb(pref))


def main():
"""Main"""
args = get_args()
if args.disable_confirmation:
print(create_adb(DEFAULT_PREF_DISABLE_CONFIRMATION))
for fn in args.fns:
with open(fn, 'r') as stream:
with open(fn, "r") as stream:
try:
prefs = yaml.load(stream, Loader=yaml.BaseLoader)
prefs = yaml.safe_load(stream)
process(prefs)
except yaml.YAMLError as exc:
print(exc, file=sys.stderr)


if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion docs/prefs_developer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#
- key: keyImeCombo
val:
- ee.ioc.phon.android.k6neleservice/.service.WebSocketRecognitionService;et-EE
- ee.ioc.phon.android.speak/.service.WebSocketRecognitionService;et-EE
- com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService;en-US

Expand All @@ -17,6 +18,7 @@
#
- key: keyCombo
val:
- ee.ioc.phon.android.k6neleservice/.service.WebSocketRecognitionService;et-EE
- ee.ioc.phon.android.speak/.service.WebSocketRecognitionService;et-EE
- ee.ioc.phon.android.speak/.service.HttpRecognitionService;et-EE

Expand All @@ -26,7 +28,6 @@
- key: keyAutoStart
val: false

# Note that the integer value needs to be passed as string
- key: keyMaxHypotheses
val: 4

Expand All @@ -39,6 +40,7 @@
- key: keyAudioCues
val: false

# Note that the integer value needs to be passed as string
- key: keyAutoStopAfterTime
val: '10'

Expand Down
Loading