DiagnosticsArrayView.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Chad Rockey
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Android Robot Monitor nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 package org.ros.android.view;
00031 
00032 import android.content.Context;
00033 import android.content.res.Resources;
00034 import android.graphics.drawable.Drawable;
00035 import android.util.AttributeSet;
00036 import android.widget.Button;
00037 import android.widget.TableLayout;
00038 import diagnostic_msgs.DiagnosticArray;
00039 import diagnostic_msgs.DiagnosticStatus;
00040 import org.ros.android.android_10.R;
00041 import org.ros.message.MessageListener;
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 import org.ros.node.topic.Subscriber;
00047 
00048 import java.util.List;
00049 
00054 public class DiagnosticsArrayView extends TableLayout implements NodeMain {
00055 
00060   private static final int STALE = 3;
00061   private static final String DIAGNOSTICS_AGGREGATOR_TOPIC = "/diagnostics_agg";
00062 
00063   private Drawable errorDrawable;
00064   private Drawable warningDrawable;
00065   private Drawable okDrawable;
00066   private Drawable staleDrawable;
00067 
00068   public DiagnosticsArrayView(Context context) {
00069     super(context);
00070     init();
00071   }
00072 
00073   public DiagnosticsArrayView(Context context, AttributeSet attrs) {
00074     super(context, attrs);
00075     init();
00076   }
00077 
00078   private void init() {
00079     Resources resources = getResources();
00080     errorDrawable = resources.getDrawable(R.drawable.error);
00081     warningDrawable = resources.getDrawable(R.drawable.warn);
00082     okDrawable = resources.getDrawable(R.drawable.ok);
00083     staleDrawable = resources.getDrawable(R.drawable.stale);
00084   }
00085 
00086   @Override
00087   public GraphName getDefaultNodeName() {
00088     return GraphName.of("android_10/diagnostics_array_view");
00089   }
00090 
00091   @Override
00092   public void onStart(ConnectedNode connectedNode) {
00093     Subscriber<DiagnosticArray> subscriber =
00094         connectedNode.newSubscriber(DIAGNOSTICS_AGGREGATOR_TOPIC,
00095             diagnostic_msgs.DiagnosticArray._TYPE);
00096     subscriber.addMessageListener(new MessageListener<DiagnosticArray>() {
00097       @Override
00098       public void onNewMessage(final DiagnosticArray message) {
00099         final List<DiagnosticStatus> diagnosticStatusMessages = message.getStatus();
00100         post(new Runnable() {
00101           @Override
00102           public void run() {
00103             removeAllViews();
00104             for (final DiagnosticStatus diagnosticStatusMessage : diagnosticStatusMessages) {
00105               Button button = new Button(getContext());
00106               button.setText(diagnosticStatusMessage.getName());
00107               if (diagnosticStatusMessage.getLevel() == STALE) {
00108                 button.setCompoundDrawablesWithIntrinsicBounds(staleDrawable, null, null, null);
00109               } else if (diagnosticStatusMessage.getLevel() == DiagnosticStatus.ERROR) {
00110                 button.setCompoundDrawablesWithIntrinsicBounds(errorDrawable, null, null, null);
00111               } else if (diagnosticStatusMessage.getLevel() == DiagnosticStatus.WARN) {
00112                 button.setCompoundDrawablesWithIntrinsicBounds(warningDrawable, null, null, null);
00113               } else {
00114                 button.setCompoundDrawablesWithIntrinsicBounds(okDrawable, null, null, null);
00115               }
00116               addView(button);
00117             }
00118           }
00119         });
00120         postInvalidate();
00121       }
00122     });
00123   }
00124 
00125   @Override
00126   public void onError(Node node, Throwable throwable) {
00127   }
00128 
00129   @Override
00130   public void onShutdown(Node node) {
00131   }
00132 
00133   @Override
00134   public void onShutdownComplete(Node node) {
00135   }
00136 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07