DiagnosticsArrayDisplay.java
Go to the documentation of this file.
00001 package org.ros.android.robot_monitor;
00002 
00003 import java.util.List;
00004 
00005 import android.content.Context;
00006 import android.graphics.drawable.Drawable;
00007 import android.widget.Button;
00008 import android.widget.TableLayout;
00009 import android.widget.TextView;
00010 
00011 import diagnostic_msgs.DiagnosticArray;
00012 import diagnostic_msgs.DiagnosticStatus;
00013 
00014 public class DiagnosticsArrayDisplay {
00015         
00016         Context context;
00017         
00018         TableLayout tl;
00019         TextView tv;
00020         
00021         Drawable error;
00022         Drawable warn;
00023         Drawable ok;
00024         Drawable stale;
00025         int error_color;
00026         int warn_color;
00027         int ok_color;
00028         int stale_color;
00029         
00030         byte level;
00031         
00032         public DiagnosticsArrayDisplay(Context context){
00033                 this.context = context;
00034         }
00035         
00036         void setTableLayout(TableLayout tl){
00037                 this.tl = tl;
00038         }
00039         
00040         void setTextView(TextView tv){
00041                 this.tv = tv;
00042         }
00043         
00044         void setDrawables(Drawable error, Drawable warn, Drawable ok, Drawable stale){
00045                 this.error = error;
00046                 this.warn = warn;
00047                 this.ok = ok;
00048                 this.stale = stale;
00049         }
00050         
00051         void setColors(int error_color, int warn_color, int ok_color, int stale_color){
00052                 this.error_color = error_color;
00053                 this.warn_color = warn_color;
00054                 this.ok_color = ok_color;
00055                 this.stale_color = stale_color;
00056         }
00057         
00058         void displayArray(DiagnosticArray msg){
00059                 final List<DiagnosticStatus> dsa = msg.getStatus();
00060                 this.level = DiagnosticStatus.OK;
00061                 tl.post(new Runnable(){
00062                         @Override
00063                         public void run(){
00064                                 tl.removeAllViews();
00065                                 // Actually display each Status
00066                                 
00067                                 for(final DiagnosticStatus ds : dsa){
00068                                         Button b = new Button(context);
00069                                         b.setText(ds.getName());
00070                                         level = updateLevel(level, ds.getLevel());
00071                                         if(ds.getLevel() == 3){ // STALE is not part of the message definitions
00072                                                 b.setTextColor(stale_color);
00073                                                 b.setCompoundDrawablesWithIntrinsicBounds(stale, null, null, null);
00074                                         } else if(ds.getLevel() == DiagnosticStatus.ERROR){
00075                                                 b.setTextColor(error_color);
00076                                                 b.setCompoundDrawablesWithIntrinsicBounds(error, null, null, null);
00077                                         } else if(ds.getLevel() == DiagnosticStatus.WARN){
00078                                                 b.setTextColor(warn_color);
00079                                                 b.setCompoundDrawablesWithIntrinsicBounds(warn, null, null, null);
00080                                         } else { // Is OK!
00081                                                 b.setTextColor(ok_color);
00082                                                 b.setCompoundDrawablesWithIntrinsicBounds(ok, null, null, null);
00083                                         }
00084                                         /*b.setOnClickListener(new View.OnClickListener() {
00085                                      public void onClick(View v) {
00086                                          /*if(!buttonPressed){
00087                                                  buttonPressed = true;
00088                                                  // Do not kill node on activity change
00089                                                  saveNode = true;
00090                                                 // Unregister listener
00091                                                         MonitorApplication ma = (MonitorApplication)getApplicationContext();
00092                                                         ma.getSubscriber().removeMessageListener(ml);
00093                                                  Intent myIntent = new Intent(dad, DiagnosticsStatusDisplay.class);
00094                                                  myIntent.putExtra("level", ds.level);
00095                                                  myIntent.putExtra("name", ds.name);
00096                                                  myIntent.putExtra("message", ds.message);
00097                                                  myIntent.putExtra("hardware_id", ds.hardware_id);
00098                                                  String[] keys = new String[ds.values.size()];
00099                                                  String[] values = new String[ds.values.size()];
00100                                                  for(int i = 0; i < ds.values.size(); i++){
00101                                                          keys[i] = ds.values.get(i).key;
00102                                                          values[i] = ds.values.get(i).value;
00103                                                  }
00104                                                  myIntent.putExtra("keys", keys);
00105                                                  myIntent.putExtra("values", values);
00106                                                  dad.startActivity(myIntent);
00107                                          }
00108                                      }
00109                                  });*/
00110                                         tl.addView(b);
00111                                 }
00112                         }
00113                 });
00114                 tl.postInvalidate();
00115                 
00116                 tv.post(new Runnable(){
00117                         @Override
00118                         public void run(){
00119                                 // TODO This is where I would store the message into buttons that scroll to look back in time?
00120                                 if(level == 3){ // STALE is not part of the message definitions
00121                                         tv.setBackgroundColor(stale_color);
00122                                 } else if(level == DiagnosticStatus.ERROR){
00123                                         tv.setBackgroundColor(error_color);
00124                                 } else if(level == DiagnosticStatus.WARN){
00125                                         tv.setBackgroundColor(warn_color);
00126                                 } else { // Is OK!
00127                                         tv.setBackgroundColor(ok_color);
00128                                 }
00129                         }
00130                 });
00131                 tv.postInvalidate();
00132 
00133         }
00134         
00135         byte updateLevel(byte oldLevel, byte newLevel){
00136                 // OK < WARN < ERROR
00137                 if(newLevel >= oldLevel){
00138                         //this.level = newLevel;
00139                         return newLevel;
00140                 } else {
00141                         //this.level = oldLevel;
00142                         return oldLevel;
00143                 }
00144         }
00145 }


android_robot_monitor
Author(s): Chad Rockey
autogenerated on Sun Oct 5 2014 22:09:23