blob: 2e7018ded030f620ba2281d8a6c54860d4a60761 [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;
alision50fa0722013-06-25 17:29:44 -040048
49 public static final String USER_ID = "user_id";
Alexandre Savard2b01c822012-09-20 15:00:37 -040050
alisionfde875f2013-05-28 17:01:54 -040051 private int mCallType = state.CALL_TYPE_UNDETERMINED;
52 private int mCallState = state.CALL_STATE_NONE;
53 private int mMediaState = state.MEDIA_STATE_NONE;
Alexandre Savard2b01c822012-09-20 15:00:37 -040054
alisionfde875f2013-05-28 17:01:54 -040055 /************************
56 * Construtors
alisionfde875f2013-05-28 17:01:54 -040057 ***********************/
Alexandre Savard557a5742012-10-24 13:54:53 -040058
alisionfde875f2013-05-28 17:01:54 -040059 private SipCall(Parcel in) {
60 ArrayList<String> list = in.createStringArrayList();
Alexandre Savard557a5742012-10-24 13:54:53 -040061
alisionfde875f2013-05-28 17:01:54 -040062 // Don't mess with this order!!!
63 mCallID = list.get(0);
64 mAccountID = list.get(1);
65 // mDisplayName = list.get(2);
66 // mPhone = list.get(3);
67 // mEmail = list.get(4);
68 // mRemoteContact = list.get(5);
Alexandre Savard74c1cad2012-10-24 16:39:00 -040069
alision907bde72013-06-20 14:40:37 -040070 contact = in.readParcelable(CallContact.class.getClassLoader());
Alexandre Savard557a5742012-10-24 13:54:53 -040071
alisionfde875f2013-05-28 17:01:54 -040072 mCallType = in.readInt();
73 mCallState = in.readInt();
74 mMediaState = in.readInt();
Alexandre Savard7a46f542012-09-20 11:23:33 -040075 }
Alexandre Savard4a19d752012-09-19 13:19:24 -040076
alisionfde875f2013-05-28 17:01:54 -040077 // public SipCall(Intent call) {
78
79 // }
80
alision907bde72013-06-20 14:40:37 -040081 public SipCall(String id, String account, int call_type, int call_state, int media_state, CallContact c) {
alisionfde875f2013-05-28 17:01:54 -040082 mCallID = id;
83 mAccountID = account;
84 mCallType = call_type;
85 mCallState = call_state;
86 mMediaState = media_state;
alision907bde72013-06-20 14:40:37 -040087 contact = c;
Alexandre Savard4a19d752012-09-19 13:19:24 -040088 }
89
alisionfde875f2013-05-28 17:01:54 -040090 // public SipCall() {
91 // }
92
93 public interface state {
94 public static final int CALL_TYPE_UNDETERMINED = 0;
95 public static final int CALL_TYPE_INCOMING = 1;
96 public static final int CALL_TYPE_OUTGOING = 2;
97
98 public static final int CALL_STATE_NONE = 0;
99 public static final int CALL_STATE_INCOMING = 1;
100 public static final int CALL_STATE_RINGING = 2;
101 public static final int CALL_STATE_CURRENT = 3;
102 public static final int CALL_STATE_HUNGUP = 4;
103 public static final int CALL_STATE_BUSY = 5;
104 public static final int CALL_STATE_FAILURE = 6;
105 public static final int CALL_STATE_HOLD = 7;
106 public static final int CALL_STATE_UNHOLD = 8;
107
108 public static final int MEDIA_STATE_NONE = 0; // No media currently
109 public static final int MEDIA_STATE_ACTIVE = 1; // Media is active
110 public static final int MEDIA_STATE_LOCAL_HOLD = 2; // Media is put on hold bu user
111 public static final int MEDIA_STATE_REMOTE_HOLD = 3; // Media is put on hold by peer
112 public static final int MEDIA_STATE_ERROR = 5; // Media is in error state
Alexandre Savard4a19d752012-09-19 13:19:24 -0400113 }
114
alisionfde875f2013-05-28 17:01:54 -0400115 @Override
116 public int describeContents() {
117 return 0;
118 }
119
120 @Override
121 public void writeToParcel(Parcel out, int flags) {
122 ArrayList<String> list = new ArrayList<String>();
123
124 // Don't mess with this order!!!
125 list.add(mCallID);
126 list.add(mAccountID);
127
128 out.writeStringList(list);
alision907bde72013-06-20 14:40:37 -0400129 out.writeParcelable(contact, 0);
alisionfde875f2013-05-28 17:01:54 -0400130 out.writeInt(mCallType);
131 out.writeInt(mCallState);
132 out.writeInt(mMediaState);
133 }
134
135 public static final Parcelable.Creator<SipCall> CREATOR = new Parcelable.Creator<SipCall>() {
136 public SipCall createFromParcel(Parcel in) {
137 return new SipCall(in);
138 }
139
140 public SipCall[] newArray(int size) {
141 return new SipCall[size];
142 }
143 };
144
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400145 public void setCallID(String callID) {
alisionfde875f2013-05-28 17:01:54 -0400146 mCallID = callID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400147 }
148
149 public String getCallId() {
alisionfde875f2013-05-28 17:01:54 -0400150 return mCallID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400151 }
152
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400153 public void setAccountID(String accountID) {
alisionfde875f2013-05-28 17:01:54 -0400154 mAccountID = accountID;
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400155 }
156
157 public String getAccountID() {
alisionfde875f2013-05-28 17:01:54 -0400158 return mAccountID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400159 }
160
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400161 public void setCallType(int callType) {
alisionfde875f2013-05-28 17:01:54 -0400162 mCallType = callType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400163 }
164
165 public int getCallType() {
alisionfde875f2013-05-28 17:01:54 -0400166 return mCallType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400167 }
168
alision85704182013-05-29 15:23:03 -0400169 public String getCallTypeString() {
170 switch (mCallType) {
171 case state.CALL_TYPE_INCOMING:
172 return "CALL_TYPE_INCOMING";
173 case state.CALL_TYPE_OUTGOING:
174 return "CALL_TYPE_OUTGOING";
175 default:
176 return "CALL_TYPE_UNDETERMINED";
177 }
178 }
179
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400180 public void setCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400181 mCallState = callState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400182 }
183
Alexandre Savarddf544262012-10-25 14:24:08 -0400184 public int getCallStateInt() {
alisionfde875f2013-05-28 17:01:54 -0400185 return mCallState;
186 }
187
188 public String getmCallID() {
189 return mCallID;
190 }
191
192 public void setmCallID(String mCallID) {
193 this.mCallID = mCallID;
194 }
195
196 public String getmAccountID() {
197 return mAccountID;
198 }
199
200 public void setmAccountID(String mAccountID) {
201 this.mAccountID = mAccountID;
202 }
203
alision907bde72013-06-20 14:40:37 -0400204 public CallContact getContact() {
205 return contact;
alisionfde875f2013-05-28 17:01:54 -0400206 }
207
alision907bde72013-06-20 14:40:37 -0400208 public void setContact(CallContact contacts) {
209 contact = contacts;
alisionfde875f2013-05-28 17:01:54 -0400210 }
211
212 public int getmCallType() {
213 return mCallType;
214 }
215
216 public void setmCallType(int mCallType) {
217 this.mCallType = mCallType;
218 }
219
alisionfde875f2013-05-28 17:01:54 -0400220 public int getmMediaState() {
221 return mMediaState;
222 }
223
224 public void setmMediaState(int mMediaState) {
225 this.mMediaState = mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400226 }
227
Alexandre Savarddf544262012-10-25 14:24:08 -0400228 public String getCallStateString() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400229
alisionfde875f2013-05-28 17:01:54 -0400230 String text_state;
231
232 switch (mCallState) {
233 case state.CALL_STATE_INCOMING:
234 text_state = "INCOMING";
alision11e8e162013-05-28 10:33:14 -0400235 break;
alisionfde875f2013-05-28 17:01:54 -0400236 case state.CALL_STATE_RINGING:
237 text_state = "RINGING";
alision11e8e162013-05-28 10:33:14 -0400238 break;
alisionfde875f2013-05-28 17:01:54 -0400239 case state.CALL_STATE_CURRENT:
240 text_state = "CURRENT";
alision11e8e162013-05-28 10:33:14 -0400241 break;
alisionfde875f2013-05-28 17:01:54 -0400242 case state.CALL_STATE_HUNGUP:
243 text_state = "HUNGUP";
alision11e8e162013-05-28 10:33:14 -0400244 break;
alisionfde875f2013-05-28 17:01:54 -0400245 case state.CALL_STATE_BUSY:
246 text_state = "BUSY";
alision11e8e162013-05-28 10:33:14 -0400247 break;
alisionfde875f2013-05-28 17:01:54 -0400248 case state.CALL_STATE_FAILURE:
249 text_state = "FAILURE";
alision11e8e162013-05-28 10:33:14 -0400250 break;
alisionfde875f2013-05-28 17:01:54 -0400251 case state.CALL_STATE_HOLD:
252 text_state = "HOLD";
alision11e8e162013-05-28 10:33:14 -0400253 break;
alisionfde875f2013-05-28 17:01:54 -0400254 case state.CALL_STATE_UNHOLD:
255 text_state = "UNHOLD";
alision11e8e162013-05-28 10:33:14 -0400256 break;
257 default:
alisionfde875f2013-05-28 17:01:54 -0400258 text_state = "NULL";
Alexandre Savarddf544262012-10-25 14:24:08 -0400259 }
260
alisionfde875f2013-05-28 17:01:54 -0400261 return text_state;
Alexandre Savarddf544262012-10-25 14:24:08 -0400262 }
263
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400264 public void setMediaState(int mediaState) {
alisionfde875f2013-05-28 17:01:54 -0400265 mMediaState = mediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400266 }
267
268 public int getMediaState() {
alisionfde875f2013-05-28 17:01:54 -0400269 return mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400270 }
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();
307 bCallID = Integer.toString(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,
335 CallContact.ContactBuilder.buildUserContact(cr, ""));
336
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) {
alisionfde875f2013-05-28 17:01:54 -0400353 if (c instanceof SipCall && ((SipCall) c).mCallID == 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
404 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
Alexandre Savard4a19d752012-09-19 13:19:24 -0400412}