blob: 713f50829770341db3b6719111bc32adbd9f3d71 [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.Context;
36import android.content.Intent;
37import android.os.Bundle;
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 Savardd1976532012-10-26 15:32:45 -040042import android.view.View;
Alexandre Savard4a19d752012-09-19 13:19:24 -040043
alisiona4325152013-04-19 11:10:03 -040044import com.savoirfairelinux.sflphone.adapters.CallElementAdapter.CallElementView;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040045import com.savoirfairelinux.sflphone.client.CallActivity;
alisionf76de3b2013-04-16 15:35:22 -040046import com.savoirfairelinux.sflphone.client.SFLPhoneHomeActivity;
47import com.savoirfairelinux.sflphone.fragments.CallElementListFragment;
alisiona4325152013-04-19 11:10:03 -040048import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040049
Alexandre Savard557a5742012-10-24 13:54:53 -040050public class SipCall
Alexandre Savard4a19d752012-09-19 13:19:24 -040051{
52 final static String TAG = "SipCall";
Alexandre Savard7a46f542012-09-20 11:23:33 -040053 public CallInfo mCallInfo;
alision371b77e2013-04-23 14:51:26 -040054
Alexandre Savard7a46f542012-09-20 11:23:33 -040055
Alexandre Savard2f1ae542012-10-26 17:05:00 -040056 public static final int CALL_TYPE_UNDETERMINED = 0;
57 public static final int CALL_TYPE_INCOMING = 1;
58 public static final int CALL_TYPE_OUTGOING = 2;
59
Alexandre Savard27a51132012-10-25 18:35:14 -040060 public static final int CALL_STATE_NONE = 0;
Alexandre Savarddf544262012-10-25 14:24:08 -040061 public static final int CALL_STATE_INCOMING = 1;
62 public static final int CALL_STATE_RINGING = 2;
63 public static final int CALL_STATE_CURRENT = 3;
64 public static final int CALL_STATE_HUNGUP = 4;
65 public static final int CALL_STATE_BUSY = 5;
66 public static final int CALL_STATE_FAILURE = 6;
67 public static final int CALL_STATE_HOLD = 7;
68 public static final int CALL_STATE_UNHOLD = 8;
Alexandre Savard2b01c822012-09-20 15:00:37 -040069
Alexandre Savarddf544262012-10-25 14:24:08 -040070 public static final int MEDIA_STATE_NONE = 0; // No media currently
71 public static final int MEDIA_STATE_ACTIVE = 1; // Media is active
72 public static final int MEDIA_STATE_LOCAL_HOLD = 2; // Media is put on hold bu user
73 public static final int MEDIA_STATE_REMOTE_HOLD = 3; // Media is put on hold by peer
74 public static final int MEDIA_STATE_ERROR = 5; // Media is in error state
Alexandre Savard2b01c822012-09-20 15:00:37 -040075
Alexandre Savard557a5742012-10-24 13:54:53 -040076 public static class CallInfo implements Parcelable
Alexandre Savard7a46f542012-09-20 11:23:33 -040077 {
Alexandre Savard2b01c822012-09-20 15:00:37 -040078 public String mCallID = "";
Alexandre Savard73bc56f2012-10-25 13:35:54 -040079 public String mAccountID = "";
Alexandre Savard7a46f542012-09-20 11:23:33 -040080 public String mDisplayName = "";
81 public String mPhone = "";
82 public String mEmail = "";
Alexandre Savard2b01c822012-09-20 15:00:37 -040083 public String mRemoteContact = "";
Alexandre Savard2f1ae542012-10-26 17:05:00 -040084 public int mCallType = CALL_TYPE_UNDETERMINED;
Alexandre Savard27a51132012-10-25 18:35:14 -040085 public int mCallState = CALL_STATE_NONE;
Alexandre Savard557a5742012-10-24 13:54:53 -040086 public int mMediaState = MEDIA_STATE_NONE;
87
88 @Override
89 public int describeContents() {
90 return 0;
91 }
92
93 @Override
94 public void writeToParcel(Parcel out, int flags) {
95 ArrayList<String> list = new ArrayList<String>();
Alexandre Savard74c1cad2012-10-24 16:39:00 -040096
97 // Don't mess with this order!!!
Alexandre Savard557a5742012-10-24 13:54:53 -040098 list.add(mCallID);
Alexandre Savard73bc56f2012-10-25 13:35:54 -040099 list.add(mAccountID);
Alexandre Savard557a5742012-10-24 13:54:53 -0400100 list.add(mDisplayName);
101 list.add(mPhone);
102 list.add(mEmail);
103 list.add(mRemoteContact);
104
105 out.writeStringList(list);
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400106 out.writeInt(mCallType);
Alexandre Savard557a5742012-10-24 13:54:53 -0400107 out.writeInt(mCallState);
108 out.writeInt(mMediaState);
109 }
110
Adrien Béraudea28cd52013-04-26 17:40:46 +1000111 public static final Parcelable.Creator<CallInfo> CREATOR = new Parcelable.Creator<CallInfo>() {
Alexandre Savard557a5742012-10-24 13:54:53 -0400112 public CallInfo createFromParcel(Parcel in) {
113 return new CallInfo(in);
114 }
115
116 public CallInfo[] newArray(int size) {
117 return new CallInfo[size];
118 }
119 };
120
121 public CallInfo() {}
122
123 private CallInfo(Parcel in) {
124 ArrayList<String> list = in.createStringArrayList();
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400125
126 // Don't mess with this order!!!
Alexandre Savard557a5742012-10-24 13:54:53 -0400127 mCallID = list.get(0);
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400128 mAccountID = list.get(1);
129 mDisplayName = list.get(2);
130 mPhone = list.get(3);
131 mEmail = list.get(4);
132 mRemoteContact = list.get(5);
Alexandre Savard557a5742012-10-24 13:54:53 -0400133
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400134 mCallType = in.readInt();
Alexandre Savard557a5742012-10-24 13:54:53 -0400135 mCallState = in.readInt();
136 mMediaState = in.readInt();
137 }
alision371b77e2013-04-23 14:51:26 -0400138
139 public CallInfo(Intent call) {
140 Bundle b = call.getBundleExtra("com.savoirfairelinux.sflphone.service.newcall");
141 mAccountID = b.getString("AccountID");
142 mCallID = b.getString("CallID");
143 mDisplayName = b.getString("From");
144 }
Alexandre Savard7a46f542012-09-20 11:23:33 -0400145 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400146
147 public SipCall()
148 {
Alexandre Savard7a46f542012-09-20 11:23:33 -0400149 mCallInfo = new CallInfo();
Alexandre Savard4a19d752012-09-19 13:19:24 -0400150 }
151
Alexandre Savard7a46f542012-09-20 11:23:33 -0400152 public SipCall(CallInfo info)
Alexandre Savard4a19d752012-09-19 13:19:24 -0400153 {
Alexandre Savard7a46f542012-09-20 11:23:33 -0400154 mCallInfo = info;
Alexandre Savard4a19d752012-09-19 13:19:24 -0400155 }
156
Alexandre Savarda1404652012-09-20 13:34:08 -0400157
Alexandre Savardd1976532012-10-26 15:32:45 -0400158
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
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400207 public void setCallType(int callType) {
208 mCallInfo.mCallType = callType;
209 }
210
211 public int getCallType() {
212 return mCallInfo.mCallType;
213 }
214
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400215 public void setCallState(int callState) {
216 mCallInfo.mCallState = callState;
217 }
218
Alexandre Savarddf544262012-10-25 14:24:08 -0400219 public int getCallStateInt() {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400220 return mCallInfo.mCallState;
221 }
222
Alexandre Savarddf544262012-10-25 14:24:08 -0400223 public String getCallStateString() {
224 String state;
225
226 switch(mCallInfo.mCallState) {
227 case CALL_STATE_INCOMING:
228 state = "INCOMING";
229 break;
230 case CALL_STATE_RINGING:
231 state = "RINGING";
232 break;
233 case CALL_STATE_CURRENT:
234 state = "CURRENT";
235 break;
236 case CALL_STATE_HUNGUP:
237 state = "HUNGUP";
238 break;
239 case CALL_STATE_BUSY:
240 state = "BUSY";
241 break;
242 case CALL_STATE_FAILURE:
243 state = "FAILURE";
244 break;
245 case CALL_STATE_HOLD:
246 state = "HOLD";
247 break;
248 case CALL_STATE_UNHOLD:
249 state = "UNHOLD";
250 break;
251 default:
252 state = "NULL";
253 }
254
255 return state;
256 }
257
258
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400259 public void setMediaState(int mediaState) {
260 mCallInfo.mMediaState = mediaState;
261 }
262
263 public int getMediaState() {
264 return mCallInfo.mMediaState;
265 }
266
alision371b77e2013-04-23 14:51:26 -0400267
Alexandre Savard4a19d752012-09-19 13:19:24 -0400268
Alexandre Savard08545ae2012-10-26 16:11:42 -0400269 public boolean notifyServiceAnswer(ISipService service)
Alexandre Savardf17e3172012-10-25 16:09:09 -0400270 {
Alexandre Savard27a51132012-10-25 18:35:14 -0400271 int callState = getCallStateInt();
272 if((callState != CALL_STATE_RINGING) &&
273 (callState != CALL_STATE_NONE)) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400274 return false;
Alexandre Savard27a51132012-10-25 18:35:14 -0400275 }
Alexandre Savardf17e3172012-10-25 16:09:09 -0400276
277 try {
278 service.accept(mCallInfo.mCallID);
279 } catch (RemoteException e) {
280 Log.e(TAG, "Cannot call service method", e);
281 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400282
283 return true;
Alexandre Savard4a19d752012-09-19 13:19:24 -0400284 }
285
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400286 /**
287 * Perform hangup action without sending request to the service
Alexandre Savarda949eec2012-10-25 17:30:49 -0400288 * Used when SipService haved been notified that this call hung up
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400289 */
alision371b77e2013-04-23 14:51:26 -0400290// public void hangupUpdateUi() {
291// Log.i(TAG, "Hangup call " + mCallInfo.mCallID);
292//
293// if(mCallElementList != null)
294// mCallElementList.removeCall(this);
295//
296// if(mHome != null)
297// mHome.onUnselectedCallAction();
298// }
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400299
300 /**
301 * Perform hangup action and send request to the service
302 */
Alexandre Savard08545ae2012-10-26 16:11:42 -0400303 public boolean notifyServiceHangup(ISipService service)
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400304 {
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400305 try {
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400306 if((getCallStateInt() == CALL_STATE_NONE) ||
307 (getCallStateInt() == CALL_STATE_CURRENT) ||
308 (getCallStateInt() == CALL_STATE_HOLD)) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400309 service.hangUp(mCallInfo.mCallID);
310 return true;
311
312 }
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400313 else if(getCallStateInt() == CALL_STATE_RINGING) {
314 if(getCallType() == CALL_TYPE_INCOMING) {
315 service.refuse(mCallInfo.mCallID);
316 return true;
317 }
318 else if(getCallType() == CALL_TYPE_OUTGOING) {
319 service.hangUp(mCallInfo.mCallID);
320 return true;
321 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400322 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400323 } catch (RemoteException e) {
324 Log.e(TAG, "Cannot call service method", e);
325 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400326
327 return false;
Alexandre Savard4a19d752012-09-19 13:19:24 -0400328 }
Alexandre Savardaef9d802012-09-20 17:31:32 -0400329
Alexandre Savard08545ae2012-10-26 16:11:42 -0400330 public boolean notifyServiceRefuse(ISipService service)
Alexandre Savard83869852012-10-26 09:14:12 -0400331 {
332 try {
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400333 if(getCallStateInt() == CALL_STATE_RINGING) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400334 service.refuse(mCallInfo.mCallID);
335 return true;
336 }
Alexandre Savard83869852012-10-26 09:14:12 -0400337 } catch (RemoteException e) {
338 Log.e(TAG, "Cannot call service method", e);
339 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400340
341 return false;
Alexandre Savard83869852012-10-26 09:14:12 -0400342 }
343
Alexandre Savard08545ae2012-10-26 16:11:42 -0400344 public boolean notifyServiceHold(ISipService service)
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400345 {
346 try {
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400347 if(getCallStateInt() == CALL_STATE_CURRENT) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400348 service.hold(mCallInfo.mCallID);
349 return true;
350 }
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400351 } catch (RemoteException e) {
352 Log.e(TAG, "Cannot call service method", e);
353 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400354
355 return false;
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400356 }
357
Alexandre Savard08545ae2012-10-26 16:11:42 -0400358 public boolean notifyServiceUnhold(ISipService service)
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400359 {
360 try {
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400361 if(getCallStateInt() == CALL_STATE_HOLD) {
Alexandre Savard08545ae2012-10-26 16:11:42 -0400362 service.unhold(mCallInfo.mCallID);
363 return true;
364 }
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400365 } catch (RemoteException e) {
366 Log.e(TAG, "Cannot call service method", e);
367 }
Alexandre Savard08545ae2012-10-26 16:11:42 -0400368
369 return false;
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400370 }
Alexandre Savard83869852012-10-26 09:14:12 -0400371
Alexandre Savardaef9d802012-09-20 17:31:32 -0400372 public void addToConference()
373 {
374 Log.i(TAG, "Add call to conference");
375 }
376
377 public void sendTextMessage()
378 {
379 Log.i(TAG, "Send text message");
380 }
Alexandre Savard3bdce7b2012-10-24 18:27:45 -0400381
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400382 public void printCallInfo()
383 {
384 Log.i(TAG, "CallInfo: CallID: " + mCallInfo.mCallID);
385 Log.i(TAG, " AccountID: " + mCallInfo.mAccountID);
386 Log.i(TAG, " Display Name: " + mCallInfo.mDisplayName);
387 Log.i(TAG, " Phone: " + mCallInfo.mPhone);
388 Log.i(TAG, " Email: " + mCallInfo.mEmail);
389 Log.i(TAG, " Contact: " + mCallInfo.mRemoteContact);
390 }
391
alision371b77e2013-04-23 14:51:26 -0400392
alision5f899632013-04-22 17:26:56 -0400393
394 /**
395 * Compare sip calls based on call ID
396 */
397 @Override
398 public boolean equals(Object c){
399 if(c instanceof SipCall && ((SipCall) c).mCallInfo.mCallID == mCallInfo.mCallID){
400 return true;
401 }
402 return false;
403
404 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400405}