forked from GitHub/gf-core
finally: the conversation view is now no longer cleared up everytime when the device is flipped
This commit is contained in:
@@ -10,6 +10,10 @@
|
||||
<string name="help">Help</string>
|
||||
<string name="global_preferences_key">org.grammaticalframework.ui.android.GLOBAL_PREFERENCES</string>
|
||||
|
||||
<string name="source_key">source_key</string>
|
||||
<string name="target_key">target_key</string>
|
||||
<string name="alternatives_key">alternatives_key</string>
|
||||
|
||||
<!-- Labels on soft keys -->
|
||||
<string name="label_done_key">Done</string>
|
||||
<string name="label_go_key">Go</string>
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.grammaticalframework.ui.android;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -11,7 +13,6 @@ 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.ScrollView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
@@ -22,7 +23,7 @@ public class ConversationView extends ScrollView {
|
||||
|
||||
private ViewGroup mContent;
|
||||
|
||||
private OnAlternativesListener mAlternativesListener;
|
||||
private OnClickListener mAlternativesListener;
|
||||
private ASR.Listener mSpeechListener;
|
||||
|
||||
public ConversationView(Context context, AttributeSet attrs, int defStyle) {
|
||||
@@ -109,6 +110,8 @@ public class ConversationView extends ScrollView {
|
||||
else {
|
||||
view = (TextView)
|
||||
mInflater.inflate(R.layout.second_person_utterance, mContent, false);
|
||||
if (mAlternativesListener != null)
|
||||
view.setOnClickListener(mAlternativesListener);
|
||||
mContent.addView(view);
|
||||
post(new Runnable() {
|
||||
public void run() {
|
||||
@@ -119,13 +122,9 @@ public class ConversationView extends ScrollView {
|
||||
mLastUtterance.setTag(view);
|
||||
}
|
||||
|
||||
view.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (mAlternativesListener != null)
|
||||
mAlternativesListener.onAlternativesSelected(source, alternatives);
|
||||
}
|
||||
});
|
||||
view.setTag(R.string.source_key, source);
|
||||
view.setTag(R.string.target_key, target);
|
||||
view.setTag(R.string.alternatives_key, alternatives);
|
||||
|
||||
// parse by words, marked by %, darkest red color
|
||||
if (target.charAt(0) == '%') {
|
||||
@@ -149,6 +148,10 @@ public class ConversationView extends ScrollView {
|
||||
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_best_utterance_bg));
|
||||
target = target.subSequence(2, target.length()) ;
|
||||
}
|
||||
|
||||
else {
|
||||
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.second_person_utterance_bg));
|
||||
}
|
||||
|
||||
view.setText(target);
|
||||
return target;
|
||||
@@ -159,8 +162,18 @@ public class ConversationView extends ScrollView {
|
||||
mLastUtterance.setText(text);
|
||||
}
|
||||
|
||||
public void setOnAlternativesListener(OnAlternativesListener listener) {
|
||||
mAlternativesListener = listener;
|
||||
public void setOnAlternativesListener(final OnAlternativesListener listener) {
|
||||
if (listener == null)
|
||||
mAlternativesListener = null;
|
||||
else
|
||||
mAlternativesListener = new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
String source = v.getTag(R.string.source_key).toString();
|
||||
Object alternatives = v.getTag(R.string.alternatives_key);
|
||||
listener.onAlternativesSelected(source, alternatives);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void setSpeechInputListener(ASR.Listener listener) {
|
||||
@@ -170,4 +183,48 @@ public class ConversationView extends ScrollView {
|
||||
public interface OnAlternativesListener {
|
||||
public void onAlternativesSelected(CharSequence word, Object lexicon);
|
||||
}
|
||||
|
||||
public void saveConversation(Bundle state) {
|
||||
ArrayList<String> firstPersonUtterances = new ArrayList<String>();
|
||||
ArrayList<String> secondPersonUtterances = new ArrayList<String>();
|
||||
ArrayList<Object> translationAlternatives = new ArrayList<Object>();
|
||||
|
||||
int childCount = mContent.getChildCount();
|
||||
for (int i = 0; i < childCount; i++) {
|
||||
View child = mContent.getChildAt(i);
|
||||
if (child.getClass() == TextView.class) {
|
||||
firstPersonUtterances.add(child.getTag(R.string.source_key).toString());
|
||||
secondPersonUtterances.add(child.getTag(R.string.target_key).toString());
|
||||
translationAlternatives.add(child.getTag(R.string.alternatives_key));
|
||||
}
|
||||
}
|
||||
|
||||
state.putStringArrayList("first_person_uterances", firstPersonUtterances);
|
||||
state.putStringArrayList("second_person_uterances", secondPersonUtterances);
|
||||
state.putSerializable("translation_alternatives",(Serializable) translationAlternatives);
|
||||
}
|
||||
|
||||
public void restoreConversation(Bundle state) {
|
||||
final ArrayList<String> firstPersonUtterances = state.getStringArrayList("first_person_uterances");
|
||||
final ArrayList<String> secondPersonUtterances = state.getStringArrayList("second_person_uterances");
|
||||
final ArrayList<Object> translationAlternatives= (ArrayList<Object>) state.getSerializable("translation_alternatives");
|
||||
|
||||
post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int i = 0;
|
||||
while (i < firstPersonUtterances.size() &&
|
||||
i < Math.min(secondPersonUtterances.size(), translationAlternatives.size())) {
|
||||
String text = firstPersonUtterances.get(i);
|
||||
addFirstPersonUtterance(text, false);
|
||||
|
||||
String translation = secondPersonUtterances.get(i);
|
||||
Object alternatives = translationAlternatives.get(i);
|
||||
addSecondPersonUtterance(text, translation, alternatives);
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ import android.os.Bundle;
|
||||
import android.speech.SpeechRecognizer;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
@@ -20,11 +18,7 @@ import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.View.OnTouchListener;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.grammaticalframework.ui.android.ASR.State;
|
||||
import org.grammaticalframework.ui.android.LanguageSelector.OnLanguageSelectedListener;
|
||||
@@ -59,7 +53,6 @@ public class MainActivity extends Activity {
|
||||
|
||||
private View mProgressBarView = null;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -157,6 +150,10 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
});
|
||||
mSwitchLanguagesButton.setOnTouchListener(touchFeedbackListener);
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
mConversationView.restoreConversation(savedInstanceState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -178,8 +175,7 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
outState.putBoolean("input_mode", input_mode);
|
||||
mConversationView.saveConversation(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -305,10 +301,10 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
protected void onPostExecute(Pair<String,List<ExprProb>> res) {
|
||||
String text = res.first;
|
||||
List<ExprProb> alts = res.second;
|
||||
Object alts = (list.size() == 0) ? res.second : list;
|
||||
if (DBG) Log.d(TAG, "Speaking: " + res.first);
|
||||
CharSequence text2 =
|
||||
mConversationView.addSecondPersonUtterance(input, text, (list.size() == 0) ? alts : list);
|
||||
CharSequence text2 =
|
||||
mConversationView.addSecondPersonUtterance(input, text, alts);
|
||||
mTts.speak(getTargetLanguageCode(), text2.toString());
|
||||
|
||||
hideProgressBar();
|
||||
|
||||
@@ -428,7 +428,6 @@ public class Translator {
|
||||
Arrays.sort(completions, 1, completions.length-1, new Comparator<CompletionInfo>() {
|
||||
@Override
|
||||
public int compare(CompletionInfo arg0, CompletionInfo arg1) {
|
||||
// TODO Auto-generated method stub
|
||||
return ((String) arg0.getText()).compareTo((String) arg1.getText());
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user