blob: a8574a4b4dc55aa47ceaa56fa2565c2b5e556705 [file] [log] [blame]
Alexandre Savard4a19d752012-09-19 13:19:24 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Savard4a19d752012-09-19 13:19:24 -04003 *
Alexandre Lision68855472013-10-10 16:20:46 -04004 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux>
5 * Alexandre Savard <alexandre.savard@gmail.com>
Alexandre Savard4a19d752012-09-19 13:19:24 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Additional permission under GNU GPL version 3 section 7:
22 *
23 * If you modify this program, or any covered work, by linking or
24 * combining it with the OpenSSL project's OpenSSL library (or a
25 * modified version of that library), containing parts covered by the
26 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
27 * grants you additional permission to convey the resulting work.
28 * Corresponding Source for a non-source form of such a combination
29 * shall include the source code for the parts of OpenSSL used as well
30 * as that of the covered work.
31 */
Alexandre Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.model;
Alexandre Savard4a19d752012-09-19 13:19:24 -040033
Alexandre Lision5ed2c972013-10-11 15:36:33 -040034import java.io.InvalidObjectException;
alisiona4325152013-04-19 11:10:03 -040035import java.util.ArrayList;
alisionfde875f2013-05-28 17:01:54 -040036import java.util.Random;
alisiona4325152013-04-19 11:10:03 -040037
alision806e18e2013-06-21 15:30:17 -040038import android.content.ContentResolver;
Alexandre Savard557a5742012-10-24 13:54:53 -040039import android.os.Parcel;
alisiona4325152013-04-19 11:10:03 -040040import android.os.Parcelable;
Alexandre Savard4a19d752012-09-19 13:19:24 -040041import android.util.Log;
Alexandre Savard4a19d752012-09-19 13:19:24 -040042
alisionfde875f2013-05-28 17:01:54 -040043public class SipCall implements Parcelable {
alision371b77e2013-04-23 14:51:26 -040044
alisionfde875f2013-05-28 17:01:54 -040045 private static final String TAG = SipCall.class.getSimpleName();
Alexandre Savard2f1ae542012-10-26 17:05:00 -040046
alisionfde875f2013-05-28 17:01:54 -040047 private String mCallID = "";
Alexandre Lision3ee516e2013-10-07 17:32:15 -040048 private Account mAccount = null;
alision907bde72013-06-20 14:40:37 -040049 private CallContact contact = null;
alisiondf1dac92013-06-27 17:35:53 -040050 private boolean isRecording = false;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040051 private long timestamp_start = 0;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050052
Alexandre Savard2b01c822012-09-20 15:00:37 -040053
alisionfde875f2013-05-28 17:01:54 -040054 private int mCallType = state.CALL_TYPE_UNDETERMINED;
55 private int mCallState = state.CALL_STATE_NONE;
56 private int mMediaState = state.MEDIA_STATE_NONE;
Alexandre Savard2b01c822012-09-20 15:00:37 -040057
alisionfde875f2013-05-28 17:01:54 -040058 /************************
59 * Construtors
alisionfde875f2013-05-28 17:01:54 -040060 ***********************/
Alexandre Savard557a5742012-10-24 13:54:53 -040061
alisionfde875f2013-05-28 17:01:54 -040062 private SipCall(Parcel in) {
Alexandre Savard557a5742012-10-24 13:54:53 -040063
Alexandre Lisiond5686032013-10-29 11:09:21 -040064 mCallID = in.readString();
Alexandre Lision3ee516e2013-10-07 17:32:15 -040065 mAccount = in.readParcelable(Account.class.getClassLoader());
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 Lisionebeb3662013-09-17 16:20:54 -040071 timestamp_start = in.readLong();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050072 }
Alexandre Savard4a19d752012-09-19 13:19:24 -040073
Alexandre Lision3ee516e2013-10-07 17:32:15 -040074 public SipCall(String id, Account account, int call_type, int call_state, int media_state, CallContact c) {
alisionfde875f2013-05-28 17:01:54 -040075 mCallID = id;
Alexandre Lision3ee516e2013-10-07 17:32:15 -040076 mAccount = account;
alisionfde875f2013-05-28 17:01:54 -040077 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 public interface state {
84 public static final int CALL_TYPE_UNDETERMINED = 0;
85 public static final int CALL_TYPE_INCOMING = 1;
86 public static final int CALL_TYPE_OUTGOING = 2;
87
88 public static final int CALL_STATE_NONE = 0;
89 public static final int CALL_STATE_INCOMING = 1;
90 public static final int CALL_STATE_RINGING = 2;
91 public static final int CALL_STATE_CURRENT = 3;
92 public static final int CALL_STATE_HUNGUP = 4;
93 public static final int CALL_STATE_BUSY = 5;
94 public static final int CALL_STATE_FAILURE = 6;
95 public static final int CALL_STATE_HOLD = 7;
96 public static final int CALL_STATE_UNHOLD = 8;
97
98 public static final int MEDIA_STATE_NONE = 0; // No media currently
99 public static final int MEDIA_STATE_ACTIVE = 1; // Media is active
100 public static final int MEDIA_STATE_LOCAL_HOLD = 2; // Media is put on hold bu user
101 public static final int MEDIA_STATE_REMOTE_HOLD = 3; // Media is put on hold by peer
102 public static final int MEDIA_STATE_ERROR = 5; // Media is in error state
Alexandre Savard4a19d752012-09-19 13:19:24 -0400103 }
104
alisionfde875f2013-05-28 17:01:54 -0400105 @Override
106 public int describeContents() {
107 return 0;
108 }
109
110 @Override
111 public void writeToParcel(Parcel out, int flags) {
alisionfde875f2013-05-28 17:01:54 -0400112
Alexandre Lisiond5686032013-10-29 11:09:21 -0400113 out.writeString(mCallID);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400114 out.writeParcelable(mAccount, 0);
115
alision907bde72013-06-20 14:40:37 -0400116 out.writeParcelable(contact, 0);
alisiondf1dac92013-06-27 17:35:53 -0400117 out.writeByte((byte) (isRecording ? 1 : 0));
alisionfde875f2013-05-28 17:01:54 -0400118 out.writeInt(mCallType);
119 out.writeInt(mCallState);
120 out.writeInt(mMediaState);
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400121 out.writeLong(timestamp_start);
alisionfde875f2013-05-28 17:01:54 -0400122 }
123
124 public static final Parcelable.Creator<SipCall> CREATOR = new Parcelable.Creator<SipCall>() {
125 public SipCall createFromParcel(Parcel in) {
126 return new SipCall(in);
127 }
128
129 public SipCall[] newArray(int size) {
130 return new SipCall[size];
131 }
132 };
133
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400134 public void setCallID(String callID) {
alisionfde875f2013-05-28 17:01:54 -0400135 mCallID = callID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400136 }
137
138 public String getCallId() {
alisionfde875f2013-05-28 17:01:54 -0400139 return mCallID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400140 }
141
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400142 public long getTimestamp_start() {
143 return timestamp_start;
144 }
145
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400146 public void setTimestamp_start(long timestamp_start) {
147 this.timestamp_start = timestamp_start;
148 }
149
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400150 public void setAccount(Account account) {
151 mAccount = account;
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400152 }
153
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400154 public Account getAccount() {
155 return mAccount;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400156 }
157
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400158 public void setCallType(int callType) {
alisionfde875f2013-05-28 17:01:54 -0400159 mCallType = callType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400160 }
161
162 public int getCallType() {
alisionfde875f2013-05-28 17:01:54 -0400163 return mCallType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400164 }
165
alision85704182013-05-29 15:23:03 -0400166 public String getCallTypeString() {
167 switch (mCallType) {
168 case state.CALL_TYPE_INCOMING:
169 return "CALL_TYPE_INCOMING";
170 case state.CALL_TYPE_OUTGOING:
171 return "CALL_TYPE_OUTGOING";
172 default:
173 return "CALL_TYPE_UNDETERMINED";
174 }
175 }
176
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400177 public void setCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400178 mCallState = callState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400179 }
180
Alexandre Savarddf544262012-10-25 14:24:08 -0400181 public int getCallStateInt() {
alisionfde875f2013-05-28 17:01:54 -0400182 return mCallState;
183 }
184
185 public String getmCallID() {
186 return mCallID;
187 }
188
189 public void setmCallID(String mCallID) {
190 this.mCallID = mCallID;
191 }
192
alision907bde72013-06-20 14:40:37 -0400193 public CallContact getContact() {
194 return contact;
alisionfde875f2013-05-28 17:01:54 -0400195 }
196
alision907bde72013-06-20 14:40:37 -0400197 public void setContact(CallContact contacts) {
198 contact = contacts;
alisionfde875f2013-05-28 17:01:54 -0400199 }
200
201 public int getmCallType() {
202 return mCallType;
203 }
204
205 public void setmCallType(int mCallType) {
206 this.mCallType = mCallType;
207 }
208
alisionfde875f2013-05-28 17:01:54 -0400209 public int getmMediaState() {
210 return mMediaState;
211 }
212
213 public void setmMediaState(int mMediaState) {
214 this.mMediaState = mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400215 }
216
Alexandre Savarddf544262012-10-25 14:24:08 -0400217 public String getCallStateString() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400218
alisionfde875f2013-05-28 17:01:54 -0400219 String text_state;
220
221 switch (mCallState) {
222 case state.CALL_STATE_INCOMING:
223 text_state = "INCOMING";
alision11e8e162013-05-28 10:33:14 -0400224 break;
alisionfde875f2013-05-28 17:01:54 -0400225 case state.CALL_STATE_RINGING:
226 text_state = "RINGING";
alision11e8e162013-05-28 10:33:14 -0400227 break;
alisionfde875f2013-05-28 17:01:54 -0400228 case state.CALL_STATE_CURRENT:
229 text_state = "CURRENT";
alision11e8e162013-05-28 10:33:14 -0400230 break;
alisionfde875f2013-05-28 17:01:54 -0400231 case state.CALL_STATE_HUNGUP:
232 text_state = "HUNGUP";
alision11e8e162013-05-28 10:33:14 -0400233 break;
alisionfde875f2013-05-28 17:01:54 -0400234 case state.CALL_STATE_BUSY:
235 text_state = "BUSY";
alision11e8e162013-05-28 10:33:14 -0400236 break;
alisionfde875f2013-05-28 17:01:54 -0400237 case state.CALL_STATE_FAILURE:
238 text_state = "FAILURE";
alision11e8e162013-05-28 10:33:14 -0400239 break;
alisionfde875f2013-05-28 17:01:54 -0400240 case state.CALL_STATE_HOLD:
241 text_state = "HOLD";
alision11e8e162013-05-28 10:33:14 -0400242 break;
alisionfde875f2013-05-28 17:01:54 -0400243 case state.CALL_STATE_UNHOLD:
244 text_state = "UNHOLD";
alision11e8e162013-05-28 10:33:14 -0400245 break;
246 default:
alisionfde875f2013-05-28 17:01:54 -0400247 text_state = "NULL";
Alexandre Savarddf544262012-10-25 14:24:08 -0400248 }
249
alisionfde875f2013-05-28 17:01:54 -0400250 return text_state;
Alexandre Savarddf544262012-10-25 14:24:08 -0400251 }
252
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400253 public void setMediaState(int mediaState) {
alisionfde875f2013-05-28 17:01:54 -0400254 mMediaState = mediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400255 }
256
257 public int getMediaState() {
alisionfde875f2013-05-28 17:01:54 -0400258 return mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400259 }
260
alisiondf1dac92013-06-27 17:35:53 -0400261 public boolean isRecording() {
262 return isRecording;
263 }
264
265 public void setRecording(boolean isRecording) {
266 this.isRecording = isRecording;
267 }
268
alisionfde875f2013-05-28 17:01:54 -0400269 public static class SipCallBuilder {
270
271 private String bCallID = "";
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400272 private Account bAccount = null;
alision907bde72013-06-20 14:40:37 -0400273 private CallContact bContact = null;
alisionfde875f2013-05-28 17:01:54 -0400274
275 private int bCallType = state.CALL_TYPE_UNDETERMINED;
276 private int bCallState = state.CALL_STATE_NONE;
277 private int bMediaState = state.MEDIA_STATE_NONE;
278
alisionfde875f2013-05-28 17:01:54 -0400279 public SipCallBuilder setCallType(int bCallType) {
280 this.bCallType = bCallType;
281 return this;
282 }
283
284 public SipCallBuilder setMediaState(int state) {
285 this.bMediaState = state;
286 return this;
287 }
288
289 public SipCallBuilder setCallState(int state) {
290 this.bCallState = state;
291 return this;
292 }
alision85704182013-05-29 15:23:03 -0400293
alisionfde875f2013-05-28 17:01:54 -0400294 public SipCallBuilder startCallCreation(String id) {
295 bCallID = id;
alisionfde875f2013-05-28 17:01:54 -0400296 bCallType = SipCall.state.CALL_TYPE_INCOMING;
297 return this;
298 }
299
300 public SipCallBuilder startCallCreation() {
301 Random random = new Random();
Tristan Matthews1a4962d2013-07-18 16:27:10 -0400302 bCallID = Integer.toString(Math.abs(random.nextInt()));
alisionfde875f2013-05-28 17:01:54 -0400303 return this;
304 }
305
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400306 public SipCallBuilder setAccount(Account a) {
307 bAccount = a;
alisionfde875f2013-05-28 17:01:54 -0400308 return this;
309 }
310
alision907bde72013-06-20 14:40:37 -0400311 public SipCallBuilder setContact(CallContact c) {
312 bContact = c;
alisionfde875f2013-05-28 17:01:54 -0400313 return this;
314 }
315
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400316 public SipCall build() throws InvalidObjectException {
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400317 if (bCallID.contentEquals("") || bAccount == null || bContact == null) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400318 throw new InvalidObjectException("SipCallBuilder's parameters missing");
alisionfde875f2013-05-28 17:01:54 -0400319 }
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400320 return new SipCall(bCallID, bAccount, bCallType, bCallState, bMediaState, bContact);
alisionfde875f2013-05-28 17:01:54 -0400321 }
322
323 public static SipCallBuilder getInstance() {
324 return new SipCallBuilder();
325 }
326
alision806e18e2013-06-21 15:30:17 -0400327 public static SipCall buildMyselfCall(ContentResolver cr, String displayName) {
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400328 return new SipCall("default", null, SipCall.state.CALL_TYPE_UNDETERMINED, state.CALL_STATE_NONE, state.MEDIA_STATE_NONE,
Alexandre Lision3874e552013-11-04 17:20:12 -0500329 CallContact.ContactBuilder.buildUserContact(cr));
alision806e18e2013-06-21 15:30:17 -0400330
331 }
332
alisionfde875f2013-05-28 17:01:54 -0400333 }
334
alision85704182013-05-29 15:23:03 -0400335 public void printCallInfo() {
336 Log.i(TAG, "CallInfo: CallID: " + mCallID);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400337 Log.i(TAG, " AccountID: " + mAccount.getAccountID());
alision85704182013-05-29 15:23:03 -0400338 Log.i(TAG, " CallState: " + mCallState);
339 Log.i(TAG, " CallType: " + mCallType);
340 }
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400341
alision5f899632013-04-22 17:26:56 -0400342 /**
343 * Compare sip calls based on call ID
344 */
345 @Override
alision11e8e162013-05-28 10:33:14 -0400346 public boolean equals(Object c) {
alision465ceba2013-07-04 09:24:30 -0400347 if (c instanceof SipCall && ((SipCall) c).mCallID.contentEquals((mCallID))) {
alision5f899632013-04-22 17:26:56 -0400348 return true;
349 }
350 return false;
alision11e8e162013-05-28 10:33:14 -0400351
alision5f899632013-04-22 17:26:56 -0400352 }
alision7f18fc82013-05-01 09:37:33 -0400353
alision85704182013-05-29 15:23:03 -0400354 public boolean isOutGoing() {
355 if (mCallType == state.CALL_TYPE_OUTGOING)
356 return true;
357
358 return false;
359 }
360
361 public boolean isRinging() {
362 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE)
363 return true;
364
365 return false;
366 }
367
368 public boolean isIncoming() {
369 if (mCallType == state.CALL_TYPE_INCOMING)
370 return true;
371
372 return false;
373 }
374
375 public void setCallState(String newState) {
376 if (newState.equals("INCOMING")) {
377 setCallState(SipCall.state.CALL_STATE_INCOMING);
378 } else if (newState.equals("RINGING")) {
379 setCallState(SipCall.state.CALL_STATE_RINGING);
380 } else if (newState.equals("CURRENT")) {
381 setCallState(SipCall.state.CALL_STATE_CURRENT);
382 } else if (newState.equals("HUNGUP")) {
383 setCallState(SipCall.state.CALL_STATE_HUNGUP);
384 } else if (newState.equals("BUSY")) {
385 setCallState(SipCall.state.CALL_STATE_BUSY);
386 } else if (newState.equals("FAILURE")) {
387 setCallState(SipCall.state.CALL_STATE_FAILURE);
388 } else if (newState.equals("HOLD")) {
389 setCallState(SipCall.state.CALL_STATE_HOLD);
390 } else if (newState.equals("UNHOLD")) {
391 setCallState(SipCall.state.CALL_STATE_CURRENT);
392 } else {
393 setCallState(SipCall.state.CALL_STATE_NONE);
394 }
alision907bde72013-06-20 14:40:37 -0400395
alision85704182013-05-29 15:23:03 -0400396 }
397
398 public boolean isOngoing() {
alision907bde72013-06-20 14:40:37 -0400399 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE || mCallState == state.CALL_STATE_FAILURE
400 || mCallState == state.CALL_STATE_BUSY || mCallState == state.CALL_STATE_HUNGUP)
alision85704182013-05-29 15:23:03 -0400401 return false;
402
403 return true;
404 }
alision04a00182013-05-10 17:05:29 -0400405
alisiondf1dac92013-06-27 17:35:53 -0400406 public boolean isOnHold() {
407 return mCallState == state.CALL_STATE_HOLD;
408 }
409
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400410 public boolean isCurrent() {
411 return mCallState == state.CALL_STATE_CURRENT;
412 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400413}