mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
add keyboard input in the android app
This commit is contained in:
BIN
src/ui/android/res/drawable-hdpi/ic_keyboard.png
Normal file
BIN
src/ui/android/res/drawable-hdpi/ic_keyboard.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 695 B |
19
src/ui/android/res/layout/input_box.xml
Normal file
19
src/ui/android/res/layout/input_box.xml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?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>
|
||||||
4
src/ui/android/res/menu/main.xml
Normal file
4
src/ui/android/res/menu/main.xml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||||
|
<item android:id="@+id/input_mode"
|
||||||
|
android:title="@string/keyboard_input"/>
|
||||||
|
</menu>
|
||||||
@@ -6,4 +6,6 @@
|
|||||||
<string name="microphone">Microphone</string>
|
<string name="microphone">Microphone</string>
|
||||||
<string name="switch_languages">Switch languages</string>
|
<string name="switch_languages">Switch languages</string>
|
||||||
<string name="open_image">Opening</string>
|
<string name="open_image">Opening</string>
|
||||||
|
<string name="mic_input">Speech Input</string>
|
||||||
|
<string name="keyboard_input">Keyboard Input</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -5,9 +5,14 @@ import android.util.AttributeSet;
|
|||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.view.inputmethod.EditorInfo;
|
||||||
|
import android.view.inputmethod.InputMethodManager;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
import android.widget.EditText;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.ScrollView;
|
import android.widget.ScrollView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.TextView.OnEditorActionListener;
|
||||||
|
|
||||||
public class ConversationView extends ScrollView {
|
public class ConversationView extends ScrollView {
|
||||||
|
|
||||||
@@ -15,7 +20,8 @@ public class ConversationView extends ScrollView {
|
|||||||
|
|
||||||
private ViewGroup mContent;
|
private ViewGroup mContent;
|
||||||
|
|
||||||
private OnWordSelectedListener mListener;
|
private OnWordSelectedListener mWordListener;
|
||||||
|
private ASR.Listener mSpeechListener;
|
||||||
|
|
||||||
public ConversationView(Context context, AttributeSet attrs, int defStyle) {
|
public ConversationView(Context context, AttributeSet attrs, int defStyle) {
|
||||||
super(context, attrs, defStyle);
|
super(context, attrs, defStyle);
|
||||||
@@ -49,6 +55,47 @@ public class ConversationView extends ScrollView {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addInputBox() {
|
||||||
|
final View view =
|
||||||
|
mInflater.inflate(R.layout.input_box, mContent, false);
|
||||||
|
EditText edittext = (EditText) view.findViewById(R.id.input_text);
|
||||||
|
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onFocusChange(View v, boolean hasFocus) {
|
||||||
|
if (hasFocus) {
|
||||||
|
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||||
|
if (inputMethodManager != null) {
|
||||||
|
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
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());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mContent.addView(view);
|
||||||
|
post(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
fullScroll(FOCUS_DOWN);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public void addSecondPersonUtterance(CharSequence text) {
|
public void addSecondPersonUtterance(CharSequence text) {
|
||||||
TextView view = (TextView)
|
TextView view = (TextView)
|
||||||
mInflater.inflate(R.layout.second_person_utterance, mContent, false);
|
mInflater.inflate(R.layout.second_person_utterance, mContent, false);
|
||||||
@@ -68,7 +115,7 @@ public class ConversationView extends ScrollView {
|
|||||||
TextView textview = (TextView) view.findViewById(R.id.text);
|
TextView textview = (TextView) view.findViewById(R.id.text);
|
||||||
textview.setText(text);
|
textview.setText(text);
|
||||||
|
|
||||||
if (lexicon != null && mListener != null) {
|
if (lexicon != null && mWordListener != null) {
|
||||||
ImageView showWordButton = (ImageView) view.findViewById(R.id.show_word);
|
ImageView showWordButton = (ImageView) view.findViewById(R.id.show_word);
|
||||||
showWordButton.setVisibility(VISIBLE);
|
showWordButton.setVisibility(VISIBLE);
|
||||||
|
|
||||||
@@ -76,10 +123,12 @@ public class ConversationView extends ScrollView {
|
|||||||
showWordButton.setOnClickListener(new OnClickListener() {
|
showWordButton.setOnClickListener(new OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
TextView textview = (TextView)
|
if (mWordListener != null) {
|
||||||
|
TextView textview = (TextView)
|
||||||
((View) v.getParent()).findViewById(R.id.text);
|
((View) v.getParent()).findViewById(R.id.text);
|
||||||
CharSequence text = textview.getText();
|
CharSequence text = textview.getText();
|
||||||
mListener.onWordSelected(text, lexicon2);
|
mWordListener.onWordSelected(text, lexicon2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -87,7 +136,11 @@ public class ConversationView extends ScrollView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setOnWordSelectedListener(OnWordSelectedListener listener) {
|
public void setOnWordSelectedListener(OnWordSelectedListener listener) {
|
||||||
mListener = listener;
|
mWordListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSpeechInputListener(ASR.Listener listener) {
|
||||||
|
mSpeechListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface OnWordSelectedListener {
|
public interface OnWordSelectedListener {
|
||||||
|
|||||||
@@ -5,13 +5,18 @@ import java.io.Serializable;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.speech.SpeechRecognizer;
|
import android.speech.SpeechRecognizer;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuInflater;
|
||||||
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
|
import android.view.inputmethod.InputMethodManager;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
import org.grammaticalframework.ui.android.ASR.State;
|
import org.grammaticalframework.ui.android.ASR.State;
|
||||||
@@ -41,6 +46,10 @@ public class MainActivity extends Activity {
|
|||||||
private TTS mTts;
|
private TTS mTts;
|
||||||
|
|
||||||
private Translator mTranslator;
|
private Translator mTranslator;
|
||||||
|
|
||||||
|
private boolean input_mode;
|
||||||
|
|
||||||
|
private SpeechInputListener mSpeechListener;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -64,7 +73,13 @@ public class MainActivity extends Activity {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
mStartStopButton.setEnabled(SpeechRecognizer.isRecognitionAvailable(this));
|
input_mode = true;
|
||||||
|
if (!SpeechRecognizer.isRecognitionAvailable(this)) {
|
||||||
|
input_mode = false;
|
||||||
|
mStartStopButton.setImageResource(R.drawable.ic_keyboard);
|
||||||
|
}
|
||||||
|
|
||||||
|
mSpeechListener = new SpeechInputListener();
|
||||||
|
|
||||||
mConversationView.setOnWordSelectedListener(new OnWordSelectedListener() {
|
mConversationView.setOnWordSelectedListener(new OnWordSelectedListener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -75,9 +90,10 @@ public class MainActivity extends Activity {
|
|||||||
MainActivity.this.startActivity(myIntent);
|
MainActivity.this.startActivity(myIntent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
mConversationView.setSpeechInputListener(mSpeechListener);
|
||||||
|
|
||||||
mAsr = new ASR(this);
|
mAsr = new ASR(this);
|
||||||
mAsr.setListener(new SpeechInputListener());
|
mAsr.setListener(mSpeechListener);
|
||||||
|
|
||||||
mTts = new TTS(this);
|
mTts = new TTS(this);
|
||||||
|
|
||||||
@@ -114,6 +130,37 @@ public class MainActivity extends Activity {
|
|||||||
mTargetLanguageView.setSelectedLanguage(mTranslator.getTargetLanguage());
|
mTargetLanguageView.setSelectedLanguage(mTranslator.getTargetLanguage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
MenuInflater inflater = getMenuInflater();
|
||||||
|
inflater.inflate(R.menu.main, menu);
|
||||||
|
|
||||||
|
if (!SpeechRecognizer.isRecognitionAvailable(this)) {
|
||||||
|
menu.getItem(0).setEnabled(false);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
// Handle item selection
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case R.id.input_mode:
|
||||||
|
if (input_mode) {
|
||||||
|
item.setTitle(R.string.mic_input);
|
||||||
|
mStartStopButton.setImageResource(R.drawable.ic_keyboard);
|
||||||
|
input_mode = false;
|
||||||
|
} else {
|
||||||
|
item.setTitle(R.string.keyboard_input);
|
||||||
|
mStartStopButton.setImageResource(R.drawable.ic_mic);
|
||||||
|
input_mode = true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
if (mAsr != null) {
|
if (mAsr != null) {
|
||||||
@@ -151,14 +198,18 @@ public class MainActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startRecognition() {
|
private void startRecognition() {
|
||||||
mConversationView.addFirstPersonUtterance("...");
|
if (FAKE_SPEECH) {
|
||||||
|
|
||||||
if (FAKE_SPEECH) {
|
|
||||||
handleSpeechInput("where is the hotel");
|
handleSpeechInput("where is the hotel");
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input_mode) {
|
||||||
|
mConversationView.addFirstPersonUtterance("...");
|
||||||
mAsr.setLanguage(getSourceLanguageCode());
|
mAsr.setLanguage(getSourceLanguageCode());
|
||||||
mAsr.startRecognition();
|
mAsr.startRecognition();
|
||||||
}
|
} else {
|
||||||
|
mConversationView.addInputBox();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopRecognition() {
|
private void stopRecognition() {
|
||||||
|
|||||||
Reference in New Issue
Block a user