blob: 1a9c297981172eae7f59fb84f548e1f9b9146c12 [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;
alisionfde875f2013-05-28 17:01:54 -040035import java.util.Random;
alisiona4325152013-04-19 11:10:03 -040036
Alexandre Savard557a5742012-10-24 13:54:53 -040037import android.os.Parcel;
alisiona4325152013-04-19 11:10:03 -040038import android.os.Parcelable;
Alexandre Savard4a19d752012-09-19 13:19:24 -040039import android.util.Log;
Alexandre Savard4a19d752012-09-19 13:19:24 -040040
alisionfde875f2013-05-28 17:01:54 -040041public class SipCall implements Parcelable {
alision371b77e2013-04-23 14:51:26 -040042
alisionfde875f2013-05-28 17:01:54 -040043 private static final String TAG = SipCall.class.getSimpleName();
Alexandre Savard2f1ae542012-10-26 17:05:00 -040044
alisionfde875f2013-05-28 17:01:54 -040045 private String mCallID = "";
Alexandre Lision3ee516e2013-10-07 17:32:15 -040046 private Account mAccount = null;
alision907bde72013-06-20 14:40:37 -040047 private CallContact contact = null;
alisiondf1dac92013-06-27 17:35:53 -040048 private boolean isRecording = false;
Alexandre Lision945e4612014-01-15 17:40:31 -050049 private long timestampStart_ = 0;
50 private long timestampEnd_ = 0;
51
Alexandre Lision67c70b42014-01-16 13:57:15 -050052 private int mCallType;
alisionfde875f2013-05-28 17:01:54 -040053 private int mCallState = state.CALL_STATE_NONE;
Alexandre Savard2b01c822012-09-20 15:00:37 -040054
Alexandre Lision5f144b82014-02-11 09:59:36 -050055 private boolean isSecured;
56 private String SAS;
57 private boolean confirmedSAS;
58
59
60 public boolean isSecured() {
61 return isSecured;
62 }
63
Alexandre Lision945e4612014-01-15 17:40:31 -050064 /**
65 * *********************
alisionfde875f2013-05-28 17:01:54 -040066 * Construtors
Alexandre Lision945e4612014-01-15 17:40:31 -050067 * *********************
68 */
Alexandre Savard557a5742012-10-24 13:54:53 -040069
alisionfde875f2013-05-28 17:01:54 -040070 private SipCall(Parcel in) {
Alexandre Savard557a5742012-10-24 13:54:53 -040071
Alexandre Lisiond5686032013-10-29 11:09:21 -040072 mCallID = in.readString();
Alexandre Lision3ee516e2013-10-07 17:32:15 -040073 mAccount = in.readParcelable(Account.class.getClassLoader());
alision907bde72013-06-20 14:40:37 -040074 contact = in.readParcelable(CallContact.class.getClassLoader());
alisiondf1dac92013-06-27 17:35:53 -040075 isRecording = in.readByte() == 1;
alisionfde875f2013-05-28 17:01:54 -040076 mCallType = in.readInt();
77 mCallState = in.readInt();
Alexandre Lision945e4612014-01-15 17:40:31 -050078 timestampStart_ = in.readLong();
79 timestampEnd_ = in.readLong();
Alexandre Lision5f144b82014-02-11 09:59:36 -050080 SAS = in.readString();
81 confirmedSAS = in.readByte() == 1;
82 isSecured = in.readByte() == 1;
Alexandre Lision945e4612014-01-15 17:40:31 -050083 }
Alexandre Savard4a19d752012-09-19 13:19:24 -040084
Alexandre Lision3e1e06f2014-02-10 12:13:25 -050085 private SipCall(String id, Account account, int call_type, int call_state, CallContact c) {
alisionfde875f2013-05-28 17:01:54 -040086 mCallID = id;
Alexandre Lision3ee516e2013-10-07 17:32:15 -040087 mAccount = account;
alisionfde875f2013-05-28 17:01:54 -040088 mCallType = call_type;
89 mCallState = call_state;
alision907bde72013-06-20 14:40:37 -040090 contact = c;
Alexandre Savard4a19d752012-09-19 13:19:24 -040091 }
92
Alexandre Lision945e4612014-01-15 17:40:31 -050093 public long getTimestampEnd_() {
94 return timestampEnd_;
95 }
96
97 public String getRecordPath() {
98 return "";
99 }
100
Alexandre Lision183bf452014-01-17 11:21:59 -0500101 public int getCallType() {
102 return mCallType;
103 }
104
Alexandre Lision5f144b82014-02-11 09:59:36 -0500105 public void setSecured(boolean secured) {
106 isSecured = secured;
107 }
108
Alexandre Lision67c70b42014-01-16 13:57:15 -0500109 public interface direction {
alisionfde875f2013-05-28 17:01:54 -0400110 public static final int CALL_TYPE_INCOMING = 1;
111 public static final int CALL_TYPE_OUTGOING = 2;
Alexandre Lision67c70b42014-01-16 13:57:15 -0500112 }
alisionfde875f2013-05-28 17:01:54 -0400113
Alexandre Lision67c70b42014-01-16 13:57:15 -0500114 public interface state {
alisionfde875f2013-05-28 17:01:54 -0400115 public static final int CALL_STATE_NONE = 0;
alisionfde875f2013-05-28 17:01:54 -0400116 public static final int CALL_STATE_RINGING = 2;
117 public static final int CALL_STATE_CURRENT = 3;
118 public static final int CALL_STATE_HUNGUP = 4;
119 public static final int CALL_STATE_BUSY = 5;
120 public static final int CALL_STATE_FAILURE = 6;
121 public static final int CALL_STATE_HOLD = 7;
122 public static final int CALL_STATE_UNHOLD = 8;
Alexandre Savard4a19d752012-09-19 13:19:24 -0400123 }
124
alisionfde875f2013-05-28 17:01:54 -0400125 @Override
126 public int describeContents() {
127 return 0;
128 }
129
130 @Override
131 public void writeToParcel(Parcel out, int flags) {
alisionfde875f2013-05-28 17:01:54 -0400132
Alexandre Lisiond5686032013-10-29 11:09:21 -0400133 out.writeString(mCallID);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400134 out.writeParcelable(mAccount, 0);
135
alision907bde72013-06-20 14:40:37 -0400136 out.writeParcelable(contact, 0);
alisiondf1dac92013-06-27 17:35:53 -0400137 out.writeByte((byte) (isRecording ? 1 : 0));
alisionfde875f2013-05-28 17:01:54 -0400138 out.writeInt(mCallType);
139 out.writeInt(mCallState);
Alexandre Lision945e4612014-01-15 17:40:31 -0500140 out.writeLong(timestampStart_);
141 out.writeLong(timestampEnd_);
Alexandre Lision5f144b82014-02-11 09:59:36 -0500142 out.writeString(SAS);
143 out.writeByte((byte) (confirmedSAS ? 1 : 0));
144 out.writeByte((byte) (isSecured ? 1 : 0));
alisionfde875f2013-05-28 17:01:54 -0400145 }
146
147 public static final Parcelable.Creator<SipCall> CREATOR = new Parcelable.Creator<SipCall>() {
148 public SipCall createFromParcel(Parcel in) {
149 return new SipCall(in);
150 }
151
152 public SipCall[] newArray(int size) {
153 return new SipCall[size];
154 }
155 };
156
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400157 public void setCallID(String callID) {
alisionfde875f2013-05-28 17:01:54 -0400158 mCallID = callID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400159 }
160
161 public String getCallId() {
alisionfde875f2013-05-28 17:01:54 -0400162 return mCallID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400163 }
164
Alexandre Lision945e4612014-01-15 17:40:31 -0500165 public long getTimestampStart_() {
166 return timestampStart_;
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400167 }
168
Alexandre Lision945e4612014-01-15 17:40:31 -0500169 public void setTimestampStart_(long timestampStart_) {
170 this.timestampStart_ = timestampStart_;
171 }
172
173 public void setTimestampEnd_(long timestampEnd_) {
174 this.timestampEnd_ = timestampEnd_;
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400175 }
176
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400177 public void setAccount(Account account) {
178 mAccount = account;
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400179 }
180
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400181 public Account getAccount() {
182 return mAccount;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400183 }
184
alision85704182013-05-29 15:23:03 -0400185 public String getCallTypeString() {
186 switch (mCallType) {
Alexandre Lision67c70b42014-01-16 13:57:15 -0500187 case direction.CALL_TYPE_INCOMING:
Alexandre Lision945e4612014-01-15 17:40:31 -0500188 return "CALL_TYPE_INCOMING";
Alexandre Lision67c70b42014-01-16 13:57:15 -0500189 case direction.CALL_TYPE_OUTGOING:
Alexandre Lision945e4612014-01-15 17:40:31 -0500190 return "CALL_TYPE_OUTGOING";
191 default:
192 return "CALL_TYPE_UNDETERMINED";
alision85704182013-05-29 15:23:03 -0400193 }
194 }
195
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400196 public void setCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400197 mCallState = callState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400198 }
199
alision907bde72013-06-20 14:40:37 -0400200 public CallContact getContact() {
201 return contact;
alisionfde875f2013-05-28 17:01:54 -0400202 }
203
alision907bde72013-06-20 14:40:37 -0400204 public void setContact(CallContact contacts) {
205 contact = contacts;
alisionfde875f2013-05-28 17:01:54 -0400206 }
207
Alexandre Savarddf544262012-10-25 14:24:08 -0400208 public String getCallStateString() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400209
alisionfde875f2013-05-28 17:01:54 -0400210 String text_state;
211
212 switch (mCallState) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500213 case state.CALL_STATE_RINGING:
214 text_state = "RINGING";
215 break;
216 case state.CALL_STATE_CURRENT:
217 text_state = "CURRENT";
218 break;
219 case state.CALL_STATE_HUNGUP:
220 text_state = "HUNGUP";
221 break;
222 case state.CALL_STATE_BUSY:
223 text_state = "BUSY";
224 break;
225 case state.CALL_STATE_FAILURE:
226 text_state = "FAILURE";
227 break;
228 case state.CALL_STATE_HOLD:
229 text_state = "HOLD";
230 break;
231 case state.CALL_STATE_UNHOLD:
232 text_state = "UNHOLD";
233 break;
234 default:
235 text_state = "NULL";
Alexandre Savarddf544262012-10-25 14:24:08 -0400236 }
237
alisionfde875f2013-05-28 17:01:54 -0400238 return text_state;
Alexandre Savarddf544262012-10-25 14:24:08 -0400239 }
240
alisiondf1dac92013-06-27 17:35:53 -0400241 public boolean isRecording() {
242 return isRecording;
243 }
244
245 public void setRecording(boolean isRecording) {
246 this.isRecording = isRecording;
247 }
248
alisionfde875f2013-05-28 17:01:54 -0400249 public static class SipCallBuilder {
250
251 private String bCallID = "";
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400252 private Account bAccount = null;
alision907bde72013-06-20 14:40:37 -0400253 private CallContact bContact = null;
alisionfde875f2013-05-28 17:01:54 -0400254
Alexandre Lision67c70b42014-01-16 13:57:15 -0500255 private int bCallType;
alisionfde875f2013-05-28 17:01:54 -0400256 private int bCallState = state.CALL_STATE_NONE;
alisionfde875f2013-05-28 17:01:54 -0400257
alisionfde875f2013-05-28 17:01:54 -0400258 public SipCallBuilder setCallType(int bCallType) {
259 this.bCallType = bCallType;
260 return this;
261 }
262
alisionfde875f2013-05-28 17:01:54 -0400263 public SipCallBuilder setCallState(int state) {
264 this.bCallState = state;
265 return this;
266 }
alision85704182013-05-29 15:23:03 -0400267
alisionfde875f2013-05-28 17:01:54 -0400268 public SipCallBuilder startCallCreation(String id) {
269 bCallID = id;
Alexandre Lision67c70b42014-01-16 13:57:15 -0500270 bCallType = direction.CALL_TYPE_INCOMING;
alisionfde875f2013-05-28 17:01:54 -0400271 return this;
272 }
273
274 public SipCallBuilder startCallCreation() {
275 Random random = new Random();
Tristan Matthews1a4962d2013-07-18 16:27:10 -0400276 bCallID = Integer.toString(Math.abs(random.nextInt()));
alisionfde875f2013-05-28 17:01:54 -0400277 return this;
278 }
279
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400280 public SipCallBuilder setAccount(Account a) {
281 bAccount = a;
alisionfde875f2013-05-28 17:01:54 -0400282 return this;
283 }
284
alision907bde72013-06-20 14:40:37 -0400285 public SipCallBuilder setContact(CallContact c) {
286 bContact = c;
alisionfde875f2013-05-28 17:01:54 -0400287 return this;
288 }
289
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400290 public SipCall build() throws InvalidObjectException {
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400291 if (bCallID.contentEquals("") || bAccount == null || bContact == null) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400292 throw new InvalidObjectException("SipCallBuilder's parameters missing");
alisionfde875f2013-05-28 17:01:54 -0400293 }
Alexandre Lision3e1e06f2014-02-10 12:13:25 -0500294 return new SipCall(bCallID, bAccount, bCallType, bCallState, bContact);
alisionfde875f2013-05-28 17:01:54 -0400295 }
296
297 public static SipCallBuilder getInstance() {
298 return new SipCallBuilder();
299 }
alisionfde875f2013-05-28 17:01:54 -0400300 }
301
alision85704182013-05-29 15:23:03 -0400302 public void printCallInfo() {
303 Log.i(TAG, "CallInfo: CallID: " + mCallID);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400304 Log.i(TAG, " AccountID: " + mAccount.getAccountID());
alision85704182013-05-29 15:23:03 -0400305 Log.i(TAG, " CallState: " + mCallState);
306 Log.i(TAG, " CallType: " + mCallType);
307 }
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400308
alision5f899632013-04-22 17:26:56 -0400309 /**
310 * Compare sip calls based on call ID
311 */
312 @Override
alision11e8e162013-05-28 10:33:14 -0400313 public boolean equals(Object c) {
alision465ceba2013-07-04 09:24:30 -0400314 if (c instanceof SipCall && ((SipCall) c).mCallID.contentEquals((mCallID))) {
alision5f899632013-04-22 17:26:56 -0400315 return true;
316 }
317 return false;
alision11e8e162013-05-28 10:33:14 -0400318
alision5f899632013-04-22 17:26:56 -0400319 }
alision7f18fc82013-05-01 09:37:33 -0400320
alision85704182013-05-29 15:23:03 -0400321 public boolean isOutGoing() {
Alexandre Lision67c70b42014-01-16 13:57:15 -0500322 if (mCallType == direction.CALL_TYPE_OUTGOING)
alision85704182013-05-29 15:23:03 -0400323 return true;
alision85704182013-05-29 15:23:03 -0400324 return false;
325 }
326
327 public boolean isRinging() {
328 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE)
329 return true;
330
331 return false;
332 }
333
334 public boolean isIncoming() {
Alexandre Lision67c70b42014-01-16 13:57:15 -0500335 if (mCallType == direction.CALL_TYPE_INCOMING)
alision85704182013-05-29 15:23:03 -0400336 return true;
337
338 return false;
339 }
340
341 public void setCallState(String newState) {
Alexandre Lision707f9082014-01-16 15:09:07 -0500342 if (newState.equals("RINGING")) {
alision85704182013-05-29 15:23:03 -0400343 setCallState(SipCall.state.CALL_STATE_RINGING);
344 } else if (newState.equals("CURRENT")) {
345 setCallState(SipCall.state.CALL_STATE_CURRENT);
346 } else if (newState.equals("HUNGUP")) {
347 setCallState(SipCall.state.CALL_STATE_HUNGUP);
348 } else if (newState.equals("BUSY")) {
349 setCallState(SipCall.state.CALL_STATE_BUSY);
350 } else if (newState.equals("FAILURE")) {
351 setCallState(SipCall.state.CALL_STATE_FAILURE);
352 } else if (newState.equals("HOLD")) {
353 setCallState(SipCall.state.CALL_STATE_HOLD);
354 } else if (newState.equals("UNHOLD")) {
355 setCallState(SipCall.state.CALL_STATE_CURRENT);
356 } else {
357 setCallState(SipCall.state.CALL_STATE_NONE);
358 }
alision907bde72013-06-20 14:40:37 -0400359
alision85704182013-05-29 15:23:03 -0400360 }
361
362 public boolean isOngoing() {
alision907bde72013-06-20 14:40:37 -0400363 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE || mCallState == state.CALL_STATE_FAILURE
364 || mCallState == state.CALL_STATE_BUSY || mCallState == state.CALL_STATE_HUNGUP)
alision85704182013-05-29 15:23:03 -0400365 return false;
366
367 return true;
368 }
alision04a00182013-05-10 17:05:29 -0400369
alisiondf1dac92013-06-27 17:35:53 -0400370 public boolean isOnHold() {
371 return mCallState == state.CALL_STATE_HOLD;
372 }
373
Alexandre Lision6ae652d2013-09-26 16:39:20 -0400374 public boolean isCurrent() {
375 return mCallState == state.CALL_STATE_CURRENT;
376 }
Alexandre Lision5f144b82014-02-11 09:59:36 -0500377
378 public boolean isConfirmedSAS() {
379 return confirmedSAS;
380 }
381
382 public void setConfirmedSAS(boolean confirmedSAS) {
383 this.confirmedSAS = confirmedSAS;
384 }
385
386 public String getSAS() {
387 return SAS;
388 }
389
390 public void setSAS(String SAS) {
391 this.SAS = SAS;
392 }
Alexandre Savard4a19d752012-09-19 13:19:24 -0400393}