blob: 38a228467d5e7a9ecf500ae152285bb4fdeb96cf [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 Lision67c70b42014-01-16 13:57:15 -050043import org.sflphone.history.HistoryManager;
Alexandre Lision064e1e02013-10-01 16:18:42 -040044import org.sflphone.loaders.HistoryLoader;
45import org.sflphone.loaders.LoaderConstants;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050046import org.sflphone.history.HistoryEntry;
Alexandre Lision064e1e02013-10-01 16:18:42 -040047import org.sflphone.service.ISipService;
48
alisione2a38e12013-04-25 14:20:20 -040049import android.app.Activity;
Alexandre Lision4ab53972013-11-04 16:59:18 -050050import android.content.Context;
Alexandre Lision2e52d392013-11-06 15:14:31 -050051import android.content.Intent;
alisiond9e29442013-04-17 16:10:18 -040052import android.os.Bundle;
alisione2a38e12013-04-25 14:20:20 -040053import android.os.RemoteException;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050054import android.app.ListFragment;
55import android.app.LoaderManager.LoaderCallbacks;
alisione2a38e12013-04-25 14:20:20 -040056import android.util.Log;
alision50fa0722013-06-25 17:29:44 -040057import android.view.View.OnClickListener;
alision2ec64f92013-06-17 17:28:58 -040058import android.widget.AdapterView;
alision2ec64f92013-06-17 17:28:58 -040059import android.widget.AdapterView.OnItemClickListener;
alision50fa0722013-06-25 17:29:44 -040060import android.widget.BaseAdapter;
61import android.widget.Button;
62import android.widget.ImageButton;
Alexandre Lision4ab53972013-11-04 16:59:18 -050063import android.widget.ListAdapter;
alision50fa0722013-06-25 17:29:44 -040064import android.widget.TextView;
alisiond9e29442013-04-17 16:10:18 -040065
alision2ec64f92013-06-17 17:28:58 -040066public class HistoryFragment extends ListFragment implements LoaderCallbacks<ArrayList<HistoryEntry>> {
alisione2a38e12013-04-25 14:20:20 -040067
68 private static final String TAG = HistoryFragment.class.getSimpleName();
alision2ec64f92013-06-17 17:28:58 -040069
alisione2a38e12013-04-25 14:20:20 -040070 HistoryAdapter mAdapter;
71 private Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lision67c70b42014-01-16 13:57:15 -050072 HistoryManager mHistoryManager;
Alexandre Lision2e52d392013-11-06 15:14:31 -050073
alisione2a38e12013-04-25 14:20:20 -040074 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
Alexandre Lision2e52d392013-11-06 15:14:31 -050087 public static String ARGS = "Bundle.args";
88
alisione2a38e12013-04-25 14:20:20 -040089 public interface Callbacks {
alision907bde72013-06-20 14:40:37 -040090 public void onCallDialed(String to);
alisione2a38e12013-04-25 14:20:20 -040091
92 public ISipService getService();
93
94 }
95
96 @Override
97 public void onAttach(Activity activity) {
Alexandre Lision4a56d432013-07-19 11:37:53 -040098 Log.i(TAG, "Attaching HISTORY");
alisione2a38e12013-04-25 14:20:20 -040099 super.onAttach(activity);
alision2ec64f92013-06-17 17:28:58 -0400100
alisione2a38e12013-04-25 14:20:20 -0400101 if (!(activity instanceof Callbacks)) {
102 throw new IllegalStateException("Activity must implement fragment's callbacks.");
103 }
104
105 mCallbacks = (Callbacks) activity;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500106
alisione2a38e12013-04-25 14:20:20 -0400107 }
108
109 @Override
110 public void onDetach() {
111 super.onDetach();
112 mCallbacks = sDummyCallbacks;
113 }
114
115 @Override
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500116 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
117 super.onCreateOptionsMenu(menu, inflater);
118 inflater.inflate(R.menu.history, menu);
Alexandre Lision67c70b42014-01-16 13:57:15 -0500119 mHistoryManager = new HistoryManager(getActivity());
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500120 }
121
122 @Override
123 public boolean onOptionsItemSelected(MenuItem item) {
124 switch (item.getItemId()) {
125 case R.id.menu_clear_history:
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500126 // TODO clean Database!
Alexandre Lision67c70b42014-01-16 13:57:15 -0500127 mHistoryManager.clearDB();
128 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500129 return true;
130 default:
131 return super.onOptionsItemSelected(item);
132 }
133 }
134
135 @Override
alisione2a38e12013-04-25 14:20:20 -0400136 public void onCreate(Bundle savedInstanceState) {
137 super.onCreate(savedInstanceState);
Alexandre Lision72e37322013-11-04 17:14:11 -0500138 mAdapter = new HistoryAdapter(getActivity(), new ArrayList<HistoryEntry>());
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500139 setHasOptionsMenu(true);
alisione2a38e12013-04-25 14:20:20 -0400140 }
alisiond9e29442013-04-17 16:10:18 -0400141
142 @Override
143 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
alisione2a38e12013-04-25 14:20:20 -0400144 View inflatedView = inflater.inflate(R.layout.frag_history, parent, false);
alision2ec64f92013-06-17 17:28:58 -0400145
Alexandre Lision2e52d392013-11-06 15:14:31 -0500146 return inflatedView;
147 }
148
149 @Override
150 public void onActivityCreated(Bundle savedInstanceState) {
151
152 super.onActivityCreated(savedInstanceState);
153 getListView().setAdapter(mAdapter);
154
155 getListView().setOnItemClickListener(new OnItemClickListener() {
alision2ec64f92013-06-17 17:28:58 -0400156
157 @Override
158 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500159
160 Bundle b = new Bundle();
161 b.putParcelable("entry", mAdapter.getItem(pos));
162 Intent toStart = new Intent(getActivity(), DetailHistoryActivity.class).putExtra(HistoryFragment.ARGS, b);
163 startActivity(toStart);
164
alision2ec64f92013-06-17 17:28:58 -0400165 }
166 });
alisione2a38e12013-04-25 14:20:20 -0400167 }
168
169 @Override
170 public void onStart() {
171 super.onStart();
172 Log.w(TAG, "onStart");
Alexandre Lision945e4612014-01-15 17:40:31 -0500173 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400174 }
175
alision1005ba12013-06-19 13:52:44 -0400176 public void makeNewCall(int position) {
alision907bde72013-06-20 14:40:37 -0400177 mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber());
alision2ec64f92013-06-17 17:28:58 -0400178 }
alisione2a38e12013-04-25 14:20:20 -0400179
Alexandre Lision72e37322013-11-04 17:14:11 -0500180 public class HistoryAdapter extends BaseAdapter implements ListAdapter {
alision50fa0722013-06-25 17:29:44 -0400181
Alexandre Lision72e37322013-11-04 17:14:11 -0500182 Context mContext;
alision50fa0722013-06-25 17:29:44 -0400183 ArrayList<HistoryEntry> dataset;
184 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
185
Alexandre Lision72e37322013-11-04 17:14:11 -0500186 public HistoryAdapter(Context activity, ArrayList<HistoryEntry> history) {
alision50fa0722013-06-25 17:29:44 -0400187 mContext = activity;
188 dataset = history;
189 }
190
191 @Override
192 public View getView(final int pos, View convertView, ViewGroup arg2) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500193
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500194 HistoryView entryView;
alision50fa0722013-06-25 17:29:44 -0400195
Alexandre Lision40954dc2013-10-09 15:24:03 -0400196 if (convertView == null) {
alision50fa0722013-06-25 17:29:44 -0400197 // Get a new instance of the row layout view
Alexandre Lision72e37322013-11-04 17:14:11 -0500198 LayoutInflater inflater = LayoutInflater.from(mContext);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400199 convertView = inflater.inflate(R.layout.item_history, null);
alision50fa0722013-06-25 17:29:44 -0400200
201 // Hold the view objects in an object
202 // so they don't need to be re-fetched
203 entryView = new HistoryView();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500204 entryView.photo = (ImageButton) convertView.findViewById(R.id.photo);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400205 entryView.displayName = (TextView) convertView.findViewById(R.id.display_name);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400206 entryView.date = (TextView) convertView.findViewById(R.id.date_start);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400207 entryView.incoming = (TextView) convertView.findViewById(R.id.incomings);
208 entryView.outgoing = (TextView) convertView.findViewById(R.id.outgoings);
209 entryView.replay = (Button) convertView.findViewById(R.id.replay);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400210 convertView.setTag(entryView);
alision50fa0722013-06-25 17:29:44 -0400211 } else {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400212 entryView = (HistoryView) convertView.getTag();
alision50fa0722013-06-25 17:29:44 -0400213 }
214
215 // Transfer the stock data from the data object
216 // to the view objects
217
218 // SipCall call = (SipCall) mCallList.values().toArray()[position];
219 entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName());
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500220 infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, dataset.get(pos).getContact()));
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400221
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400222 entryView.incoming.setText(getString(R.string.hist_in_calls, dataset.get(pos).getIncoming_sum()));
223 entryView.outgoing.setText(getString(R.string.hist_out_calls, dataset.get(pos).getOutgoing_sum()));
alision50fa0722013-06-25 17:29:44 -0400224
Alexandre Lision945e4612014-01-15 17:40:31 -0500225 /*if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
alision50fa0722013-06-25 17:29:44 -0400226 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);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500236 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
alision50fa0722013-06-25 17:29:44 -0400237 } else {
238 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
239 v.setTag(R.id.replay, true);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500240 ((Button) v).setText(getString(R.string.hist_replay_button));
alision50fa0722013-06-25 17:29:44 -0400241 }
242 } catch (RemoteException e) {
243 // TODO Auto-generated catch block
244 e.printStackTrace();
245 }
246 }
247 });
Alexandre Lision945e4612014-01-15 17:40:31 -0500248 }*/
alision50fa0722013-06-25 17:29:44 -0400249
Alexandre Lision945e4612014-01-15 17:40:31 -0500250 /*entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate());*/
Alexandre Lision2e52d392013-11-06 15:14:31 -0500251 entryView.photo.setOnClickListener(new OnClickListener() {
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400252
253 @Override
254 public void onClick(View v) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500255 makeNewCall(pos);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400256
257 }
258 });
alision50fa0722013-06-25 17:29:44 -0400259
Alexandre Lision40954dc2013-10-09 15:24:03 -0400260 return convertView;
alision50fa0722013-06-25 17:29:44 -0400261
262 }
263
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500264 /**
265 * ******************
alision50fa0722013-06-25 17:29:44 -0400266 * ViewHolder Pattern
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500267 * *******************
268 */
alision50fa0722013-06-25 17:29:44 -0400269 public class HistoryView {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500270 public ImageButton photo;
alision50fa0722013-06-25 17:29:44 -0400271 protected TextView displayName;
272 protected TextView date;
alision50fa0722013-06-25 17:29:44 -0400273 private Button replay;
alision50fa0722013-06-25 17:29:44 -0400274 private TextView outgoing;
275 private TextView incoming;
276 }
277
278 @Override
279 public int getCount() {
280
281 return dataset.size();
282 }
283
284 @Override
285 public HistoryEntry getItem(int pos) {
286 return dataset.get(pos);
287 }
288
289 @Override
290 public long getItemId(int arg0) {
291 return 0;
292 }
293
294 public void clear() {
295 dataset.clear();
296
297 }
298
Alexandre Lision72e37322013-11-04 17:14:11 -0500299 public void addAll(ArrayList<HistoryEntry> history) {
300 dataset.addAll(history);
alision50fa0722013-06-25 17:29:44 -0400301 }
302
303 }
304
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500305 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500306 public AsyncTaskLoader<ArrayList<HistoryEntry>> onCreateLoader(int arg0, Bundle arg1) {
307 HistoryLoader loader = new HistoryLoader(getActivity());
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500308 loader.forceLoad();
309 return loader;
310 }
311
312 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500313 public void onLoadFinished(Loader<ArrayList<HistoryEntry>> loader, ArrayList<HistoryEntry> data) {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500314 mAdapter.clear();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500315 mAdapter.addAll(data);
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500316 mAdapter.notifyDataSetChanged();
317 }
318
319 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500320 public void onLoaderReset(Loader<ArrayList<HistoryEntry>> loader) {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500321
322 }
323
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500324
alisiond9e29442013-04-17 16:10:18 -0400325}