blob: 8a2e0a2cc25b86133afb25f470c87133a182ca5c [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) {
97 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;
alisionb1763882013-06-18 17:30:51 -0400104 getLoaderManager().initLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400105 }
106
107 @Override
108 public void onDetach() {
109 super.onDetach();
110 mCallbacks = sDummyCallbacks;
111 }
112
113 @Override
114 public void onCreate(Bundle savedInstanceState) {
115 super.onCreate(savedInstanceState);
alisionb1763882013-06-18 17:30:51 -0400116 getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this);
alisione2a38e12013-04-25 14:20:20 -0400117 }
alisiond9e29442013-04-17 16:10:18 -0400118
119 @Override
120 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
alisione2a38e12013-04-25 14:20:20 -0400121 View inflatedView = inflater.inflate(R.layout.frag_history, parent, false);
alision2ec64f92013-06-17 17:28:58 -0400122
alision1005ba12013-06-19 13:52:44 -0400123 ((ListView) inflatedView.findViewById(android.R.id.list)).setOnItemClickListener(new OnItemClickListener() {
alision2ec64f92013-06-17 17:28:58 -0400124
125 @Override
126 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
127 mAdapter.getItem(pos);
128 }
129 });
alisione2a38e12013-04-25 14:20:20 -0400130 return inflatedView;
131 }
132
133 @Override
134 public void onStart() {
135 super.onStart();
136 Log.w(TAG, "onStart");
alisione2a38e12013-04-25 14:20:20 -0400137 }
138
alision1005ba12013-06-19 13:52:44 -0400139 public void makeNewCall(int position) {
alision907bde72013-06-20 14:40:37 -0400140 mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber());
alision2ec64f92013-06-17 17:28:58 -0400141 }
alisione2a38e12013-04-25 14:20:20 -0400142
alision2ec64f92013-06-17 17:28:58 -0400143 @Override
144 public Loader<ArrayList<HistoryEntry>> onCreateLoader(int id, Bundle args) {
alision1005ba12013-06-19 13:52:44 -0400145
alision2ec64f92013-06-17 17:28:58 -0400146 HistoryLoader loader = new HistoryLoader(getActivity(), mCallbacks.getService());
147 loader.forceLoad();
148 return loader;
alision50fa0722013-06-25 17:29:44 -0400149
alision2ec64f92013-06-17 17:28:58 -0400150 }
151
152 @Override
153 public void onLoadFinished(Loader<ArrayList<HistoryEntry>> arg0, ArrayList<HistoryEntry> history) {
154 mAdapter = new HistoryAdapter(this, history);
155 getListView().setAdapter(mAdapter);
156 mAdapter.notifyDataSetChanged();
157
158 }
159
160 @Override
161 public void onLoaderReset(Loader<ArrayList<HistoryEntry>> arg0) {
162 // TODO Auto-generated method stub
alisione2a38e12013-04-25 14:20:20 -0400163
alisiond9e29442013-04-17 16:10:18 -0400164 }
alision1005ba12013-06-19 13:52:44 -0400165
alision50fa0722013-06-25 17:29:44 -0400166 public class HistoryAdapter extends BaseAdapter {
167
168 HistoryFragment mContext;
169 ArrayList<HistoryEntry> dataset;
170 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
171
172 public HistoryAdapter(HistoryFragment activity, ArrayList<HistoryEntry> history) {
173 mContext = activity;
174 dataset = history;
175 }
176
177 @Override
178 public View getView(final int pos, View convertView, ViewGroup arg2) {
179 View rowView = convertView;
180 HistoryView entryView = null;
181
182 if (rowView == null) {
183 // Get a new instance of the row layout view
184 LayoutInflater inflater = LayoutInflater.from(mContext.getActivity());
185 rowView = inflater.inflate(R.layout.item_history, null);
186
187 // Hold the view objects in an object
188 // so they don't need to be re-fetched
189 entryView = new HistoryView();
190 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
191 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
192 entryView.duration = (TextView) rowView.findViewById(R.id.duration);
193 entryView.date = (TextView) rowView.findViewById(R.id.date_start);
194 entryView.missed = (TextView) rowView.findViewById(R.id.missed);
195 entryView.incoming = (TextView) rowView.findViewById(R.id.incomings);
196 entryView.outgoing = (TextView) rowView.findViewById(R.id.outgoings);
197 entryView.replay = (Button) rowView.findViewById(R.id.replay);
198 entryView.call_button = (ImageButton) rowView.findViewById(R.id.action_call);
199 entryView.call_button.setOnClickListener(new OnClickListener() {
200
201 @Override
202 public void onClick(View v) {
203 mContext.makeNewCall(pos);
204
205 }
206 });
207 rowView.setTag(entryView);
208 } else {
209 entryView = (HistoryView) rowView.getTag();
210 }
211
212 // Transfer the stock data from the data object
213 // to the view objects
214
215 // SipCall call = (SipCall) mCallList.values().toArray()[position];
216 entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName());
217
218 infos_fetcher.execute(new ContactPictureLoader(mContext.getActivity(), entryView.photo, dataset.get(pos).getContact().getId()));
219
220 entryView.missed.setText("Missed:" + dataset.get(pos).getMissed_sum());
221 entryView.incoming.setText("In:" + dataset.get(pos).getIncoming_sum());
222 entryView.outgoing.setText("Out:" + dataset.get(pos).getOutgoing_sum());
223
224 if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) {
225 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);
235 ((Button)v).setText("Stop");
236 } else {
237 mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath());
238 v.setTag(R.id.replay, true);
239 ((Button)v).setText("Replay");
240 }
241 } catch (RemoteException e) {
242 // TODO Auto-generated catch block
243 e.printStackTrace();
244 }
245 }
246 });
247 }
248
249 entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate("yyyy-MM-dd"));
250 entryView.duration.setText(dataset.get(pos).getTotalDuration());
251
252 return rowView;
253
254 }
255
256 /*********************
257 * ViewHolder Pattern
258 *********************/
259 public class HistoryView {
260 public ImageView photo;
261 protected TextView displayName;
262 protected TextView date;
263 public TextView duration;
264 private ImageButton call_button;
265 private Button replay;
266 private TextView missed;
267 private TextView outgoing;
268 private TextView incoming;
269 }
270
271 @Override
272 public int getCount() {
273
274 return dataset.size();
275 }
276
277 @Override
278 public HistoryEntry getItem(int pos) {
279 return dataset.get(pos);
280 }
281
282 @Override
283 public long getItemId(int arg0) {
284 return 0;
285 }
286
287 public void clear() {
288 dataset.clear();
289
290 }
291
292 public void addAll(ArrayList<HashMap<String, String>> history) {
293 // dataset.addAll(history);
294
295 }
296
297 }
298
alisiond9e29442013-04-17 16:10:18 -0400299}