blob: 7cc6f4a88f3a3a01768843b41b3bb994bf41ad42 [file] [log] [blame]
Alexandre Lision064e1e02013-10-01 16:18:42 -04001package org.sflphone.service;
Emeric Vigier9380ae52012-09-14 17:40:39 -04002
Alexandre Savard74c1cad2012-10-24 16:39:00 -04003import android.content.Intent;
Alexandre Savard74c1cad2012-10-24 16:39:00 -04004import android.os.Bundle;
Alexandre Lisionb4e60612014-01-14 17:47:23 -05005import android.util.Log;
6import org.sflphone.client.CallActivity;
7import org.sflphone.model.*;
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -05008import org.sflphone.utils.SwigNativeConverter;
Alexandre Lisionb4e60612014-01-14 17:47:23 -05009
10import java.util.ArrayList;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050011import java.util.Iterator;
12import java.util.Map;
Emeric Vigier9380ae52012-09-14 17:40:39 -040013
14public class CallManagerCallBack extends Callback {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050015
Emeric Vigier9380ae52012-09-14 17:40:39 -040016 private static final String TAG = "CallManagerCallBack";
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050017 private SipService mService;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040018
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040019 static public final String CALL_STATE_CHANGED = "call-state-changed";
20 static public final String INCOMING_CALL = "incoming-call";
alision04a00182013-05-10 17:05:29 -040021 static public final String INCOMING_TEXT = "incoming-text";
alision907bde72013-06-20 14:40:37 -040022 static public final String CONF_CREATED = "conf_created";
alision806e18e2013-06-21 15:30:17 -040023 static public final String CONF_REMOVED = "conf_removed";
24 static public final String CONF_CHANGED = "conf_changed";
alisiondf1dac92013-06-27 17:35:53 -040025 static public final String RECORD_STATE_CHANGED = "record_state";
alision907bde72013-06-20 14:40:37 -040026
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040027
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050028 public CallManagerCallBack(SipService context) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050029 mService = context;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040030 }
Emeric Vigier9380ae52012-09-14 17:40:39 -040031
32 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -050033 public void on_call_state_changed(String callID, String newState) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050034 Log.d(TAG, "on_call_state_changed : (" + callID + ", " + newState + ")");
alisiondf1dac92013-06-27 17:35:53 -040035 Bundle bundle = new Bundle();
36 bundle.putString("CallID", callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050037 bundle.putString("State", newState);
alisiondf1dac92013-06-27 17:35:53 -040038 Intent intent = new Intent(CALL_STATE_CHANGED);
39 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050040
Alexandre Lisionb4e60612014-01-14 17:47:23 -050041
Alexandre Lision707f9082014-01-16 15:09:07 -050042 /*if (newState.equals("INCOMING")) {
Alexandre Lision945e4612014-01-15 17:40:31 -050043 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_INCOMING);
Alexandre Lision707f9082014-01-16 15:09:07 -050044 } else*/
45 if (newState.equals("RINGING")) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050046 try {
Alexandre Lision945e4612014-01-15 17:40:31 -050047 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_RINGING);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050048 } catch (NullPointerException e) {
Alexandre Lision945e4612014-01-15 17:40:31 -050049 if (mService.getConferences() == null) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050050 return;
51 }
Alexandre Lision945e4612014-01-15 17:40:31 -050052 if (mService.getConferences().get(callID) == null) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050053 Log.e(TAG, "call for " + callID + " is null");
54 return;
55 }
56 }
57
58 } else if (newState.equals("CURRENT")) {
Alexandre Lision945e4612014-01-15 17:40:31 -050059 if (mService.getConferences().get(callID) != null) {
60 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050061 } else {
62 // Check if call is in a conference
Alexandre Lision945e4612014-01-15 17:40:31 -050063 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisionb4e60612014-01-14 17:47:23 -050064 while (it.hasNext()) {
65 Conference tmp = it.next().getValue();
66 for (SipCall c : tmp.getParticipants()) {
67 if (c.getCallId().contentEquals(callID))
68 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
69 }
70 }
71 }
72
73 } else if (newState.equals("HUNGUP")) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050074 Log.d(TAG, "Hanging up " + callID);
Alexandre Lision945e4612014-01-15 17:40:31 -050075 if (mService.getConferences().get(callID) != null) {
76 if (mService.getConferences().get(callID).isRinging()
77 && mService.getConferences().get(callID).isIncoming())
78 mService.mNotificationManager.publishMissedCallNotification(mService.getConferences().get(callID));
79
80 mService.mHistoryManager.insertNewEntry(mService.getConferences().get(callID));
81 mService.getConferences().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050082 } else {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050083
Alexandre Lision96db8032014-01-17 16:43:51 -050084 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
85 while (it.hasNext()) {
86 Conference tmp = it.next().getValue();
87 for (SipCall c : tmp.getParticipants()) {
88 if (c.getCallId().contentEquals(callID)) {
89 mService.mHistoryManager.insertNewEntry(c);
90 mService.getConferences().get(tmp.getId()).removeParticipant(c);
91 break;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050092 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -050093 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -050094 }
95 }
96
Alexandre Lisionb4e60612014-01-14 17:47:23 -050097 } else if (newState.equals("BUSY")) {
Alexandre Lision945e4612014-01-15 17:40:31 -050098 mService.getConferences().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050099 } else if (newState.equals("FAILURE")) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500100 mService.getConferences().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500101 } else if (newState.equals("HOLD")) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500102 if (mService.getConferences().get(callID) != null) {
103 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_HOLD);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500104 } else {
105 // Check if call is in a conference
Alexandre Lision945e4612014-01-15 17:40:31 -0500106 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500107 while (it.hasNext()) {
108 Conference tmp = it.next().getValue();
109 for (SipCall c : tmp.getParticipants()) {
110 if (c.getCallId().contentEquals(callID))
111 c.setCallState(SipCall.state.CALL_STATE_HOLD);
112 }
113 }
114 }
115 } else if (newState.equals("UNHOLD")) {
116
Alexandre Lision945e4612014-01-15 17:40:31 -0500117 if (mService.getConferences().get(callID) != null) {
118 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500119 } else {
120 // Check if call is in a conference
Alexandre Lision945e4612014-01-15 17:40:31 -0500121 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500122 while (it.hasNext()) {
123 Conference tmp = it.next().getValue();
124 for (SipCall c : tmp.getParticipants()) {
125 if (c.getCallId().contentEquals(callID))
126 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
127 }
128 }
129 }
130 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500131 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_NONE);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500132 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500133 mService.sendBroadcast(intent);
Alexandre Savard14323be2012-10-24 10:02:13 -0400134 }
135
136 @Override
Emeric Vigier9380ae52012-09-14 17:40:39 -0400137 public void on_incoming_call(String accountID, String callID, String from) {
138 Log.d(TAG, "on_incoming_call(" + accountID + ", " + callID + ", " + from + ")");
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500139
140 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
141 try {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500142 StringMap details = mService.getConfigurationManagerJNI().getAccountDetails(accountID);
143 VectMap credentials = mService.getConfigurationManagerJNI().getCredentials(accountID);
144 Account acc = new Account(accountID, SwigNativeConverter.convertAccountToNative(details), SwigNativeConverter.convertCredentialsToNative(credentials));
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500145 callBuilder.startCallCreation(callID).setAccount(acc).setCallState(SipCall.state.CALL_STATE_RINGING)
Alexandre Lision67c70b42014-01-16 13:57:15 -0500146 .setCallType(SipCall.direction.CALL_TYPE_INCOMING);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500147 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(from));
148
149 Intent toSend = new Intent(CallManagerCallBack.INCOMING_CALL);
150 toSend.setClass(mService, CallActivity.class);
151 toSend.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
152
153 SipCall newCall = callBuilder.build();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500154 StringMap callDetails = mService.getCallManagerJNI().getCallDetails(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500155
Alexandre Lision945e4612014-01-15 17:40:31 -0500156 newCall.setTimestampStart_(Long.parseLong(callDetails.get(ServiceConstants.call.TIMESTAMP_START)));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500157
158 Conference toAdd = new Conference(newCall);
159
Alexandre Lision945e4612014-01-15 17:40:31 -0500160 mService.getConferences().put(toAdd.getId(), toAdd);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500161
162 Bundle bundle = new Bundle();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500163
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500164 bundle.putParcelable("conference", toAdd);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500165 toSend.putExtra("resuming", false);
166 toSend.putExtras(bundle);
167 mService.startActivity(toSend);
Alexandre Lision945e4612014-01-15 17:40:31 -0500168 mService.mMediaManager.startRing("");
169 mService.mMediaManager.obtainAudioFocus(true);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500170 } catch (Exception e) {
171 e.printStackTrace();
172 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400173 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500174
alision7f18fc82013-05-01 09:37:33 -0400175 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500176 public void on_transfer_state_changed(String result) {
177 Log.w(TAG, "TRANSFER STATE CHANGED:" + result);
alision7f18fc82013-05-01 09:37:33 -0400178 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500179
alision43a9b362013-05-01 16:30:15 -0400180 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500181 public void on_conference_created(final String confID) {
182 Log.w(TAG, "CONFERENCE CREATED:" + confID);
alision806e18e2013-06-21 15:30:17 -0400183 Intent intent = new Intent(CONF_CREATED);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500184 Conference created = new Conference(confID);
185
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500186 StringVect all_participants = mService.getCallManagerJNI().getParticipantList(confID);
187 Log.w(TAG, "all_participants:" + all_participants.size());
188 for (int i = 0; i < all_participants.size(); ++i) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500189 if (mService.getConferences().get(all_participants.get(i)) != null) {
190 created.addParticipant(mService.getConferences().get(all_participants.get(i)).getCallById(all_participants.get(i)));
191 mService.getConferences().remove(all_participants.get(i));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500192 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500193 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500194 while (it.hasNext()) {
195 Conference tmp = it.next().getValue();
196 for (SipCall c : tmp.getParticipants()) {
197 if (c.getCallId().contentEquals(all_participants.get(i))) {
198 created.addParticipant(c);
Alexandre Lision96db8032014-01-17 16:43:51 -0500199 mService.getConferences().get(tmp.getId()).removeParticipant(c);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500200 }
201 }
202 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500203 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500204 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500205 intent.putExtra("newconf", created);
Alexandre Lision945e4612014-01-15 17:40:31 -0500206 mService.getConferences().put(created.getId(), created);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500207 mService.sendBroadcast(intent);
alision43a9b362013-05-01 16:30:15 -0400208 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500209
alision4a0eb092013-05-07 13:52:03 -0400210 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500211 public void on_incoming_message(String ID, String from, String msg) {
212 Log.w(TAG, "on_incoming_message:" + msg);
alisiondf1dac92013-06-27 17:35:53 -0400213 Bundle bundle = new Bundle();
214
215 bundle.putString("CallID", ID);
216 bundle.putString("From", from);
217 bundle.putString("Msg", msg);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500218 Intent intent = new Intent(INCOMING_TEXT);
alisiondf1dac92013-06-27 17:35:53 -0400219 intent.putExtra("com.savoirfairelinux.sflphone.service.newtext", bundle);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500220
221
Alexandre Lision945e4612014-01-15 17:40:31 -0500222 if (mService.getConferences().get(ID) != null) {
223 mService.getConferences().get(ID).addSipMessage(new SipMessage(true, msg));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500224 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500225 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500226 while (it.hasNext()) {
227 Conference tmp = it.next().getValue();
228 for (SipCall c : tmp.getParticipants()) {
229 if (c.getCallId().contentEquals(ID)) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500230 mService.getConferences().get(tmp.getId()).addSipMessage(new SipMessage(true, msg));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500231 }
232 }
233 }
234
235 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500236
237 mService.sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400238 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500239
alision04a00182013-05-10 17:05:29 -0400240 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500241 public void on_conference_removed(String confID) {
Alexandre Lision183bf452014-01-17 11:21:59 -0500242 Log.i(TAG, "on_conference_removed:");
alision806e18e2013-06-21 15:30:17 -0400243 Intent intent = new Intent(CONF_REMOVED);
244 intent.putExtra("confID", confID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500245
Alexandre Lision945e4612014-01-15 17:40:31 -0500246 Conference toReInsert = mService.getConferences().get(confID);
Alexandre Lision183bf452014-01-17 11:21:59 -0500247 for (SipCall call : toReInsert.getParticipants()) {
248 mService.getConferences().put(call.getCallId(), new Conference(call));
249 }
Alexandre Lision945e4612014-01-15 17:40:31 -0500250 mService.getConferences().remove(confID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500251 mService.sendBroadcast(intent);
252
alision4a0eb092013-05-07 13:52:03 -0400253 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500254
alision4a0eb092013-05-07 13:52:03 -0400255 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500256 public void on_conference_state_changed(String confID, String state) {
Alexandre Lision183bf452014-01-17 11:21:59 -0500257 Log.i(TAG, "on_conference_state_changed:");
alision806e18e2013-06-21 15:30:17 -0400258 Intent intent = new Intent(CONF_CHANGED);
259 intent.putExtra("confID", confID);
260 intent.putExtra("State", state);
Alexandre Lision183bf452014-01-17 11:21:59 -0500261 mService.getConferences().get(confID).setCallState(confID, state);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500262
Alexandre Lision183bf452014-01-17 11:21:59 -0500263 Log.i(TAG, "Received:" + intent.getAction());
264 Log.i(TAG, "State:" + state);
265
266 Conference toModify = mService.getConferences().get(confID);
267
268 ArrayList<String> newParticipants = SwigNativeConverter.convertSwigToNative(mService.getCallManagerJNI().getParticipantList(intent.getStringExtra("confID")));
269
270 if (toModify.getParticipants().size() < newParticipants.size()) {
271 // We need to add the new participant to the conf
272 for (int i = 0; i < newParticipants.size(); ++i) {
273 if(toModify.getCallById(newParticipants.get(i))==null){
274 mService.addCallToConference(toModify.getId(), newParticipants.get(i));
275 }
276 }
277 } else if (toModify.getParticipants().size() > newParticipants.size()) {
Alexandre Lision96db8032014-01-17 16:43:51 -0500278 Log.i(TAG, "toModify.getParticipants().size() > newParticipants.size()");
Alexandre Lision183bf452014-01-17 11:21:59 -0500279 for (SipCall participant : toModify.getParticipants()) {
280 if (!newParticipants.contains(participant.getCallId())) {
Alexandre Lision96db8032014-01-17 16:43:51 -0500281 mService.detachCallFromConference(toModify.getId(), participant);
Alexandre Lision183bf452014-01-17 11:21:59 -0500282 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500283 }
Alexandre Lision945e4612014-01-15 17:40:31 -0500284 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500285
Alexandre Lision183bf452014-01-17 11:21:59 -0500286 mService.sendBroadcast(intent);
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400287 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500288
alisiondf1dac92013-06-27 17:35:53 -0400289 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500290 public void on_record_playback_filepath(String id, String filename) {
alisiondf1dac92013-06-27 17:35:53 -0400291 Intent intent = new Intent(RECORD_STATE_CHANGED);
292 intent.putExtra("id", id);
293 intent.putExtra("file", filename);
Alexandre Lision945e4612014-01-15 17:40:31 -0500294 mService.sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400295 }
alisiondf1dac92013-06-27 17:35:53 -0400296
Emeric Vigier9380ae52012-09-14 17:40:39 -0400297}