MainActivity.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 OSRF.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package com.github.rosjava.android_apps.teleop;
00018 
00019 import android.os.Bundle;
00020 import android.view.Menu;
00021 import android.view.MenuItem;
00022 import android.view.View;
00023 import android.widget.Button;
00024 
00025 import com.github.rosjava.android_remocons.common_tools.apps.RosAppActivity;
00026 
00027 import org.ros.android.BitmapFromCompressedImage;
00028 import org.ros.android.view.RosImageView;
00029 import org.ros.android.view.VirtualJoystickView;
00030 import org.ros.namespace.NameResolver;
00031 import org.ros.node.NodeConfiguration;
00032 import org.ros.node.NodeMainExecutor;
00033 
00034 import java.io.IOException;
00035 
00039 public class MainActivity extends RosAppActivity {
00040         private RosImageView<sensor_msgs.CompressedImage> cameraView;
00041         private VirtualJoystickView virtualJoystickView;
00042         private Button backButton;
00043 
00044         public MainActivity() {
00045                 // The RosActivity constructor configures the notification title and ticker messages.
00046                 super("android teleop", "android teleop");
00047         }
00048 
00049         @SuppressWarnings("unchecked")
00050         @Override
00051         public void onCreate(Bundle savedInstanceState) {
00052 
00053                 setDashboardResource(R.id.top_bar);
00054                 setMainWindowResource(R.layout.main);
00055                 super.onCreate(savedInstanceState);
00056 
00057         cameraView = (RosImageView<sensor_msgs.CompressedImage>) findViewById(R.id.image);
00058         cameraView.setMessageType(sensor_msgs.CompressedImage._TYPE);
00059         cameraView.setMessageToBitmapCallable(new BitmapFromCompressedImage());
00060         virtualJoystickView = (VirtualJoystickView) findViewById(R.id.virtual_joystick);
00061         backButton = (Button) findViewById(R.id.back_button);
00062         backButton.setOnClickListener(new View.OnClickListener() {
00063             @Override
00064             public void onClick(View view) {
00065                 onBackPressed();
00066             }
00067         });
00068         }
00069 
00070         @Override
00071         protected void init(NodeMainExecutor nodeMainExecutor) {
00072                 
00073                 super.init(nodeMainExecutor);
00074 
00075         try {
00076             java.net.Socket socket = new java.net.Socket(getMasterUri().getHost(), getMasterUri().getPort());
00077             java.net.InetAddress local_network_address = socket.getLocalAddress();
00078             socket.close();
00079             NodeConfiguration nodeConfiguration =
00080                     NodeConfiguration.newPublic(local_network_address.getHostAddress(), getMasterUri());
00081 
00082         String joyTopic = remaps.get(getString(R.string.joystick_topic));
00083         String camTopic = remaps.get(getString(R.string.camera_topic));
00084 
00085         NameResolver appNameSpace = getMasterNameSpace();
00086         joyTopic = appNameSpace.resolve(joyTopic).toString();
00087         camTopic = appNameSpace.resolve(camTopic).toString();
00088 
00089                 cameraView.setTopicName(camTopic);
00090         virtualJoystickView.setTopicName(joyTopic);
00091                 
00092                 nodeMainExecutor.execute(cameraView, nodeConfiguration
00093                                 .setNodeName("android/camera_view"));
00094                 nodeMainExecutor.execute(virtualJoystickView,
00095                                 nodeConfiguration.setNodeName("android/virtual_joystick"));
00096         } catch (IOException e) {
00097             // Socket problem
00098         }
00099 
00100         }
00101         
00102           @Override
00103           public boolean onCreateOptionsMenu(Menu menu){
00104                   menu.add(0,0,0,R.string.stop_app);
00105 
00106                   return super.onCreateOptionsMenu(menu);
00107           }
00108           
00109           @Override
00110           public boolean onOptionsItemSelected(MenuItem item){
00111                   super.onOptionsItemSelected(item);
00112                   switch (item.getItemId()){
00113                   case 0:
00114                           onDestroy();
00115                           break;
00116                   }
00117                   return true;
00118           }
00119 }


android_apps
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Thu Jun 6 2019 21:11:28