Home | History | Annotate | Download | only in discovery
      1 
      2 package com.example.android.wifidirect.discovery;
      3 
      4 import android.app.ListFragment;
      5 import android.content.Context;
      6 import android.net.wifi.p2p.WifiP2pDevice;
      7 import android.os.Bundle;
      8 import android.view.LayoutInflater;
      9 import android.view.View;
     10 import android.view.ViewGroup;
     11 import android.widget.ArrayAdapter;
     12 import android.widget.ListView;
     13 import android.widget.TextView;
     14 
     15 import java.util.ArrayList;
     16 import java.util.List;
     17 
     18 /**
     19  * A simple ListFragment that shows the available services as published by the
     20  * peers
     21  */
     22 public class WiFiDirectServicesList extends ListFragment {
     23 
     24     WiFiDevicesAdapter listAdapter = null;
     25 
     26     interface DeviceClickListener {
     27         public void connectP2p(WiFiP2pService wifiP2pService);
     28     }
     29 
     30     @Override
     31     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     32             Bundle savedInstanceState) {
     33         return inflater.inflate(R.layout.devices_list, container, false);
     34     }
     35 
     36     @Override
     37     public void onActivityCreated(Bundle savedInstanceState) {
     38         super.onActivityCreated(savedInstanceState);
     39         listAdapter = new WiFiDevicesAdapter(this.getActivity(),
     40                 android.R.layout.simple_list_item_2, android.R.id.text1,
     41                 new ArrayList<WiFiP2pService>());
     42         setListAdapter(listAdapter);
     43     }
     44 
     45     @Override
     46     public void onListItemClick(ListView l, View v, int position, long id) {
     47         // TODO Auto-generated method stub
     48         ((DeviceClickListener) getActivity()).connectP2p((WiFiP2pService) l
     49                 .getItemAtPosition(position));
     50         ((TextView) v.findViewById(android.R.id.text2)).setText("Connecting");
     51 
     52     }
     53 
     54     public class WiFiDevicesAdapter extends ArrayAdapter<WiFiP2pService> {
     55 
     56         private List<WiFiP2pService> items;
     57 
     58         public WiFiDevicesAdapter(Context context, int resource,
     59                 int textViewResourceId, List<WiFiP2pService> items) {
     60             super(context, resource, textViewResourceId, items);
     61             this.items = items;
     62         }
     63 
     64         @Override
     65         public View getView(int position, View convertView, ViewGroup parent) {
     66             View v = convertView;
     67             if (v == null) {
     68                 LayoutInflater vi = (LayoutInflater) getActivity()
     69                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     70                 v = vi.inflate(android.R.layout.simple_list_item_2, null);
     71             }
     72             WiFiP2pService service = items.get(position);
     73             if (service != null) {
     74                 TextView nameText = (TextView) v
     75                         .findViewById(android.R.id.text1);
     76 
     77                 if (nameText != null) {
     78                     nameText.setText(service.device.deviceName + " - " + service.instanceName);
     79                 }
     80                 TextView statusText = (TextView) v
     81                         .findViewById(android.R.id.text2);
     82                 statusText.setText(getDeviceStatus(service.device.status));
     83             }
     84             return v;
     85         }
     86 
     87     }
     88 
     89     public static String getDeviceStatus(int statusCode) {
     90         switch (statusCode) {
     91             case WifiP2pDevice.CONNECTED:
     92                 return "Connected";
     93             case WifiP2pDevice.INVITED:
     94                 return "Invited";
     95             case WifiP2pDevice.FAILED:
     96                 return "Failed";
     97             case WifiP2pDevice.AVAILABLE:
     98                 return "Available";
     99             case WifiP2pDevice.UNAVAILABLE:
    100                 return "Unavailable";
    101             default:
    102                 return "Unknown";
    103 
    104         }
    105     }
    106 
    107 }
    108