blob: 22b26b60948f05fe16ab2a28060a92be5a7d4482 [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 */
Alexandre Lision064e1e02013-10-01 16:18:42 -040031package org.sflphone.fragments;
alisiond9e29442013-04-17 16:10:18 -040032
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
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.R;
39import org.sflphone.adapters.ContactPictureTask;
40import org.sflphone.loaders.HistoryLoader;
41import org.sflphone.loaders.LoaderConstants;
42import org.sflphone.model.HistoryEntry;
43import org.sflphone.service.ISipService;
44
alisione2a38e12013-04-25 14:20:20 -040045import android.app.Activity;
46import android.app.ListFragment;
alision2ec64f92013-06-17 17:28:58 -040047import android.app.LoaderManager.LoaderCallbacks;
48import android.content.Loader;
alisiond9e29442013-04-17 16:10:18 -040049import android.os.Bundle;
alisione2a38e12013-04-25 14:20:20 -040050import android.os.RemoteException;
51import android.util.Log;
alisiond9e29442013-04-17 16:10:18 -040052import android.view.LayoutInflater;
53import android.view.View;
alision50fa0722013-06-25 17:29:44 -040054import android.view.View.OnClickListener;
alisiond9e29442013-04-17 16:10:18 -040055import android.view.ViewGroup;
alision2ec64f92013-06-17 17:28:58 -040056import android.widget.AdapterView;
alision2ec64f92013-06-17 17:28:58 -040057import android.widget.AdapterView.OnItemClickListener;
alision50fa0722013-06-25 17:29:44 -040058import android.widget.BaseAdapter;
59import android.widget.Button;
60import android.widget.ImageButton;
61import android.widget.ImageView;
62import android.widget.ListView;
63import android.widget.TextView;
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);
alision50fa0722013-06-25 17:29:44 -0400200 rowView.setTag(entryView);
201 } else {
202 entryView = (HistoryView) rowView.getTag();
203 }
204
205 // Transfer the stock data from the data object
206 // to the view objects
207
208 // SipCall call = (SipCall) mCallList.values().toArray()[position];
209 entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName());
210
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400211 infos_fetcher.execute(new ContactPictureTask(mContext.getActivity(), entryView.photo, dataset.get(pos).getContact().getId()));
alision50fa0722013-06-25 17:29:44 -0400212
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400213
214 entryView.missed.setText(getString(R.string.hist_missed_calls, dataset.get(pos).getMissed_sum()));
215 entryView.incoming.setText(getString(R.string.hist_in_calls, dataset.get(pos).getIncoming_sum()));
216 entryView.outgoing.setText(getString(R.string.hist_out_calls, dataset.get(pos).getOutgoing_sum()));
alision50fa0722013-06-25 17:29:44 -0400217
218 if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
219 entryView.replay.setVisibility(View.VISIBLE);
220 entryView.replay.setTag(R.id.replay, true);
221 entryView.replay.setOnClickListener(new OnClickListener() {
222
223 @Override
224 public void onClick(View v) {
225 try {
226 if ((Boolean) v.getTag(R.id.replay)) {
227 mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
228 v.setTag(R.id.replay, false);
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400229 ((Button)v).setText(getString(R.string.hist_replay_button_stop));
alision50fa0722013-06-25 17:29:44 -0400230 } else {
231 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
232 v.setTag(R.id.replay, true);
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400233 ((Button)v).setText(getString(R.string.hist_replay_button));
alision50fa0722013-06-25 17:29:44 -0400234 }
235 } catch (RemoteException e) {
236 // TODO Auto-generated catch block
237 e.printStackTrace();
238 }
239 }
240 });
241 }
242
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400243 entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate("hh:mm dd/MM/yyyy"));
alision50fa0722013-06-25 17:29:44 -0400244 entryView.duration.setText(dataset.get(pos).getTotalDuration());
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400245 entryView.call_button.setOnClickListener(new OnClickListener() {
246
247 @Override
248 public void onClick(View v) {
249 mContext.makeNewCall(pos);
250
251 }
252 });
alision50fa0722013-06-25 17:29:44 -0400253
254 return rowView;
255
256 }
257
258 /*********************
259 * ViewHolder Pattern
260 *********************/
261 public class HistoryView {
262 public ImageView photo;
263 protected TextView displayName;
264 protected TextView date;
265 public TextView duration;
266 private ImageButton call_button;
267 private Button replay;
268 private TextView missed;
269 private TextView outgoing;
270 private TextView incoming;
271 }
272
273 @Override
274 public int getCount() {
275
276 return dataset.size();
277 }
278
279 @Override
280 public HistoryEntry getItem(int pos) {
281 return dataset.get(pos);
282 }
283
284 @Override
285 public long getItemId(int arg0) {
286 return 0;
287 }
288
289 public void clear() {
290 dataset.clear();
291
292 }
293
294 public void addAll(ArrayList<HashMap<String, String>> history) {
295 // dataset.addAll(history);
296
297 }
298
299 }
300
alisiond9e29442013-04-17 16:10:18 -0400301}