blob: c89c236d6f2f67ce7e0cf1d782cfcf9634228b1f [file] [log] [blame]
alisiona4325152013-04-19 11:10:03 -04001package com.savoirfairelinux.sflphone.adapters;
2
alision371b77e2013-04-23 14:51:26 -04003import java.util.HashMap;
4import java.util.Iterator;
alisiona4325152013-04-19 11:10:03 -04005import java.util.List;
6import java.util.concurrent.ExecutorService;
7import java.util.concurrent.Executors;
8
9import android.content.Context;
10import android.view.LayoutInflater;
11import android.view.View;
12import android.view.ViewGroup;
alision5f899632013-04-22 17:26:56 -040013import android.widget.BaseAdapter;
alisiona4325152013-04-19 11:10:03 -040014import android.widget.ImageView;
15import android.widget.TextView;
16
17import com.savoirfairelinux.sflphone.R;
18import com.savoirfairelinux.sflphone.model.SipCall;
19
20/**
21 * A CursorAdapter that creates and update call elements using corresponding contact infos. TODO: handle contact list separatly to allow showing
22 * synchronized contacts on Call cards with multiple contacts etc.
23 */
alision5f899632013-04-22 17:26:56 -040024public class CallElementAdapter extends BaseAdapter {
alisiona4325152013-04-19 11:10:03 -040025 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
26 private Context mContext;
alision371b77e2013-04-23 14:51:26 -040027 private final HashMap<String, SipCall> mCallList;
alisiona4325152013-04-19 11:10:03 -040028 private static final String CURRENT_STATE_LABEL = " CURRENT STATE: ";
29
alision5f899632013-04-22 17:26:56 -040030 public CallElementAdapter(Context context, List<SipCall> callList) {
31 super();
alisiona4325152013-04-19 11:10:03 -040032 mContext = context;
alision371b77e2013-04-23 14:51:26 -040033 mCallList = new HashMap<String, SipCall>();
34 for(SipCall c : callList){
35 mCallList.put(c.getCallId(), c);
36 }
37
alisiona4325152013-04-19 11:10:03 -040038 }
39
40 @Override
41 public View getView(int position, View convertView, ViewGroup parent) {
42 View rowView = convertView;
43 CallElementView entryView = null;
44
45 if (rowView == null) {
46 // Get a new instance of the row layout view
47 LayoutInflater inflater = LayoutInflater.from(mContext);
48 rowView = inflater.inflate(R.layout.item_contact, null);
49
50 // Hold the view objects in an object
51 // so they don't need to be re-fetched
52 entryView = new CallElementView();
alisiona4325152013-04-19 11:10:03 -040053 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
54 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
55 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
56 entryView.state = (TextView) rowView.findViewById(R.id.callstate);
57
58 // Cache the view obects in the tag
59 // so they can be re-accessed later
60 rowView.setTag(entryView);
61 } else {
62 entryView = (CallElementView) rowView.getTag();
63 }
64
65 // Transfer the stock data from the data object
66 // to the view objects
alision371b77e2013-04-23 14:51:26 -040067
68 SipCall call = (SipCall) mCallList.values().toArray()[position];
alisiona4325152013-04-19 11:10:03 -040069 entryView.displayName.setText(call.getDisplayName());
70 entryView.phones.setText(call.getPhone());
71 entryView.state.setText(CURRENT_STATE_LABEL + call.getCallStateString());
72
73 return rowView;
74 }
75
76 /*********************
77 * ViewHolder Pattern
78 *********************/
79 public class CallElementView {
alisiona4325152013-04-19 11:10:03 -040080 protected ImageView photo;
81 protected TextView displayName;
82 protected TextView phones;
83 public TextView state;
84 }
alision5f899632013-04-22 17:26:56 -040085
86 @Override
87 public int getCount() {
88 return mCallList.size();
89 }
90
91 @Override
92 public Object getItem(int pos) {
alision371b77e2013-04-23 14:51:26 -040093 return mCallList.values().toArray()[pos];
alision5f899632013-04-22 17:26:56 -040094 }
95
96 @Override
97 public long getItemId(int arg0) {
98 // TODO Auto-generated method stub
99 return 0;
100 }
101
102 public void add(SipCall c) {
alision371b77e2013-04-23 14:51:26 -0400103 mCallList.put(c.getCallId(), c);
104 notifyDataSetChanged();
alision5f899632013-04-22 17:26:56 -0400105
106 }
107
alision371b77e2013-04-23 14:51:26 -0400108
109 public void update(String id, String newState) {
110 if(newState.equals("INCOMING")) {
111 mCallList.get(id).setCallState(SipCall.CALL_STATE_INCOMING);
112 } else if(newState.equals("RINGING")) {
113 mCallList.get(id).setCallState(SipCall.CALL_STATE_RINGING);
114 } else if(newState.equals("CURRENT")) {
115 mCallList.get(id).setCallState(SipCall.CALL_STATE_CURRENT);
116 } else if(newState.equals("HUNGUP")) {
117 mCallList.get(id).setCallState(SipCall.CALL_STATE_HUNGUP);
118 } else if(newState.equals("BUSY")) {
119 mCallList.get(id).setCallState(SipCall.CALL_STATE_BUSY);
120 } else if(newState.equals("FAILURE")) {
121 mCallList.get(id).setCallState(SipCall.CALL_STATE_FAILURE);
122 } else if(newState.equals("HOLD")) {
123 mCallList.get(id).setCallState(SipCall.CALL_STATE_HOLD);
124 } else if(newState.equals("UNHOLD")) {
125 mCallList.get(id).setCallState(SipCall.CALL_STATE_CURRENT);
126 } else {
127 mCallList.get(id).setCallState(SipCall.CALL_STATE_NONE);
128 }
129 notifyDataSetChanged();
alision5f899632013-04-22 17:26:56 -0400130
131 }
132
alisiona4325152013-04-19 11:10:03 -0400133}