From 67debbd8c32e234f1565ed8f47ce5aa14cfdf4c2 Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Fri, 4 Apr 2014 09:04:00 +0000 Subject: [PATCH] now the output from the speech recognizer is editable with the keyboard --- .../res/layout/first_person_utterance.xml | 8 +- src/ui/android/res/layout/input_box.xml | 19 -- .../layout/second_person_best_utterance.xml | 12 -- .../layout/second_person_chunk_utterance.xml | 12 -- .../layout/second_person_worst_utterance.xml | 12 -- .../ui/android/ConversationView.java | 203 +++++++++--------- .../ui/android/MainActivity.java | 4 +- .../ui/android/Translator.java | 10 +- 8 files changed, 118 insertions(+), 162 deletions(-) delete mode 100644 src/ui/android/res/layout/input_box.xml delete mode 100644 src/ui/android/res/layout/second_person_best_utterance.xml delete mode 100644 src/ui/android/res/layout/second_person_chunk_utterance.xml delete mode 100644 src/ui/android/res/layout/second_person_worst_utterance.xml diff --git a/src/ui/android/res/layout/first_person_utterance.xml b/src/ui/android/res/layout/first_person_utterance.xml index ce0abf875..f7d96c467 100644 --- a/src/ui/android/res/layout/first_person_utterance.xml +++ b/src/ui/android/res/layout/first_person_utterance.xml @@ -4,21 +4,23 @@ android:layout_height="wrap_content" android:background="@drawable/first_person_utterance_bg" > - - - - - - \ No newline at end of file diff --git a/src/ui/android/res/layout/second_person_best_utterance.xml b/src/ui/android/res/layout/second_person_best_utterance.xml deleted file mode 100644 index 58d2d76a8..000000000 --- a/src/ui/android/res/layout/second_person_best_utterance.xml +++ /dev/null @@ -1,12 +0,0 @@ - - diff --git a/src/ui/android/res/layout/second_person_chunk_utterance.xml b/src/ui/android/res/layout/second_person_chunk_utterance.xml deleted file mode 100644 index 6810f32da..000000000 --- a/src/ui/android/res/layout/second_person_chunk_utterance.xml +++ /dev/null @@ -1,12 +0,0 @@ - - diff --git a/src/ui/android/res/layout/second_person_worst_utterance.xml b/src/ui/android/res/layout/second_person_worst_utterance.xml deleted file mode 100644 index 12d33a24d..000000000 --- a/src/ui/android/res/layout/second_person_worst_utterance.xml +++ /dev/null @@ -1,12 +0,0 @@ - - diff --git a/src/ui/android/src/org/grammaticalframework/ui/android/ConversationView.java b/src/ui/android/src/org/grammaticalframework/ui/android/ConversationView.java index 07e401ef8..8cc105322 100644 --- a/src/ui/android/src/org/grammaticalframework/ui/android/ConversationView.java +++ b/src/ui/android/src/org/grammaticalframework/ui/android/ConversationView.java @@ -25,7 +25,7 @@ public class ConversationView extends ScrollView { private ASR.Listener mSpeechListener; public ConversationView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); + super(context, attrs, defStyle); } public ConversationView(Context context, AttributeSet attrs) { @@ -42,127 +42,136 @@ public class ConversationView extends ScrollView { mContent = (ViewGroup) findViewById(R.id.conversation_content); mInflater = LayoutInflater.from(getContext()); } + + 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; + } - public void addFirstPersonUtterance(CharSequence text) { + @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); - 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() { - @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); - } - if (mSpeechListener != null) - mSpeechListener.onSpeechInput(text.toString().trim()); - return true; - } - return false; - } - }); + edittext.setText(text); + edittext.setOnEditorActionListener(mEditorListener); + edittext.setOnClickListener(mEditorListener); Bundle extras = edittext.getInputExtras(true); extras.putBoolean("show_language_toggle", false); - mContent.addView(view); - - edittext.requestFocus(); - InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); - imm.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT); + + 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; } - 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 - if (text.charAt(0) == '%') { - view = (TextView) - mInflater.inflate(R.layout.second_person_worst_utterance, mContent, false) ; - text = text.subSequence(2, text.length()) ; - } + // parse by words, marked by %, darkest red colour + if (text.charAt(0) == '%') { + 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) ; - } + // parse error or unknown translations (in []) present, darkest red colour + else if (text.toString().contains("parse error:") || text.toString().contains("[")) { + 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) ; - text = text.subSequence(2, text.length()) ; - } + // parse by chunks, marked by *, red colour + else if (text.charAt(0) == '*') { + 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) ; - 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); + // parse by domain grammar, marked by +, green colour + else if (text.charAt(0) == '+') { + view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_best_utterance_bg)); + text = text.subSequence(2, text.length()) ; + } - view.setText(text); - mContent.addView(view); - post(new Runnable() { - public void run() { - fullScroll(FOCUS_DOWN); - } - }); - return text ; + view.setText(text); + 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); - textview.setText(text); - - if (lexicon != null && mWordListener != null) { - ImageView showWordButton = (ImageView) view.findViewById(R.id.show_word); - showWordButton.setVisibility(VISIBLE); + if (mLastUtterance == null) + return; - final Object lexicon2 = lexicon; - showWordButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (mWordListener != null) { - TextView textview = (TextView) - ((View) v.getParent()).findViewById(R.id.text); - CharSequence text = textview.getText(); - mWordListener.onWordSelected(text, lexicon2); - } - } - }); - } + EditText textview = (EditText) mLastUtterance.findViewById(R.id.input_text); + if (textview == null) + return; + + textview.setText(text); + + if (lexicon != null && mWordListener != null) { + ImageView showWordButton = (ImageView) mLastUtterance.findViewById(R.id.show_word); + showWordButton.setVisibility(VISIBLE); + + final Object lexicon2 = lexicon; + showWordButton.setOnClickListener(new OnClickListener() { + @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); + } + } + }); } } diff --git a/src/ui/android/src/org/grammaticalframework/ui/android/MainActivity.java b/src/ui/android/src/org/grammaticalframework/ui/android/MainActivity.java index 3e104758c..404b0ee11 100644 --- a/src/ui/android/src/org/grammaticalframework/ui/android/MainActivity.java +++ b/src/ui/android/src/org/grammaticalframework/ui/android/MainActivity.java @@ -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); } } diff --git a/src/ui/android/src/org/grammaticalframework/ui/android/Translator.java b/src/ui/android/src/org/grammaticalframework/ui/android/Translator.java index c1fd3b485..705796494 100644 --- a/src/ui/android/src/org/grammaticalframework/ui/android/Translator.java +++ b/src/ui/android/src/org/grammaticalframework/ui/android/Translator.java @@ -52,16 +52,16 @@ public class Translator { // TODO: build dynamically? private Language[] mLanguages = { - 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("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("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), new Language("hi-IN", "Hindi", "AppHin", R.xml.devanagari_page1, R.xml.devanagari_page2), new Language("it-IT", "Italian", "AppIta", R.xml.qwerty), - new Language("es-ES", "Spanish", "AppSpa", R.xml.qwerty), - new Language("sv-SE", "Swedish", "AppSwe", R.xml.qwerty), + new Language("es-ES", "Spanish", "AppSpa", R.xml.qwerty), + new Language("sv-SE", "Swedish", "AppSwe", R.xml.qwerty), }; // */