blob: 7bc3e158a2b215c0ad7be75fa0edfcd55f081420 [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;
alisionfde875f2013-05-28 17:01:54 -040034import java.util.Random;
Tristan Matthews1a4962d2013-07-18 16:27:10 -040035import java.lang.Math;
alisiona4325152013-04-19 11:10:03 -040036
alision806e18e2013-06-21 15:30:17 -040037import android.content.ContentResolver;
Alexandre Savard557a5742012-10-24 13:54:53 -040038import android.os.Parcel;
alisiona4325152013-04-19 11:10:03 -040039import android.os.Parcelable;
Alexandre Savard4a19d752012-09-19 13:19:24 -040040import android.util.Log;
Alexandre Savard4a19d752012-09-19 13:19:24 -040041
alisionfde875f2013-05-28 17:01:54 -040042public class SipCall implements Parcelable {
alision371b77e2013-04-23 14:51:26 -040043
alisionfde875f2013-05-28 17:01:54 -040044 private static final String TAG = SipCall.class.getSimpleName();
Alexandre Savard2f1ae542012-10-26 17:05:00 -040045
alisionfde875f2013-05-28 17:01:54 -040046 private String mCallID = "";
47 private String mAccountID = "";
alision907bde72013-06-20 14:40:37 -040048 private CallContact contact = null;
alisiondf1dac92013-06-27 17:35:53 -040049 private boolean isRecording = false;
50
alision50fa0722013-06-25 17:29:44 -040051 public static final String USER_ID = "user_id";
Alexandre Savard2b01c822012-09-20 15:00:37 -040052
alisionfde875f2013-05-28 17:01:54 -040053 private int mCallType = state.CALL_TYPE_UNDETERMINED;
54 private int mCallState = state.CALL_STATE_NONE;
55 private int mMediaState = state.MEDIA_STATE_NONE;
Alexandre Savard2b01c822012-09-20 15:00:37 -040056
alisionfde875f2013-05-28 17:01:54 -040057 /************************
58 * Construtors
alisionfde875f2013-05-28 17:01:54 -040059 ***********************/
Alexandre Savard557a5742012-10-24 13:54:53 -040060
alisionfde875f2013-05-28 17:01:54 -040061 private SipCall(Parcel in) {
62 ArrayList<String> list = in.createStringArrayList();
Alexandre Savard557a5742012-10-24 13:54:53 -040063
alisionfde875f2013-05-28 17:01:54 -040064 mCallID = list.get(0);
65 mAccountID = list.get(1);
alision907bde72013-06-20 14:40:37 -040066 contact = in.readParcelable(CallContact.class.getClassLoader());
alisiondf1dac92013-06-27 17:35:53 -040067 isRecording = in.readByte() == 1;
alisionfde875f2013-05-28 17:01:54 -040068 mCallType = in.readInt();
69 mCallState = in.readInt();
70 mMediaState = in.readInt();
Alexandre Savard7a46f542012-09-20 11:23:33 -040071 }
Alexandre Savard4a19d752012-09-19 13:19:24 -040072
alisionfde875f2013-05-28 17:01:54 -040073
alision907bde72013-06-20 14:40:37 -040074 public SipCall(String id, String account, int call_type, int call_state, int media_state, CallContact c) {
alisionfde875f2013-05-28 17:01:54 -040075 mCallID = id;
76 mAccountID = account;
77 mCallType = call_type;
78 mCallState = call_state;
79 mMediaState = media_state;
alision907bde72013-06-20 14:40:37 -040080 contact = c;
Alexandre Savard4a19d752012-09-19 13:19:24 -040081 }
82
alisionfde875f2013-05-28 17:01:54 -040083
84 public interface state {
85 public static final int CALL_TYPE_UNDETERMINED = 0;
86 public static final int CALL_TYPE_INCOMING = 1;
87 public static final int CALL_TYPE_OUTGOING = 2;
88
89 public static final int CALL_STATE_NONE = 0;
90 public static final int CALL_STATE_INCOMING = 1;
91 public static final int CALL_STATE_RINGING = 2;
92 public static final int CALL_STATE_CURRENT = 3;
93 public static final int CALL_STATE_HUNGUP = 4;
94 public static final int CALL_STATE_BUSY = 5;
95 public static final int CALL_STATE_FAILURE = 6;
96 public static final int CALL_STATE_HOLD = 7;
97 public static final int CALL_STATE_UNHOLD = 8;
98
99 public static final int MEDIA_STATE_NONE = 0; // No media currently
100 public static final int MEDIA_STATE_ACTIVE = 1; // Media is active
101 public static final int MEDIA_STATE_LOCAL_HOLD = 2; // Media is put on hold bu user
102 public static final int MEDIA_STATE_REMOTE_HOLD = 3; // Media is put on hold by peer
103 public static final int MEDIA_STATE_ERROR = 5; // Media is in error state
Alexandre Savard4a19d752012-09-19 13:19:24 -0400104 }
105
alisionfde875f2013-05-28 17:01:54 -0400106 @Override
107 public int describeContents() {
108 return 0;
109 }
110
111 @Override
112 public void writeToParcel(Parcel out, int flags) {
113 ArrayList<String> list = new ArrayList<String>();
114
115 // Don't mess with this order!!!
116 list.add(mCallID);
117 list.add(mAccountID);
118
119 out.writeStringList(list);
alision907bde72013-06-20 14:40:37 -0400120 out.writeParcelable(contact, 0);
alisiondf1dac92013-06-27 17:35:53 -0400121 out.writeByte((byte) (isRecording ? 1 : 0));
alisionfde875f2013-05-28 17:01:54 -0400122 out.writeInt(mCallType);
123 out.writeInt(mCallState);
124 out.writeInt(mMediaState);
125 }
126
127 public static final Parcelable.Creator<SipCall> CREATOR = new Parcelable.Creator<SipCall>() {
128 public SipCall createFromParcel(Parcel in) {
129 return new SipCall(in);
130 }
131
132 public SipCall[] newArray(int size) {
133 return new SipCall[size];
134 }
135 };
136
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400137 public void setCallID(String callID) {
alisionfde875f2013-05-28 17:01:54 -0400138 mCallID = callID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400139 }
140
141 public String getCallId() {
alisionfde875f2013-05-28 17:01:54 -0400142 return mCallID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400143 }
144
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400145 public void setAccountID(String accountID) {
alisionfde875f2013-05-28 17:01:54 -0400146 mAccountID = accountID;
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400147 }
148
149 public String getAccountID() {
alisionfde875f2013-05-28 17:01:54 -0400150 return mAccountID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400151 }
152
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400153 public void setCallType(int callType) {
alisionfde875f2013-05-28 17:01:54 -0400154 mCallType = callType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400155 }
156
157 public int getCallType() {
alisionfde875f2013-05-28 17:01:54 -0400158 return mCallType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400159 }
160
alision85704182013-05-29 15:23:03 -0400161 public String getCallTypeString() {
162 switch (mCallType) {
163 case state.CALL_TYPE_INCOMING:
164 return "CALL_TYPE_INCOMING";
165 case state.CALL_TYPE_OUTGOING:
166 return "CALL_TYPE_OUTGOING";
167 default:
168 return "CALL_TYPE_UNDETERMINED";
169 }
170 }
171
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400172 public void setCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400173 mCallState = callState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400174 }
175
Alexandre Savarddf544262012-10-25 14:24:08 -0400176 public int getCallStateInt() {
alisionfde875f2013-05-28 17:01:54 -0400177 return mCallState;
178 }
179
180 public String getmCallID() {
181 return mCallID;
182 }
183
184 public void setmCallID(String mCallID) {
185 this.mCallID = mCallID;
186 }
187
188 public String getmAccountID() {
189 return mAccountID;
190 }
191
192 public void setmAccountID(String mAccountID) {
193 this.mAccountID = mAccountID;
194 }
195
alision907bde72013-06-20 14:40:37 -0400196 public CallContact getContact() {
197 return contact;
alisionfde875f2013-05-28 17:01:54 -0400198 }
199
alision907bde72013-06-20 14:40:37 -0400200 public void setContact(CallContact contacts) {
201 contact = contacts;
alisionfde875f2013-05-28 17:01:54 -0400202 }
203
204 public int getmCallType() {
205 return mCallType;
206 }
207
208 public void setmCallType(int mCallType) {
209 this.mCallType = mCallType;
210 }
211
alisionfde875f2013-05-28 17:01:54 -0400212 public int getmMediaState() {
213 return mMediaState;
214 }
215
216 public void setmMediaState(int mMediaState) {
217 this.mMediaState = mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400218 }
219
Alexandre Savarddf544262012-10-25 14:24:08 -0400220 public String getCallStateString() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400221
alisionfde875f2013-05-28 17:01:54 -0400222 String text_state;
223
224 switch (mCallState) {
225 case state.CALL_STATE_INCOMING:
226 text_state = "INCOMING";
alision11e8e162013-05-28 10:33:14 -0400227 break;
alisionfde875f2013-05-28 17:01:54 -0400228 case state.CALL_STATE_RINGING:
229 text_state = "RINGING";
alision11e8e162013-05-28 10:33:14 -0400230 break;
alisionfde875f2013-05-28 17:01:54 -0400231 case state.CALL_STATE_CURRENT:
232 text_state = "CURRENT";
alision11e8e162013-05-28 10:33:14 -0400233 break;
alisionfde875f2013-05-28 17:01:54 -0400234 case state.CALL_STATE_HUNGUP:
235 text_state = "HUNGUP";
alision11e8e162013-05-28 10:33:14 -0400236 break;
alisionfde875f2013-05-28 17:01:54 -0400237 case state.CALL_STATE_BUSY:
238 text_state = "BUSY";
alision11e8e162013-05-28 10:33:14 -0400239 break;
alisionfde875f2013-05-28 17:01:54 -0400240 case state.CALL_STATE_FAILURE:
241 text_state = "FAILURE";
alision11e8e162013-05-28 10:33:14 -0400242 break;
alisionfde875f2013-05-28 17:01:54 -0400243 case state.CALL_STATE_HOLD:
244 text_state = "HOLD";
alision11e8e162013-05-28 10:33:14 -0400245 break;
alisionfde875f2013-05-28 17:01:54 -0400246 case state.CALL_STATE_UNHOLD:
247 text_state = "UNHOLD";
alision11e8e162013-05-28 10:33:14 -0400248 break;
249 default:
alisionfde875f2013-05-28 17:01:54 -0400250 text_state = "NULL";
Alexandre Savarddf544262012-10-25 14:24:08 -0400251 }
252
alisionfde875f2013-05-28 17:01:54 -0400253 return text_state;
Alexandre Savarddf544262012-10-25 14:24:08 -0400254 }
255
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400256 public void setMediaState(int mediaState) {
alisionfde875f2013-05-28 17:01:54 -0400257 mMediaState = mediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400258 }
259
260 public int getMediaState() {
alisionfde875f2013-05-28 17:01:54 -0400261 return mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400262 }
263
alisiondf1dac92013-06-27 17:35:53 -0400264 public boolean isRecording() {
265 return isRecording;
266 }
267
268 public void setRecording(boolean isRecording) {
269 this.isRecording = isRecording;
270 }
271
alisionfde875f2013-05-28 17:01:54 -0400272 public static class SipCallBuilder {
273
274 private String bCallID = "";
275 private String bAccountID = "";
alision907bde72013-06-20 14:40:37 -0400276 private CallContact bContact = null;
alisionfde875f2013-05-28 17:01:54 -0400277
278 private int bCallType = state.CALL_TYPE_UNDETERMINED;
279 private int bCallState = state.CALL_STATE_NONE;
280 private int bMediaState = state.MEDIA_STATE_NONE;
281
alisionfde875f2013-05-28 17:01:54 -0400282 public SipCallBuilder setCallType(int bCallType) {
283 this.bCallType = bCallType;
284 return this;
285 }
286
287 public SipCallBuilder setMediaState(int state) {
288 this.bMediaState = state;
289 return this;
290 }
291
292 public SipCallBuilder setCallState(int state) {
293 this.bCallState = state;
294 return this;
295 }
alision85704182013-05-29 15:23:03 -0400296
alisionfde875f2013-05-28 17:01:54 -0400297 private static final String TAG = SipCallBuilder.class.getSimpleName();
alision85704182013-05-29 15:23:03 -0400298
alisionfde875f2013-05-28 17:01:54 -0400299 public SipCallBuilder startCallCreation(String id) {
300 bCallID = id;
alisionfde875f2013-05-28 17:01:54 -0400301 bCallType = SipCall.state.CALL_TYPE_INCOMING;
302 return this;
303 }
304
305 public SipCallBuilder startCallCreation() {
306 Random random = new Random();
Tristan Matthews1a4962d2013-07-18 16:27:10 -0400307 bCallID = Integer.toString(Math.abs(random.nextInt()));
alisionfde875f2013-05-28 17:01:54 -0400308 return this;
309 }
310
311 public SipCallBuilder setAccountID(String h) {
312 Log.i(TAG, "setAccountID" + h);
313 bAccountID = h;
314 return this;
315 }
316
alision907bde72013-06-20 14:40:37 -0400317 public SipCallBuilder setContact(CallContact c) {
318 bContact = c;
alisionfde875f2013-05-28 17:01:54 -0400319 return this;
320 }
321
322 public SipCall build() throws Exception {
alision907bde72013-06-20 14:40:37 -0400323 if (bCallID.contentEquals("") || bAccountID.contentEquals("") || bContact == null) {
alisionfde875f2013-05-28 17:01:54 -0400324 throw new Exception("SipCallBuilder's parameters missing");
325 }
alision907bde72013-06-20 14:40:37 -0400326 return new SipCall(bCallID, bAccountID, bCallType, bCallState, bMediaState, bContact);
alisionfde875f2013-05-28 17:01:54 -0400327 }
328
329 public static SipCallBuilder getInstance() {
330 return new SipCallBuilder();
331 }
332
alision806e18e2013-06-21 15:30:17 -0400333 public static SipCall buildMyselfCall(ContentResolver cr, String displayName) {
334 return new SipCall("default", "default", SipCall.state.CALL_TYPE_UNDETERMINED, state.CALL_STATE_NONE, state.MEDIA_STATE_NONE,
alisiondf1dac92013-06-27 17:35:53 -0400335 CallContact.ContactBuilder.buildUserContact(cr, displayName));
alision806e18e2013-06-21 15:30:17 -0400336
337 }
338
alisionfde875f2013-05-28 17:01:54 -0400339 }
340
alision85704182013-05-29 15:23:03 -0400341 public void printCallInfo() {
342 Log.i(TAG, "CallInfo: CallID: " + mCallID);
343 Log.i(TAG, " AccountID: " + mAccountID);
344 Log.i(TAG, " CallState: " + mCallState);
345 Log.i(TAG, " CallType: " + mCallType);
346 }
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400347
alision5f899632013-04-22 17:26:56 -0400348 /**
349 * Compare sip calls based on call ID
350 */
351 @Override
alision11e8e162013-05-28 10:33:14 -0400352 public boolean equals(Object c) {
alision465ceba2013-07-04 09:24:30 -0400353 if (c instanceof SipCall && ((SipCall) c).mCallID.contentEquals((mCallID))) {
alision5f899632013-04-22 17:26:56 -0400354 return true;
355 }
356 return false;
alision11e8e162013-05-28 10:33:14 -0400357
alision5f899632013-04-22 17:26:56 -0400358 }
alision7f18fc82013-05-01 09:37:33 -0400359
alision85704182013-05-29 15:23:03 -0400360 public boolean isOutGoing() {
361 if (mCallType == state.CALL_TYPE_OUTGOING)
362 return true;
363
364 return false;
365 }
366
367 public boolean isRinging() {
368 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE)
369 return true;
370
371 return false;
372 }
373
374 public boolean isIncoming() {
375 if (mCallType == state.CALL_TYPE_INCOMING)
376 return true;
377
378 return false;
379 }
380
381 public void setCallState(String newState) {
382 if (newState.equals("INCOMING")) {
383 setCallState(SipCall.state.CALL_STATE_INCOMING);
384 } else if (newState.equals("RINGING")) {
385 setCallState(SipCall.state.CALL_STATE_RINGING);
386 } else if (newState.equals("CURRENT")) {
387 setCallState(SipCall.state.CALL_STATE_CURRENT);
388 } else if (newState.equals("HUNGUP")) {
389 setCallState(SipCall.state.CALL_STATE_HUNGUP);
390 } else if (newState.equals("BUSY")) {
391 setCallState(SipCall.state.CALL_STATE_BUSY);
392 } else if (newState.equals("FAILURE")) {
393 setCallState(SipCall.state.CALL_STATE_FAILURE);
394 } else if (newState.equals("HOLD")) {
395 setCallState(SipCall.state.CALL_STATE_HOLD);
396 } else if (newState.equals("UNHOLD")) {
397 setCallState(SipCall.state.CALL_STATE_CURRENT);
398 } else {
399 setCallState(SipCall.state.CALL_STATE_NONE);
400 }
alision907bde72013-06-20 14:40:37 -0400401
alision85704182013-05-29 15:23:03 -0400402 }
403
alisiondf1dac92013-06-27 17:35:53 -0400404
alision85704182013-05-29 15:23:03 -0400405 public boolean isOngoing() {
alision907bde72013-06-20 14:40:37 -0400406 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE || mCallState == state.CALL_STATE_FAILURE
407 || mCallState == state.CALL_STATE_BUSY || mCallState == state.CALL_STATE_HUNGUP)
alision85704182013-05-29 15:23:03 -0400408 return false;
409
410 return true;
411 }
alision04a00182013-05-10 17:05:29 -0400412
alisiondf1dac92013-06-27 17:35:53 -0400413 public boolean isOnHold() {
414 return mCallState == state.CALL_STATE_HOLD;
415 }
416
Alexandre Savard4a19d752012-09-19 13:19:24 -0400417}