Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 package com.github.rosjava.android_apps.application_management;
00035
00036 import org.ros.namespace.GraphName;
00037 import org.ros.node.ConnectedNode;
00038 import org.ros.node.Node;
00039 import org.ros.node.NodeMain;
00040
00041 import android.app.Activity;
00042 import android.content.Context;
00043 import android.util.Log;
00044 import android.view.View;
00045 import android.view.ViewGroup;
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_apps.application_management.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
00125 private static DashboardInterface createDashboard(Context context) {
00126 if (customDashboardPath != null) {
00127 return createDashboard(customDashboardPath, context);
00128
00129
00130
00131
00132 } else {
00133 return createDashboard(defaultDashboardPath, context);
00134 }
00135 }
00136
00137 @Override
00138 public void onError(Node arg0, Throwable arg1) {
00139
00140
00141 }
00142
00143 @Override
00144 public void onShutdown(final Node node) {
00145 activity.runOnUiThread(new Runnable() {
00146 @Override
00147 public void run() {
00148 Dashboard.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
00161
00162 }
00163
00164 @Override
00165 public void onStart(ConnectedNode connectedNode) {
00166 if (dashboard != null) {
00167
00168
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 Dashboard.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
00199 return null;
00200 }
00201 }