blob: 5ba0d67d5eeab9d3073052f1fc8c58d29a4903a2 [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;
5import android.support.v4.content.LocalBroadcastManager;
Alexandre Lisionb4e60612014-01-14 17:47:23 -05006import android.util.Log;
7import org.sflphone.client.CallActivity;
8import org.sflphone.model.*;
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -05009import org.sflphone.utils.SwigNativeConverter;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050010
11import java.util.ArrayList;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050012import java.util.Iterator;
13import java.util.Map;
Emeric Vigier9380ae52012-09-14 17:40:39 -040014
15public class CallManagerCallBack extends Callback {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050016
Emeric Vigier9380ae52012-09-14 17:40:39 -040017 private static final String TAG = "CallManagerCallBack";
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050018 private SipService mService;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040019
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040020 static public final String CALL_STATE_CHANGED = "call-state-changed";
21 static public final String INCOMING_CALL = "incoming-call";
alision04a00182013-05-10 17:05:29 -040022 static public final String INCOMING_TEXT = "incoming-text";
alision907bde72013-06-20 14:40:37 -040023 static public final String CONF_CREATED = "conf_created";
alision806e18e2013-06-21 15:30:17 -040024 static public final String CONF_REMOVED = "conf_removed";
25 static public final String CONF_CHANGED = "conf_changed";
alisiondf1dac92013-06-27 17:35:53 -040026 static public final String RECORD_STATE_CHANGED = "record_state";
alision907bde72013-06-20 14:40:37 -040027
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040028
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050029 public CallManagerCallBack(SipService context) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050030 mService = context;
Alexandre Savard74c1cad2012-10-24 16:39:00 -040031 }
Emeric Vigier9380ae52012-09-14 17:40:39 -040032
33 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -050034 public void on_call_state_changed(String callID, String newState) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050035 Log.d(TAG, "on_call_state_changed : (" + callID + ", " + newState + ")");
alisiondf1dac92013-06-27 17:35:53 -040036 Bundle bundle = new Bundle();
37 bundle.putString("CallID", callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050038 bundle.putString("State", newState);
alisiondf1dac92013-06-27 17:35:53 -040039 Intent intent = new Intent(CALL_STATE_CHANGED);
40 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050041
42 /*try {
43 if (mService.getCurrentCalls().get(callID) != null && mBinder.isConferenceParticipant(callID)) {
44 mService.getCurrentCalls().remove(callID);
45 }
46 } catch (RemoteException e1) {
47 e1.printStackTrace();
48 }*/
49
50
51 if (newState.equals("INCOMING")) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050052 mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_INCOMING);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050053 } else if (newState.equals("RINGING")) {
54 try {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050055 mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_RINGING);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050056 } catch (NullPointerException e) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050057 if (mService.getCurrentConfs() == null) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050058 return;
59 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050060 if (mService.getCurrentConfs().get(callID) == null) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -050061 Log.e(TAG, "call for " + callID + " is null");
62 return;
63 }
64 }
65
66 } else if (newState.equals("CURRENT")) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050067 if (mService.getCurrentConfs().get(callID) != null) {
68 mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050069 } else {
70 // Check if call is in a conference
71 Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
72 while (it.hasNext()) {
73 Conference tmp = it.next().getValue();
74 for (SipCall c : tmp.getParticipants()) {
75 if (c.getCallId().contentEquals(callID))
76 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
77 }
78 }
79 }
80
81 } else if (newState.equals("HUNGUP")) {
82
83 Log.d(TAG, "Hanging up " + callID);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050084 if (mService.getCurrentConfs().get(callID) != null) {
85 if (mService.getCurrentConfs().get(callID).isRinging()
86 && mService.getCurrentConfs().get(callID).isIncoming())
87 mService.notificationManager.publishMissedCallNotification(mService.getCurrentConfs().get(callID));
88 mService.getCurrentConfs().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050089 } else {
90 ArrayList<Conference> it = new ArrayList<Conference>(mService.getCurrentConfs().values());
91
92 boolean found = false;
93 int i = 0;
94 while (!found && i < it.size()) {
95 Conference tmp = it.get(i);
96
97 for (int j = 0; j < tmp.getParticipants().size(); ++j) {
98 if (tmp.getParticipants().get(j).getCallId().contentEquals(callID)) {
99 mService.getCurrentConfs().get(tmp.getId()).getParticipants().remove(tmp.getParticipants().get(j));
100 found = true;
101 }
102
103 }
104 ++i;
105
106 }
107 }
108
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500109 } else if (newState.equals("BUSY")) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500110 mService.getCurrentConfs().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500111 } else if (newState.equals("FAILURE")) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500112 mService.getCurrentConfs().remove(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500113 } else if (newState.equals("HOLD")) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500114 if (mService.getCurrentConfs().get(callID) != null) {
115 mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_HOLD);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500116 } else {
117 // Check if call is in a conference
118 Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
119 while (it.hasNext()) {
120 Conference tmp = it.next().getValue();
121 for (SipCall c : tmp.getParticipants()) {
122 if (c.getCallId().contentEquals(callID))
123 c.setCallState(SipCall.state.CALL_STATE_HOLD);
124 }
125 }
126 }
127 } else if (newState.equals("UNHOLD")) {
128
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500129 if (mService.getCurrentConfs().get(callID) != null) {
130 mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_CURRENT);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500131 } else {
132 // Check if call is in a conference
133 Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
134 while (it.hasNext()) {
135 Conference tmp = it.next().getValue();
136 for (SipCall c : tmp.getParticipants()) {
137 if (c.getCallId().contentEquals(callID))
138 c.setCallState(SipCall.state.CALL_STATE_CURRENT);
139 }
140 }
141 }
142 } else {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500143 mService.getCurrentConfs().get(callID).setCallState(callID, SipCall.state.CALL_STATE_NONE);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500144 }
145
146
147 Log.d(TAG, "Hanging up " + callID);
148 mService.sendBroadcast(intent);
149
Alexandre Savard14323be2012-10-24 10:02:13 -0400150 }
151
152 @Override
Emeric Vigier9380ae52012-09-14 17:40:39 -0400153 public void on_incoming_call(String accountID, String callID, String from) {
154 Log.d(TAG, "on_incoming_call(" + accountID + ", " + callID + ", " + from + ")");
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500155
156 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
157 try {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500158 StringMap details = mService.getConfigurationManagerJNI().getAccountDetails(accountID);
159 VectMap credentials = mService.getConfigurationManagerJNI().getCredentials(accountID);
160 Account acc = new Account(accountID, SwigNativeConverter.convertAccountToNative(details), SwigNativeConverter.convertCredentialsToNative(credentials));
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500161 callBuilder.startCallCreation(callID).setAccount(acc).setCallState(SipCall.state.CALL_STATE_RINGING)
162 .setCallType(SipCall.state.CALL_TYPE_INCOMING);
163 callBuilder.setContact(CallContact.ContactBuilder.buildUnknownContact(from));
164
165 Intent toSend = new Intent(CallManagerCallBack.INCOMING_CALL);
166 toSend.setClass(mService, CallActivity.class);
167 toSend.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
168
169 SipCall newCall = callBuilder.build();
170 toSend.putExtra("newcall", newCall);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500171 StringMap callDetails = mService.getCallManagerJNI().getCallDetails(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500172
173 newCall.setTimestamp_start(Long.parseLong(callDetails.get(ServiceConstants.call.TIMESTAMP_START)));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500174
175 Conference toAdd = new Conference(newCall);
176
177 mService.getCurrentConfs().put(toAdd.getId(), toAdd);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500178
179 Bundle bundle = new Bundle();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500180
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500181 bundle.putParcelable("conference", toAdd);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500182 toSend.putExtra("resuming", false);
183 toSend.putExtras(bundle);
184 mService.startActivity(toSend);
185 mService.mediaManager.startRing("");
186 mService.mediaManager.obtainAudioFocus(true);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500187 } catch (Exception e) {
188 e.printStackTrace();
189 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400190 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500191
alision7f18fc82013-05-01 09:37:33 -0400192 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500193 public void on_transfer_state_changed(String result) {
194 Log.w(TAG, "TRANSFER STATE CHANGED:" + result);
alision7f18fc82013-05-01 09:37:33 -0400195 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500196
alision43a9b362013-05-01 16:30:15 -0400197 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500198 public void on_conference_created(final String confID) {
199 Log.w(TAG, "CONFERENCE CREATED:" + confID);
alision806e18e2013-06-21 15:30:17 -0400200 Intent intent = new Intent(CONF_CREATED);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500201 Conference created = new Conference(confID);
202
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500203 StringVect all_participants = mService.getCallManagerJNI().getParticipantList(confID);
204 Log.w(TAG, "all_participants:" + all_participants.size());
205 for (int i = 0; i < all_participants.size(); ++i) {
206 if (mService.getCurrentConfs().get(all_participants.get(i)) != null) {
207 created.addParticipant(mService.getCurrentConfs().get(all_participants.get(i)).getCallById(all_participants.get(i)));
208 mService.getCurrentConfs().remove(all_participants.get(i));
209 } else {
210 Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
211 while (it.hasNext()) {
212 Conference tmp = it.next().getValue();
213 for (SipCall c : tmp.getParticipants()) {
214 if (c.getCallId().contentEquals(all_participants.get(i))) {
215 created.addParticipant(c);
216 mService.getCurrentConfs().get(tmp.getId()).removeParticipant(c.getCallId());
217 }
218 }
219 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500220 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500221 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500222 intent.putExtra("newconf", created);
223 mService.getCurrentConfs().put(created.getId(), created);
224 mService.sendBroadcast(intent);
alision43a9b362013-05-01 16:30:15 -0400225 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500226
alision4a0eb092013-05-07 13:52:03 -0400227 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500228 public void on_incoming_message(String ID, String from, String msg) {
229 Log.w(TAG, "on_incoming_message:" + msg);
alisiondf1dac92013-06-27 17:35:53 -0400230 Bundle bundle = new Bundle();
231
232 bundle.putString("CallID", ID);
233 bundle.putString("From", from);
234 bundle.putString("Msg", msg);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500235 Intent intent = new Intent(INCOMING_TEXT);
alisiondf1dac92013-06-27 17:35:53 -0400236 intent.putExtra("com.savoirfairelinux.sflphone.service.newtext", bundle);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500237
238
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500239 if (mService.getCurrentConfs().get(ID) != null) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500240 mService.getCurrentConfs().get(ID).addSipMessage(new SipMessage(true, msg));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500241 } else {
242 Iterator<Map.Entry<String, Conference>> it = mService.getCurrentConfs().entrySet().iterator();
243 while (it.hasNext()) {
244 Conference tmp = it.next().getValue();
245 for (SipCall c : tmp.getParticipants()) {
246 if (c.getCallId().contentEquals(ID)) {
247 mService.getCurrentConfs().get(tmp.getId()).addSipMessage(new SipMessage(true, msg));
248 }
249 }
250 }
251
252 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500253
254 mService.sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400255 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500256
alision04a00182013-05-10 17:05:29 -0400257 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500258 public void on_conference_removed(String confID) {
alision806e18e2013-06-21 15:30:17 -0400259 Intent intent = new Intent(CONF_REMOVED);
260 intent.putExtra("confID", confID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500261
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500262 Conference toReInsert = mService.getCurrentConfs().get(confID);
263 /*for (int i = 0; i < toDestroy.getParticipants().size(); ++i) {
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500264 mService.getCurrentCalls().put(toDestroy.getParticipants().get(i).getCallId(), toDestroy.getParticipants().get(i));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500265 }*/
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500266 mService.getCurrentConfs().remove(confID);
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500267 mService.getCurrentConfs().put(toReInsert.getId(), toReInsert);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500268 mService.sendBroadcast(intent);
269
alision4a0eb092013-05-07 13:52:03 -0400270 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500271
alision4a0eb092013-05-07 13:52:03 -0400272 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500273 public void on_conference_state_changed(String confID, String state) {
274
alision806e18e2013-06-21 15:30:17 -0400275 Intent intent = new Intent(CONF_CHANGED);
276 intent.putExtra("confID", confID);
277 intent.putExtra("State", state);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500278 ArrayList<String> all_participants;
279
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500280/* try {
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500281 all_participants = (ArrayList<String>) mBinder.getParticipantList(intent.getStringExtra("confID"));
282 for (String participant : all_participants) {
283 if (mService.getCurrentConfs().get(confID).getParticipants().size() < all_participants.size()
284 && mService.getCurrentCalls().get(participant) != null) { // We need to add the new participant to the conf
285 mService.getCurrentConfs().get(confID).getParticipants()
286 .add(mService.getCurrentCalls().get(participant));
287 mService.getCurrentCalls().remove(participant);
288 mService.getCurrentConfs().get(confID).setState(intent.getStringExtra("State"));
289 mService.sendBroadcast(intent);
290 return;
291 }
292 }
293 } catch (RemoteException e) {
294 e.printStackTrace();
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500295 }*/
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500296
297 Log.i(TAG, "Received" + intent.getAction());
298 if (mService.getCurrentConfs().get(confID) != null) {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500299 mService.getCurrentConfs().get(confID).setCallState(confID, state);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500300 mService.sendBroadcast(intent);
301 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400302 }
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500303
alisiondf1dac92013-06-27 17:35:53 -0400304 @Override
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500305 public void on_record_playback_filepath(String id, String filename) {
alisiondf1dac92013-06-27 17:35:53 -0400306 Intent intent = new Intent(RECORD_STATE_CHANGED);
307 intent.putExtra("id", id);
308 intent.putExtra("file", filename);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500309 LocalBroadcastManager.getInstance(mService).sendBroadcast(intent);
alision04a00182013-05-10 17:05:29 -0400310 }
alisiondf1dac92013-06-27 17:35:53 -0400311
Emeric Vigier9380ae52012-09-14 17:40:39 -0400312}