blob: 806ef5c165c01062161169a3d4966f9c28dad417 [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 Lisionbe54c342014-01-21 17:10:33 -050037import android.support.v4.app.ListFragment;
38import android.support.v4.app.LoaderManager;
39import android.support.v4.content.AsyncTaskLoader;
40import android.support.v4.content.Loader;
Alexandre Lision6158d1a2014-01-07 17:09:18 -050041import android.view.*;
Alexandre Lision064e1e02013-10-01 16:18:42 -040042import org.sflphone.R;
43import org.sflphone.adapters.ContactPictureTask;
Alexandre Lision2e52d392013-11-06 15:14:31 -050044import org.sflphone.client.DetailHistoryActivity;
Alexandre Lision67c70b42014-01-16 13:57:15 -050045import org.sflphone.history.HistoryManager;
Alexandre Lision064e1e02013-10-01 16:18:42 -040046import org.sflphone.loaders.HistoryLoader;
47import org.sflphone.loaders.LoaderConstants;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050048import org.sflphone.history.HistoryEntry;
Alexandre Lision064e1e02013-10-01 16:18:42 -040049import org.sflphone.service.ISipService;
50
alisione2a38e12013-04-25 14:20:20 -040051import android.app.Activity;
Alexandre Lision4ab53972013-11-04 16:59:18 -050052import android.content.Context;
Alexandre Lision2e52d392013-11-06 15:14:31 -050053import android.content.Intent;
alisiond9e29442013-04-17 16:10:18 -040054import android.os.Bundle;
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
Alexandre Lisionbe54c342014-01-21 17:10:33 -050065public class HistoryFragment extends ListFragment implements LoaderManager.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 Lision67c70b42014-01-16 13:57:15 -050071 HistoryManager mHistoryManager;
Alexandre Lision2e52d392013-11-06 15:14:31 -050072
alisione2a38e12013-04-25 14:20:20 -040073 private static Callbacks sDummyCallbacks = new Callbacks() {
74 @Override
Alexandre Lisione796a872014-01-17 17:08:13 -050075 public void onCallHistory(HistoryEntry to) {
alisione2a38e12013-04-25 14:20:20 -040076 }
77
78 @Override
79 public ISipService getService() {
80 Log.i(TAG, "Dummy");
81 return null;
82 }
83
84 };
85
Alexandre Lision2e52d392013-11-06 15:14:31 -050086 public static String ARGS = "Bundle.args";
87
alisione2a38e12013-04-25 14:20:20 -040088 public interface Callbacks {
Alexandre Lisione796a872014-01-17 17:08:13 -050089 public void onCallHistory(HistoryEntry to);
alisione2a38e12013-04-25 14:20:20 -040090
91 public ISipService getService();
92
93 }
94
95 @Override
96 public void onAttach(Activity activity) {
Alexandre Lision4a56d432013-07-19 11:37:53 -040097 Log.i(TAG, "Attaching HISTORY");
alisione2a38e12013-04-25 14:20:20 -040098 super.onAttach(activity);
alision2ec64f92013-06-17 17:28:58 -040099
alisione2a38e12013-04-25 14:20:20 -0400100 if (!(activity instanceof Callbacks)) {
101 throw new IllegalStateException("Activity must implement fragment's callbacks.");
102 }
103
104 mCallbacks = (Callbacks) activity;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500105
alisione2a38e12013-04-25 14:20:20 -0400106 }
107
108 @Override
109 public void onDetach() {
110 super.onDetach();
111 mCallbacks = sDummyCallbacks;
112 }
113
114 @Override
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500115 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
116 super.onCreateOptionsMenu(menu, inflater);
117 inflater.inflate(R.menu.history, menu);
Alexandre Lision67c70b42014-01-16 13:57:15 -0500118 mHistoryManager = new HistoryManager(getActivity());
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500119 }
120
121 @Override
122 public boolean onOptionsItemSelected(MenuItem item) {
123 switch (item.getItemId()) {
124 case R.id.menu_clear_history:
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500125 // TODO clean Database!
Alexandre Lision67c70b42014-01-16 13:57:15 -0500126 mHistoryManager.clearDB();
127 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500128 return true;
129 default:
130 return super.onOptionsItemSelected(item);
131 }
132 }
133
134 @Override
alisione2a38e12013-04-25 14:20:20 -0400135 public void onCreate(Bundle savedInstanceState) {
136 super.onCreate(savedInstanceState);
Alexandre Lision72e37322013-11-04 17:14:11 -0500137 mAdapter = new HistoryAdapter(getActivity(), new ArrayList<HistoryEntry>());
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500138 setHasOptionsMenu(true);
alisione2a38e12013-04-25 14:20:20 -0400139 }
alisiond9e29442013-04-17 16:10:18 -0400140
141 @Override
142 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
alisione2a38e12013-04-25 14:20:20 -0400143 View inflatedView = inflater.inflate(R.layout.frag_history, parent, false);
alision2ec64f92013-06-17 17:28:58 -0400144
Alexandre Lision2e52d392013-11-06 15:14:31 -0500145 return inflatedView;
146 }
147
148 @Override
149 public void onActivityCreated(Bundle savedInstanceState) {
150
151 super.onActivityCreated(savedInstanceState);
152 getListView().setAdapter(mAdapter);
153
154 getListView().setOnItemClickListener(new OnItemClickListener() {
alision2ec64f92013-06-17 17:28:58 -0400155
156 @Override
157 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500158
159 Bundle b = new Bundle();
160 b.putParcelable("entry", mAdapter.getItem(pos));
161 Intent toStart = new Intent(getActivity(), DetailHistoryActivity.class).putExtra(HistoryFragment.ARGS, b);
162 startActivity(toStart);
163
alision2ec64f92013-06-17 17:28:58 -0400164 }
165 });
alisione2a38e12013-04-25 14:20:20 -0400166 }
167
168 @Override
169 public void onStart() {
170 super.onStart();
171 Log.w(TAG, "onStart");
Alexandre Lision945e4612014-01-15 17:40:31 -0500172 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400173 }
174
alision1005ba12013-06-19 13:52:44 -0400175 public void makeNewCall(int position) {
Alexandre Lisione796a872014-01-17 17:08:13 -0500176 mCallbacks.onCallHistory(mAdapter.getItem(position));
alision2ec64f92013-06-17 17:28:58 -0400177 }
alisione2a38e12013-04-25 14:20:20 -0400178
Alexandre Lision72e37322013-11-04 17:14:11 -0500179 public class HistoryAdapter extends BaseAdapter implements ListAdapter {
alision50fa0722013-06-25 17:29:44 -0400180
Alexandre Lision72e37322013-11-04 17:14:11 -0500181 Context mContext;
alision50fa0722013-06-25 17:29:44 -0400182 ArrayList<HistoryEntry> dataset;
183 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
184
Alexandre Lision72e37322013-11-04 17:14:11 -0500185 public HistoryAdapter(Context activity, ArrayList<HistoryEntry> history) {
alision50fa0722013-06-25 17:29:44 -0400186 mContext = activity;
187 dataset = history;
188 }
189
190 @Override
191 public View getView(final int pos, View convertView, ViewGroup arg2) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500192
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500193 HistoryView entryView;
alision50fa0722013-06-25 17:29:44 -0400194
Alexandre Lision40954dc2013-10-09 15:24:03 -0400195 if (convertView == null) {
alision50fa0722013-06-25 17:29:44 -0400196 // Get a new instance of the row layout view
Alexandre Lision72e37322013-11-04 17:14:11 -0500197 LayoutInflater inflater = LayoutInflater.from(mContext);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400198 convertView = inflater.inflate(R.layout.item_history, null);
alision50fa0722013-06-25 17:29:44 -0400199
200 // Hold the view objects in an object
201 // so they don't need to be re-fetched
202 entryView = new HistoryView();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500203 entryView.photo = (ImageButton) convertView.findViewById(R.id.photo);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400204 entryView.displayName = (TextView) convertView.findViewById(R.id.display_name);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400205 entryView.date = (TextView) convertView.findViewById(R.id.date_start);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400206 entryView.incoming = (TextView) convertView.findViewById(R.id.incomings);
207 entryView.outgoing = (TextView) convertView.findViewById(R.id.outgoings);
208 entryView.replay = (Button) convertView.findViewById(R.id.replay);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400209 convertView.setTag(entryView);
alision50fa0722013-06-25 17:29:44 -0400210 } else {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400211 entryView = (HistoryView) convertView.getTag();
alision50fa0722013-06-25 17:29:44 -0400212 }
213
214 // Transfer the stock data from the data object
215 // to the view objects
216
217 // SipCall call = (SipCall) mCallList.values().toArray()[position];
218 entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName());
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500219 infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, dataset.get(pos).getContact()));
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400220
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400221 entryView.incoming.setText(getString(R.string.hist_in_calls, dataset.get(pos).getIncoming_sum()));
222 entryView.outgoing.setText(getString(R.string.hist_out_calls, dataset.get(pos).getOutgoing_sum()));
alision50fa0722013-06-25 17:29:44 -0400223
Alexandre Lision945e4612014-01-15 17:40:31 -0500224 /*if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
alision50fa0722013-06-25 17:29:44 -0400225 entryView.replay.setVisibility(View.VISIBLE);
226 entryView.replay.setTag(R.id.replay, true);
227 entryView.replay.setOnClickListener(new OnClickListener() {
228
229 @Override
230 public void onClick(View v) {
231 try {
232 if ((Boolean) v.getTag(R.id.replay)) {
233 mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
234 v.setTag(R.id.replay, false);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500235 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
alision50fa0722013-06-25 17:29:44 -0400236 } else {
237 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
238 v.setTag(R.id.replay, true);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500239 ((Button) v).setText(getString(R.string.hist_replay_button));
alision50fa0722013-06-25 17:29:44 -0400240 }
241 } catch (RemoteException e) {
242 // TODO Auto-generated catch block
243 e.printStackTrace();
244 }
245 }
246 });
Alexandre Lision945e4612014-01-15 17:40:31 -0500247 }*/
alision50fa0722013-06-25 17:29:44 -0400248
Alexandre Lision945e4612014-01-15 17:40:31 -0500249 /*entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate());*/
Alexandre Lision2e52d392013-11-06 15:14:31 -0500250 entryView.photo.setOnClickListener(new OnClickListener() {
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400251
252 @Override
253 public void onClick(View v) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500254 makeNewCall(pos);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400255
256 }
257 });
alision50fa0722013-06-25 17:29:44 -0400258
Alexandre Lision40954dc2013-10-09 15:24:03 -0400259 return convertView;
alision50fa0722013-06-25 17:29:44 -0400260
261 }
262
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500263 /**
264 * ******************
alision50fa0722013-06-25 17:29:44 -0400265 * ViewHolder Pattern
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500266 * *******************
267 */
alision50fa0722013-06-25 17:29:44 -0400268 public class HistoryView {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500269 public ImageButton photo;
alision50fa0722013-06-25 17:29:44 -0400270 protected TextView displayName;
271 protected TextView date;
alision50fa0722013-06-25 17:29:44 -0400272 private Button replay;
alision50fa0722013-06-25 17:29:44 -0400273 private TextView outgoing;
274 private TextView incoming;
275 }
276
277 @Override
278 public int getCount() {
279
280 return dataset.size();
281 }
282
283 @Override
284 public HistoryEntry getItem(int pos) {
285 return dataset.get(pos);
286 }
287
288 @Override
289 public long getItemId(int arg0) {
290 return 0;
291 }
292
293 public void clear() {
294 dataset.clear();
295
296 }
297
Alexandre Lision72e37322013-11-04 17:14:11 -0500298 public void addAll(ArrayList<HistoryEntry> history) {
299 dataset.addAll(history);
alision50fa0722013-06-25 17:29:44 -0400300 }
301
302 }
303
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500304 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500305 public AsyncTaskLoader<ArrayList<HistoryEntry>> onCreateLoader(int arg0, Bundle arg1) {
306 HistoryLoader loader = new HistoryLoader(getActivity());
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500307 loader.forceLoad();
308 return loader;
309 }
310
311 @Override
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500312 public void onLoadFinished(Loader<ArrayList<HistoryEntry>> arrayListLoader, ArrayList<HistoryEntry> historyEntries) {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500313 mAdapter.clear();
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500314 mAdapter.addAll(historyEntries);
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500315 mAdapter.notifyDataSetChanged();
316 }
317
318 @Override
Alexandre Lisionbe54c342014-01-21 17:10:33 -0500319 public void onLoaderReset(Loader<ArrayList<HistoryEntry>> arrayListLoader) {
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500320
321 }
322
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500323
alisiond9e29442013-04-17 16:10:18 -0400324}