Home | History | Annotate | Download | only in wifidirect
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.wifidirect;
     18 
     19 import android.app.ListFragment;
     20 import android.app.ProgressDialog;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.net.wifi.p2p.WifiP2pConfig;
     24 import android.net.wifi.p2p.WifiP2pDevice;
     25 import android.net.wifi.p2p.WifiP2pDeviceList;
     26 import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
     27 import android.os.Bundle;
     28 import android.util.Log;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.view.ViewGroup;
     32 import android.widget.ArrayAdapter;
     33 import android.widget.ListView;
     34 import android.widget.TextView;
     35 
     36 import java.util.ArrayList;
     37 import java.util.List;
     38 
     39 /**
     40  * A ListFragment that displays available peers on discovery and requests the
     41  * parent activity to handle user interaction events
     42  */
     43 public class DeviceListFragment extends ListFragment implements PeerListListener {
     44 
     45     private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
     46     ProgressDialog progressDialog = null;
     47     View mContentView = null;
     48     private WifiP2pDevice device;
     49 
     50     @Override
     51     public void onActivityCreated(Bundle savedInstanceState) {
     52         super.onActivityCreated(savedInstanceState);
     53         this.setListAdapter(new WiFiPeerListAdapter(getActivity(), R.layout.row_devices, peers));
     54 
     55     }
     56 
     57     @Override
     58     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     59         mContentView = inflater.inflate(R.layout.device_list, null);
     60         return mContentView;
     61     }
     62 
     63     /**
     64      * @return this device
     65      */
     66     public WifiP2pDevice getDevice() {
     67         return device;
     68     }
     69 
     70     private static String getDeviceStatus(int deviceStatus) {
     71         Log.d(WiFiDirectActivity.TAG, "Peer status :" + deviceStatus);
     72         switch (deviceStatus) {
     73             case WifiP2pDevice.AVAILABLE:
     74                 return "Available";
     75             case WifiP2pDevice.INVITED:
     76                 return "Invited";
     77             case WifiP2pDevice.CONNECTED:
     78                 return "Connected";
     79             case WifiP2pDevice.FAILED:
     80                 return "Failed";
     81             case WifiP2pDevice.UNAVAILABLE:
     82                 return "Unavailable";
     83             default:
     84                 return "Unknown";
     85 
     86         }
     87     }
     88 
     89     /**
     90      * Initiate a connection with the peer.
     91      */
     92     @Override
     93     public void onListItemClick(ListView l, View v, int position, long id) {
     94         WifiP2pDevice device = (WifiP2pDevice) getListAdapter().getItem(position);
     95         ((DeviceActionListener) getActivity()).showDetails(device);
     96     }
     97 
     98     /**
     99      * Array adapter for ListFragment that maintains WifiP2pDevice list.
    100      */
    101     private class WiFiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
    102 
    103         private List<WifiP2pDevice> items;
    104 
    105         /**
    106          * @param context
    107          * @param textViewResourceId
    108          * @param objects
    109          */
    110         public WiFiPeerListAdapter(Context context, int textViewResourceId,
    111                 List<WifiP2pDevice> objects) {
    112             super(context, textViewResourceId, objects);
    113             items = objects;
    114 
    115         }
    116 
    117         @Override
    118         public View getView(int position, View convertView, ViewGroup parent) {
    119             View v = convertView;
    120             if (v == null) {
    121                 LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
    122                         Context.LAYOUT_INFLATER_SERVICE);
    123                 v = vi.inflate(R.layout.row_devices, null);
    124             }
    125             WifiP2pDevice device = items.get(position);
    126             if (device != null) {
    127                 TextView top = (TextView) v.findViewById(R.id.device_name);
    128                 TextView bottom = (TextView) v.findViewById(R.id.device_details);
    129                 if (top != null) {
    130                     top.setText(device.deviceName);
    131                 }
    132                 if (bottom != null) {
    133                     bottom.setText(getDeviceStatus(device.status));
    134                 }
    135             }
    136 
    137             return v;
    138 
    139         }
    140     }
    141 
    142     /**
    143      * Update UI for this device.
    144      *
    145      * @param device WifiP2pDevice object
    146      */
    147     public void updateThisDevice(WifiP2pDevice device) {
    148         this.device = device;
    149         TextView view = (TextView) mContentView.findViewById(R.id.my_name);
    150         view.setText(device.deviceName);
    151         view = (TextView) mContentView.findViewById(R.id.my_status);
    152         view.setText(getDeviceStatus(device.status));
    153     }
    154 
    155     @Override
    156     public void onPeersAvailable(WifiP2pDeviceList peerList) {
    157         if (progressDialog != null && progressDialog.isShowing()) {
    158             progressDialog.dismiss();
    159         }
    160         peers.clear();
    161         peers.addAll(peerList.getDeviceList());
    162         ((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
    163         if (peers.size() == 0) {
    164             Log.d(WiFiDirectActivity.TAG, "No devices found");
    165             return;
    166         }
    167 
    168     }
    169 
    170     public void clearPeers() {
    171         peers.clear();
    172         ((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
    173     }
    174 
    175     /**
    176      *
    177      */
    178     public void onInitiateDiscovery() {
    179         if (progressDialog != null && progressDialog.isShowing()) {
    180             progressDialog.dismiss();
    181         }
    182         progressDialog = ProgressDialog.show(getActivity(), "Press back to cancel", "finding peers", true,
    183                 true, new DialogInterface.OnCancelListener() {
    184 
    185                     @Override
    186                     public void onCancel(DialogInterface dialog) {
    187 
    188                     }
    189                 });
    190     }
    191 
    192     /**
    193      * An interface-callback for the activity to listen to fragment interaction
    194      * events.
    195      */
    196     public interface DeviceActionListener {
    197 
    198         void showDetails(WifiP2pDevice device);
    199 
    200         void cancelDisconnect();
    201 
    202         void connect(WifiP2pConfig config);
    203 
    204         void disconnect();
    205     }
    206 
    207 }
    208