blob: e41baae2d7e12328352f4eeed4f232e7c8a245f1 [file] [log] [blame]
Alexandre Lision81ef7502013-12-11 17:29:49 -05001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@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 */
Alexandre Lision064e1e02013-10-01 16:18:42 -040031package org.sflphone.receivers;
alision2cb99562013-05-30 17:02:20 -040032
alision806e18e2013-06-21 15:30:17 -040033import java.util.ArrayList;
Alexandre Lisionebeb3662013-09-17 16:20:54 -040034import java.util.HashMap;
alision34673e62013-06-25 14:40:07 -040035import java.util.Iterator;
36import java.util.Map.Entry;
alision806e18e2013-06-21 15:30:17 -040037
Alexandre Lision47a72042013-12-10 10:43:02 -050038import org.sflphone.client.CallActivity;
Alexandre Lision3ee516e2013-10-07 17:32:15 -040039import org.sflphone.model.Account;
Alexandre Lision064e1e02013-10-01 16:18:42 -040040import org.sflphone.model.CallContact;
41import org.sflphone.model.Conference;
42import org.sflphone.model.SipCall;
Alexandre Lisiond5686032013-10-29 11:09:21 -040043import org.sflphone.model.SipMessage;
Alexandre Lision064e1e02013-10-01 16:18:42 -040044import org.sflphone.service.CallManagerCallBack;
45import org.sflphone.service.ConfigurationManagerCallback;
Alexandre Lision3b7148e2013-11-13 17:23:06 -050046import org.sflphone.service.ISipService.Stub;
Alexandre Lision064e1e02013-10-01 16:18:42 -040047import org.sflphone.service.ServiceConstants;
48import org.sflphone.service.SipService;
49
alision2cb99562013-05-30 17:02:20 -040050import android.content.BroadcastReceiver;
51import android.content.Context;
52import android.content.Intent;
53import android.os.Bundle;
alision806e18e2013-06-21 15:30:17 -040054import android.os.RemoteException;
alision2cb99562013-05-30 17:02:20 -040055import android.util.Log;
alision2cb99562013-05-30 17:02:20 -040056
alision34673e62013-06-25 14:40:07 -040057public class IncomingReceiver extends BroadcastReceiver {
58
alision58356b72013-06-03 17:13:36 -040059 static final String TAG = IncomingReceiver.class.getSimpleName();
alision2cb99562013-05-30 17:02:20 -040060
61 SipService callback;
alision806e18e2013-06-21 15:30:17 -040062 Stub mBinder;
alision34673e62013-06-25 14:40:07 -040063
64 public IncomingReceiver(SipService client, Stub bind) {
alision2cb99562013-05-30 17:02:20 -040065 callback = client;
alision806e18e2013-06-21 15:30:17 -040066 mBinder = bind;
alision2cb99562013-05-30 17:02:20 -040067 }
alision34673e62013-06-25 14:40:07 -040068
Alexandre Lision80f0a7e2013-09-26 12:27:51 -040069 @SuppressWarnings("unchecked")
70 // Hashmap runtime cast
alision2cb99562013-05-30 17:02:20 -040071 @Override
72 public void onReceive(Context context, Intent intent) {
73
74 if (intent.getAction().contentEquals(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED)) {
alision34673e62013-06-25 14:40:07 -040075
alision2cb99562013-05-30 17:02:20 -040076 Log.i(TAG, "Received" + intent.getAction());
alision58356b72013-06-03 17:13:36 -040077 callback.sendBroadcast(intent);
alision34673e62013-06-25 14:40:07 -040078
alision2cb99562013-05-30 17:02:20 -040079 } else if (intent.getAction().contentEquals(ConfigurationManagerCallback.ACCOUNTS_CHANGED)) {
alision34673e62013-06-25 14:40:07 -040080
alision2cb99562013-05-30 17:02:20 -040081 Log.i(TAG, "Received" + intent.getAction());
alision58356b72013-06-03 17:13:36 -040082 callback.sendBroadcast(intent);
alision34673e62013-06-25 14:40:07 -040083
alision2cb99562013-05-30 17:02:20 -040084 } else if (intent.getAction().contentEquals(CallManagerCallBack.INCOMING_TEXT)) {
alision34673e62013-06-25 14:40:07 -040085
Alexandre Lisiond5686032013-10-29 11:09:21 -040086 Bundle extra = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newtext");
alision2cb99562013-05-30 17:02:20 -040087 Log.i(TAG, "Received" + intent.getAction());
Alexandre Lisiond5545232013-10-29 11:24:02 -040088 if (callback.getCurrent_calls().get(extra.getString("CallID")) != null) {
89 callback.getCurrent_calls().get(extra.get("CallID")).addSipMessage(new SipMessage(true, extra.getString("Msg")));
Alexandre Lision3b7148e2013-11-13 17:23:06 -050090 } else if (callback.getCurrent_confs().get(extra.getString("CallID")) != null) {
Alexandre Lisiond5545232013-10-29 11:24:02 -040091 callback.getCurrent_confs().get(extra.get("CallID")).addSipMessage(new SipMessage(true, extra.getString("Msg")));
Alexandre Lision3b7148e2013-11-13 17:23:06 -050092 } else
Alexandre Lisiond5545232013-10-29 11:24:02 -040093 return;
Alexandre Lision3b7148e2013-11-13 17:23:06 -050094
alision2cb99562013-05-30 17:02:20 -040095 callback.sendBroadcast(intent);
alision34673e62013-06-25 14:40:07 -040096
alision2cb99562013-05-30 17:02:20 -040097 } else if (intent.getAction().contentEquals(CallManagerCallBack.INCOMING_CALL)) {
98 Bundle b = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newcall");
99
100 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
alision2cb99562013-05-30 17:02:20 -0400101
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400102 Account acc;
alision2cb99562013-05-30 17:02:20 -0400103 try {
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500104 HashMap<String, String> details = (HashMap<String, String>) mBinder.getAccountDetails(b.getString("AccountID"));
105 ArrayList<HashMap<String, String>> credentials = (ArrayList<HashMap<String, String>>) mBinder
106 .getCredentials(b.getString("AccountID"));
107 acc = new Account(b.getString("AccountID"), details, credentials);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400108 callBuilder.startCallCreation(b.getString("CallID")).setAccount(acc).setCallState(SipCall.state.CALL_STATE_RINGING)
109 .setCallType(SipCall.state.CALL_TYPE_INCOMING);
110 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(b.getString("From")));
111
112 Intent toSend = new Intent(CallManagerCallBack.INCOMING_CALL);
Alexandre Lision47a72042013-12-10 10:43:02 -0500113 toSend.setClass(callback, CallActivity.class);
114 toSend.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400115
alision2cb99562013-05-30 17:02:20 -0400116 SipCall newCall = callBuilder.build();
117 toSend.putExtra("newcall", newCall);
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500118 HashMap<String, String> callDetails = (HashMap<String, String>) mBinder.getCallDetails(b.getString("CallID"));
Alexandre Lision47a72042013-12-10 10:43:02 -0500119
120 newCall.setTimestamp_start(Long.parseLong(callDetails.get(ServiceConstants.call.TIMESTAMP_START)));
alision2cb99562013-05-30 17:02:20 -0400121 callback.getCurrent_calls().put(newCall.getCallId(), newCall);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500122 // callback.sendBroadcast(toSend);
Alexandre Lision47a72042013-12-10 10:43:02 -0500123 Bundle bundle = new Bundle();
124 Conference tmp = new Conference("-1");
125
126 tmp.getParticipants().add(newCall);
127
128 bundle.putParcelable("conference", tmp);
129 toSend.putExtra("resuming", false);
130 toSend.putExtras(bundle);
131 callback.startActivity(toSend);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500132 callback.mediaManager.startRing("");
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500133 callback.mediaManager.obtainAudioFocus(true);
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400134 } catch (RemoteException e1) {
135 e1.printStackTrace();
alision2cb99562013-05-30 17:02:20 -0400136 } catch (Exception e) {
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400137 e.printStackTrace();
alision2cb99562013-05-30 17:02:20 -0400138 }
139
140 } else if (intent.getAction().contentEquals(CallManagerCallBack.CALL_STATE_CHANGED)) {
141
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400142 Log.i(TAG, "Received " + intent.getAction());
alision2cb99562013-05-30 17:02:20 -0400143 Bundle b = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
144 String newState = b.getString("State");
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400145
alisioncd8fb912013-06-28 14:43:51 -0400146 try {
147 if (callback.getCurrent_calls().get(b.getString("CallID")) != null && mBinder.isConferenceParticipant(b.getString("CallID"))) {
148 callback.getCurrent_calls().remove(b.getString("CallID"));
149 }
Alexandre Lision5ed2c972013-10-11 15:36:33 -0400150 } catch (RemoteException e1) {
151 e1.printStackTrace();
alisioncd8fb912013-06-28 14:43:51 -0400152 }
153
alision2cb99562013-05-30 17:02:20 -0400154 if (newState.equals("INCOMING")) {
155 callback.getCurrent_calls().get(b.getString("CallID")).setCallState(SipCall.state.CALL_STATE_INCOMING);
156 } else if (newState.equals("RINGING")) {
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400157 try {
158 callback.getCurrent_calls().get(b.getString("CallID")).setCallState(SipCall.state.CALL_STATE_RINGING);
159 } catch (NullPointerException e) {
160 if (callback.getCurrent_calls() == null) {
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400161 return;
162 }
163 if (callback.getCurrent_calls().get(b.getString("CallID")) == null) {
164 Log.e(TAG, "get(b.getString(callID)) null");
165 return;
166 }
167 }
168
alision2cb99562013-05-30 17:02:20 -0400169 } else if (newState.equals("CURRENT")) {
alisioncd8fb912013-06-28 14:43:51 -0400170 if (callback.getCurrent_calls().get(b.getString("CallID")) != null) {
171 callback.getCurrent_calls().get(b.getString("CallID")).setCallState(SipCall.state.CALL_STATE_CURRENT);
172 } else {
173 // Check if call is in a conference
174 Iterator<Entry<String, Conference>> it = callback.getCurrent_confs().entrySet().iterator();
175 while (it.hasNext()) {
176 Conference tmp = it.next().getValue();
177 for (SipCall c : tmp.getParticipants()) {
178 if (c.getCallId().contentEquals(b.getString("CallID")))
179 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
180 }
181 }
182 }
183
alision2cb99562013-05-30 17:02:20 -0400184 } else if (newState.equals("HUNGUP")) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500185
alisioncd8fb912013-06-28 14:43:51 -0400186 if (callback.getCurrent_calls().get(b.getString("CallID")) != null) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500187
188 if (callback.getCurrent_calls().get(b.getString("CallID")).isRinging()
189 && callback.getCurrent_calls().get(b.getString("CallID")).isIncoming())
Alexandre Lision81ef7502013-12-11 17:29:49 -0500190 callback.notificationManager.publishMissedCallNotification(callback.getCurrent_calls().get(b.getString("CallID")));
alisioncd8fb912013-06-28 14:43:51 -0400191 callback.getCurrent_calls().remove(b.getString("CallID"));
192 } else {
193 ArrayList<Conference> it = new ArrayList<Conference>(callback.getCurrent_confs().values());
194
195 boolean found = false;
196 int i = 0;
197 while (!found && i < it.size()) {
198 Conference tmp = it.get(i);
199
200 for (int j = 0; j < tmp.getParticipants().size(); ++j) {
201 if (tmp.getParticipants().get(j).getCallId().contentEquals(b.getString("CallID"))) {
202 callback.getCurrent_confs().get(tmp.getId()).getParticipants().remove(tmp.getParticipants().get(j));
203 found = true;
204 }
205
206 }
207 ++i;
208
209 }
210 }
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400211
alisioncd8fb912013-06-28 14:43:51 -0400212 callback.sendBroadcast(intent);
213
alision2cb99562013-05-30 17:02:20 -0400214 } else if (newState.equals("BUSY")) {
215 callback.getCurrent_calls().remove(b.getString("CallID"));
216 } else if (newState.equals("FAILURE")) {
217 callback.getCurrent_calls().remove(b.getString("CallID"));
218 } else if (newState.equals("HOLD")) {
alision34673e62013-06-25 14:40:07 -0400219 if (callback.getCurrent_calls().get(b.getString("CallID")) != null) {
220 callback.getCurrent_calls().get(b.getString("CallID")).setCallState(SipCall.state.CALL_STATE_HOLD);
221 } else {
222 // Check if call is in a conference
223 Iterator<Entry<String, Conference>> it = callback.getCurrent_confs().entrySet().iterator();
alisiondf1dac92013-06-27 17:35:53 -0400224 while (it.hasNext()) {
alision34673e62013-06-25 14:40:07 -0400225 Conference tmp = it.next().getValue();
alisiondf1dac92013-06-27 17:35:53 -0400226 for (SipCall c : tmp.getParticipants()) {
227 if (c.getCallId().contentEquals(b.getString("CallID")))
alisioncd8fb912013-06-28 14:43:51 -0400228 c.setCallState(SipCall.state.CALL_STATE_HOLD);
alision34673e62013-06-25 14:40:07 -0400229 }
230 }
231 }
alision2cb99562013-05-30 17:02:20 -0400232 } else if (newState.equals("UNHOLD")) {
alisiondf1dac92013-06-27 17:35:53 -0400233
alision34673e62013-06-25 14:40:07 -0400234 if (callback.getCurrent_calls().get(b.getString("CallID")) != null) {
235 callback.getCurrent_calls().get(b.getString("CallID")).setCallState(SipCall.state.CALL_STATE_CURRENT);
236 } else {
237 // Check if call is in a conference
238 Iterator<Entry<String, Conference>> it = callback.getCurrent_confs().entrySet().iterator();
alisiondf1dac92013-06-27 17:35:53 -0400239 while (it.hasNext()) {
alision34673e62013-06-25 14:40:07 -0400240 Conference tmp = it.next().getValue();
alisiondf1dac92013-06-27 17:35:53 -0400241 for (SipCall c : tmp.getParticipants()) {
242 if (c.getCallId().contentEquals(b.getString("CallID")))
Alexandre Lision40954dc2013-10-09 15:24:03 -0400243 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
alision34673e62013-06-25 14:40:07 -0400244 }
245 }
246 }
alision2cb99562013-05-30 17:02:20 -0400247 } else {
248 callback.getCurrent_calls().get(b.getString("CallID")).setCallState(SipCall.state.CALL_STATE_NONE);
249 }
250
251 callback.sendBroadcast(intent);
alision34673e62013-06-25 14:40:07 -0400252
alision2cb99562013-05-30 17:02:20 -0400253 } else if (intent.getAction().contentEquals(CallManagerCallBack.NEW_CALL_CREATED)) {
alision34673e62013-06-25 14:40:07 -0400254
alision2cb99562013-05-30 17:02:20 -0400255 Log.i(TAG, "Received" + intent.getAction());
alision34673e62013-06-25 14:40:07 -0400256
alision806e18e2013-06-21 15:30:17 -0400257 } else if (intent.getAction().contentEquals(CallManagerCallBack.CONF_CREATED)) {
alision34673e62013-06-25 14:40:07 -0400258
alision806e18e2013-06-21 15:30:17 -0400259 Log.i(TAG, "Received" + intent.getAction());
260 Conference created = new Conference(intent.getStringExtra("confID"));
alisiondf1dac92013-06-27 17:35:53 -0400261
alision806e18e2013-06-21 15:30:17 -0400262 try {
alisiondf1dac92013-06-27 17:35:53 -0400263 ArrayList<String> all_participants = (ArrayList<String>) mBinder.getParticipantList(intent.getStringExtra("confID"));
alision34673e62013-06-25 14:40:07 -0400264 for (String participant : all_participants) {
alision806e18e2013-06-21 15:30:17 -0400265 created.getParticipants().add(callback.getCurrent_calls().get(participant));
alisiondf1dac92013-06-27 17:35:53 -0400266 callback.getCurrent_calls().remove(participant);
alision806e18e2013-06-21 15:30:17 -0400267 }
268 Intent toSend = new Intent(CallManagerCallBack.CONF_CREATED);
269 toSend.putExtra("newconf", created);
270 callback.getCurrent_confs().put(intent.getStringExtra("confID"), created);
271 callback.sendBroadcast(toSend);
272 } catch (RemoteException e1) {
273 e1.printStackTrace();
274 }
275 Log.i(TAG, "current_confs size " + callback.getCurrent_confs().size());
alision34673e62013-06-25 14:40:07 -0400276
alision806e18e2013-06-21 15:30:17 -0400277 } else if (intent.getAction().contentEquals(CallManagerCallBack.CONF_REMOVED)) {
alision34673e62013-06-25 14:40:07 -0400278
alision806e18e2013-06-21 15:30:17 -0400279 Log.i(TAG, "Received" + intent.getAction());
280 Conference toDestroy = callback.getCurrent_confs().get(intent.getStringExtra("confID"));
alision34673e62013-06-25 14:40:07 -0400281 for (int i = 0; i < toDestroy.getParticipants().size(); ++i) {
alision806e18e2013-06-21 15:30:17 -0400282 callback.getCurrent_calls().put(toDestroy.getParticipants().get(i).getCallId(), toDestroy.getParticipants().get(i));
283 }
284 callback.getCurrent_confs().remove(intent.getStringExtra("confID"));
alisiondf1dac92013-06-27 17:35:53 -0400285 callback.sendBroadcast(intent);
alision34673e62013-06-25 14:40:07 -0400286
alision806e18e2013-06-21 15:30:17 -0400287 } else if (intent.getAction().contentEquals(CallManagerCallBack.CONF_CHANGED)) {
alision34673e62013-06-25 14:40:07 -0400288
alisiondf1dac92013-06-27 17:35:53 -0400289 ArrayList<String> all_participants;
290 try {
291 all_participants = (ArrayList<String>) mBinder.getParticipantList(intent.getStringExtra("confID"));
292 for (String participant : all_participants) {
293 if (callback.getCurrent_confs().get(intent.getStringExtra("confID")).getParticipants().size() < all_participants.size()
294 && callback.getCurrent_calls().get(participant) != null) { // We need to add the new participant to the conf
295 callback.getCurrent_confs().get(intent.getStringExtra("confID")).getParticipants()
296 .add(callback.getCurrent_calls().get(participant));
297 callback.getCurrent_calls().remove(participant);
298 callback.getCurrent_confs().get(intent.getStringExtra("confID")).setState(intent.getStringExtra("State"));
299 callback.sendBroadcast(intent);
300 return;
301 }
302 }
303 } catch (RemoteException e) {
304 e.printStackTrace();
305 }
306
alision806e18e2013-06-21 15:30:17 -0400307 Log.i(TAG, "Received" + intent.getAction());
Alexandre Lision40954dc2013-10-09 15:24:03 -0400308 if (callback.getCurrent_confs().get(intent.getStringExtra("confID")) != null) {
309
310 callback.getCurrent_confs().get(intent.getStringExtra("confID")).setState(intent.getStringExtra("State"));
311 callback.sendBroadcast(intent);
312 }
alisiondf1dac92013-06-27 17:35:53 -0400313
314 } else if (intent.getAction().contentEquals(CallManagerCallBack.RECORD_STATE_CHANGED)) {
315
316 Log.i(TAG, "Received" + intent.getAction());
317
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500318 // try {
319 // if (callback.getCurrent_confs().get(intent.getStringExtra("id")) != null) {
320 // callback.getCurrent_confs().get(intent.getStringExtra("id")).setRecording(mBinder.isRecording(intent.getStringExtra("id")));
321 // } else if (callback.getCurrent_calls().get(intent.getStringExtra("id")) != null) {
322 // callback.getCurrent_calls().get(intent.getStringExtra("id")).setRecording(mBinder.isRecording(intent.getStringExtra("id")));
323 // } else {
324 // // A call in a conference has been put on hold
325 // Iterator<Conference> it = callback.getCurrent_confs().values().iterator();
326 // while (it.hasNext()) {
327 // Conference c = it.next();
328 // if (c.getCall(intent.getStringExtra("id")) != null)
329 // c.getCall(intent.getStringExtra("id")).setRecording(mBinder.isRecording(intent.getStringExtra("id")));
330 // }
331 // }
332 // // Re sending the same intent to the app
333 // callback.sendBroadcast(intent);
334 // ;
335 // } catch (RemoteException e) {
336 // // TODO Auto-generated catch block
337 // e.printStackTrace();
338 // }
alisiondf1dac92013-06-27 17:35:53 -0400339
alision2cb99562013-05-30 17:02:20 -0400340 }
341
342 }
343}