Go to the documentation of this file.00001 package org.ros.android.androidVoiceMessage;
00002
00003 import java.net.URI;
00004 import java.net.URISyntaxException;
00005 import java.util.List;
00006
00007 import org.ros.address.InetAddressFactory;
00008 import org.ros.android.androidVoiceMessage.AndroidVoiceMessageNode;
00009 import org.ros.node.DefaultNodeMainExecutor;
00010 import org.ros.node.NodeConfiguration;
00011 import org.ros.node.NodeMain;
00012 import org.ros.node.NodeMainExecutor;
00013
00014 import android.app.IntentService;
00015 import android.app.PendingIntent;
00016 import android.content.Intent;
00017 import android.hardware.Sensor;
00018 import android.hardware.SensorManager;
00019 import android.os.Binder;
00020 import android.os.Bundle;
00021 import android.os.IBinder;
00022 import android.speech.SpeechRecognizer;
00023 import android.util.Log;
00024 import android.widget.TextView;
00025 import android.widget.ImageView;
00026
00027
00028 public class AndroidVoiceMessageService extends IntentService {
00029
00030 SensorManager sensorManager;
00031 List<Sensor> sensors;
00032 AndroidVoiceMessageNode androidVoiceMessageNode;
00033 Intent mintent;
00034 NodeMainExecutor nodeMainExecutor;
00035 private String packageName;
00036 SpeechRecognizer speechRecognizer;
00037
00038 final IBinder binder = new MyBinder();
00039
00040 public class MyBinder extends Binder {
00041 AndroidVoiceMessageService getService() {
00042 return AndroidVoiceMessageService.this;
00043 }
00044 }
00045
00046 public AndroidVoiceMessageService() {
00047 super("AndroidVoiceMessageService");
00048 }
00049
00050 @Override
00051 public IBinder onBind(Intent intent) {
00052 mintent = intent;
00053 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
00054 sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
00055 return binder;
00056 }
00057
00058 public void setNode(SpeechRecognizer speechRecognizer, TextView textView, ImageView imageView, String packageName) {
00059 showNotification();
00060 this.speechRecognizer = speechRecognizer;
00061 this.packageName = packageName;
00062
00063
00064 Bundle b = mintent.getExtras();
00065
00066 if (this.androidVoiceMessageNode == null) {
00067 try {
00068 URI masterUri = new URI(b.getString("masterUri"));
00069 String hostLocal = InetAddressFactory.newNonLoopback()
00070 .getHostAddress();
00071 nodeMainExecutor = DefaultNodeMainExecutor.newDefault();
00072
00073 NodeConfiguration nodeConfiguration = NodeConfiguration
00074 .newPublic(hostLocal, masterUri);
00075 nodeConfiguration.setMasterUri(masterUri);
00076 nodeConfiguration.setNodeName("android_voice_message");
00077
00078 androidVoiceMessageNode = new AndroidVoiceMessageNode(
00079 sensorManager, speechRecognizer, packageName);
00080
00081 setView(textView,imageView);
00082
00083 nodeMainExecutor.execute(androidVoiceMessageNode,
00084 nodeConfiguration);
00085
00086
00087 } catch (URISyntaxException e) {
00088 e.printStackTrace();
00089 }
00090 }
00091 }
00092
00093 public void setStart() {
00094 androidVoiceMessageNode.setFlag();
00095 }
00096
00097 public void setRawModeStart(AndroidVoiceMessageView androidVoiceMessageView) {
00098 androidVoiceMessageNode.publishRawSound(androidVoiceMessageView);
00099 }
00100
00101 public void setRawModeStop(){
00102 androidVoiceMessageNode.stopRawSound();
00103 }
00104
00105 public void setView(TextView textView,ImageView imageView){
00106 androidVoiceMessageNode.setView(textView,imageView);
00107 }
00108
00109 @Override
00110 protected void onHandleIntent(Intent intent) {
00111 try {
00112 Thread.sleep(10000);
00113 } catch (InterruptedException e) {
00114 e.printStackTrace();
00115 }
00116
00117 }
00118
00119 private void showNotification() {
00120
00121 Intent intent = new Intent(this, AndroidVoiceMessage.class);
00122
00123 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
00124 | Intent.FLAG_ACTIVITY_SINGLE_TOP);
00125
00126 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
00127 intent, 0);
00128
00129 }
00130
00131 @Override
00132 public void onDestroy() {
00133 nodeMainExecutor.shutdownNodeMain(androidVoiceMessageNode);
00134 }
00135 }