blob: 483fd64b6c7e7d8068d21333be03914ea7b8bf50 [file] [log] [blame]
Alexandre Savard14323be2012-10-24 10:02:13 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5 *
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 */
31
32package com.savoirfairelinux.sflphone.utils;
33
34import com.savoirfairelinux.sflphone.client.SipCall;
35
36import android.content.BroadcastReceiver;
37import android.content.Context;
38import android.content.Intent;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040039import android.os.Bundle;
Alexandre Savard14323be2012-10-24 10:02:13 -040040import android.util.Log;
41
42import java.util.ArrayList;
43
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040044import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
45
Alexandre Savard14323be2012-10-24 10:02:13 -040046public class CallList extends BroadcastReceiver
47{
48 static final String TAG = "CallList";
Alexandre Savard74c1cad2012-10-24 16:39:00 -040049 static ArrayList<SipCall> mList = new ArrayList<SipCall>();
Alexandre Savard73bc56f2012-10-25 13:35:54 -040050 private Context mContext = null;
Alexandre Savard14323be2012-10-24 10:02:13 -040051
52 private enum Signals {
53 NEW_CALL_CREATED,
54 INCOMING_CALL,
55 INCOMING_MESSAGE,
56 CALL_STATE_CHANGED
57 }
58
Alexandre Savard74c1cad2012-10-24 16:39:00 -040059 /**
60 * Factory method to create/retreive call instance
61 */
62 public static SipCall getCallInstance(SipCall.CallInfo info)
63 {
Alexandre Savard73bc56f2012-10-25 13:35:54 -040064 if(mList.isEmpty()) {
65 SipCall call = new SipCall(info);
66 mList.add(call);
67 return call;
68 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -040069
70 for(SipCall sipcall : mList) {
71 if(sipcall.mCallInfo.mCallID.equals(info.mCallID)) {
72 return sipcall;
73 }
74 }
75
76 SipCall call = new SipCall(info);
77 mList.add(call);
78
79 return call;
80 }
81
Alexandre Savard73bc56f2012-10-25 13:35:54 -040082 public CallList(Context context) {
83 mContext = context;
84 }
85
Alexandre Savard14323be2012-10-24 10:02:13 -040086 @Override
87 public void onReceive(Context context, Intent intent)
88 {
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040089 String signalName = intent.getStringExtra(CallManagerCallBack.SIGNAL_NAME);
Alexandre Savard14323be2012-10-24 10:02:13 -040090 Log.d(TAG, "Signal received: " + signalName);
91
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040092 if(signalName.equals(CallManagerCallBack.NEW_CALL_CREATED)) {
93 } else if(signalName.equals(CallManagerCallBack.CALL_STATE_CHANGED)) {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040094 processCallStateChangedSignal(intent);
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040095 } else if(signalName.equals(CallManagerCallBack.INCOMING_CALL)) {
Alexandre Savard73bc56f2012-10-25 13:35:54 -040096 processIncomingCallSignal(intent);
Alexandre Savard14323be2012-10-24 10:02:13 -040097 }
98 }
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040099
100 private void processCallStateChangedSignal(Intent intent) {
101 Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400102
103 SipCall.CallInfo info = new SipCall.CallInfo();
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400104 info.mCallID = bundle.getString("CallID");
105
106 String newState = bundle.getString("State");
107
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400108 SipCall call = getCallInstance(info);
109 if(newState.equals("INCOMING")) {
110 call.setCallState(SipCall.CALL_STATE_INCOMING);
111 } else if(newState.equals("RINGING")) {
112 call.setCallState(SipCall.CALL_STATE_RINGING);
113 } else if(newState.equals("CURRENT")) {
114 call.setCallState(SipCall.CALL_STATE_CURRENT);
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400115 } else if(newState.equals("HUNGUP")) {
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400116 call.setCallState(SipCall.CALL_STATE_HUNGUP);
Alexandre Savardf17e3172012-10-25 16:09:09 -0400117 call.hangup();
118 mList.remove(call);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400119 } else if(newState.equals("BUSY")) {
120 call.setCallState(SipCall.CALL_STATE_BUSY);
121 } else if(newState.equals("FAILURE")) {
122 call.setCallState(SipCall.CALL_STATE_FAILURE);
123 } else if(newState.equals("HOLD")) {
124 call.setCallState(SipCall.CALL_STATE_HOLD);
125 } else if(newState.equals("UNHOLD")) {
126 call.setCallState(SipCall.CALL_STATE_UNHOLD);
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400127 } else {
128 call.setCallState(SipCall.CALL_STATE_NULL);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400129 }
130 }
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400131
132 private void processIncomingCallSignal(Intent intent) {
133 Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newcall");
134 String accountID = bundle.getString("AccountID");
135 String callID = bundle.getString("CallID");
136 String from = bundle.getString("From");
137
138 SipCall.CallInfo info = new SipCall.CallInfo();
139 info.mCallID = callID;
140
141 SipCall call = getCallInstance(info);
142 call.placeCall();
143 call.launchCallActivity(mContext);
144 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400145}