blob: 2d83e99ab45f90694db5a75d04a69d1b5aa01510 [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;
alisiona4325152013-04-19 11:10:03 -040035
alision806e18e2013-06-21 15:30:17 -040036import android.content.ContentResolver;
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 = "";
46 private String mAccountID = "";
alision907bde72013-06-20 14:40:37 -040047 private CallContact contact = null;
alisiondf1dac92013-06-27 17:35:53 -040048 private boolean isRecording = false;
49
alision50fa0722013-06-25 17:29:44 -040050 public static final String USER_ID = "user_id";
Alexandre Savard2b01c822012-09-20 15:00:37 -040051
alisionfde875f2013-05-28 17:01:54 -040052 private int mCallType = state.CALL_TYPE_UNDETERMINED;
53 private int mCallState = state.CALL_STATE_NONE;
54 private int mMediaState = state.MEDIA_STATE_NONE;
Alexandre Savard2b01c822012-09-20 15:00:37 -040055
alisionfde875f2013-05-28 17:01:54 -040056 /************************
57 * Construtors
alisionfde875f2013-05-28 17:01:54 -040058 ***********************/
Alexandre Savard557a5742012-10-24 13:54:53 -040059
alisionfde875f2013-05-28 17:01:54 -040060 private SipCall(Parcel in) {
61 ArrayList<String> list = in.createStringArrayList();
Alexandre Savard557a5742012-10-24 13:54:53 -040062
alisionfde875f2013-05-28 17:01:54 -040063 mCallID = list.get(0);
64 mAccountID = list.get(1);
alision907bde72013-06-20 14:40:37 -040065 contact = in.readParcelable(CallContact.class.getClassLoader());
alisiondf1dac92013-06-27 17:35:53 -040066 isRecording = in.readByte() == 1;
alisionfde875f2013-05-28 17:01:54 -040067 mCallType = in.readInt();
68 mCallState = in.readInt();
69 mMediaState = in.readInt();
Alexandre Savard7a46f542012-09-20 11:23:33 -040070 }
Alexandre Savard4a19d752012-09-19 13:19:24 -040071
alisionfde875f2013-05-28 17:01:54 -040072
alision907bde72013-06-20 14:40:37 -040073 public SipCall(String id, String account, int call_type, int call_state, int media_state, CallContact c) {
alisionfde875f2013-05-28 17:01:54 -040074 mCallID = id;
75 mAccountID = account;
76 mCallType = call_type;
77 mCallState = call_state;
78 mMediaState = media_state;
alision907bde72013-06-20 14:40:37 -040079 contact = c;
Alexandre Savard4a19d752012-09-19 13:19:24 -040080 }
81
alisionfde875f2013-05-28 17:01:54 -040082
83 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) {
112 ArrayList<String> list = new ArrayList<String>();
113
114 // Don't mess with this order!!!
115 list.add(mCallID);
116 list.add(mAccountID);
117
118 out.writeStringList(list);
alision907bde72013-06-20 14:40:37 -0400119 out.writeParcelable(contact, 0);
alisiondf1dac92013-06-27 17:35:53 -0400120 out.writeByte((byte) (isRecording ? 1 : 0));
alisionfde875f2013-05-28 17:01:54 -0400121 out.writeInt(mCallType);
122 out.writeInt(mCallState);
123 out.writeInt(mMediaState);
124 }
125
126 public static final Parcelable.Creator<SipCall> CREATOR = new Parcelable.Creator<SipCall>() {
127 public SipCall createFromParcel(Parcel in) {
128 return new SipCall(in);
129 }
130
131 public SipCall[] newArray(int size) {
132 return new SipCall[size];
133 }
134 };
135
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400136 public void setCallID(String callID) {
alisionfde875f2013-05-28 17:01:54 -0400137 mCallID = callID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400138 }
139
140 public String getCallId() {
alisionfde875f2013-05-28 17:01:54 -0400141 return mCallID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400142 }
143
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400144 public void setAccountID(String accountID) {
alisionfde875f2013-05-28 17:01:54 -0400145 mAccountID = accountID;
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400146 }
147
148 public String getAccountID() {
alisionfde875f2013-05-28 17:01:54 -0400149 return mAccountID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400150 }
151
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400152 public void setCallType(int callType) {
alisionfde875f2013-05-28 17:01:54 -0400153 mCallType = callType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400154 }
155
156 public int getCallType() {
alisionfde875f2013-05-28 17:01:54 -0400157 return mCallType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400158 }
159
alision85704182013-05-29 15:23:03 -0400160 public String getCallTypeString() {
161 switch (mCallType) {
162 case state.CALL_TYPE_INCOMING:
163 return "CALL_TYPE_INCOMING";
164 case state.CALL_TYPE_OUTGOING:
165 return "CALL_TYPE_OUTGOING";
166 default:
167 return "CALL_TYPE_UNDETERMINED";
168 }
169 }
170
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400171 public void setCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400172 mCallState = callState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400173 }
174
Alexandre Savarddf544262012-10-25 14:24:08 -0400175 public int getCallStateInt() {
alisionfde875f2013-05-28 17:01:54 -0400176 return mCallState;
177 }
178
179 public String getmCallID() {
180 return mCallID;
181 }
182
183 public void setmCallID(String mCallID) {
184 this.mCallID = mCallID;
185 }
186
187 public String getmAccountID() {
188 return mAccountID;
189 }
190
191 public void setmAccountID(String mAccountID) {
192 this.mAccountID = mAccountID;
193 }
194
alision907bde72013-06-20 14:40:37 -0400195 public CallContact getContact() {
196 return contact;
alisionfde875f2013-05-28 17:01:54 -0400197 }
198
alision907bde72013-06-20 14:40:37 -0400199 public void setContact(CallContact contacts) {
200 contact = contacts;
alisionfde875f2013-05-28 17:01:54 -0400201 }
202
203 public int getmCallType() {
204 return mCallType;
205 }
206
207 public void setmCallType(int mCallType) {
208 this.mCallType = mCallType;
209 }
210
alisionfde875f2013-05-28 17:01:54 -0400211 public int getmMediaState() {
212 return mMediaState;
213 }
214
215 public void setmMediaState(int mMediaState) {
216 this.mMediaState = mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400217 }
218
Alexandre Savarddf544262012-10-25 14:24:08 -0400219 public String getCallStateString() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400220
alisionfde875f2013-05-28 17:01:54 -0400221 String text_state;
222
223 switch (mCallState) {
224 case state.CALL_STATE_INCOMING:
225 text_state = "INCOMING";
alision11e8e162013-05-28 10:33:14 -0400226 break;
alisionfde875f2013-05-28 17:01:54 -0400227 case state.CALL_STATE_RINGING:
228 text_state = "RINGING";
alision11e8e162013-05-28 10:33:14 -0400229 break;
alisionfde875f2013-05-28 17:01:54 -0400230 case state.CALL_STATE_CURRENT:
231 text_state = "CURRENT";
alision11e8e162013-05-28 10:33:14 -0400232 break;
alisionfde875f2013-05-28 17:01:54 -0400233 case state.CALL_STATE_HUNGUP:
234 text_state = "HUNGUP";
alision11e8e162013-05-28 10:33:14 -0400235 break;
alisionfde875f2013-05-28 17:01:54 -0400236 case state.CALL_STATE_BUSY:
237 text_state = "BUSY";
alision11e8e162013-05-28 10:33:14 -0400238 break;
alisionfde875f2013-05-28 17:01:54 -0400239 case state.CALL_STATE_FAILURE:
240 text_state = "FAILURE";
alision11e8e162013-05-28 10:33:14 -0400241 break;
alisionfde875f2013-05-28 17:01:54 -0400242 case state.CALL_STATE_HOLD:
243 text_state = "HOLD";
alision11e8e162013-05-28 10:33:14 -0400244 break;
alisionfde875f2013-05-28 17:01:54 -0400245 case state.CALL_STATE_UNHOLD:
246 text_state = "UNHOLD";
alision11e8e162013-05-28 10:33:14 -0400247 break;
248 default:
alisionfde875f2013-05-28 17:01:54 -0400249 text_state = "NULL";
Alexandre Savarddf544262012-10-25 14:24:08 -0400250 }
251
alisionfde875f2013-05-28 17:01:54 -0400252 return text_state;
Alexandre Savarddf544262012-10-25 14:24:08 -0400253 }
254
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400255 public void setMediaState(int mediaState) {
alisionfde875f2013-05-28 17:01:54 -0400256 mMediaState = mediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400257 }
258
259 public int getMediaState() {
alisionfde875f2013-05-28 17:01:54 -0400260 return mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400261 }
262
alisiondf1dac92013-06-27 17:35:53 -0400263 public boolean isRecording() {
264 return isRecording;
265 }
266
267 public void setRecording(boolean isRecording) {
268 this.isRecording = isRecording;
269 }
270
alisionfde875f2013-05-28 17:01:54 -0400271 public static class SipCallBuilder {
272
273 private String bCallID = "";
274 private String bAccountID = "";
alision907bde72013-06-20 14:40:37 -0400275 private CallContact bContact = null;
alisionfde875f2013-05-28 17:01:54 -0400276
277 private int bCallType = state.CALL_TYPE_UNDETERMINED;
278 private int bCallState = state.CALL_STATE_NONE;
279 private int bMediaState = state.MEDIA_STATE_NONE;
280
alisionfde875f2013-05-28 17:01:54 -0400281 public SipCallBuilder setCallType(int bCallType) {
282 this.bCallType = bCallType;
283 return this;
284 }
285
286 public SipCallBuilder setMediaState(int state) {
287 this.bMediaState = state;
288 return this;
289 }
290
291 public SipCallBuilder setCallState(int state) {
292 this.bCallState = state;
293 return this;
294 }
alision85704182013-05-29 15:23:03 -0400295
alisionfde875f2013-05-28 17:01:54 -0400296 private static final String TAG = SipCallBuilder.class.getSimpleName();
alision85704182013-05-29 15:23:03 -0400297
alisionfde875f2013-05-28 17:01:54 -0400298 public SipCallBuilder startCallCreation(String id) {
299 bCallID = id;
alisionfde875f2013-05-28 17:01:54 -0400300 bCallType = SipCall.state.CALL_TYPE_INCOMING;
301 return this;
302 }
303
304 public SipCallBuilder startCallCreation() {
305 Random random = new Random();
306 bCallID = Integer.toString(random.nextInt());
alisionfde875f2013-05-28 17:01:54 -0400307 return this;
308 }
309
310 public SipCallBuilder setAccountID(String h) {
311 Log.i(TAG, "setAccountID" + h);
312 bAccountID = h;
313 return this;
314 }
315
alision907bde72013-06-20 14:40:37 -0400316 public SipCallBuilder setContact(CallContact c) {
317 bContact = c;
alisionfde875f2013-05-28 17:01:54 -0400318 return this;
319 }
320
321 public SipCall build() throws Exception {
alision907bde72013-06-20 14:40:37 -0400322 if (bCallID.contentEquals("") || bAccountID.contentEquals("") || bContact == null) {
alisionfde875f2013-05-28 17:01:54 -0400323 throw new Exception("SipCallBuilder's parameters missing");
324 }
alision907bde72013-06-20 14:40:37 -0400325 return new SipCall(bCallID, bAccountID, bCallType, bCallState, bMediaState, bContact);
alisionfde875f2013-05-28 17:01:54 -0400326 }
327
328 public static SipCallBuilder getInstance() {
329 return new SipCallBuilder();
330 }
331
alision806e18e2013-06-21 15:30:17 -0400332 public static SipCall buildMyselfCall(ContentResolver cr, String displayName) {
333 return new SipCall("default", "default", SipCall.state.CALL_TYPE_UNDETERMINED, state.CALL_STATE_NONE, state.MEDIA_STATE_NONE,
alisiondf1dac92013-06-27 17:35:53 -0400334 CallContact.ContactBuilder.buildUserContact(cr, displayName));
alision806e18e2013-06-21 15:30:17 -0400335
336 }
337
alisionfde875f2013-05-28 17:01:54 -0400338 }
339
alision85704182013-05-29 15:23:03 -0400340 public void printCallInfo() {
341 Log.i(TAG, "CallInfo: CallID: " + mCallID);
342 Log.i(TAG, " AccountID: " + mAccountID);
343 Log.i(TAG, " CallState: " + mCallState);
344 Log.i(TAG, " CallType: " + mCallType);
345 }
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400346
alision5f899632013-04-22 17:26:56 -0400347 /**
348 * Compare sip calls based on call ID
349 */
350 @Override
alision11e8e162013-05-28 10:33:14 -0400351 public boolean equals(Object c) {
alision465ceba2013-07-04 09:24:30 -0400352 if (c instanceof SipCall && ((SipCall) c).mCallID.contentEquals((mCallID))) {
alision5f899632013-04-22 17:26:56 -0400353 return true;
354 }
355 return false;
alision11e8e162013-05-28 10:33:14 -0400356
alision5f899632013-04-22 17:26:56 -0400357 }
alision7f18fc82013-05-01 09:37:33 -0400358
alision85704182013-05-29 15:23:03 -0400359 public boolean isOutGoing() {
360 if (mCallType == state.CALL_TYPE_OUTGOING)
361 return true;
362
363 return false;
364 }
365
366 public boolean isRinging() {
367 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE)
368 return true;
369
370 return false;
371 }
372
373 public boolean isIncoming() {
374 if (mCallType == state.CALL_TYPE_INCOMING)
375 return true;
376
377 return false;
378 }
379
380 public void setCallState(String newState) {
381 if (newState.equals("INCOMING")) {
382 setCallState(SipCall.state.CALL_STATE_INCOMING);
383 } else if (newState.equals("RINGING")) {
384 setCallState(SipCall.state.CALL_STATE_RINGING);
385 } else if (newState.equals("CURRENT")) {
386 setCallState(SipCall.state.CALL_STATE_CURRENT);
387 } else if (newState.equals("HUNGUP")) {
388 setCallState(SipCall.state.CALL_STATE_HUNGUP);
389 } else if (newState.equals("BUSY")) {
390 setCallState(SipCall.state.CALL_STATE_BUSY);
391 } else if (newState.equals("FAILURE")) {
392 setCallState(SipCall.state.CALL_STATE_FAILURE);
393 } else if (newState.equals("HOLD")) {
394 setCallState(SipCall.state.CALL_STATE_HOLD);
395 } else if (newState.equals("UNHOLD")) {
396 setCallState(SipCall.state.CALL_STATE_CURRENT);
397 } else {
398 setCallState(SipCall.state.CALL_STATE_NONE);
399 }
alision907bde72013-06-20 14:40:37 -0400400
alision85704182013-05-29 15:23:03 -0400401 }
402
alisiondf1dac92013-06-27 17:35:53 -0400403
alision85704182013-05-29 15:23:03 -0400404 public boolean isOngoing() {
alision907bde72013-06-20 14:40:37 -0400405 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE || mCallState == state.CALL_STATE_FAILURE
406 || mCallState == state.CALL_STATE_BUSY || mCallState == state.CALL_STATE_HUNGUP)
alision85704182013-05-29 15:23:03 -0400407 return false;
408
409 return true;
410 }
alision04a00182013-05-10 17:05:29 -0400411
alisiondf1dac92013-06-27 17:35:53 -0400412 public boolean isOnHold() {
413 return mCallState == state.CALL_STATE_HOLD;
414 }
415
Alexandre Savard4a19d752012-09-19 13:19:24 -0400416}