blob: 62c719a231187362582103e8acba12bd7986858a [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
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040036import android.content.Intent;
37import android.os.Bundle;
alision04a00182013-05-10 17:05:29 -040038import android.os.Environment;
Alexandre Savard557a5742012-10-24 13:54:53 -040039import android.os.Parcel;
alisiona4325152013-04-19 11:10:03 -040040import android.os.Parcelable;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040041import android.os.RemoteException;
Alexandre Savard4a19d752012-09-19 13:19:24 -040042import android.util.Log;
Alexandre Savard4a19d752012-09-19 13:19:24 -040043
alisionfde875f2013-05-28 17:01:54 -040044import com.savoirfairelinux.sflphone.model.Account.AccountBuilder;
45import com.savoirfairelinux.sflphone.model.CallContact.ContactBuilder;
alisiona4325152013-04-19 11:10:03 -040046import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040047
alisionfde875f2013-05-28 17:01:54 -040048public class SipCall implements Parcelable {
alision371b77e2013-04-23 14:51:26 -040049
alisionfde875f2013-05-28 17:01:54 -040050 private static final String TAG = SipCall.class.getSimpleName();
Alexandre Savard2f1ae542012-10-26 17:05:00 -040051
alisionfde875f2013-05-28 17:01:54 -040052 private String mCallID = "";
53 private String mAccountID = "";
54 private ArrayList<CallContact> contacts = new ArrayList<CallContact>();
Alexandre Savard2b01c822012-09-20 15:00:37 -040055
alisionfde875f2013-05-28 17:01:54 -040056 private int mCallType = state.CALL_TYPE_UNDETERMINED;
57 private int mCallState = state.CALL_STATE_NONE;
58 private int mMediaState = state.MEDIA_STATE_NONE;
Alexandre Savard2b01c822012-09-20 15:00:37 -040059
alisionfde875f2013-05-28 17:01:54 -040060 /************************
61 * Construtors
62 *
63 ***********************/
Alexandre Savard557a5742012-10-24 13:54:53 -040064
alisionfde875f2013-05-28 17:01:54 -040065 private SipCall(Parcel in) {
66 ArrayList<String> list = in.createStringArrayList();
Alexandre Savard557a5742012-10-24 13:54:53 -040067
alisionfde875f2013-05-28 17:01:54 -040068 // Don't mess with this order!!!
69 mCallID = list.get(0);
70 mAccountID = list.get(1);
71 // mDisplayName = list.get(2);
72 // mPhone = list.get(3);
73 // mEmail = list.get(4);
74 // mRemoteContact = list.get(5);
Alexandre Savard74c1cad2012-10-24 16:39:00 -040075
alisionfde875f2013-05-28 17:01:54 -040076 contacts = in.createTypedArrayList(CallContact.CREATOR);
Alexandre Savard557a5742012-10-24 13:54:53 -040077
alisionfde875f2013-05-28 17:01:54 -040078 mCallType = in.readInt();
79 mCallState = in.readInt();
80 mMediaState = in.readInt();
Alexandre Savard7a46f542012-09-20 11:23:33 -040081 }
Alexandre Savard4a19d752012-09-19 13:19:24 -040082
alisionfde875f2013-05-28 17:01:54 -040083 // public SipCall(Intent call) {
84
85 // }
86
87 public SipCall(String id, String account, int call_type, int call_state, int media_state, ArrayList<CallContact> c) {
88 mCallID = id;
89 mAccountID = account;
90 mCallType = call_type;
91 mCallState = call_state;
92 mMediaState = media_state;
93 this.contacts = new ArrayList<CallContact>(c);
Alexandre Savard4a19d752012-09-19 13:19:24 -040094 }
95
alisionfde875f2013-05-28 17:01:54 -040096 // public SipCall() {
97 // }
98
99 public interface state {
100 public static final int CALL_TYPE_UNDETERMINED = 0;
101 public static final int CALL_TYPE_INCOMING = 1;
102 public static final int CALL_TYPE_OUTGOING = 2;
103
104 public static final int CALL_STATE_NONE = 0;
105 public static final int CALL_STATE_INCOMING = 1;
106 public static final int CALL_STATE_RINGING = 2;
107 public static final int CALL_STATE_CURRENT = 3;
108 public static final int CALL_STATE_HUNGUP = 4;
109 public static final int CALL_STATE_BUSY = 5;
110 public static final int CALL_STATE_FAILURE = 6;
111 public static final int CALL_STATE_HOLD = 7;
112 public static final int CALL_STATE_UNHOLD = 8;
113
114 public static final int MEDIA_STATE_NONE = 0; // No media currently
115 public static final int MEDIA_STATE_ACTIVE = 1; // Media is active
116 public static final int MEDIA_STATE_LOCAL_HOLD = 2; // Media is put on hold bu user
117 public static final int MEDIA_STATE_REMOTE_HOLD = 3; // Media is put on hold by peer
118 public static final int MEDIA_STATE_ERROR = 5; // Media is in error state
Alexandre Savard4a19d752012-09-19 13:19:24 -0400119 }
120
alisionfde875f2013-05-28 17:01:54 -0400121 @Override
122 public int describeContents() {
123 return 0;
124 }
125
126 @Override
127 public void writeToParcel(Parcel out, int flags) {
128 ArrayList<String> list = new ArrayList<String>();
129
130 // Don't mess with this order!!!
131 list.add(mCallID);
132 list.add(mAccountID);
133
134 out.writeStringList(list);
135 out.writeTypedList(contacts);
136 out.writeInt(mCallType);
137 out.writeInt(mCallState);
138 out.writeInt(mMediaState);
139 }
140
141 public static final Parcelable.Creator<SipCall> CREATOR = new Parcelable.Creator<SipCall>() {
142 public SipCall createFromParcel(Parcel in) {
143 return new SipCall(in);
144 }
145
146 public SipCall[] newArray(int size) {
147 return new SipCall[size];
148 }
149 };
150
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400151 public void setCallID(String callID) {
alisionfde875f2013-05-28 17:01:54 -0400152 mCallID = callID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400153 }
154
155 public String getCallId() {
alisionfde875f2013-05-28 17:01:54 -0400156 return mCallID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400157 }
158
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400159 public void setAccountID(String accountID) {
alisionfde875f2013-05-28 17:01:54 -0400160 mAccountID = accountID;
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400161 }
162
163 public String getAccountID() {
alisionfde875f2013-05-28 17:01:54 -0400164 return mAccountID;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400165 }
166
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400167 public void setCallType(int callType) {
alisionfde875f2013-05-28 17:01:54 -0400168 mCallType = callType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400169 }
170
171 public int getCallType() {
alisionfde875f2013-05-28 17:01:54 -0400172 return mCallType;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400173 }
174
alision85704182013-05-29 15:23:03 -0400175 public String getCallTypeString() {
176 switch (mCallType) {
177 case state.CALL_TYPE_INCOMING:
178 return "CALL_TYPE_INCOMING";
179 case state.CALL_TYPE_OUTGOING:
180 return "CALL_TYPE_OUTGOING";
181 default:
182 return "CALL_TYPE_UNDETERMINED";
183 }
184 }
185
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400186 public void setCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400187 mCallState = callState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400188 }
189
Alexandre Savarddf544262012-10-25 14:24:08 -0400190 public int getCallStateInt() {
alisionfde875f2013-05-28 17:01:54 -0400191 return mCallState;
192 }
193
194 public String getmCallID() {
195 return mCallID;
196 }
197
198 public void setmCallID(String mCallID) {
199 this.mCallID = mCallID;
200 }
201
202 public String getmAccountID() {
203 return mAccountID;
204 }
205
206 public void setmAccountID(String mAccountID) {
207 this.mAccountID = mAccountID;
208 }
209
210 public ArrayList<CallContact> getContacts() {
211 return contacts;
212 }
213
214 public void setContacts(ArrayList<CallContact> contacts) {
215 this.contacts = contacts;
216 }
217
218 public int getmCallType() {
219 return mCallType;
220 }
221
222 public void setmCallType(int mCallType) {
223 this.mCallType = mCallType;
224 }
225
alisionfde875f2013-05-28 17:01:54 -0400226 public int getmMediaState() {
227 return mMediaState;
228 }
229
230 public void setmMediaState(int mMediaState) {
231 this.mMediaState = mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400232 }
233
Alexandre Savarddf544262012-10-25 14:24:08 -0400234 public String getCallStateString() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400235
alisionfde875f2013-05-28 17:01:54 -0400236 String text_state;
237
238 switch (mCallState) {
239 case state.CALL_STATE_INCOMING:
240 text_state = "INCOMING";
alision11e8e162013-05-28 10:33:14 -0400241 break;
alisionfde875f2013-05-28 17:01:54 -0400242 case state.CALL_STATE_RINGING:
243 text_state = "RINGING";
alision11e8e162013-05-28 10:33:14 -0400244 break;
alisionfde875f2013-05-28 17:01:54 -0400245 case state.CALL_STATE_CURRENT:
246 text_state = "CURRENT";
alision11e8e162013-05-28 10:33:14 -0400247 break;
alisionfde875f2013-05-28 17:01:54 -0400248 case state.CALL_STATE_HUNGUP:
249 text_state = "HUNGUP";
alision11e8e162013-05-28 10:33:14 -0400250 break;
alisionfde875f2013-05-28 17:01:54 -0400251 case state.CALL_STATE_BUSY:
252 text_state = "BUSY";
alision11e8e162013-05-28 10:33:14 -0400253 break;
alisionfde875f2013-05-28 17:01:54 -0400254 case state.CALL_STATE_FAILURE:
255 text_state = "FAILURE";
alision11e8e162013-05-28 10:33:14 -0400256 break;
alisionfde875f2013-05-28 17:01:54 -0400257 case state.CALL_STATE_HOLD:
258 text_state = "HOLD";
alision11e8e162013-05-28 10:33:14 -0400259 break;
alisionfde875f2013-05-28 17:01:54 -0400260 case state.CALL_STATE_UNHOLD:
261 text_state = "UNHOLD";
alision11e8e162013-05-28 10:33:14 -0400262 break;
263 default:
alisionfde875f2013-05-28 17:01:54 -0400264 text_state = "NULL";
Alexandre Savarddf544262012-10-25 14:24:08 -0400265 }
266
alisionfde875f2013-05-28 17:01:54 -0400267 return text_state;
Alexandre Savarddf544262012-10-25 14:24:08 -0400268 }
269
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400270 public void setMediaState(int mediaState) {
alisionfde875f2013-05-28 17:01:54 -0400271 mMediaState = mediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400272 }
273
274 public int getMediaState() {
alisionfde875f2013-05-28 17:01:54 -0400275 return mMediaState;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400276 }
277
alisionfde875f2013-05-28 17:01:54 -0400278 public static class SipCallBuilder {
279
280 private String bCallID = "";
281 private String bAccountID = "";
282 private ArrayList<CallContact> bContacts = new ArrayList<CallContact>();
283
284 private int bCallType = state.CALL_TYPE_UNDETERMINED;
285 private int bCallState = state.CALL_STATE_NONE;
286 private int bMediaState = state.MEDIA_STATE_NONE;
287
alisionfde875f2013-05-28 17:01:54 -0400288 public SipCallBuilder setCallType(int bCallType) {
289 this.bCallType = bCallType;
290 return this;
291 }
292
293 public SipCallBuilder setMediaState(int state) {
294 this.bMediaState = state;
295 return this;
296 }
297
298 public SipCallBuilder setCallState(int state) {
299 this.bCallState = state;
300 return this;
301 }
alision85704182013-05-29 15:23:03 -0400302
alisionfde875f2013-05-28 17:01:54 -0400303 private static final String TAG = SipCallBuilder.class.getSimpleName();
alision85704182013-05-29 15:23:03 -0400304
alisionfde875f2013-05-28 17:01:54 -0400305 public SipCallBuilder startCallCreation(String id) {
306 bCallID = id;
307 bContacts = new ArrayList<CallContact>();
308 bCallType = SipCall.state.CALL_TYPE_INCOMING;
309 return this;
310 }
311
312 public SipCallBuilder startCallCreation() {
313 Random random = new Random();
314 bCallID = Integer.toString(random.nextInt());
315 bContacts = new ArrayList<CallContact>();
316 return this;
317 }
318
319 public SipCallBuilder setAccountID(String h) {
320 Log.i(TAG, "setAccountID" + h);
321 bAccountID = h;
322 return this;
323 }
324
325 public SipCallBuilder addContact(CallContact c) {
326 bContacts.add(c);
327 return this;
328 }
329
330 public SipCall build() throws Exception {
331 if (bCallID.contentEquals("") || bAccountID.contentEquals("") || bContacts.size() == 0) {
332 throw new Exception("SipCallBuilder's parameters missing");
333 }
334 return new SipCall(bCallID, bAccountID, bCallType, bCallState, bMediaState, bContacts);
335 }
336
337 public static SipCallBuilder getInstance() {
338 return new SipCallBuilder();
339 }
340
alisionfde875f2013-05-28 17:01:54 -0400341 }
342
alision85704182013-05-29 15:23:03 -0400343 public void printCallInfo() {
344 Log.i(TAG, "CallInfo: CallID: " + mCallID);
345 Log.i(TAG, " AccountID: " + mAccountID);
346 Log.i(TAG, " CallState: " + mCallState);
347 Log.i(TAG, " CallType: " + mCallType);
348 }
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400349
alision5f899632013-04-22 17:26:56 -0400350 /**
351 * Compare sip calls based on call ID
352 */
353 @Override
alision11e8e162013-05-28 10:33:14 -0400354 public boolean equals(Object c) {
alisionfde875f2013-05-28 17:01:54 -0400355 if (c instanceof SipCall && ((SipCall) c).mCallID == mCallID) {
alision5f899632013-04-22 17:26:56 -0400356 return true;
357 }
358 return false;
alision11e8e162013-05-28 10:33:14 -0400359
alision5f899632013-04-22 17:26:56 -0400360 }
alision7f18fc82013-05-01 09:37:33 -0400361
alision85704182013-05-29 15:23:03 -0400362 public boolean isOutGoing() {
363 if (mCallType == state.CALL_TYPE_OUTGOING)
364 return true;
365
366 return false;
367 }
368
369 public boolean isRinging() {
370 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE)
371 return true;
372
373 return false;
374 }
375
376 public boolean isIncoming() {
377 if (mCallType == state.CALL_TYPE_INCOMING)
378 return true;
379
380 return false;
381 }
382
383 public void setCallState(String newState) {
384 if (newState.equals("INCOMING")) {
385 setCallState(SipCall.state.CALL_STATE_INCOMING);
386 } else if (newState.equals("RINGING")) {
387 setCallState(SipCall.state.CALL_STATE_RINGING);
388 } else if (newState.equals("CURRENT")) {
389 setCallState(SipCall.state.CALL_STATE_CURRENT);
390 } else if (newState.equals("HUNGUP")) {
391 setCallState(SipCall.state.CALL_STATE_HUNGUP);
392 } else if (newState.equals("BUSY")) {
393 setCallState(SipCall.state.CALL_STATE_BUSY);
394 } else if (newState.equals("FAILURE")) {
395 setCallState(SipCall.state.CALL_STATE_FAILURE);
396 } else if (newState.equals("HOLD")) {
397 setCallState(SipCall.state.CALL_STATE_HOLD);
398 } else if (newState.equals("UNHOLD")) {
399 setCallState(SipCall.state.CALL_STATE_CURRENT);
400 } else {
401 setCallState(SipCall.state.CALL_STATE_NONE);
402 }
403
404 }
405
406 public boolean isOngoing() {
407 if (mCallState == state.CALL_STATE_RINGING || mCallState == state.CALL_STATE_NONE || mCallState == state.CALL_STATE_FAILURE || mCallState == state.CALL_STATE_BUSY || mCallState == state.CALL_STATE_HUNGUP)
408 return false;
409
410 return true;
411 }
alision04a00182013-05-10 17:05:29 -0400412
Alexandre Savard4a19d752012-09-19 13:19:24 -0400413}