DiscoveryAdapter.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Yujin Robot.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package com.github.rosjava.android_remocons.robot_remocon.zeroconf;
00018 
00019 import java.util.ArrayList;
00020 
00021 import android.content.Context;
00022 import android.view.LayoutInflater;
00023 import android.view.View;
00024 import android.view.ViewGroup;
00025 import android.widget.ArrayAdapter;
00026 import android.widget.Checkable;
00027 import android.widget.CheckedTextView;
00028 import android.widget.ImageView;
00029 import android.widget.LinearLayout;
00030 import android.widget.TextView;
00031 
00032 import com.github.rosjava.zeroconf_jmdns_suite.jmdns.DiscoveredService;
00033 import com.github.rosjava.android_remocons.robot_remocon.R;
00034 
00035 
00036 public class DiscoveryAdapter extends ArrayAdapter<DiscoveredService> {
00037 
00042         public class CustomCheckBox extends LinearLayout implements Checkable {
00043         private CheckedTextView checkbox;
00044 
00045         public CustomCheckBox(Context context) {
00046                 super(context);
00047 
00048                 View
00049 view=((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.zeroconf_master_item,
00050 this,false);
00051                 checkbox=(CheckedTextView)view.findViewById(R.id.service_detail);
00052                 addView(view);
00053         }
00054 
00055 
00056         @Override
00057         public void setChecked(boolean checked) {
00058                 checkbox.setChecked(checked);
00059 
00060         }
00061 
00062         @Override
00063         public boolean isChecked() {
00064                 return checkbox.isChecked();
00065         }
00066 
00067         @Override
00068         public void toggle() {
00069                 setChecked(!isChecked());
00070 
00071         }
00072 
00073         }
00074 
00075                 
00076         private final Context context;
00077         private ArrayList<DiscoveredService> discovered_services;
00078 
00079     public DiscoveryAdapter(Context context, ArrayList<DiscoveredService> discovered_services) {
00080         super(context, R.layout.zeroconf_master_item, discovered_services); // pass the list to the super
00081         this.context = context;
00082         this.discovered_services = discovered_services;  // keep a pointer locally so we can play with it
00083     }
00084 
00085     @Override
00086     public View getView(int position, View convertView, ViewGroup parent) {
00087         View v = convertView;
00088         if (v == null) {
00089                 v = new CustomCheckBox(getContext());
00090         }
00091 
00092         DiscoveredService discovered_service = discovered_services.get(position);
00093         if (discovered_service != null) {
00094                 TextView tt = (TextView) v.findViewById(R.id.service_name);
00095                 TextView bt = (TextView) v.findViewById(R.id.service_detail);
00096                 if (tt != null) {
00097                     tt.setText(discovered_service.name);
00098                 }
00099                 if( bt != null ) {
00100                         String result = "";
00101                     for ( String ipv4_address : discovered_service.ipv4_addresses ) {
00102                                 if ( result.equals("") ) {
00103                                         result += ipv4_address + ":" + discovered_service.port;
00104                                 } else { 
00105                                         result += "\n" + ipv4_address + ":" + discovered_service.port;
00106                                 }
00107                     }
00108                     for ( String ipv6_address : discovered_service.ipv6_addresses ) {
00109                         if ( result.equals("") ) {
00110                             result += ipv6_address + ":" + discovered_service.port;
00111                         } else {
00112                             result += "\n" + ipv6_address + ":" + discovered_service.port;
00113                         }
00114                     }
00115                     bt.setText(result);
00116                 }
00117                 ImageView im = (ImageView) v.findViewById(R.id.icon);
00118                 if ( im != null ) {
00119                         if ( discovered_service.type.indexOf("_ros-master._tcp" ) != -1 ||
00120                                  discovered_service.type.indexOf("_ros-master._udp" ) != -1) {
00121                         im.setImageDrawable(context.getResources().getDrawable(R.drawable.turtle));
00122                         } else {
00123                         im.setImageDrawable(context.getResources().getDrawable(R.drawable.conductor));
00124                         }
00125                 }
00126         }
00127         return v;
00128     }
00129 }


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