Home | History | Annotate | Download | only in discovery
      1 
      2 package com.example.android.wifidirect.discovery;
      3 
      4 import android.app.Fragment;
      5 import android.content.Context;
      6 import android.os.Bundle;
      7 import android.os.Handler;
      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  * This fragment handles chat related UI which includes a list view for messages
     20  * and a message entry field with send button.
     21  */
     22 public class WiFiChatFragment extends Fragment {
     23 
     24     private View view;
     25     private ChatManager chatManager;
     26     private TextView chatLine;
     27     private ListView listView;
     28     ChatMessageAdapter adapter = null;
     29     private List<String> items = new ArrayList<String>();
     30 
     31     @Override
     32     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     33             Bundle savedInstanceState) {
     34         view = inflater.inflate(R.layout.fragment_chat, container, false);
     35         chatLine = (TextView) view.findViewById(R.id.txtChatLine);
     36         listView = (ListView) view.findViewById(android.R.id.list);
     37         adapter = new ChatMessageAdapter(getActivity(), android.R.id.text1,
     38                 items);
     39         listView.setAdapter(adapter);
     40         view.findViewById(R.id.button1).setOnClickListener(
     41                 new View.OnClickListener() {
     42 
     43                     @Override
     44                     public void onClick(View arg0) {
     45                         if (chatManager != null) {
     46                             chatManager.write(chatLine.getText().toString());
     47                             pushMessage("Me: " + chatLine.getText().toString());
     48                             chatLine.setText("");
     49                             chatLine.clearFocus();
     50                         }
     51                     }
     52                 });
     53         return view;
     54     }
     55 
     56     public interface MessageTarget {
     57         public Handler getHandler();
     58     }
     59 
     60     public void setChatManager(ChatManager obj) {
     61         chatManager = obj;
     62     }
     63 
     64     public void pushMessage(String readMessage) {
     65         adapter.add(readMessage);
     66         adapter.notifyDataSetChanged();
     67     }
     68 
     69     /**
     70      * ArrayAdapter to manage chat messages.
     71      */
     72     public class ChatMessageAdapter extends ArrayAdapter<String> {
     73 
     74         List<String> messages = null;
     75 
     76         public ChatMessageAdapter(Context context, int textViewResourceId,
     77                 List<String> items) {
     78             super(context, textViewResourceId, items);
     79         }
     80 
     81         @Override
     82         public View getView(int position, View convertView, ViewGroup parent) {
     83             View v = convertView;
     84             if (v == null) {
     85                 LayoutInflater vi = (LayoutInflater) getActivity()
     86                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     87                 v = vi.inflate(android.R.layout.simple_list_item_1, null);
     88             }
     89             String message = items.get(position);
     90             if (message != null && !message.isEmpty()) {
     91                 TextView nameText = (TextView) v
     92                         .findViewById(android.R.id.text1);
     93 
     94                 if (nameText != null) {
     95                     nameText.setText(message);
     96                     if (message.startsWith("Me: ")) {
     97                         nameText.setTextAppearance(getActivity(),
     98                                 R.style.normalText);
     99                     } else {
    100                         nameText.setTextAppearance(getActivity(),
    101                                 R.style.boldText);
    102                     }
    103                 }
    104             }
    105             return v;
    106         }
    107     }
    108 }
    109