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.rocon_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.net.wifi.WifiManager;
00042 import android.util.Log;
00043 import android.view.LayoutInflater;
00044 import android.view.View;
00045 import android.view.ViewGroup;
00046 import android.widget.ImageView;
00047 import android.widget.ProgressBar;
00048 import android.widget.TextView;
00049
00050 import com.github.rosjava.android_remocons.common_tools.master.ConcertChecker;
00051 import com.github.rosjava.android_remocons.common_tools.master.RoconDescription;
00052 import com.github.rosjava.android_remocons.common_tools.system.WifiChecker;
00053
00054 import org.jboss.netty.buffer.ChannelBuffer;
00055
00063 public class MasterItem implements ConcertChecker.ConcertDescriptionReceiver,
00064 ConcertChecker.FailureHandler {
00065 private ConcertChecker checker;
00066 private View view;
00067 private RoconDescription description;
00068 private MasterChooser parentMca;
00069 private String errorReason;
00070
00071 public MasterItem(RoconDescription roconDescription, MasterChooser parentMca) {
00072 errorReason = "";
00073 this.parentMca = parentMca;
00074 this.description = roconDescription;
00075 this.description.setConnectionStatus(RoconDescription.CONNECTING);
00076 if (WifiChecker.wifiValid(this.description.getMasterId(),
00077 (WifiManager) parentMca.getSystemService(parentMca.WIFI_SERVICE))) {
00078 checker = new ConcertChecker(this, this);
00079 checker.beginChecking(this.description.getMasterId());
00080 } else {
00081 errorReason = "Wrong WiFi Network";
00082 description.setConnectionStatus(RoconDescription.WIFI);
00083 safePopulateView();
00084 }
00085 }
00086
00087 public boolean isOk() {
00088 return this.description.getConnectionStatus().equals(RoconDescription.OK);
00089 }
00090
00091 public void handleSuccess() {
00092 checker.beginChecking(this.description.getMasterId());
00093 }
00094
00095 @Override
00096 public void receive(RoconDescription roconDescription) {
00097 description.copyFrom(roconDescription);
00098 safePopulateView();
00099 }
00100
00101 @Override
00102 public void handleFailure(String reason) {
00103 errorReason = reason;
00104 description.setConnectionStatus(RoconDescription.ERROR);
00105 safePopulateView();
00106 }
00107
00108 public View getView(Context context, View convert_view, ViewGroup parent) {
00109 LayoutInflater inflater = (LayoutInflater) context
00110 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
00111
00112
00113 view = inflater.inflate(R.layout.master_item, null);
00114 populateView();
00115 return view;
00116 }
00117
00118 private void safePopulateView() {
00119 if (view != null) {
00120 final MasterChooser mca = parentMca;
00121 view.post(new Runnable() {
00122 @Override
00123 public void run() {
00124 populateView();
00125 mca.writeMasterList();
00126 }
00127 });
00128 }
00129 }
00130
00131 private void populateView() {
00132 Log.i("MasterItem", "connection status = " + description.getConnectionStatus());
00133 boolean isOk = description.getConnectionStatus().equals(RoconDescription.OK);
00134 boolean isUnavailable = description.getConnectionStatus().equals(RoconDescription.UNAVAILABLE);
00135 boolean isWifi = description.getConnectionStatus().equals(RoconDescription.WIFI);
00136 boolean isError = description.getConnectionStatus().equals(RoconDescription.ERROR);
00137 boolean isConnecting = description.getConnectionStatus().equals(RoconDescription.CONNECTING);
00138 ProgressBar progress = (ProgressBar) view.findViewById(R.id.progress_circle);
00139 progress.setIndeterminate(true);
00140 progress.setVisibility(isConnecting ? View.VISIBLE : View.GONE);
00141 ImageView errorImage = (ImageView) view.findViewById(R.id.error_icon);
00142 errorImage.setVisibility(isError ? View.VISIBLE : View.GONE);
00143 ImageView iv = (ImageView) view.findViewById(R.id.concert_icon);
00144 iv.setVisibility((isOk || isWifi || isUnavailable) ? View.VISIBLE : View.GONE);
00145 if (isWifi) {
00146 iv.setImageResource(R.drawable.wifi_question_mark);
00147 } else if (description.getMasterIconData() == null) {
00148 iv.setImageResource(R.drawable.question_mark);
00149 } else if (description.getMasterIconData().array().length > 0 && description.getMasterIconFormat() != null &&
00150 (description.getMasterIconFormat().equals("jpeg") || description.getMasterIconFormat().equals("png"))) {
00151 ChannelBuffer buffer = description.getMasterIconData();
00152 Bitmap iconBitmap = BitmapFactory.decodeByteArray(buffer.array(), buffer.arrayOffset(), buffer.readableBytes());
00153 if (iconBitmap != null) {
00154 iv.setImageBitmap(iconBitmap);
00155 } else {
00156 iv.setImageResource(R.drawable.question_mark);
00157 }
00158 } else {
00159 iv.setImageResource(R.drawable.question_mark);
00160 }
00161 if (isUnavailable) {
00162
00163 ColorMatrix matrix = new ColorMatrix();
00164 matrix.setSaturation(0);
00165 ColorMatrixColorFilter cf = new ColorMatrixColorFilter(matrix);
00166 iv.setColorFilter(cf);
00167 }
00168 TextView tv;
00169 tv = (TextView) view.findViewById(R.id.uri);
00170 tv.setText(description.getMasterId().toString());
00171 tv = (TextView) view.findViewById(R.id.name);
00172 tv.setText(description.getMasterFriendlyName());
00173 tv = (TextView) view.findViewById(R.id.status);
00174 tv.setText(errorReason);
00175 }
00176 }