blob: 480b122999e37d3a2b812b238e60679c60a7a847 [file] [log] [blame]
alision2ec64f92013-06-17 17:28:58 -04001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
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 */
alisiond9e29442013-04-17 16:10:18 -040031package com.savoirfairelinux.sflphone.fragments;
32
alisione2a38e12013-04-25 14:20:20 -040033import java.util.ArrayList;
34import java.util.HashMap;
alision50fa0722013-06-25 17:29:44 -040035import java.util.concurrent.ExecutorService;
36import java.util.concurrent.Executors;
alisione2a38e12013-04-25 14:20:20 -040037
38import android.app.Activity;
39import android.app.ListFragment;
alision2ec64f92013-06-17 17:28:58 -040040import android.app.LoaderManager.LoaderCallbacks;
41import android.content.Loader;
alisiond9e29442013-04-17 16:10:18 -040042import android.os.Bundle;
alisione2a38e12013-04-25 14:20:20 -040043import android.os.RemoteException;
44import android.util.Log;
alisiond9e29442013-04-17 16:10:18 -040045import android.view.LayoutInflater;
46import android.view.View;
alision50fa0722013-06-25 17:29:44 -040047import android.view.View.OnClickListener;
alisiond9e29442013-04-17 16:10:18 -040048import android.view.ViewGroup;
alision2ec64f92013-06-17 17:28:58 -040049import android.widget.AdapterView;
alision2ec64f92013-06-17 17:28:58 -040050import android.widget.AdapterView.OnItemClickListener;
alision50fa0722013-06-25 17:29:44 -040051import android.widget.BaseAdapter;
52import android.widget.Button;
53import android.widget.ImageButton;
54import android.widget.ImageView;
55import android.widget.ListView;
56import android.widget.TextView;
alisiond9e29442013-04-17 16:10:18 -040057
alisione2a38e12013-04-25 14:20:20 -040058import com.savoirfairelinux.sflphone.R;
Alexandre Lision6e8931e2013-09-19 16:49:34 -040059import com.savoirfairelinux.sflphone.adapters.ContactPictureTask;
alision2ec64f92013-06-17 17:28:58 -040060import com.savoirfairelinux.sflphone.loaders.HistoryLoader;
61import com.savoirfairelinux.sflphone.loaders.LoaderConstants;
alision2ec64f92013-06-17 17:28:58 -040062import com.savoirfairelinux.sflphone.model.HistoryEntry;
alisione2a38e12013-04-25 14:20:20 -040063import com.savoirfairelinux.sflphone.service.ISipService;
alisiond9e29442013-04-17 16:10:18 -040064
alision2ec64f92013-06-17 17:28:58 -040065public class HistoryFragment extends ListFragment implements LoaderCallbacks<ArrayList<HistoryEntry>> {
alisione2a38e12013-04-25 14:20:20 -040066
67 private static final String TAG = HistoryFragment.class.getSimpleName();
alision2ec64f92013-06-17 17:28:58 -040068
alisione2a38e12013-04-25 14:20:20 -040069 HistoryAdapter mAdapter;
70 private Callbacks mCallbacks = sDummyCallbacks;
71 /**
72 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
73 */
74 private static Callbacks sDummyCallbacks = new Callbacks() {
75 @Override
alision907bde72013-06-20 14:40:37 -040076 public void onCallDialed(String to) {
alisione2a38e12013-04-25 14:20:20 -040077 }
78
79 @Override
80 public ISipService getService() {
81 Log.i(TAG, "Dummy");
82 return null;
83 }
84
85 };
86
87 public interface Callbacks {
alision907bde72013-06-20 14:40:37 -040088 public void onCallDialed(String to);
alisione2a38e12013-04-25 14:20:20 -040089
90 public ISipService getService();
91
92 }
93
94 @Override
95 public void onAttach(Activity activity) {
Alexandre Lision4a56d432013-07-19 11:37:53 -040096 Log.i(TAG, "Attaching HISTORY");
alisione2a38e12013-04-25 14:20:20 -040097 super.onAttach(activity);
alision2ec64f92013-06-17 17:28:58 -040098
alisione2a38e12013-04-25 14:20:20 -040099 if (!(activity instanceof Callbacks)) {
100 throw new IllegalStateException("Activity must implement fragment's callbacks.");
101 }
102
103 mCallbacks = (Callbacks) activity;
alisionb1763882013-06-18 17:30:51 -0400104 getLoaderManager().initLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400105 }
106
107 @Override
108 public void onDetach() {
109 super.onDetach();
110 mCallbacks = sDummyCallbacks;
111 }
112
113 @Override
114 public void onCreate(Bundle savedInstanceState) {
115 super.onCreate(savedInstanceState);
Alexandre Lision4a56d432013-07-19 11:37:53 -0400116
alisione2a38e12013-04-25 14:20:20 -0400117 }
alisiond9e29442013-04-17 16:10:18 -0400118
119 @Override
120 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
alisione2a38e12013-04-25 14:20:20 -0400121 View inflatedView = inflater.inflate(R.layout.frag_history, parent, false);
alision2ec64f92013-06-17 17:28:58 -0400122
alision1005ba12013-06-19 13:52:44 -0400123 ((ListView) inflatedView.findViewById(android.R.id.list)).setOnItemClickListener(new OnItemClickListener() {
alision2ec64f92013-06-17 17:28:58 -0400124
125 @Override
126 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
127 mAdapter.getItem(pos);
128 }
129 });
alisione2a38e12013-04-25 14:20:20 -0400130 return inflatedView;
131 }
132
133 @Override
134 public void onStart() {
135 super.onStart();
136 Log.w(TAG, "onStart");
Alexandre Lision4a56d432013-07-19 11:37:53 -0400137 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400138 }
139
alision1005ba12013-06-19 13:52:44 -0400140 public void makeNewCall(int position) {
alision907bde72013-06-20 14:40:37 -0400141 mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber());
alision2ec64f92013-06-17 17:28:58 -0400142 }
alisione2a38e12013-04-25 14:20:20 -0400143
alision2ec64f92013-06-17 17:28:58 -0400144 @Override
145 public Loader<ArrayList<HistoryEntry>> onCreateLoader(int id, Bundle args) {
alision1005ba12013-06-19 13:52:44 -0400146
alision2ec64f92013-06-17 17:28:58 -0400147 HistoryLoader loader = new HistoryLoader(getActivity(), mCallbacks.getService());
148 loader.forceLoad();
149 return loader;
alision50fa0722013-06-25 17:29:44 -0400150
alision2ec64f92013-06-17 17:28:58 -0400151 }
152
153 @Override
154 public void onLoadFinished(Loader<ArrayList<HistoryEntry>> arg0, ArrayList<HistoryEntry> history) {
155 mAdapter = new HistoryAdapter(this, history);
156 getListView().setAdapter(mAdapter);
157 mAdapter.notifyDataSetChanged();
158
159 }
160
161 @Override
162 public void onLoaderReset(Loader<ArrayList<HistoryEntry>> arg0) {
163 // TODO Auto-generated method stub
alisione2a38e12013-04-25 14:20:20 -0400164
alisiond9e29442013-04-17 16:10:18 -0400165 }
alision1005ba12013-06-19 13:52:44 -0400166
alision50fa0722013-06-25 17:29:44 -0400167 public class HistoryAdapter extends BaseAdapter {
168
169 HistoryFragment mContext;
170 ArrayList<HistoryEntry> dataset;
171 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
172
173 public HistoryAdapter(HistoryFragment activity, ArrayList<HistoryEntry> history) {
174 mContext = activity;
175 dataset = history;
176 }
177
178 @Override
179 public View getView(final int pos, View convertView, ViewGroup arg2) {
180 View rowView = convertView;
181 HistoryView entryView = null;
182
183 if (rowView == null) {
184 // Get a new instance of the row layout view
185 LayoutInflater inflater = LayoutInflater.from(mContext.getActivity());
186 rowView = inflater.inflate(R.layout.item_history, null);
187
188 // Hold the view objects in an object
189 // so they don't need to be re-fetched
190 entryView = new HistoryView();
191 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
192 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
193 entryView.duration = (TextView) rowView.findViewById(R.id.duration);
194 entryView.date = (TextView) rowView.findViewById(R.id.date_start);
195 entryView.missed = (TextView) rowView.findViewById(R.id.missed);
196 entryView.incoming = (TextView) rowView.findViewById(R.id.incomings);
197 entryView.outgoing = (TextView) rowView.findViewById(R.id.outgoings);
198 entryView.replay = (Button) rowView.findViewById(R.id.replay);
199 entryView.call_button = (ImageButton) rowView.findViewById(R.id.action_call);
200 entryView.call_button.setOnClickListener(new OnClickListener() {
201
202 @Override
203 public void onClick(View v) {
204 mContext.makeNewCall(pos);
205
206 }
207 });
208 rowView.setTag(entryView);
209 } else {
210 entryView = (HistoryView) rowView.getTag();
211 }
212
213 // Transfer the stock data from the data object
214 // to the view objects
215
216 // SipCall call = (SipCall) mCallList.values().toArray()[position];
217 entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName());
218
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400219 infos_fetcher.execute(new ContactPictureTask(mContext.getActivity(), entryView.photo, dataset.get(pos).getContact().getId()));
alision50fa0722013-06-25 17:29:44 -0400220
221 entryView.missed.setText("Missed:" + dataset.get(pos).getMissed_sum());
222 entryView.incoming.setText("In:" + dataset.get(pos).getIncoming_sum());
223 entryView.outgoing.setText("Out:" + dataset.get(pos).getOutgoing_sum());
224
225 if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
226 entryView.replay.setVisibility(View.VISIBLE);
227 entryView.replay.setTag(R.id.replay, true);
228 entryView.replay.setOnClickListener(new OnClickListener() {
229
230 @Override
231 public void onClick(View v) {
232 try {
233 if ((Boolean) v.getTag(R.id.replay)) {
234 mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
235 v.setTag(R.id.replay, false);
236 ((Button)v).setText("Stop");
237 } else {
238 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
239 v.setTag(R.id.replay, true);
240 ((Button)v).setText("Replay");
241 }
242 } catch (RemoteException e) {
243 // TODO Auto-generated catch block
244 e.printStackTrace();
245 }
246 }
247 });
248 }
249
250 entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate("yyyy-MM-dd"));
251 entryView.duration.setText(dataset.get(pos).getTotalDuration());
252
253 return rowView;
254
255 }
256
257 /*********************
258 * ViewHolder Pattern
259 *********************/
260 public class HistoryView {
261 public ImageView photo;
262 protected TextView displayName;
263 protected TextView date;
264 public TextView duration;
265 private ImageButton call_button;
266 private Button replay;
267 private TextView missed;
268 private TextView outgoing;
269 private TextView incoming;
270 }
271
272 @Override
273 public int getCount() {
274
275 return dataset.size();
276 }
277
278 @Override
279 public HistoryEntry getItem(int pos) {
280 return dataset.get(pos);
281 }
282
283 @Override
284 public long getItemId(int arg0) {
285 return 0;
286 }
287
288 public void clear() {
289 dataset.clear();
290
291 }
292
293 public void addAll(ArrayList<HashMap<String, String>> history) {
294 // dataset.addAll(history);
295
296 }
297
298 }
299
alisiond9e29442013-04-17 16:10:18 -0400300}