blob: 0aeb1374c377c2c6fcfa4c95cb26d73767c10291 [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)
149 .setCallType(SipCall.state.CALL_TYPE_INCOMING);
150 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();
157 toSend.putExtra("newcall", newCall);
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) {
alision806e18e2013-06-21 15:30:17 -0400246 Intent intent = new Intent(CONF_REMOVED);
247 intent.putExtra("confID", confID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500248
Alexandre Lision945e4612014-01-15 17:40:31 -0500249 Conference toReInsert = mService.getConferences().get(confID);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500250 /*for (int i = 0; i < toDestroy.getParticipants().size(); ++i) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500251 mService.getCurrentCalls().put(toDestroy.getParticipants().get(i).getCallId(), toDestroy.getParticipants().get(i));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500252 }*/
Alexandre Lision945e4612014-01-15 17:40:31 -0500253 mService.getConferences().remove(confID);
254 mService.getConferences().put(toReInsert.getId(), toReInsert);
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) {
261
alision806e18e2013-06-21 15:30:17 -0400262 Intent intent = new Intent(CONF_CHANGED);
263 intent.putExtra("confID", confID);
264 intent.putExtra("State", state);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500265
Alexandre Lision945e4612014-01-15 17:40:31 -0500266 StringVect all_participants = mService.getCallManagerJNI().getParticipantList(intent.getStringExtra("confID"));
267 for (int i = 0; i < all_participants.size(); ++i) {
268 if (mService.getConferences().get(confID).getParticipants().size() < all_participants.size()
269 && mService.getConferences().get(all_participants.get(i)) != null) { // We need to add the new participant to the conf
270 mService.getConferences().get(confID).addParticipant(mService.getConferences().get(all_participants.get(i)).getCallById(all_participants.get(i)));
271 mService.getConferences().remove(all_participants.get(i));
272 mService.getConferences().get(confID).setCallState(confID, intent.getStringExtra("State"));
273 mService.sendBroadcast(intent);
274 return;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500275 }
Alexandre Lision945e4612014-01-15 17:40:31 -0500276 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500277
278 Log.i(TAG, "Received" + intent.getAction());
Alexandre Lision945e4612014-01-15 17:40:31 -0500279 if (mService.getConferences().get(confID) != null) {
280 mService.getConferences().get(confID).setCallState(confID, state);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500281 mService.sendBroadcast(intent);
282 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400283 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500284
alisiondf1dac92013-06-27 17:35:53 -0400285 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500286 public void on_record_playback_filepath(String id, String filename) {
alisiondf1dac92013-06-27 17:35:53 -0400287 Intent intent = new Intent(RECORD_STATE_CHANGED);
288 intent.putExtra("id", id);
289 intent.putExtra("file", filename);
Alexandre Lision945e4612014-01-15 17:40:31 -0500290 mService.sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400291 }
alisiondf1dac92013-06-27 17:35:53 -0400292
Emeric Vigier9380ae52012-09-14 17:40:39 -0400293}