blob: ded02b2a2310a5dd8e43dfbfd468e50e77cb6307 [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 Lisiona7ab2e32014-02-14 15:33:33 -050057import java.util.Random;
Alexandre Lision2e52d392013-11-06 15:14:31 -050058
59public class DetailsHistoryEntryFragment extends Fragment {
60
Alexandre Lision2e52d392013-11-06 15:14:31 -050061 DetailHistoryAdapter mAdapter;
62 HistoryEntry toDisplay;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050063 @SuppressWarnings("unused")
Alexandre Lision2e52d392013-11-06 15:14:31 -050064 private static final String TAG = DetailsHistoryEntryFragment.class.getSimpleName();
65 ContactPictureTask tasker;
66
Alexandre Lision2e52d392013-11-06 15:14:31 -050067 private ListView lvMain;
Alexandre Lisiondede80e2014-01-29 09:51:07 -050068 private LinearLayout llMain;
Alexandre Lision2e52d392013-11-06 15:14:31 -050069 private RelativeLayout iv;
70
71 private Callbacks mCallbacks = sDummyCallbacks;
72
73 private static Callbacks sDummyCallbacks = new Callbacks() {
74
75 @Override
76 public ISipService getService() {
77 return null;
78 }
79
Alexandre Lision6da3bb92013-12-10 17:32:46 -050080 @Override
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050081 public void onCall(SipCall call) {
Alexandre Lision6da3bb92013-12-10 17:32:46 -050082 }
83
Alexandre Lision2e52d392013-11-06 15:14:31 -050084 };
85
86 public interface Callbacks {
87
88 public ISipService getService();
Alexandre Lisiondede80e2014-01-29 09:51:07 -050089
Alexandre Lisionddbb46f2013-12-12 12:18:58 -050090 public void onCall(SipCall call);
Alexandre Lision2e52d392013-11-06 15:14:31 -050091
92 }
93
94 @Override
95 public void onAttach(Activity activity) {
96 super.onAttach(activity);
97
98 if (!(activity instanceof Callbacks)) {
99 throw new IllegalStateException("Activity must implement fragment's callbacks.");
100 }
101
102 mCallbacks = (Callbacks) activity;
103 }
104
105 @Override
106 public void onDetach() {
107 super.onDetach();
108 mCallbacks = sDummyCallbacks;
109 }
110
111 @Override
112 public void onCreate(Bundle savedInstanceState) {
113 super.onCreate(savedInstanceState);
114
115 toDisplay = (HistoryEntry) getArguments().get("entry");
116 mAdapter = new DetailHistoryAdapter(toDisplay.getCalls(), getActivity());
117 }
118
119 @Override
120 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
121 View inflatedView = inflater.inflate(R.layout.frag_history_detail, parent, false);
122
Alexandre Lision2e52d392013-11-06 15:14:31 -0500123 llMain = (LinearLayout) inflatedView.findViewById(R.id.llMain);
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500124 /*llMainHolder = (LinearLayout) inflatedView.findViewById(R.id.llMainHolder);*/
Alexandre Lision2e52d392013-11-06 15:14:31 -0500125 lvMain = (ListView) inflatedView.findViewById(R.id.lvMain);
126 lvMain.setAdapter(mAdapter);
127 iv = (RelativeLayout) inflatedView.findViewById(R.id.iv);
128
129 ((TextView) iv.findViewById(R.id.history_call_name)).setText(toDisplay.getContact().getmDisplayName());
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500130
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500131 tasker = new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.contact_photo), toDisplay.getContact());
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500132 tasker.run();
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500133// ((TextView) iv.findViewById(R.id.history_entry_number)).setText(getString(R.string.detail_hist_call_number, toDisplay.getNumber()));
134 iv.findViewById(R.id.history_call_name).setOnClickListener(new OnClickListener() {
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500135
136 @Override
137 public void onClick(View v) {
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500138 try {
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500139 HashMap<String, String> details = (HashMap<String, String>) mCallbacks.getService().getAccountDetails(toDisplay.getAccountID());
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500140 ArrayList<HashMap<String, String>> creds = (ArrayList<HashMap<String, String>>) mCallbacks.getService().getCredentials(toDisplay.getAccountID());
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500141 Bundle args = new Bundle();
142 args.putString(SipCall.ID, Integer.toString(Math.abs(new Random().nextInt())));
143 args.putParcelable(SipCall.ACCOUNT, new Account(toDisplay.getAccountID(), details, creds));
144 args.putInt(SipCall.STATE, SipCall.state.CALL_STATE_RINGING);
145 args.putInt(SipCall.TYPE, SipCall.direction.CALL_TYPE_OUTGOING);
146 args.putParcelable(SipCall.CONTACT, toDisplay.getContact());
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500147
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500148 mCallbacks.onCall(new SipCall(args));
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500149
150 } catch (RemoteException e) {
151 // TODO Bloc catch généré automatiquement
152 e.printStackTrace();
Alexandre Lisionddbb46f2013-12-12 12:18:58 -0500153 }
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500154 }
155 });
Alexandre Lision2e52d392013-11-06 15:14:31 -0500156 return inflatedView;
157 }
158
159 public void onActivityCreated(Bundle savedInstanceState) {
160 super.onActivityCreated(savedInstanceState);
161
162 }
163
Alexandre Lision2e52d392013-11-06 15:14:31 -0500164 private class DetailHistoryAdapter extends BaseAdapter implements ListAdapter {
165
166 ArrayList<HistoryCall> dataset;
167 Context mContext;
168
169 public DetailHistoryAdapter(NavigableMap<Long, HistoryCall> calls, Context c) {
170 dataset = new ArrayList<HistoryCall>(calls.descendingMap().values());
171 mContext = c;
172 }
173
174 @Override
175 public int getCount() {
176 return dataset.size();
177 }
178
179 @Override
180 public Object getItem(int position) {
181 return dataset.get(position);
182 }
183
184 @Override
185 public long getItemId(int position) {
186 return 0;
187 }
188
189 @Override
190 public View getView(int position, View convertView, ViewGroup parent) {
191 HistoryCallView entryView = null;
192
193 if (convertView == null) {
194 // Get a new instance of the row layout view
195 LayoutInflater inflater = LayoutInflater.from(mContext);
196 convertView = inflater.inflate(R.layout.item_history_call, null);
197
198 // Hold the view objects in an object
199 // so they don't need to be re-fetched
200 entryView = new HistoryCallView();
201 entryView.historyCallState = (TextView) convertView.findViewById(R.id.history_call_state);
202 entryView.formatted_date = (TextView) convertView.findViewById(R.id.history_call_date_formatted);
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500203 entryView.formatted_hour = (TextView) convertView.findViewById(R.id.history_call_hour);
Alexandre Lision2e52d392013-11-06 15:14:31 -0500204 entryView.record = (Button) convertView.findViewById(R.id.history_call_record);
205 entryView.duration = (TextView) convertView.findViewById(R.id.history_call_duration);
206
207 convertView.setTag(entryView);
208 } else {
209 entryView = (HistoryCallView) convertView.getTag();
210 }
211
212 final HistoryCall item = dataset.get(position);
213
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500214 entryView.historyCallState.setText(item.getDirection());
Alexandre Lision2e52d392013-11-06 15:14:31 -0500215 entryView.formatted_date.setText(item.getDate());
Alexandre Lision2e52d392013-11-06 15:14:31 -0500216 entryView.duration.setText(item.getDurationString());
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500217 entryView.formatted_hour.setText(item.getStartString("h:mm a"));
218 if (item.isIncoming() && item.isMissed())
Alexandre Lisioncb2345c2013-12-09 15:39:13 -0500219 convertView.setBackgroundColor(getResources().getColor(R.color.holo_red_light));
Alexandre Lision2e52d392013-11-06 15:14:31 -0500220
221 if (item.hasRecord()) {
222 entryView.record.setVisibility(View.VISIBLE);
223 entryView.record.setTag(R.id.history_call_record, true);
224 entryView.record.setOnClickListener(new OnClickListener() {
225
226 @Override
227 public void onClick(View v) {
228 try {
229 if ((Boolean) v.getTag(R.id.history_call_record)) {
230 mCallbacks.getService().startRecordedFilePlayback(item.getRecordPath());
231 v.setTag(R.id.replay, false);
232 ((Button) v).setText(getString(R.string.hist_replay_button_stop));
233 } else {
234 mCallbacks.getService().stopRecordedFilePlayback(item.getRecordPath());
235 v.setTag(R.id.history_call_record, true);
236 ((Button) v).setText(getString(R.string.hist_replay_button));
237 }
238 } catch (RemoteException e) {
239 // TODO Auto-generated catch block
240 e.printStackTrace();
241 }
242 }
243 });
244 }
245
246 return convertView;
247 }
248
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500249 /**
250 * ******************
Alexandre Lision2e52d392013-11-06 15:14:31 -0500251 * ViewHolder Pattern
Alexandre Lisiondede80e2014-01-29 09:51:07 -0500252 * *******************
253 */
Alexandre Lision2e52d392013-11-06 15:14:31 -0500254 public class HistoryCallView {
255 protected TextView historyCallState;
256 protected TextView formatted_date;
Alexandre Lision6da3bb92013-12-10 17:32:46 -0500257 protected TextView formatted_hour;
Alexandre Lision2e52d392013-11-06 15:14:31 -0500258 protected Button record;
259 protected TextView duration;
260 }
261
262 }
263
264}