now the output from the speech recognizer is editable with the keyboard

This commit is contained in:
kr.angelov
2014-04-04 09:04:00 +00:00
parent 232b4518ed
commit 9f0ee95fd6
8 changed files with 118 additions and 162 deletions

View File

@@ -4,21 +4,23 @@
android:layout_height="wrap_content"
android:background="@drawable/first_person_utterance_bg" >
<TextView
android:id="@+id/text"
<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"
/>
<ImageView
android:id="@+id/show_word"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/text"
android:layout_toRightOf="@id/input_text"
android:padding="8dp"
android:src="@drawable/ic_dictionary"
android:visibility="gone"

View File

@@ -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>

View File

@@ -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"
/>

View File

@@ -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"
/>

View File

@@ -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"
/>

View File

@@ -43,111 +43,121 @@ public class ConversationView extends ScrollView {
mInflater = LayoutInflater.from(getContext());
}
public void addFirstPersonUtterance(CharSequence text) {
View view =
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.setOnEditorActionListener(new OnEditorActionListener() {
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();
mContent.removeView(view);
addFirstPersonUtterance("...");
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 =
mInflater.inflate(R.layout.first_person_utterance, mContent, false);
EditText edittext = (EditText) view.findViewById(R.id.input_text);
edittext.setText(text);
edittext.setOnEditorActionListener(mEditorListener);
edittext.setOnClickListener(mEditorListener);
Bundle extras = edittext.getInputExtras(true);
extras.putBoolean("show_language_toggle", false);
mContent.addView(view);
if (focused) {
edittext.requestFocus();
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
} else {
edittext.setFocusable(false);
}
post(new Runnable() {
public void run() {
fullScroll(FOCUS_DOWN);
}
});
mLastUtterance = view;
}
@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);
}
});
mLastUtterance.setTag(view);
}
// parse by words, marked by %, darkest red colour
if (text.charAt(0) == '%') {
view = (TextView)
mInflater.inflate(R.layout.second_person_worst_utterance, mContent, false) ;
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_worst_utterance_bg));
text = text.subSequence(2, text.length()) ;
}
// parse error or unknown translations (in []) present, darkest red colour
else if (text.toString().contains("parse error:") || text.toString().contains("[")) {
view = (TextView)
mInflater.inflate(R.layout.second_person_worst_utterance, mContent, false) ;
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_worst_utterance_bg));
}
// parse by chunks, marked by *, red colour
else if (text.charAt(0) == '*') {
view = (TextView)
mInflater.inflate(R.layout.second_person_chunk_utterance, mContent, false) ;
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_chunk_utterance_bg));
text = text.subSequence(2, text.length()) ;
}
// parse by domain grammar, marked by +, green colour
else if (text.charAt(0) == '+') {
view = (TextView)
mInflater.inflate(R.layout.second_person_best_utterance, mContent, false) ;
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_best_utterance_bg));
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);
mContent.addView(view);
post(new Runnable() {
public void run() {
fullScroll(FOCUS_DOWN);
}
});
return text ;
}
public void updateLastUtterance(CharSequence text, Object lexicon) {
int count = mContent.getChildCount();
if (count > 0) {
View view = mContent.getChildAt(count - 1);
TextView textview = (TextView) view.findViewById(R.id.text);
if (mLastUtterance == null)
return;
EditText textview = (EditText) mLastUtterance.findViewById(R.id.input_text);
if (textview == null)
return;
textview.setText(text);
if (lexicon != null && mWordListener != null) {
ImageView showWordButton = (ImageView) view.findViewById(R.id.show_word);
ImageView showWordButton = (ImageView) mLastUtterance.findViewById(R.id.show_word);
showWordButton.setVisibility(VISIBLE);
final Object lexicon2 = lexicon;
@@ -156,7 +166,7 @@ public class ConversationView extends ScrollView {
public void onClick(View v) {
if (mWordListener != null) {
TextView textview = (TextView)
((View) v.getParent()).findViewById(R.id.text);
((View) v.getParent()).findViewById(R.id.input_text);
CharSequence text = textview.getText();
mWordListener.onWordSelected(text, lexicon2);
}
@@ -164,7 +174,6 @@ public class ConversationView extends ScrollView {
});
}
}
}
public void setOnWordSelectedListener(OnWordSelectedListener listener) {
mWordListener = listener;

View File

@@ -269,11 +269,11 @@ public class MainActivity extends Activity {
private void startRecognition() {
if (input_mode) {
mConversationView.addFirstPersonUtterance("...");
mConversationView.addFirstPersonUtterance("...", false);
mAsr.setLanguage(getSourceLanguageCode());
mAsr.startRecognition();
} else {
mConversationView.addInputBox();
mConversationView.addFirstPersonUtterance("", true);
}
}

View File

@@ -52,9 +52,9 @@ public class Translator {
// TODO: build dynamically?
private Language[] mLanguages = {
new Language("en-US", "English", "AppEng", R.xml.qwerty),
new Language("bg-BG", "Bulgarian", "AppBul", R.xml.cyrillic),
new Language("cmn-Hans-CN", "Chinese", "AppChi", R.xml.qwerty),
new Language("en-US", "English", "AppEng", R.xml.qwerty),
new Language("fi-FI", "Finnish", "AppFin", R.xml.qwerty),
new Language("fr-FR", "French", "AppFre", R.xml.qwerty),
new Language("de-DE", "German", "AppGer", R.xml.qwerty),