blob: 72b990a02693b25539f1db072bc5cca9a5613b66 [file] [log] [blame]
Alexandre Lision2e52d392013-11-06 15:14:31 -05001/*
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 */
31
32package org.sflphone.fragments;
33
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050034import java.io.InvalidObjectException;
Alexandre Lision2e52d392013-11-06 15:14:31 -050035import java.util.ArrayList;
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050036import java.util.HashMap;
37import java.util.List;
38import java.util.Map;
Alexandre Lision2e52d392013-11-06 15:14:31 -050039import java.util.NavigableMap;
40
41import org.sflphone.R;
42import org.sflphone.adapters.ContactPictureTask;
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050043import org.sflphone.model.Account;
Alexandre Lision2e52d392013-11-06 15:14:31 -050044import org.sflphone.model.HistoryEntry;
45import org.sflphone.model.HistoryEntry.HistoryCall;
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050046import org.sflphone.model.SipCall;
Alexandre Lision2e52d392013-11-06 15:14:31 -050047import org.sflphone.service.ISipService;
48import org.sflphone.views.parallaxscrollview.AnotherView;
Alexandre Lision2e52d392013-11-06 15:14:31 -050049
50import android.app.Activity;
51import android.app.Fragment;
52import android.content.Context;
53import android.os.Bundle;
54import android.os.RemoteException;
Alexandre Lision6da3bb92013-12-10 17:32:46 -050055import android.util.Log;
Alexandre Lision2e52d392013-11-06 15:14:31 -050056import android.view.LayoutInflater;
57import android.view.View;
58import android.view.View.MeasureSpec;
59import android.view.View.OnClickListener;
60import android.view.ViewGroup;
61import android.widget.BaseAdapter;
62import android.widget.Button;
63import android.widget.ImageView;
64import android.widget.LinearLayout;
65import android.widget.ListAdapter;
66import android.widget.ListView;
67import android.widget.RelativeLayout;
68import android.widget.TextView;
69
70public class DetailsHistoryEntryFragment extends Fragment {
71
Alexandre Lision2e52d392013-11-06 15:14:31 -050072 DetailHistoryAdapter mAdapter;
73 HistoryEntry toDisplay;
74 private static final String TAG = DetailsHistoryEntryFragment.class.getSimpleName();
75 ContactPictureTask tasker;
76
Alexandre Lision2e52d392013-11-06 15:14:31 -050077 private ListView lvMain;
78 private LinearLayout llMain, llMainHolder;
79 private AnotherView anotherView;
80 private RelativeLayout iv;
81
82 private Callbacks mCallbacks = sDummyCallbacks;
83
84 private static Callbacks sDummyCallbacks = new Callbacks() {
85
86 @Override
87 public ISipService getService() {
88 return null;
89 }
90
Alexandre Lision6da3bb92013-12-10 17:32:46 -050091 @Override
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050092 public void onCall(SipCall call) {
Alexandre Lision6da3bb92013-12-10 17:32:46 -050093 }
94
Alexandre Lision2e52d392013-11-06 15:14:31 -050095 };
96
97 public interface Callbacks {
98
99 public ISipService getService();
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500100
101 public void onCall(SipCall call);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500102
103 }
104
105 @Override
106 public void onAttach(Activity activity) {
107 super.onAttach(activity);
108
109 if (!(activity instanceof Callbacks)) {
110 throw new IllegalStateException("Activity must implement fragment's callbacks.");
111 }
112
113 mCallbacks = (Callbacks) activity;
114 }
115
116 @Override
117 public void onDetach() {
118 super.onDetach();
119 mCallbacks = sDummyCallbacks;
120 }
121
122 @Override
123 public void onCreate(Bundle savedInstanceState) {
124 super.onCreate(savedInstanceState);
125
126 toDisplay = (HistoryEntry) getArguments().get("entry");
127 mAdapter = new DetailHistoryAdapter(toDisplay.getCalls(), getActivity());
128 }
129
130 @Override
131 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
132 View inflatedView = inflater.inflate(R.layout.frag_history_detail, parent, false);
133
134 // mheaderView = LayoutInflater.from(getActivity()).inflate(R.layout.history_detail_header, null);
135
Alexandre Lision2e52d392013-11-06 15:14:31 -0500136 llMain = (LinearLayout) inflatedView.findViewById(R.id.llMain);
137 llMainHolder = (LinearLayout) inflatedView.findViewById(R.id.llMainHolder);
138 lvMain = (ListView) inflatedView.findViewById(R.id.lvMain);
139 lvMain.setAdapter(mAdapter);
140 iv = (RelativeLayout) inflatedView.findViewById(R.id.iv);
141
142 ((TextView) iv.findViewById(R.id.history_call_name)).setText(toDisplay.getContact().getmDisplayName());
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500143
144 tasker = new ContactPictureTask(getActivity(), (ImageView) iv.findViewById(R.id.contact_photo), toDisplay.getContact());
145 tasker.run();
146 anotherView = (AnotherView) inflatedView.findViewById(R.id.anotherView);
147
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500148 ((TextView) anotherView.findViewById(R.id.history_entry_number)).setText(getString(R.string.detail_hist_call_number, toDisplay.getNumber()));
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500149 ((RelativeLayout) anotherView.findViewById(R.id.call_main_action)).setOnClickListener(new OnClickListener() {
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500150
151 @Override
152 public void onClick(View v) {
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500153 try {
154 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
155
156 HashMap<String, String> details = (HashMap<String, String>) mCallbacks.getService().getAccountDetails(toDisplay.getAccountID());
157 ArrayList<HashMap<String, String>> creds;
158
159 creds = (ArrayList<HashMap<String, String>>) mCallbacks.getService().getCredentials(toDisplay.getAccountID());
160
161 callBuilder.startCallCreation().setAccount(new Account(toDisplay.getAccountID(), details, creds))
162 .setCallType(SipCall.state.CALL_TYPE_OUTGOING);
163 callBuilder.setContact(toDisplay.getContact());
164
165 mCallbacks.onCall(callBuilder.build());
166
167 } catch (RemoteException e) {
168 // TODO Bloc catch généré automatiquement
169 e.printStackTrace();
170 } catch (InvalidObjectException e) {
171 // TODO Bloc catch généré automatiquement
172 e.printStackTrace();
173 }
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500174 }
175 });
176
Alexandre Lision2e52d392013-11-06 15:14:31 -0500177 lvMain.post(new Runnable() {
178
179 @Override
180 public void run() {
181
182 // Adjusts llMain's height to match ListView's height
183 setListViewHeight(lvMain, llMain);
184
185 // LayoutParams to set the top margin of LinearLayout holding
186 // the content.
187 // topMargin = iv.getHeight() - tvTitle.getHeight()
188 LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) llMainHolder.getLayoutParams();
189 // p.topMargin = iv.getHeight() - tvTitle.getHeight();
190 llMainHolder.setLayoutParams(p);
191 }
192 });
193 return inflatedView;
194 }
195
196 public void onActivityCreated(Bundle savedInstanceState) {
197 super.onActivityCreated(savedInstanceState);
198
199 }
200
201 // Sets the ListView holder's height
202 public void setListViewHeight(ListView listView, LinearLayout llMain) {
203 ListAdapter listAdapter = listView.getAdapter();
204 if (listAdapter == null) {
205
206 return;
207 }
208
209 int totalHeight = 0;
210 int firstHeight = 0;
211 int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
212
213 for (int i = 0; i < listAdapter.getCount(); i++) {
214
215 if (i == 0) {
216 View listItem = listAdapter.getView(i, null, listView);
217 listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
218 firstHeight = listItem.getMeasuredHeight();
219 }
220 totalHeight += firstHeight;
221 }
222
Alexandre Lision4e458dc2013-11-12 10:07:40 -0500223 // totalHeight -= iv.getMeasuredHeight();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500224
225 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llMain.getLayoutParams();
226
227 params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
228 llMain.setLayoutParams(params);
229 anotherView.requestLayout();
230 }
231
232 private class DetailHistoryAdapter extends BaseAdapter implements ListAdapter {
233
234 ArrayList<HistoryCall> dataset;
235 Context mContext;
236
237 public DetailHistoryAdapter(NavigableMap<Long, HistoryCall> calls, Context c) {
238 dataset = new ArrayList<HistoryCall>(calls.descendingMap().values());
239 mContext = c;
240 }
241
242 @Override
243 public int getCount() {
244 return dataset.size();
245 }
246
247 @Override
248 public Object getItem(int position) {
249 return dataset.get(position);
250 }
251
252 @Override
253 public long getItemId(int position) {
254 return 0;
255 }
256
257 @Override
258 public View getView(int position, View convertView, ViewGroup parent) {
259 HistoryCallView entryView = null;
260
261 if (convertView == null) {
262 // Get a new instance of the row layout view
263 LayoutInflater inflater = LayoutInflater.from(mContext);
264 convertView = inflater.inflate(R.layout.item_history_call, null);
265
266 // Hold the view objects in an object
267 // so they don't need to be re-fetched
268 entryView = new HistoryCallView();
269 entryView.historyCallState = (TextView) convertView.findViewById(R.id.history_call_state);
270 entryView.formatted_date = (TextView) convertView.findViewById(R.id.history_call_date_formatted);
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500271 entryView.formatted_hour = (TextView) convertView.findViewById(R.id.history_call_hour);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500272 entryView.record = (Button) convertView.findViewById(R.id.history_call_record);
273 entryView.duration = (TextView) convertView.findViewById(R.id.history_call_duration);
274
275 convertView.setTag(entryView);
276 } else {
277 entryView = (HistoryCallView) convertView.getTag();
278 }
279
280 final HistoryCall item = dataset.get(position);
281
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500282 entryView.historyCallState.setText(item.getDirection());
Alexandre Lision2e52d392013-11-06 15:14:31 -0500283 entryView.formatted_date.setText(item.getDate());
Alexandre Lision2e52d392013-11-06 15:14:31 -0500284 entryView.duration.setText(item.getDurationString());
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500285 entryView.formatted_hour.setText(item.getStartString("h:mm a"));
286 if (item.isIncoming() && item.isMissed())
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500287 convertView.setBackgroundColor(getResources().getColor(R.color.holo_red_light));
Alexandre Lision2e52d392013-11-06 15:14:31 -0500288
289 if (item.hasRecord()) {
290 entryView.record.setVisibility(View.VISIBLE);
291 entryView.record.setTag(R.id.history_call_record, true);
292 entryView.record.setOnClickListener(new OnClickListener() {
293
294 @Override
295 public void onClick(View v) {
296 try {
297 if ((Boolean) v.getTag(R.id.history_call_record)) {
298 mCallbacks.getService().startRecordedFilePlayback(item.getRecordPath());
299 v.setTag(R.id.replay, false);
300 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
301 } else {
302 mCallbacks.getService().stopRecordedFilePlayback(item.getRecordPath());
303 v.setTag(R.id.history_call_record, true);
304 ((Button) v).setText(getString(R.string.hist_replay_button));
305 }
306 } catch (RemoteException e) {
307 // TODO Auto-generated catch block
308 e.printStackTrace();
309 }
310 }
311 });
312 }
313
314 return convertView;
315 }
316
317 /*********************
318 * ViewHolder Pattern
319 *********************/
320 public class HistoryCallView {
321 protected TextView historyCallState;
322 protected TextView formatted_date;
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500323 protected TextView formatted_hour;
Alexandre Lision2e52d392013-11-06 15:14:31 -0500324 protected Button record;
325 protected TextView duration;
326 }
327
328 }
329
330}