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                                     .getBytes());
     48                             pushMessage("Me: " + chatLine.getText().toString());
     49                             chatLine.setText("");
     50                             chatLine.clearFocus();
     51                         }
     52                     }
     53                 });
     54         return view;
     55     }
     56 
     57     public interface MessageTarget {
     58         public Handler getHandler();
     59     }
     60 
     61     public void setChatManager(ChatManager obj) {
     62         chatManager = obj;
     63     }
     64 
     65     public void pushMessage(String readMessage) {
     66         adapter.add(readMessage);
     67         adapter.notifyDataSetChanged();
     68     }
     69 
     70     /**
     71      * ArrayAdapter to manage chat messages.
     72      */
     73     public class ChatMessageAdapter extends ArrayAdapter<String> {
     74 
     75         List<String> messages = null;
     76 
     77         public ChatMessageAdapter(Context context, int textViewResourceId,
     78                 List<String> items) {
     79             super(context, textViewResourceId, items);
     80         }
     81 
     82         @Override
     83         public View getView(int position, View convertView, ViewGroup parent) {
     84             View v = convertView;
     85             if (v == null) {
     86                 LayoutInflater vi = (LayoutInflater) getActivity()
     87                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     88                 v = vi.inflate(android.R.layout.simple_list_item_1, null);
     89             }
     90             String message = items.get(position);
     91             if (message != null && !message.isEmpty()) {
     92                 TextView nameText = (TextView) v
     93                         .findViewById(android.R.id.text1);
     94 
     95                 if (nameText != null) {
     96                     nameText.setText(message);
     97                     if (message.startsWith("Me: ")) {
     98                         nameText.setTextAppearance(getActivity(),
     99                                 R.style.normalText);
    100                     } else {
    101                         nameText.setTextAppearance(getActivity(),
    102                                 R.style.boldText);
    103                     }
    104                 }
    105             }
    106             return v;
    107         }
    108     }
    109 }
    110