blob: fcd826e9d5cf7d419612bb2bd5384f8b090e3faa [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 */
alisionf76de3b2013-04-16 15:35:22 -040031package com.savoirfairelinux.sflphone.model;
Alexandre Savard4a19d752012-09-19 13:19:24 -040032
alisiona4325152013-04-19 11:10:03 -040033import java.util.ArrayList;
34
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040035import android.content.Intent;
36import android.os.Bundle;
alision04a00182013-05-10 17:05:29 -040037import android.os.Environment;
Alexandre Savard557a5742012-10-24 13:54:53 -040038import android.os.Parcel;
alisiona4325152013-04-19 11:10:03 -040039import android.os.Parcelable;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040040import android.os.RemoteException;
Alexandre Savard4a19d752012-09-19 13:19:24 -040041import android.util.Log;
Alexandre Savard4a19d752012-09-19 13:19:24 -040042
alisiona4325152013-04-19 11:10:03 -040043import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040044
Alexandre Savard557a5742012-10-24 13:54:53 -040045public class SipCall
Alexandre Savard4a19d752012-09-19 13:19:24 -040046{
47 final static String TAG = "SipCall";
Alexandre Savard7a46f542012-09-20 11:23:33 -040048 public CallInfo mCallInfo;
alision371b77e2013-04-23 14:51:26 -040049
Alexandre Savard7a46f542012-09-20 11:23:33 -040050
Alexandre Savard2f1ae542012-10-26 17:05:00 -040051 public static final int CALL_TYPE_UNDETERMINED = 0;
52 public static final int CALL_TYPE_INCOMING = 1;
53 public static final int CALL_TYPE_OUTGOING = 2;
54
Alexandre Savard27a51132012-10-25 18:35:14 -040055 public static final int CALL_STATE_NONE = 0;
Alexandre Savarddf544262012-10-25 14:24:08 -040056 public static final int CALL_STATE_INCOMING = 1;
57 public static final int CALL_STATE_RINGING = 2;
58 public static final int CALL_STATE_CURRENT = 3;
59 public static final int CALL_STATE_HUNGUP = 4;
60 public static final int CALL_STATE_BUSY = 5;
61 public static final int CALL_STATE_FAILURE = 6;
62 public static final int CALL_STATE_HOLD = 7;
63 public static final int CALL_STATE_UNHOLD = 8;
Alexandre Savard2b01c822012-09-20 15:00:37 -040064
Alexandre Savarddf544262012-10-25 14:24:08 -040065 public static final int MEDIA_STATE_NONE = 0; // No media currently
66 public static final int MEDIA_STATE_ACTIVE = 1; // Media is active
67 public static final int MEDIA_STATE_LOCAL_HOLD = 2; // Media is put on hold bu user
68 public static final int MEDIA_STATE_REMOTE_HOLD = 3; // Media is put on hold by peer
69 public static final int MEDIA_STATE_ERROR = 5; // Media is in error state
Alexandre Savard2b01c822012-09-20 15:00:37 -040070
Alexandre Savard557a5742012-10-24 13:54:53 -040071 public static class CallInfo implements Parcelable
Alexandre Savard7a46f542012-09-20 11:23:33 -040072 {
Alexandre Savard2b01c822012-09-20 15:00:37 -040073 public String mCallID = "";
Alexandre Savard73bc56f2012-10-25 13:35:54 -040074 public String mAccountID = "";
Alexandre Savard7a46f542012-09-20 11:23:33 -040075 public String mDisplayName = "";
76 public String mPhone = "";
77 public String mEmail = "";
Alexandre Savard2b01c822012-09-20 15:00:37 -040078 public String mRemoteContact = "";
Alexandre Savard2f1ae542012-10-26 17:05:00 -040079 public int mCallType = CALL_TYPE_UNDETERMINED;
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);
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400101 out.writeInt(mCallType);
Alexandre Savard557a5742012-10-24 13:54:53 -0400102 out.writeInt(mCallState);
103 out.writeInt(mMediaState);
104 }
105
Adrien Béraudea28cd52013-04-26 17:40:46 +1000106 public static final Parcelable.Creator<CallInfo> CREATOR = new Parcelable.Creator<CallInfo>() {
Alexandre Savard557a5742012-10-24 13:54:53 -0400107 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
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400129 mCallType = in.readInt();
Alexandre Savard557a5742012-10-24 13:54:53 -0400130 mCallState = in.readInt();
131 mMediaState = in.readInt();
132 }
alision371b77e2013-04-23 14:51:26 -0400133
134 public CallInfo(Intent call) {
135 Bundle b = call.getBundleExtra("com.savoirfairelinux.sflphone.service.newcall");
136 mAccountID = b.getString("AccountID");
137 mCallID = b.getString("CallID");
138 mDisplayName = b.getString("From");
139 }
Alexandre Savard7a46f542012-09-20 11:23:33 -0400140 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400141
142 public SipCall()
143 {
Alexandre Savard7a46f542012-09-20 11:23:33 -0400144 mCallInfo = new CallInfo();
Alexandre Savard4a19d752012-09-19 13:19:24 -0400145 }
146
Alexandre Savard7a46f542012-09-20 11:23:33 -0400147 public SipCall(CallInfo info)
Alexandre Savard4a19d752012-09-19 13:19:24 -0400148 {
Alexandre Savard7a46f542012-09-20 11:23:33 -0400149 mCallInfo = info;
Alexandre Savard4a19d752012-09-19 13:19:24 -0400150 }
151
Alexandre Savarda1404652012-09-20 13:34:08 -0400152
Alexandre Savardd1976532012-10-26 15:32:45 -0400153
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400154 public void setCallID(String callID) {
155 mCallInfo.mCallID = callID;
156 }
157
158 public String getCallId() {
159 return mCallInfo.mCallID;
160 }
161
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400162 public void setAccountID(String accountID) {
163 mCallInfo.mAccountID = accountID;
164 }
165
166 public String getAccountID() {
167 return mCallInfo.mAccountID;
168 }
169
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400170 public void setDisplayName(String displayName) {
171 mCallInfo.mDisplayName = displayName;
172 }
173
174 public String getDisplayName() {
175 return mCallInfo.mDisplayName;
176 }
177
178 public void setPhone(String phone) {
179 mCallInfo.mPhone = phone;
180 }
181
182 public String getPhone() {
183 return mCallInfo.mPhone;
184 }
185
186 public void setEmail(String email) {
187 mCallInfo.mEmail = email;
188 }
189
190 public String getEmail() {
191 return mCallInfo.mEmail;
192 }
193
194 public void setRemoteContact(String remoteContact) {
195 mCallInfo.mRemoteContact = remoteContact;
196 }
197
198 public String getRemoteContact() {
199 return mCallInfo.mRemoteContact;
200 }
201
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400202 public void setCallType(int callType) {
203 mCallInfo.mCallType = callType;
204 }
205
206 public int getCallType() {
207 return mCallInfo.mCallType;
208 }
209
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400210 public void setCallState(int callState) {
211 mCallInfo.mCallState = callState;
212 }
213
Alexandre Savarddf544262012-10-25 14:24:08 -0400214 public int getCallStateInt() {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400215 return mCallInfo.mCallState;
216 }
217
Alexandre Savarddf544262012-10-25 14:24:08 -0400218 public String getCallStateString() {
219 String state;
220
221 switch(mCallInfo.mCallState) {
222 case CALL_STATE_INCOMING:
223 state = "INCOMING";
224 break;
225 case CALL_STATE_RINGING:
226 state = "RINGING";
227 break;
228 case CALL_STATE_CURRENT:
229 state = "CURRENT";
230 break;
231 case CALL_STATE_HUNGUP:
232 state = "HUNGUP";
233 break;
234 case CALL_STATE_BUSY:
235 state = "BUSY";
236 break;
237 case CALL_STATE_FAILURE:
238 state = "FAILURE";
239 break;
240 case CALL_STATE_HOLD:
241 state = "HOLD";
242 break;
243 case CALL_STATE_UNHOLD:
244 state = "UNHOLD";
245 break;
246 default:
247 state = "NULL";
248 }
249
250 return state;
251 }
252
253
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400254 public void setMediaState(int mediaState) {
255 mCallInfo.mMediaState = mediaState;
256 }
257
258 public int getMediaState() {
259 return mCallInfo.mMediaState;
260 }
261
alision371b77e2013-04-23 14:51:26 -0400262
Alexandre Savard4a19d752012-09-19 13:19:24 -0400263
Alexandre Savard08545ae2012-10-26 16:11:42 -0400264 public boolean notifyServiceAnswer(ISipService service)
Alexandre Savardf17e3172012-10-25 16:09:09 -0400265 {
Alexandre Savard27a51132012-10-25 18:35:14 -0400266 int callState = getCallStateInt();
267 if((callState != CALL_STATE_RINGING) &&
268 (callState != CALL_STATE_NONE)) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400269 return false;
Alexandre Savard27a51132012-10-25 18:35:14 -0400270 }
Alexandre Savardf17e3172012-10-25 16:09:09 -0400271
272 try {
273 service.accept(mCallInfo.mCallID);
274 } catch (RemoteException e) {
275 Log.e(TAG, "Cannot call service method", e);
276 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400277
278 return true;
Alexandre Savard4a19d752012-09-19 13:19:24 -0400279 }
280
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400281 /**
282 * Perform hangup action without sending request to the service
Alexandre Savarda949eec2012-10-25 17:30:49 -0400283 * Used when SipService haved been notified that this call hung up
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400284 */
alision371b77e2013-04-23 14:51:26 -0400285// public void hangupUpdateUi() {
286// Log.i(TAG, "Hangup call " + mCallInfo.mCallID);
287//
288// if(mCallElementList != null)
289// mCallElementList.removeCall(this);
290//
291// if(mHome != null)
292// mHome.onUnselectedCallAction();
293// }
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400294
295 /**
296 * Perform hangup action and send request to the service
297 */
Alexandre Savard08545ae2012-10-26 16:11:42 -0400298 public boolean notifyServiceHangup(ISipService service)
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400299 {
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400300 try {
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400301 if((getCallStateInt() == CALL_STATE_NONE) ||
302 (getCallStateInt() == CALL_STATE_CURRENT) ||
303 (getCallStateInt() == CALL_STATE_HOLD)) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400304 service.hangUp(mCallInfo.mCallID);
305 return true;
306
307 }
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400308 else if(getCallStateInt() == CALL_STATE_RINGING) {
309 if(getCallType() == CALL_TYPE_INCOMING) {
310 service.refuse(mCallInfo.mCallID);
311 return true;
312 }
313 else if(getCallType() == CALL_TYPE_OUTGOING) {
314 service.hangUp(mCallInfo.mCallID);
315 return true;
316 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400317 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400318 } catch (RemoteException e) {
319 Log.e(TAG, "Cannot call service method", e);
320 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400321
322 return false;
Alexandre Savard4a19d752012-09-19 13:19:24 -0400323 }
Alexandre Savardaef9d802012-09-20 17:31:32 -0400324
Alexandre Savard08545ae2012-10-26 16:11:42 -0400325 public boolean notifyServiceRefuse(ISipService service)
Alexandre Savard83869852012-10-26 09:14:12 -0400326 {
327 try {
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400328 if(getCallStateInt() == CALL_STATE_RINGING) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400329 service.refuse(mCallInfo.mCallID);
330 return true;
331 }
Alexandre Savard83869852012-10-26 09:14:12 -0400332 } catch (RemoteException e) {
333 Log.e(TAG, "Cannot call service method", e);
334 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400335
336 return false;
Alexandre Savard83869852012-10-26 09:14:12 -0400337 }
338
Alexandre Savard08545ae2012-10-26 16:11:42 -0400339 public boolean notifyServiceHold(ISipService service)
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400340 {
341 try {
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400342 if(getCallStateInt() == CALL_STATE_CURRENT) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400343 service.hold(mCallInfo.mCallID);
344 return true;
345 }
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400346 } catch (RemoteException e) {
347 Log.e(TAG, "Cannot call service method", e);
348 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400349
350 return false;
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400351 }
352
Alexandre Savard08545ae2012-10-26 16:11:42 -0400353 public boolean notifyServiceUnhold(ISipService service)
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400354 {
355 try {
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400356 if(getCallStateInt() == CALL_STATE_HOLD) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400357 service.unhold(mCallInfo.mCallID);
358 return true;
359 }
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400360 } catch (RemoteException e) {
361 Log.e(TAG, "Cannot call service method", e);
362 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400363
364 return false;
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400365 }
Alexandre Savard83869852012-10-26 09:14:12 -0400366
Alexandre Savardaef9d802012-09-20 17:31:32 -0400367 public void addToConference()
368 {
369 Log.i(TAG, "Add call to conference");
370 }
371
372 public void sendTextMessage()
373 {
374 Log.i(TAG, "Send text message");
375 }
Alexandre Savard3bdce7b2012-10-24 18:27:45 -0400376
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400377 public void printCallInfo()
378 {
379 Log.i(TAG, "CallInfo: CallID: " + mCallInfo.mCallID);
380 Log.i(TAG, " AccountID: " + mCallInfo.mAccountID);
381 Log.i(TAG, " Display Name: " + mCallInfo.mDisplayName);
382 Log.i(TAG, " Phone: " + mCallInfo.mPhone);
383 Log.i(TAG, " Email: " + mCallInfo.mEmail);
384 Log.i(TAG, " Contact: " + mCallInfo.mRemoteContact);
385 }
386
alision371b77e2013-04-23 14:51:26 -0400387
alision5f899632013-04-22 17:26:56 -0400388
389 /**
390 * Compare sip calls based on call ID
391 */
392 @Override
393 public boolean equals(Object c){
394 if(c instanceof SipCall && ((SipCall) c).mCallInfo.mCallID == mCallInfo.mCallID){
395 return true;
396 }
397 return false;
398
399 }
alision7f18fc82013-05-01 09:37:33 -0400400
401 public void notifyServiceTransfer(ISipService service, String to) {
402 try {
403 if(getCallStateInt() == CALL_STATE_CURRENT) {
404 service.transfer(mCallInfo.mCallID, to);
405 }
406 } catch (RemoteException e) {
407 Log.e(TAG, "Cannot call service method", e);
408 }
409 }
alision04a00182013-05-10 17:05:29 -0400410
411 public void notifyServiceRecord(ISipService service) {
412 try {
413 if(getCallStateInt() == CALL_STATE_CURRENT) {
414 service.setRecordPath(Environment.getExternalStorageDirectory().getAbsolutePath());
415 Log.w(TAG,"Recording path"+service.getRecordPath());
416 service.setRecordingCall(mCallInfo.mCallID);
417 }
418 } catch (RemoteException e) {
419 Log.e(TAG, "Cannot call service method", e);
420 }
421
422 }
423
424 public void notifyServiceSendMsg(ISipService service,String msg) {
425 try {
426 if(getCallStateInt() == CALL_STATE_CURRENT) {
427 service.sendTextMessage(mCallInfo.mCallID, msg, mCallInfo.mDisplayName);
428 }
429 } catch (RemoteException e) {
430 Log.e(TAG, "Cannot call service method", e);
431 }
432
433 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400434}