blob: dcbc0845858121b7bb5fff330b6fc1cd849ee38f [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
42 if (newState.equals("INCOMING")) {
Alexandre Lision945e4612014-01-15 17:40:31 -050043 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_INCOMING);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050044 } else if (newState.equals("RINGING")) {
45 try {
Alexandre Lision945e4612014-01-15 17:40:31 -050046 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_RINGING);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050047 } catch (NullPointerException e) {
Alexandre Lision945e4612014-01-15 17:40:31 -050048 if (mService.getConferences() == null) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050049 return;
50 }
Alexandre Lision945e4612014-01-15 17:40:31 -050051 if (mService.getConferences().get(callID) == null) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050052 Log.e(TAG, "call for " + callID + " is null");
53 return;
54 }
55 }
56
57 } else if (newState.equals("CURRENT")) {
Alexandre Lision945e4612014-01-15 17:40:31 -050058 if (mService.getConferences().get(callID) != null) {
59 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050060 } else {
61 // Check if call is in a conference
Alexandre Lision945e4612014-01-15 17:40:31 -050062 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisionb4e60612014-01-14 17:47:23 -050063 while (it.hasNext()) {
64 Conference tmp = it.next().getValue();
65 for (SipCall c : tmp.getParticipants()) {
66 if (c.getCallId().contentEquals(callID))
67 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
68 }
69 }
70 }
71
72 } else if (newState.equals("HUNGUP")) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050073 Log.d(TAG, "Hanging up " + callID);
Alexandre Lision945e4612014-01-15 17:40:31 -050074 if (mService.getConferences().get(callID) != null) {
75 if (mService.getConferences().get(callID).isRinging()
76 && mService.getConferences().get(callID).isIncoming())
77 mService.mNotificationManager.publishMissedCallNotification(mService.getConferences().get(callID));
78
79 mService.mHistoryManager.insertNewEntry(mService.getConferences().get(callID));
80 mService.getConferences().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050081 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -050082 ArrayList<Conference> it = new ArrayList<Conference>(mService.getConferences().values());
Alexandre Lisionb4e60612014-01-14 17:47:23 -050083
84 boolean found = false;
85 int i = 0;
86 while (!found && i < it.size()) {
87 Conference tmp = it.get(i);
88
Alexandre Lision945e4612014-01-15 17:40:31 -050089 for (SipCall call : tmp.getParticipants()) {
90 if (call.getCallId().contentEquals(callID)) {
91 mService.mHistoryManager.insertNewEntry(call);
92 mService.getConferences().get(tmp.getId()).removeParticipant(call.getCallId());
Alexandre Lisionb4e60612014-01-14 17:47:23 -050093 found = true;
94 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -050095 }
96 ++i;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050097 }
98 }
99
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500100 } else if (newState.equals("BUSY")) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500101 mService.getConferences().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500102 } else if (newState.equals("FAILURE")) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500103 mService.getConferences().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500104 } else if (newState.equals("HOLD")) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500105 if (mService.getConferences().get(callID) != null) {
106 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_HOLD);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500107 } else {
108 // Check if call is in a conference
Alexandre Lision945e4612014-01-15 17:40:31 -0500109 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500110 while (it.hasNext()) {
111 Conference tmp = it.next().getValue();
112 for (SipCall c : tmp.getParticipants()) {
113 if (c.getCallId().contentEquals(callID))
114 c.setCallState(SipCall.state.CALL_STATE_HOLD);
115 }
116 }
117 }
118 } else if (newState.equals("UNHOLD")) {
119
Alexandre Lision945e4612014-01-15 17:40:31 -0500120 if (mService.getConferences().get(callID) != null) {
121 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500122 } else {
123 // Check if call is in a conference
Alexandre Lision945e4612014-01-15 17:40:31 -0500124 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500125 while (it.hasNext()) {
126 Conference tmp = it.next().getValue();
127 for (SipCall c : tmp.getParticipants()) {
128 if (c.getCallId().contentEquals(callID))
129 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
130 }
131 }
132 }
133 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500134 mService.getConferences().get(callID).setCallState(callID, SipCall.state.CALL_STATE_NONE);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500135 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500136 mService.sendBroadcast(intent);
Alexandre Savard14323be2012-10-24 10:02:13 -0400137 }
138
139 @Override
Emeric Vigier9380ae52012-09-14 17:40:39 -0400140 public void on_incoming_call(String accountID, String callID, String from) {
141 Log.d(TAG, "on_incoming_call(" + accountID + ", " + callID + ", " + from + ")");
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500142
143 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
144 try {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500145 StringMap details = mService.getConfigurationManagerJNI().getAccountDetails(accountID);
146 VectMap credentials = mService.getConfigurationManagerJNI().getCredentials(accountID);
147 Account acc = new Account(accountID, SwigNativeConverter.convertAccountToNative(details), SwigNativeConverter.convertCredentialsToNative(credentials));
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500148 callBuilder.startCallCreation(callID).setAccount(acc).setCallState(SipCall.state.CALL_STATE_RINGING)
Alexandre Lision67c70b42014-01-16 13:57:15 -0500149 .setCallType(SipCall.direction.CALL_TYPE_INCOMING);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500150 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(from));
151
152 Intent toSend = new Intent(CallManagerCallBack.INCOMING_CALL);
153 toSend.setClass(mService, CallActivity.class);
154 toSend.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155
156 SipCall newCall = callBuilder.build();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500157 StringMap callDetails = mService.getCallManagerJNI().getCallDetails(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500158
Alexandre Lision945e4612014-01-15 17:40:31 -0500159 newCall.setTimestampStart_(Long.parseLong(callDetails.get(ServiceConstants.call.TIMESTAMP_START)));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500160
161 Conference toAdd = new Conference(newCall);
162
Alexandre Lision945e4612014-01-15 17:40:31 -0500163 mService.getConferences().put(toAdd.getId(), toAdd);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500164
165 Bundle bundle = new Bundle();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500166
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500167 bundle.putParcelable("conference", toAdd);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500168 toSend.putExtra("resuming", false);
169 toSend.putExtras(bundle);
170 mService.startActivity(toSend);
Alexandre Lision945e4612014-01-15 17:40:31 -0500171 mService.mMediaManager.startRing("");
172 mService.mMediaManager.obtainAudioFocus(true);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500173 } catch (Exception e) {
174 e.printStackTrace();
175 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400176 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500177
alision7f18fc82013-05-01 09:37:33 -0400178 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500179 public void on_transfer_state_changed(String result) {
180 Log.w(TAG, "TRANSFER STATE CHANGED:" + result);
alision7f18fc82013-05-01 09:37:33 -0400181 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500182
alision43a9b362013-05-01 16:30:15 -0400183 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500184 public void on_conference_created(final String confID) {
185 Log.w(TAG, "CONFERENCE CREATED:" + confID);
alision806e18e2013-06-21 15:30:17 -0400186 Intent intent = new Intent(CONF_CREATED);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500187 Conference created = new Conference(confID);
188
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500189 StringVect all_participants = mService.getCallManagerJNI().getParticipantList(confID);
190 Log.w(TAG, "all_participants:" + all_participants.size());
191 for (int i = 0; i < all_participants.size(); ++i) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500192 if (mService.getConferences().get(all_participants.get(i)) != null) {
193 created.addParticipant(mService.getConferences().get(all_participants.get(i)).getCallById(all_participants.get(i)));
194 mService.getConferences().remove(all_participants.get(i));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500195 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500196 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500197 while (it.hasNext()) {
198 Conference tmp = it.next().getValue();
199 for (SipCall c : tmp.getParticipants()) {
200 if (c.getCallId().contentEquals(all_participants.get(i))) {
201 created.addParticipant(c);
Alexandre Lision945e4612014-01-15 17:40:31 -0500202 mService.getConferences().get(tmp.getId()).removeParticipant(c.getCallId());
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500203 }
204 }
205 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500206 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500207 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500208 intent.putExtra("newconf", created);
Alexandre Lision945e4612014-01-15 17:40:31 -0500209 mService.getConferences().put(created.getId(), created);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500210 mService.sendBroadcast(intent);
alision43a9b362013-05-01 16:30:15 -0400211 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500212
alision4a0eb092013-05-07 13:52:03 -0400213 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500214 public void on_incoming_message(String ID, String from, String msg) {
215 Log.w(TAG, "on_incoming_message:" + msg);
alisiondf1dac92013-06-27 17:35:53 -0400216 Bundle bundle = new Bundle();
217
218 bundle.putString("CallID", ID);
219 bundle.putString("From", from);
220 bundle.putString("Msg", msg);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500221 Intent intent = new Intent(INCOMING_TEXT);
alisiondf1dac92013-06-27 17:35:53 -0400222 intent.putExtra("com.savoirfairelinux.sflphone.service.newtext", bundle);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500223
224
Alexandre Lision945e4612014-01-15 17:40:31 -0500225 if (mService.getConferences().get(ID) != null) {
226 mService.getConferences().get(ID).addSipMessage(new SipMessage(true, msg));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500227 } else {
Alexandre Lision945e4612014-01-15 17:40:31 -0500228 Iterator<Map.Entry<String, Conference>> it = mService.getConferences().entrySet().iterator();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500229 while (it.hasNext()) {
230 Conference tmp = it.next().getValue();
231 for (SipCall c : tmp.getParticipants()) {
232 if (c.getCallId().contentEquals(ID)) {
Alexandre Lision945e4612014-01-15 17:40:31 -0500233 mService.getConferences().get(tmp.getId()).addSipMessage(new SipMessage(true, msg));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500234 }
235 }
236 }
237
238 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500239
240 mService.sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400241 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500242
alision04a00182013-05-10 17:05:29 -0400243 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500244 public void on_conference_removed(String confID) {
alision806e18e2013-06-21 15:30:17 -0400245 Intent intent = new Intent(CONF_REMOVED);
246 intent.putExtra("confID", confID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500247
Alexandre Lision945e4612014-01-15 17:40:31 -0500248 Conference toReInsert = mService.getConferences().get(confID);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500249 /*for (int i = 0; i < toDestroy.getParticipants().size(); ++i) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500250 mService.getCurrentCalls().put(toDestroy.getParticipants().get(i).getCallId(), toDestroy.getParticipants().get(i));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500251 }*/
Alexandre Lision945e4612014-01-15 17:40:31 -0500252 mService.getConferences().remove(confID);
253 mService.getConferences().put(toReInsert.getId(), toReInsert);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500254 mService.sendBroadcast(intent);
255
alision4a0eb092013-05-07 13:52:03 -0400256 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500257
alision4a0eb092013-05-07 13:52:03 -0400258 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500259 public void on_conference_state_changed(String confID, String state) {
260
alision806e18e2013-06-21 15:30:17 -0400261 Intent intent = new Intent(CONF_CHANGED);
262 intent.putExtra("confID", confID);
263 intent.putExtra("State", state);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500264
Alexandre Lision945e4612014-01-15 17:40:31 -0500265 StringVect all_participants = mService.getCallManagerJNI().getParticipantList(intent.getStringExtra("confID"));
266 for (int i = 0; i < all_participants.size(); ++i) {
267 if (mService.getConferences().get(confID).getParticipants().size() < all_participants.size()
268 && mService.getConferences().get(all_participants.get(i)) != null) { // We need to add the new participant to the conf
269 mService.getConferences().get(confID).addParticipant(mService.getConferences().get(all_participants.get(i)).getCallById(all_participants.get(i)));
270 mService.getConferences().remove(all_participants.get(i));
271 mService.getConferences().get(confID).setCallState(confID, intent.getStringExtra("State"));
272 mService.sendBroadcast(intent);
273 return;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500274 }
Alexandre Lision945e4612014-01-15 17:40:31 -0500275 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500276
277 Log.i(TAG, "Received" + intent.getAction());
Alexandre Lision945e4612014-01-15 17:40:31 -0500278 if (mService.getConferences().get(confID) != null) {
279 mService.getConferences().get(confID).setCallState(confID, state);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500280 mService.sendBroadcast(intent);
281 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400282 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500283
alisiondf1dac92013-06-27 17:35:53 -0400284 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500285 public void on_record_playback_filepath(String id, String filename) {
alisiondf1dac92013-06-27 17:35:53 -0400286 Intent intent = new Intent(RECORD_STATE_CHANGED);
287 intent.putExtra("id", id);
288 intent.putExtra("file", filename);
Alexandre Lision945e4612014-01-15 17:40:31 -0500289 mService.sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400290 }
alisiondf1dac92013-06-27 17:35:53 -0400291
Emeric Vigier9380ae52012-09-14 17:40:39 -0400292}