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_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     
00119     
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       
00172       ColorMatrix matrix = new ColorMatrix();
00173       matrix.setSaturation(0); 
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 }