blob: 439a6ab733ef55e2c23f5d71cbfa238072f51459 [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
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500632 @Override
633 public void clearHistory() throws RemoteException {
634 getExecutor().execute(new SipRunnable() {
635 @Override
636 protected void doRun() throws SameThreadException {
637 Log.i(TAG, "SipService.clearHistory() thread running...");
638 configurationManagerJNI.clearHistory();
639 }
640 });
641
642 }
643
alision43a9b362013-05-01 16:30:15 -0400644 /*************************
645 * Transfer related API
646 *************************/
647
alision7f18fc82013-05-01 09:37:33 -0400648 @Override
649 public void transfer(final String callID, final String to) throws RemoteException {
650 getExecutor().execute(new SipRunnable() {
651 @Override
652 protected void doRun() throws SameThreadException, RemoteException {
653 Log.i(TAG, "SipService.transfer() thread running...");
654 if (callManagerJNI.transfer(callID, to)) {
655 Bundle bundle = new Bundle();
656 bundle.putString("CallID", callID);
657 bundle.putString("State", "HUNGUP");
658 Intent intent = new Intent(CallManagerCallBack.CALL_STATE_CHANGED);
alision7f18fc82013-05-01 09:37:33 -0400659 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
alision84813a12013-05-27 17:40:39 -0400660 sendBroadcast(intent);
alision7f18fc82013-05-01 09:37:33 -0400661 } else
662 Log.i(TAG, "NOT OK");
663 }
664 });
665
666 }
alision43a9b362013-05-01 16:30:15 -0400667
alision7f18fc82013-05-01 09:37:33 -0400668 @Override
669 public void attendedTransfer(final String transferID, final String targetID) throws RemoteException {
670 getExecutor().execute(new SipRunnable() {
671 @Override
672 protected void doRun() throws SameThreadException, RemoteException {
alision43a9b362013-05-01 16:30:15 -0400673 Log.i(TAG, "SipService.attendedTransfer() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400674 if (callManagerJNI.attendedTransfer(transferID, targetID)) {
675 Log.i(TAG, "OK");
alision7f18fc82013-05-01 09:37:33 -0400676 } else
677 Log.i(TAG, "NOT OK");
678 }
679 });
alision43a9b362013-05-01 16:30:15 -0400680
681 }
682
683 /*************************
684 * Conference related API
685 *************************/
686
687 @Override
688 public void removeConference(final String confID) throws RemoteException {
689 getExecutor().execute(new SipRunnable() {
690 @Override
691 protected void doRun() throws SameThreadException, RemoteException {
692 Log.i(TAG, "SipService.createConference() thread running...");
693 callManagerJNI.removeConference(confID);
694 }
695 });
696
alision7f18fc82013-05-01 09:37:33 -0400697 }
698
699 @Override
alision43a9b362013-05-01 16:30:15 -0400700 public void joinParticipant(final String sel_callID, final String drag_callID) throws RemoteException {
701 getExecutor().execute(new SipRunnable() {
702 @Override
703 protected void doRun() throws SameThreadException, RemoteException {
704 Log.i(TAG, "SipService.joinParticipant() thread running...");
705 callManagerJNI.joinParticipant(sel_callID, drag_callID);
alision806e18e2013-06-21 15:30:17 -0400706 // Generate a CONF_CREATED callback
alision43a9b362013-05-01 16:30:15 -0400707 }
708 });
709
alision7f18fc82013-05-01 09:37:33 -0400710 }
711
alision7f18fc82013-05-01 09:37:33 -0400712 @Override
alision806e18e2013-06-21 15:30:17 -0400713 public void addParticipant(final SipCall call, final String confID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400714 getExecutor().execute(new SipRunnable() {
715 @Override
716 protected void doRun() throws SameThreadException, RemoteException {
717 Log.i(TAG, "SipService.addParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400718 callManagerJNI.addParticipant(call.getCallId(), confID);
719 current_confs.get(confID).getParticipants().add(call);
alision43a9b362013-05-01 16:30:15 -0400720 }
721 });
722
alision7f18fc82013-05-01 09:37:33 -0400723 }
724
725 @Override
alision43a9b362013-05-01 16:30:15 -0400726 public void addMainParticipant(final String confID) throws RemoteException {
727 getExecutor().execute(new SipRunnable() {
728 @Override
729 protected void doRun() throws SameThreadException, RemoteException {
730 Log.i(TAG, "SipService.addMainParticipant() thread running...");
731 callManagerJNI.addMainParticipant(confID);
732 }
733 });
734
alision7f18fc82013-05-01 09:37:33 -0400735 }
736
737 @Override
alision43a9b362013-05-01 16:30:15 -0400738 public void detachParticipant(final String callID) throws RemoteException {
739 getExecutor().execute(new SipRunnable() {
740 @Override
741 protected void doRun() throws SameThreadException, RemoteException {
742 Log.i(TAG, "SipService.detachParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400743 Log.i(TAG, "Detaching " + callID);
744 Iterator<Entry<String, Conference>> it = current_confs.entrySet().iterator();
745 Log.i(TAG, "current_confs size " + current_confs.size());
746 while (it.hasNext()) {
747 Conference tmp = it.next().getValue();
748 Log.i(TAG, "conf has " + tmp.getParticipants().size() + " participants");
749 if (tmp.contains(callID)) {
750 current_calls.put(callID, tmp.getCall(callID));
751 Log.i(TAG, "Call found and put in current_calls");
752 }
753 }
alision43a9b362013-05-01 16:30:15 -0400754 callManagerJNI.detachParticipant(callID);
755 }
756 });
757
alision7f18fc82013-05-01 09:37:33 -0400758 }
759
760 @Override
alision43a9b362013-05-01 16:30:15 -0400761 public void joinConference(final String sel_confID, final String drag_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.joinConference(sel_confID, drag_confID);
767 }
768 });
769
alision7f18fc82013-05-01 09:37:33 -0400770 }
771
772 @Override
alision43a9b362013-05-01 16:30:15 -0400773 public void hangUpConference(final String confID) throws RemoteException {
774 getExecutor().execute(new SipRunnable() {
775 @Override
776 protected void doRun() throws SameThreadException, RemoteException {
777 Log.i(TAG, "SipService.joinConference() thread running...");
778 callManagerJNI.hangUpConference(confID);
779 }
780 });
781
alision7f18fc82013-05-01 09:37:33 -0400782 }
783
784 @Override
alision43a9b362013-05-01 16:30:15 -0400785 public void holdConference(final String confID) throws RemoteException {
786 getExecutor().execute(new SipRunnable() {
787 @Override
788 protected void doRun() throws SameThreadException, RemoteException {
789 Log.i(TAG, "SipService.holdConference() thread running...");
790 callManagerJNI.holdConference(confID);
791 }
792 });
793
alision7f18fc82013-05-01 09:37:33 -0400794 }
795
796 @Override
alision43a9b362013-05-01 16:30:15 -0400797 public void unholdConference(final String confID) throws RemoteException {
798 getExecutor().execute(new SipRunnable() {
799 @Override
800 protected void doRun() throws SameThreadException, RemoteException {
801 Log.i(TAG, "SipService.unholdConference() thread running...");
802 callManagerJNI.unholdConference(confID);
803 }
804 });
805
alision7f18fc82013-05-01 09:37:33 -0400806 }
Alexandre Lision67817192013-07-18 12:04:30 -0400807
alisioncd8fb912013-06-28 14:43:51 -0400808 @Override
809 public boolean isConferenceParticipant(final String callID) throws RemoteException {
810 class IsParticipant extends SipRunnableWithReturn {
811
812 @Override
813 protected Boolean doRun() throws SameThreadException {
814 Log.i(TAG, "SipService.isRecording() thread running...");
815 return callManagerJNI.isConferenceParticipant(callID);
816 }
817 }
818
819 IsParticipant runInstance = new IsParticipant();
820 getExecutor().execute(runInstance);
821 while (!runInstance.isDone()) {
822 }
823
824 return (Boolean) runInstance.getVal();
825 }
alision7f18fc82013-05-01 09:37:33 -0400826
827 @Override
alisiondf1dac92013-06-27 17:35:53 -0400828 public HashMap<String, Conference> getConferenceList() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -0400829 // class ConfList extends SipRunnableWithReturn {
830 // @Override
831 // protected StringVect doRun() throws SameThreadException {
832 // Log.i(TAG, "SipService.getConferenceList() thread running...");
833 // return callManagerJNI.getConferenceList();
834 // }
835 // }
836 // ;
837 // ConfList runInstance = new ConfList();
838 // getExecutor().execute(runInstance);
839 // while (!runInstance.isDone()) {
840 // // Log.w(TAG, "Waiting for getConferenceList");
841 // }
842 // StringVect swigvect = (StringVect) runInstance.getVal();
843 //
844 // ArrayList<String> nativelist = new ArrayList<String>();
845 //
846 // for (int i = 0; i < swigvect.size(); i++)
847 // nativelist.add(swigvect.get(i));
848 //
849 // return nativelist;
alisiondf1dac92013-06-27 17:35:53 -0400850 return current_confs;
alision7f18fc82013-05-01 09:37:33 -0400851 }
852
853 @Override
alision907bde72013-06-20 14:40:37 -0400854 public List getParticipantList(final String confID) throws RemoteException {
855 class PartList extends SipRunnableWithReturn {
856 @Override
857 protected StringVect doRun() throws SameThreadException {
858 Log.i(TAG, "SipService.getAccountList() thread running...");
859 return callManagerJNI.getParticipantList(confID);
860 }
861 }
862 ;
863 PartList runInstance = new PartList();
864 getExecutor().execute(runInstance);
865 while (!runInstance.isDone()) {
866 // Log.w(TAG, "Waiting for getConferenceList");
867 }
868 StringVect swigvect = (StringVect) runInstance.getVal();
869
870 ArrayList<String> nativelist = new ArrayList<String>();
871
872 for (int i = 0; i < swigvect.size(); i++)
873 nativelist.add(swigvect.get(i));
874
875 return nativelist;
alision7f18fc82013-05-01 09:37:33 -0400876 }
alision806e18e2013-06-21 15:30:17 -0400877
alision1005ba12013-06-19 13:52:44 -0400878 @Override
alision7f18fc82013-05-01 09:37:33 -0400879 public String getConferenceId(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400880 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400881 return null;
882 }
883
884 @Override
alision806e18e2013-06-21 15:30:17 -0400885 public String getConferenceDetails(final String callID) throws RemoteException {
886 class ConfDetails extends SipRunnableWithReturn {
887 @Override
888 protected StringMap doRun() throws SameThreadException {
889 Log.i(TAG, "SipService.getAccountList() thread running...");
890 return callManagerJNI.getConferenceDetails(callID);
891 }
892 }
893 ;
894 ConfDetails runInstance = new ConfDetails();
895 getExecutor().execute(runInstance);
896 while (!runInstance.isDone()) {
897 // Log.w(TAG, "Waiting for getConferenceList");
898 }
899 StringMap swigvect = (StringMap) runInstance.getVal();
900
901 return swigvect.get("CONF_STATE");
alision7f18fc82013-05-01 09:37:33 -0400902 }
903
alision04a00182013-05-10 17:05:29 -0400904 @Override
905 public String getRecordPath() throws RemoteException {
906 class RecordPath extends SipRunnableWithReturn {
907
908 @Override
909 protected String doRun() throws SameThreadException {
910 Log.i(TAG, "SipService.getRecordPath() thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +1000911 return configurationManagerJNI.getRecordPath();
alision04a00182013-05-10 17:05:29 -0400912 }
913 }
914
915 RecordPath runInstance = new RecordPath();
916 getExecutor().execute(runInstance);
917 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400918 // Log.w(TAG, "Waiting for getRecordPath");
alision04a00182013-05-10 17:05:29 -0400919 }
920 String path = (String) runInstance.getVal();
921
922 return path;
923 }
924
925 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400926 public boolean toggleRecordingCall(final String id) throws RemoteException {
Adrien Béraud9360f242013-09-19 11:07:42 +1000927
Alexandre Lisiona764c682013-09-09 10:02:07 -0400928 class ToggleRecording extends SipRunnableWithReturn {
929
alision04a00182013-05-10 17:05:29 -0400930 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400931 protected Boolean doRun() throws SameThreadException {
932 Log.i(TAG, "SipService.toggleRecordingCall() thread running...");
Alexandre Lision3874e552013-11-04 17:20:12 -0500933 boolean result = callManagerJNI.toggleRecording(id);
Alexandre Lision35577132013-12-06 15:21:15 -0500934
935 if (getCurrent_calls().containsKey(id)) {
Alexandre Lision3874e552013-11-04 17:20:12 -0500936 getCurrent_calls().get(id).setRecording(result);
Alexandre Lision35577132013-12-06 15:21:15 -0500937 } else if (getCurrent_confs().containsKey(id)) {
Alexandre Lision3874e552013-11-04 17:20:12 -0500938 getCurrent_confs().get(id).setRecording(result);
939 } else {
940 // A call in a conference has been put on hold
941 Iterator<Conference> it = getCurrent_confs().values().iterator();
942 while (it.hasNext()) {
943 Conference c = it.next();
944 if (c.getCall(id) != null)
945 c.getCall(id).setRecording(result);
946 }
947 }
948 return result;
alision04a00182013-05-10 17:05:29 -0400949 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400950 }
951
952 ToggleRecording runInstance = new ToggleRecording();
953 getExecutor().execute(runInstance);
954 while (!runInstance.isDone()) {
955 }
956
957 return (Boolean) runInstance.getVal();
alision04a00182013-05-10 17:05:29 -0400958
959 }
Alexandre Lision67817192013-07-18 12:04:30 -0400960
alision50fa0722013-06-25 17:29:44 -0400961 @Override
962 public boolean startRecordedFilePlayback(final String filepath) throws RemoteException {
963 getExecutor().execute(new SipRunnable() {
964 @Override
965 protected void doRun() throws SameThreadException, RemoteException {
966 Log.i(TAG, "SipService.setRecordingCall() thread running...");
967 callManagerJNI.startRecordedFilePlayback(filepath);
968 }
969 });
970 return false;
971 }
972
973 @Override
974 public void stopRecordedFilePlayback(final String filepath) throws RemoteException {
975 getExecutor().execute(new SipRunnable() {
976 @Override
977 protected void doRun() throws SameThreadException, RemoteException {
978 Log.i(TAG, "SipService.stopRecordedFilePlayback() thread running...");
979 callManagerJNI.stopRecordedFilePlayback(filepath);
980 }
981 });
982 }
alision04a00182013-05-10 17:05:29 -0400983
984 @Override
985 public void setRecordPath(final String path) throws RemoteException {
986 getExecutor().execute(new SipRunnable() {
987 @Override
988 protected void doRun() throws SameThreadException, RemoteException {
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400989 Log.i(TAG, "SipService.setRecordPath() " + path + " thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +1000990 configurationManagerJNI.setRecordPath(path);
alision04a00182013-05-10 17:05:29 -0400991 }
992 });
993 }
994
995 @Override
Alexandre Lisiond5686032013-10-29 11:09:21 -0400996 public void sendTextMessage(final String callID, final SipMessage message) throws RemoteException {
alision04a00182013-05-10 17:05:29 -0400997 getExecutor().execute(new SipRunnable() {
998 @Override
999 protected void doRun() throws SameThreadException, RemoteException {
1000 Log.i(TAG, "SipService.sendTextMessage() thread running...");
Alexandre Lisiond5686032013-10-29 11:09:21 -04001001 callManagerJNI.sendTextMessage(callID, message.comment);
Alexandre Lision35577132013-12-06 15:21:15 -05001002 if (getCurrent_calls().get(callID) != null)
Alexandre Lisiond5545232013-10-29 11:24:02 -04001003 getCurrent_calls().get(callID).addSipMessage(message);
Alexandre Lision35577132013-12-06 15:21:15 -05001004 else if (getCurrent_confs().get(callID) != null)
Alexandre Lisiond5545232013-10-29 11:24:02 -04001005 getCurrent_confs().get(callID).addSipMessage(message);
alision04a00182013-05-10 17:05:29 -04001006 }
1007 });
1008
1009 }
1010
alisiond295ec22013-05-17 10:12:13 -04001011 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -04001012 public List getAudioCodecList(final String accountID) throws RemoteException {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001013 class AudioCodecList extends SipRunnableWithReturn {
1014
1015 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001016 protected ArrayList<Codec> doRun() throws SameThreadException {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001017 Log.i(TAG, "SipService.getAudioCodecList() thread running...");
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001018 ArrayList<Codec> results = new ArrayList<Codec>();
Alexandre Lision4cf78702013-10-16 13:43:23 -04001019
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001020 IntVect active_payloads = configurationManagerJNI.getActiveAudioCodecList(accountID);
1021 for (int i = 0; i < active_payloads.size(); ++i) {
1022
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001023 results.add(new Codec(active_payloads.get(i), configurationManagerJNI.getAudioCodecDetails(active_payloads.get(i)), true));
Alexandre Lisione0045442013-10-25 09:16:19 -04001024
Alexandre Lision4cf78702013-10-16 13:43:23 -04001025 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001026
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001027 // if (results.get(active_payloads.get(i)) != null) {
1028 // results.get(active_payloads.get(i)).setEnabled(true);
Alexandre Lision35577132013-12-06 15:21:15 -05001029
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001030 IntVect payloads = configurationManagerJNI.getAudioCodecList();
1031
1032 for (int i = 0; i < payloads.size(); ++i) {
1033 boolean isActive = false;
1034 for (Codec co : results) {
1035 if (co.getPayload().toString().contentEquals(String.valueOf(payloads.get(i))))
1036 isActive = true;
1037
1038 }
1039 if (isActive)
1040 continue;
1041 else
1042 results.add(new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i)), false));
1043
1044 }
1045
1046 // if (!results.containsKey(payloads.get(i))) {
1047 // results.put(payloads.get(i), new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i)), false));
1048 // Log.i(TAG, "Other, Adding:" + results.get((payloads.get(i))).getName());
1049 // }
1050
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001051 return results;
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001052 }
1053 }
1054
1055 AudioCodecList runInstance = new AudioCodecList();
1056 getExecutor().execute(runInstance);
1057 while (!runInstance.isDone()) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001058 }
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001059 ArrayList<Codec> codecs = (ArrayList<Codec>) runInstance.getVal();
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001060 return codecs;
alisiond295ec22013-05-17 10:12:13 -04001061 }
1062
alision9f7a6ec2013-05-24 16:26:26 -04001063 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -04001064 public Map getRingtoneList() throws RemoteException {
1065 class RingtoneList extends SipRunnableWithReturn {
1066
1067 @Override
1068 protected StringMap doRun() throws SameThreadException {
1069 Log.i(TAG, "SipService.getRingtoneList() thread running...");
1070 return configurationManagerJNI.getRingtoneList();
1071 }
1072 }
1073
1074 RingtoneList runInstance = new RingtoneList();
1075 getExecutor().execute(runInstance);
1076 while (!runInstance.isDone()) {
1077 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001078 StringMap ringtones = (StringMap) runInstance.getVal();
1079
1080 for (int i = 0; i < ringtones.size(); ++i) {
1081 // Log.i(TAG,"ringtones "+i+" "+ ringtones.);
1082 }
1083
Alexandre Lision4cf78702013-10-16 13:43:23 -04001084 return null;
1085 }
1086
1087 @Override
1088 public void setActiveCodecList(final List codecs, final String accountID) throws RemoteException {
1089 getExecutor().execute(new SipRunnable() {
1090 @Override
1091 protected void doRun() throws SameThreadException, RemoteException {
1092 Log.i(TAG, "SipService.setActiveAudioCodecList() thread running...");
1093 StringVect list = new StringVect();
1094 for (int i = 0; i < codecs.size(); ++i) {
1095 list.add((String) codecs.get(i));
1096 }
1097 configurationManagerJNI.setActiveAudioCodecList(list, accountID);
1098 }
1099 });
1100 }
1101
1102 @Override
alisionfde875f2013-05-28 17:01:54 -04001103 public HashMap<String, SipCall> getCallList() throws RemoteException {
alision85704182013-05-29 15:23:03 -04001104 // class CallList extends SipRunnableWithReturn {
1105 //
1106 // @Override
1107 // protected StringVect doRun() throws SameThreadException {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -05001108 // Log.i(TAG, "SipService.getCallList() thread running...incoming");
alision85704182013-05-29 15:23:03 -04001109 // return callManagerJNI.getCallList();
1110 // }
1111 // }
1112 //
1113 // CallList runInstance = new CallList();
1114 // getExecutor().execute(runInstance);
1115 // while (!runInstance.isDone()) {
1116 // Log.w(TAG, "Waiting for getAudioCodecList");
1117 // }
1118 // StringVect swigmap = (StringVect) runInstance.getVal();
1119 //
1120 // ArrayList<String> nativemap = new ArrayList<String>();
1121 // for (int i = 0; i < swigmap.size(); ++i) {
1122 //
1123 // String t = swigmap.get(i);
1124 // nativemap.add(t);
1125 // }
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001126 // if(callManagerJNI == null)
1127 // return new HashMap<String, SipCall>();
1128 //
1129 //
1130 // HashMap<String, SipCall> results = new HashMap<String, SipCall>();
1131 // StringVect calls = callManagerJNI.getCallList();
1132 // for(int i = 0 ; i < calls.size(); ++i){
1133 // results.put(calls.get(i), new SipCall(calls.get(i), callManagerJNI.getCallDetails(calls.get(i))));
1134 // }
alision9f7a6ec2013-05-24 16:26:26 -04001135
alision2cb99562013-05-30 17:02:20 -04001136 return getCurrent_calls();
alision9f7a6ec2013-05-24 16:26:26 -04001137 }
1138
alision85704182013-05-29 15:23:03 -04001139 @Override
1140 public SipCall getCall(String callID) throws RemoteException {
alision2cb99562013-05-30 17:02:20 -04001141 return getCurrent_calls().get(callID);
1142 }
1143
1144 /***********************
1145 * Notification API
1146 ***********************/
1147 @Override
1148 public void createNotification() throws RemoteException {
Alexandre Lisionf02190d2013-12-12 17:26:12 -05001149 notificationManager.makeNotification(getCurrent_calls());
alisioncc7bb422013-06-06 15:31:39 -04001150
alision2cb99562013-05-30 17:02:20 -04001151 }
1152
1153 @Override
1154 public void destroyNotification() throws RemoteException {
Alexandre Lisionf02190d2013-12-12 17:26:12 -05001155 notificationManager.removeNotification();
alisioncc7bb422013-06-06 15:31:39 -04001156
alision2cb99562013-05-30 17:02:20 -04001157 }
alisioncc7bb422013-06-06 15:31:39 -04001158
alisiondf1dac92013-06-27 17:35:53 -04001159 @Override
1160 public Conference getCurrentCall() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -04001161 for (SipCall i : current_calls.values()) {
Adrien Béraud9360f242013-09-19 11:07:42 +10001162
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001163 // Incoming >> Ongoing
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001164 if (i.isIncoming()) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001165 Conference tmp = new Conference("-1");
1166 tmp.getParticipants().add(i);
1167 return tmp;
1168 }
Adrien Béraud9360f242013-09-19 11:07:42 +10001169
Alexandre Lision67817192013-07-18 12:04:30 -04001170 if (i.isOngoing()) {
alisiondf1dac92013-06-27 17:35:53 -04001171 Conference tmp = new Conference("-1");
1172 tmp.getParticipants().add(i);
1173 return tmp;
1174 }
1175 }
Alexandre Lision67817192013-07-18 12:04:30 -04001176
1177 if (!current_confs.isEmpty()) {
alisiondf1dac92013-06-27 17:35:53 -04001178 return (Conference) current_confs.values().toArray()[0];
1179 }
1180 return null;
1181 }
1182
Alexandre Lision64dc8c02013-09-25 15:32:25 -04001183 @Override
1184 public void playDtmf(final String key) throws RemoteException {
1185 getExecutor().execute(new SipRunnable() {
1186 @Override
1187 protected void doRun() throws SameThreadException, RemoteException {
1188 Log.i(TAG, "SipService.playDtmf() thread running...");
1189 callManagerJNI.playDTMF(key);
1190 }
1191 });
1192 }
1193
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001194 @Override
1195 public List getConcurrentCalls() throws RemoteException {
1196 ArrayList<Conference> toReturn = new ArrayList<Conference>();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001197
1198 for (SipCall sip : current_calls.values()) {
1199 if (!sip.isCurrent()) {
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001200 Conference tmp = new Conference("-1");
1201 tmp.getParticipants().add(sip);
1202 toReturn.add(tmp);
1203 }
1204 }
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001205
1206 Log.i(TAG, "toReturn SIZE " + toReturn.size());
1207
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001208 return toReturn;
1209 }
1210
Alexandre Lision4fb22622013-10-21 16:26:33 -04001211 @Override
1212 public String getCurrentAudioCodecName(String callID) throws RemoteException {
1213 return callManagerJNI.getCurrentAudioCodecName(callID);
1214 }
1215
Alexandre Lision31e30902013-11-08 15:16:59 -05001216 @Override
1217 public void setMuted(final boolean mute) throws RemoteException {
1218 getExecutor().execute(new SipRunnable() {
1219 @Override
1220 protected void doRun() throws SameThreadException, RemoteException {
1221 Log.i(TAG, "SipService.setMuted() thread running...");
1222 configurationManagerJNI.muteCapture(mute);
1223 }
1224 });
1225 }
1226
1227 @Override
1228 public boolean isCaptureMuted() throws RemoteException {
1229 class IsMuted extends SipRunnableWithReturn {
1230
1231 @Override
1232 protected Boolean doRun() throws SameThreadException {
1233 Log.i(TAG, "SipService.isCaptureMuted() thread running...");
1234 return configurationManagerJNI.isCaptureMuted();
1235 }
1236 }
1237
1238 IsMuted runInstance = new IsMuted();
1239 getExecutor().execute(runInstance);
1240 while (!runInstance.isDone()) {
1241 }
1242
1243 return (Boolean) runInstance.getVal();
1244 }
1245
Alexandre Lision3b7148e2013-11-13 17:23:06 -05001246 @Override
1247 public List getCredentials(final String accountID) throws RemoteException {
1248 class Credentials extends SipRunnableWithReturn {
1249
1250 @Override
1251 protected List doRun() throws SameThreadException {
1252 Log.i(TAG, "SipService.getCredentials() thread running...");
1253 VectMap map = configurationManagerJNI.getCredentials(accountID);
Alexandre Lision2aae48d2013-12-04 13:50:38 -05001254 ArrayList<HashMap<String, String>> result = SwigNativeConverter.convertCredentialsToNative(map);
Alexandre Lision3b7148e2013-11-13 17:23:06 -05001255 return result;
1256 }
1257 }
1258
1259 Credentials runInstance = new Credentials();
1260 getExecutor().execute(runInstance);
1261 while (!runInstance.isDone()) {
1262 }
1263 return (List) runInstance.getVal();
1264 }
1265
1266 @Override
1267 public void setCredentials(final String accountID, final List creds) throws RemoteException {
1268 getExecutor().execute(new SipRunnable() {
1269 @Override
1270 protected void doRun() throws SameThreadException, RemoteException {
1271 Log.i(TAG, "SipService.setCredentials() thread running...");
Alexandre Lision3cefec22013-11-14 17:26:35 -05001272 ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) creds;
Alexandre Lision2aae48d2013-12-04 13:50:38 -05001273 configurationManagerJNI.setCredentials(accountID, SwigNativeConverter.convertFromNativeToSwig(creds));
Alexandre Lision3b7148e2013-11-13 17:23:06 -05001274 }
1275 });
1276 }
1277
Alexandre Lision3e2a1d02013-11-19 17:23:00 -05001278 @Override
1279 public void registerAllAccounts() throws RemoteException {
1280 getExecutor().execute(new SipRunnable() {
1281 @Override
1282 protected void doRun() throws SameThreadException, RemoteException {
1283 Log.i(TAG, "SipService.registerAllAccounts() thread running...");
1284 configurationManagerJNI.registerAllAccounts();
1285 }
1286 });
1287 }
1288
Alexandre Lision5f733bc2013-12-04 13:10:30 -05001289 @Override
Alexandre Lision35577132013-12-06 15:21:15 -05001290 public void toggleSpeakerPhone(boolean toggle) throws RemoteException {
1291 if (toggle)
1292 mediaManager.RouteToSpeaker();
1293 else
1294 mediaManager.RouteToInternalSpeaker();
Alexandre Lision5f733bc2013-12-04 13:10:30 -05001295 }
1296
Emeric Vigier6119d782012-09-21 18:04:14 -04001297 };
Alexandre Lision35577132013-12-06 15:21:15 -05001298}