blob: eba51eeaead5ae58d9c35c97b19845a6c073335a [file] [log] [blame]
alisionf76de3b2013-04-16 15:35:22 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
alisionf76de3b2013-04-16 15:35:22 -04003 *
alision2ec64f92013-06-17 17:28:58 -04004 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
alisionf76de3b2013-04-16 15:35:22 -04005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31package com.savoirfairelinux.sflphone.fragments;
32
Alexandre Lision6d75d062013-09-13 14:18:34 -040033import java.util.ArrayList;
alision2cb99562013-05-30 17:02:20 -040034import java.util.HashMap;
alisionf76de3b2013-04-16 15:35:22 -040035
36import android.app.Activity;
alisionb1763882013-06-18 17:30:51 -040037import android.app.Fragment;
Alexandre Lision6d75d062013-09-13 14:18:34 -040038import android.content.Context;
alisionf76de3b2013-04-16 15:35:22 -040039import android.os.Bundle;
alision2cb99562013-05-30 17:02:20 -040040import android.os.RemoteException;
alisionf76de3b2013-04-16 15:35:22 -040041import android.util.Log;
42import android.view.LayoutInflater;
alisiond8c83882013-05-17 17:00:42 -040043import android.view.Menu;
44import android.view.MenuInflater;
alisionf76de3b2013-04-16 15:35:22 -040045import android.view.View;
alision2cb99562013-05-30 17:02:20 -040046import android.view.View.OnClickListener;
alision43a9b362013-05-01 16:30:15 -040047import android.view.ViewGroup;
Alexandre Lision1a9e3b12013-09-16 11:06:07 -040048import android.widget.AdapterView;
49import android.widget.AdapterView.OnItemClickListener;
Alexandre Lision6d75d062013-09-13 14:18:34 -040050import android.widget.BaseAdapter;
alision43a9b362013-05-01 16:30:15 -040051import android.widget.Button;
Alexandre Lision573045c2013-09-11 17:20:25 -040052import android.widget.ListView;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040053import android.widget.TextView;
alisionf76de3b2013-04-16 15:35:22 -040054
55import com.savoirfairelinux.sflphone.R;
alisiondf1dac92013-06-27 17:35:53 -040056import com.savoirfairelinux.sflphone.model.Conference;
alisionf76de3b2013-04-16 15:35:22 -040057import com.savoirfairelinux.sflphone.model.SipCall;
58import com.savoirfairelinux.sflphone.service.ISipService;
59
alisionb1763882013-06-18 17:30:51 -040060public class HomeFragment extends Fragment {
alision55c36cb2013-06-14 14:57:38 -040061 private static final String TAG = HomeFragment.class.getSimpleName();
alision907bde72013-06-20 14:40:37 -040062
alision9f7a6ec2013-05-24 16:26:26 -040063 private Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lision1a9e3b12013-09-16 11:06:07 -040064// Button access_calls;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040065 TextView nb_calls, nb_confs;
Alexandre Lision6d75d062013-09-13 14:18:34 -040066 CallListAdapter confs_adapter;
67
68 private CallListAdapter calls_adapter;
alision43a9b362013-05-01 16:30:15 -040069
alision9f7a6ec2013-05-24 16:26:26 -040070 /**
71 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
72 */
73 private static Callbacks sDummyCallbacks = new Callbacks() {
alision371b77e2013-04-23 14:51:26 -040074
alision9f7a6ec2013-05-24 16:26:26 -040075 @Override
76 public ISipService getService() {
alision2cb99562013-05-30 17:02:20 -040077 Log.i(TAG, "I'm a dummy");
alision9f7a6ec2013-05-24 16:26:26 -040078 return null;
79 }
alisiondf1dac92013-06-27 17:35:53 -040080
81 @Override
Alexandre Lision1a9e3b12013-09-16 11:06:07 -040082 public void selectedCall(Conference c) {
alisiondf1dac92013-06-27 17:35:53 -040083 }
alision9f7a6ec2013-05-24 16:26:26 -040084 };
alision371b77e2013-04-23 14:51:26 -040085
alision9f7a6ec2013-05-24 16:26:26 -040086 /**
87 * The Activity calling this fragment has to implement this interface
88 *
89 */
90 public interface Callbacks {
alision371b77e2013-04-23 14:51:26 -040091
alision9f7a6ec2013-05-24 16:26:26 -040092 public ISipService getService();
alision43a9b362013-05-01 16:30:15 -040093
Alexandre Lision1a9e3b12013-09-16 11:06:07 -040094 public void selectedCall(Conference c);
alisiondf1dac92013-06-27 17:35:53 -040095
alision9f7a6ec2013-05-24 16:26:26 -040096 }
alisionf76de3b2013-04-16 15:35:22 -040097
alision9f7a6ec2013-05-24 16:26:26 -040098 @Override
99 public void onAttach(Activity activity) {
100 super.onAttach(activity);
alisionf76de3b2013-04-16 15:35:22 -0400101
alision9f7a6ec2013-05-24 16:26:26 -0400102 if (!(activity instanceof Callbacks)) {
103 throw new IllegalStateException("Activity must implement fragment's callbacks.");
104 }
alisionf76de3b2013-04-16 15:35:22 -0400105
alision9f7a6ec2013-05-24 16:26:26 -0400106 mCallbacks = (Callbacks) activity;
alisiondf1dac92013-06-27 17:35:53 -0400107
alision2cb99562013-05-30 17:02:20 -0400108 }
alisiondf1dac92013-06-27 17:35:53 -0400109
alision2cb99562013-05-30 17:02:20 -0400110 @Override
alisiondf1dac92013-06-27 17:35:53 -0400111 public void onResume() {
alision2cb99562013-05-30 17:02:20 -0400112 super.onResume();
alision2cb99562013-05-30 17:02:20 -0400113 if (mCallbacks.getService() != null) {
114 try {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400115
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400116 updateLists();
alision2cb99562013-05-30 17:02:20 -0400117
alision2cb99562013-05-30 17:02:20 -0400118 } catch (RemoteException e) {
119 Log.e(TAG, e.toString());
120 }
121 }
alisiondf1dac92013-06-27 17:35:53 -0400122
alision9f7a6ec2013-05-24 16:26:26 -0400123 }
alisionf76de3b2013-04-16 15:35:22 -0400124
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400125 public void updateLists() throws RemoteException {
126 HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList();
127 HashMap<String, Conference> confs = (HashMap<String, Conference>) mCallbacks.getService().getConferenceList();
128
129 updateCallList(calls);
130 updateConferenceList(confs);
131 }
132
Alexandre Lision573045c2013-09-11 17:20:25 -0400133 private void updateConferenceList(HashMap<String, Conference> confs) {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400134 nb_confs.setText("" + confs.size());
135 confs_adapter.update(new ArrayList<Conference>(confs.values()));
Alexandre Lision573045c2013-09-11 17:20:25 -0400136 }
137
138 private void updateCallList(HashMap<String, SipCall> calls) {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400139 nb_calls.setText("" + calls.size());
140 ArrayList<Conference> conferences = new ArrayList<Conference>();
141 for (SipCall call : calls.values()) {
142 Log.w(TAG, "SimpleCall:" + call.getCallId());
143 Conference confOne = new Conference("-1");
144 confOne.getParticipants().add(call);
145 conferences.add(confOne);
146 }
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400147
Alexandre Lision6d75d062013-09-13 14:18:34 -0400148 calls_adapter.update(conferences);
149
Alexandre Lision573045c2013-09-11 17:20:25 -0400150 }
151
alision9f7a6ec2013-05-24 16:26:26 -0400152 @Override
153 public void onDetach() {
154 super.onDetach();
155 mCallbacks = sDummyCallbacks;
156 }
alisionf76de3b2013-04-16 15:35:22 -0400157
alision9f7a6ec2013-05-24 16:26:26 -0400158 @Override
159 public void onCreate(Bundle savedInstanceState) {
160 super.onCreate(savedInstanceState);
alisiondf1dac92013-06-27 17:35:53 -0400161 // mAdapter = new CallElementAdapter(getActivity(), new ArrayList<SipCall>());
162
alision9f7a6ec2013-05-24 16:26:26 -0400163 }
alisionf76de3b2013-04-16 15:35:22 -0400164
alision9f7a6ec2013-05-24 16:26:26 -0400165 @Override
166 public void onActivityCreated(Bundle savedInstanceState) {
167 super.onActivityCreated(savedInstanceState);
alisionf76de3b2013-04-16 15:35:22 -0400168
alision9f7a6ec2013-05-24 16:26:26 -0400169 // Give some text to display if there is no data. In a real
170 // application this would come from a resource.
171 // setEmptyText("No phone numbers");
alisionf76de3b2013-04-16 15:35:22 -0400172
alision9f7a6ec2013-05-24 16:26:26 -0400173 // We have a menu item to show in action bar.
174 setHasOptionsMenu(true);
alisionf76de3b2013-04-16 15:35:22 -0400175
alision9f7a6ec2013-05-24 16:26:26 -0400176 }
alision371b77e2013-04-23 14:51:26 -0400177
alision9f7a6ec2013-05-24 16:26:26 -0400178 @Override
179 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
180 inflater.inflate(R.menu.call_element_menu, menu);
alisionf76de3b2013-04-16 15:35:22 -0400181
alision9f7a6ec2013-05-24 16:26:26 -0400182 }
alisionf76de3b2013-04-16 15:35:22 -0400183
alision9f7a6ec2013-05-24 16:26:26 -0400184 @Override
185 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
186 Log.i(TAG, "onCreateView");
alision465ceba2013-07-04 09:24:30 -0400187 View inflatedView = inflater.inflate(R.layout.frag_home, container, false);
alision43a9b362013-05-01 16:30:15 -0400188
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400189 nb_calls = (TextView) inflatedView.findViewById(R.id.calls_counter);
190 nb_confs = (TextView) inflatedView.findViewById(R.id.confs_counter);
Alexandre Lision6d75d062013-09-13 14:18:34 -0400191
192 confs_adapter = new CallListAdapter(getActivity());
193 ((ListView) inflatedView.findViewById(R.id.confs_list)).setAdapter(confs_adapter);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400194
Alexandre Lision6d75d062013-09-13 14:18:34 -0400195 calls_adapter = new CallListAdapter(getActivity());
196 ((ListView) inflatedView.findViewById(R.id.calls_list)).setAdapter(calls_adapter);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400197 ((ListView) inflatedView.findViewById(R.id.calls_list)).setOnItemClickListener(callClickListener);
198 ((ListView) inflatedView.findViewById(R.id.confs_list)).setOnItemClickListener(callClickListener);
alision43a9b362013-05-01 16:30:15 -0400199
alision9f7a6ec2013-05-24 16:26:26 -0400200 return inflatedView;
201 }
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400202
203 OnItemClickListener callClickListener= new OnItemClickListener() {
204
205 @Override
206 public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
207 mCallbacks.selectedCall((Conference)v.getTag());
208 }
209 };
210
alisione2a38e12013-04-25 14:20:20 -0400211
Alexandre Lision6d75d062013-09-13 14:18:34 -0400212 public class CallListAdapter extends BaseAdapter {
213
214 private ArrayList<Conference> calls;
215
216 private Context mContext;
217
218 public CallListAdapter(Context act) {
219 super();
220 mContext = act;
221 calls = new ArrayList<Conference>();
222
223 }
224
225 public void remove(Conference transfer) {
226
227 }
228
229 public void update(ArrayList<Conference> list) {
230 calls.clear();
231 calls.addAll(list);
232 notifyDataSetChanged();
233 }
234
235 @Override
236 public int getCount() {
237 return calls.size();
238 }
239
240 @Override
241 public Conference getItem(int position) {
242 return calls.get(position);
243 }
244
245 @Override
246 public long getItemId(int position) {
247 return 0;
248 }
249
250 @Override
251 public View getView(int position, View convertView, ViewGroup parent) {
252 if (convertView == null)
253 convertView = LayoutInflater.from(mContext).inflate(R.layout.item_calllist, null);
254
255 Conference call = calls.get(position);
256 if (call.getParticipants().size() == 1) {
257 ((TextView) convertView.findViewById(R.id.call_title)).setText(call.getParticipants().get(0).getContact().getmDisplayName());
258 } else {
259 String tmp = "Conference with " + call.getParticipants().size() + " participants";
Alexandre Lision6d75d062013-09-13 14:18:34 -0400260 ((TextView) convertView.findViewById(R.id.call_title)).setText(tmp);
261 }
262 // ((TextView) convertView.findViewById(R.id.num_participants)).setText("" + call.getParticipants().size());
263 ((TextView) convertView.findViewById(R.id.call_status)).setText(call.getState());
264
265 convertView.setTag(call);
266 return convertView;
267 }
268
269 }
270
alisionf76de3b2013-04-16 15:35:22 -0400271}