MasterItem.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.robot_remocon;
00035 
00036 import android.content.Context;
00037 import android.graphics.Bitmap;
00038 import android.graphics.BitmapFactory;
00039 import android.graphics.ColorMatrix;
00040 import android.graphics.ColorMatrixColorFilter;
00041 import android.util.Log;
00042 import android.view.LayoutInflater;
00043 import android.view.View;
00044 import android.view.ViewGroup;
00045 import android.widget.ImageView;
00046 import android.widget.ProgressBar;
00047 import android.widget.TextView;
00048 import com.github.rosjava.android_apps.application_management.WifiChecker;
00049 import com.github.rosjava.android_apps.application_management.ControlChecker;
00050 import com.github.rosjava.android_apps.application_management.MasterChecker;
00051 import com.github.rosjava.android_apps.application_management.RobotDescription;
00052 import android.net.wifi.WifiManager;
00053 
00054 import org.jboss.netty.buffer.ChannelBuffer;
00055 
00063 public class MasterItem implements MasterChecker.RobotDescriptionReceiver,
00064                                    MasterChecker.FailureHandler,
00065                                    ControlChecker.SuccessHandler,
00066                                    ControlChecker.FailureHandler {
00067   private ControlChecker controlChecker;
00068   private MasterChecker checker;
00069   private View view;
00070   private RobotDescription description;
00071   private RobotMasterChooser parentMca;
00072   private String errorReason;
00073   private boolean control;
00074   public MasterItem(RobotDescription robotDescription, RobotMasterChooser parentMca) {
00075     errorReason = "";
00076     this.parentMca = parentMca;
00077     this.description = robotDescription;
00078     this.description.setConnectionStatus(RobotDescription.CONNECTING);
00079     if (WifiChecker.wifiValid(this.description.getRobotId(), 
00080                         (WifiManager)parentMca.getSystemService(parentMca.WIFI_SERVICE))) {
00081       checker = new MasterChecker(this, this);
00082       if (this.description.getRobotId().getControlUri() != null) {
00083         control = true;
00084         controlChecker = new ControlChecker(this, this);
00085         controlChecker.beginChecking(this.description.getRobotId());
00086       } else {
00087         control = false;
00088         checker.beginChecking(this.description.getRobotId());
00089       }
00090     } else {
00091       errorReason = "Wrong WiFi Network";
00092       description.setConnectionStatus(RobotDescription.WIFI);
00093       safePopulateView();
00094     }
00095   }
00096   public boolean isOk() {
00097     return this.description.getConnectionStatus().equals(RobotDescription.OK);
00098   }
00099   @Override
00100   public void handleSuccess() {
00101     control = false;
00102     checker.beginChecking(this.description.getRobotId());
00103   }
00104   @Override
00105   public void receive(RobotDescription robotDescription) {
00106     description.copyFrom(robotDescription);
00107     safePopulateView();
00108   }
00109   @Override
00110   public void handleFailure(String reason) {
00111     errorReason = reason;
00112     description.setConnectionStatus(control ? RobotDescription.CONTROL : RobotDescription.ERROR);
00113     safePopulateView();
00114   }
00115   public View getView(Context context, View convert_view, ViewGroup parent) {
00116     LayoutInflater inflater = (LayoutInflater) context
00117         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
00118     // Using convert_view here seems to cause the wrong view to show
00119     // up sometimes, so I'm always making new ones.
00120     view = inflater.inflate(R.layout.master_item, null);
00121     populateView();
00122     return view;
00123   }
00124   private void safePopulateView() {
00125     if (view != null) {
00126       final RobotMasterChooser mca = parentMca;
00127       view.post(new Runnable() {
00128         @Override
00129         public void run() {
00130           populateView();
00131           mca.writeRobotList();
00132         }
00133       });
00134     }
00135   }
00136   
00137   private void populateView() {
00138     Log.i("MasterItem", "connection status = " + description.getConnectionStatus());
00139     boolean isOk = description.getConnectionStatus().equals(RobotDescription.OK);
00140     boolean isUnavailable = description.getConnectionStatus().equals(RobotDescription.UNAVAILABLE);
00141     boolean isControl = description.getConnectionStatus().equals(RobotDescription.CONTROL);
00142     boolean isWifi = description.getConnectionStatus().equals(RobotDescription.WIFI);
00143     boolean isError = description.getConnectionStatus().equals(RobotDescription.ERROR);
00144     boolean isConnecting = description.getConnectionStatus().equals(RobotDescription.CONNECTING);
00145     ProgressBar progress = (ProgressBar) view.findViewById(R.id.progress_circle);
00146     progress.setIndeterminate(true);
00147     progress.setVisibility(isConnecting ? View.VISIBLE : View.GONE );
00148     ImageView errorImage = (ImageView) view.findViewById(R.id.error_icon);
00149     errorImage.setVisibility( isError ? View.VISIBLE : View.GONE );
00150     ImageView iv = (ImageView) view.findViewById(R.id.robot_icon);
00151     iv.setVisibility((isOk || isWifi || isControl || isUnavailable) ? View.VISIBLE : View.GONE);
00152     if (description.getRobotType() == null) {
00153       iv.setImageResource(R.drawable.question_mark);
00154     } else if (isWifi) {
00155       iv.setImageResource(R.drawable.wifi_question_mark);
00156     } else if ( description.getRobotIconData() == null ) {
00157         iv.setImageResource(R.drawable.question_mark);
00158     } else if( description.getRobotIconData().array().length > 0 && description.getRobotIconFormat() != null &&
00159             (description.getRobotIconFormat().equals("jpeg") || description.getRobotIconFormat().equals("png")) ) {
00160       ChannelBuffer buffer = description.getRobotIconData();
00161       Bitmap iconBitmap = BitmapFactory.decodeByteArray(buffer.array(), buffer.arrayOffset(), buffer.readableBytes());
00162       if( iconBitmap != null ) {
00163         iv.setImageBitmap(iconBitmap);
00164       } else {
00165           iv.setImageResource(R.drawable.question_mark);
00166       }
00167     } else {
00168       iv.setImageResource(R.drawable.question_mark);
00169     }
00170     if ( isUnavailable ) {
00171       // Be nice to do alpha here, but that is api 16 and we are targeting 10.
00172       ColorMatrix matrix = new ColorMatrix();
00173       matrix.setSaturation(0); //0 means grayscale
00174       ColorMatrixColorFilter cf = new ColorMatrixColorFilter(matrix);
00175       iv.setColorFilter(cf);
00176     }
00177     TextView tv;
00178     tv = (TextView) view.findViewById(R.id.uri);
00179     tv.setText(description.getRobotId().toString());
00180     tv = (TextView) view.findViewById(R.id.name);
00181     tv.setText(description.getRobotFriendlyName());
00182     tv = (TextView) view.findViewById(R.id.status);
00183     tv.setText(errorReason);
00184   }
00185 }


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