Go to the documentation of this file.00001 package org.ros.android.androidVoiceMessage;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import android.os.Bundle;
00020 import android.os.IBinder;
00021
00022 import org.ros.android.MessageCallable;
00023 import org.ros.android.RosActivity;
00024 import org.ros.node.NodeConfiguration;
00025 import org.ros.node.NodeMainExecutor;
00026 import org.ros.rosjava_tutorial_pubsub.Talker;
00027
00028
00029 import android.app.AlertDialog;
00030 import android.content.ComponentName;
00031 import android.content.Context;
00032 import android.content.DialogInterface;
00033 import android.content.Intent;
00034 import android.content.ServiceConnection;
00035 import android.hardware.Sensor;
00036 import android.hardware.SensorEvent;
00037 import android.hardware.SensorEventListener;
00038 import android.hardware.SensorManager;
00039 import android.speech.RecognitionListener;
00040 import android.speech.RecognizerIntent;
00041 import android.speech.SpeechRecognizer;
00042 import android.view.Menu;
00043 import android.view.MenuItem;
00044 import android.view.View;
00045 import android.view.SurfaceView;
00046 import android.widget.Button;
00047 import android.widget.CheckBox;
00048 import android.widget.ImageView;
00049 import android.widget.TextView;
00050
00051
00052 import java.net.URI;
00053 import java.net.URISyntaxException;
00054 import java.util.ArrayList;
00055 import org.ros.node.topic.Publisher;
00056
00057 import java.util.List;
00058 import jsk_gui_msgs.VoiceMessage;
00059
00060
00061 public class AndroidVoiceMessage extends RosActivity {
00062
00063 private URI masterUri;
00064 private TextView textView;
00065 private ImageView imageView;
00066 private String packageName;
00067 private int mode;
00068
00069 SpeechRecognizer speechRecignizer;
00070 private SensorManager sensorManager;
00071 private Intent intent;
00072 private AndroidVoiceMessageService androidVoiceMessageService;
00073 private AndroidVoiceMessageView androidVoiceMessageView;
00074
00075 ServiceConnection serviceConnection = new ServiceConnection() {
00076
00077 @Override
00078 public void onServiceConnected(ComponentName name, IBinder service) {
00079 androidVoiceMessageService = ((AndroidVoiceMessageService.MyBinder) service)
00080 .getService();
00081 startService(intent);
00082 imageView = (ImageView)findViewById(R.id.ImageView);
00083 androidVoiceMessageService.setNode(speechRecignizer, textView,imageView, packageName);
00084 }
00085
00086 @Override
00087 public void onServiceDisconnected(ComponentName name) {
00088 androidVoiceMessageService = null;
00089 }
00090
00091 };
00092
00093 public AndroidVoiceMessage() {
00094 super("AndroidVoiceMessage", "AndroidVoiceMessage");
00095 }
00096
00097 @Override
00098 public void onCreate(Bundle savedInstanceState) {
00099 super.onCreate(savedInstanceState);
00100 setContentView(R.layout.main);
00101 mode = 1;
00102 textView = (TextView) findViewById(R.id.text);
00103 imageView = (ImageView) findViewById(R.id.ImageView);
00104 imageView.setOnClickListener(new View.OnClickListener() {
00105 @Override
00106 public void onClick(View v) {
00107 androidVoiceMessageService.setStart();
00108 }
00109 });
00110
00111
00112 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
00113 speechRecignizer = SpeechRecognizer.createSpeechRecognizer(this);
00114 packageName = getPackageName();
00115 androidVoiceMessageView = new AndroidVoiceMessageView(this);
00116 }
00117
00118 @Override
00119 public void onResume() {
00120 super.onResume();
00121 if (masterUri != null) {
00122
00123 intent = new Intent(getBaseContext(),
00124 AndroidVoiceMessageService.class);
00125 intent.putExtra("masterUri", masterUri.toString());
00126 bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
00127
00128 }
00129
00130 }
00131
00132 @Override
00133 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
00134 if (requestCode == 0 && resultCode == RESULT_OK) {
00135 try {
00136 masterUri = new URI(data.getStringExtra("ROS_MASTER_URI"));
00137 } catch (URISyntaxException e) {
00138 throw new RuntimeException(e);
00139 }
00140 }
00141 }
00142
00143 @Override
00144 public boolean onCreateOptionsMenu(Menu menu) {
00145 menu.add(0,0,0,R.string.change_mode);
00146 menu.add(0, 1, 1, R.string.app_about);
00147 menu.add(0, 2, 1, R.string.str_exit);
00148 return super.onCreateOptionsMenu(menu);
00149 }
00150
00151 @Override
00152 public boolean onOptionsItemSelected(MenuItem item) {
00153 super.onOptionsItemSelected(item);
00154 switch (item.getItemId()) {
00155 case 0:
00156 mode *= -1;
00157 if(mode == 1){
00158 setContentView(R.layout.main);
00159 textView = (TextView) findViewById(R.id.text);
00160 imageView = (ImageView) findViewById(R.id.ImageView);
00161 imageView.setOnClickListener(new View.OnClickListener() {
00162 @Override
00163 public void onClick(View v) {
00164 androidVoiceMessageService.setStart();
00165 }
00166 });
00167 androidVoiceMessageService.setView(textView,imageView);
00168 androidVoiceMessageService.setRawModeStop();
00169 }
00170 else{
00171 setContentView(R.layout.main2);
00172 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.SV);
00173 androidVoiceMessageView.setView(surfaceView);
00174 androidVoiceMessageService.setRawModeStart(androidVoiceMessageView);
00175
00176 }
00177 break;
00178 case 1:
00179 openOptionsDialog();
00180 break;
00181 case 2:
00182 exitOptionsDialog();
00183 break;
00184 }
00185 return true;
00186 }
00187
00188 private void openOptionsDialog() {
00189 new AlertDialog.Builder(this)
00190 .setTitle(R.string.app_about)
00191 .setMessage(R.string.app_about_message)
00192 .setPositiveButton(R.string.str_ok,
00193 new DialogInterface.OnClickListener() {
00194 public void onClick(
00195 DialogInterface dialoginterface, int i) {
00196 }
00197 }).show();
00198 }
00199
00200 private void exitOptionsDialog() {
00201 stopService(new Intent(this, AndroidVoiceMessageService.class));
00202 finish();
00203 }
00204
00205 @Override
00206 protected void init(NodeMainExecutor nodeMainExecutor) {
00207
00208 }
00209 }