AndroidVoiceMessage.java
Go to the documentation of this file.
00001 package org.ros.android.androidVoiceMessage;
00002 
00003 /*
00004  * Copyright (C) 2011 Google Inc.
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00007  * use this file except in compliance with the License. You may obtain a copy of
00008  * the License at
00009  *
00010  * http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00014  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00015  * License for the specific language governing permissions and limitations under
00016  * the License.
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 
00027 //add
00028 import android.app.AlertDialog;
00029 import android.content.ComponentName;
00030 import android.content.Context;
00031 import android.content.DialogInterface;
00032 import android.content.Intent;
00033 import android.content.ServiceConnection;
00034 import android.hardware.Sensor;
00035 import android.hardware.SensorEvent;
00036 import android.hardware.SensorEventListener;
00037 import android.hardware.SensorManager;
00038 import android.speech.RecognitionListener;
00039 import android.speech.RecognizerIntent;
00040 import android.speech.SpeechRecognizer;
00041 import android.view.Menu;
00042 import android.view.MenuItem;
00043 import android.view.View;
00044 import android.view.SurfaceView;
00045 import android.view.WindowManager;
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 Button button;
00067         private String packageName;
00068         private int mode;
00069         private boolean eternal = false;
00070 
00071         private SensorManager sensorManager;
00072         private Intent intent;
00073         private AndroidVoiceMessageService androidVoiceMessageService;
00074         private AndroidVoiceMessageView androidVoiceMessageView;
00075 
00076         ServiceConnection serviceConnection = new ServiceConnection() {
00077 
00078                 @Override
00079                 public void onServiceConnected(ComponentName name, IBinder service) {
00080                         androidVoiceMessageService = ((AndroidVoiceMessageService.MyBinder) service)
00081                                         .getService();
00082                         startService(intent);
00083                         imageView = (ImageView)findViewById(R.id.ImageView);
00084                         androidVoiceMessageService.setNode(AndroidVoiceMessage.this, textView,imageView, packageName);
00085                 }
00086 
00087                 @Override
00088                 public void onServiceDisconnected(ComponentName name) {
00089                         androidVoiceMessageService = null;
00090                 }
00091 
00092         };
00093 
00094         public AndroidVoiceMessage() {
00095                 super("ROS Voice Recognition", "ROS Voice Recognition");
00096         }
00097 
00098         @Override
00099         public void onCreate(Bundle savedInstanceState) {
00100                 super.onCreate(savedInstanceState);
00101                 
00102                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
00103                 
00104                 setContentView(R.layout.main);
00105                 mode = 1;
00106                 textView = (TextView) findViewById(R.id.text);
00107                 imageView = (ImageView) findViewById(R.id.ImageView);
00108                 imageView.setOnClickListener(new View.OnClickListener() {
00109                         @Override
00110                         public void onClick(View v) {
00111                                 androidVoiceMessageService.setStart();
00112                         }
00113                 });
00114                 button = (Button)findViewById(R.id.button);
00115                 button.setOnClickListener(new View.OnClickListener() {
00116                         @Override
00117                         public void onClick(View v) {
00118                                 if(eternal) {
00119                                         eternal = false;
00120                                         androidVoiceMessageService.setMode(1);
00121                                         button.setText("Once"); 
00122                                 }
00123                                 else {
00124                                         eternal = true;
00125                                         androidVoiceMessageService.setMode(-1);
00126                                         button.setText("Eternal");
00127                                 }
00128                         }
00129                 });
00130 
00131                 // add
00132                 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
00133                 packageName = getPackageName();
00134                 androidVoiceMessageView = new AndroidVoiceMessageView(this);
00135         }
00136 
00137         @Override
00138         public void onResume() {
00139                 super.onResume();
00140                 if (masterUri != null) {
00141 
00142                         intent = new Intent(getBaseContext(),
00143                                         AndroidVoiceMessageService.class);
00144                         intent.putExtra("masterUri", masterUri.toString());
00145                         bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
00146 
00147                 }
00148 
00149         }
00150 
00151         @Override
00152         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
00153                 if (requestCode == 0 && resultCode == RESULT_OK) {
00154                         try {
00155                                 masterUri = new URI(data.getStringExtra("ROS_MASTER_URI"));
00156                         } catch (URISyntaxException e) {
00157                                 throw new RuntimeException(e);
00158                         }
00159                 }
00160         }
00161 
00162         @Override
00163         public boolean onCreateOptionsMenu(Menu menu) {
00164                 menu.add(0,0,0,R.string.change_mode);
00165                 menu.add(0, 1, 1, R.string.app_about);
00166                 menu.add(0, 2, 1, R.string.str_exit);
00167                 return super.onCreateOptionsMenu(menu);
00168         }
00169 
00170         @Override
00171         public boolean onOptionsItemSelected(MenuItem item) {
00172                 super.onOptionsItemSelected(item);
00173                 switch (item.getItemId()) {
00174                 case 0:
00175                         mode *= -1;
00176                         if(mode == 1){
00177                                 setContentView(R.layout.main);
00178                                 eternal = false;
00179                                 textView = (TextView) findViewById(R.id.text);
00180                                 imageView = (ImageView) findViewById(R.id.ImageView);
00181                                 imageView.setOnClickListener(new View.OnClickListener() {
00182                                         @Override
00183                                         public void onClick(View v) {
00184                                                 androidVoiceMessageService.setStart();
00185                                         }
00186                                 });
00187 
00188                                 button = (Button)findViewById(R.id.button);
00189                                 button.setOnClickListener(new View.OnClickListener() {
00190                                         @Override
00191                                         public void onClick(View v) {
00192                                                 if(eternal) {
00193                                                         eternal = false;
00194                                                         androidVoiceMessageService.setMode(1);
00195                                                         button.setText("Once"); 
00196                                                 }
00197                                                 else {
00198                                                         eternal = true;
00199                                                         androidVoiceMessageService.setMode(-1);
00200                                                         button.setText("Eternal");
00201                                                 }
00202                                         }
00203                                 });
00204                                 
00205                                 androidVoiceMessageService.setView(textView,imageView);
00206                                 androidVoiceMessageService.setRawModeStop();
00207                         }
00208                         else{
00209                                 androidVoiceMessageService.setMode(1);
00210                                 setContentView(R.layout.main2);
00211                                 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.SV);
00212                                 androidVoiceMessageView.setView(surfaceView);
00213                                 androidVoiceMessageService.setRawModeStart(androidVoiceMessageView);
00214 
00215                         }
00216                         break;
00217                 case 1:
00218                         openOptionsDialog();
00219                         break;
00220                 case 2:
00221                         exitOptionsDialog();
00222                         break;
00223                 }
00224                 return true;
00225         }
00226 
00227         private void openOptionsDialog() {
00228                 new AlertDialog.Builder(this)
00229                                 .setTitle(R.string.app_about)
00230                                 .setMessage(R.string.app_about_message)
00231                                 .setPositiveButton(R.string.str_ok,
00232                                                 new DialogInterface.OnClickListener() {
00233                                                         public void onClick(
00234                                                                         DialogInterface dialoginterface, int i) {
00235                                                         }
00236                                                 }).show();
00237         }
00238 
00239         private void exitOptionsDialog() {
00240                 stopService(new Intent(this, AndroidVoiceMessageService.class));
00241                 finish();
00242         }
00243 
00244         @Override
00245         protected void init(NodeMainExecutor nodeMainExecutor) {
00246 
00247         }
00248 }


jsk_android_apps
Author(s): Kazuto Murase
autogenerated on Thu Jun 6 2019 18:03:46