RosserialLauncherActivity.java
Go to the documentation of this file.
00001 package org.ros.rosserial;
00002 
00003 import org.ros.message.rosserial_msgs.TopicInfo;
00004 import org.ros.rosserial.ROSSerialADK;
00005 import org.ros.rosserial.ROSSerialADKService.LocalBinder;
00006 
00007 import android.app.Activity;
00008 import android.app.ActivityManager;
00009 import android.app.ActivityManager.RunningServiceInfo;
00010 import android.content.ComponentName;
00011 import android.content.Context;
00012 import android.content.Intent;
00013 import android.content.ServiceConnection;
00014 import android.os.Bundle;
00015 import android.os.IBinder;
00016 import android.util.Log;
00017 import android.view.View;
00018 import android.view.View.OnClickListener;
00019 import android.widget.Button;
00020 import android.widget.TextView;
00021 import android.widget.Toast;
00022 
00023 public class RosserialLauncherActivity extends Activity {
00024 
00025         static final String TAG = "RosserialLauncherActivity";
00026         
00027         ROSSerialADK adk = null;
00028         Context mContext = null;
00029         LocalBinder mBinder= null;
00030         
00031         static private class TextViewHandler implements Runnable{
00032                 TextView view;
00033                 String msg;
00034                 TextViewHandler(TextView v, String text){
00035                         view = v;
00036                         msg = text;
00037                 }
00038                 @Override
00039                 public void run() {
00040                         view.setText(msg);
00041                 }
00042         }
00043 
00044         @Override
00045         protected void onCreate(Bundle savedInstanceState) {
00046                 super.onCreate(savedInstanceState);
00047                 setContentView(R.layout.launcher);
00048         
00049                 mContext = this;
00050                 
00051                 Button b = (Button) findViewById(R.id.StartButton);
00052                 b.setOnClickListener(start_click_listener);
00053                 
00054                 b = (Button) findViewById(R.id.StopButton);
00055                 b.setOnClickListener(new OnClickListener() {
00056                         
00057                         @Override
00058                         public void onClick(View arg0) {
00059 
00060                                 Log.v(TAG, "Trying to stop service");
00061                                 try{
00062                                         unbindService(mConnection);
00063                                         stopService(new Intent(mContext, ROSSerialADKService.class));
00064                                 }
00065                                 catch(IllegalArgumentException e){
00066                                         Toast t = Toast.makeText(mContext, "All instances of ROSSeriakADK are stopped.", Toast.LENGTH_SHORT);
00067                                         t.show();
00068                                 }
00069                         }
00070                 });
00071                 
00072         }
00073 
00074         
00075         @Override
00076         protected void onResume() {
00077 
00078                 super.onResume();
00079                 if (isADKRunning()){
00080                         //bind to it
00081                         
00082                         Intent i =  new Intent(mContext, ROSSerialADKService.class);
00083                         try{
00084                                 bindService(i, mConnection, 0);
00085                         }
00086                         catch(Exception e){
00087                                 e.printStackTrace();
00088                         }
00089                 }
00090         }
00091 
00092 
00093         OnClickListener start_click_listener = new OnClickListener() {
00094                 
00095                 @Override
00096                 public void onClick(View v) {
00097                         TextView txt_MasterURi = (TextView) findViewById(R.id.MasterURITxt);
00098                         TextView txt_NodeName =  (TextView) findViewById(R.id.NodeNameTxt);
00099                         
00100                         Intent i =  new Intent(mContext, ROSSerialADKService.class);
00101 
00102                         i.putExtra("ROS_MASTER_URI", "http://"+txt_MasterURi.getText());
00103                         i.putExtra("node name", txt_NodeName.getText());
00104                         
00105                         mContext.startService(i);
00106                         bindService(i, mConnection, 0);
00107                         Log.v(TAG,"Started sdk service");
00108                 }
00109         };
00110         
00111         private boolean isADKRunning() {
00112             ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
00113             for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
00114                 if ("org.ros.rosserial.ROSSerialADKService".equals(service.service.getClassName())) {
00115                     return true;
00116                 }
00117             }
00118             return false;
00119         }
00120         
00121         void setADKConnected(){
00122                 TextView v = (TextView) findViewById(R.id.ConncectionStatusView);
00123                 v.setText("ADK Is connected");
00124         }
00125         
00126         static void displayTopics(TextView v, String type, TopicInfo[] infos){
00127                 int l =type.length();
00128                 for(TopicInfo info : infos){
00129                         l+= info.topic_name.length();
00130                         l+= info.message_type.length();
00131                 }
00132                 StringBuilder msg = new StringBuilder(l+infos.length*5);
00133                 msg.append(type);
00134                 for(TopicInfo info : infos){
00135                         msg.append(" : \n");
00136 
00137                         msg.append("  ");
00138                         msg.append(info.topic_name);
00139                         msg.append(" - ");
00140                         msg.append(info.message_type);
00141                 }
00142                 
00143                 v.post(new TextViewHandler(v, msg.toString()) );                                        
00144                 
00145         }
00146         
00147         private ServiceConnection mConnection = new ServiceConnection() {
00148                 
00149                 @Override
00150                 public void onServiceDisconnected(ComponentName name) {
00151                         
00152                 }
00153         
00154                 
00155                 @Override
00156                 public void onServiceConnected(ComponentName name, IBinder service) {
00157                         Log.v(TAG, "Binder service connected");
00158                         
00159                         mBinder =  (LocalBinder) service;
00160                         
00161                         adk = mBinder.getADK();
00162                         
00163                         adk.setOnPublicationCB(new TopicRegistrationListener() {
00164                                 
00165                                 @Override
00166                                 public void onNewTopic(TopicInfo arg0) {
00167                                         TextView v = (TextView) findViewById(R.id.PublicationsView);
00168                                         TopicInfo[] topics = adk.getPublications();
00169                                         displayTopics(v, "Publications", topics);
00170                                 }
00171                         });
00172                         
00173                         adk.setOnSubscriptionCB(new TopicRegistrationListener() {
00174                                 
00175                                 @Override
00176                                 public void onNewTopic(TopicInfo arg0) {
00177                                         TextView v = (TextView) findViewById(R.id.SubscriptionsView);
00178                                         TopicInfo[] topics = adk.getSubscriptions();
00179                                         displayTopics(v, "Subscriptions", topics);
00180                                 }
00181                         });
00182                                                 
00183                         
00184                         if (adk.isConnected()) {
00185                                 setADKConnected();
00186                                 
00187                                 displayTopics( (TextView) findViewById(R.id.PublicationsView),
00188                                      "Publications", adk.getPublications()) ;
00189                                 
00190                                 displayTopics( (TextView) findViewById(R.id.SubscriptionsView),
00191                                              "Subscriptions", adk.getSubscriptions()) ;
00192                         }
00193                         else{
00194                                 adk.setOnConnectonListener(new ROSSerialADK.onConnectionListener() {
00195                                         @Override
00196                                         public void trigger(boolean connection) {
00197                                                 TextView v = (TextView) findViewById(R.id.ConncectionStatusView);
00198                                                 if (connection) v.post(new TextViewHandler(v,"ADK Is Connected"));
00199                                                 else v.post(new TextViewHandler(v, "ADK is Disconnected"));
00200                                         }
00201                                 });
00202                         }
00203                         
00204 
00205                 }
00206         };
00207 }                     


rosserial_android
Author(s): Adam Stambler
autogenerated on Mon Dec 2 2013 12:02:05