blob: ec2fddb0a23f5d42baca534900248108d7e1beb7 [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;
Alexandre Lision3c6b7102013-09-16 16:56:46 -040050 private long timestamp_start = 0;
alisiondf1dac92013-06-27 17:35:53 -040051
alision50fa0722013-06-25 17:29:44 -040052 public static final String USER_ID = "user_id";
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) {
63 ArrayList<String> list = in.createStringArrayList();
Alexandre Savard557a5742012-10-24 13:54:53 -040064
alisionfde875f2013-05-28 17:01:54 -040065 mCallID = list.get(0);
66 mAccountID = list.get(1);
alision907bde72013-06-20 14:40:37 -040067 contact = in.readParcelable(CallContact.class.getClassLoader());
alisiondf1dac92013-06-27 17:35:53 -040068 isRecording = in.readByte() == 1;
alisionfde875f2013-05-28 17:01:54 -040069 mCallType = in.readInt();
70 mCallState = in.readInt();
71 mMediaState = in.readInt();
Alexandre Lisionebeb3662013-09-17 16:20:54 -040072 timestamp_start = in.readLong();
Alexandre Savard7a46f542012-09-20 11:23:33 -040073 }
Alexandre Savard4a19d752012-09-19 13:19:24 -040074
alisionfde875f2013-05-28 17:01:54 -040075
alision907bde72013-06-20 14:40:37 -040076 public SipCall(String id, String account, int call_type, int call_state, int media_state, CallContact c) {
alisionfde875f2013-05-28 17:01:54 -040077 mCallID = id;
78 mAccountID = account;
79 mCallType = call_type;
80 mCallState = call_state;
81 mMediaState = media_state;
alision907bde72013-06-20 14:40:37 -040082 contact = c;
Alexandre Savard4a19d752012-09-19 13:19:24 -040083 }
84
alisionfde875f2013-05-28 17:01:54 -040085
86 public interface state {
87 public static final int CALL_TYPE_UNDETERMINED = 0;
88 public static final int CALL_TYPE_INCOMING = 1;
89 public static final int CALL_TYPE_OUTGOING = 2;
90
91 public static final int CALL_STATE_NONE = 0;
92 public static final int CALL_STATE_INCOMING = 1;
93 public static final int CALL_STATE_RINGING = 2;
94 public static final int CALL_STATE_CURRENT = 3;
95 public static final int CALL_STATE_HUNGUP = 4;
96 public static final int CALL_STATE_BUSY = 5;
97 public static final int CALL_STATE_FAILURE = 6;
98 public static final int CALL_STATE_HOLD = 7;
99 public static final int CALL_STATE_UNHOLD = 8;
100
101 public static final int MEDIA_STATE_NONE = 0; // No media currently
102 public static final int MEDIA_STATE_ACTIVE = 1; // Media is active
103 public static final int MEDIA_STATE_LOCAL_HOLD = 2; // Media is put on hold bu user
104 public static final int MEDIA_STATE_REMOTE_HOLD = 3; // Media is put on hold by peer
105 public static final int MEDIA_STATE_ERROR = 5; // Media is in error state
Alexandre Savard4a19d752012-09-19 13:19:24 -0400106 }
107
alisionfde875f2013-05-28 17:01:54 -0400108 @Override
109 public int describeContents() {
110 return 0;
111 }
112
113 @Override
114 public void writeToParcel(Parcel out, int flags) {
115 ArrayList<String> list = new ArrayList<String>();
116
117 // Don't mess with this order!!!
118 list.add(mCallID);
119 list.add(mAccountID);
120
121 out.writeStringList(list);
alision907bde72013-06-20 14:40:37 -0400122 out.writeParcelable(contact, 0);
alisiondf1dac92013-06-27 17:35:53 -0400123 out.writeByte((byte) (isRecording ? 1 : 0));
alisionfde875f2013-05-28 17:01:54 -0400124 out.writeInt(mCallType);
125 out.writeInt(mCallState);
126 out.writeInt(mMediaState);
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400127 out.writeLong(timestamp_start);
alisionfde875f2013-05-28 17:01:54 -0400128 }
129
130 public static final Parcelable.Creator<SipCall> CREATOR = new Parcelable.Creator<SipCall>() {
131 public SipCall createFromParcel(Parcel in) {
132 return new SipCall(in);
133 }
134
135 public SipCall[] newArray(int size) {
136 return new SipCall[size];
137 }
138 };
139
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400140 public void setCallID(String callID) {
alisionfde875f2013-05-28 17:01:54 -0400141 mCallID = callID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400142 }
143
144 public String getCallId() {
alisionfde875f2013-05-28 17:01:54 -0400145 return mCallID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400146 }
147
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400148 public long getTimestamp_start() {
149 return timestamp_start;
150 }
151
152
153 public void setTimestamp_start(long timestamp_start) {
154 this.timestamp_start = timestamp_start;
155 }
156
157
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400158 public void setAccountID(String accountID) {
alisionfde875f2013-05-28 17:01:54 -0400159 mAccountID = accountID;
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400160 }
161
162 public String getAccountID() {
alisionfde875f2013-05-28 17:01:54 -0400163 return mAccountID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400164 }
165
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400166 public void setCallType(int callType) {
alisionfde875f2013-05-28 17:01:54 -0400167 mCallType = callType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400168 }
169
170 public int getCallType() {
alisionfde875f2013-05-28 17:01:54 -0400171 return mCallType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400172 }
173
alision85704182013-05-29 15:23:03 -0400174 public String getCallTypeString() {
175 switch (mCallType) {
176 case state.CALL_TYPE_INCOMING:
177 return "CALL_TYPE_INCOMING";
178 case state.CALL_TYPE_OUTGOING:
179 return "CALL_TYPE_OUTGOING";
180 default:
181 return "CALL_TYPE_UNDETERMINED";
182 }
183 }
184
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400185 public void setCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400186 mCallState = callState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400187 }
188
Alexandre Savarddf544262012-10-25 14:24:08 -0400189 public int getCallStateInt() {
alisionfde875f2013-05-28 17:01:54 -0400190 return mCallState;
191 }
192
193 public String getmCallID() {
194 return mCallID;
195 }
196
197 public void setmCallID(String mCallID) {
198 this.mCallID = mCallID;
199 }
200
201 public String getmAccountID() {
202 return mAccountID;
203 }
204
205 public void setmAccountID(String mAccountID) {
206 this.mAccountID = mAccountID;
207 }
208
alision907bde72013-06-20 14:40:37 -0400209 public CallContact getContact() {
210 return contact;
alisionfde875f2013-05-28 17:01:54 -0400211 }
212
alision907bde72013-06-20 14:40:37 -0400213 public void setContact(CallContact contacts) {
214 contact = contacts;
alisionfde875f2013-05-28 17:01:54 -0400215 }
216
217 public int getmCallType() {
218 return mCallType;
219 }
220
221 public void setmCallType(int mCallType) {
222 this.mCallType = mCallType;
223 }
224
alisionfde875f2013-05-28 17:01:54 -0400225 public int getmMediaState() {
226 return mMediaState;
227 }
228
229 public void setmMediaState(int mMediaState) {
230 this.mMediaState = mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400231 }
232
Alexandre Savarddf544262012-10-25 14:24:08 -0400233 public String getCallStateString() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400234
alisionfde875f2013-05-28 17:01:54 -0400235 String text_state;
236
237 switch (mCallState) {
238 case state.CALL_STATE_INCOMING:
239 text_state = "INCOMING";
alision11e8e162013-05-28 10:33:14 -0400240 break;
alisionfde875f2013-05-28 17:01:54 -0400241 case state.CALL_STATE_RINGING:
242 text_state = "RINGING";
alision11e8e162013-05-28 10:33:14 -0400243 break;
alisionfde875f2013-05-28 17:01:54 -0400244 case state.CALL_STATE_CURRENT:
245 text_state = "CURRENT";
alision11e8e162013-05-28 10:33:14 -0400246 break;
alisionfde875f2013-05-28 17:01:54 -0400247 case state.CALL_STATE_HUNGUP:
248 text_state = "HUNGUP";
alision11e8e162013-05-28 10:33:14 -0400249 break;
alisionfde875f2013-05-28 17:01:54 -0400250 case state.CALL_STATE_BUSY:
251 text_state = "BUSY";
alision11e8e162013-05-28 10:33:14 -0400252 break;
alisionfde875f2013-05-28 17:01:54 -0400253 case state.CALL_STATE_FAILURE:
254 text_state = "FAILURE";
alision11e8e162013-05-28 10:33:14 -0400255 break;
alisionfde875f2013-05-28 17:01:54 -0400256 case state.CALL_STATE_HOLD:
257 text_state = "HOLD";
alision11e8e162013-05-28 10:33:14 -0400258 break;
alisionfde875f2013-05-28 17:01:54 -0400259 case state.CALL_STATE_UNHOLD:
260 text_state = "UNHOLD";
alision11e8e162013-05-28 10:33:14 -0400261 break;
262 default:
alisionfde875f2013-05-28 17:01:54 -0400263 text_state = "NULL";
Alexandre Savarddf544262012-10-25 14:24:08 -0400264 }
265
alisionfde875f2013-05-28 17:01:54 -0400266 return text_state;
Alexandre Savarddf544262012-10-25 14:24:08 -0400267 }
268
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400269 public void setMediaState(int mediaState) {
alisionfde875f2013-05-28 17:01:54 -0400270 mMediaState = mediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400271 }
272
273 public int getMediaState() {
alisionfde875f2013-05-28 17:01:54 -0400274 return mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400275 }
276
alisiondf1dac92013-06-27 17:35:53 -0400277 public boolean isRecording() {
278 return isRecording;
279 }
280
281 public void setRecording(boolean isRecording) {
282 this.isRecording = isRecording;
283 }
284
alisionfde875f2013-05-28 17:01:54 -0400285 public static class SipCallBuilder {
286
287 private String bCallID = "";
288 private String bAccountID = "";
alision907bde72013-06-20 14:40:37 -0400289 private CallContact bContact = null;
alisionfde875f2013-05-28 17:01:54 -0400290
291 private int bCallType = state.CALL_TYPE_UNDETERMINED;
292 private int bCallState = state.CALL_STATE_NONE;
293 private int bMediaState = state.MEDIA_STATE_NONE;
294
alisionfde875f2013-05-28 17:01:54 -0400295 public SipCallBuilder setCallType(int bCallType) {
296 this.bCallType = bCallType;
297 return this;
298 }
299
300 public SipCallBuilder setMediaState(int state) {
301 this.bMediaState = state;
302 return this;
303 }
304
305 public SipCallBuilder setCallState(int state) {
306 this.bCallState = state;
307 return this;
308 }
alision85704182013-05-29 15:23:03 -0400309
alisionfde875f2013-05-28 17:01:54 -0400310 private static final String TAG = SipCallBuilder.class.getSimpleName();
alision85704182013-05-29 15:23:03 -0400311
alisionfde875f2013-05-28 17:01:54 -0400312 public SipCallBuilder startCallCreation(String id) {
313 bCallID = id;
alisionfde875f2013-05-28 17:01:54 -0400314 bCallType = SipCall.state.CALL_TYPE_INCOMING;
315 return this;
316 }
317
318 public SipCallBuilder startCallCreation() {
319 Random random = new Random();
Tristan Matthews1a4962d2013-07-18 16:27:10 -0400320 bCallID = Integer.toString(Math.abs(random.nextInt()));
alisionfde875f2013-05-28 17:01:54 -0400321 return this;
322 }
323
324 public SipCallBuilder setAccountID(String h) {
325 Log.i(TAG, "setAccountID" + h);
326 bAccountID = h;
327 return this;
328 }
329
alision907bde72013-06-20 14:40:37 -0400330 public SipCallBuilder setContact(CallContact c) {
331 bContact = c;
alisionfde875f2013-05-28 17:01:54 -0400332 return this;
333 }
334
335 public SipCall build() throws Exception {
alision907bde72013-06-20 14:40:37 -0400336 if (bCallID.contentEquals("") || bAccountID.contentEquals("") || bContact == null) {
alisionfde875f2013-05-28 17:01:54 -0400337 throw new Exception("SipCallBuilder's parameters missing");
338 }
alision907bde72013-06-20 14:40:37 -0400339 return new SipCall(bCallID, bAccountID, bCallType, bCallState, bMediaState, bContact);
alisionfde875f2013-05-28 17:01:54 -0400340 }
341
342 public static SipCallBuilder getInstance() {
343 return new SipCallBuilder();
344 }
345
alision806e18e2013-06-21 15:30:17 -0400346 public static SipCall buildMyselfCall(ContentResolver cr, String displayName) {
347 return new SipCall("default", "default", SipCall.state.CALL_TYPE_UNDETERMINED, state.CALL_STATE_NONE, state.MEDIA_STATE_NONE,
alisiondf1dac92013-06-27 17:35:53 -0400348 CallContact.ContactBuilder.buildUserContact(cr, displayName));
alision806e18e2013-06-21 15:30:17 -0400349
350 }
351
alisionfde875f2013-05-28 17:01:54 -0400352 }
353
alision85704182013-05-29 15:23:03 -0400354 public void printCallInfo() {
355 Log.i(TAG, "CallInfo: CallID: " + mCallID);
356 Log.i(TAG, " AccountID: " + mAccountID);
357 Log.i(TAG, " CallState: " + mCallState);
358 Log.i(TAG, " CallType: " + mCallType);
359 }
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400360
alision5f899632013-04-22 17:26:56 -0400361 /**
362 * Compare sip calls based on call ID
363 */
364 @Override
alision11e8e162013-05-28 10:33:14 -0400365 public boolean equals(Object c) {
alision465ceba2013-07-04 09:24:30 -0400366 if (c instanceof SipCall && ((SipCall) c).mCallID.contentEquals((mCallID))) {
alision5f899632013-04-22 17:26:56 -0400367 return true;
368 }
369 return false;
alision11e8e162013-05-28 10:33:14 -0400370
alision5f899632013-04-22 17:26:56 -0400371 }
alision7f18fc82013-05-01 09:37:33 -0400372
alision85704182013-05-29 15:23:03 -0400373 public boolean isOutGoing() {
374 if (mCallType == state.CALL_TYPE_OUTGOING)
375 return true;
376
377 return false;
378 }
379
380 public boolean isRinging() {
381 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE)
382 return true;
383
384 return false;
385 }
386
387 public boolean isIncoming() {
388 if (mCallType == state.CALL_TYPE_INCOMING)
389 return true;
390
391 return false;
392 }
393
394 public void setCallState(String newState) {
395 if (newState.equals("INCOMING")) {
396 setCallState(SipCall.state.CALL_STATE_INCOMING);
397 } else if (newState.equals("RINGING")) {
398 setCallState(SipCall.state.CALL_STATE_RINGING);
399 } else if (newState.equals("CURRENT")) {
400 setCallState(SipCall.state.CALL_STATE_CURRENT);
401 } else if (newState.equals("HUNGUP")) {
402 setCallState(SipCall.state.CALL_STATE_HUNGUP);
403 } else if (newState.equals("BUSY")) {
404 setCallState(SipCall.state.CALL_STATE_BUSY);
405 } else if (newState.equals("FAILURE")) {
406 setCallState(SipCall.state.CALL_STATE_FAILURE);
407 } else if (newState.equals("HOLD")) {
408 setCallState(SipCall.state.CALL_STATE_HOLD);
409 } else if (newState.equals("UNHOLD")) {
410 setCallState(SipCall.state.CALL_STATE_CURRENT);
411 } else {
412 setCallState(SipCall.state.CALL_STATE_NONE);
413 }
alision907bde72013-06-20 14:40:37 -0400414
alision85704182013-05-29 15:23:03 -0400415 }
416
alisiondf1dac92013-06-27 17:35:53 -0400417
alision85704182013-05-29 15:23:03 -0400418 public boolean isOngoing() {
alision907bde72013-06-20 14:40:37 -0400419 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE || mCallState == state.CALL_STATE_FAILURE
420 || mCallState == state.CALL_STATE_BUSY || mCallState == state.CALL_STATE_HUNGUP)
alision85704182013-05-29 15:23:03 -0400421 return false;
422
423 return true;
424 }
alision04a00182013-05-10 17:05:29 -0400425
alisiondf1dac92013-06-27 17:35:53 -0400426 public boolean isOnHold() {
427 return mCallState == state.CALL_STATE_HOLD;
428 }
429
Alexandre Savard4a19d752012-09-19 13:19:24 -0400430}