Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package com.github.rosjava.android_remocons.common_tools.zeroconf;
00018
00019 import android.content.Context;
00020 import android.view.LayoutInflater;
00021 import android.view.View;
00022 import android.view.ViewGroup;
00023 import android.widget.ArrayAdapter;
00024 import android.widget.Checkable;
00025 import android.widget.CheckedTextView;
00026 import android.widget.ImageView;
00027 import android.widget.LinearLayout;
00028 import android.widget.TextView;
00029
00030 import com.github.rosjava.android_remocons.common_tools.R;
00031 import com.github.rosjava.zeroconf_jmdns_suite.jmdns.DiscoveredService;
00032
00033 import java.util.ArrayList;
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 view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
00049 .inflate(R.layout.zeroconf_master_item, this, false);
00050 checkbox = (CheckedTextView) view.findViewById(R.id.service_detail);
00051 addView(view);
00052 }
00053
00054
00055 @Override
00056 public void setChecked(boolean checked) {
00057 checkbox.setChecked(checked);
00058
00059 }
00060
00061 @Override
00062 public boolean isChecked() {
00063 return checkbox.isChecked();
00064 }
00065
00066 @Override
00067 public void toggle() {
00068 setChecked(!isChecked());
00069
00070 }
00071 }
00072
00073
00074 private final Context context;
00075 private ArrayList<DiscoveredService> discoveredServices;
00076 private String targetServiceName;
00077 private int targetServiceDrawable;
00078 private int otherServicesDrawable;
00079
00080 public DiscoveryAdapter(Context context, ArrayList<DiscoveredService> discoveredServices,
00081 String targetServiceName, int targetServiceDrawable, int otherServicesDrawable) {
00082 super(context, R.layout.zeroconf_master_item, discoveredServices);
00083 this.context = context;
00084 this.discoveredServices = discoveredServices;
00085 this.targetServiceName = targetServiceName;
00086 this.targetServiceDrawable = targetServiceDrawable;
00087 this.otherServicesDrawable = otherServicesDrawable;
00088 }
00089
00090 @Override
00091 public View getView(int position, View convertView, ViewGroup parent) {
00092 View v = convertView;
00093 if (v == null) {
00094 v = new CustomCheckBox(getContext());
00095 }
00096
00097 DiscoveredService discovered_service = discoveredServices.get(position);
00098 if (discovered_service != null) {
00099 TextView tt = (TextView) v.findViewById(R.id.service_name);
00100 TextView bt = (TextView) v.findViewById(R.id.service_detail);
00101 if (tt != null) {
00102 tt.setText(discovered_service.name);
00103 }
00104 if (bt != null) {
00105 String result = "";
00106 for (String ipv4_address : discovered_service.ipv4_addresses) {
00107 if (result.equals("")) {
00108 result += ipv4_address + ":" + discovered_service.port;
00109 } else {
00110 result += "\n" + ipv4_address + ":" + discovered_service.port;
00111 }
00112 }
00113 for (String ipv6_address : discovered_service.ipv6_addresses) {
00114 if (result.equals("")) {
00115 result += ipv6_address + ":" + discovered_service.port;
00116 } else {
00117 result += "\n" + ipv6_address + ":" + discovered_service.port;
00118 }
00119 }
00120 bt.setText(result);
00121 }
00122 ImageView im = (ImageView) v.findViewById(R.id.icon);
00123 if (im != null) {
00124 if (discovered_service.type.indexOf("_" + targetServiceName + "._tcp") != -1 ||
00125 discovered_service.type.indexOf("_" + targetServiceName + "._udp") != -1) {
00126 im.setImageDrawable(context.getResources().getDrawable(targetServiceDrawable));
00127 } else {
00128 im.setImageDrawable(context.getResources().getDrawable(otherServicesDrawable));
00129 }
00130 }
00131 }
00132 return v;
00133 }
00134 }