Added voice recognition to yaaic

This commit is contained in:
Sebastian Kaspari 2010-08-29 16:42:50 +02:00
parent a1731b597b
commit 17aba7ca71
3 changed files with 108 additions and 6 deletions

View File

@ -60,10 +60,21 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
android:id="@+id/dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionSend" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:imeOptions="actionSend" />
<Button
android:id="@+id/speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/ic_btn_speak_now" />
</LinearLayout>
</LinearLayout>

View File

@ -20,7 +20,9 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/
package org.yaaic.activity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.yaaic.R;
import org.yaaic.Yaaic;
@ -35,6 +37,7 @@ import org.yaaic.listener.ConversationClickListener;
import org.yaaic.listener.ConversationListener;
import org.yaaic.listener.ConversationSelectedListener;
import org.yaaic.listener.ServerListener;
import org.yaaic.listener.SpeechClickListener;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Conversation;
import org.yaaic.model.Extra;
@ -54,8 +57,11 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.speech.RecognizerIntent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
@ -63,6 +69,7 @@ import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
@ -77,6 +84,8 @@ import android.widget.ViewSwitcher;
*/
public class ConversationActivity extends Activity implements ServiceConnection, ServerListener, ConversationListener, OnKeyListener
{
public static final int REQUEST_CODE_SPEECH = 99;
private static final int REQUEST_CODE_JOIN = 1;
private static final int REQUEST_CODE_USERS = 2;
private static final int REQUEST_CODE_USER = 3;
@ -135,6 +144,16 @@ public class ConversationActivity extends Activity implements ServiceConnection,
deckAdapter.clearConversations();
}
// Check if speech recognition is available
PackageManager pm = getPackageManager();
Button speechButton = (Button) findViewById(R.id.speech);
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) {
((Button) findViewById(R.id.speech)).setOnClickListener(new SpeechClickListener(this));
} else {
speechButton.setVisibility(View.GONE);
}
// Optimization : cache field lookups
Collection<Conversation> mConversations = server.getConversations();
@ -530,6 +549,15 @@ public class ConversationActivity extends Activity implements ServiceConnection,
}
switch (requestCode) {
case REQUEST_CODE_SPEECH:
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
StringBuffer buffer = new StringBuffer();
for (String partial : matches) {
buffer.append(" ");
buffer.append(partial);
}
((EditText) findViewById(R.id.input)).setText(buffer.toString().trim());
break;
case REQUEST_CODE_JOIN:
joinChannelBuffer = data.getExtras().getString("channel");
break;

View File

@ -0,0 +1,63 @@
/*
Yaaic - Yet Another Android IRC Client
Copyright 2009-2010 Sebastian Kaspari
This file is part of Yaaic.
Yaaic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Yaaic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/
package org.yaaic.listener;
import org.yaaic.activity.ConversationActivity;
import android.app.Activity;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
/**
* OnClickListener for the Speech Recognition Button
*
* @author Sebastian Kaspari <sebastian@yaaic.org>
*/
public class SpeechClickListener implements OnClickListener
{
private Activity activity;
/**
* Create a new listener for speech button
*
* @param activity
* @param input
*/
public SpeechClickListener(Activity activity)
{
this.activity = activity;
}
/**
* On Click on speech button
*/
public void onClick(View v)
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
activity.startActivityForResult(intent, ConversationActivity.REQUEST_CODE_SPEECH);
}
}