1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-29 04:12:18 -05:00

Fix voice recognition: Only add top match to the text input (and code cleanup)

This commit is contained in:
Sebastian Kaspari 2011-01-15 23:17:16 +01:00
parent e68ef9c8c2
commit 7c63155cfb

View File

@ -17,7 +17,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Yaaic. If not, see <http://www.gnu.org/licenses/>. along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.yaaic.activity; package org.yaaic.activity;
import java.util.ArrayList; import java.util.ArrayList;
@ -70,8 +70,8 @@ import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.View.OnKeyListener; import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.view.Window; import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Gallery; import android.widget.Gallery;
@ -93,19 +93,19 @@ public class ConversationActivity extends Activity implements ServiceConnection,
private static final int REQUEST_CODE_USERS = 2; private static final int REQUEST_CODE_USERS = 2;
private static final int REQUEST_CODE_USER = 3; private static final int REQUEST_CODE_USER = 3;
private static final int REQUEST_CODE_NICK_COMPLETION= 4; private static final int REQUEST_CODE_NICK_COMPLETION= 4;
private int serverId; private int serverId;
private Server server; private Server server;
private IRCBinder binder; private IRCBinder binder;
private ConversationReceiver channelReceiver; private ConversationReceiver channelReceiver;
private ServerReceiver serverReceiver; private ServerReceiver serverReceiver;
private ViewSwitcher switcher; private ViewSwitcher switcher;
private Gallery deck; private Gallery deck;
private DeckAdapter deckAdapter; private DeckAdapter deckAdapter;
private Scrollback scrollback; private Scrollback scrollback;
private ConversationSwitcher dots; private ConversationSwitcher dots;
// XXX: This is ugly. This is a buffer for a channel that should be joined after showing the // XXX: This is ugly. This is a buffer for a channel that should be joined after showing the
// JoinActivity. As onActivityResult() is called before onResume() a "channel joined" // JoinActivity. As onActivityResult() is called before onResume() a "channel joined"
// broadcast may get lost as the broadcast receivers are registered in onResume() but the // broadcast may get lost as the broadcast receivers are registered in onResume() but the
@ -117,7 +117,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
// shall be TYPE_TEXT_FLAG_NO_SUGGESTIONS but it's not supported in all API levels (only in 5+) // shall be TYPE_TEXT_FLAG_NO_SUGGESTIONS but it's not supported in all API levels (only in 5+)
// We'll set it to 0 if it's not supported // We'll set it to 0 if it's not supported
private int setInputTypeFlag; private int setInputTypeFlag;
/** /**
* On create * On create
*/ */
@ -126,18 +126,18 @@ public class ConversationActivity extends Activity implements ServiceConnection,
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
serverId = getIntent().getExtras().getInt("serverId"); serverId = getIntent().getExtras().getInt("serverId");
server = (Server) Yaaic.getInstance().getServerById(serverId); server = Yaaic.getInstance().getServerById(serverId);
setTitle("Yaaic - " + server.getTitle()); setTitle("Yaaic - " + server.getTitle());
setContentView(R.layout.conversations); setContentView(R.layout.conversations);
((TextView) findViewById(R.id.title)).setText(server.getTitle()); ((TextView) findViewById(R.id.title)).setText(server.getTitle());
((EditText) findViewById(R.id.input)).setOnKeyListener(this); ((EditText) findViewById(R.id.input)).setOnKeyListener(this);
switcher = (ViewSwitcher) findViewById(R.id.switcher); switcher = (ViewSwitcher) findViewById(R.id.switcher);
dots = (ConversationSwitcher) findViewById(R.id.dots); dots = (ConversationSwitcher) findViewById(R.id.dots);
dots.setServer(server); dots.setServer(server);
@ -147,15 +147,15 @@ public class ConversationActivity extends Activity implements ServiceConnection,
deck.setAdapter(deckAdapter); deck.setAdapter(deckAdapter);
deck.setOnItemClickListener(new ConversationClickListener(deckAdapter, switcher)); deck.setOnItemClickListener(new ConversationClickListener(deckAdapter, switcher));
deck.setBackgroundDrawable(new NonScalingBackgroundDrawable(this, deck, R.drawable.background)); deck.setBackgroundDrawable(new NonScalingBackgroundDrawable(this, deck, R.drawable.background));
if (server.getStatus() == Status.PRE_CONNECTING) { if (server.getStatus() == Status.PRE_CONNECTING) {
server.clearConversations(); server.clearConversations();
deckAdapter.clearConversations(); deckAdapter.clearConversations();
} }
// Optimization : cache field lookups // Optimization : cache field lookups
Collection<Conversation> mConversations = server.getConversations(); Collection<Conversation> mConversations = server.getConversations();
for (Conversation conversation : mConversations) { for (Conversation conversation : mConversations) {
onNewConversation(conversation.getName()); onNewConversation(conversation.getName());
} }
@ -171,7 +171,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
// Create a new scrollback history // Create a new scrollback history
scrollback = new Scrollback(); scrollback = new Scrollback();
} }
/** /**
* On resume * On resume
*/ */
@ -183,12 +183,12 @@ public class ConversationActivity extends Activity implements ServiceConnection,
registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_MESSAGE)); registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_MESSAGE));
registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_NEW)); registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_NEW));
registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_REMOVE)); registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_REMOVE));
serverReceiver = new ServerReceiver(this); serverReceiver = new ServerReceiver(this);
registerReceiver(serverReceiver, new IntentFilter(Broadcast.SERVER_UPDATE)); registerReceiver(serverReceiver, new IntentFilter(Broadcast.SERVER_UPDATE));
super.onResume(); super.onResume();
// Check if speech recognition is enabled and available // Check if speech recognition is enabled and available
if (new Settings(this).isVoiceRecognitionEnabled()) { if (new Settings(this).isVoiceRecognitionEnabled()) {
PackageManager pm = getPackageManager(); PackageManager pm = getPackageManager();
@ -199,38 +199,39 @@ public class ConversationActivity extends Activity implements ServiceConnection,
speechButton.setVisibility(View.VISIBLE); speechButton.setVisibility(View.VISIBLE);
} }
} }
((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon()); ((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon());
// Start service // Start service
Intent intent = new Intent(this, IRCService.class); Intent intent = new Intent(this, IRCService.class);
intent.setAction(IRCService.ACTION_FOREGROUND); intent.setAction(IRCService.ACTION_FOREGROUND);
startService(intent); startService(intent);
bindService(intent, this, 0); bindService(intent, this, 0);
if (!server.isConnected()) { if (!server.isConnected()) {
((EditText) findViewById(R.id.input)).setEnabled(false); ((EditText) findViewById(R.id.input)).setEnabled(false);
} else { } else {
((EditText) findViewById(R.id.input)).setEnabled(true); ((EditText) findViewById(R.id.input)).setEnabled(true);
} }
// Optimization - cache field lookup // Optimization - cache field lookup
Collection<Conversation> mConversations = server.getConversations(); Collection<Conversation> mConversations = server.getConversations();
MessageListAdapter mAdapter; MessageListAdapter mAdapter;
// Fill view with messages that have been buffered while paused // Fill view with messages that have been buffered while paused
for (Conversation conversation : mConversations) { for (Conversation conversation : mConversations) {
mAdapter = conversation.getMessageListAdapter(); mAdapter = conversation.getMessageListAdapter();
if (mAdapter != null) { if (mAdapter != null) {
mAdapter.addBulkMessages(conversation.getBuffer()); mAdapter.addBulkMessages(conversation.getBuffer());
conversation.clearBuffer(); conversation.clearBuffer();
} }
} }
// Join channel that has been selected in JoinActivity (onActivityResult()) // Join channel that has been selected in JoinActivity (onActivityResult())
if (joinChannelBuffer != null) { if (joinChannelBuffer != null) {
new Thread() { new Thread() {
@Override
public void run() { public void run() {
binder.getService().getConnection(serverId).joinChannel(joinChannelBuffer); binder.getService().getConnection(serverId).joinChannel(joinChannelBuffer);
joinChannelBuffer = null; joinChannelBuffer = null;
@ -238,7 +239,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
}.start(); }.start();
} }
} }
/** /**
* On Pause * On Pause
*/ */
@ -246,7 +247,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
public void onPause() public void onPause()
{ {
super.onPause(); super.onPause();
if (binder != null && binder.getService() != null) { if (binder != null && binder.getService() != null) {
binder.getService().checkServiceStatus(); binder.getService().checkServiceStatus();
} }
@ -259,10 +260,11 @@ public class ConversationActivity extends Activity implements ServiceConnection,
/** /**
* On service connected * On service connected
*/ */
@Override
public void onServiceConnected(ComponentName name, IBinder service) public void onServiceConnected(ComponentName name, IBinder service)
{ {
this.binder = (IRCBinder) service; this.binder = (IRCBinder) service;
// connect to irc server if connect has been requested // connect to irc server if connect has been requested
if (server.getStatus() == Status.PRE_CONNECTING && getIntent().hasExtra("connect")) { if (server.getStatus() == Status.PRE_CONNECTING && getIntent().hasExtra("connect")) {
server.setStatus(Status.CONNECTING); server.setStatus(Status.CONNECTING);
@ -273,6 +275,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
/** /**
* On service disconnected * On service disconnected
*/ */
@Override
public void onServiceDisconnected(ComponentName name) public void onServiceDisconnected(ComponentName name)
{ {
this.binder = null; this.binder = null;
@ -285,14 +288,14 @@ public class ConversationActivity extends Activity implements ServiceConnection,
public boolean onCreateOptionsMenu(Menu menu) public boolean onCreateOptionsMenu(Menu menu)
{ {
super.onCreateOptionsMenu(menu); super.onCreateOptionsMenu(menu);
// inflate from xml // inflate from xml
MenuInflater inflater = getMenuInflater(); MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.conversations, menu); inflater.inflate(R.menu.conversations, menu);
return true; return true;
} }
/** /**
* On prepare options menu * On prepare options menu
*/ */
@ -302,7 +305,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
menu.getItem(0).setEnabled(server.isConnected()); // join menu.getItem(0).setEnabled(server.isConnected()); // join
menu.getItem(1).setEnabled(server.isConnected()); // users menu.getItem(1).setEnabled(server.isConnected()); // users
menu.getItem(2).setEnabled(server.isConnected()); // close menu.getItem(2).setEnabled(server.isConnected()); // close
return true; return true;
} }
@ -352,10 +355,10 @@ public class ConversationActivity extends Activity implements ServiceConnection,
} }
break; break;
} }
return true; return true;
} }
/** /**
* Get server object assigned to this activity * Get server object assigned to this activity
* *
@ -369,27 +372,28 @@ public class ConversationActivity extends Activity implements ServiceConnection,
/** /**
* On conversation message * On conversation message
*/ */
@Override
public void onConversationMessage(String target) public void onConversationMessage(String target)
{ {
Conversation conversation = server.getConversation(target); Conversation conversation = server.getConversation(target);
if (conversation == null) { if (conversation == null) {
// In an early state it can happen that the conversation object // In an early state it can happen that the conversation object
// is not created yet. // is not created yet.
return; return;
} }
MessageListAdapter adapter = conversation.getMessageListAdapter(); MessageListAdapter adapter = conversation.getMessageListAdapter();
conversation.setStatus(Conversation.STATUS_MESSAGE); conversation.setStatus(Conversation.STATUS_MESSAGE);
if (dots != null) { if (dots != null) {
dots.invalidate(); dots.invalidate();
} }
while(conversation.hasBufferedMessages()) { while(conversation.hasBufferedMessages()) {
Message message = conversation.pollBufferedMessage(); Message message = conversation.pollBufferedMessage();
if (adapter != null) { if (adapter != null) {
adapter.addMessage(message); adapter.addMessage(message);
} }
@ -399,23 +403,25 @@ public class ConversationActivity extends Activity implements ServiceConnection,
/** /**
* On new conversation * On new conversation
*/ */
@Override
public void onNewConversation(String target) public void onNewConversation(String target)
{ {
deckAdapter.addItem(server.getConversation(target)); deckAdapter.addItem(server.getConversation(target));
if (!deckAdapter.isSwitched()) { if (!deckAdapter.isSwitched()) {
// Scroll to new conversation // Scroll to new conversation
deck.setSelection(deckAdapter.getCount() - 1); deck.setSelection(deckAdapter.getCount() - 1);
} }
} }
/** /**
* On conversation remove * On conversation remove
*/ */
@Override
public void onRemoveConversation(String target) public void onRemoveConversation(String target)
{ {
deckAdapter.removeItem(target); deckAdapter.removeItem(target);
if (deckAdapter.isSwitched()) { if (deckAdapter.isSwitched()) {
switcher.showNext(); switcher.showNext();
switcher.removeView(deckAdapter.getSwitchedView()); switcher.removeView(deckAdapter.getSwitchedView());
@ -426,14 +432,15 @@ public class ConversationActivity extends Activity implements ServiceConnection,
/** /**
* On server status update * On server status update
*/ */
@Override
public void onStatusUpdate() public void onStatusUpdate()
{ {
((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon()); ((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon());
EditText input = (EditText) findViewById(R.id.input); EditText input = (EditText) findViewById(R.id.input);
if (server.isConnected()) { if (server.isConnected()) {
input.setEnabled(true); input.setEnabled(true);
} else { } else {
input.setEnabled(false); input.setEnabled(false);
@ -448,6 +455,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
builder.setMessage(getResources().getString(R.string.reconnect_after_disconnect, server.getTitle())) builder.setMessage(getResources().getString(R.string.reconnect_after_disconnect, server.getTitle()))
.setCancelable(false) .setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() { .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
binder.getService().getConnection(server.getId()).setAutojoinChannels( binder.getService().getConnection(server.getId()).setAutojoinChannels(
server.getCurrentChannelNames() server.getCurrentChannelNames()
@ -459,6 +467,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
} }
}) })
.setNegativeButton(getString(R.string.negative_button), new DialogInterface.OnClickListener() { .setNegativeButton(getString(R.string.negative_button), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); dialog.cancel();
} }
@ -491,10 +500,11 @@ public class ConversationActivity extends Activity implements ServiceConnection,
/** /**
* On key pressed (input line) * On key pressed (input line)
*/ */
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) public boolean onKey(View view, int keyCode, KeyEvent event)
{ {
EditText input = (EditText) view; EditText input = (EditText) view;
if (keyCode == KeyEvent.KEYCODE_DPAD_UP && event.getAction() == KeyEvent.ACTION_DOWN) { if (keyCode == KeyEvent.KEYCODE_DPAD_UP && event.getAction() == KeyEvent.ACTION_DOWN) {
String message = scrollback.goBack(); String message = scrollback.goBack();
if (message != null) { if (message != null) {
@ -502,7 +512,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
} }
return true; return true;
} }
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) { if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) {
String message = scrollback.goForward(); String message = scrollback.goForward();
if (message != null) { if (message != null) {
@ -510,7 +520,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
} }
return true; return true;
} }
// Nick completion // Nick completion
if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getAction() == KeyEvent.ACTION_DOWN) { if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getAction() == KeyEvent.ACTION_DOWN) {
String text = input.getText().toString(); String text = input.getText().toString();
@ -620,19 +630,19 @@ public class ConversationActivity extends Activity implements ServiceConnection,
server.getConversation(server.getSelectedConversation()).addMessage(message); server.getConversation(server.getSelectedConversation()).addMessage(message);
onConversationMessage(server.getSelectedConversation()); onConversationMessage(server.getSelectedConversation());
} }
String text = input.getText().toString(); String text = input.getText().toString();
input.setText(""); input.setText("");
if (text.equals("")) { if (text.equals("")) {
// ignore empty messages // ignore empty messages
return true; return true;
} }
scrollback.addMessage(text); scrollback.addMessage(text);
Conversation conversation = deckAdapter.getItem(deck.getSelectedItemPosition()); Conversation conversation = deckAdapter.getItem(deck.getSelectedItemPosition());
if (conversation != null) { if (conversation != null) {
if (!text.trim().startsWith("/")) { if (!text.trim().startsWith("/")) {
if (conversation.getType() != Conversation.TYPE_SERVER) { if (conversation.getType() != Conversation.TYPE_SERVER) {
@ -650,12 +660,12 @@ public class ConversationActivity extends Activity implements ServiceConnection,
CommandParser.getInstance().parse(text, server, conversation, binder.getService()); CommandParser.getInstance().parse(text, server, conversation, binder.getService());
} }
} }
return true; return true;
} }
return false; return false;
} }
/** /**
* On activity result * On activity result
*/ */
@ -666,16 +676,13 @@ public class ConversationActivity extends Activity implements ServiceConnection,
// ignore other result codes // ignore other result codes
return; return;
} }
switch (requestCode) { switch (requestCode) {
case REQUEST_CODE_SPEECH: case REQUEST_CODE_SPEECH:
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
StringBuffer buffer = new StringBuffer(); if (matches.size() > 0) {
for (String partial : matches) { ((EditText) findViewById(R.id.input)).setText(matches.get(0));
buffer.append(" ");
buffer.append(partial);
} }
((EditText) findViewById(R.id.input)).setText(buffer.toString().trim());
break; break;
case REQUEST_CODE_JOIN: case REQUEST_CODE_JOIN:
joinChannelBuffer = data.getExtras().getString("channel"); joinChannelBuffer = data.getExtras().getString("channel");
@ -719,15 +726,16 @@ public class ConversationActivity extends Activity implements ServiceConnection,
// XXX: Implement me - The action should be handled after onResume() // XXX: Implement me - The action should be handled after onResume()
// to catch the broadcasts... now we just wait a second // to catch the broadcasts... now we just wait a second
// Yes .. that's very ugly - we need some kind of queue that is handled after onResume() // Yes .. that's very ugly - we need some kind of queue that is handled after onResume()
new Thread() { new Thread() {
@Override
public void run() { public void run() {
try { try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
// Do nothing // Do nothing
} }
switch (actionId) { switch (actionId) {
case R.id.op: case R.id.op:
connection.op(conversation, nickname); connection.op(conversation, nickname);