blob: 40ae6ac307d3d7ecc81d0bb57758143ec772e6dc [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 Lision945e4612014-01-15 17:40:31 -050083 ArrayList<Conference> it = new ArrayList<Conference>(mService.getConferences().values());
Alexandre Lisionb4e60612014-01-14 17:47:23 -050084
85 boolean found = false;
86 int i = 0;
87 while (!found && i < it.size()) {
88 Conference tmp = it.get(i);
89
Alexandre Lision945e4612014-01-15 17:40:31 -050090 for (SipCall call : tmp.getParticipants()) {
91 if (call.getCallId().contentEquals(callID)) {
92 mService.mHistoryManager.insertNewEntry(call);
93 mService.getConferences().get(tmp.getId()).removeParticipant(call.getCallId());
Alexandre Lisionb4e60612014-01-14 17:47:23 -050094 found = true;
95 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -050096 }
97 ++i;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050098 }
99 }
100
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500101 } else if (newState.equals("BUSY")) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500102 mService.getConferences().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500103 } else if (newState.equals("FAILURE")) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500104 mService.getConferences().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500105 } else if (newState.equals("HOLD")) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500106 if (mService.getConferences().get(callID) != null) {
107 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_HOLD);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500108 } else {
109 // Check if call is in a conference
Alexandre Lision945e4612014-01-15 17:40:31 -0500110 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500111 while (it.hasNext()) {
112 Conference tmp = it.next().getValue();
113 for (SipCall c : tmp.getParticipants()) {
114 if (c.getCallId().contentEquals(callID))
115 c.setCallState(SipCall.state.CALL_STATE_HOLD);
116 }
117 }
118 }
119 } else if (newState.equals("UNHOLD")) {
120
Alexandre Lision945e4612014-01-15 17:40:31 -0500121 if (mService.getConferences().get(callID) != null) {
122 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500123 } else {
124 // Check if call is in a conference
Alexandre Lision945e4612014-01-15 17:40:31 -0500125 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500126 while (it.hasNext()) {
127 Conference tmp = it.next().getValue();
128 for (SipCall c : tmp.getParticipants()) {
129 if (c.getCallId().contentEquals(callID))
130 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
131 }
132 }
133 }
134 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500135 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_NONE);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500136 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500137 mService.sendBroadcast(intent);
Alexandre Savard14323be2012-10-24 10:02:13 -0400138 }
139
140 @Override
Emeric Vigier9380ae52012-09-14 17:40:39 -0400141 public void on_incoming_call(String accountID, String callID, String from) {
142 Log.d(TAG, "on_incoming_call(" + accountID + ", " + callID + ", " + from + ")");
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500143
144 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
145 try {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500146 StringMap details = mService.getConfigurationManagerJNI().getAccountDetails(accountID);
147 VectMap credentials = mService.getConfigurationManagerJNI().getCredentials(accountID);
148 Account acc = new Account(accountID, SwigNativeConverter.convertAccountToNative(details), SwigNativeConverter.convertCredentialsToNative(credentials));
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500149 callBuilder.startCallCreation(callID).setAccount(acc).setCallState(SipCall.state.CALL_STATE_RINGING)
Alexandre Lision67c70b42014-01-16 13:57:15 -0500150 .setCallType(SipCall.direction.CALL_TYPE_INCOMING);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500151 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(from));
152
153 Intent toSend = new Intent(CallManagerCallBack.INCOMING_CALL);
154 toSend.setClass(mService, CallActivity.class);
155 toSend.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
156
157 SipCall newCall = callBuilder.build();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500158 StringMap callDetails = mService.getCallManagerJNI().getCallDetails(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500159
Alexandre Lision945e4612014-01-15 17:40:31 -0500160 newCall.setTimestampStart_(Long.parseLong(callDetails.get(ServiceConstants.call.TIMESTAMP_START)));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500161
162 Conference toAdd = new Conference(newCall);
163
Alexandre Lision945e4612014-01-15 17:40:31 -0500164 mService.getConferences().put(toAdd.getId(), toAdd);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500165
166 Bundle bundle = new Bundle();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500167
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500168 bundle.putParcelable("conference", toAdd);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500169 toSend.putExtra("resuming", false);
170 toSend.putExtras(bundle);
171 mService.startActivity(toSend);
Alexandre Lision945e4612014-01-15 17:40:31 -0500172 mService.mMediaManager.startRing("");
173 mService.mMediaManager.obtainAudioFocus(true);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500174 } catch (Exception e) {
175 e.printStackTrace();
176 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400177 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500178
alision7f18fc82013-05-01 09:37:33 -0400179 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500180 public void on_transfer_state_changed(String result) {
181 Log.w(TAG, "TRANSFER STATE CHANGED:" + result);
alision7f18fc82013-05-01 09:37:33 -0400182 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500183
alision43a9b362013-05-01 16:30:15 -0400184 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500185 public void on_conference_created(final String confID) {
186 Log.w(TAG, "CONFERENCE CREATED:" + confID);
alision806e18e2013-06-21 15:30:17 -0400187 Intent intent = new Intent(CONF_CREATED);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500188 Conference created = new Conference(confID);
189
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500190 StringVect all_participants = mService.getCallManagerJNI().getParticipantList(confID);
191 Log.w(TAG, "all_participants:" + all_participants.size());
192 for (int i = 0; i < all_participants.size(); ++i) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500193 if (mService.getConferences().get(all_participants.get(i)) != null) {
194 created.addParticipant(mService.getConferences().get(all_participants.get(i)).getCallById(all_participants.get(i)));
195 mService.getConferences().remove(all_participants.get(i));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500196 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500197 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500198 while (it.hasNext()) {
199 Conference tmp = it.next().getValue();
200 for (SipCall c : tmp.getParticipants()) {
201 if (c.getCallId().contentEquals(all_participants.get(i))) {
202 created.addParticipant(c);
Alexandre Lision945e4612014-01-15 17:40:31 -0500203 mService.getConferences().get(tmp.getId()).removeParticipant(c.getCallId());
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500204 }
205 }
206 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500207 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500208 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500209 intent.putExtra("newconf", created);
Alexandre Lision945e4612014-01-15 17:40:31 -0500210 mService.getConferences().put(created.getId(), created);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500211 mService.sendBroadcast(intent);
alision43a9b362013-05-01 16:30:15 -0400212 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500213
alision4a0eb092013-05-07 13:52:03 -0400214 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500215 public void on_incoming_message(String ID, String from, String msg) {
216 Log.w(TAG, "on_incoming_message:" + msg);
alisiondf1dac92013-06-27 17:35:53 -0400217 Bundle bundle = new Bundle();
218
219 bundle.putString("CallID", ID);
220 bundle.putString("From", from);
221 bundle.putString("Msg", msg);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500222 Intent intent = new Intent(INCOMING_TEXT);
alisiondf1dac92013-06-27 17:35:53 -0400223 intent.putExtra("com.savoirfairelinux.sflphone.service.newtext", bundle);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500224
225
Alexandre Lision945e4612014-01-15 17:40:31 -0500226 if (mService.getConferences().get(ID) != null) {
227 mService.getConferences().get(ID).addSipMessage(new SipMessage(true, msg));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500228 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500229 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500230 while (it.hasNext()) {
231 Conference tmp = it.next().getValue();
232 for (SipCall c : tmp.getParticipants()) {
233 if (c.getCallId().contentEquals(ID)) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500234 mService.getConferences().get(tmp.getId()).addSipMessage(new SipMessage(true, msg));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500235 }
236 }
237 }
238
239 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500240
241 mService.sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400242 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500243
alision04a00182013-05-10 17:05:29 -0400244 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500245 public void on_conference_removed(String confID) {
Alexandre Lision183bf452014-01-17 11:21:59 -0500246 Log.i(TAG, "on_conference_removed:");
alision806e18e2013-06-21 15:30:17 -0400247 Intent intent = new Intent(CONF_REMOVED);
248 intent.putExtra("confID", confID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500249
Alexandre Lision945e4612014-01-15 17:40:31 -0500250 Conference toReInsert = mService.getConferences().get(confID);
Alexandre Lision183bf452014-01-17 11:21:59 -0500251 for (SipCall call : toReInsert.getParticipants()) {
252 mService.getConferences().put(call.getCallId(), new Conference(call));
253 }
Alexandre Lision945e4612014-01-15 17:40:31 -0500254 mService.getConferences().remove(confID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500255 mService.sendBroadcast(intent);
256
alision4a0eb092013-05-07 13:52:03 -0400257 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500258
alision4a0eb092013-05-07 13:52:03 -0400259 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500260 public void on_conference_state_changed(String confID, String state) {
Alexandre Lision183bf452014-01-17 11:21:59 -0500261 Log.i(TAG, "on_conference_state_changed:");
alision806e18e2013-06-21 15:30:17 -0400262 Intent intent = new Intent(CONF_CHANGED);
263 intent.putExtra("confID", confID);
264 intent.putExtra("State", state);
Alexandre Lision183bf452014-01-17 11:21:59 -0500265 mService.getConferences().get(confID).setCallState(confID, state);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500266
Alexandre Lision183bf452014-01-17 11:21:59 -0500267 Log.i(TAG, "Received:" + intent.getAction());
268 Log.i(TAG, "State:" + state);
269
270 Conference toModify = mService.getConferences().get(confID);
271
272 ArrayList<String> newParticipants = SwigNativeConverter.convertSwigToNative(mService.getCallManagerJNI().getParticipantList(intent.getStringExtra("confID")));
273
274 if (toModify.getParticipants().size() < newParticipants.size()) {
275 // We need to add the new participant to the conf
276 for (int i = 0; i < newParticipants.size(); ++i) {
277 if(toModify.getCallById(newParticipants.get(i))==null){
278 mService.addCallToConference(toModify.getId(), newParticipants.get(i));
279 }
280 }
281 } else if (toModify.getParticipants().size() > newParticipants.size()) {
282
283 for (SipCall participant : toModify.getParticipants()) {
284 if (!newParticipants.contains(participant.getCallId())) {
285 mService.removeCallFromConference(toModify.getId(), participant.getCallId());
286 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500287 }
Alexandre Lision945e4612014-01-15 17:40:31 -0500288 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500289
Alexandre Lision183bf452014-01-17 11:21:59 -0500290 mService.sendBroadcast(intent);
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400291 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500292
alisiondf1dac92013-06-27 17:35:53 -0400293 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500294 public void on_record_playback_filepath(String id, String filename) {
alisiondf1dac92013-06-27 17:35:53 -0400295 Intent intent = new Intent(RECORD_STATE_CHANGED);
296 intent.putExtra("id", id);
297 intent.putExtra("file", filename);
Alexandre Lision945e4612014-01-15 17:40:31 -0500298 mService.sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400299 }
alisiondf1dac92013-06-27 17:35:53 -0400300
Emeric Vigier9380ae52012-09-14 17:40:39 -0400301}