blob: 2c1624a98a1acda56fb5743cee0034379e7af092 [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 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;
Alexandre Lision4ab53972013-11-04 16:59:18 -050046import android.content.Context;
Alexandre Lision2e52d392013-11-06 15:14:31 -050047import android.content.Intent;
alisiond9e29442013-04-17 16:10:18 -040048import android.os.Bundle;
alisione2a38e12013-04-25 14:20:20 -040049import android.os.RemoteException;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050050import android.support.v4.app.ListFragment;
51import android.support.v4.app.LoaderManager.LoaderCallbacks;
alisione2a38e12013-04-25 14:20:20 -040052import android.util.Log;
alisiond9e29442013-04-17 16:10:18 -040053import android.view.LayoutInflater;
54import android.view.View;
alision50fa0722013-06-25 17:29:44 -040055import android.view.View.OnClickListener;
alisiond9e29442013-04-17 16:10:18 -040056import android.view.ViewGroup;
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;
104 }
105
106 @Override
107 public void onDetach() {
108 super.onDetach();
109 mCallbacks = sDummyCallbacks;
110 }
111
112 @Override
113 public void onCreate(Bundle savedInstanceState) {
114 super.onCreate(savedInstanceState);
Alexandre Lision72e37322013-11-04 17:14:11 -0500115 mAdapter = new HistoryAdapter(getActivity(), new ArrayList<HistoryEntry>());
alisione2a38e12013-04-25 14:20:20 -0400116 }
alisiond9e29442013-04-17 16:10:18 -0400117
118 @Override
119 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
alisione2a38e12013-04-25 14:20:20 -0400120 View inflatedView = inflater.inflate(R.layout.frag_history, parent, false);
alision2ec64f92013-06-17 17:28:58 -0400121
Alexandre Lision2e52d392013-11-06 15:14:31 -0500122 return inflatedView;
123 }
124
125 @Override
126 public void onActivityCreated(Bundle savedInstanceState) {
127
128 super.onActivityCreated(savedInstanceState);
129 getListView().setAdapter(mAdapter);
130
131 getListView().setOnItemClickListener(new OnItemClickListener() {
alision2ec64f92013-06-17 17:28:58 -0400132
133 @Override
134 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500135
136 Bundle b = new Bundle();
137 b.putParcelable("entry", mAdapter.getItem(pos));
138 Intent toStart = new Intent(getActivity(), DetailHistoryActivity.class).putExtra(HistoryFragment.ARGS, b);
139 startActivity(toStart);
140
alision2ec64f92013-06-17 17:28:58 -0400141 }
142 });
alisione2a38e12013-04-25 14:20:20 -0400143 }
144
145 @Override
146 public void onStart() {
147 super.onStart();
148 Log.w(TAG, "onStart");
Alexandre Lision4a56d432013-07-19 11:37:53 -0400149 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400150 }
151
alision1005ba12013-06-19 13:52:44 -0400152 public void makeNewCall(int position) {
alision907bde72013-06-20 14:40:37 -0400153 mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber());
alision2ec64f92013-06-17 17:28:58 -0400154 }
alisione2a38e12013-04-25 14:20:20 -0400155
Alexandre Lision72e37322013-11-04 17:14:11 -0500156 public class HistoryAdapter extends BaseAdapter implements ListAdapter {
alision50fa0722013-06-25 17:29:44 -0400157
Alexandre Lision72e37322013-11-04 17:14:11 -0500158 Context mContext;
alision50fa0722013-06-25 17:29:44 -0400159 ArrayList<HistoryEntry> dataset;
160 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
161
Alexandre Lision72e37322013-11-04 17:14:11 -0500162 public HistoryAdapter(Context activity, ArrayList<HistoryEntry> history) {
alision50fa0722013-06-25 17:29:44 -0400163 mContext = activity;
164 dataset = history;
165 }
166
167 @Override
168 public View getView(final int pos, View convertView, ViewGroup arg2) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500169
alision50fa0722013-06-25 17:29:44 -0400170 HistoryView entryView = null;
171
Alexandre Lision40954dc2013-10-09 15:24:03 -0400172 if (convertView == null) {
alision50fa0722013-06-25 17:29:44 -0400173 // Get a new instance of the row layout view
Alexandre Lision72e37322013-11-04 17:14:11 -0500174 LayoutInflater inflater = LayoutInflater.from(mContext);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400175 convertView = inflater.inflate(R.layout.item_history, null);
alision50fa0722013-06-25 17:29:44 -0400176
177 // Hold the view objects in an object
178 // so they don't need to be re-fetched
179 entryView = new HistoryView();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500180 entryView.photo = (ImageButton) convertView.findViewById(R.id.photo);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400181 entryView.displayName = (TextView) convertView.findViewById(R.id.display_name);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400182 entryView.date = (TextView) convertView.findViewById(R.id.date_start);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400183 entryView.incoming = (TextView) convertView.findViewById(R.id.incomings);
184 entryView.outgoing = (TextView) convertView.findViewById(R.id.outgoings);
185 entryView.replay = (Button) convertView.findViewById(R.id.replay);
Alexandre Lision40954dc2013-10-09 15:24:03 -0400186 convertView.setTag(entryView);
alision50fa0722013-06-25 17:29:44 -0400187 } else {
Alexandre Lision40954dc2013-10-09 15:24:03 -0400188 entryView = (HistoryView) convertView.getTag();
alision50fa0722013-06-25 17:29:44 -0400189 }
190
191 // Transfer the stock data from the data object
192 // to the view objects
193
194 // SipCall call = (SipCall) mCallList.values().toArray()[position];
195 entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName());
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500196 infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, dataset.get(pos).getContact()));
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400197
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400198 entryView.incoming.setText(getString(R.string.hist_in_calls, dataset.get(pos).getIncoming_sum()));
199 entryView.outgoing.setText(getString(R.string.hist_out_calls, dataset.get(pos).getOutgoing_sum()));
alision50fa0722013-06-25 17:29:44 -0400200
201 if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
202 entryView.replay.setVisibility(View.VISIBLE);
203 entryView.replay.setTag(R.id.replay, true);
204 entryView.replay.setOnClickListener(new OnClickListener() {
205
206 @Override
207 public void onClick(View v) {
208 try {
209 if ((Boolean) v.getTag(R.id.replay)) {
210 mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
211 v.setTag(R.id.replay, false);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500212 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
alision50fa0722013-06-25 17:29:44 -0400213 } else {
214 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
215 v.setTag(R.id.replay, true);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500216 ((Button) v).setText(getString(R.string.hist_replay_button));
alision50fa0722013-06-25 17:29:44 -0400217 }
218 } catch (RemoteException e) {
219 // TODO Auto-generated catch block
220 e.printStackTrace();
221 }
222 }
223 });
224 }
225
Alexandre Lision2e52d392013-11-06 15:14:31 -0500226 entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate());
227 entryView.photo.setOnClickListener(new OnClickListener() {
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400228
229 @Override
230 public void onClick(View v) {
Alexandre Lision72e37322013-11-04 17:14:11 -0500231 makeNewCall(pos);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400232
233 }
234 });
alision50fa0722013-06-25 17:29:44 -0400235
Alexandre Lision40954dc2013-10-09 15:24:03 -0400236 return convertView;
alision50fa0722013-06-25 17:29:44 -0400237
238 }
239
240 /*********************
241 * ViewHolder Pattern
242 *********************/
243 public class HistoryView {
Alexandre Lision2e52d392013-11-06 15:14:31 -0500244 public ImageButton photo;
alision50fa0722013-06-25 17:29:44 -0400245 protected TextView displayName;
246 protected TextView date;
alision50fa0722013-06-25 17:29:44 -0400247 private Button replay;
alision50fa0722013-06-25 17:29:44 -0400248 private TextView outgoing;
249 private TextView incoming;
250 }
251
252 @Override
253 public int getCount() {
254
255 return dataset.size();
256 }
257
258 @Override
259 public HistoryEntry getItem(int pos) {
260 return dataset.get(pos);
261 }
262
263 @Override
264 public long getItemId(int arg0) {
265 return 0;
266 }
267
268 public void clear() {
269 dataset.clear();
270
271 }
272
Alexandre Lision72e37322013-11-04 17:14:11 -0500273 public void addAll(ArrayList<HistoryEntry> history) {
274 dataset.addAll(history);
alision50fa0722013-06-25 17:29:44 -0400275 }
276
277 }
278
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500279 @Override
280 public android.support.v4.content.Loader<ArrayList<HistoryEntry>> onCreateLoader(int arg0, Bundle arg1) {
281 HistoryLoader loader = new HistoryLoader(getActivity(), mCallbacks.getService());
282 loader.forceLoad();
283 return loader;
284 }
285
286 @Override
287 public void onLoadFinished(android.support.v4.content.Loader<ArrayList<HistoryEntry>> arg0, ArrayList<HistoryEntry> history) {
288 mAdapter.clear();
289 mAdapter.addAll(history);
290 mAdapter.notifyDataSetChanged();
291 }
292
293 @Override
294 public void onLoaderReset(android.support.v4.content.Loader<ArrayList<HistoryEntry>> arg0) {
295 // TODO Stub de la méthode généré automatiquement
296
297 }
298
alisiond9e29442013-04-17 16:10:18 -0400299}