blob: 766e0672330d85b1532c8b4cde2bb7b4a96dd7d7 [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 */
alisiond9e29442013-04-17 16:10:18 -040031package com.savoirfairelinux.sflphone.fragments;
32
alisione2a38e12013-04-25 14:20:20 -040033import java.util.ArrayList;
34import java.util.HashMap;
alision50fa0722013-06-25 17:29:44 -040035import java.util.concurrent.ExecutorService;
36import java.util.concurrent.Executors;
alisione2a38e12013-04-25 14:20:20 -040037
38import android.app.Activity;
39import android.app.ListFragment;
alision2ec64f92013-06-17 17:28:58 -040040import android.app.LoaderManager.LoaderCallbacks;
41import android.content.Loader;
alisiond9e29442013-04-17 16:10:18 -040042import android.os.Bundle;
alisione2a38e12013-04-25 14:20:20 -040043import android.os.RemoteException;
44import android.util.Log;
alisiond9e29442013-04-17 16:10:18 -040045import android.view.LayoutInflater;
46import android.view.View;
alision50fa0722013-06-25 17:29:44 -040047import android.view.View.OnClickListener;
alisiond9e29442013-04-17 16:10:18 -040048import android.view.ViewGroup;
alision2ec64f92013-06-17 17:28:58 -040049import android.widget.AdapterView;
alision2ec64f92013-06-17 17:28:58 -040050import android.widget.AdapterView.OnItemClickListener;
alision50fa0722013-06-25 17:29:44 -040051import android.widget.BaseAdapter;
52import android.widget.Button;
53import android.widget.ImageButton;
54import android.widget.ImageView;
55import android.widget.ListView;
56import android.widget.TextView;
alisiond9e29442013-04-17 16:10:18 -040057
alisione2a38e12013-04-25 14:20:20 -040058import com.savoirfairelinux.sflphone.R;
alision50fa0722013-06-25 17:29:44 -040059import com.savoirfairelinux.sflphone.adapters.ContactPictureLoader;
alision2ec64f92013-06-17 17:28:58 -040060import com.savoirfairelinux.sflphone.loaders.HistoryLoader;
61import com.savoirfairelinux.sflphone.loaders.LoaderConstants;
alision2ec64f92013-06-17 17:28:58 -040062import com.savoirfairelinux.sflphone.model.HistoryEntry;
alisione2a38e12013-04-25 14:20:20 -040063import com.savoirfairelinux.sflphone.service.ISipService;
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();
alisiond9e29442013-04-17 16:10:18 -040068 public static final String ARG_SECTION_NUMBER = "section_number";
alision2ec64f92013-06-17 17:28:58 -040069
alisione2a38e12013-04-25 14:20:20 -040070 HistoryAdapter mAdapter;
71 private Callbacks mCallbacks = sDummyCallbacks;
72 /**
73 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
74 */
75 private static Callbacks sDummyCallbacks = new Callbacks() {
76 @Override
alision907bde72013-06-20 14:40:37 -040077 public void onCallDialed(String to) {
alisione2a38e12013-04-25 14:20:20 -040078 }
79
80 @Override
81 public ISipService getService() {
82 Log.i(TAG, "Dummy");
83 return null;
84 }
85
86 };
87
88 public interface Callbacks {
alision907bde72013-06-20 14:40:37 -040089 public void onCallDialed(String 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;
alisionb1763882013-06-18 17:30:51 -0400105 getLoaderManager().initLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400106 }
107
108 @Override
109 public void onDetach() {
110 super.onDetach();
111 mCallbacks = sDummyCallbacks;
112 }
113
114 @Override
115 public void onCreate(Bundle savedInstanceState) {
116 super.onCreate(savedInstanceState);
Alexandre Lision4a56d432013-07-19 11:37:53 -0400117
alisione2a38e12013-04-25 14:20:20 -0400118 }
alisiond9e29442013-04-17 16:10:18 -0400119
120 @Override
121 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
alisione2a38e12013-04-25 14:20:20 -0400122 View inflatedView = inflater.inflate(R.layout.frag_history, parent, false);
alision2ec64f92013-06-17 17:28:58 -0400123
alision1005ba12013-06-19 13:52:44 -0400124 ((ListView) inflatedView.findViewById(android.R.id.list)).setOnItemClickListener(new OnItemClickListener() {
alision2ec64f92013-06-17 17:28:58 -0400125
126 @Override
127 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
128 mAdapter.getItem(pos);
129 }
130 });
alisione2a38e12013-04-25 14:20:20 -0400131 return inflatedView;
132 }
133
134 @Override
135 public void onStart() {
136 super.onStart();
137 Log.w(TAG, "onStart");
Alexandre Lision4a56d432013-07-19 11:37:53 -0400138 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400139 }
140
alision1005ba12013-06-19 13:52:44 -0400141 public void makeNewCall(int position) {
alision907bde72013-06-20 14:40:37 -0400142 mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber());
alision2ec64f92013-06-17 17:28:58 -0400143 }
alisione2a38e12013-04-25 14:20:20 -0400144
alision2ec64f92013-06-17 17:28:58 -0400145 @Override
146 public Loader<ArrayList<HistoryEntry>> onCreateLoader(int id, Bundle args) {
alision1005ba12013-06-19 13:52:44 -0400147
alision2ec64f92013-06-17 17:28:58 -0400148 HistoryLoader loader = new HistoryLoader(getActivity(), mCallbacks.getService());
149 loader.forceLoad();
150 return loader;
alision50fa0722013-06-25 17:29:44 -0400151
alision2ec64f92013-06-17 17:28:58 -0400152 }
153
154 @Override
155 public void onLoadFinished(Loader<ArrayList<HistoryEntry>> arg0, ArrayList<HistoryEntry> history) {
156 mAdapter = new HistoryAdapter(this, history);
157 getListView().setAdapter(mAdapter);
158 mAdapter.notifyDataSetChanged();
159
160 }
161
162 @Override
163 public void onLoaderReset(Loader<ArrayList<HistoryEntry>> arg0) {
164 // TODO Auto-generated method stub
alisione2a38e12013-04-25 14:20:20 -0400165
alisiond9e29442013-04-17 16:10:18 -0400166 }
alision1005ba12013-06-19 13:52:44 -0400167
alision50fa0722013-06-25 17:29:44 -0400168 public class HistoryAdapter extends BaseAdapter {
169
170 HistoryFragment mContext;
171 ArrayList<HistoryEntry> dataset;
172 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
173
174 public HistoryAdapter(HistoryFragment activity, ArrayList<HistoryEntry> history) {
175 mContext = activity;
176 dataset = history;
177 }
178
179 @Override
180 public View getView(final int pos, View convertView, ViewGroup arg2) {
181 View rowView = convertView;
182 HistoryView entryView = null;
183
184 if (rowView == null) {
185 // Get a new instance of the row layout view
186 LayoutInflater inflater = LayoutInflater.from(mContext.getActivity());
187 rowView = inflater.inflate(R.layout.item_history, null);
188
189 // Hold the view objects in an object
190 // so they don't need to be re-fetched
191 entryView = new HistoryView();
192 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
193 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
194 entryView.duration = (TextView) rowView.findViewById(R.id.duration);
195 entryView.date = (TextView) rowView.findViewById(R.id.date_start);
196 entryView.missed = (TextView) rowView.findViewById(R.id.missed);
197 entryView.incoming = (TextView) rowView.findViewById(R.id.incomings);
198 entryView.outgoing = (TextView) rowView.findViewById(R.id.outgoings);
199 entryView.replay = (Button) rowView.findViewById(R.id.replay);
200 entryView.call_button = (ImageButton) rowView.findViewById(R.id.action_call);
201 entryView.call_button.setOnClickListener(new OnClickListener() {
202
203 @Override
204 public void onClick(View v) {
205 mContext.makeNewCall(pos);
206
207 }
208 });
209 rowView.setTag(entryView);
210 } else {
211 entryView = (HistoryView) rowView.getTag();
212 }
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());
219
220 infos_fetcher.execute(new ContactPictureLoader(mContext.getActivity(), entryView.photo, dataset.get(pos).getContact().getId()));
221
222 entryView.missed.setText("Missed:" + dataset.get(pos).getMissed_sum());
223 entryView.incoming.setText("In:" + dataset.get(pos).getIncoming_sum());
224 entryView.outgoing.setText("Out:" + dataset.get(pos).getOutgoing_sum());
225
226 if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
227 entryView.replay.setVisibility(View.VISIBLE);
228 entryView.replay.setTag(R.id.replay, true);
229 entryView.replay.setOnClickListener(new OnClickListener() {
230
231 @Override
232 public void onClick(View v) {
233 try {
234 if ((Boolean) v.getTag(R.id.replay)) {
235 mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
236 v.setTag(R.id.replay, false);
237 ((Button)v).setText("Stop");
238 } else {
239 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
240 v.setTag(R.id.replay, true);
241 ((Button)v).setText("Replay");
242 }
243 } catch (RemoteException e) {
244 // TODO Auto-generated catch block
245 e.printStackTrace();
246 }
247 }
248 });
249 }
250
251 entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate("yyyy-MM-dd"));
252 entryView.duration.setText(dataset.get(pos).getTotalDuration());
253
254 return rowView;
255
256 }
257
258 /*********************
259 * ViewHolder Pattern
260 *********************/
261 public class HistoryView {
262 public ImageView photo;
263 protected TextView displayName;
264 protected TextView date;
265 public TextView duration;
266 private ImageButton call_button;
267 private Button replay;
268 private TextView missed;
269 private TextView outgoing;
270 private TextView incoming;
271 }
272
273 @Override
274 public int getCount() {
275
276 return dataset.size();
277 }
278
279 @Override
280 public HistoryEntry getItem(int pos) {
281 return dataset.get(pos);
282 }
283
284 @Override
285 public long getItemId(int arg0) {
286 return 0;
287 }
288
289 public void clear() {
290 dataset.clear();
291
292 }
293
294 public void addAll(ArrayList<HashMap<String, String>> history) {
295 // dataset.addAll(history);
296
297 }
298
299 }
300
alisiond9e29442013-04-17 16:10:18 -0400301}