forked from GitHub/gf-core
now the output from the speech recognizer is editable with the keyboard
This commit is contained in:
@@ -4,21 +4,23 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:background="@drawable/first_person_utterance_bg" >
|
android:background="@drawable/first_person_utterance_bg" >
|
||||||
|
|
||||||
<TextView
|
<EditText
|
||||||
android:id="@+id/text"
|
android:id="@+id/input_text"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_marginBottom="16dp"
|
android:layout_marginBottom="16dp"
|
||||||
android:layout_gravity="left"
|
android:layout_gravity="left"
|
||||||
android:padding="8dp"
|
android:padding="8dp"
|
||||||
android:textSize="20sp"
|
android:textSize="20sp"
|
||||||
|
android:inputType="text"
|
||||||
|
android:background="@drawable/first_person_utterance_bg"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/show_word"
|
android:id="@+id/show_word"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_toRightOf="@id/text"
|
android:layout_toRightOf="@id/input_text"
|
||||||
android:padding="8dp"
|
android:padding="8dp"
|
||||||
android:src="@drawable/ic_dictionary"
|
android:src="@drawable/ic_dictionary"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:background="@drawable/first_person_utterance_bg" >
|
|
||||||
|
|
||||||
<EditText
|
|
||||||
android:id="@+id/input_text"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_marginBottom="16dp"
|
|
||||||
android:layout_gravity="left"
|
|
||||||
android:padding="8dp"
|
|
||||||
android:textSize="20sp"
|
|
||||||
android:inputType="text"
|
|
||||||
android:background="@drawable/first_person_utterance_bg"
|
|
||||||
/>
|
|
||||||
|
|
||||||
</RelativeLayout>
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<TextView
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_marginBottom="16dp"
|
|
||||||
android:layout_marginLeft="32dp"
|
|
||||||
android:layout_gravity="right"
|
|
||||||
android:padding="8dp"
|
|
||||||
android:textSize="20sp"
|
|
||||||
android:background="@drawable/second_person_best_utterance_bg"
|
|
||||||
/>
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<TextView
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_marginBottom="16dp"
|
|
||||||
android:layout_marginLeft="32dp"
|
|
||||||
android:layout_gravity="right"
|
|
||||||
android:padding="8dp"
|
|
||||||
android:textSize="20sp"
|
|
||||||
android:background="@drawable/second_person_chunk_utterance_bg"
|
|
||||||
/>
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<TextView
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_marginBottom="16dp"
|
|
||||||
android:layout_marginLeft="32dp"
|
|
||||||
android:layout_gravity="right"
|
|
||||||
android:padding="8dp"
|
|
||||||
android:textSize="20sp"
|
|
||||||
android:background="@drawable/second_person_worst_utterance_bg"
|
|
||||||
/>
|
|
||||||
@@ -43,126 +43,135 @@ public class ConversationView extends ScrollView {
|
|||||||
mInflater = LayoutInflater.from(getContext());
|
mInflater = LayoutInflater.from(getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addFirstPersonUtterance(CharSequence text) {
|
private class EditorListener implements OnEditorActionListener, OnClickListener {
|
||||||
|
@Override
|
||||||
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||||
|
if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
|
||||||
|
CharSequence text = v.getText();
|
||||||
|
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||||
|
if (inputMethodManager != null) {
|
||||||
|
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
|
||||||
|
}
|
||||||
|
v.setFocusable(false);
|
||||||
|
mLastUtterance = (View) v.getParent();
|
||||||
|
if (mSpeechListener != null)
|
||||||
|
mSpeechListener.onSpeechInput(text.toString().trim());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
v.setFocusableInTouchMode(true);
|
||||||
|
v.requestFocus();
|
||||||
|
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||||
|
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private EditorListener mEditorListener = new EditorListener();
|
||||||
|
private View mLastUtterance = null;
|
||||||
|
|
||||||
|
public void addFirstPersonUtterance(CharSequence text, boolean focused) {
|
||||||
View view =
|
View view =
|
||||||
mInflater.inflate(R.layout.first_person_utterance, mContent, false);
|
mInflater.inflate(R.layout.first_person_utterance, mContent, false);
|
||||||
TextView textview = (TextView) view.findViewById(R.id.text);
|
|
||||||
textview.setText(text);
|
|
||||||
mContent.addView(view);
|
|
||||||
post(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
fullScroll(FOCUS_DOWN);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addInputBox() {
|
|
||||||
final View view =
|
|
||||||
mInflater.inflate(R.layout.input_box, mContent, false);
|
|
||||||
EditText edittext = (EditText) view.findViewById(R.id.input_text);
|
EditText edittext = (EditText) view.findViewById(R.id.input_text);
|
||||||
edittext.setOnEditorActionListener(new OnEditorActionListener() {
|
edittext.setText(text);
|
||||||
@Override
|
edittext.setOnEditorActionListener(mEditorListener);
|
||||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
edittext.setOnClickListener(mEditorListener);
|
||||||
if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
|
|
||||||
CharSequence text = v.getText();
|
|
||||||
mContent.removeView(view);
|
|
||||||
addFirstPersonUtterance("...");
|
|
||||||
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
||||||
if (inputMethodManager != null) {
|
|
||||||
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
|
|
||||||
}
|
|
||||||
if (mSpeechListener != null)
|
|
||||||
mSpeechListener.onSpeechInput(text.toString().trim());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Bundle extras = edittext.getInputExtras(true);
|
Bundle extras = edittext.getInputExtras(true);
|
||||||
extras.putBoolean("show_language_toggle", false);
|
extras.putBoolean("show_language_toggle", false);
|
||||||
|
|
||||||
mContent.addView(view);
|
mContent.addView(view);
|
||||||
|
|
||||||
edittext.requestFocus();
|
if (focused) {
|
||||||
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
edittext.requestFocus();
|
||||||
imm.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
|
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||||
|
imm.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
|
||||||
|
} else {
|
||||||
|
edittext.setFocusable(false);
|
||||||
|
}
|
||||||
|
|
||||||
post(new Runnable() {
|
post(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
fullScroll(FOCUS_DOWN);
|
fullScroll(FOCUS_DOWN);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
mLastUtterance = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CharSequence addSecondPersonUtterance(CharSequence text) {
|
@SuppressWarnings("deprecation")
|
||||||
|
public CharSequence addSecondPersonUtterance(CharSequence text) {
|
||||||
|
TextView view;
|
||||||
|
if (mLastUtterance != null && mLastUtterance.getTag() != null)
|
||||||
|
view = (TextView) mLastUtterance.getTag();
|
||||||
|
else {
|
||||||
|
view = (TextView)
|
||||||
|
mInflater.inflate(R.layout.second_person_utterance, mContent, false);
|
||||||
|
mContent.addView(view);
|
||||||
|
post(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
fullScroll(FOCUS_DOWN);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
TextView view ;
|
mLastUtterance.setTag(view);
|
||||||
|
}
|
||||||
|
|
||||||
// parse by words, marked by %, darkest red colour
|
// parse by words, marked by %, darkest red colour
|
||||||
if (text.charAt(0) == '%') {
|
if (text.charAt(0) == '%') {
|
||||||
view = (TextView)
|
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_worst_utterance_bg));
|
||||||
mInflater.inflate(R.layout.second_person_worst_utterance, mContent, false) ;
|
text = text.subSequence(2, text.length()) ;
|
||||||
text = text.subSequence(2, text.length()) ;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// parse error or unknown translations (in []) present, darkest red colour
|
// parse error or unknown translations (in []) present, darkest red colour
|
||||||
else if (text.toString().contains("parse error:") || text.toString().contains("[")) {
|
else if (text.toString().contains("parse error:") || text.toString().contains("[")) {
|
||||||
view = (TextView)
|
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_worst_utterance_bg));
|
||||||
mInflater.inflate(R.layout.second_person_worst_utterance, mContent, false) ;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// parse by chunks, marked by *, red colour
|
// parse by chunks, marked by *, red colour
|
||||||
else if (text.charAt(0) == '*') {
|
else if (text.charAt(0) == '*') {
|
||||||
view = (TextView)
|
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_chunk_utterance_bg));
|
||||||
mInflater.inflate(R.layout.second_person_chunk_utterance, mContent, false) ;
|
text = text.subSequence(2, text.length()) ;
|
||||||
text = text.subSequence(2, text.length()) ;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// parse by domain grammar, marked by +, green colour
|
// parse by domain grammar, marked by +, green colour
|
||||||
else if (text.charAt(0) == '+') {
|
else if (text.charAt(0) == '+') {
|
||||||
view = (TextView)
|
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_best_utterance_bg));
|
||||||
mInflater.inflate(R.layout.second_person_best_utterance, mContent, false) ;
|
text = text.subSequence(2, text.length()) ;
|
||||||
text = text.subSequence(2, text.length()) ;
|
}
|
||||||
}
|
|
||||||
// parse by resource grammar, no mark, yellow colour
|
|
||||||
else
|
|
||||||
view = (TextView)
|
|
||||||
mInflater.inflate(R.layout.second_person_utterance, mContent, false);
|
|
||||||
|
|
||||||
view.setText(text);
|
view.setText(text);
|
||||||
mContent.addView(view);
|
return text ;
|
||||||
post(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
fullScroll(FOCUS_DOWN);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return text ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateLastUtterance(CharSequence text, Object lexicon) {
|
public void updateLastUtterance(CharSequence text, Object lexicon) {
|
||||||
int count = mContent.getChildCount();
|
if (mLastUtterance == null)
|
||||||
if (count > 0) {
|
return;
|
||||||
View view = mContent.getChildAt(count - 1);
|
|
||||||
TextView textview = (TextView) view.findViewById(R.id.text);
|
|
||||||
textview.setText(text);
|
|
||||||
|
|
||||||
if (lexicon != null && mWordListener != null) {
|
EditText textview = (EditText) mLastUtterance.findViewById(R.id.input_text);
|
||||||
ImageView showWordButton = (ImageView) view.findViewById(R.id.show_word);
|
if (textview == null)
|
||||||
showWordButton.setVisibility(VISIBLE);
|
return;
|
||||||
|
|
||||||
final Object lexicon2 = lexicon;
|
textview.setText(text);
|
||||||
showWordButton.setOnClickListener(new OnClickListener() {
|
|
||||||
@Override
|
if (lexicon != null && mWordListener != null) {
|
||||||
public void onClick(View v) {
|
ImageView showWordButton = (ImageView) mLastUtterance.findViewById(R.id.show_word);
|
||||||
if (mWordListener != null) {
|
showWordButton.setVisibility(VISIBLE);
|
||||||
TextView textview = (TextView)
|
|
||||||
((View) v.getParent()).findViewById(R.id.text);
|
final Object lexicon2 = lexicon;
|
||||||
CharSequence text = textview.getText();
|
showWordButton.setOnClickListener(new OnClickListener() {
|
||||||
mWordListener.onWordSelected(text, lexicon2);
|
@Override
|
||||||
}
|
public void onClick(View v) {
|
||||||
}
|
if (mWordListener != null) {
|
||||||
});
|
TextView textview = (TextView)
|
||||||
}
|
((View) v.getParent()).findViewById(R.id.input_text);
|
||||||
|
CharSequence text = textview.getText();
|
||||||
|
mWordListener.onWordSelected(text, lexicon2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -269,11 +269,11 @@ public class MainActivity extends Activity {
|
|||||||
|
|
||||||
private void startRecognition() {
|
private void startRecognition() {
|
||||||
if (input_mode) {
|
if (input_mode) {
|
||||||
mConversationView.addFirstPersonUtterance("...");
|
mConversationView.addFirstPersonUtterance("...", false);
|
||||||
mAsr.setLanguage(getSourceLanguageCode());
|
mAsr.setLanguage(getSourceLanguageCode());
|
||||||
mAsr.startRecognition();
|
mAsr.startRecognition();
|
||||||
} else {
|
} else {
|
||||||
mConversationView.addInputBox();
|
mConversationView.addFirstPersonUtterance("", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,9 +52,9 @@ public class Translator {
|
|||||||
|
|
||||||
// TODO: build dynamically?
|
// TODO: build dynamically?
|
||||||
private Language[] mLanguages = {
|
private Language[] mLanguages = {
|
||||||
new Language("bg-BG", "Bulgarian", "AppBul", R.xml.cyrillic),
|
new Language("en-US", "English", "AppEng", R.xml.qwerty),
|
||||||
new Language("cmn-Hans-CN", "Chinese", "AppChi", R.xml.qwerty),
|
new Language("bg-BG", "Bulgarian", "AppBul", R.xml.cyrillic),
|
||||||
new Language("en-US", "English", "AppEng", R.xml.qwerty),
|
new Language("cmn-Hans-CN", "Chinese", "AppChi", R.xml.qwerty),
|
||||||
new Language("fi-FI", "Finnish", "AppFin", R.xml.qwerty),
|
new Language("fi-FI", "Finnish", "AppFin", R.xml.qwerty),
|
||||||
new Language("fr-FR", "French", "AppFre", R.xml.qwerty),
|
new Language("fr-FR", "French", "AppFre", R.xml.qwerty),
|
||||||
new Language("de-DE", "German", "AppGer", R.xml.qwerty),
|
new Language("de-DE", "German", "AppGer", R.xml.qwerty),
|
||||||
|
|||||||
Reference in New Issue
Block a user