blob: d838d4125c064fa032c3945eb22e94e134080a9d [file] [log] [blame]
alisionfde875f2013-05-28 17:01:54 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
alisionfde875f2013-05-28 17:01:54 -04003 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 *
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 */
Alexandre Lision064e1e02013-10-01 16:18:42 -040031package org.sflphone.fragments;
alisionfde875f2013-05-28 17:01:54 -040032
33import java.util.ArrayList;
34import java.util.HashMap;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050035import java.util.Observable;
36import java.util.Observer;
alisionfde875f2013-05-28 17:01:54 -040037
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.R;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050039import org.sflphone.model.CallTimer;
Alexandre Lision064e1e02013-10-01 16:18:42 -040040import org.sflphone.model.Conference;
41import org.sflphone.model.SipCall;
42import org.sflphone.service.ISipService;
Alexandre Lision064e1e02013-10-01 16:18:42 -040043
alisionfde875f2013-05-28 17:01:54 -040044import android.app.Activity;
alision465ceba2013-07-04 09:24:30 -040045import android.content.ClipData;
46import android.content.ClipData.Item;
alisionb1763882013-06-18 17:30:51 -040047import android.content.Context;
48import android.content.Intent;
alision465ceba2013-07-04 09:24:30 -040049import android.graphics.Color;
alisionfde875f2013-05-28 17:01:54 -040050import android.os.Bundle;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050051import android.os.Handler;
alisionfde875f2013-05-28 17:01:54 -040052import android.os.RemoteException;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050053import android.os.SystemClock;
alision465ceba2013-07-04 09:24:30 -040054import android.os.Vibrator;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050055import android.support.v4.app.Fragment;
alisionfde875f2013-05-28 17:01:54 -040056import android.util.Log;
alision465ceba2013-07-04 09:24:30 -040057import android.view.DragEvent;
alisionfde875f2013-05-28 17:01:54 -040058import android.view.LayoutInflater;
59import android.view.View;
alision465ceba2013-07-04 09:24:30 -040060import android.view.View.DragShadowBuilder;
61import android.view.View.OnDragListener;
alisionfde875f2013-05-28 17:01:54 -040062import android.view.ViewGroup;
alision465ceba2013-07-04 09:24:30 -040063import android.widget.AdapterView;
64import android.widget.AdapterView.OnItemClickListener;
65import android.widget.AdapterView.OnItemLongClickListener;
alision465ceba2013-07-04 09:24:30 -040066import android.widget.BaseAdapter;
alision465ceba2013-07-04 09:24:30 -040067import android.widget.ListView;
alisionb1763882013-06-18 17:30:51 -040068import android.widget.TextView;
alision1005ba12013-06-19 13:52:44 -040069import android.widget.Toast;
alisionfde875f2013-05-28 17:01:54 -040070
alisionfde875f2013-05-28 17:01:54 -040071public class CallListFragment extends Fragment {
Alexandre Lisionf02190d2013-12-12 17:26:12 -050072 private static final String TAG = CallListFragment.class.getSimpleName();
alisionfde875f2013-05-28 17:01:54 -040073
74 private Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050075 private TextView nb_calls, nb_confs;
76 CallListAdapter confs_adapter, calls_adapter;
77 CallTimer timer;
alisionfde875f2013-05-28 17:01:54 -040078
alision1005ba12013-06-19 13:52:44 -040079 public static final int REQUEST_TRANSFER = 10;
80 public static final int REQUEST_CONF = 20;
81
alisionfde875f2013-05-28 17:01:54 -040082 /**
83 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
84 */
85 private static Callbacks sDummyCallbacks = new Callbacks() {
86
87 @Override
88 public ISipService getService() {
Alexandre Lisionf02190d2013-12-12 17:26:12 -050089 Log.i(TAG, "I'm a dummy");
alisionfde875f2013-05-28 17:01:54 -040090 return null;
91 }
alision85992112013-05-29 12:18:08 -040092
93 @Override
Alexandre Lisionf02190d2013-12-12 17:26:12 -050094 public void selectedCall(Conference c) {
alision85992112013-05-29 12:18:08 -040095 }
alisionfde875f2013-05-28 17:01:54 -040096 };
97
98 /**
99 * The Activity calling this fragment has to implement this interface
100 *
101 */
102 public interface Callbacks {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500103
alisionfde875f2013-05-28 17:01:54 -0400104 public ISipService getService();
alisionb1763882013-06-18 17:30:51 -0400105
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500106 public void selectedCall(Conference c);
alisionfde875f2013-05-28 17:01:54 -0400107
108 }
109
110 @Override
111 public void onAttach(Activity activity) {
112 super.onAttach(activity);
113
114 if (!(activity instanceof Callbacks)) {
115 throw new IllegalStateException("Activity must implement fragment's callbacks.");
116 }
117
118 mCallbacks = (Callbacks) activity;
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500119
120 }
121
122 private Runnable mUpdateTimeTask = new Runnable() {
123 public void run() {
124 final long start = SystemClock.uptimeMillis();
125 long millis = SystemClock.uptimeMillis() - start;
126 int seconds = (int) (millis / 1000);
127 int minutes = seconds / 60;
128 seconds = seconds % 60;
129
130 calls_adapter.notifyDataSetChanged();
131 confs_adapter.notifyDataSetChanged();
132 mHandler.postAtTime(this, start + (((minutes * 60) + seconds + 1) * 1000));
133 }
134 };
135
136 private Handler mHandler = new Handler();
137
138 @Override
139 public void onResume() {
140 super.onResume();
141 if (mCallbacks.getService() != null) {
142 try {
143 updateLists();
144 if (!calls_adapter.isEmpty() || !confs_adapter.isEmpty()) {
145 mHandler.postDelayed(mUpdateTimeTask, 0);
146 }
147
148 } catch (RemoteException e) {
149 Log.e(TAG, e.toString());
150 }
151 }
152
153 }
154
155 @SuppressWarnings("unchecked")
156 // No proper solution with HashMap runtime cast
157 public void updateLists() throws RemoteException {
158 HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList();
159 HashMap<String, Conference> confs = (HashMap<String, Conference>) mCallbacks.getService().getConferenceList();
160
161 updateCallList(calls);
162 updateConferenceList(confs);
163 }
164
165 private void updateConferenceList(HashMap<String, Conference> confs) {
166 nb_confs.setText("" + confs.size());
167 confs_adapter.updateDataset(new ArrayList<Conference>(confs.values()));
168 }
169
170 private void updateCallList(HashMap<String, SipCall> calls) {
171 nb_calls.setText("" + calls.size());
172 ArrayList<Conference> conferences = new ArrayList<Conference>();
173 for (SipCall call : calls.values()) {
174 Conference confOne = new Conference("-1");
175 confOne.getParticipants().add(call);
176 conferences.add(confOne);
177 }
178
179 calls_adapter.updateDataset(conferences);
180
alisionfde875f2013-05-28 17:01:54 -0400181 }
182
183 @Override
184 public void onDetach() {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500185
alisionfde875f2013-05-28 17:01:54 -0400186 super.onDetach();
187 mCallbacks = sDummyCallbacks;
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500188
alisionfde875f2013-05-28 17:01:54 -0400189 }
190
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500191 @Override
192 public void onCreate(Bundle savedInstanceState) {
193 super.onCreate(savedInstanceState);
194 }
195
196 @Override
197 public void onPause() {
198 super.onPause();
199 mHandler.removeCallbacks(mUpdateTimeTask);
200 }
201
202 @Override
203 public void onActivityCreated(Bundle savedInstanceState) {
204
205 super.onActivityCreated(savedInstanceState);
206
207 // Give some text to display if there is no data. In a real
208 // application this would come from a resource.
209 // setEmptyText("No phone numbers");
210
211 // We have a menu item to show in action bar.
212 setHasOptionsMenu(true);
213
214 }
alisionb1763882013-06-18 17:30:51 -0400215
alisionfde875f2013-05-28 17:01:54 -0400216 @Override
217 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500218 Log.i(TAG, "onCreateView");
219 View inflatedView = inflater.inflate(R.layout.frag_call_list, container, false);
alisionfde875f2013-05-28 17:01:54 -0400220
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500221 nb_calls = (TextView) inflatedView.findViewById(R.id.calls_counter);
222 nb_confs = (TextView) inflatedView.findViewById(R.id.confs_counter);
alisionb1763882013-06-18 17:30:51 -0400223
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500224 confs_adapter = new CallListAdapter(getActivity());
225 ((ListView) inflatedView.findViewById(R.id.confs_list)).setAdapter(confs_adapter);
alision465ceba2013-07-04 09:24:30 -0400226
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500227 calls_adapter = new CallListAdapter(getActivity());
228 ((ListView) inflatedView.findViewById(R.id.calls_list)).setAdapter(calls_adapter);
229 ((ListView) inflatedView.findViewById(R.id.calls_list)).setOnItemClickListener(callClickListener);
230 ((ListView) inflatedView.findViewById(R.id.confs_list)).setOnItemClickListener(callClickListener);
231
232 ((ListView) inflatedView.findViewById(R.id.calls_list)).setOnItemLongClickListener(mItemLongClickListener);
233 ((ListView) inflatedView.findViewById(R.id.confs_list)).setOnItemLongClickListener(mItemLongClickListener);
234
235 return inflatedView;
236 }
237
238 OnItemClickListener callClickListener = new OnItemClickListener() {
239
240 @Override
241 public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
242 mCallbacks.selectedCall((Conference) v.getTag());
243 }
244 };
245
246 private OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {
247
248 @Override
249 public boolean onItemLongClick(AdapterView<?> adptv, View view, int pos, long arg3) {
250 final Vibrator vibe = (Vibrator) view.getContext().getSystemService(Context.VIBRATOR_SERVICE);
251 vibe.vibrate(80);
252 Intent i = new Intent();
253 Bundle b = new Bundle();
254 b.putParcelable("conference", (Conference) adptv.getAdapter().getItem(pos));
255 i.putExtra("bconference", b);
256
257 DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
258 ClipData data = ClipData.newIntent("conference", i);
259 view.startDrag(data, shadowBuilder, view, 0);
260 return false;
261 }
262
263 };
264
265 public class CallListAdapter extends BaseAdapter implements Observer {
266
267 private ArrayList<Conference> calls;
268
269 private Context mContext;
270
271 public CallListAdapter(Context act) {
272 super();
273 mContext = act;
274 calls = new ArrayList<Conference>();
275
276 }
277
278 public ArrayList<Conference> getDataset() {
279 return calls;
280 }
281
282 public void remove(Conference transfer) {
283
284 }
285
286 public void updateDataset(ArrayList<Conference> list) {
287 calls.clear();
288 calls.addAll(list);
289 notifyDataSetChanged();
290 }
291
292 @Override
293 public int getCount() {
294 return calls.size();
295 }
296
297 @Override
298 public Conference getItem(int position) {
299 return calls.get(position);
300 }
301
302 @Override
303 public long getItemId(int position) {
304 return 0;
305 }
306
307 @Override
308 public View getView(int position, View convertView, ViewGroup parent) {
309 if (convertView == null)
310 convertView = LayoutInflater.from(mContext).inflate(R.layout.item_calllist, null);
311
312 Conference call = calls.get(position);
313 if (call.getParticipants().size() == 1) {
314 ((TextView) convertView.findViewById(R.id.call_title)).setText(call.getParticipants().get(0).getContact().getmDisplayName());
315
316 long duration = System.currentTimeMillis() / 1000 - (call.getParticipants().get(0).getTimestamp_start());
317
318 ((TextView) convertView.findViewById(R.id.call_time)).setText(String.format("%d:%02d:%02d", duration / 3600, (duration % 3600) / 60,
319 (duration % 60)));
320 } else {
321// String tmp = "Conference with " + call.getParticipants().size() + " participants";
322 ((TextView) convertView.findViewById(R.id.call_title)).setText(getString(R.string.home_conf_item, call.getParticipants().size()));
323 }
324 // ((TextView) convertView.findViewById(R.id.num_participants)).setText("" + call.getParticipants().size());
325 ((TextView) convertView.findViewById(R.id.call_status)).setText(call.getState());
326
327 convertView.setOnDragListener(dragListener);
328 convertView.setTag(call);
329
330 return convertView;
331 }
332
333 @Override
334 public void update(Observable observable, Object data) {
335 Log.i(TAG, "Updating views...");
336 notifyDataSetChanged();
337 }
338
alisionfde875f2013-05-28 17:01:54 -0400339 }
340
alision465ceba2013-07-04 09:24:30 -0400341 OnDragListener dragListener = new OnDragListener() {
342
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500343 @SuppressWarnings("deprecation")
344 // deprecated in API 16....
alision465ceba2013-07-04 09:24:30 -0400345 @Override
346 public boolean onDrag(View v, DragEvent event) {
347 switch (event.getAction()) {
348 case DragEvent.ACTION_DRAG_STARTED:
349 // Do nothing
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500350 // Log.w(TAG, "ACTION_DRAG_STARTED");
alision465ceba2013-07-04 09:24:30 -0400351 break;
352 case DragEvent.ACTION_DRAG_ENTERED:
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500353 // Log.w(TAG, "ACTION_DRAG_ENTERED");
alision465ceba2013-07-04 09:24:30 -0400354 v.setBackgroundColor(Color.GREEN);
355 break;
356 case DragEvent.ACTION_DRAG_EXITED:
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500357 // Log.w(TAG, "ACTION_DRAG_EXITED");
358 v.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_generic_selector));
alision465ceba2013-07-04 09:24:30 -0400359 break;
360 case DragEvent.ACTION_DROP:
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500361 // Log.w(TAG, "ACTION_DROP");
alision465ceba2013-07-04 09:24:30 -0400362 View view = (View) event.getLocalState();
363
364 Item i = event.getClipData().getItemAt(0);
365 Intent intent = i.getIntent();
366 intent.setExtrasClassLoader(Conference.class.getClassLoader());
367
368 Conference initial = (Conference) view.getTag();
369 Conference target = (Conference) v.getTag();
alisionf2ae4362013-07-05 16:16:12 -0400370
371 if (initial == target) {
alision465ceba2013-07-04 09:24:30 -0400372 return true;
373 }
374
375 DropActionsChoice dialog = DropActionsChoice.newInstance();
376 Bundle b = new Bundle();
377 b.putParcelable("call_initial", initial);
378 b.putParcelable("call_targeted", target);
379 dialog.setArguments(b);
380 dialog.setTargetFragment(CallListFragment.this, 0);
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500381 dialog.show(getChildFragmentManager(), "dialog");
alision465ceba2013-07-04 09:24:30 -0400382
alision465ceba2013-07-04 09:24:30 -0400383 // view.setBackgroundColor(Color.WHITE);
384 // v.setBackgroundColor(Color.BLACK);
385 break;
386 case DragEvent.ACTION_DRAG_ENDED:
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500387 // Log.w(TAG, "ACTION_DRAG_ENDED");
alision465ceba2013-07-04 09:24:30 -0400388 View view1 = (View) event.getLocalState();
389 view1.setVisibility(View.VISIBLE);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500390 v.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_generic_selector));
alision465ceba2013-07-04 09:24:30 -0400391 default:
392 break;
393 }
394 return true;
395 }
396
397 };
398
alisionb1763882013-06-18 17:30:51 -0400399 @Override
400 public void onActivityResult(int requestCode, int resultCode, Intent data) {
401 super.onActivityResult(requestCode, resultCode, data);
alisiondf1dac92013-06-27 17:35:53 -0400402 Conference transfer = null;
alision1005ba12013-06-19 13:52:44 -0400403 if (requestCode == REQUEST_TRANSFER) {
404 switch (resultCode) {
405 case 0:
alisiondf1dac92013-06-27 17:35:53 -0400406 Conference c = data.getParcelableExtra("target");
alision1005ba12013-06-19 13:52:44 -0400407 transfer = data.getParcelableExtra("transfer");
408 try {
alisionb1763882013-06-18 17:30:51 -0400409
alisiondf1dac92013-06-27 17:35:53 -0400410 mCallbacks.getService().attendedTransfer(transfer.getParticipants().get(0).getCallId(), c.getParticipants().get(0).getCallId());
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500411 calls_adapter.remove(transfer);
412 calls_adapter.remove(c);
413 calls_adapter.notifyDataSetChanged();
alision1005ba12013-06-19 13:52:44 -0400414 } catch (RemoteException e) {
415 // TODO Auto-generated catch block
416 e.printStackTrace();
417 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500418 Toast.makeText(getActivity(), getString(R.string.home_transfer_complet), Toast.LENGTH_LONG).show();
alision1005ba12013-06-19 13:52:44 -0400419 break;
420
421 case 1:
422 String to = data.getStringExtra("to_number");
423 transfer = data.getParcelableExtra("transfer");
424 try {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500425 Toast.makeText(getActivity(), getString(R.string.home_transfering,transfer.getParticipants().get(0).getContact().getmDisplayName(),to),
alisiondf1dac92013-06-27 17:35:53 -0400426 Toast.LENGTH_SHORT).show();
427 mCallbacks.getService().transfer(transfer.getParticipants().get(0).getCallId(), to);
428 mCallbacks.getService().hangUp(transfer.getParticipants().get(0).getCallId());
alision1005ba12013-06-19 13:52:44 -0400429 } catch (RemoteException e) {
430 // TODO Auto-generated catch block
431 e.printStackTrace();
432 }
433 break;
434
435 default:
436 break;
alisionb1763882013-06-18 17:30:51 -0400437 }
alision907bde72013-06-20 14:40:37 -0400438 } else if (requestCode == REQUEST_CONF) {
alision1005ba12013-06-19 13:52:44 -0400439 switch (resultCode) {
440 case 0:
alision465ceba2013-07-04 09:24:30 -0400441 Conference call_to_add = data.getParcelableExtra("transfer");
442 Conference call_target = data.getParcelableExtra("target");
alision1005ba12013-06-19 13:52:44 -0400443
alision465ceba2013-07-04 09:24:30 -0400444 bindCalls(call_to_add, call_target);
alision1005ba12013-06-19 13:52:44 -0400445 break;
446
447 default:
448 break;
449 }
alisionb1763882013-06-18 17:30:51 -0400450 }
451 }
452
alision465ceba2013-07-04 09:24:30 -0400453 private void bindCalls(Conference call_to_add, Conference call_target) {
454 try {
455
456 if (call_target.hasMultipleParticipants() && !call_to_add.hasMultipleParticipants()) {
457
458 mCallbacks.getService().addParticipant(call_to_add.getParticipants().get(0), call_target.getId());
459
460 } else if (call_target.hasMultipleParticipants() && call_to_add.hasMultipleParticipants()) {
461
462 // We join two conferences
463 mCallbacks.getService().joinConference(call_to_add.getId(), call_target.getId());
464
465 } else if (!call_target.hasMultipleParticipants() && call_to_add.hasMultipleParticipants()) {
466
467 mCallbacks.getService().addParticipant(call_target.getParticipants().get(0), call_to_add.getId());
468
469 } else {
470 // We join two single calls to create a conf
471 mCallbacks.getService().joinParticipant(call_to_add.getParticipants().get(0).getCallId(),
472 call_target.getParticipants().get(0).getCallId());
473 }
474
475 } catch (RemoteException e) {
476 // TODO Auto-generated catch block
477 e.printStackTrace();
478 }
479 }
480
alisionfde875f2013-05-28 17:01:54 -0400481}