blob: 0ab1712e8be42f88ed61b6597c877f17ca22db94 [file] [log] [blame]
alision2ec64f92013-06-17 17:28:58 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
alision2ec64f92013-06-17 17:28:58 -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;
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 Lisionb4e60612014-01-14 17:47:23 -050037import android.content.AsyncTaskLoader;
38import android.content.Loader;
Alexandre Lision6158d1a2014-01-07 17:09:18 -050039import android.view.*;
Alexandre Lision064e1e02013-10-01 16:18:42 -040040import org.sflphone.R;
41import org.sflphone.adapters.ContactPictureTask;
Alexandre Lision2e52d392013-11-06 15:14:31 -050042import org.sflphone.client.DetailHistoryActivity;
Alexandre Lision064e1e02013-10-01 16:18:42 -040043import org.sflphone.loaders.HistoryLoader;
44import org.sflphone.loaders.LoaderConstants;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050045import org.sflphone.history.HistoryEntry;
Alexandre Lision064e1e02013-10-01 16:18:42 -040046import org.sflphone.service.ISipService;
47
alisione2a38e12013-04-25 14:20:20 -040048import android.app.Activity;
Alexandre Lision4ab53972013-11-04 16:59:18 -050049import android.content.Context;
Alexandre Lision2e52d392013-11-06 15:14:31 -050050import android.content.Intent;
alisiond9e29442013-04-17 16:10:18 -040051import android.os.Bundle;
alisione2a38e12013-04-25 14:20:20 -040052import android.os.RemoteException;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050053import android.app.ListFragment;
54import android.app.LoaderManager.LoaderCallbacks;
alisione2a38e12013-04-25 14:20:20 -040055import android.util.Log;
alision50fa0722013-06-25 17:29:44 -040056import android.view.View.OnClickListener;
alision2ec64f92013-06-17 17:28:58 -040057import android.widget.AdapterView;
alision2ec64f92013-06-17 17:28:58 -040058import android.widget.AdapterView.OnItemClickListener;
alision50fa0722013-06-25 17:29:44 -040059import android.widget.BaseAdapter;
60import android.widget.Button;
61import android.widget.ImageButton;
Alexandre Lision4ab53972013-11-04 16:59:18 -050062import android.widget.ListAdapter;
alision50fa0722013-06-25 17:29:44 -040063import 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;
Alexandre Lision2e52d392013-11-06 15:14:31 -050071
alisione2a38e12013-04-25 14:20:20 -040072 private static Callbacks sDummyCallbacks = new Callbacks() {
73 @Override
alision907bde72013-06-20 14:40:37 -040074 public void onCallDialed(String to) {
alisione2a38e12013-04-25 14:20:20 -040075 }
76
77 @Override
78 public ISipService getService() {
79 Log.i(TAG, "Dummy");
80 return null;
81 }
82
83 };
84
Alexandre Lision2e52d392013-11-06 15:14:31 -050085 public static String ARGS = "Bundle.args";
86
alisione2a38e12013-04-25 14:20:20 -040087 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;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500104
alisione2a38e12013-04-25 14:20:20 -0400105 }
106
107 @Override
108 public void onDetach() {
109 super.onDetach();
110 mCallbacks = sDummyCallbacks;
111 }
112
113 @Override
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500114 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
115 super.onCreateOptionsMenu(menu, inflater);
116 inflater.inflate(R.menu.history, menu);
117 }
118
119 @Override
120 public boolean onOptionsItemSelected(MenuItem item) {
121 switch (item.getItemId()) {
122 case R.id.menu_clear_history:
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500123 // TODO clean Database!
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500124 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500125 return true;
126 default:
127 return super.onOptionsItemSelected(item);
128 }
129 }
130
131 @Override
alisione2a38e12013-04-25 14:20:20 -0400132 public void onCreate(Bundle savedInstanceState) {
133 super.onCreate(savedInstanceState);
Alexandre Lision72e37322013-11-04 17:14:11 -0500134 mAdapter = new HistoryAdapter(getActivity(), new ArrayList<HistoryEntry>());
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500135 setHasOptionsMenu(true);
alisione2a38e12013-04-25 14:20:20 -0400136 }
alisiond9e29442013-04-17 16:10:18 -0400137
138 @Override
139 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
alisione2a38e12013-04-25 14:20:20 -0400140 View inflatedView = inflater.inflate(R.layout.frag_history, parent, false);
alision2ec64f92013-06-17 17:28:58 -0400141
Alexandre Lision2e52d392013-11-06 15:14:31 -0500142 return inflatedView;
143 }
144
145 @Override
146 public void onActivityCreated(Bundle savedInstanceState) {
147
148 super.onActivityCreated(savedInstanceState);
149 getListView().setAdapter(mAdapter);
150
151 getListView().setOnItemClickListener(new OnItemClickListener() {
alision2ec64f92013-06-17 17:28:58 -0400152
153 @Override
154 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500155
156 Bundle b = new Bundle();
157 b.putParcelable("entry", mAdapter.getItem(pos));
158 Intent toStart = new Intent(getActivity(), DetailHistoryActivity.class).putExtra(HistoryFragment.ARGS, b);
159 startActivity(toStart);
160
alision2ec64f92013-06-17 17:28:58 -0400161 }
162 });
alisione2a38e12013-04-25 14:20:20 -0400163 }
164
165 @Override
166 public void onStart() {
167 super.onStart();
168 Log.w(TAG, "onStart");
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500169 //getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400170 }
171
alision1005ba12013-06-19 13:52:44 -0400172 public void makeNewCall(int position) {
alision907bde72013-06-20 14:40:37 -0400173 mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber());
alision2ec64f92013-06-17 17:28:58 -0400174 }
alisione2a38e12013-04-25 14:20:20 -0400175
Alexandre Lision72e37322013-11-04 17:14:11 -0500176 public class HistoryAdapter extends BaseAdapter implements ListAdapter {
alision50fa0722013-06-25 17:29:44 -0400177
Alexandre Lision72e37322013-11-04 17:14:11 -0500178 Context mContext;
alision50fa0722013-06-25 17:29:44 -0400179 ArrayList<HistoryEntry> dataset;
180 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
181
Alexandre Lision72e37322013-11-04 17:14:11 -0500182 public HistoryAdapter(Context activity, ArrayList<HistoryEntry> history) {
alision50fa0722013-06-25 17:29:44 -0400183 mContext = activity;
184 dataset = history;
185 }
186
187 @Override
188 public View getView(final int pos, View convertView, ViewGroup arg2) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500189
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500190 HistoryView entryView;
alision50fa0722013-06-25 17:29:44 -0400191
Alexandre Lision40954dc2013-10-09 15:24:03 -0400192 if (convertView == null) {
alision50fa0722013-06-25 17:29:44 -0400193 // Get a new instance of the row layout view
Alexandre Lision72e37322013-11-04 17:14:11 -0500194 LayoutInflater inflater = LayoutInflater.from(mContext);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400195 convertView = inflater.inflate(R.layout.item_history, null);
alision50fa0722013-06-25 17:29:44 -0400196
197 // Hold the view objects in an object
198 // so they don't need to be re-fetched
199 entryView = new HistoryView();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500200 entryView.photo = (ImageButton) convertView.findViewById(R.id.photo);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400201 entryView.displayName = (TextView) convertView.findViewById(R.id.display_name);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400202 entryView.date = (TextView) convertView.findViewById(R.id.date_start);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400203 entryView.incoming = (TextView) convertView.findViewById(R.id.incomings);
204 entryView.outgoing = (TextView) convertView.findViewById(R.id.outgoings);
205 entryView.replay = (Button) convertView.findViewById(R.id.replay);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400206 convertView.setTag(entryView);
alision50fa0722013-06-25 17:29:44 -0400207 } else {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400208 entryView = (HistoryView) convertView.getTag();
alision50fa0722013-06-25 17:29:44 -0400209 }
210
211 // Transfer the stock data from the data object
212 // to the view objects
213
214 // SipCall call = (SipCall) mCallList.values().toArray()[position];
215 entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName());
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500216 infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, dataset.get(pos).getContact()));
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400217
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400218 entryView.incoming.setText(getString(R.string.hist_in_calls, dataset.get(pos).getIncoming_sum()));
219 entryView.outgoing.setText(getString(R.string.hist_out_calls, dataset.get(pos).getOutgoing_sum()));
alision50fa0722013-06-25 17:29:44 -0400220
221 if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
222 entryView.replay.setVisibility(View.VISIBLE);
223 entryView.replay.setTag(R.id.replay, true);
224 entryView.replay.setOnClickListener(new OnClickListener() {
225
226 @Override
227 public void onClick(View v) {
228 try {
229 if ((Boolean) v.getTag(R.id.replay)) {
230 mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
231 v.setTag(R.id.replay, false);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500232 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
alision50fa0722013-06-25 17:29:44 -0400233 } else {
234 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
235 v.setTag(R.id.replay, true);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500236 ((Button) v).setText(getString(R.string.hist_replay_button));
alision50fa0722013-06-25 17:29:44 -0400237 }
238 } catch (RemoteException e) {
239 // TODO Auto-generated catch block
240 e.printStackTrace();
241 }
242 }
243 });
244 }
245
Alexandre Lision2e52d392013-11-06 15:14:31 -0500246 entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate());
247 entryView.photo.setOnClickListener(new OnClickListener() {
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400248
249 @Override
250 public void onClick(View v) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500251 makeNewCall(pos);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400252
253 }
254 });
alision50fa0722013-06-25 17:29:44 -0400255
Alexandre Lision40954dc2013-10-09 15:24:03 -0400256 return convertView;
alision50fa0722013-06-25 17:29:44 -0400257
258 }
259
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500260 /**
261 * ******************
alision50fa0722013-06-25 17:29:44 -0400262 * ViewHolder Pattern
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500263 * *******************
264 */
alision50fa0722013-06-25 17:29:44 -0400265 public class HistoryView {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500266 public ImageButton photo;
alision50fa0722013-06-25 17:29:44 -0400267 protected TextView displayName;
268 protected TextView date;
alision50fa0722013-06-25 17:29:44 -0400269 private Button replay;
alision50fa0722013-06-25 17:29:44 -0400270 private TextView outgoing;
271 private TextView incoming;
272 }
273
274 @Override
275 public int getCount() {
276
277 return dataset.size();
278 }
279
280 @Override
281 public HistoryEntry getItem(int pos) {
282 return dataset.get(pos);
283 }
284
285 @Override
286 public long getItemId(int arg0) {
287 return 0;
288 }
289
290 public void clear() {
291 dataset.clear();
292
293 }
294
Alexandre Lision72e37322013-11-04 17:14:11 -0500295 public void addAll(ArrayList<HistoryEntry> history) {
296 dataset.addAll(history);
alision50fa0722013-06-25 17:29:44 -0400297 }
298
299 }
300
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500301 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500302 public AsyncTaskLoader<ArrayList<HistoryEntry>> onCreateLoader(int arg0, Bundle arg1) {
303 HistoryLoader loader = new HistoryLoader(getActivity());
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500304 loader.forceLoad();
305 return loader;
306 }
307
308 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500309 public void onLoadFinished(Loader<ArrayList<HistoryEntry>> loader, ArrayList<HistoryEntry> data) {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500310 mAdapter.clear();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500311 mAdapter.addAll(data);
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500312 mAdapter.notifyDataSetChanged();
313 }
314
315 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500316 public void onLoaderReset(Loader<ArrayList<HistoryEntry>> loader) {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500317
318 }
319
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500320
alisiond9e29442013-04-17 16:10:18 -0400321}