blob: 06600d325d250eb34fe577056c0848a552f7cb3c [file] [log] [blame]
Alexandre Lision2e52d392013-11-06 15:14:31 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lision2e52d392013-11-06 15:14:31 -05003 *
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 */
31
32package org.sflphone.fragments;
33
Alexandre Lision2e52d392013-11-06 15:14:31 -050034
35import android.app.Activity;
36import android.app.Fragment;
37import android.content.Context;
38import android.os.Bundle;
39import android.os.RemoteException;
40import android.view.LayoutInflater;
41import android.view.View;
Alexandre Lision2e52d392013-11-06 15:14:31 -050042import android.view.View.OnClickListener;
43import android.view.ViewGroup;
Alexandre Lision64efcc92014-01-29 11:25:07 -050044import android.widget.*;
45import org.sflphone.R;
46import org.sflphone.adapters.ContactPictureTask;
47import org.sflphone.history.HistoryCall;
48import org.sflphone.history.HistoryEntry;
49import org.sflphone.model.Account;
50import org.sflphone.model.SipCall;
51import org.sflphone.service.ISipService;
52
53import java.io.InvalidObjectException;
54import java.util.ArrayList;
55import java.util.HashMap;
56import java.util.NavigableMap;
Alexandre Lision2e52d392013-11-06 15:14:31 -050057
58public class DetailsHistoryEntryFragment extends Fragment {
59
Alexandre Lision2e52d392013-11-06 15:14:31 -050060 DetailHistoryAdapter mAdapter;
61 HistoryEntry toDisplay;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050062 @SuppressWarnings("unused")
Alexandre Lision2e52d392013-11-06 15:14:31 -050063 private static final String TAG = DetailsHistoryEntryFragment.class.getSimpleName();
64 ContactPictureTask tasker;
65
Alexandre Lision2e52d392013-11-06 15:14:31 -050066 private ListView lvMain;
Alexandre Lisiondede80e2014-01-29 09:51:07 -050067 private LinearLayout llMain;
Alexandre Lision2e52d392013-11-06 15:14:31 -050068 private RelativeLayout iv;
69
70 private Callbacks mCallbacks = sDummyCallbacks;
71
72 private static Callbacks sDummyCallbacks = new Callbacks() {
73
74 @Override
75 public ISipService getService() {
76 return null;
77 }
78
Alexandre Lision6da3bb92013-12-10 17:32:46 -050079 @Override
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050080 public void onCall(SipCall call) {
Alexandre Lision6da3bb92013-12-10 17:32:46 -050081 }
82
Alexandre Lision2e52d392013-11-06 15:14:31 -050083 };
84
85 public interface Callbacks {
86
87 public ISipService getService();
Alexandre Lisiondede80e2014-01-29 09:51:07 -050088
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050089 public void onCall(SipCall call);
Alexandre Lision2e52d392013-11-06 15:14:31 -050090
91 }
92
93 @Override
94 public void onAttach(Activity activity) {
95 super.onAttach(activity);
96
97 if (!(activity instanceof Callbacks)) {
98 throw new IllegalStateException("Activity must implement fragment's callbacks.");
99 }
100
101 mCallbacks = (Callbacks) activity;
102 }
103
104 @Override
105 public void onDetach() {
106 super.onDetach();
107 mCallbacks = sDummyCallbacks;
108 }
109
110 @Override
111 public void onCreate(Bundle savedInstanceState) {
112 super.onCreate(savedInstanceState);
113
114 toDisplay = (HistoryEntry) getArguments().get("entry");
115 mAdapter = new DetailHistoryAdapter(toDisplay.getCalls(), getActivity());
116 }
117
118 @Override
119 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
120 View inflatedView = inflater.inflate(R.layout.frag_history_detail, parent, false);
121
Alexandre Lision2e52d392013-11-06 15:14:31 -0500122 llMain = (LinearLayout) inflatedView.findViewById(R.id.llMain);
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500123 /*llMainHolder = (LinearLayout) inflatedView.findViewById(R.id.llMainHolder);*/
Alexandre Lision2e52d392013-11-06 15:14:31 -0500124 lvMain = (ListView) inflatedView.findViewById(R.id.lvMain);
125 lvMain.setAdapter(mAdapter);
126 iv = (RelativeLayout) inflatedView.findViewById(R.id.iv);
127
128 ((TextView) iv.findViewById(R.id.history_call_name)).setText(toDisplay.getContact().getmDisplayName());
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500129
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500130 tasker = new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.contact_photo), toDisplay.getContact());
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500131 tasker.run();
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500132// ((TextView) iv.findViewById(R.id.history_entry_number)).setText(getString(R.string.detail_hist_call_number, toDisplay.getNumber()));
133 iv.findViewById(R.id.history_call_name).setOnClickListener(new OnClickListener() {
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500134
135 @Override
136 public void onClick(View v) {
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500137 try {
138 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
139
140 HashMap<String, String> details = (HashMap<String, String>) mCallbacks.getService().getAccountDetails(toDisplay.getAccountID());
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500141 ArrayList<HashMap<String, String>> creds = (ArrayList<HashMap<String, String>>) mCallbacks.getService().getCredentials(toDisplay.getAccountID());
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500142
143 callBuilder.startCallCreation().setAccount(new Account(toDisplay.getAccountID(), details, creds))
Alexandre Lision67c70b42014-01-16 13:57:15 -0500144 .setCallType(SipCall.direction.CALL_TYPE_OUTGOING);
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500145 callBuilder.setContact(toDisplay.getContact());
146
147 mCallbacks.onCall(callBuilder.build());
148
149 } catch (RemoteException e) {
150 // TODO Bloc catch généré automatiquement
151 e.printStackTrace();
152 } catch (InvalidObjectException e) {
153 // TODO Bloc catch généré automatiquement
154 e.printStackTrace();
155 }
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500156 }
157 });
Alexandre Lision2e52d392013-11-06 15:14:31 -0500158 return inflatedView;
159 }
160
161 public void onActivityCreated(Bundle savedInstanceState) {
162 super.onActivityCreated(savedInstanceState);
163
164 }
165
Alexandre Lision2e52d392013-11-06 15:14:31 -0500166 private class DetailHistoryAdapter extends BaseAdapter implements ListAdapter {
167
168 ArrayList<HistoryCall> dataset;
169 Context mContext;
170
171 public DetailHistoryAdapter(NavigableMap<Long, HistoryCall> calls, Context c) {
172 dataset = new ArrayList<HistoryCall>(calls.descendingMap().values());
173 mContext = c;
174 }
175
176 @Override
177 public int getCount() {
178 return dataset.size();
179 }
180
181 @Override
182 public Object getItem(int position) {
183 return dataset.get(position);
184 }
185
186 @Override
187 public long getItemId(int position) {
188 return 0;
189 }
190
191 @Override
192 public View getView(int position, View convertView, ViewGroup parent) {
193 HistoryCallView entryView = null;
194
195 if (convertView == null) {
196 // Get a new instance of the row layout view
197 LayoutInflater inflater = LayoutInflater.from(mContext);
198 convertView = inflater.inflate(R.layout.item_history_call, null);
199
200 // Hold the view objects in an object
201 // so they don't need to be re-fetched
202 entryView = new HistoryCallView();
203 entryView.historyCallState = (TextView) convertView.findViewById(R.id.history_call_state);
204 entryView.formatted_date = (TextView) convertView.findViewById(R.id.history_call_date_formatted);
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500205 entryView.formatted_hour = (TextView) convertView.findViewById(R.id.history_call_hour);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500206 entryView.record = (Button) convertView.findViewById(R.id.history_call_record);
207 entryView.duration = (TextView) convertView.findViewById(R.id.history_call_duration);
208
209 convertView.setTag(entryView);
210 } else {
211 entryView = (HistoryCallView) convertView.getTag();
212 }
213
214 final HistoryCall item = dataset.get(position);
215
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500216 entryView.historyCallState.setText(item.getDirection());
Alexandre Lision2e52d392013-11-06 15:14:31 -0500217 entryView.formatted_date.setText(item.getDate());
Alexandre Lision2e52d392013-11-06 15:14:31 -0500218 entryView.duration.setText(item.getDurationString());
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500219 entryView.formatted_hour.setText(item.getStartString("h:mm a"));
220 if (item.isIncoming() && item.isMissed())
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500221 convertView.setBackgroundColor(getResources().getColor(R.color.holo_red_light));
Alexandre Lision2e52d392013-11-06 15:14:31 -0500222
223 if (item.hasRecord()) {
224 entryView.record.setVisibility(View.VISIBLE);
225 entryView.record.setTag(R.id.history_call_record, true);
226 entryView.record.setOnClickListener(new OnClickListener() {
227
228 @Override
229 public void onClick(View v) {
230 try {
231 if ((Boolean) v.getTag(R.id.history_call_record)) {
232 mCallbacks.getService().startRecordedFilePlayback(item.getRecordPath());
233 v.setTag(R.id.replay, false);
234 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
235 } else {
236 mCallbacks.getService().stopRecordedFilePlayback(item.getRecordPath());
237 v.setTag(R.id.history_call_record, true);
238 ((Button) v).setText(getString(R.string.hist_replay_button));
239 }
240 } catch (RemoteException e) {
241 // TODO Auto-generated catch block
242 e.printStackTrace();
243 }
244 }
245 });
246 }
247
248 return convertView;
249 }
250
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500251 /**
252 * ******************
Alexandre Lision2e52d392013-11-06 15:14:31 -0500253 * ViewHolder Pattern
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500254 * *******************
255 */
Alexandre Lision2e52d392013-11-06 15:14:31 -0500256 public class HistoryCallView {
257 protected TextView historyCallState;
258 protected TextView formatted_date;
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500259 protected TextView formatted_hour;
Alexandre Lision2e52d392013-11-06 15:14:31 -0500260 protected Button record;
261 protected TextView duration;
262 }
263
264 }
265
266}