blob: eff94797ff7b183769e33f363cbcb7f0cf0a15a2 [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;
alision50fa0722013-06-25 17:29:44 -040034import java.util.concurrent.ExecutorService;
35import java.util.concurrent.Executors;
alisione2a38e12013-04-25 14:20:20 -040036
Alexandre Lision064e1e02013-10-01 16:18:42 -040037import org.sflphone.R;
38import org.sflphone.adapters.ContactPictureTask;
Alexandre Lision2e52d392013-11-06 15:14:31 -050039import org.sflphone.client.DetailHistoryActivity;
Alexandre Lision064e1e02013-10-01 16:18:42 -040040import 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;
Alexandre Lision4ab53972013-11-04 16:59:18 -050048import android.content.Context;
Alexandre Lision2e52d392013-11-06 15:14:31 -050049import android.content.Intent;
alision2ec64f92013-06-17 17:28:58 -040050import android.content.Loader;
alisiond9e29442013-04-17 16:10:18 -040051import android.os.Bundle;
alisione2a38e12013-04-25 14:20:20 -040052import android.os.RemoteException;
53import android.util.Log;
alisiond9e29442013-04-17 16:10:18 -040054import android.view.LayoutInflater;
55import android.view.View;
alision50fa0722013-06-25 17:29:44 -040056import android.view.View.OnClickListener;
alisiond9e29442013-04-17 16:10:18 -040057import android.view.ViewGroup;
alision2ec64f92013-06-17 17:28:58 -040058import android.widget.AdapterView;
alision2ec64f92013-06-17 17:28:58 -040059import android.widget.AdapterView.OnItemClickListener;
Alexandre Lision2e52d392013-11-06 15:14:31 -050060import android.widget.AdapterView.OnItemLongClickListener;
alision50fa0722013-06-25 17:29:44 -040061import android.widget.BaseAdapter;
62import android.widget.Button;
63import android.widget.ImageButton;
64import android.widget.ImageView;
Alexandre Lision4ab53972013-11-04 16:59:18 -050065import android.widget.ListAdapter;
alision50fa0722013-06-25 17:29:44 -040066import android.widget.TextView;
Alexandre Lision2e52d392013-11-06 15:14:31 -050067import android.widget.Toast;
alisiond9e29442013-04-17 16:10:18 -040068
alision2ec64f92013-06-17 17:28:58 -040069public class HistoryFragment extends ListFragment implements LoaderCallbacks<ArrayList<HistoryEntry>> {
alisione2a38e12013-04-25 14:20:20 -040070
71 private static final String TAG = HistoryFragment.class.getSimpleName();
alision2ec64f92013-06-17 17:28:58 -040072
alisione2a38e12013-04-25 14:20:20 -040073 HistoryAdapter mAdapter;
74 private Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lision2e52d392013-11-06 15:14:31 -050075
alisione2a38e12013-04-25 14:20:20 -040076 private static Callbacks sDummyCallbacks = new Callbacks() {
77 @Override
alision907bde72013-06-20 14:40:37 -040078 public void onCallDialed(String to) {
alisione2a38e12013-04-25 14:20:20 -040079 }
80
81 @Override
82 public ISipService getService() {
83 Log.i(TAG, "Dummy");
84 return null;
85 }
86
87 };
88
Alexandre Lision2e52d392013-11-06 15:14:31 -050089 public static String ARGS = "Bundle.args";
90
alisione2a38e12013-04-25 14:20:20 -040091 public interface Callbacks {
alision907bde72013-06-20 14:40:37 -040092 public void onCallDialed(String to);
alisione2a38e12013-04-25 14:20:20 -040093
94 public ISipService getService();
95
96 }
97
98 @Override
99 public void onAttach(Activity activity) {
Alexandre Lision4a56d432013-07-19 11:37:53 -0400100 Log.i(TAG, "Attaching HISTORY");
alisione2a38e12013-04-25 14:20:20 -0400101 super.onAttach(activity);
alision2ec64f92013-06-17 17:28:58 -0400102
alisione2a38e12013-04-25 14:20:20 -0400103 if (!(activity instanceof Callbacks)) {
104 throw new IllegalStateException("Activity must implement fragment's callbacks.");
105 }
106
107 mCallbacks = (Callbacks) activity;
alisionb1763882013-06-18 17:30:51 -0400108 getLoaderManager().initLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400109 }
110
111 @Override
112 public void onDetach() {
113 super.onDetach();
114 mCallbacks = sDummyCallbacks;
115 }
116
117 @Override
118 public void onCreate(Bundle savedInstanceState) {
119 super.onCreate(savedInstanceState);
Alexandre Lision72e37322013-11-04 17:14:11 -0500120 mAdapter = new HistoryAdapter(getActivity(), new ArrayList<HistoryEntry>());
alisione2a38e12013-04-25 14:20:20 -0400121 }
alisiond9e29442013-04-17 16:10:18 -0400122
123 @Override
124 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
alisione2a38e12013-04-25 14:20:20 -0400125 View inflatedView = inflater.inflate(R.layout.frag_history, parent, false);
alision2ec64f92013-06-17 17:28:58 -0400126
Alexandre Lision2e52d392013-11-06 15:14:31 -0500127 return inflatedView;
128 }
129
130 @Override
131 public void onActivityCreated(Bundle savedInstanceState) {
132
133 super.onActivityCreated(savedInstanceState);
134 getListView().setAdapter(mAdapter);
135
136 getListView().setOnItemClickListener(new OnItemClickListener() {
alision2ec64f92013-06-17 17:28:58 -0400137
138 @Override
139 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500140
141 Bundle b = new Bundle();
142 b.putParcelable("entry", mAdapter.getItem(pos));
143 Intent toStart = new Intent(getActivity(), DetailHistoryActivity.class).putExtra(HistoryFragment.ARGS, b);
144 startActivity(toStart);
145
alision2ec64f92013-06-17 17:28:58 -0400146 }
147 });
alisione2a38e12013-04-25 14:20:20 -0400148 }
149
150 @Override
151 public void onStart() {
152 super.onStart();
153 Log.w(TAG, "onStart");
Alexandre Lision4a56d432013-07-19 11:37:53 -0400154 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400155 }
156
alision1005ba12013-06-19 13:52:44 -0400157 public void makeNewCall(int position) {
alision907bde72013-06-20 14:40:37 -0400158 mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber());
alision2ec64f92013-06-17 17:28:58 -0400159 }
alisione2a38e12013-04-25 14:20:20 -0400160
alision2ec64f92013-06-17 17:28:58 -0400161 @Override
162 public Loader<ArrayList<HistoryEntry>> onCreateLoader(int id, Bundle args) {
alision1005ba12013-06-19 13:52:44 -0400163
alision2ec64f92013-06-17 17:28:58 -0400164 HistoryLoader loader = new HistoryLoader(getActivity(), mCallbacks.getService());
165 loader.forceLoad();
166 return loader;
alision50fa0722013-06-25 17:29:44 -0400167
alision2ec64f92013-06-17 17:28:58 -0400168 }
169
170 @Override
171 public void onLoadFinished(Loader<ArrayList<HistoryEntry>> arg0, ArrayList<HistoryEntry> history) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500172 mAdapter.clear();
173 mAdapter.addAll(history);
alision2ec64f92013-06-17 17:28:58 -0400174 mAdapter.notifyDataSetChanged();
175
176 }
177
178 @Override
179 public void onLoaderReset(Loader<ArrayList<HistoryEntry>> arg0) {
180 // TODO Auto-generated method stub
alisione2a38e12013-04-25 14:20:20 -0400181
alisiond9e29442013-04-17 16:10:18 -0400182 }
alision1005ba12013-06-19 13:52:44 -0400183
Alexandre Lision72e37322013-11-04 17:14:11 -0500184 public class HistoryAdapter extends BaseAdapter implements ListAdapter {
alision50fa0722013-06-25 17:29:44 -0400185
Alexandre Lision72e37322013-11-04 17:14:11 -0500186 Context mContext;
alision50fa0722013-06-25 17:29:44 -0400187 ArrayList<HistoryEntry> dataset;
188 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
189
Alexandre Lision72e37322013-11-04 17:14:11 -0500190 public HistoryAdapter(Context activity, ArrayList<HistoryEntry> history) {
alision50fa0722013-06-25 17:29:44 -0400191 mContext = activity;
192 dataset = history;
193 }
194
195 @Override
196 public View getView(final int pos, View convertView, ViewGroup arg2) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500197
alision50fa0722013-06-25 17:29:44 -0400198 HistoryView entryView = null;
199
Alexandre Lision40954dc2013-10-09 15:24:03 -0400200 if (convertView == null) {
alision50fa0722013-06-25 17:29:44 -0400201 // Get a new instance of the row layout view
Alexandre Lision72e37322013-11-04 17:14:11 -0500202 LayoutInflater inflater = LayoutInflater.from(mContext);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400203 convertView = inflater.inflate(R.layout.item_history, null);
alision50fa0722013-06-25 17:29:44 -0400204
205 // Hold the view objects in an object
206 // so they don't need to be re-fetched
207 entryView = new HistoryView();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500208 entryView.photo = (ImageButton) convertView.findViewById(R.id.photo);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400209 entryView.displayName = (TextView) convertView.findViewById(R.id.display_name);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400210 entryView.date = (TextView) convertView.findViewById(R.id.date_start);
211 entryView.missed = (TextView) convertView.findViewById(R.id.missed);
212 entryView.incoming = (TextView) convertView.findViewById(R.id.incomings);
213 entryView.outgoing = (TextView) convertView.findViewById(R.id.outgoings);
214 entryView.replay = (Button) convertView.findViewById(R.id.replay);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400215 convertView.setTag(entryView);
alision50fa0722013-06-25 17:29:44 -0400216 } else {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400217 entryView = (HistoryView) convertView.getTag();
alision50fa0722013-06-25 17:29:44 -0400218 }
219
220 // Transfer the stock data from the data object
221 // to the view objects
222
223 // SipCall call = (SipCall) mCallList.values().toArray()[position];
224 entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName());
225
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500226 infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, dataset.get(pos).getContact()));
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400227
228 entryView.missed.setText(getString(R.string.hist_missed_calls, dataset.get(pos).getMissed_sum()));
229 entryView.incoming.setText(getString(R.string.hist_in_calls, dataset.get(pos).getIncoming_sum()));
230 entryView.outgoing.setText(getString(R.string.hist_out_calls, dataset.get(pos).getOutgoing_sum()));
alision50fa0722013-06-25 17:29:44 -0400231
232 if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
233 entryView.replay.setVisibility(View.VISIBLE);
234 entryView.replay.setTag(R.id.replay, true);
235 entryView.replay.setOnClickListener(new OnClickListener() {
236
237 @Override
238 public void onClick(View v) {
239 try {
240 if ((Boolean) v.getTag(R.id.replay)) {
241 mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
242 v.setTag(R.id.replay, false);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500243 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
alision50fa0722013-06-25 17:29:44 -0400244 } else {
245 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
246 v.setTag(R.id.replay, true);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500247 ((Button) v).setText(getString(R.string.hist_replay_button));
alision50fa0722013-06-25 17:29:44 -0400248 }
249 } catch (RemoteException e) {
250 // TODO Auto-generated catch block
251 e.printStackTrace();
252 }
253 }
254 });
255 }
256
Alexandre Lision2e52d392013-11-06 15:14:31 -0500257 entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate());
258 entryView.photo.setOnClickListener(new OnClickListener() {
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400259
260 @Override
261 public void onClick(View v) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500262 makeNewCall(pos);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400263
264 }
265 });
alision50fa0722013-06-25 17:29:44 -0400266
Alexandre Lision40954dc2013-10-09 15:24:03 -0400267 return convertView;
alision50fa0722013-06-25 17:29:44 -0400268
269 }
270
271 /*********************
272 * ViewHolder Pattern
273 *********************/
274 public class HistoryView {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500275 public ImageButton photo;
alision50fa0722013-06-25 17:29:44 -0400276 protected TextView displayName;
277 protected TextView date;
alision50fa0722013-06-25 17:29:44 -0400278 private Button replay;
279 private TextView missed;
280 private TextView outgoing;
281 private TextView incoming;
282 }
283
284 @Override
285 public int getCount() {
286
287 return dataset.size();
288 }
289
290 @Override
291 public HistoryEntry getItem(int pos) {
292 return dataset.get(pos);
293 }
294
295 @Override
296 public long getItemId(int arg0) {
297 return 0;
298 }
299
300 public void clear() {
301 dataset.clear();
302
303 }
304
Alexandre Lision72e37322013-11-04 17:14:11 -0500305 public void addAll(ArrayList<HistoryEntry> history) {
306 dataset.addAll(history);
alision50fa0722013-06-25 17:29:44 -0400307 }
308
309 }
310
alisiond9e29442013-04-17 16:10:18 -0400311}