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.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         // Using convert_view here seems to cause the wrong view to show
00112         // up sometimes, so I'm always making new ones.
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             // Be nice to do alpha here, but that is api 16 and we are targeting 10.
00163             ColorMatrix matrix = new ColorMatrix();
00164             matrix.setSaturation(0); //0 means grayscale
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 }


android_remocons
Author(s): Daniel Stonier, Kazuto Murase
autogenerated on Sat Jun 8 2019 19:32:24