blob: 7f03e3f8ff38e06fe4a4cdc05eab3ec745fa452e [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
34import java.util.ArrayList;
35import java.util.NavigableMap;
36
37import org.sflphone.R;
38import org.sflphone.adapters.ContactPictureTask;
39import org.sflphone.model.HistoryEntry;
40import org.sflphone.model.HistoryEntry.HistoryCall;
41import org.sflphone.service.ISipService;
42import org.sflphone.views.parallaxscrollview.AnotherView;
Alexandre Lision2e52d392013-11-06 15:14:31 -050043
44import android.app.Activity;
45import android.app.Fragment;
46import android.content.Context;
47import android.os.Bundle;
48import android.os.RemoteException;
Alexandre Lision6da3bb92013-12-10 17:32:46 -050049import android.util.Log;
Alexandre Lision2e52d392013-11-06 15:14:31 -050050import android.view.LayoutInflater;
51import android.view.View;
52import android.view.View.MeasureSpec;
53import android.view.View.OnClickListener;
54import android.view.ViewGroup;
55import android.widget.BaseAdapter;
56import android.widget.Button;
57import android.widget.ImageView;
58import android.widget.LinearLayout;
59import android.widget.ListAdapter;
60import android.widget.ListView;
61import android.widget.RelativeLayout;
62import android.widget.TextView;
63
64public class DetailsHistoryEntryFragment extends Fragment {
65
66 View mheaderView;
67 DetailHistoryAdapter mAdapter;
68 HistoryEntry toDisplay;
69 private static final String TAG = DetailsHistoryEntryFragment.class.getSimpleName();
70 ContactPictureTask tasker;
71
Alexandre Lision2e52d392013-11-06 15:14:31 -050072 private ListView lvMain;
73 private LinearLayout llMain, llMainHolder;
74 private AnotherView anotherView;
75 private RelativeLayout iv;
76
77 private Callbacks mCallbacks = sDummyCallbacks;
78
79 private static Callbacks sDummyCallbacks = new Callbacks() {
80
81 @Override
82 public ISipService getService() {
83 return null;
84 }
85
Alexandre Lision6da3bb92013-12-10 17:32:46 -050086 @Override
87 public void onCallDialed(String to) {
88 }
89
Alexandre Lision2e52d392013-11-06 15:14:31 -050090 };
91
92 public interface Callbacks {
93
Alexandre Lision6da3bb92013-12-10 17:32:46 -050094 public void onCallDialed(String to);
95
Alexandre Lision2e52d392013-11-06 15:14:31 -050096 public ISipService getService();
97
98 }
99
100 @Override
101 public void onAttach(Activity activity) {
102 super.onAttach(activity);
103
104 if (!(activity instanceof Callbacks)) {
105 throw new IllegalStateException("Activity must implement fragment's callbacks.");
106 }
107
108 mCallbacks = (Callbacks) activity;
109 }
110
111 @Override
112 public void onDetach() {
113 super.onDetach();
114 mCallbacks = sDummyCallbacks;
115 }
116
117 @Override
118 public void onCreate(Bundle savedInstanceState) {
119 super.onCreate(savedInstanceState);
120
121 toDisplay = (HistoryEntry) getArguments().get("entry");
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500122
123 Log.i(TAG, "Ok, we got " + toDisplay.getIncoming_sum() + " incoming calls");
124 Log.i(TAG, "Ok, we got " + toDisplay.getMissed_sum() + " missed calls");
125 Log.i(TAG, "Ok, we got " + toDisplay.getOutgoing_sum() + " outgoing calls");
126
Alexandre Lision2e52d392013-11-06 15:14:31 -0500127 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
148 ((TextView) anotherView.findViewById(R.id.history_entry_number)).setText(toDisplay.getNumber());
149 ((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) {
153 Log.i(TAG, "Clicking");
154 mCallbacks.onCallDialed(toDisplay.getNumber());
155 }
156 });
157
Alexandre Lision2e52d392013-11-06 15:14:31 -0500158 lvMain.post(new Runnable() {
159
160 @Override
161 public void run() {
162
163 // Adjusts llMain's height to match ListView's height
164 setListViewHeight(lvMain, llMain);
165
166 // LayoutParams to set the top margin of LinearLayout holding
167 // the content.
168 // topMargin = iv.getHeight() - tvTitle.getHeight()
169 LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) llMainHolder.getLayoutParams();
170 // p.topMargin = iv.getHeight() - tvTitle.getHeight();
171 llMainHolder.setLayoutParams(p);
172 }
173 });
174 return inflatedView;
175 }
176
177 public void onActivityCreated(Bundle savedInstanceState) {
178 super.onActivityCreated(savedInstanceState);
179
180 }
181
182 // Sets the ListView holder's height
183 public void setListViewHeight(ListView listView, LinearLayout llMain) {
184 ListAdapter listAdapter = listView.getAdapter();
185 if (listAdapter == null) {
186
187 return;
188 }
189
190 int totalHeight = 0;
191 int firstHeight = 0;
192 int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
193
194 for (int i = 0; i < listAdapter.getCount(); i++) {
195
196 if (i == 0) {
197 View listItem = listAdapter.getView(i, null, listView);
198 listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
199 firstHeight = listItem.getMeasuredHeight();
200 }
201 totalHeight += firstHeight;
202 }
203
Alexandre Lision4e458dc2013-11-12 10:07:40 -0500204 // totalHeight -= iv.getMeasuredHeight();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500205
206 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llMain.getLayoutParams();
207
208 params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
209 llMain.setLayoutParams(params);
210 anotherView.requestLayout();
211 }
212
213 private class DetailHistoryAdapter extends BaseAdapter implements ListAdapter {
214
215 ArrayList<HistoryCall> dataset;
216 Context mContext;
217
218 public DetailHistoryAdapter(NavigableMap<Long, HistoryCall> calls, Context c) {
219 dataset = new ArrayList<HistoryCall>(calls.descendingMap().values());
220 mContext = c;
221 }
222
223 @Override
224 public int getCount() {
225 return dataset.size();
226 }
227
228 @Override
229 public Object getItem(int position) {
230 return dataset.get(position);
231 }
232
233 @Override
234 public long getItemId(int position) {
235 return 0;
236 }
237
238 @Override
239 public View getView(int position, View convertView, ViewGroup parent) {
240 HistoryCallView entryView = null;
241
242 if (convertView == null) {
243 // Get a new instance of the row layout view
244 LayoutInflater inflater = LayoutInflater.from(mContext);
245 convertView = inflater.inflate(R.layout.item_history_call, null);
246
247 // Hold the view objects in an object
248 // so they don't need to be re-fetched
249 entryView = new HistoryCallView();
250 entryView.historyCallState = (TextView) convertView.findViewById(R.id.history_call_state);
251 entryView.formatted_date = (TextView) convertView.findViewById(R.id.history_call_date_formatted);
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500252 entryView.formatted_hour = (TextView) convertView.findViewById(R.id.history_call_hour);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500253 entryView.record = (Button) convertView.findViewById(R.id.history_call_record);
254 entryView.duration = (TextView) convertView.findViewById(R.id.history_call_duration);
255
256 convertView.setTag(entryView);
257 } else {
258 entryView = (HistoryCallView) convertView.getTag();
259 }
260
261 final HistoryCall item = dataset.get(position);
262
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500263 entryView.historyCallState.setText(item.getDirection());
Alexandre Lision2e52d392013-11-06 15:14:31 -0500264 entryView.formatted_date.setText(item.getDate());
Alexandre Lision2e52d392013-11-06 15:14:31 -0500265 entryView.duration.setText(item.getDurationString());
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500266 entryView.formatted_hour.setText(item.getStartString("h:mm a"));
267 if (item.isIncoming() && item.isMissed())
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500268 convertView.setBackgroundColor(getResources().getColor(R.color.holo_red_light));
Alexandre Lision2e52d392013-11-06 15:14:31 -0500269
270 if (item.hasRecord()) {
271 entryView.record.setVisibility(View.VISIBLE);
272 entryView.record.setTag(R.id.history_call_record, true);
273 entryView.record.setOnClickListener(new OnClickListener() {
274
275 @Override
276 public void onClick(View v) {
277 try {
278 if ((Boolean) v.getTag(R.id.history_call_record)) {
279 mCallbacks.getService().startRecordedFilePlayback(item.getRecordPath());
280 v.setTag(R.id.replay, false);
281 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
282 } else {
283 mCallbacks.getService().stopRecordedFilePlayback(item.getRecordPath());
284 v.setTag(R.id.history_call_record, true);
285 ((Button) v).setText(getString(R.string.hist_replay_button));
286 }
287 } catch (RemoteException e) {
288 // TODO Auto-generated catch block
289 e.printStackTrace();
290 }
291 }
292 });
293 }
294
295 return convertView;
296 }
297
298 /*********************
299 * ViewHolder Pattern
300 *********************/
301 public class HistoryCallView {
302 protected TextView historyCallState;
303 protected TextView formatted_date;
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500304 protected TextView formatted_hour;
Alexandre Lision2e52d392013-11-06 15:14:31 -0500305 protected Button record;
306 protected TextView duration;
307 }
308
309 }
310
311}