blob: 0f8d141804f3146b7806ca8b3d18ce33d6595586 [file] [log] [blame]
Emeric Vigier6119d782012-09-21 18:04:14 -04001/**
2 * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
Alexandre Lisionc1024c02014-01-06 11:12:53 -05003 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Emeric Vigier6119d782012-09-21 18:04:14 -04004 *
5 * Author: Regis Montoya <r3gis.3R@gmail.com>
6 * Author: Emeric Vigier <emeric.vigier@savoirfairelinux.com>
alision11e8e162013-05-28 10:33:14 -04007 * Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Emeric Vigier6119d782012-09-21 18:04:14 -04008 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 * If you own a pjsip commercial license you can also redistribute it
14 * and/or modify it under the terms of the GNU Lesser General Public License
15 * as an android library.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
Alexandre Lision064e1e02013-10-01 16:18:42 -040025package org.sflphone.service;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040026
Emeric Vigier6119d782012-09-21 18:04:14 -040027import java.lang.ref.WeakReference;
alision17052d42013-04-22 10:39:38 -040028import java.util.ArrayList;
29import java.util.HashMap;
alisioncc7bb422013-06-06 15:31:39 -040030import java.util.Iterator;
alision7f18fc82013-05-01 09:37:33 -040031import java.util.List;
alision17052d42013-04-22 10:39:38 -040032import java.util.Map;
alision806e18e2013-06-21 15:30:17 -040033import java.util.Map.Entry;
Emeric Vigier6119d782012-09-21 18:04:14 -040034
Alexandre Lision933ef0a2013-10-15 17:28:40 -040035import org.sflphone.model.Codec;
Alexandre Lision064e1e02013-10-01 16:18:42 -040036import org.sflphone.model.Conference;
37import org.sflphone.model.SipCall;
Alexandre Lisiond5686032013-10-29 11:09:21 -040038import org.sflphone.model.SipMessage;
Alexandre Lision064e1e02013-10-01 16:18:42 -040039import org.sflphone.receivers.IncomingReceiver;
Alexandre Lision6d867b92013-10-25 15:36:28 -040040import org.sflphone.utils.MediaManager;
Alexandre Lisione0045442013-10-25 09:16:19 -040041import org.sflphone.utils.SipNotifications;
Alexandre Lision2aae48d2013-12-04 13:50:38 -050042import org.sflphone.utils.SwigNativeConverter;
Alexandre Lision064e1e02013-10-01 16:18:42 -040043
Emeric Vigiereaf2c492012-09-19 14:38:20 -040044import android.app.Service;
45import android.content.Intent;
alision17052d42013-04-22 10:39:38 -040046import android.content.IntentFilter;
alision7f18fc82013-05-01 09:37:33 -040047import android.os.Bundle;
Emeric Vigier6119d782012-09-21 18:04:14 -040048import android.os.Handler;
49import android.os.HandlerThread;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040050import android.os.IBinder;
Emeric Vigier6119d782012-09-21 18:04:14 -040051import android.os.Looper;
52import android.os.Message;
alision5f899632013-04-22 17:26:56 -040053import android.os.RemoteException;
alision17052d42013-04-22 10:39:38 -040054import android.support.v4.content.LocalBroadcastManager;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040055import android.util.Log;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040056
Emeric Vigiereaf2c492012-09-19 14:38:20 -040057public class SipService extends Service {
58
59 static final String TAG = "SipService";
60 static final int DELAY = 5000; /* 5 sec */
Emeric Vigier6119d782012-09-21 18:04:14 -040061 private SipServiceExecutor mExecutor;
62 private static HandlerThread executorThread;
alision3ea8f3c2013-07-16 17:35:35 -040063 private CallManager callManagerJNI;
Alexandre Lision67817192013-07-18 12:04:30 -040064 private ManagerImpl managerImpl;
Emeric Vigier0007dee2012-09-24 11:35:58 -040065 private CallManagerCallBack callManagerCallBack;
alision3ea8f3c2013-07-16 17:35:35 -040066 private ConfigurationManager configurationManagerJNI;
Alexandre Savardfccd1dc2012-10-17 17:31:38 -040067 private ConfigurationManagerCallback configurationManagerCallback;
Emeric Vigier6119d782012-09-21 18:04:14 -040068 private boolean isPjSipStackStarted = false;
alisioncc7bb422013-06-06 15:31:39 -040069
Alexandre Lisione0045442013-10-25 09:16:19 -040070 public SipNotifications notificationManager;
Alexandre Lision6d867b92013-10-25 15:36:28 -040071 public MediaManager mediaManager;
Alexandre Lisione0045442013-10-25 09:16:19 -040072
alision2cb99562013-05-30 17:02:20 -040073 private HashMap<String, SipCall> current_calls = new HashMap<String, SipCall>();
alision806e18e2013-06-21 15:30:17 -040074 private HashMap<String, Conference> current_confs = new HashMap<String, Conference>();
75 private IncomingReceiver receiver;
Emeric Vigier6119d782012-09-21 18:04:14 -040076
alision806e18e2013-06-21 15:30:17 -040077 public HashMap<String, Conference> getCurrent_confs() {
78 return current_confs;
79 }
alision43a9b362013-05-01 16:30:15 -040080
81 @Override
82 public boolean onUnbind(Intent i) {
83 super.onUnbind(i);
84 Log.i(TAG, "onUnbind(intent)");
Alexandre Lision0f550ee2013-10-25 15:34:38 -040085 return true;
86 }
Alexandre Lisiondd68f652013-10-28 11:00:12 -040087
Alexandre Lision0f550ee2013-10-25 15:34:38 -040088 @Override
Alexandre Lisiondd68f652013-10-28 11:00:12 -040089 public void onRebind(Intent i) {
Alexandre Lision0f550ee2013-10-25 15:34:38 -040090 super.onRebind(i);
alision43a9b362013-05-01 16:30:15 -040091 }
92
93 /* called once by startService() */
94 @Override
95 public void onCreate() {
96 Log.i(TAG, "onCreated");
97 super.onCreate();
98
alision43a9b362013-05-01 16:30:15 -040099 IntentFilter callFilter = new IntentFilter(CallManagerCallBack.CALL_STATE_CHANGED);
100 callFilter.addAction(CallManagerCallBack.INCOMING_CALL);
101 callFilter.addAction(CallManagerCallBack.NEW_CALL_CREATED);
alision4a0eb092013-05-07 13:52:03 -0400102 callFilter.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
103 callFilter.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
alision04a00182013-05-10 17:05:29 -0400104 callFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
alision806e18e2013-06-21 15:30:17 -0400105 callFilter.addAction(CallManagerCallBack.CONF_CREATED);
106 callFilter.addAction(CallManagerCallBack.CONF_REMOVED);
107 callFilter.addAction(CallManagerCallBack.CONF_CHANGED);
alisiondf1dac92013-06-27 17:35:53 -0400108 callFilter.addAction(CallManagerCallBack.RECORD_STATE_CHANGED);
alision806e18e2013-06-21 15:30:17 -0400109 receiver = new IncomingReceiver(this, mBinder);
alision2cb99562013-05-30 17:02:20 -0400110 LocalBroadcastManager.getInstance(this).registerReceiver(receiver, callFilter);
alision2cb99562013-05-30 17:02:20 -0400111
alisioncc7bb422013-06-06 15:31:39 -0400112 getExecutor().execute(new StartRunnable());
Alexandre Lisione0045442013-10-25 09:16:19 -0400113
114 notificationManager = new SipNotifications(this);
Alexandre Lision6d867b92013-10-25 15:36:28 -0400115 mediaManager = new MediaManager(this);
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400116
Alexandre Lisione0045442013-10-25 09:16:19 -0400117 notificationManager.onServiceCreate();
Alexandre Lision6d867b92013-10-25 15:36:28 -0400118 mediaManager.startService();
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400119
alisioncc7bb422013-06-06 15:31:39 -0400120 }
alision43a9b362013-05-01 16:30:15 -0400121
122 /* called for each startService() */
123 @Override
124 public int onStartCommand(Intent intent, int flags, int startId) {
125 Log.i(TAG, "onStarted");
126 super.onStartCommand(intent, flags, startId);
127
alision806e18e2013-06-21 15:30:17 -0400128 receiver = new IncomingReceiver(this, mBinder);
Alexandre Lisione0045442013-10-25 09:16:19 -0400129
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400130 return START_STICKY; /* started and stopped explicitly */
alision43a9b362013-05-01 16:30:15 -0400131 }
132
133 @Override
134 public void onDestroy() {
Alexandre Lision52214992013-10-28 17:41:23 -0400135 Log.i(TAG, "onDestroy");
alision43a9b362013-05-01 16:30:15 -0400136 /* called once by stopService() */
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400137
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400138 LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
Alexandre Lisione0045442013-10-25 09:16:19 -0400139 notificationManager.onServiceDestroy();
Alexandre Lisionafbe8f62013-10-25 15:52:44 -0400140
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400141 getExecutor().execute(new FinalizeRunnable());
alision43a9b362013-05-01 16:30:15 -0400142 super.onDestroy();
143
alision43a9b362013-05-01 16:30:15 -0400144 }
145
146 @Override
147 public IBinder onBind(Intent arg0) {
148 Log.i(TAG, "onBound");
149 return mBinder;
150 }
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400151
alision43a9b362013-05-01 16:30:15 -0400152 private static Looper createLooper() {
153 if (executorThread == null) {
154 Log.d(TAG, "Creating new handler thread");
155 // ADT gives a fake warning due to bad parse rule.
156 executorThread = new HandlerThread("SipService.Executor");
157 executorThread.start();
158 }
159 return executorThread.getLooper();
160 }
161
162 public SipServiceExecutor getExecutor() {
163 // create mExecutor lazily
164 if (mExecutor == null) {
165 mExecutor = new SipServiceExecutor(this);
166 }
167 return mExecutor;
168 }
169
170 // Executes immediate tasks in a single executorThread.
171 public static class SipServiceExecutor extends Handler {
172 WeakReference<SipService> handlerService;
173
174 SipServiceExecutor(SipService s) {
175 super(createLooper());
176 handlerService = new WeakReference<SipService>(s);
177 }
178
179 public void execute(Runnable task) {
180 // TODO: add wakelock
181 Message.obtain(this, 0/* don't care */, task).sendToTarget();
Alexandre Lisioncdec5952013-07-17 14:18:22 -0400182 Log.w(TAG, "SenT!");
alision43a9b362013-05-01 16:30:15 -0400183 }
184
185 @Override
186 public void handleMessage(Message msg) {
187 if (msg.obj instanceof Runnable) {
188 executeInternal((Runnable) msg.obj);
189 } else {
190 Log.w(TAG, "can't handle msg: " + msg);
191 }
192 }
193
194 private void executeInternal(Runnable task) {
195 try {
196 task.run();
197 } catch (Throwable t) {
198 Log.e(TAG, "run task: " + task, t);
199 }
200 }
201 }
Alexandre Lision63870a72013-10-28 16:33:47 -0400202
203 private void stopDaemon() {
204 if (managerImpl != null) {
205 managerImpl.finish();
206 isPjSipStackStarted = false;
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400207 }
208 }
alision43a9b362013-05-01 16:30:15 -0400209
210 private void startPjSipStack() throws SameThreadException {
211 if (isPjSipStackStarted)
212 return;
213
214 try {
215 System.loadLibrary("gnustl_shared");
alision43a9b362013-05-01 16:30:15 -0400216 System.loadLibrary("crypto");
217 System.loadLibrary("ssl");
alision43a9b362013-05-01 16:30:15 -0400218 System.loadLibrary("sflphone");
219 isPjSipStackStarted = true;
Adrien Béraud9360f242013-09-19 11:07:42 +1000220
alision43a9b362013-05-01 16:30:15 -0400221 } catch (UnsatisfiedLinkError e) {
222 Log.e(TAG, "Problem with the current Pj stack...", e);
223 isPjSipStackStarted = false;
224 return;
225 } catch (Exception e) {
226 Log.e(TAG, "Problem with the current Pj stack...", e);
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400227 isPjSipStackStarted = false;
alision43a9b362013-05-01 16:30:15 -0400228 }
229
Alexandre Lision67817192013-07-18 12:04:30 -0400230 Log.i(TAG, "PjSIPStack started");
231 managerImpl = SFLPhoneservice.instance();
232
233 /* set static AppPath before calling manager.init */
Alexandre Lision63870a72013-10-28 16:33:47 -0400234 // managerImpl.setPath(getApplication().getFilesDir().getAbsolutePath());
alision43a9b362013-05-01 16:30:15 -0400235
alision3ea8f3c2013-07-16 17:35:35 -0400236 callManagerJNI = new CallManager();
alision43a9b362013-05-01 16:30:15 -0400237 callManagerCallBack = new CallManagerCallBack(this);
238 SFLPhoneservice.setCallbackObject(callManagerCallBack);
239
alision3ea8f3c2013-07-16 17:35:35 -0400240 configurationManagerJNI = new ConfigurationManager();
alision43a9b362013-05-01 16:30:15 -0400241 configurationManagerCallback = new ConfigurationManagerCallback(this);
242 SFLPhoneservice.setConfigurationCallbackObject(configurationManagerCallback);
Alexandre Lision67817192013-07-18 12:04:30 -0400243 managerImpl.init("");
Adrien Béraud9360f242013-09-19 11:07:42 +1000244
Alexandre Lision67817192013-07-18 12:04:30 -0400245 Log.i(TAG, "->startPjSipStack");
alision43a9b362013-05-01 16:30:15 -0400246 }
247
Adrien Béraud9360f242013-09-19 11:07:42 +1000248 public HashMap<String, SipCall> getCurrent_calls() {
alision2cb99562013-05-30 17:02:20 -0400249 return current_calls;
250 }
251
alision43a9b362013-05-01 16:30:15 -0400252 // Enforce same thread contract to ensure we do not call from somewhere else
253 public class SameThreadException extends Exception {
254 private static final long serialVersionUID = -905639124232613768L;
255
256 public SameThreadException() {
257 super("Should be launched from a single worker thread");
258 }
259 }
260
261 public abstract static class SipRunnable implements Runnable {
262 protected abstract void doRun() throws SameThreadException, RemoteException;
263
Adrien Béraud9360f242013-09-19 11:07:42 +1000264 @Override
alision43a9b362013-05-01 16:30:15 -0400265 public void run() {
266 try {
267 doRun();
268 } catch (SameThreadException e) {
269 Log.e(TAG, "Not done from same thread");
270 } catch (RemoteException e) {
271 Log.e(TAG, e.toString());
272 }
273 }
274 }
275
Alexandre Lisionfab23f82013-11-01 11:22:30 -0400276 public abstract class SipRunnableWithReturn implements Runnable {
alision43a9b362013-05-01 16:30:15 -0400277 Object obj = null;
278 boolean done = false;
279
280 protected abstract Object doRun() throws SameThreadException;
281
282 public Object getVal() {
283 return obj;
284 }
285
286 public boolean isDone() {
287 return done;
288 }
289
Adrien Béraud9360f242013-09-19 11:07:42 +1000290 @Override
alision43a9b362013-05-01 16:30:15 -0400291 public void run() {
292 try {
Alexandre Lision35577132013-12-06 15:21:15 -0500293 if (isPjSipStackStarted)
Alexandre Lisionfab23f82013-11-01 11:22:30 -0400294 obj = doRun();
alision43a9b362013-05-01 16:30:15 -0400295 done = true;
296 } catch (SameThreadException e) {
297 Log.e(TAG, "Not done from same thread");
298 }
299 }
300 }
301
302 class StartRunnable extends SipRunnable {
303 @Override
304 protected void doRun() throws SameThreadException {
305 startPjSipStack();
306 }
307 }
Alexandre Lision63870a72013-10-28 16:33:47 -0400308
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400309 class FinalizeRunnable extends SipRunnable {
310 @Override
311 protected void doRun() throws SameThreadException {
312 stopDaemon();
313 }
314 }
alision43a9b362013-05-01 16:30:15 -0400315
alision43a9b362013-05-01 16:30:15 -0400316 /* ************************************
317 *
318 * Implement public interface for the service
319 *
Alexandre Lision67817192013-07-18 12:04:30 -0400320 * *********************************
321 */
322
Emeric Vigier6119d782012-09-21 18:04:14 -0400323 private final ISipService.Stub mBinder = new ISipService.Stub() {
324
325 @Override
alisionfde875f2013-05-28 17:01:54 -0400326 public void placeCall(final SipCall call) {
Emeric Vigier6119d782012-09-21 18:04:14 -0400327 getExecutor().execute(new SipRunnable() {
328 @Override
329 protected void doRun() throws SameThreadException {
330 Log.i(TAG, "SipService.placeCall() thread running...");
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400331 callManagerJNI.placeCall(call.getAccount().getAccountID(), call.getCallId(), call.getContact().getPhones().get(0).getNumber());
Adrien Béraud9360f242013-09-19 11:07:42 +1000332
Alexandre Lision2aae48d2013-12-04 13:50:38 -0500333 HashMap<String, String> details = SwigNativeConverter.convertCallDetailsToNative(callManagerJNI.getCallDetails(call.getCallId()));
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400334 // watchout timestamp stored by sflphone is in seconds
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400335 call.setTimestamp_start(Long.parseLong(details.get(ServiceConstants.call.TIMESTAMP_START)));
alision2cb99562013-05-30 17:02:20 -0400336 getCurrent_calls().put(call.getCallId(), call);
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500337 mediaManager.obtainAudioFocus(false);
Emeric Vigier6119d782012-09-21 18:04:14 -0400338 }
339 });
340 }
341
342 @Override
343 public void refuse(final String callID) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500344
Emeric Vigier6119d782012-09-21 18:04:14 -0400345 getExecutor().execute(new SipRunnable() {
346 @Override
347 protected void doRun() throws SameThreadException {
348 Log.i(TAG, "SipService.refuse() thread running...");
349 callManagerJNI.refuse(callID);
350 }
351 });
352 }
353
354 @Override
355 public void accept(final String callID) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500356 mediaManager.stopRing();
Emeric Vigier6119d782012-09-21 18:04:14 -0400357 getExecutor().execute(new SipRunnable() {
358 @Override
359 protected void doRun() throws SameThreadException {
Tristan Matthews40cf25e2013-07-24 13:45:15 -0400360 Log.i(TAG, "SipService.accept() thread running...");
Emeric Vigier6119d782012-09-21 18:04:14 -0400361 callManagerJNI.accept(callID);
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500362 mediaManager.RouteToInternalSpeaker();
Emeric Vigier6119d782012-09-21 18:04:14 -0400363 }
364 });
365 }
366
367 @Override
368 public void hangUp(final String callID) {
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500369 mediaManager.stopRing();
Emeric Vigier6119d782012-09-21 18:04:14 -0400370 getExecutor().execute(new SipRunnable() {
371 @Override
372 protected void doRun() throws SameThreadException {
373 Log.i(TAG, "SipService.hangUp() thread running...");
374 callManagerJNI.hangUp(callID);
Alexandre Lision35577132013-12-06 15:21:15 -0500375 mediaManager.abandonAudioFocus();
Emeric Vigier6119d782012-09-21 18:04:14 -0400376 }
377 });
378 }
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400379
380 @Override
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400381 public void hold(final String callID) {
382 getExecutor().execute(new SipRunnable() {
383 @Override
384 protected void doRun() throws SameThreadException {
385 Log.i(TAG, "SipService.hold() thread running...");
386 callManagerJNI.hold(callID);
387 }
388 });
389 }
390
391 @Override
392 public void unhold(final String callID) {
393 getExecutor().execute(new SipRunnable() {
394 @Override
395 protected void doRun() throws SameThreadException {
396 Log.i(TAG, "SipService.unhold() thread running...");
397 callManagerJNI.unhold(callID);
398 }
399 });
400 }
Adrien Béraud9360f242013-09-19 11:07:42 +1000401
Alexandre Lision6711ab22013-09-16 15:15:38 -0400402 @Override
403 public HashMap<String, String> getCallDetails(String callID) throws RemoteException {
404 class CallDetails extends SipRunnableWithReturn {
405 private String id;
406
407 CallDetails(String callID) {
408 id = callID;
409 }
410
411 @Override
412 protected StringMap doRun() throws SameThreadException {
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400413 Log.i(TAG, "SipService.getCallDetails() thread running...");
Alexandre Lision6711ab22013-09-16 15:15:38 -0400414 return callManagerJNI.getCallDetails(id);
415 }
416 }
417
418 CallDetails runInstance = new CallDetails(callID);
419 getExecutor().execute(runInstance);
420
421 while (!runInstance.isDone()) {
422 }
423 StringMap swigmap = (StringMap) runInstance.getVal();
424
Alexandre Lision2aae48d2013-12-04 13:50:38 -0500425 HashMap<String, String> nativemap = SwigNativeConverter.convertCallDetailsToNative(swigmap);
Alexandre Lision6711ab22013-09-16 15:15:38 -0400426
427 return nativemap;
Adrien Béraud9360f242013-09-19 11:07:42 +1000428
Alexandre Lision6711ab22013-09-16 15:15:38 -0400429 }
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400430
431 @Override
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400432 public void setAudioPlugin(final String audioPlugin) {
433 getExecutor().execute(new SipRunnable() {
alision371b77e2013-04-23 14:51:26 -0400434 @Override
435 protected void doRun() throws SameThreadException {
436 Log.i(TAG, "SipService.setAudioPlugin() thread running...");
437 configurationManagerJNI.setAudioPlugin(audioPlugin);
438 }
Alexandre Savard31d27c62012-10-04 16:05:08 -0400439 });
440 }
441
442 @Override
443 public String getCurrentAudioOutputPlugin() {
444 class CurrentAudioPlugin extends SipRunnableWithReturn {
445 @Override
446 protected String doRun() throws SameThreadException {
447 Log.i(TAG, "SipService.getCurrentAudioOutputPlugin() thread running...");
448 return configurationManagerJNI.getCurrentAudioOutputPlugin();
449 }
alision371b77e2013-04-23 14:51:26 -0400450 }
451 ;
Alexandre Savard31d27c62012-10-04 16:05:08 -0400452
453 CurrentAudioPlugin runInstance = new CurrentAudioPlugin();
454 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400455 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400456 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400457 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400458 return (String) runInstance.getVal();
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400459 }
Alexandre Savard713a34d2012-09-26 15:50:41 -0400460
461 @Override
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400462 public ArrayList<String> getAccountList() {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400463 class AccountList extends SipRunnableWithReturn {
464 @Override
465 protected StringVect doRun() throws SameThreadException {
466 Log.i(TAG, "SipService.getAccountList() thread running...");
467 return configurationManagerJNI.getAccountList();
468 }
alision371b77e2013-04-23 14:51:26 -0400469 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400470 AccountList runInstance = new AccountList();
Alexandre Lisioncdec5952013-07-17 14:18:22 -0400471 Log.i(TAG, "SipService.getAccountList() thread running...");
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400472 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400473 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400474 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400475 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400476 StringVect swigvect = (StringVect) runInstance.getVal();
477
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400478 ArrayList<String> nativelist = new ArrayList<String>();
Alexandre Savard52a72522012-09-27 16:40:13 -0400479
alision371b77e2013-04-23 14:51:26 -0400480 for (int i = 0; i < swigvect.size(); i++)
481 nativelist.add(swigvect.get(i));
Alexandre Savard52a72522012-09-27 16:40:13 -0400482
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400483 return nativelist;
alision371b77e2013-04-23 14:51:26 -0400484 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -0400485
Alexandre Lision4cf78702013-10-16 13:43:23 -0400486 @Override
Alexandre Lision4b4233a2013-10-16 17:24:17 -0400487 public void setAccountOrder(final String order) {
Alexandre Lision4cf78702013-10-16 13:43:23 -0400488 getExecutor().execute(new SipRunnable() {
489 @Override
490 protected void doRun() throws SameThreadException {
491 Log.i(TAG, "SipService.setAccountsOrder() thread running...");
492 configurationManagerJNI.setAccountsOrder(order);
493 }
494 });
495 }
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400496
497 @Override
alision371b77e2013-04-23 14:51:26 -0400498 public HashMap<String, String> getAccountDetails(final String accountID) {
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400499 class AccountDetails extends SipRunnableWithReturn {
500 private String id;
alision371b77e2013-04-23 14:51:26 -0400501
502 AccountDetails(String accountId) {
503 id = accountId;
504 }
505
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400506 @Override
507 protected StringMap doRun() throws SameThreadException {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400508 Log.i(TAG, "SipService.getAccountDetails() thread running...");
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400509 return configurationManagerJNI.getAccountDetails(id);
510 }
alision371b77e2013-04-23 14:51:26 -0400511 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400512
513 AccountDetails runInstance = new AccountDetails(accountID);
514 getExecutor().execute(runInstance);
alisionfde875f2013-05-28 17:01:54 -0400515
alision371b77e2013-04-23 14:51:26 -0400516 while (!runInstance.isDone()) {
517 }
518 StringMap swigmap = (StringMap) runInstance.getVal();
Alexandre Savard713a34d2012-09-26 15:50:41 -0400519
Alexandre Lision2aae48d2013-12-04 13:50:38 -0500520 HashMap<String, String> nativemap = SwigNativeConverter.convertAccountToNative(swigmap);
Alexandre Savard713a34d2012-09-26 15:50:41 -0400521
522 return nativemap;
523 }
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400524
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400525 @SuppressWarnings("unchecked")
526 // Hashmap runtime cast
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400527 @Override
alisioncc7bb422013-06-06 15:31:39 -0400528 public void setAccountDetails(final String accountId, final Map map) {
alision371b77e2013-04-23 14:51:26 -0400529 HashMap<String, String> nativemap = (HashMap<String, String>) map;
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400530
Alexandre Lision2aae48d2013-12-04 13:50:38 -0500531 final StringMap swigmap = SwigNativeConverter.convertFromNativeToSwig(nativemap);
Alexandre Savard718d49f2012-10-02 15:17:13 -0400532
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400533 getExecutor().execute(new SipRunnable() {
534 @Override
535 protected void doRun() throws SameThreadException {
alisioncc7bb422013-06-06 15:31:39 -0400536
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400537 configurationManagerJNI.setAccountDetails(accountId, swigmap);
alisioncc7bb422013-06-06 15:31:39 -0400538 Log.i(TAG, "SipService.setAccountDetails() thread running...");
539 }
540
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400541 });
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400542 }
543
Alexandre Lision451f2a82013-11-12 12:55:55 -0500544 @Override
545 public Map getAccountTemplate() throws RemoteException {
546 class AccountTemplate extends SipRunnableWithReturn {
547
548 @Override
549 protected StringMap doRun() throws SameThreadException {
550 Log.i(TAG, "SipService.getAccountTemplate() thread running...");
551 return configurationManagerJNI.getAccountTemplate();
552 }
553 }
554
555 AccountTemplate runInstance = new AccountTemplate();
556 getExecutor().execute(runInstance);
557
558 while (!runInstance.isDone()) {
559 }
560 StringMap swigmap = (StringMap) runInstance.getVal();
561
Alexandre Lision2aae48d2013-12-04 13:50:38 -0500562 HashMap<String, String> nativemap = SwigNativeConverter.convertAccountToNative(swigmap);
Alexandre Lision451f2a82013-11-12 12:55:55 -0500563
564 return nativemap;
565 }
alisioncc7bb422013-06-06 15:31:39 -0400566
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400567 @SuppressWarnings("unchecked")
568 // Hashmap runtime cast
Alexandre Savard46036572012-10-05 13:56:49 -0400569 @Override
570 public String addAccount(Map map) {
571 class AddAccount extends SipRunnableWithReturn {
572 StringMap map;
alision371b77e2013-04-23 14:51:26 -0400573
574 AddAccount(StringMap m) {
575 map = m;
576 }
577
Alexandre Savard46036572012-10-05 13:56:49 -0400578 @Override
579 protected String doRun() throws SameThreadException {
580 Log.i(TAG, "SipService.getAccountDetails() thread running...");
581 return configurationManagerJNI.addAccount(map);
582 }
alision371b77e2013-04-23 14:51:26 -0400583 }
Alexandre Savard46036572012-10-05 13:56:49 -0400584
Alexandre Lision2aae48d2013-12-04 13:50:38 -0500585 final StringMap swigmap = SwigNativeConverter.convertFromNativeToSwig((HashMap<String, String>) map);
Alexandre Savard46036572012-10-05 13:56:49 -0400586
587 AddAccount runInstance = new AddAccount(swigmap);
588 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400589 while (!runInstance.isDone()) {
590 }
Alexandre Savard46036572012-10-05 13:56:49 -0400591 String accountId = (String) runInstance.getVal();
592
593 return accountId;
594 }
595
596 @Override
597 public void removeAccount(final String accountId) {
598 getExecutor().execute(new SipRunnable() {
599 @Override
600 protected void doRun() throws SameThreadException {
601 Log.i(TAG, "SipService.setAccountDetails() thread running...");
602 configurationManagerJNI.removeAccount(accountId);
603 }
604 });
605 }
alision5f899632013-04-22 17:26:56 -0400606
alisione2a38e12013-04-25 14:20:20 -0400607 @Override
608 public ArrayList<HashMap<String, String>> getHistory() throws RemoteException {
609 class History extends SipRunnableWithReturn {
610
611 @Override
612 protected VectMap doRun() throws SameThreadException {
613 Log.i(TAG, "SipService.getHistory() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400614
alisione2a38e12013-04-25 14:20:20 -0400615 return configurationManagerJNI.getHistory();
616 }
617 }
618
619 History runInstance = new History();
620 getExecutor().execute(runInstance);
621 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400622 // Log.w(TAG, "Waiting for getHistory");
alisione2a38e12013-04-25 14:20:20 -0400623 }
alision1005ba12013-06-19 13:52:44 -0400624 Log.i(TAG, "SipService.getHistory() DONE");
alisione2a38e12013-04-25 14:20:20 -0400625 VectMap swigmap = (VectMap) runInstance.getVal();
626
Alexandre Lision2aae48d2013-12-04 13:50:38 -0500627 ArrayList<HashMap<String, String>> nativemap = SwigNativeConverter.convertHistoryToNative(swigmap);
alisione2a38e12013-04-25 14:20:20 -0400628
629 return nativemap;
630 }
alision7f18fc82013-05-01 09:37:33 -0400631
alision43a9b362013-05-01 16:30:15 -0400632 /*************************
633 * Transfer related API
634 *************************/
635
alision7f18fc82013-05-01 09:37:33 -0400636 @Override
637 public void transfer(final String callID, final String to) throws RemoteException {
638 getExecutor().execute(new SipRunnable() {
639 @Override
640 protected void doRun() throws SameThreadException, RemoteException {
641 Log.i(TAG, "SipService.transfer() thread running...");
642 if (callManagerJNI.transfer(callID, to)) {
643 Bundle bundle = new Bundle();
644 bundle.putString("CallID", callID);
645 bundle.putString("State", "HUNGUP");
646 Intent intent = new Intent(CallManagerCallBack.CALL_STATE_CHANGED);
alision7f18fc82013-05-01 09:37:33 -0400647 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
alision84813a12013-05-27 17:40:39 -0400648 sendBroadcast(intent);
alision7f18fc82013-05-01 09:37:33 -0400649 } else
650 Log.i(TAG, "NOT OK");
651 }
652 });
653
654 }
alision43a9b362013-05-01 16:30:15 -0400655
alision7f18fc82013-05-01 09:37:33 -0400656 @Override
657 public void attendedTransfer(final String transferID, final String targetID) throws RemoteException {
658 getExecutor().execute(new SipRunnable() {
659 @Override
660 protected void doRun() throws SameThreadException, RemoteException {
alision43a9b362013-05-01 16:30:15 -0400661 Log.i(TAG, "SipService.attendedTransfer() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400662 if (callManagerJNI.attendedTransfer(transferID, targetID)) {
663 Log.i(TAG, "OK");
alision7f18fc82013-05-01 09:37:33 -0400664 } else
665 Log.i(TAG, "NOT OK");
666 }
667 });
alision43a9b362013-05-01 16:30:15 -0400668
669 }
670
671 /*************************
672 * Conference related API
673 *************************/
674
675 @Override
676 public void removeConference(final String confID) throws RemoteException {
677 getExecutor().execute(new SipRunnable() {
678 @Override
679 protected void doRun() throws SameThreadException, RemoteException {
680 Log.i(TAG, "SipService.createConference() thread running...");
681 callManagerJNI.removeConference(confID);
682 }
683 });
684
alision7f18fc82013-05-01 09:37:33 -0400685 }
686
687 @Override
alision43a9b362013-05-01 16:30:15 -0400688 public void joinParticipant(final String sel_callID, final String drag_callID) throws RemoteException {
689 getExecutor().execute(new SipRunnable() {
690 @Override
691 protected void doRun() throws SameThreadException, RemoteException {
692 Log.i(TAG, "SipService.joinParticipant() thread running...");
693 callManagerJNI.joinParticipant(sel_callID, drag_callID);
alision806e18e2013-06-21 15:30:17 -0400694 // Generate a CONF_CREATED callback
alision43a9b362013-05-01 16:30:15 -0400695 }
696 });
697
alision7f18fc82013-05-01 09:37:33 -0400698 }
699
alision7f18fc82013-05-01 09:37:33 -0400700 @Override
alision806e18e2013-06-21 15:30:17 -0400701 public void addParticipant(final SipCall call, final String confID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400702 getExecutor().execute(new SipRunnable() {
703 @Override
704 protected void doRun() throws SameThreadException, RemoteException {
705 Log.i(TAG, "SipService.addParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400706 callManagerJNI.addParticipant(call.getCallId(), confID);
707 current_confs.get(confID).getParticipants().add(call);
alision43a9b362013-05-01 16:30:15 -0400708 }
709 });
710
alision7f18fc82013-05-01 09:37:33 -0400711 }
712
713 @Override
alision43a9b362013-05-01 16:30:15 -0400714 public void addMainParticipant(final String confID) throws RemoteException {
715 getExecutor().execute(new SipRunnable() {
716 @Override
717 protected void doRun() throws SameThreadException, RemoteException {
718 Log.i(TAG, "SipService.addMainParticipant() thread running...");
719 callManagerJNI.addMainParticipant(confID);
720 }
721 });
722
alision7f18fc82013-05-01 09:37:33 -0400723 }
724
725 @Override
alision43a9b362013-05-01 16:30:15 -0400726 public void detachParticipant(final String callID) throws RemoteException {
727 getExecutor().execute(new SipRunnable() {
728 @Override
729 protected void doRun() throws SameThreadException, RemoteException {
730 Log.i(TAG, "SipService.detachParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400731 Log.i(TAG, "Detaching " + callID);
732 Iterator<Entry<String, Conference>> it = current_confs.entrySet().iterator();
733 Log.i(TAG, "current_confs size " + current_confs.size());
734 while (it.hasNext()) {
735 Conference tmp = it.next().getValue();
736 Log.i(TAG, "conf has " + tmp.getParticipants().size() + " participants");
737 if (tmp.contains(callID)) {
738 current_calls.put(callID, tmp.getCall(callID));
739 Log.i(TAG, "Call found and put in current_calls");
740 }
741 }
alision43a9b362013-05-01 16:30:15 -0400742 callManagerJNI.detachParticipant(callID);
743 }
744 });
745
alision7f18fc82013-05-01 09:37:33 -0400746 }
747
748 @Override
alision43a9b362013-05-01 16:30:15 -0400749 public void joinConference(final String sel_confID, final String drag_confID) throws RemoteException {
750 getExecutor().execute(new SipRunnable() {
751 @Override
752 protected void doRun() throws SameThreadException, RemoteException {
753 Log.i(TAG, "SipService.joinConference() thread running...");
754 callManagerJNI.joinConference(sel_confID, drag_confID);
755 }
756 });
757
alision7f18fc82013-05-01 09:37:33 -0400758 }
759
760 @Override
alision43a9b362013-05-01 16:30:15 -0400761 public void hangUpConference(final String confID) throws RemoteException {
762 getExecutor().execute(new SipRunnable() {
763 @Override
764 protected void doRun() throws SameThreadException, RemoteException {
765 Log.i(TAG, "SipService.joinConference() thread running...");
766 callManagerJNI.hangUpConference(confID);
767 }
768 });
769
alision7f18fc82013-05-01 09:37:33 -0400770 }
771
772 @Override
alision43a9b362013-05-01 16:30:15 -0400773 public void holdConference(final String confID) throws RemoteException {
774 getExecutor().execute(new SipRunnable() {
775 @Override
776 protected void doRun() throws SameThreadException, RemoteException {
777 Log.i(TAG, "SipService.holdConference() thread running...");
778 callManagerJNI.holdConference(confID);
779 }
780 });
781
alision7f18fc82013-05-01 09:37:33 -0400782 }
783
784 @Override
alision43a9b362013-05-01 16:30:15 -0400785 public void unholdConference(final String confID) throws RemoteException {
786 getExecutor().execute(new SipRunnable() {
787 @Override
788 protected void doRun() throws SameThreadException, RemoteException {
789 Log.i(TAG, "SipService.unholdConference() thread running...");
790 callManagerJNI.unholdConference(confID);
791 }
792 });
793
alision7f18fc82013-05-01 09:37:33 -0400794 }
Alexandre Lision67817192013-07-18 12:04:30 -0400795
alisioncd8fb912013-06-28 14:43:51 -0400796 @Override
797 public boolean isConferenceParticipant(final String callID) throws RemoteException {
798 class IsParticipant extends SipRunnableWithReturn {
799
800 @Override
801 protected Boolean doRun() throws SameThreadException {
802 Log.i(TAG, "SipService.isRecording() thread running...");
803 return callManagerJNI.isConferenceParticipant(callID);
804 }
805 }
806
807 IsParticipant runInstance = new IsParticipant();
808 getExecutor().execute(runInstance);
809 while (!runInstance.isDone()) {
810 }
811
812 return (Boolean) runInstance.getVal();
813 }
alision7f18fc82013-05-01 09:37:33 -0400814
815 @Override
alisiondf1dac92013-06-27 17:35:53 -0400816 public HashMap<String, Conference> getConferenceList() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -0400817 // class ConfList extends SipRunnableWithReturn {
818 // @Override
819 // protected StringVect doRun() throws SameThreadException {
820 // Log.i(TAG, "SipService.getConferenceList() thread running...");
821 // return callManagerJNI.getConferenceList();
822 // }
823 // }
824 // ;
825 // ConfList runInstance = new ConfList();
826 // getExecutor().execute(runInstance);
827 // while (!runInstance.isDone()) {
828 // // Log.w(TAG, "Waiting for getConferenceList");
829 // }
830 // StringVect swigvect = (StringVect) runInstance.getVal();
831 //
832 // ArrayList<String> nativelist = new ArrayList<String>();
833 //
834 // for (int i = 0; i < swigvect.size(); i++)
835 // nativelist.add(swigvect.get(i));
836 //
837 // return nativelist;
alisiondf1dac92013-06-27 17:35:53 -0400838 return current_confs;
alision7f18fc82013-05-01 09:37:33 -0400839 }
840
841 @Override
alision907bde72013-06-20 14:40:37 -0400842 public List getParticipantList(final String confID) throws RemoteException {
843 class PartList extends SipRunnableWithReturn {
844 @Override
845 protected StringVect doRun() throws SameThreadException {
846 Log.i(TAG, "SipService.getAccountList() thread running...");
847 return callManagerJNI.getParticipantList(confID);
848 }
849 }
850 ;
851 PartList runInstance = new PartList();
852 getExecutor().execute(runInstance);
853 while (!runInstance.isDone()) {
854 // Log.w(TAG, "Waiting for getConferenceList");
855 }
856 StringVect swigvect = (StringVect) runInstance.getVal();
857
858 ArrayList<String> nativelist = new ArrayList<String>();
859
860 for (int i = 0; i < swigvect.size(); i++)
861 nativelist.add(swigvect.get(i));
862
863 return nativelist;
alision7f18fc82013-05-01 09:37:33 -0400864 }
alision806e18e2013-06-21 15:30:17 -0400865
alision1005ba12013-06-19 13:52:44 -0400866 @Override
alision7f18fc82013-05-01 09:37:33 -0400867 public String getConferenceId(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400868 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400869 return null;
870 }
871
872 @Override
alision806e18e2013-06-21 15:30:17 -0400873 public String getConferenceDetails(final String callID) throws RemoteException {
874 class ConfDetails extends SipRunnableWithReturn {
875 @Override
876 protected StringMap doRun() throws SameThreadException {
877 Log.i(TAG, "SipService.getAccountList() thread running...");
878 return callManagerJNI.getConferenceDetails(callID);
879 }
880 }
881 ;
882 ConfDetails runInstance = new ConfDetails();
883 getExecutor().execute(runInstance);
884 while (!runInstance.isDone()) {
885 // Log.w(TAG, "Waiting for getConferenceList");
886 }
887 StringMap swigvect = (StringMap) runInstance.getVal();
888
889 return swigvect.get("CONF_STATE");
alision7f18fc82013-05-01 09:37:33 -0400890 }
891
alision04a00182013-05-10 17:05:29 -0400892 @Override
893 public String getRecordPath() throws RemoteException {
894 class RecordPath extends SipRunnableWithReturn {
895
896 @Override
897 protected String doRun() throws SameThreadException {
898 Log.i(TAG, "SipService.getRecordPath() thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +1000899 return configurationManagerJNI.getRecordPath();
alision04a00182013-05-10 17:05:29 -0400900 }
901 }
902
903 RecordPath runInstance = new RecordPath();
904 getExecutor().execute(runInstance);
905 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400906 // Log.w(TAG, "Waiting for getRecordPath");
alision04a00182013-05-10 17:05:29 -0400907 }
908 String path = (String) runInstance.getVal();
909
910 return path;
911 }
912
913 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400914 public boolean toggleRecordingCall(final String id) throws RemoteException {
Adrien Béraud9360f242013-09-19 11:07:42 +1000915
Alexandre Lisiona764c682013-09-09 10:02:07 -0400916 class ToggleRecording extends SipRunnableWithReturn {
917
alision04a00182013-05-10 17:05:29 -0400918 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400919 protected Boolean doRun() throws SameThreadException {
920 Log.i(TAG, "SipService.toggleRecordingCall() thread running...");
Alexandre Lision3874e552013-11-04 17:20:12 -0500921 boolean result = callManagerJNI.toggleRecording(id);
Alexandre Lision35577132013-12-06 15:21:15 -0500922
923 if (getCurrent_calls().containsKey(id)) {
Alexandre Lision3874e552013-11-04 17:20:12 -0500924 getCurrent_calls().get(id).setRecording(result);
Alexandre Lision35577132013-12-06 15:21:15 -0500925 } else if (getCurrent_confs().containsKey(id)) {
Alexandre Lision3874e552013-11-04 17:20:12 -0500926 getCurrent_confs().get(id).setRecording(result);
927 } else {
928 // A call in a conference has been put on hold
929 Iterator<Conference> it = getCurrent_confs().values().iterator();
930 while (it.hasNext()) {
931 Conference c = it.next();
932 if (c.getCall(id) != null)
933 c.getCall(id).setRecording(result);
934 }
935 }
936 return result;
alision04a00182013-05-10 17:05:29 -0400937 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400938 }
939
940 ToggleRecording runInstance = new ToggleRecording();
941 getExecutor().execute(runInstance);
942 while (!runInstance.isDone()) {
943 }
944
945 return (Boolean) runInstance.getVal();
alision04a00182013-05-10 17:05:29 -0400946
947 }
Alexandre Lision67817192013-07-18 12:04:30 -0400948
alision50fa0722013-06-25 17:29:44 -0400949 @Override
950 public boolean startRecordedFilePlayback(final String filepath) throws RemoteException {
951 getExecutor().execute(new SipRunnable() {
952 @Override
953 protected void doRun() throws SameThreadException, RemoteException {
954 Log.i(TAG, "SipService.setRecordingCall() thread running...");
955 callManagerJNI.startRecordedFilePlayback(filepath);
956 }
957 });
958 return false;
959 }
960
961 @Override
962 public void stopRecordedFilePlayback(final String filepath) throws RemoteException {
963 getExecutor().execute(new SipRunnable() {
964 @Override
965 protected void doRun() throws SameThreadException, RemoteException {
966 Log.i(TAG, "SipService.stopRecordedFilePlayback() thread running...");
967 callManagerJNI.stopRecordedFilePlayback(filepath);
968 }
969 });
970 }
alision04a00182013-05-10 17:05:29 -0400971
972 @Override
973 public void setRecordPath(final String path) throws RemoteException {
974 getExecutor().execute(new SipRunnable() {
975 @Override
976 protected void doRun() throws SameThreadException, RemoteException {
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400977 Log.i(TAG, "SipService.setRecordPath() " + path + " thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +1000978 configurationManagerJNI.setRecordPath(path);
alision04a00182013-05-10 17:05:29 -0400979 }
980 });
981 }
982
983 @Override
Alexandre Lisiond5686032013-10-29 11:09:21 -0400984 public void sendTextMessage(final String callID, final SipMessage message) throws RemoteException {
alision04a00182013-05-10 17:05:29 -0400985 getExecutor().execute(new SipRunnable() {
986 @Override
987 protected void doRun() throws SameThreadException, RemoteException {
988 Log.i(TAG, "SipService.sendTextMessage() thread running...");
Alexandre Lisiond5686032013-10-29 11:09:21 -0400989 callManagerJNI.sendTextMessage(callID, message.comment);
Alexandre Lision35577132013-12-06 15:21:15 -0500990 if (getCurrent_calls().get(callID) != null)
Alexandre Lisiond5545232013-10-29 11:24:02 -0400991 getCurrent_calls().get(callID).addSipMessage(message);
Alexandre Lision35577132013-12-06 15:21:15 -0500992 else if (getCurrent_confs().get(callID) != null)
Alexandre Lisiond5545232013-10-29 11:24:02 -0400993 getCurrent_confs().get(callID).addSipMessage(message);
alision04a00182013-05-10 17:05:29 -0400994 }
995 });
996
997 }
998
alisiond295ec22013-05-17 10:12:13 -0400999 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -04001000 public List getAudioCodecList(final String accountID) throws RemoteException {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001001 class AudioCodecList extends SipRunnableWithReturn {
1002
1003 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001004 protected ArrayList<Codec> doRun() throws SameThreadException {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001005 Log.i(TAG, "SipService.getAudioCodecList() thread running...");
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001006 ArrayList<Codec> results = new ArrayList<Codec>();
Alexandre Lision4cf78702013-10-16 13:43:23 -04001007
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001008 IntVect active_payloads = configurationManagerJNI.getActiveAudioCodecList(accountID);
1009 for (int i = 0; i < active_payloads.size(); ++i) {
1010
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001011 results.add(new Codec(active_payloads.get(i), configurationManagerJNI.getAudioCodecDetails(active_payloads.get(i)), true));
Alexandre Lisione0045442013-10-25 09:16:19 -04001012
Alexandre Lision4cf78702013-10-16 13:43:23 -04001013 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001014
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001015 // if (results.get(active_payloads.get(i)) != null) {
1016 // results.get(active_payloads.get(i)).setEnabled(true);
Alexandre Lision35577132013-12-06 15:21:15 -05001017
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001018 IntVect payloads = configurationManagerJNI.getAudioCodecList();
1019
1020 for (int i = 0; i < payloads.size(); ++i) {
1021 boolean isActive = false;
1022 for (Codec co : results) {
1023 if (co.getPayload().toString().contentEquals(String.valueOf(payloads.get(i))))
1024 isActive = true;
1025
1026 }
1027 if (isActive)
1028 continue;
1029 else
1030 results.add(new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i)), false));
1031
1032 }
1033
1034 // if (!results.containsKey(payloads.get(i))) {
1035 // results.put(payloads.get(i), new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i)), false));
1036 // Log.i(TAG, "Other, Adding:" + results.get((payloads.get(i))).getName());
1037 // }
1038
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001039 return results;
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001040 }
1041 }
1042
1043 AudioCodecList runInstance = new AudioCodecList();
1044 getExecutor().execute(runInstance);
1045 while (!runInstance.isDone()) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001046 }
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001047 ArrayList<Codec> codecs = (ArrayList<Codec>) runInstance.getVal();
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001048 return codecs;
alisiond295ec22013-05-17 10:12:13 -04001049 }
1050
alision9f7a6ec2013-05-24 16:26:26 -04001051 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -04001052 public Map getRingtoneList() throws RemoteException {
1053 class RingtoneList extends SipRunnableWithReturn {
1054
1055 @Override
1056 protected StringMap doRun() throws SameThreadException {
1057 Log.i(TAG, "SipService.getRingtoneList() thread running...");
1058 return configurationManagerJNI.getRingtoneList();
1059 }
1060 }
1061
1062 RingtoneList runInstance = new RingtoneList();
1063 getExecutor().execute(runInstance);
1064 while (!runInstance.isDone()) {
1065 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001066 StringMap ringtones = (StringMap) runInstance.getVal();
1067
1068 for (int i = 0; i < ringtones.size(); ++i) {
1069 // Log.i(TAG,"ringtones "+i+" "+ ringtones.);
1070 }
1071
Alexandre Lision4cf78702013-10-16 13:43:23 -04001072 return null;
1073 }
1074
1075 @Override
1076 public void setActiveCodecList(final List codecs, final String accountID) throws RemoteException {
1077 getExecutor().execute(new SipRunnable() {
1078 @Override
1079 protected void doRun() throws SameThreadException, RemoteException {
1080 Log.i(TAG, "SipService.setActiveAudioCodecList() thread running...");
1081 StringVect list = new StringVect();
1082 for (int i = 0; i < codecs.size(); ++i) {
1083 list.add((String) codecs.get(i));
1084 }
1085 configurationManagerJNI.setActiveAudioCodecList(list, accountID);
1086 }
1087 });
1088 }
1089
1090 @Override
alisionfde875f2013-05-28 17:01:54 -04001091 public HashMap<String, SipCall> getCallList() throws RemoteException {
alision85704182013-05-29 15:23:03 -04001092 // class CallList extends SipRunnableWithReturn {
1093 //
1094 // @Override
1095 // protected StringVect doRun() throws SameThreadException {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -05001096 // Log.i(TAG, "SipService.getCallList() thread running...incoming");
alision85704182013-05-29 15:23:03 -04001097 // return callManagerJNI.getCallList();
1098 // }
1099 // }
1100 //
1101 // CallList runInstance = new CallList();
1102 // getExecutor().execute(runInstance);
1103 // while (!runInstance.isDone()) {
1104 // Log.w(TAG, "Waiting for getAudioCodecList");
1105 // }
1106 // StringVect swigmap = (StringVect) runInstance.getVal();
1107 //
1108 // ArrayList<String> nativemap = new ArrayList<String>();
1109 // for (int i = 0; i < swigmap.size(); ++i) {
1110 //
1111 // String t = swigmap.get(i);
1112 // nativemap.add(t);
1113 // }
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001114 // if(callManagerJNI == null)
1115 // return new HashMap<String, SipCall>();
1116 //
1117 //
1118 // HashMap<String, SipCall> results = new HashMap<String, SipCall>();
1119 // StringVect calls = callManagerJNI.getCallList();
1120 // for(int i = 0 ; i < calls.size(); ++i){
1121 // results.put(calls.get(i), new SipCall(calls.get(i), callManagerJNI.getCallDetails(calls.get(i))));
1122 // }
alision9f7a6ec2013-05-24 16:26:26 -04001123
alision2cb99562013-05-30 17:02:20 -04001124 return getCurrent_calls();
alision9f7a6ec2013-05-24 16:26:26 -04001125 }
1126
alision85704182013-05-29 15:23:03 -04001127 @Override
1128 public SipCall getCall(String callID) throws RemoteException {
alision2cb99562013-05-30 17:02:20 -04001129 return getCurrent_calls().get(callID);
1130 }
1131
1132 /***********************
1133 * Notification API
1134 ***********************/
1135 @Override
1136 public void createNotification() throws RemoteException {
Alexandre Lisionf02190d2013-12-12 17:26:12 -05001137 notificationManager.makeNotification(getCurrent_calls());
alisioncc7bb422013-06-06 15:31:39 -04001138
alision2cb99562013-05-30 17:02:20 -04001139 }
1140
1141 @Override
1142 public void destroyNotification() throws RemoteException {
Alexandre Lisionf02190d2013-12-12 17:26:12 -05001143 notificationManager.removeNotification();
alisioncc7bb422013-06-06 15:31:39 -04001144
alision2cb99562013-05-30 17:02:20 -04001145 }
alisioncc7bb422013-06-06 15:31:39 -04001146
alisiondf1dac92013-06-27 17:35:53 -04001147 @Override
1148 public Conference getCurrentCall() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -04001149 for (SipCall i : current_calls.values()) {
Adrien Béraud9360f242013-09-19 11:07:42 +10001150
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001151 // Incoming >> Ongoing
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001152 if (i.isIncoming()) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001153 Conference tmp = new Conference("-1");
1154 tmp.getParticipants().add(i);
1155 return tmp;
1156 }
Adrien Béraud9360f242013-09-19 11:07:42 +10001157
Alexandre Lision67817192013-07-18 12:04:30 -04001158 if (i.isOngoing()) {
alisiondf1dac92013-06-27 17:35:53 -04001159 Conference tmp = new Conference("-1");
1160 tmp.getParticipants().add(i);
1161 return tmp;
1162 }
1163 }
Alexandre Lision67817192013-07-18 12:04:30 -04001164
1165 if (!current_confs.isEmpty()) {
alisiondf1dac92013-06-27 17:35:53 -04001166 return (Conference) current_confs.values().toArray()[0];
1167 }
1168 return null;
1169 }
1170
Alexandre Lision64dc8c02013-09-25 15:32:25 -04001171 @Override
1172 public void playDtmf(final String key) throws RemoteException {
1173 getExecutor().execute(new SipRunnable() {
1174 @Override
1175 protected void doRun() throws SameThreadException, RemoteException {
1176 Log.i(TAG, "SipService.playDtmf() thread running...");
1177 callManagerJNI.playDTMF(key);
1178 }
1179 });
1180 }
1181
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001182 @Override
1183 public List getConcurrentCalls() throws RemoteException {
1184 ArrayList<Conference> toReturn = new ArrayList<Conference>();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001185
1186 for (SipCall sip : current_calls.values()) {
1187 if (!sip.isCurrent()) {
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001188 Conference tmp = new Conference("-1");
1189 tmp.getParticipants().add(sip);
1190 toReturn.add(tmp);
1191 }
1192 }
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001193
1194 Log.i(TAG, "toReturn SIZE " + toReturn.size());
1195
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001196 return toReturn;
1197 }
1198
Alexandre Lision4fb22622013-10-21 16:26:33 -04001199 @Override
1200 public String getCurrentAudioCodecName(String callID) throws RemoteException {
1201 return callManagerJNI.getCurrentAudioCodecName(callID);
1202 }
1203
Alexandre Lision31e30902013-11-08 15:16:59 -05001204 @Override
1205 public void setMuted(final boolean mute) throws RemoteException {
1206 getExecutor().execute(new SipRunnable() {
1207 @Override
1208 protected void doRun() throws SameThreadException, RemoteException {
1209 Log.i(TAG, "SipService.setMuted() thread running...");
1210 configurationManagerJNI.muteCapture(mute);
1211 }
1212 });
1213 }
1214
1215 @Override
1216 public boolean isCaptureMuted() throws RemoteException {
1217 class IsMuted extends SipRunnableWithReturn {
1218
1219 @Override
1220 protected Boolean doRun() throws SameThreadException {
1221 Log.i(TAG, "SipService.isCaptureMuted() thread running...");
1222 return configurationManagerJNI.isCaptureMuted();
1223 }
1224 }
1225
1226 IsMuted runInstance = new IsMuted();
1227 getExecutor().execute(runInstance);
1228 while (!runInstance.isDone()) {
1229 }
1230
1231 return (Boolean) runInstance.getVal();
1232 }
1233
Alexandre Lision3b7148e2013-11-13 17:23:06 -05001234 @Override
1235 public List getCredentials(final String accountID) throws RemoteException {
1236 class Credentials extends SipRunnableWithReturn {
1237
1238 @Override
1239 protected List doRun() throws SameThreadException {
1240 Log.i(TAG, "SipService.getCredentials() thread running...");
1241 VectMap map = configurationManagerJNI.getCredentials(accountID);
Alexandre Lision2aae48d2013-12-04 13:50:38 -05001242 ArrayList<HashMap<String, String>> result = SwigNativeConverter.convertCredentialsToNative(map);
Alexandre Lision3b7148e2013-11-13 17:23:06 -05001243 return result;
1244 }
1245 }
1246
1247 Credentials runInstance = new Credentials();
1248 getExecutor().execute(runInstance);
1249 while (!runInstance.isDone()) {
1250 }
1251 return (List) runInstance.getVal();
1252 }
1253
1254 @Override
1255 public void setCredentials(final String accountID, final List creds) throws RemoteException {
1256 getExecutor().execute(new SipRunnable() {
1257 @Override
1258 protected void doRun() throws SameThreadException, RemoteException {
1259 Log.i(TAG, "SipService.setCredentials() thread running...");
Alexandre Lision3cefec22013-11-14 17:26:35 -05001260 ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) creds;
Alexandre Lision2aae48d2013-12-04 13:50:38 -05001261 configurationManagerJNI.setCredentials(accountID, SwigNativeConverter.convertFromNativeToSwig(creds));
Alexandre Lision3b7148e2013-11-13 17:23:06 -05001262 }
1263 });
1264 }
1265
Alexandre Lision3e2a1d02013-11-19 17:23:00 -05001266 @Override
1267 public void registerAllAccounts() throws RemoteException {
1268 getExecutor().execute(new SipRunnable() {
1269 @Override
1270 protected void doRun() throws SameThreadException, RemoteException {
1271 Log.i(TAG, "SipService.registerAllAccounts() thread running...");
1272 configurationManagerJNI.registerAllAccounts();
1273 }
1274 });
1275 }
1276
Alexandre Lision5f733bc2013-12-04 13:10:30 -05001277 @Override
Alexandre Lision35577132013-12-06 15:21:15 -05001278 public void toggleSpeakerPhone(boolean toggle) throws RemoteException {
1279 if (toggle)
1280 mediaManager.RouteToSpeaker();
1281 else
1282 mediaManager.RouteToInternalSpeaker();
Alexandre Lision5f733bc2013-12-04 13:10:30 -05001283 }
1284
Emeric Vigier6119d782012-09-21 18:04:14 -04001285 };
Alexandre Lision35577132013-12-06 15:21:15 -05001286}