blob: 70bd65b7426f38c1a701a1bd66f81492b69c1b1f [file] [log] [blame]
Alexandre Savard4a19d752012-09-19 13:19:24 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
Alexandre Savarda1404652012-09-20 13:34:08 -04004 * Author: Alexandre Savard <alexandre.savard@gmail.com>
Alexandre Savard4a19d752012-09-19 13:19:24 -04005 *
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 */
31package com.savoirfairelinux.sflphone.client;
32
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040033import android.content.Context;
34import android.content.Intent;
35import android.os.Bundle;
Alexandre Savard85936b92012-10-24 11:23:53 -040036import android.os.Parcelable;
Alexandre Savard557a5742012-10-24 13:54:53 -040037import android.os.Parcel;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040038import android.os.RemoteException;
Alexandre Savard4a19d752012-09-19 13:19:24 -040039import android.util.Log;
Alexandre Savardd1976532012-10-26 15:32:45 -040040import android.view.View;
Alexandre Savard4a19d752012-09-19 13:19:24 -040041import java.util.ArrayList;
42
Alexandre Savard74c1cad2012-10-24 16:39:00 -040043import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040044import com.savoirfairelinux.sflphone.client.CallActivity;
Alexandre Savardd1976532012-10-26 15:32:45 -040045import com.savoirfairelinux.sflphone.client.CallElementList.CallElementView;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040046
Alexandre Savard557a5742012-10-24 13:54:53 -040047public class SipCall
Alexandre Savard4a19d752012-09-19 13:19:24 -040048{
49 final static String TAG = "SipCall";
Alexandre Savard7a46f542012-09-20 11:23:33 -040050 public CallInfo mCallInfo;
Alexandre Savarda949eec2012-10-25 17:30:49 -040051 // Update UI on actions (answer, hangup)
52 static private CallElementList mCallElementList = null;
53 static private SFLPhoneHome mHome = null;
Alexandre Savardd1976532012-10-26 15:32:45 -040054 private View mRowView = null;
Alexandre Savard7a46f542012-09-20 11:23:33 -040055
Alexandre Savard27a51132012-10-25 18:35:14 -040056 public static final int CALL_STATE_NONE = 0;
Alexandre Savarddf544262012-10-25 14:24:08 -040057 public static final int CALL_STATE_INCOMING = 1;
58 public static final int CALL_STATE_RINGING = 2;
59 public static final int CALL_STATE_CURRENT = 3;
60 public static final int CALL_STATE_HUNGUP = 4;
61 public static final int CALL_STATE_BUSY = 5;
62 public static final int CALL_STATE_FAILURE = 6;
63 public static final int CALL_STATE_HOLD = 7;
64 public static final int CALL_STATE_UNHOLD = 8;
Alexandre Savard2b01c822012-09-20 15:00:37 -040065
Alexandre Savarddf544262012-10-25 14:24:08 -040066 public static final int MEDIA_STATE_NONE = 0; // No media currently
67 public static final int MEDIA_STATE_ACTIVE = 1; // Media is active
68 public static final int MEDIA_STATE_LOCAL_HOLD = 2; // Media is put on hold bu user
69 public static final int MEDIA_STATE_REMOTE_HOLD = 3; // Media is put on hold by peer
70 public static final int MEDIA_STATE_ERROR = 5; // Media is in error state
Alexandre Savard2b01c822012-09-20 15:00:37 -040071
Alexandre Savard557a5742012-10-24 13:54:53 -040072 public static class CallInfo implements Parcelable
Alexandre Savard7a46f542012-09-20 11:23:33 -040073 {
Alexandre Savard2b01c822012-09-20 15:00:37 -040074 public String mCallID = "";
Alexandre Savard73bc56f2012-10-25 13:35:54 -040075 public String mAccountID = "";
Alexandre Savard7a46f542012-09-20 11:23:33 -040076 public String mDisplayName = "";
77 public String mPhone = "";
78 public String mEmail = "";
Alexandre Savard2b01c822012-09-20 15:00:37 -040079 public String mRemoteContact = "";
Alexandre Savard27a51132012-10-25 18:35:14 -040080 public int mCallState = CALL_STATE_NONE;
Alexandre Savard557a5742012-10-24 13:54:53 -040081 public int mMediaState = MEDIA_STATE_NONE;
82
83 @Override
84 public int describeContents() {
85 return 0;
86 }
87
88 @Override
89 public void writeToParcel(Parcel out, int flags) {
90 ArrayList<String> list = new ArrayList<String>();
Alexandre Savard74c1cad2012-10-24 16:39:00 -040091
92 // Don't mess with this order!!!
Alexandre Savard557a5742012-10-24 13:54:53 -040093 list.add(mCallID);
Alexandre Savard73bc56f2012-10-25 13:35:54 -040094 list.add(mAccountID);
Alexandre Savard557a5742012-10-24 13:54:53 -040095 list.add(mDisplayName);
96 list.add(mPhone);
97 list.add(mEmail);
98 list.add(mRemoteContact);
99
100 out.writeStringList(list);
101 out.writeInt(mCallState);
102 out.writeInt(mMediaState);
103 }
104
105 public static final Parcelable.Creator<CallInfo> CREATOR
106 = new Parcelable.Creator<CallInfo>() {
107 public CallInfo createFromParcel(Parcel in) {
108 return new CallInfo(in);
109 }
110
111 public CallInfo[] newArray(int size) {
112 return new CallInfo[size];
113 }
114 };
115
116 public CallInfo() {}
117
118 private CallInfo(Parcel in) {
119 ArrayList<String> list = in.createStringArrayList();
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400120
121 // Don't mess with this order!!!
Alexandre Savard557a5742012-10-24 13:54:53 -0400122 mCallID = list.get(0);
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400123 mAccountID = list.get(1);
124 mDisplayName = list.get(2);
125 mPhone = list.get(3);
126 mEmail = list.get(4);
127 mRemoteContact = list.get(5);
Alexandre Savard557a5742012-10-24 13:54:53 -0400128
129 mCallState = in.readInt();
130 mMediaState = in.readInt();
131 }
Alexandre Savard7a46f542012-09-20 11:23:33 -0400132 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400133
134 public SipCall()
135 {
Alexandre Savard7a46f542012-09-20 11:23:33 -0400136 mCallInfo = new CallInfo();
Alexandre Savard4a19d752012-09-19 13:19:24 -0400137 }
138
Alexandre Savard7a46f542012-09-20 11:23:33 -0400139 public SipCall(CallInfo info)
Alexandre Savard4a19d752012-09-19 13:19:24 -0400140 {
Alexandre Savard7a46f542012-09-20 11:23:33 -0400141 mCallInfo = info;
Alexandre Savard4a19d752012-09-19 13:19:24 -0400142 }
143
Alexandre Savarda1404652012-09-20 13:34:08 -0400144 public static void setCallElementList(CallElementList list)
145 {
146 mCallElementList = list;
147 }
148
Alexandre Savarda949eec2012-10-25 17:30:49 -0400149 public static void setSFLPhoneHomeContext(SFLPhoneHome home)
150 {
151 mHome = home;
152 }
153
Alexandre Savardd1976532012-10-26 15:32:45 -0400154 public void setAssociatedRowView(View view)
155 {
156 mRowView = view;
157 }
158
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400159 public void setCallID(String callID) {
160 mCallInfo.mCallID = callID;
161 }
162
163 public String getCallId() {
164 return mCallInfo.mCallID;
165 }
166
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400167 public void setAccountID(String accountID) {
168 mCallInfo.mAccountID = accountID;
169 }
170
171 public String getAccountID() {
172 return mCallInfo.mAccountID;
173 }
174
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400175 public void setDisplayName(String displayName) {
176 mCallInfo.mDisplayName = displayName;
177 }
178
179 public String getDisplayName() {
180 return mCallInfo.mDisplayName;
181 }
182
183 public void setPhone(String phone) {
184 mCallInfo.mPhone = phone;
185 }
186
187 public String getPhone() {
188 return mCallInfo.mPhone;
189 }
190
191 public void setEmail(String email) {
192 mCallInfo.mEmail = email;
193 }
194
195 public String getEmail() {
196 return mCallInfo.mEmail;
197 }
198
199 public void setRemoteContact(String remoteContact) {
200 mCallInfo.mRemoteContact = remoteContact;
201 }
202
203 public String getRemoteContact() {
204 return mCallInfo.mRemoteContact;
205 }
206
207 public void setCallState(int callState) {
208 mCallInfo.mCallState = callState;
Alexandre Savardde820072012-10-26 15:39:21 -0400209
210 // Check if this call is associated to a view in CallElementList
Alexandre Savardd1976532012-10-26 15:32:45 -0400211 if(mRowView == null)
212 return;
213
Alexandre Savardde820072012-10-26 15:39:21 -0400214 // Update the state to the view
Alexandre Savardd1976532012-10-26 15:32:45 -0400215 CallElementView entryView = (CallElementView) mRowView.getTag();
216 final String CURRENT_STATE_LABEL = " CURRENT STATE: ";
217 entryView.state.setText(CURRENT_STATE_LABEL + getCallStateString());
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400218 }
219
Alexandre Savarddf544262012-10-25 14:24:08 -0400220 public int getCallStateInt() {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400221 return mCallInfo.mCallState;
222 }
223
Alexandre Savarddf544262012-10-25 14:24:08 -0400224 public String getCallStateString() {
225 String state;
226
227 switch(mCallInfo.mCallState) {
228 case CALL_STATE_INCOMING:
229 state = "INCOMING";
230 break;
231 case CALL_STATE_RINGING:
232 state = "RINGING";
233 break;
234 case CALL_STATE_CURRENT:
235 state = "CURRENT";
236 break;
237 case CALL_STATE_HUNGUP:
238 state = "HUNGUP";
239 break;
240 case CALL_STATE_BUSY:
241 state = "BUSY";
242 break;
243 case CALL_STATE_FAILURE:
244 state = "FAILURE";
245 break;
246 case CALL_STATE_HOLD:
247 state = "HOLD";
248 break;
249 case CALL_STATE_UNHOLD:
250 state = "UNHOLD";
251 break;
252 default:
253 state = "NULL";
254 }
255
256 return state;
257 }
258
259
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400260 public void setMediaState(int mediaState) {
261 mCallInfo.mMediaState = mediaState;
262 }
263
264 public int getMediaState() {
265 return mCallInfo.mMediaState;
266 }
267
Alexandre Savarda949eec2012-10-25 17:30:49 -0400268 public void placeCallUpdateUi()
269 {
270 if(mCallElementList != null)
271 mCallElementList.addCall(this);
272
273 if(mHome != null)
274 mHome.onSelectedCallAction(this);
275 }
276
Alexandre Savard27a51132012-10-25 18:35:14 -0400277 public void notifyServicePlaceCall(ISipService service)
278 {
279 try {
280 service.placeCall(mCallInfo.mAccountID, mCallInfo.mCallID, mCallInfo.mPhone);
281 } catch (RemoteException e) {
282 Log.e(TAG, "Cannot call service method", e);
283 }
284 }
285
Alexandre Savarda949eec2012-10-25 17:30:49 -0400286 public void receiveCallUpdateUi()
Alexandre Savard23240c12012-09-19 18:23:44 -0400287 {
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400288 if(mCallElementList != null)
289 mCallElementList.addCall(this);
Alexandre Savarda949eec2012-10-25 17:30:49 -0400290
291 if(mHome != null)
292 mHome.onSelectedCallAction(this);
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400293 }
294
Alexandre Savarda949eec2012-10-25 17:30:49 -0400295 public void answerUpdateUi()
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400296 {
Alexandre Savarda949eec2012-10-25 17:30:49 -0400297 if(mHome != null)
298 mHome.onSelectedCallAction(this);
Alexandre Savardf17e3172012-10-25 16:09:09 -0400299
300 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400301
Alexandre Savardf17e3172012-10-25 16:09:09 -0400302 public void notifyServiceAnswer(ISipService service)
303 {
Alexandre Savard27a51132012-10-25 18:35:14 -0400304 int callState = getCallStateInt();
305 if((callState != CALL_STATE_RINGING) &&
306 (callState != CALL_STATE_NONE)) {
Alexandre Savardf17e3172012-10-25 16:09:09 -0400307 return;
Alexandre Savard27a51132012-10-25 18:35:14 -0400308 }
Alexandre Savardf17e3172012-10-25 16:09:09 -0400309
310 try {
311 service.accept(mCallInfo.mCallID);
312 } catch (RemoteException e) {
313 Log.e(TAG, "Cannot call service method", e);
314 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400315 }
316
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400317 /**
318 * Perform hangup action without sending request to the service
Alexandre Savarda949eec2012-10-25 17:30:49 -0400319 * Used when SipService haved been notified that this call hung up
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400320 */
Alexandre Savarda949eec2012-10-25 17:30:49 -0400321 public void hangupUpdateUi() {
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400322 Log.i(TAG, "Hangup call " + mCallInfo.mCallID);
323
324 if(mCallElementList != null)
325 mCallElementList.removeCall(this);
Alexandre Savarda949eec2012-10-25 17:30:49 -0400326
327 if(mHome != null)
328 mHome.onUnselectedCallAction();
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400329 }
330
331 /**
332 * Perform hangup action and send request to the service
333 */
334 public void notifyServiceHangup(ISipService service)
335 {
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400336 try {
337 service.hangUp(mCallInfo.mCallID);
338 } catch (RemoteException e) {
339 Log.e(TAG, "Cannot call service method", e);
340 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400341 }
Alexandre Savardaef9d802012-09-20 17:31:32 -0400342
Alexandre Savard83869852012-10-26 09:14:12 -0400343 public void notifyServiceRefuse(ISipService service)
344 {
345 try {
346 service.refuse(mCallInfo.mCallID);
347 } catch (RemoteException e) {
348 Log.e(TAG, "Cannot call service method", e);
349 }
350 }
351
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400352 public void notifyServiceHold(ISipService service)
353 {
354 try {
355 service.hold(mCallInfo.mCallID);
356 } catch (RemoteException e) {
357 Log.e(TAG, "Cannot call service method", e);
358 }
359 }
360
361 public void notifyServiceUnhold(ISipService service)
362 {
363 try {
364 service.unhold(mCallInfo.mCallID);
365 } catch (RemoteException e) {
366 Log.e(TAG, "Cannot call service method", e);
367 }
368 }
Alexandre Savard83869852012-10-26 09:14:12 -0400369
Alexandre Savardaef9d802012-09-20 17:31:32 -0400370 public void addToConference()
371 {
372 Log.i(TAG, "Add call to conference");
373 }
374
375 public void sendTextMessage()
376 {
377 Log.i(TAG, "Send text message");
378 }
Alexandre Savard3bdce7b2012-10-24 18:27:45 -0400379
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400380 public void printCallInfo()
381 {
382 Log.i(TAG, "CallInfo: CallID: " + mCallInfo.mCallID);
383 Log.i(TAG, " AccountID: " + mCallInfo.mAccountID);
384 Log.i(TAG, " Display Name: " + mCallInfo.mDisplayName);
385 Log.i(TAG, " Phone: " + mCallInfo.mPhone);
386 Log.i(TAG, " Email: " + mCallInfo.mEmail);
387 Log.i(TAG, " Contact: " + mCallInfo.mRemoteContact);
388 }
389
Alexandre Savard3bdce7b2012-10-24 18:27:45 -0400390 public void launchCallActivity(Context context)
391 {
392 Log.i(TAG, "Launch Call Activity");
393 Bundle bundle = new Bundle();
394 bundle.putParcelable("CallInfo", mCallInfo);
395 Intent intent = new Intent().setClass(context, CallActivity.class);
396 intent.putExtras(bundle);
397 context.startActivity(intent);
398 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400399}