blob: 33228ae0e0246b8b8e5741097bd63657cacfcecd [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 Lisionebeb3662013-09-17 16:20:54 -040033import java.text.SimpleDateFormat;
Alexandre Lision6d75d062013-09-13 14:18:34 -040034import java.util.ArrayList;
Alexandre Lisionebeb3662013-09-17 16:20:54 -040035import java.util.Date;
alision2cb99562013-05-30 17:02:20 -040036import java.util.HashMap;
Alexandre Lisionebeb3662013-09-17 16:20:54 -040037import java.util.Locale;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040038import java.util.Observable;
39import java.util.Observer;
Alexandre Lisionebeb3662013-09-17 16:20:54 -040040import java.util.TimeZone;
alisionf76de3b2013-04-16 15:35:22 -040041
42import android.app.Activity;
alisionb1763882013-06-18 17:30:51 -040043import android.app.Fragment;
Alexandre Lision6d75d062013-09-13 14:18:34 -040044import android.content.Context;
alisionf76de3b2013-04-16 15:35:22 -040045import android.os.Bundle;
Alexandre Lisionebeb3662013-09-17 16:20:54 -040046import android.os.Handler;
alision2cb99562013-05-30 17:02:20 -040047import android.os.RemoteException;
Alexandre Lisionebeb3662013-09-17 16:20:54 -040048import android.os.SystemClock;
49import android.text.format.DateFormat;
alisionf76de3b2013-04-16 15:35:22 -040050import android.util.Log;
51import android.view.LayoutInflater;
alisiond8c83882013-05-17 17:00:42 -040052import android.view.Menu;
53import android.view.MenuInflater;
alisionf76de3b2013-04-16 15:35:22 -040054import android.view.View;
alision43a9b362013-05-01 16:30:15 -040055import android.view.ViewGroup;
Alexandre Lision1a9e3b12013-09-16 11:06:07 -040056import android.widget.AdapterView;
57import android.widget.AdapterView.OnItemClickListener;
Alexandre Lision6d75d062013-09-13 14:18:34 -040058import android.widget.BaseAdapter;
Alexandre Lision573045c2013-09-11 17:20:25 -040059import android.widget.ListView;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040060import android.widget.TextView;
alisionf76de3b2013-04-16 15:35:22 -040061
62import com.savoirfairelinux.sflphone.R;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040063import com.savoirfairelinux.sflphone.model.CallTimer;
alisiondf1dac92013-06-27 17:35:53 -040064import com.savoirfairelinux.sflphone.model.Conference;
alisionf76de3b2013-04-16 15:35:22 -040065import com.savoirfairelinux.sflphone.model.SipCall;
66import com.savoirfairelinux.sflphone.service.ISipService;
67
alisionb1763882013-06-18 17:30:51 -040068public class HomeFragment extends Fragment {
alision55c36cb2013-06-14 14:57:38 -040069 private static final String TAG = HomeFragment.class.getSimpleName();
alision907bde72013-06-20 14:40:37 -040070
alision9f7a6ec2013-05-24 16:26:26 -040071 private Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lisionebeb3662013-09-17 16:20:54 -040072 // Button access_calls;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040073 TextView nb_calls, nb_confs;
Alexandre Lision6d75d062013-09-13 14:18:34 -040074 CallListAdapter confs_adapter;
Alexandre Lisionebeb3662013-09-17 16:20:54 -040075 CallTimer timer;
Alexandre Lision6d75d062013-09-13 14:18:34 -040076
77 private CallListAdapter calls_adapter;
alision43a9b362013-05-01 16:30:15 -040078
alision9f7a6ec2013-05-24 16:26:26 -040079 /**
80 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
81 */
82 private static Callbacks sDummyCallbacks = new Callbacks() {
alision371b77e2013-04-23 14:51:26 -040083
alision9f7a6ec2013-05-24 16:26:26 -040084 @Override
85 public ISipService getService() {
alision2cb99562013-05-30 17:02:20 -040086 Log.i(TAG, "I'm a dummy");
alision9f7a6ec2013-05-24 16:26:26 -040087 return null;
88 }
alisiondf1dac92013-06-27 17:35:53 -040089
90 @Override
Alexandre Lision1a9e3b12013-09-16 11:06:07 -040091 public void selectedCall(Conference c) {
alisiondf1dac92013-06-27 17:35:53 -040092 }
alision9f7a6ec2013-05-24 16:26:26 -040093 };
alision371b77e2013-04-23 14:51:26 -040094
alision9f7a6ec2013-05-24 16:26:26 -040095 /**
96 * The Activity calling this fragment has to implement this interface
97 *
98 */
99 public interface Callbacks {
alision371b77e2013-04-23 14:51:26 -0400100
alision9f7a6ec2013-05-24 16:26:26 -0400101 public ISipService getService();
alision43a9b362013-05-01 16:30:15 -0400102
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400103 public void selectedCall(Conference c);
alisiondf1dac92013-06-27 17:35:53 -0400104
alision9f7a6ec2013-05-24 16:26:26 -0400105 }
alisionf76de3b2013-04-16 15:35:22 -0400106
alision9f7a6ec2013-05-24 16:26:26 -0400107 @Override
108 public void onAttach(Activity activity) {
109 super.onAttach(activity);
alisionf76de3b2013-04-16 15:35:22 -0400110
alision9f7a6ec2013-05-24 16:26:26 -0400111 if (!(activity instanceof Callbacks)) {
112 throw new IllegalStateException("Activity must implement fragment's callbacks.");
113 }
alisionf76de3b2013-04-16 15:35:22 -0400114
alision9f7a6ec2013-05-24 16:26:26 -0400115 mCallbacks = (Callbacks) activity;
alisiondf1dac92013-06-27 17:35:53 -0400116
alision2cb99562013-05-30 17:02:20 -0400117 }
alisiondf1dac92013-06-27 17:35:53 -0400118
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400119 private Runnable mUpdateTimeTask = new Runnable() {
120 public void run() {
121 final long start = SystemClock.uptimeMillis();
122 long millis = SystemClock.uptimeMillis() - start;
123 int seconds = (int) (millis / 1000);
124 int minutes = seconds / 60;
125 seconds = seconds % 60;
126
127 calls_adapter.notifyDataSetChanged();
Alexandre Lision0c384512013-09-17 17:15:57 -0400128 confs_adapter.notifyDataSetChanged();
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400129 mHandler.postAtTime(this, start + (((minutes * 60) + seconds + 1) * 1000));
130 }
131 };
132
133 private Handler mHandler = new Handler();
134
alision2cb99562013-05-30 17:02:20 -0400135 @Override
alisiondf1dac92013-06-27 17:35:53 -0400136 public void onResume() {
alision2cb99562013-05-30 17:02:20 -0400137 super.onResume();
alision2cb99562013-05-30 17:02:20 -0400138 if (mCallbacks.getService() != null) {
139 try {
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400140 updateLists();
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400141 if (!calls_adapter.isEmpty() || !confs_adapter.isEmpty()) {
Alexandre Lision0c384512013-09-17 17:15:57 -0400142 mHandler.postDelayed(mUpdateTimeTask, 0);
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400143 }
alision2cb99562013-05-30 17:02:20 -0400144
alision2cb99562013-05-30 17:02:20 -0400145 } catch (RemoteException e) {
146 Log.e(TAG, e.toString());
147 }
148 }
alisiondf1dac92013-06-27 17:35:53 -0400149
alision9f7a6ec2013-05-24 16:26:26 -0400150 }
alisionf76de3b2013-04-16 15:35:22 -0400151
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400152 public void updateLists() throws RemoteException {
153 HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList();
154 HashMap<String, Conference> confs = (HashMap<String, Conference>) mCallbacks.getService().getConferenceList();
155
156 updateCallList(calls);
157 updateConferenceList(confs);
158 }
159
Alexandre Lision573045c2013-09-11 17:20:25 -0400160 private void updateConferenceList(HashMap<String, Conference> confs) {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400161 nb_confs.setText("" + confs.size());
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400162 confs_adapter.updateDataset(new ArrayList<Conference>(confs.values()));
Alexandre Lision573045c2013-09-11 17:20:25 -0400163 }
164
165 private void updateCallList(HashMap<String, SipCall> calls) {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400166 nb_calls.setText("" + calls.size());
167 ArrayList<Conference> conferences = new ArrayList<Conference>();
168 for (SipCall call : calls.values()) {
169 Log.w(TAG, "SimpleCall:" + call.getCallId());
170 Conference confOne = new Conference("-1");
171 confOne.getParticipants().add(call);
172 conferences.add(confOne);
173 }
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400174
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400175 calls_adapter.updateDataset(conferences);
Alexandre Lision6d75d062013-09-13 14:18:34 -0400176
Alexandre Lision573045c2013-09-11 17:20:25 -0400177 }
178
alision9f7a6ec2013-05-24 16:26:26 -0400179 @Override
180 public void onDetach() {
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400181
alision9f7a6ec2013-05-24 16:26:26 -0400182 super.onDetach();
183 mCallbacks = sDummyCallbacks;
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400184
alision9f7a6ec2013-05-24 16:26:26 -0400185 }
alisionf76de3b2013-04-16 15:35:22 -0400186
alision9f7a6ec2013-05-24 16:26:26 -0400187 @Override
188 public void onCreate(Bundle savedInstanceState) {
189 super.onCreate(savedInstanceState);
Alexandre Lision0c384512013-09-17 17:15:57 -0400190 }
191
192 @Override
193 public void onPause(){
194 super.onPause();
195 mHandler.removeCallbacks(mUpdateTimeTask);
alision9f7a6ec2013-05-24 16:26:26 -0400196 }
alisionf76de3b2013-04-16 15:35:22 -0400197
alision9f7a6ec2013-05-24 16:26:26 -0400198 @Override
199 public void onActivityCreated(Bundle savedInstanceState) {
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400200
alision9f7a6ec2013-05-24 16:26:26 -0400201 super.onActivityCreated(savedInstanceState);
alisionf76de3b2013-04-16 15:35:22 -0400202
alision9f7a6ec2013-05-24 16:26:26 -0400203 // Give some text to display if there is no data. In a real
204 // application this would come from a resource.
205 // setEmptyText("No phone numbers");
alisionf76de3b2013-04-16 15:35:22 -0400206
alision9f7a6ec2013-05-24 16:26:26 -0400207 // We have a menu item to show in action bar.
208 setHasOptionsMenu(true);
alisionf76de3b2013-04-16 15:35:22 -0400209
alision9f7a6ec2013-05-24 16:26:26 -0400210 }
alision371b77e2013-04-23 14:51:26 -0400211
alision9f7a6ec2013-05-24 16:26:26 -0400212 @Override
213 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400214
alision9f7a6ec2013-05-24 16:26:26 -0400215 inflater.inflate(R.menu.call_element_menu, menu);
alisionf76de3b2013-04-16 15:35:22 -0400216
alision9f7a6ec2013-05-24 16:26:26 -0400217 }
alisionf76de3b2013-04-16 15:35:22 -0400218
alision9f7a6ec2013-05-24 16:26:26 -0400219 @Override
220 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
221 Log.i(TAG, "onCreateView");
alision465ceba2013-07-04 09:24:30 -0400222 View inflatedView = inflater.inflate(R.layout.frag_home, container, false);
alision43a9b362013-05-01 16:30:15 -0400223
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400224 nb_calls = (TextView) inflatedView.findViewById(R.id.calls_counter);
225 nb_confs = (TextView) inflatedView.findViewById(R.id.confs_counter);
Alexandre Lision6d75d062013-09-13 14:18:34 -0400226
227 confs_adapter = new CallListAdapter(getActivity());
228 ((ListView) inflatedView.findViewById(R.id.confs_list)).setAdapter(confs_adapter);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400229
Alexandre Lision6d75d062013-09-13 14:18:34 -0400230 calls_adapter = new CallListAdapter(getActivity());
231 ((ListView) inflatedView.findViewById(R.id.calls_list)).setAdapter(calls_adapter);
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400232 ((ListView) inflatedView.findViewById(R.id.calls_list)).setOnItemClickListener(callClickListener);
233 ((ListView) inflatedView.findViewById(R.id.confs_list)).setOnItemClickListener(callClickListener);
alision43a9b362013-05-01 16:30:15 -0400234
alision9f7a6ec2013-05-24 16:26:26 -0400235 return inflatedView;
236 }
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400237
238 OnItemClickListener callClickListener = new OnItemClickListener() {
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400239
240 @Override
241 public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400242 mCallbacks.selectedCall((Conference) v.getTag());
Alexandre Lision1a9e3b12013-09-16 11:06:07 -0400243 }
244 };
alisione2a38e12013-04-25 14:20:20 -0400245
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400246 public class CallListAdapter extends BaseAdapter implements Observer {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400247
248 private ArrayList<Conference> calls;
249
250 private Context mContext;
251
252 public CallListAdapter(Context act) {
253 super();
254 mContext = act;
255 calls = new ArrayList<Conference>();
256
257 }
258
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400259 public ArrayList<Conference> getDataset() {
260 return calls;
261 }
262
Alexandre Lision6d75d062013-09-13 14:18:34 -0400263 public void remove(Conference transfer) {
264
265 }
266
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400267 public void updateDataset(ArrayList<Conference> list) {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400268 calls.clear();
269 calls.addAll(list);
270 notifyDataSetChanged();
271 }
272
273 @Override
274 public int getCount() {
275 return calls.size();
276 }
277
278 @Override
279 public Conference getItem(int position) {
280 return calls.get(position);
281 }
282
283 @Override
284 public long getItemId(int position) {
285 return 0;
286 }
287
288 @Override
289 public View getView(int position, View convertView, ViewGroup parent) {
290 if (convertView == null)
291 convertView = LayoutInflater.from(mContext).inflate(R.layout.item_calllist, null);
292
293 Conference call = calls.get(position);
294 if (call.getParticipants().size() == 1) {
295 ((TextView) convertView.findViewById(R.id.call_title)).setText(call.getParticipants().get(0).getContact().getmDisplayName());
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400296
297 long duration = System.currentTimeMillis() / 1000 - (call.getParticipants().get(0).getTimestamp_start());
298
299 ((TextView) convertView.findViewById(R.id.call_time)).setText(String.format("%d:%02d:%02d", duration/3600, (duration%3600)/60, (duration%60)));
Alexandre Lision6d75d062013-09-13 14:18:34 -0400300 } else {
301 String tmp = "Conference with " + call.getParticipants().size() + " participants";
Alexandre Lision6d75d062013-09-13 14:18:34 -0400302 ((TextView) convertView.findViewById(R.id.call_title)).setText(tmp);
303 }
304 // ((TextView) convertView.findViewById(R.id.num_participants)).setText("" + call.getParticipants().size());
305 ((TextView) convertView.findViewById(R.id.call_status)).setText(call.getState());
306
307 convertView.setTag(call);
308 return convertView;
309 }
310
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400311 @Override
312 public void update(Observable observable, Object data) {
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400313 Log.i(TAG, "Updating views...");
314 notifyDataSetChanged();
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400315 }
316
Alexandre Lision6d75d062013-09-13 14:18:34 -0400317 }
318
alisionf76de3b2013-04-16 15:35:22 -0400319}