Dashboard.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * All rights reserved.
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above
00013  *    copyright notice, this list of conditions and the following
00014  *    disclaimer in the documentation and/or other materials provided
00015  *    with the distribution.
00016  *  * Neither the name of Willow Garage, Inc. nor the names of its
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  * POSSIBILITY OF SUCH DAMAGE.
00032  */
00033 
00034 package com.github.rosjava.android_remocons.common_tools.dashboards;
00035 
00036 import android.app.Activity;
00037 import android.content.Context;
00038 import android.util.Log;
00039 import android.view.View;
00040 import android.view.ViewGroup;
00041 
00042 import org.ros.namespace.GraphName;
00043 import org.ros.node.ConnectedNode;
00044 import org.ros.node.Node;
00045 import org.ros.node.NodeMain;
00046 
00047 public class Dashboard implements NodeMain {
00048         public interface DashboardInterface {
00053                 public void onStart(ConnectedNode connectedNode);
00054 
00055                 public void onShutdown(Node node);
00056         }
00057 
00058     private static final String defaultDashboardPath = "com.github.rosjava.android_remocons.common_tools.dashboards.DefaultDashboard";
00059         private static final String turtlebotDashboardPath = "com.github.turtlebot.turtlebot_android.turtlebot_core.dashboards.TurtlebotDashboard";
00060         private static final String pr2DashboardPath = "com.ros.pr2.apps.core_components.Pr2Dashboard";
00061 
00062         private DashboardInterface dashboard;
00063         private Activity activity;
00064         private ViewGroup view;
00065         private ViewGroup.LayoutParams lparams;
00066         private static String robotName;
00067         private static String customDashboardPath;
00068 
00069         public Dashboard(Activity activity) {
00070                 dashboard = null;
00071                 this.activity = activity;
00072                 this.view = null;
00073                 this.lparams = null;
00074         }
00075 
00076         public void setView(ViewGroup view, ViewGroup.LayoutParams lparams) {
00077                 if (view == null) {
00078                         Log.e("Dashboard", "Null view for dashboard");
00079                 }
00080                 this.view = view;
00081                 this.lparams = lparams;
00082         }
00083 
00084         public void setRobotName(String name) {
00085                 robotName = name;
00086         }
00087 
00088         public void setCustomDashboardPath(String path) {
00089                 this.customDashboardPath = path;
00090         }
00091 
00092         private static DashboardInterface createDashboard(Class dashClass,
00093                         Context context) {
00094                 ClassLoader classLoader = Dashboard.class.getClassLoader();
00095                 Object[] args = new Object[1];
00096                 DashboardInterface result = null;
00097                 args[0] = context;
00098                 try {
00099                         Class contextClass = Class.forName("android.content.Context");
00100                         result = (DashboardInterface) dashClass.getConstructor(contextClass).newInstance(args);
00101                 } catch (Exception ex) {
00102                         Log.e("Dashboard", "Error during dashboard instantiation:", ex);
00103                         result = null;
00104                 }
00105                 return result;
00106         }
00107 
00108         private static DashboardInterface createDashboard(String className,
00109                         Context context) {
00110                 Class dashClass = null;
00111                 try {
00112                         dashClass = Class.forName(className);
00113                 } catch (Exception ex) {
00114                         Log.e("Dashboard", "Error during dashboard class loading:", ex);
00115                         return null;
00116                 }
00117                 return createDashboard(dashClass, context);
00118 
00119         }
00120 
00124         // TODO: deal with the custom robot dashboard
00125         private static DashboardInterface createDashboard(Context context) {
00126                 if (customDashboardPath != null) {
00127                         return createDashboard(customDashboardPath, context);
00128 //              } else if (robotName.equals("turtlebot")) {
00129 //                      return createDashboard(turtlebotDashboardPath, context);
00130 //              } else if (robotName.equals("pr2")) {
00131 //                      return createDashboard(pr2DashboardPath, context);
00132                 } else {
00133                     return createDashboard(defaultDashboardPath, context);
00134         }
00135         }
00136 
00137         @Override
00138         public void onError(Node arg0, Throwable arg1) {
00139                 // TODO Auto-generated method stub
00140 
00141         }
00142 
00143         @Override
00144         public void onShutdown(final Node node) {
00145                 activity.runOnUiThread(new Runnable() {
00146                         @Override
00147                         public void run() {
00148                                 DashboardInterface dash = dashboard;
00149                                 if (dash != null) {
00150                                         dash.onShutdown(node);
00151                                         view.removeView((View) dash);
00152                                 }
00153                                 dashboard = null;
00154                         }
00155                 });
00156         }
00157 
00158         @Override
00159         public void onShutdownComplete(Node node) {
00160                 // TODO Auto-generated method stub
00161 
00162         }
00163 
00164         @Override
00165         public void onStart(ConnectedNode connectedNode) {
00166                 if (dashboard != null) {
00167                         // FIXME: should we re-start the dashboard? I think this is really
00168                         // an error.
00169                         return;
00170                 }
00171                 dashboard = Dashboard.createDashboard(activity);
00172                 if (dashboard != null) {
00173                         activity.runOnUiThread(new Runnable() {
00174                                 @Override
00175                                 public void run() {
00176 
00177                                         DashboardInterface dash = dashboard;
00178                                         ViewGroup localView = view;
00179                                         if (dash != null && localView != null) {
00180                                                 localView.addView((View) dash, lparams);
00181                                         } else if (dash == null) {
00182                                                 Log.e("Dashboard",
00183                                                                 "Dashboard could not start: no dashboard");
00184                                         } else if (view == null) {
00185                                                 Log.e("Dashboard", "Dashboard could not start: no view");
00186                                         } else {
00187                                                 Log.e("Dashboard",
00188                                                                 "Dashboard could not start: no view or dashboard");
00189                                         }
00190                                 }
00191                         });
00192                         dashboard.onStart(connectedNode);
00193                 }
00194         }
00195 
00196         @Override
00197         public GraphName getDefaultNodeName() {
00198                 // TODO Auto-generated method stub
00199                 return null;
00200         }
00201 }


android_remocons
Author(s): Daniel Stonier, Kazuto Murase
autogenerated on Sat Jun 8 2019 19:32:24