RobotActivity.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Daniel Stonier.
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_remocons.robot_remocon;
00018 
00019 import java.net.URI;
00020 import java.net.URISyntaxException;
00021 
00022 import android.content.Intent;
00023 import android.os.AsyncTask;
00024 import android.os.Bundle;
00025 import android.util.Log;
00026 import android.view.Window;
00027 import android.view.WindowManager;
00028 import android.widget.LinearLayout;
00029 
00030 import org.ros.address.InetAddressFactory;
00031 import org.ros.android.RosActivity;
00032 import org.ros.exception.RemoteException;
00033 import org.ros.exception.RosRuntimeException;
00034 import org.ros.namespace.NameResolver;
00035 import org.ros.node.NodeConfiguration;
00036 import org.ros.node.NodeMainExecutor;
00037 import org.ros.node.service.ServiceResponseListener;
00038 
00039 import com.github.rosjava.android_apps.application_management.AppManager;
00040 import com.github.rosjava.android_apps.application_management.Dashboard;
00041 import com.github.rosjava.android_apps.application_management.RobotDescription;
00042 import com.github.rosjava.android_apps.application_management.RobotNameResolver;
00043 
00044 import com.github.rosjava.android_apps.application_management.rapp_manager.PairingApplicationNamePublisher;
00045 
00046 import rocon_app_manager_msgs.StopAppResponse;
00047 
00061 public abstract class RobotActivity extends RosActivity {
00062 
00063         private String robotAppName = null;
00064         private String defaultRobotAppName = null;
00065         private String defaultRobotName = null;
00066     /*
00067       By default we assume the remocon has just launched independantly, however
00068       it can be launched upon the closure of one of its children applications.
00069      */
00070     protected boolean fromApplication = false;  // true if it is a remocon activity getting control from a closing application
00071 
00072         private int dashboardResourceId = 0;
00073         private int mainWindowId = 0;
00074         private Dashboard dashboard = null;
00075         protected NodeConfiguration nodeConfiguration;
00076     protected NodeMainExecutor nodeMainExecutor;
00077         protected RobotNameResolver robotNameResolver;
00078         protected RobotDescription robotDescription;
00079     protected PairingApplicationNamePublisher pairingApplicationNamePublisher = null;
00080 
00081         protected void setDashboardResource(int resource) {
00082                 dashboardResourceId = resource;
00083         }
00084 
00085         protected void setMainWindowResource(int resource) {
00086                 mainWindowId = resource;
00087         }
00088 
00089         protected void setDefaultRobotName(String name) {
00090                 defaultRobotName = name;
00091         }
00092 
00093         protected void setDefaultAppName(String name) {
00094         defaultRobotAppName = name;
00095         }
00096 
00097         protected void setCustomDashboardPath(String path) {
00098                 dashboard.setCustomDashboardPath(path);
00099         }
00100 
00101         protected RobotActivity(String notificationTicker, String notificationTitle) {
00102                 super(notificationTicker, notificationTitle);
00103         }
00104 
00105         @Override
00106         public void onCreate(Bundle savedInstanceState) {
00107                 super.onCreate(savedInstanceState);
00108 
00109                 if (mainWindowId == 0) {
00110                         Log.e("RobotRemocon",
00111                                         "You must set the dashboard resource ID in your RobotActivity");
00112                         return;
00113                 }
00114                 if (dashboardResourceId == 0) {
00115                         Log.e("RobotRemocon",
00116                                         "You must set the dashboard resource ID in your RobotActivity");
00117                         return;
00118                 }
00119 
00120                 requestWindowFeature(Window.FEATURE_NO_TITLE);
00121                 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
00122                                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
00123                 setContentView(mainWindowId);
00124 
00125                 robotNameResolver = new RobotNameResolver();
00126 
00127                 if (defaultRobotName != null) {
00128                         robotNameResolver.setRobotName(defaultRobotName);
00129                 }
00130 
00131                 robotAppName = getIntent().getStringExtra(
00132                                 AppManager.PACKAGE + ".robot_app_name");
00133                 if (robotAppName == null) {
00134                         robotAppName = defaultRobotAppName;
00135         } else if (robotAppName.equals("AppChooser")) { // ugly legacy identifier, it's misleading so change it sometime
00136             Log.i("RobotRemocon", "reinitialising from a closing remocon application");
00137             fromApplication = true;
00138                 } else {
00139                         // DJS: do we need anything here? I think the first two cases cover everything
00140                 }
00141 
00142                 if (dashboard == null) {
00143                         dashboard = new Dashboard(this);
00144                         dashboard.setView((LinearLayout) findViewById(dashboardResourceId),
00145                                         new LinearLayout.LayoutParams(
00146                                                         LinearLayout.LayoutParams.WRAP_CONTENT,
00147                                                         LinearLayout.LayoutParams.WRAP_CONTENT));
00148                 }
00149         }
00150 
00161         @Override
00162         protected void init(NodeMainExecutor nodeMainExecutor) {
00163                 this.nodeMainExecutor = nodeMainExecutor;
00164         nodeConfiguration = NodeConfiguration.newPublic(InetAddressFactory
00165                 .newNonLoopback().getHostAddress(), getMasterUri());
00166 
00167         // robotDescription will get set by the robot master chooser as it exits
00168         // or passed back as an intent from a closing remocon application.
00169         // It should never be null!
00170         robotNameResolver.setRobot(robotDescription);
00171         dashboard.setRobotName(robotDescription.getRobotType());
00172         pairingApplicationNamePublisher = new PairingApplicationNamePublisher("Robot Remocon");
00173         nodeMainExecutor.execute(pairingApplicationNamePublisher,
00174                 nodeConfiguration.setNodeName("pairingApplicationNamePublisher"));
00175         nodeMainExecutor.execute(robotNameResolver,
00176                 nodeConfiguration.setNodeName("robotNameResolver"));
00177         robotNameResolver.waitForResolver();
00178         nodeMainExecutor.execute(dashboard,
00179                 nodeConfiguration.setNodeName("dashboard"));
00180         // Child application post-handling
00181         if (fromApplication) {
00182             stopApp();
00183         }
00184     }
00185 
00186         protected NameResolver getAppNameSpace() {
00187                 return robotNameResolver.getAppNameSpace();
00188         }
00189 
00190         protected NameResolver getRobotNameSpaceResolver() {
00191                 return robotNameResolver.getRobotNameSpace();
00192         }
00193 
00194     protected String getRobotNameSpace() {
00195         return robotNameResolver.getRobotNameSpace().getNamespace().toString();
00196     }
00197 
00198         protected void stopApp() {
00199                 Log.i("RobotRemocon", "android application stopping a rapp [" + robotAppName + "]");
00200                 AppManager appManager = new AppManager(robotAppName,
00201                                 getRobotNameSpaceResolver());
00202                 appManager.setFunction("stop");
00203 
00204                 appManager
00205                                 .setStopService(new ServiceResponseListener<StopAppResponse>() {
00206                                         @Override
00207                                         public void onSuccess(StopAppResponse message) {
00208                         if ( message.getStopped() ) {
00209                                                     Log.i("RobotRemocon", "rapp stopped successfully");
00210                         } else {
00211                             Log.i("RobotRemocon", "stop rapp request rejected [" + message.getMessage() + "]");
00212                         }
00213                                         }
00214 
00215                                         @Override
00216                                         public void onFailure(RemoteException e) {
00217                                                 Log.e("RobotRemocon", "rapp failed to stop when requested!");
00218                                         }
00219                                 });
00220                 nodeMainExecutor.execute(appManager,
00221                                 nodeConfiguration.setNodeName("stop_app"));
00222         }
00223 
00224         protected void releaseRobotNameResolver() {
00225                 nodeMainExecutor.shutdownNodeMain(robotNameResolver);
00226         }
00227 
00228         protected void releaseDashboardNode() {
00229                 nodeMainExecutor.shutdownNodeMain(dashboard);
00230         }
00231 
00232         @Override
00233         protected void onDestroy() {
00234         Log.d("RobotRemocon", "onDestroy()");
00235                 super.onDestroy();
00236         }
00237 }


android_remocons
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Wed Aug 26 2015 10:40:28