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