blob: 9fe9f5bede821f72063fce9decf60625445c4f4f [file] [log] [blame]
Emeric Vigier6119d782012-09-21 18:04:14 -04001/**
2 * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
alisionb1763882013-06-18 17:30:51 -04003 * Copyright (C) 2004-2013 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;
alision2cb99562013-05-30 17:02:20 -040034import java.util.Random;
Emeric Vigier6119d782012-09-21 18:04:14 -040035
Alexandre Lision3ccccf02013-10-07 14:10:46 -040036import org.sflphone.R;
Alexandre Lisiona3650992013-11-13 14:19:35 -050037import org.sflphone.account.AccountDetailAdvanced;
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.account.AccountDetailBasic;
39import org.sflphone.account.AccountDetailsHandler;
40import org.sflphone.account.CallDetailsHandler;
41import org.sflphone.account.HistoryHandler;
Alexandre Lision4ab53972013-11-04 16:59:18 -050042import org.sflphone.client.HomeActivity;
Alexandre Lision933ef0a2013-10-15 17:28:40 -040043import org.sflphone.model.Codec;
Alexandre Lision064e1e02013-10-01 16:18:42 -040044import org.sflphone.model.Conference;
45import org.sflphone.model.SipCall;
Alexandre Lisiond5686032013-10-29 11:09:21 -040046import org.sflphone.model.SipMessage;
Alexandre Lision064e1e02013-10-01 16:18:42 -040047import org.sflphone.receivers.IncomingReceiver;
Alexandre Lision6d867b92013-10-25 15:36:28 -040048import org.sflphone.utils.MediaManager;
Alexandre Lisione0045442013-10-25 09:16:19 -040049import org.sflphone.utils.SipNotifications;
Alexandre Lision064e1e02013-10-01 16:18:42 -040050
alision2cb99562013-05-30 17:02:20 -040051import android.app.Notification;
52import android.app.NotificationManager;
53import android.app.PendingIntent;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040054import android.app.Service;
alision17052d42013-04-22 10:39:38 -040055import android.content.Context;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040056import android.content.Intent;
alision17052d42013-04-22 10:39:38 -040057import android.content.IntentFilter;
alision7f18fc82013-05-01 09:37:33 -040058import android.os.Bundle;
Emeric Vigier6119d782012-09-21 18:04:14 -040059import android.os.Handler;
60import android.os.HandlerThread;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040061import android.os.IBinder;
Emeric Vigier6119d782012-09-21 18:04:14 -040062import android.os.Looper;
63import android.os.Message;
alision5f899632013-04-22 17:26:56 -040064import android.os.RemoteException;
alision2cb99562013-05-30 17:02:20 -040065import android.support.v4.app.NotificationCompat;
alision17052d42013-04-22 10:39:38 -040066import android.support.v4.content.LocalBroadcastManager;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040067import android.util.Log;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040068
Emeric Vigiereaf2c492012-09-19 14:38:20 -040069public class SipService extends Service {
70
71 static final String TAG = "SipService";
72 static final int DELAY = 5000; /* 5 sec */
Emeric Vigier6119d782012-09-21 18:04:14 -040073 private SipServiceExecutor mExecutor;
74 private static HandlerThread executorThread;
alision3ea8f3c2013-07-16 17:35:35 -040075 private CallManager callManagerJNI;
Alexandre Lision67817192013-07-18 12:04:30 -040076 private ManagerImpl managerImpl;
Emeric Vigier0007dee2012-09-24 11:35:58 -040077 private CallManagerCallBack callManagerCallBack;
alision3ea8f3c2013-07-16 17:35:35 -040078 private ConfigurationManager configurationManagerJNI;
Alexandre Savardfccd1dc2012-10-17 17:31:38 -040079 private ConfigurationManagerCallback configurationManagerCallback;
Emeric Vigier6119d782012-09-21 18:04:14 -040080 private boolean isPjSipStackStarted = false;
alisioncc7bb422013-06-06 15:31:39 -040081
Alexandre Lisione0045442013-10-25 09:16:19 -040082 public SipNotifications notificationManager;
Alexandre Lision6d867b92013-10-25 15:36:28 -040083 public MediaManager mediaManager;
Alexandre Lisione0045442013-10-25 09:16:19 -040084
alision2cb99562013-05-30 17:02:20 -040085 private HashMap<String, SipCall> current_calls = new HashMap<String, SipCall>();
alision806e18e2013-06-21 15:30:17 -040086 private HashMap<String, Conference> current_confs = new HashMap<String, Conference>();
87 private IncomingReceiver receiver;
Emeric Vigier6119d782012-09-21 18:04:14 -040088
alision806e18e2013-06-21 15:30:17 -040089 public HashMap<String, Conference> getCurrent_confs() {
90 return current_confs;
91 }
alision43a9b362013-05-01 16:30:15 -040092
93 @Override
94 public boolean onUnbind(Intent i) {
95 super.onUnbind(i);
96 Log.i(TAG, "onUnbind(intent)");
Alexandre Lision0f550ee2013-10-25 15:34:38 -040097 return true;
98 }
Alexandre Lisiondd68f652013-10-28 11:00:12 -040099
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400100 @Override
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400101 public void onRebind(Intent i) {
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400102 super.onRebind(i);
alision43a9b362013-05-01 16:30:15 -0400103 }
104
105 /* called once by startService() */
106 @Override
107 public void onCreate() {
108 Log.i(TAG, "onCreated");
109 super.onCreate();
110
alision43a9b362013-05-01 16:30:15 -0400111 IntentFilter callFilter = new IntentFilter(CallManagerCallBack.CALL_STATE_CHANGED);
112 callFilter.addAction(CallManagerCallBack.INCOMING_CALL);
113 callFilter.addAction(CallManagerCallBack.NEW_CALL_CREATED);
alision4a0eb092013-05-07 13:52:03 -0400114 callFilter.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
115 callFilter.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
alision04a00182013-05-10 17:05:29 -0400116 callFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
alision806e18e2013-06-21 15:30:17 -0400117 callFilter.addAction(CallManagerCallBack.CONF_CREATED);
118 callFilter.addAction(CallManagerCallBack.CONF_REMOVED);
119 callFilter.addAction(CallManagerCallBack.CONF_CHANGED);
alisiondf1dac92013-06-27 17:35:53 -0400120 callFilter.addAction(CallManagerCallBack.RECORD_STATE_CHANGED);
alision806e18e2013-06-21 15:30:17 -0400121 receiver = new IncomingReceiver(this, mBinder);
alision2cb99562013-05-30 17:02:20 -0400122 LocalBroadcastManager.getInstance(this).registerReceiver(receiver, callFilter);
alision2cb99562013-05-30 17:02:20 -0400123
alisioncc7bb422013-06-06 15:31:39 -0400124 getExecutor().execute(new StartRunnable());
Alexandre Lisione0045442013-10-25 09:16:19 -0400125
126 notificationManager = new SipNotifications(this);
Alexandre Lision6d867b92013-10-25 15:36:28 -0400127 mediaManager = new MediaManager(this);
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400128
Alexandre Lisione0045442013-10-25 09:16:19 -0400129 notificationManager.onServiceCreate();
Alexandre Lision6d867b92013-10-25 15:36:28 -0400130 mediaManager.startService();
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400131
alisioncc7bb422013-06-06 15:31:39 -0400132 }
alision43a9b362013-05-01 16:30:15 -0400133
134 /* called for each startService() */
135 @Override
136 public int onStartCommand(Intent intent, int flags, int startId) {
137 Log.i(TAG, "onStarted");
138 super.onStartCommand(intent, flags, startId);
139
alision806e18e2013-06-21 15:30:17 -0400140 receiver = new IncomingReceiver(this, mBinder);
Alexandre Lisione0045442013-10-25 09:16:19 -0400141
Alexandre Lision0f550ee2013-10-25 15:34:38 -0400142 return START_STICKY; /* started and stopped explicitly */
alision43a9b362013-05-01 16:30:15 -0400143 }
144
145 @Override
146 public void onDestroy() {
Alexandre Lision52214992013-10-28 17:41:23 -0400147 Log.i(TAG, "onDestroy");
alision43a9b362013-05-01 16:30:15 -0400148 /* called once by stopService() */
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400149
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400150 LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
Alexandre Lisione0045442013-10-25 09:16:19 -0400151 notificationManager.onServiceDestroy();
Alexandre Lisionafbe8f62013-10-25 15:52:44 -0400152
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400153 getExecutor().execute(new FinalizeRunnable());
alision43a9b362013-05-01 16:30:15 -0400154 super.onDestroy();
155
alision43a9b362013-05-01 16:30:15 -0400156 }
157
158 @Override
159 public IBinder onBind(Intent arg0) {
160 Log.i(TAG, "onBound");
161 return mBinder;
162 }
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400163
alision43a9b362013-05-01 16:30:15 -0400164 private static Looper createLooper() {
165 if (executorThread == null) {
166 Log.d(TAG, "Creating new handler thread");
167 // ADT gives a fake warning due to bad parse rule.
168 executorThread = new HandlerThread("SipService.Executor");
169 executorThread.start();
170 }
171 return executorThread.getLooper();
172 }
173
174 public SipServiceExecutor getExecutor() {
175 // create mExecutor lazily
176 if (mExecutor == null) {
177 mExecutor = new SipServiceExecutor(this);
178 }
179 return mExecutor;
180 }
181
182 // Executes immediate tasks in a single executorThread.
183 public static class SipServiceExecutor extends Handler {
184 WeakReference<SipService> handlerService;
185
186 SipServiceExecutor(SipService s) {
187 super(createLooper());
188 handlerService = new WeakReference<SipService>(s);
189 }
190
191 public void execute(Runnable task) {
192 // TODO: add wakelock
193 Message.obtain(this, 0/* don't care */, task).sendToTarget();
Alexandre Lisioncdec5952013-07-17 14:18:22 -0400194 Log.w(TAG, "SenT!");
alision43a9b362013-05-01 16:30:15 -0400195 }
196
197 @Override
198 public void handleMessage(Message msg) {
199 if (msg.obj instanceof Runnable) {
200 executeInternal((Runnable) msg.obj);
201 } else {
202 Log.w(TAG, "can't handle msg: " + msg);
203 }
204 }
205
206 private void executeInternal(Runnable task) {
207 try {
208 task.run();
209 } catch (Throwable t) {
210 Log.e(TAG, "run task: " + task, t);
211 }
212 }
213 }
Alexandre Lision63870a72013-10-28 16:33:47 -0400214
215 private void stopDaemon() {
216 if (managerImpl != null) {
217 managerImpl.finish();
218 isPjSipStackStarted = false;
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400219 }
220 }
alision43a9b362013-05-01 16:30:15 -0400221
222 private void startPjSipStack() throws SameThreadException {
223 if (isPjSipStackStarted)
224 return;
225
226 try {
227 System.loadLibrary("gnustl_shared");
228 System.loadLibrary("expat");
229 System.loadLibrary("yaml");
230 System.loadLibrary("ccgnu2");
231 System.loadLibrary("crypto");
232 System.loadLibrary("ssl");
Alexandre Lision7c6f4a62013-09-05 13:27:01 -0400233 System.loadLibrary("sndfile");
alision43a9b362013-05-01 16:30:15 -0400234 System.loadLibrary("ccrtp1");
alision43a9b362013-05-01 16:30:15 -0400235 System.loadLibrary("samplerate");
236 System.loadLibrary("codec_ulaw");
237 System.loadLibrary("codec_alaw");
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400238 System.loadLibrary("codec_g722");
Alexandre Lision6deda412013-09-25 13:21:22 -0400239 System.loadLibrary("codec_opus");
Alexandre Lisiona1ad1c32013-10-15 16:35:20 -0400240 System.loadLibrary("codec_gsm");
Alexandre Lision62138172013-10-17 11:52:45 -0400241 System.loadLibrary("codec_speex_nb");
242 System.loadLibrary("codec_speex_ub");
243 System.loadLibrary("codec_speex_wb");
alision43a9b362013-05-01 16:30:15 -0400244 System.loadLibrary("speexresampler");
245 System.loadLibrary("sflphone");
246 isPjSipStackStarted = true;
Adrien Béraud9360f242013-09-19 11:07:42 +1000247
alision43a9b362013-05-01 16:30:15 -0400248 } catch (UnsatisfiedLinkError e) {
249 Log.e(TAG, "Problem with the current Pj stack...", e);
250 isPjSipStackStarted = false;
251 return;
252 } catch (Exception e) {
253 Log.e(TAG, "Problem with the current Pj stack...", e);
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400254 isPjSipStackStarted = false;
alision43a9b362013-05-01 16:30:15 -0400255 }
256
Alexandre Lision67817192013-07-18 12:04:30 -0400257 Log.i(TAG, "PjSIPStack started");
258 managerImpl = SFLPhoneservice.instance();
259
260 /* set static AppPath before calling manager.init */
Alexandre Lision63870a72013-10-28 16:33:47 -0400261 // managerImpl.setPath(getApplication().getFilesDir().getAbsolutePath());
alision43a9b362013-05-01 16:30:15 -0400262
alision3ea8f3c2013-07-16 17:35:35 -0400263 callManagerJNI = new CallManager();
alision43a9b362013-05-01 16:30:15 -0400264 callManagerCallBack = new CallManagerCallBack(this);
265 SFLPhoneservice.setCallbackObject(callManagerCallBack);
266
alision3ea8f3c2013-07-16 17:35:35 -0400267 configurationManagerJNI = new ConfigurationManager();
alision43a9b362013-05-01 16:30:15 -0400268 configurationManagerCallback = new ConfigurationManagerCallback(this);
269 SFLPhoneservice.setConfigurationCallbackObject(configurationManagerCallback);
Alexandre Lision67817192013-07-18 12:04:30 -0400270 managerImpl.init("");
Adrien Béraud9360f242013-09-19 11:07:42 +1000271
Alexandre Lision67817192013-07-18 12:04:30 -0400272 Log.i(TAG, "->startPjSipStack");
alision43a9b362013-05-01 16:30:15 -0400273 }
274
Adrien Béraud9360f242013-09-19 11:07:42 +1000275 public HashMap<String, SipCall> getCurrent_calls() {
alision2cb99562013-05-30 17:02:20 -0400276 return current_calls;
277 }
278
alision43a9b362013-05-01 16:30:15 -0400279 // Enforce same thread contract to ensure we do not call from somewhere else
280 public class SameThreadException extends Exception {
281 private static final long serialVersionUID = -905639124232613768L;
282
283 public SameThreadException() {
284 super("Should be launched from a single worker thread");
285 }
286 }
287
288 public abstract static class SipRunnable implements Runnable {
289 protected abstract void doRun() throws SameThreadException, RemoteException;
290
Adrien Béraud9360f242013-09-19 11:07:42 +1000291 @Override
alision43a9b362013-05-01 16:30:15 -0400292 public void run() {
293 try {
294 doRun();
295 } catch (SameThreadException e) {
296 Log.e(TAG, "Not done from same thread");
297 } catch (RemoteException e) {
298 Log.e(TAG, e.toString());
299 }
300 }
301 }
302
Alexandre Lisionfab23f82013-11-01 11:22:30 -0400303 public abstract class SipRunnableWithReturn implements Runnable {
alision43a9b362013-05-01 16:30:15 -0400304 Object obj = null;
305 boolean done = false;
306
307 protected abstract Object doRun() throws SameThreadException;
308
309 public Object getVal() {
310 return obj;
311 }
312
313 public boolean isDone() {
314 return done;
315 }
316
Adrien Béraud9360f242013-09-19 11:07:42 +1000317 @Override
alision43a9b362013-05-01 16:30:15 -0400318 public void run() {
319 try {
Alexandre Lisionfab23f82013-11-01 11:22:30 -0400320 if(isPjSipStackStarted)
321 obj = doRun();
alision43a9b362013-05-01 16:30:15 -0400322 done = true;
323 } catch (SameThreadException e) {
324 Log.e(TAG, "Not done from same thread");
325 }
326 }
327 }
328
329 class StartRunnable extends SipRunnable {
330 @Override
331 protected void doRun() throws SameThreadException {
332 startPjSipStack();
333 }
334 }
Alexandre Lision63870a72013-10-28 16:33:47 -0400335
Alexandre Lisiondd68f652013-10-28 11:00:12 -0400336 class FinalizeRunnable extends SipRunnable {
337 @Override
338 protected void doRun() throws SameThreadException {
339 stopDaemon();
340 }
341 }
alision43a9b362013-05-01 16:30:15 -0400342
alision43a9b362013-05-01 16:30:15 -0400343 /* ************************************
344 *
345 * Implement public interface for the service
346 *
Alexandre Lision67817192013-07-18 12:04:30 -0400347 * *********************************
348 */
349
Emeric Vigier6119d782012-09-21 18:04:14 -0400350 private final ISipService.Stub mBinder = new ISipService.Stub() {
351
352 @Override
alisionfde875f2013-05-28 17:01:54 -0400353 public void placeCall(final SipCall call) {
Emeric Vigier6119d782012-09-21 18:04:14 -0400354 getExecutor().execute(new SipRunnable() {
355 @Override
356 protected void doRun() throws SameThreadException {
357 Log.i(TAG, "SipService.placeCall() thread running...");
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400358 callManagerJNI.placeCall(call.getAccount().getAccountID(), call.getCallId(), call.getContact().getPhones().get(0).getNumber());
Adrien Béraud9360f242013-09-19 11:07:42 +1000359
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400360 HashMap<String, String> details = CallDetailsHandler.convertSwigToNative(callManagerJNI.getCallDetails(call.getCallId()));
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400361 // watchout timestamp stored by sflphone is in seconds
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400362 call.setTimestamp_start(Long.parseLong(details.get(ServiceConstants.call.TIMESTAMP_START)));
alision2cb99562013-05-30 17:02:20 -0400363 getCurrent_calls().put(call.getCallId(), call);
Alexandre Lision6e9c7e52013-10-29 15:54:11 -0400364 mediaManager.obtainAudioFocus();
Emeric Vigier6119d782012-09-21 18:04:14 -0400365 }
366 });
367 }
368
369 @Override
370 public void refuse(final String callID) {
371 getExecutor().execute(new SipRunnable() {
372 @Override
373 protected void doRun() throws SameThreadException {
374 Log.i(TAG, "SipService.refuse() thread running...");
375 callManagerJNI.refuse(callID);
376 }
377 });
378 }
379
380 @Override
381 public void accept(final String callID) {
382 getExecutor().execute(new SipRunnable() {
383 @Override
384 protected void doRun() throws SameThreadException {
Tristan Matthews40cf25e2013-07-24 13:45:15 -0400385 Log.i(TAG, "SipService.accept() thread running...");
Emeric Vigier6119d782012-09-21 18:04:14 -0400386 callManagerJNI.accept(callID);
387 }
388 });
389 }
390
391 @Override
392 public void hangUp(final String callID) {
393 getExecutor().execute(new SipRunnable() {
394 @Override
395 protected void doRun() throws SameThreadException {
396 Log.i(TAG, "SipService.hangUp() thread running...");
397 callManagerJNI.hangUp(callID);
398 }
399 });
400 }
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400401
402 @Override
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400403 public void hold(final String callID) {
404 getExecutor().execute(new SipRunnable() {
405 @Override
406 protected void doRun() throws SameThreadException {
407 Log.i(TAG, "SipService.hold() thread running...");
408 callManagerJNI.hold(callID);
409 }
410 });
411 }
412
413 @Override
414 public void unhold(final String callID) {
415 getExecutor().execute(new SipRunnable() {
416 @Override
417 protected void doRun() throws SameThreadException {
418 Log.i(TAG, "SipService.unhold() thread running...");
419 callManagerJNI.unhold(callID);
420 }
421 });
422 }
Adrien Béraud9360f242013-09-19 11:07:42 +1000423
Alexandre Lision6711ab22013-09-16 15:15:38 -0400424 @Override
425 public HashMap<String, String> getCallDetails(String callID) throws RemoteException {
426 class CallDetails extends SipRunnableWithReturn {
427 private String id;
428
429 CallDetails(String callID) {
430 id = callID;
431 }
432
433 @Override
434 protected StringMap doRun() throws SameThreadException {
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400435 Log.i(TAG, "SipService.getCallDetails() thread running...");
Alexandre Lision6711ab22013-09-16 15:15:38 -0400436 return callManagerJNI.getCallDetails(id);
437 }
438 }
439
440 CallDetails runInstance = new CallDetails(callID);
441 getExecutor().execute(runInstance);
442
443 while (!runInstance.isDone()) {
444 }
445 StringMap swigmap = (StringMap) runInstance.getVal();
446
447 HashMap<String, String> nativemap = CallDetailsHandler.convertSwigToNative(swigmap);
448
449 return nativemap;
Adrien Béraud9360f242013-09-19 11:07:42 +1000450
Alexandre Lision6711ab22013-09-16 15:15:38 -0400451 }
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400452
453 @Override
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400454 public void setAudioPlugin(final String audioPlugin) {
455 getExecutor().execute(new SipRunnable() {
alision371b77e2013-04-23 14:51:26 -0400456 @Override
457 protected void doRun() throws SameThreadException {
458 Log.i(TAG, "SipService.setAudioPlugin() thread running...");
459 configurationManagerJNI.setAudioPlugin(audioPlugin);
460 }
Alexandre Savard31d27c62012-10-04 16:05:08 -0400461 });
462 }
463
464 @Override
465 public String getCurrentAudioOutputPlugin() {
466 class CurrentAudioPlugin extends SipRunnableWithReturn {
467 @Override
468 protected String doRun() throws SameThreadException {
469 Log.i(TAG, "SipService.getCurrentAudioOutputPlugin() thread running...");
470 return configurationManagerJNI.getCurrentAudioOutputPlugin();
471 }
alision371b77e2013-04-23 14:51:26 -0400472 }
473 ;
Alexandre Savard31d27c62012-10-04 16:05:08 -0400474
475 CurrentAudioPlugin runInstance = new CurrentAudioPlugin();
476 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400477 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400478 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400479 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400480 return (String) runInstance.getVal();
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400481 }
Alexandre Savard713a34d2012-09-26 15:50:41 -0400482
483 @Override
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400484 public ArrayList<String> getAccountList() {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400485 class AccountList extends SipRunnableWithReturn {
486 @Override
487 protected StringVect doRun() throws SameThreadException {
488 Log.i(TAG, "SipService.getAccountList() thread running...");
489 return configurationManagerJNI.getAccountList();
490 }
alision371b77e2013-04-23 14:51:26 -0400491 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400492 AccountList runInstance = new AccountList();
Alexandre Lisioncdec5952013-07-17 14:18:22 -0400493 Log.i(TAG, "SipService.getAccountList() thread running...");
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400494 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400495 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400496 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400497 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400498 StringVect swigvect = (StringVect) runInstance.getVal();
499
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400500 ArrayList<String> nativelist = new ArrayList<String>();
Alexandre Savard52a72522012-09-27 16:40:13 -0400501
alision371b77e2013-04-23 14:51:26 -0400502 for (int i = 0; i < swigvect.size(); i++)
503 nativelist.add(swigvect.get(i));
Alexandre Savard52a72522012-09-27 16:40:13 -0400504
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400505 return nativelist;
alision371b77e2013-04-23 14:51:26 -0400506 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -0400507
Alexandre Lision4cf78702013-10-16 13:43:23 -0400508 @Override
Alexandre Lision4b4233a2013-10-16 17:24:17 -0400509 public void setAccountOrder(final String order) {
Alexandre Lision4cf78702013-10-16 13:43:23 -0400510 getExecutor().execute(new SipRunnable() {
511 @Override
512 protected void doRun() throws SameThreadException {
513 Log.i(TAG, "SipService.setAccountsOrder() thread running...");
514 configurationManagerJNI.setAccountsOrder(order);
515 }
516 });
517 }
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400518
519 @Override
alision371b77e2013-04-23 14:51:26 -0400520 public HashMap<String, String> getAccountDetails(final String accountID) {
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400521 class AccountDetails extends SipRunnableWithReturn {
522 private String id;
alision371b77e2013-04-23 14:51:26 -0400523
524 AccountDetails(String accountId) {
525 id = accountId;
526 }
527
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400528 @Override
529 protected StringMap doRun() throws SameThreadException {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400530 Log.i(TAG, "SipService.getAccountDetails() thread running...");
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400531 return configurationManagerJNI.getAccountDetails(id);
532 }
alision371b77e2013-04-23 14:51:26 -0400533 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400534
535 AccountDetails runInstance = new AccountDetails(accountID);
536 getExecutor().execute(runInstance);
alisionfde875f2013-05-28 17:01:54 -0400537
alision371b77e2013-04-23 14:51:26 -0400538 while (!runInstance.isDone()) {
539 }
540 StringMap swigmap = (StringMap) runInstance.getVal();
Alexandre Savard713a34d2012-09-26 15:50:41 -0400541
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400542 HashMap<String, String> nativemap = AccountDetailsHandler.convertSwigToNative(swigmap);
Alexandre Savard713a34d2012-09-26 15:50:41 -0400543
544 return nativemap;
545 }
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400546
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400547 @SuppressWarnings("unchecked")
548 // Hashmap runtime cast
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400549 @Override
alisioncc7bb422013-06-06 15:31:39 -0400550 public void setAccountDetails(final String accountId, final Map map) {
alision371b77e2013-04-23 14:51:26 -0400551 HashMap<String, String> nativemap = (HashMap<String, String>) map;
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400552
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400553 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig(nativemap);
Alexandre Savard718d49f2012-10-02 15:17:13 -0400554
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400555 getExecutor().execute(new SipRunnable() {
556 @Override
557 protected void doRun() throws SameThreadException {
alisioncc7bb422013-06-06 15:31:39 -0400558
559 configurationManagerJNI.setCredentials(accountId, extractCredentials(map));
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400560 configurationManagerJNI.setAccountDetails(accountId, swigmap);
alisioncc7bb422013-06-06 15:31:39 -0400561
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400562 // convertSwigToNative(configurationManagerJNI.getCredentials(accountId));
alisioncc7bb422013-06-06 15:31:39 -0400563 Log.i(TAG, "SipService.setAccountDetails() thread running...");
564 }
565
566 private VectMap extractCredentials(Map map) {
567 VectMap swigmap = new VectMap();
568 StringMap entry = new StringMap();
alision5cfc35d2013-07-11 15:11:39 -0400569 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME));
570 if ((String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_REALM) != null)
571 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_REALM, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_REALM));
alisioncc7bb422013-06-06 15:31:39 -0400572 else
alision5cfc35d2013-07-11 15:11:39 -0400573 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_REALM, "*");
574 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD));
alisioncc7bb422013-06-06 15:31:39 -0400575 swigmap.add(entry);
576 return swigmap;
577
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400578 }
579 });
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400580 }
581
Alexandre Lision451f2a82013-11-12 12:55:55 -0500582 @Override
583 public Map getAccountTemplate() throws RemoteException {
584 class AccountTemplate extends SipRunnableWithReturn {
585
586 @Override
587 protected StringMap doRun() throws SameThreadException {
588 Log.i(TAG, "SipService.getAccountTemplate() thread running...");
589 return configurationManagerJNI.getAccountTemplate();
590 }
591 }
592
593 AccountTemplate runInstance = new AccountTemplate();
594 getExecutor().execute(runInstance);
595
596 while (!runInstance.isDone()) {
597 }
598 StringMap swigmap = (StringMap) runInstance.getVal();
599
600 HashMap<String, String> nativemap = AccountDetailsHandler.convertSwigToNative(swigmap);
601
602 return nativemap;
603 }
alisioncc7bb422013-06-06 15:31:39 -0400604
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400605 @SuppressWarnings("unchecked")
606 // Hashmap runtime cast
Alexandre Savard46036572012-10-05 13:56:49 -0400607 @Override
608 public String addAccount(Map map) {
609 class AddAccount extends SipRunnableWithReturn {
610 StringMap map;
alision371b77e2013-04-23 14:51:26 -0400611
612 AddAccount(StringMap m) {
613 map = m;
614 }
615
Alexandre Savard46036572012-10-05 13:56:49 -0400616 @Override
617 protected String doRun() throws SameThreadException {
618 Log.i(TAG, "SipService.getAccountDetails() thread running...");
619 return configurationManagerJNI.addAccount(map);
620 }
alision371b77e2013-04-23 14:51:26 -0400621 }
Alexandre Savard46036572012-10-05 13:56:49 -0400622
alision371b77e2013-04-23 14:51:26 -0400623 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig((HashMap<String, String>) map);
Alexandre Savard46036572012-10-05 13:56:49 -0400624
625 AddAccount runInstance = new AddAccount(swigmap);
626 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400627 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400628 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400629 }
Alexandre Savard46036572012-10-05 13:56:49 -0400630 String accountId = (String) runInstance.getVal();
631
632 return accountId;
633 }
634
635 @Override
636 public void removeAccount(final String accountId) {
637 getExecutor().execute(new SipRunnable() {
638 @Override
639 protected void doRun() throws SameThreadException {
640 Log.i(TAG, "SipService.setAccountDetails() thread running...");
641 configurationManagerJNI.removeAccount(accountId);
642 }
643 });
644 }
alision5f899632013-04-22 17:26:56 -0400645
alisione2a38e12013-04-25 14:20:20 -0400646 @Override
647 public ArrayList<HashMap<String, String>> getHistory() throws RemoteException {
648 class History extends SipRunnableWithReturn {
649
650 @Override
651 protected VectMap doRun() throws SameThreadException {
652 Log.i(TAG, "SipService.getHistory() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400653
alisione2a38e12013-04-25 14:20:20 -0400654 return configurationManagerJNI.getHistory();
655 }
656 }
657
658 History runInstance = new History();
659 getExecutor().execute(runInstance);
660 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400661 // Log.w(TAG, "Waiting for getHistory");
alisione2a38e12013-04-25 14:20:20 -0400662 }
alision1005ba12013-06-19 13:52:44 -0400663 Log.i(TAG, "SipService.getHistory() DONE");
alisione2a38e12013-04-25 14:20:20 -0400664 VectMap swigmap = (VectMap) runInstance.getVal();
665
666 ArrayList<HashMap<String, String>> nativemap = HistoryHandler.convertSwigToNative(swigmap);
667
668 return nativemap;
669 }
alision7f18fc82013-05-01 09:37:33 -0400670
alision43a9b362013-05-01 16:30:15 -0400671 /*************************
672 * Transfer related API
673 *************************/
674
alision7f18fc82013-05-01 09:37:33 -0400675 @Override
676 public void transfer(final String callID, final String to) throws RemoteException {
677 getExecutor().execute(new SipRunnable() {
678 @Override
679 protected void doRun() throws SameThreadException, RemoteException {
680 Log.i(TAG, "SipService.transfer() thread running...");
681 if (callManagerJNI.transfer(callID, to)) {
682 Bundle bundle = new Bundle();
683 bundle.putString("CallID", callID);
684 bundle.putString("State", "HUNGUP");
685 Intent intent = new Intent(CallManagerCallBack.CALL_STATE_CHANGED);
alision7f18fc82013-05-01 09:37:33 -0400686 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
alision84813a12013-05-27 17:40:39 -0400687 sendBroadcast(intent);
alision7f18fc82013-05-01 09:37:33 -0400688 } else
689 Log.i(TAG, "NOT OK");
690 }
691 });
692
693 }
alision43a9b362013-05-01 16:30:15 -0400694
alision7f18fc82013-05-01 09:37:33 -0400695 @Override
696 public void attendedTransfer(final String transferID, final String targetID) throws RemoteException {
697 getExecutor().execute(new SipRunnable() {
698 @Override
699 protected void doRun() throws SameThreadException, RemoteException {
alision43a9b362013-05-01 16:30:15 -0400700 Log.i(TAG, "SipService.attendedTransfer() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400701 if (callManagerJNI.attendedTransfer(transferID, targetID)) {
702 Log.i(TAG, "OK");
alision7f18fc82013-05-01 09:37:33 -0400703 } else
704 Log.i(TAG, "NOT OK");
705 }
706 });
alision43a9b362013-05-01 16:30:15 -0400707
708 }
709
710 /*************************
711 * Conference related API
712 *************************/
713
714 @Override
715 public void removeConference(final String confID) throws RemoteException {
716 getExecutor().execute(new SipRunnable() {
717 @Override
718 protected void doRun() throws SameThreadException, RemoteException {
719 Log.i(TAG, "SipService.createConference() thread running...");
720 callManagerJNI.removeConference(confID);
721 }
722 });
723
alision7f18fc82013-05-01 09:37:33 -0400724 }
725
726 @Override
alision43a9b362013-05-01 16:30:15 -0400727 public void joinParticipant(final String sel_callID, final String drag_callID) throws RemoteException {
728 getExecutor().execute(new SipRunnable() {
729 @Override
730 protected void doRun() throws SameThreadException, RemoteException {
731 Log.i(TAG, "SipService.joinParticipant() thread running...");
732 callManagerJNI.joinParticipant(sel_callID, drag_callID);
alision806e18e2013-06-21 15:30:17 -0400733 // Generate a CONF_CREATED callback
alision43a9b362013-05-01 16:30:15 -0400734 }
735 });
736
alision7f18fc82013-05-01 09:37:33 -0400737 }
738
alision7f18fc82013-05-01 09:37:33 -0400739 @Override
alision806e18e2013-06-21 15:30:17 -0400740 public void addParticipant(final SipCall call, final String confID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400741 getExecutor().execute(new SipRunnable() {
742 @Override
743 protected void doRun() throws SameThreadException, RemoteException {
744 Log.i(TAG, "SipService.addParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400745 callManagerJNI.addParticipant(call.getCallId(), confID);
746 current_confs.get(confID).getParticipants().add(call);
alision43a9b362013-05-01 16:30:15 -0400747 }
748 });
749
alision7f18fc82013-05-01 09:37:33 -0400750 }
751
752 @Override
alision43a9b362013-05-01 16:30:15 -0400753 public void addMainParticipant(final String confID) throws RemoteException {
754 getExecutor().execute(new SipRunnable() {
755 @Override
756 protected void doRun() throws SameThreadException, RemoteException {
757 Log.i(TAG, "SipService.addMainParticipant() thread running...");
758 callManagerJNI.addMainParticipant(confID);
759 }
760 });
761
alision7f18fc82013-05-01 09:37:33 -0400762 }
763
764 @Override
alision43a9b362013-05-01 16:30:15 -0400765 public void detachParticipant(final String callID) throws RemoteException {
766 getExecutor().execute(new SipRunnable() {
767 @Override
768 protected void doRun() throws SameThreadException, RemoteException {
769 Log.i(TAG, "SipService.detachParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400770 Log.i(TAG, "Detaching " + callID);
771 Iterator<Entry<String, Conference>> it = current_confs.entrySet().iterator();
772 Log.i(TAG, "current_confs size " + current_confs.size());
773 while (it.hasNext()) {
774 Conference tmp = it.next().getValue();
775 Log.i(TAG, "conf has " + tmp.getParticipants().size() + " participants");
776 if (tmp.contains(callID)) {
777 current_calls.put(callID, tmp.getCall(callID));
778 Log.i(TAG, "Call found and put in current_calls");
779 }
780 }
alision43a9b362013-05-01 16:30:15 -0400781 callManagerJNI.detachParticipant(callID);
782 }
783 });
784
alision7f18fc82013-05-01 09:37:33 -0400785 }
786
787 @Override
alision43a9b362013-05-01 16:30:15 -0400788 public void joinConference(final String sel_confID, final String drag_confID) throws RemoteException {
789 getExecutor().execute(new SipRunnable() {
790 @Override
791 protected void doRun() throws SameThreadException, RemoteException {
792 Log.i(TAG, "SipService.joinConference() thread running...");
793 callManagerJNI.joinConference(sel_confID, drag_confID);
794 }
795 });
796
alision7f18fc82013-05-01 09:37:33 -0400797 }
798
799 @Override
alision43a9b362013-05-01 16:30:15 -0400800 public void hangUpConference(final String confID) throws RemoteException {
801 getExecutor().execute(new SipRunnable() {
802 @Override
803 protected void doRun() throws SameThreadException, RemoteException {
804 Log.i(TAG, "SipService.joinConference() thread running...");
805 callManagerJNI.hangUpConference(confID);
806 }
807 });
808
alision7f18fc82013-05-01 09:37:33 -0400809 }
810
811 @Override
alision43a9b362013-05-01 16:30:15 -0400812 public void holdConference(final String confID) throws RemoteException {
813 getExecutor().execute(new SipRunnable() {
814 @Override
815 protected void doRun() throws SameThreadException, RemoteException {
816 Log.i(TAG, "SipService.holdConference() thread running...");
817 callManagerJNI.holdConference(confID);
818 }
819 });
820
alision7f18fc82013-05-01 09:37:33 -0400821 }
822
823 @Override
alision43a9b362013-05-01 16:30:15 -0400824 public void unholdConference(final String confID) throws RemoteException {
825 getExecutor().execute(new SipRunnable() {
826 @Override
827 protected void doRun() throws SameThreadException, RemoteException {
828 Log.i(TAG, "SipService.unholdConference() thread running...");
829 callManagerJNI.unholdConference(confID);
830 }
831 });
832
alision7f18fc82013-05-01 09:37:33 -0400833 }
Alexandre Lision67817192013-07-18 12:04:30 -0400834
alisioncd8fb912013-06-28 14:43:51 -0400835 @Override
836 public boolean isConferenceParticipant(final String callID) throws RemoteException {
837 class IsParticipant extends SipRunnableWithReturn {
838
839 @Override
840 protected Boolean doRun() throws SameThreadException {
841 Log.i(TAG, "SipService.isRecording() thread running...");
842 return callManagerJNI.isConferenceParticipant(callID);
843 }
844 }
845
846 IsParticipant runInstance = new IsParticipant();
847 getExecutor().execute(runInstance);
848 while (!runInstance.isDone()) {
849 }
850
851 return (Boolean) runInstance.getVal();
852 }
alision7f18fc82013-05-01 09:37:33 -0400853
854 @Override
alisiondf1dac92013-06-27 17:35:53 -0400855 public HashMap<String, Conference> getConferenceList() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -0400856 // class ConfList extends SipRunnableWithReturn {
857 // @Override
858 // protected StringVect doRun() throws SameThreadException {
859 // Log.i(TAG, "SipService.getConferenceList() thread running...");
860 // return callManagerJNI.getConferenceList();
861 // }
862 // }
863 // ;
864 // ConfList runInstance = new ConfList();
865 // getExecutor().execute(runInstance);
866 // while (!runInstance.isDone()) {
867 // // Log.w(TAG, "Waiting for getConferenceList");
868 // }
869 // StringVect swigvect = (StringVect) runInstance.getVal();
870 //
871 // ArrayList<String> nativelist = new ArrayList<String>();
872 //
873 // for (int i = 0; i < swigvect.size(); i++)
874 // nativelist.add(swigvect.get(i));
875 //
876 // return nativelist;
alisiondf1dac92013-06-27 17:35:53 -0400877 return current_confs;
alision7f18fc82013-05-01 09:37:33 -0400878 }
879
880 @Override
alision907bde72013-06-20 14:40:37 -0400881 public List getParticipantList(final String confID) throws RemoteException {
882 class PartList extends SipRunnableWithReturn {
883 @Override
884 protected StringVect doRun() throws SameThreadException {
885 Log.i(TAG, "SipService.getAccountList() thread running...");
886 return callManagerJNI.getParticipantList(confID);
887 }
888 }
889 ;
890 PartList runInstance = new PartList();
891 getExecutor().execute(runInstance);
892 while (!runInstance.isDone()) {
893 // Log.w(TAG, "Waiting for getConferenceList");
894 }
895 StringVect swigvect = (StringVect) runInstance.getVal();
896
897 ArrayList<String> nativelist = new ArrayList<String>();
898
899 for (int i = 0; i < swigvect.size(); i++)
900 nativelist.add(swigvect.get(i));
901
902 return nativelist;
alision7f18fc82013-05-01 09:37:33 -0400903 }
alision806e18e2013-06-21 15:30:17 -0400904
alision1005ba12013-06-19 13:52:44 -0400905 @Override
alision7f18fc82013-05-01 09:37:33 -0400906 public String getConferenceId(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400907 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400908 return null;
909 }
910
911 @Override
alision806e18e2013-06-21 15:30:17 -0400912 public String getConferenceDetails(final String callID) throws RemoteException {
913 class ConfDetails extends SipRunnableWithReturn {
914 @Override
915 protected StringMap doRun() throws SameThreadException {
916 Log.i(TAG, "SipService.getAccountList() thread running...");
917 return callManagerJNI.getConferenceDetails(callID);
918 }
919 }
920 ;
921 ConfDetails runInstance = new ConfDetails();
922 getExecutor().execute(runInstance);
923 while (!runInstance.isDone()) {
924 // Log.w(TAG, "Waiting for getConferenceList");
925 }
926 StringMap swigvect = (StringMap) runInstance.getVal();
927
928 return swigvect.get("CONF_STATE");
alision7f18fc82013-05-01 09:37:33 -0400929 }
930
alision04a00182013-05-10 17:05:29 -0400931 @Override
932 public String getRecordPath() throws RemoteException {
933 class RecordPath extends SipRunnableWithReturn {
934
935 @Override
936 protected String doRun() throws SameThreadException {
937 Log.i(TAG, "SipService.getRecordPath() thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +1000938 return configurationManagerJNI.getRecordPath();
alision04a00182013-05-10 17:05:29 -0400939 }
940 }
941
942 RecordPath runInstance = new RecordPath();
943 getExecutor().execute(runInstance);
944 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400945 // Log.w(TAG, "Waiting for getRecordPath");
alision04a00182013-05-10 17:05:29 -0400946 }
947 String path = (String) runInstance.getVal();
948
949 return path;
950 }
951
952 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400953 public boolean toggleRecordingCall(final String id) throws RemoteException {
Adrien Béraud9360f242013-09-19 11:07:42 +1000954
Alexandre Lisiona764c682013-09-09 10:02:07 -0400955 class ToggleRecording extends SipRunnableWithReturn {
956
alision04a00182013-05-10 17:05:29 -0400957 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400958 protected Boolean doRun() throws SameThreadException {
959 Log.i(TAG, "SipService.toggleRecordingCall() thread running...");
Alexandre Lision3874e552013-11-04 17:20:12 -0500960 boolean result = callManagerJNI.toggleRecording(id);
961
962 if(getCurrent_calls().containsKey(id)){
963 getCurrent_calls().get(id).setRecording(result);
964 } else if(getCurrent_confs().containsKey(id)){
965 getCurrent_confs().get(id).setRecording(result);
966 } else {
967 // A call in a conference has been put on hold
968 Iterator<Conference> it = getCurrent_confs().values().iterator();
969 while (it.hasNext()) {
970 Conference c = it.next();
971 if (c.getCall(id) != null)
972 c.getCall(id).setRecording(result);
973 }
974 }
975 return result;
alision04a00182013-05-10 17:05:29 -0400976 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400977 }
978
979 ToggleRecording runInstance = new ToggleRecording();
980 getExecutor().execute(runInstance);
981 while (!runInstance.isDone()) {
982 }
983
984 return (Boolean) runInstance.getVal();
alision04a00182013-05-10 17:05:29 -0400985
986 }
Alexandre Lision67817192013-07-18 12:04:30 -0400987
alision50fa0722013-06-25 17:29:44 -0400988 @Override
989 public boolean startRecordedFilePlayback(final String filepath) throws RemoteException {
990 getExecutor().execute(new SipRunnable() {
991 @Override
992 protected void doRun() throws SameThreadException, RemoteException {
993 Log.i(TAG, "SipService.setRecordingCall() thread running...");
994 callManagerJNI.startRecordedFilePlayback(filepath);
995 }
996 });
997 return false;
998 }
999
1000 @Override
1001 public void stopRecordedFilePlayback(final String filepath) throws RemoteException {
1002 getExecutor().execute(new SipRunnable() {
1003 @Override
1004 protected void doRun() throws SameThreadException, RemoteException {
1005 Log.i(TAG, "SipService.stopRecordedFilePlayback() thread running...");
1006 callManagerJNI.stopRecordedFilePlayback(filepath);
1007 }
1008 });
1009 }
alision04a00182013-05-10 17:05:29 -04001010
1011 @Override
1012 public void setRecordPath(final String path) throws RemoteException {
1013 getExecutor().execute(new SipRunnable() {
1014 @Override
1015 protected void doRun() throws SameThreadException, RemoteException {
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001016 Log.i(TAG, "SipService.setRecordPath() " + path + " thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +10001017 configurationManagerJNI.setRecordPath(path);
alision04a00182013-05-10 17:05:29 -04001018 }
1019 });
1020 }
1021
1022 @Override
Alexandre Lisiond5686032013-10-29 11:09:21 -04001023 public void sendTextMessage(final String callID, final SipMessage message) throws RemoteException {
alision04a00182013-05-10 17:05:29 -04001024 getExecutor().execute(new SipRunnable() {
1025 @Override
1026 protected void doRun() throws SameThreadException, RemoteException {
1027 Log.i(TAG, "SipService.sendTextMessage() thread running...");
Alexandre Lisiond5686032013-10-29 11:09:21 -04001028 callManagerJNI.sendTextMessage(callID, message.comment);
Alexandre Lisiond5545232013-10-29 11:24:02 -04001029 if(getCurrent_calls().get(callID) != null)
1030 getCurrent_calls().get(callID).addSipMessage(message);
1031 else if(getCurrent_confs().get(callID) != null)
1032 getCurrent_confs().get(callID).addSipMessage(message);
alision04a00182013-05-10 17:05:29 -04001033 }
1034 });
1035
1036 }
1037
alisiond295ec22013-05-17 10:12:13 -04001038 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -04001039 public List getAudioCodecList(final String accountID) throws RemoteException {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001040 class AudioCodecList extends SipRunnableWithReturn {
1041
1042 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001043 protected ArrayList<Codec> doRun() throws SameThreadException {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001044 Log.i(TAG, "SipService.getAudioCodecList() thread running...");
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001045 ArrayList<Codec> results = new ArrayList<Codec>();
Alexandre Lision4cf78702013-10-16 13:43:23 -04001046
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001047 IntVect active_payloads = configurationManagerJNI.getActiveAudioCodecList(accountID);
1048 for (int i = 0; i < active_payloads.size(); ++i) {
1049
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001050 results.add(new Codec(active_payloads.get(i), configurationManagerJNI.getAudioCodecDetails(active_payloads.get(i)), true));
Alexandre Lisione0045442013-10-25 09:16:19 -04001051
Alexandre Lision4cf78702013-10-16 13:43:23 -04001052 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001053
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001054 // if (results.get(active_payloads.get(i)) != null) {
1055 // results.get(active_payloads.get(i)).setEnabled(true);
Alexandre Lision451f2a82013-11-12 12:55:55 -05001056
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001057 IntVect payloads = configurationManagerJNI.getAudioCodecList();
1058
1059 for (int i = 0; i < payloads.size(); ++i) {
1060 boolean isActive = false;
1061 for (Codec co : results) {
1062 if (co.getPayload().toString().contentEquals(String.valueOf(payloads.get(i))))
1063 isActive = true;
1064
1065 }
1066 if (isActive)
1067 continue;
1068 else
1069 results.add(new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i)), false));
1070
1071 }
1072
1073 // if (!results.containsKey(payloads.get(i))) {
1074 // results.put(payloads.get(i), new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i)), false));
1075 // Log.i(TAG, "Other, Adding:" + results.get((payloads.get(i))).getName());
1076 // }
1077
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001078 return results;
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001079 }
1080 }
1081
1082 AudioCodecList runInstance = new AudioCodecList();
1083 getExecutor().execute(runInstance);
1084 while (!runInstance.isDone()) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001085 }
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001086 ArrayList<Codec> codecs = (ArrayList<Codec>) runInstance.getVal();
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001087 return codecs;
alisiond295ec22013-05-17 10:12:13 -04001088 }
1089
alision9f7a6ec2013-05-24 16:26:26 -04001090 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -04001091 public Map getRingtoneList() throws RemoteException {
1092 class RingtoneList extends SipRunnableWithReturn {
1093
1094 @Override
1095 protected StringMap doRun() throws SameThreadException {
1096 Log.i(TAG, "SipService.getRingtoneList() thread running...");
1097 return configurationManagerJNI.getRingtoneList();
1098 }
1099 }
1100
1101 RingtoneList runInstance = new RingtoneList();
1102 getExecutor().execute(runInstance);
1103 while (!runInstance.isDone()) {
1104 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001105 StringMap ringtones = (StringMap) runInstance.getVal();
1106
1107 for (int i = 0; i < ringtones.size(); ++i) {
1108 // Log.i(TAG,"ringtones "+i+" "+ ringtones.);
1109 }
1110
Alexandre Lision4cf78702013-10-16 13:43:23 -04001111 return null;
1112 }
1113
1114 @Override
1115 public void setActiveCodecList(final List codecs, final String accountID) throws RemoteException {
1116 getExecutor().execute(new SipRunnable() {
1117 @Override
1118 protected void doRun() throws SameThreadException, RemoteException {
1119 Log.i(TAG, "SipService.setActiveAudioCodecList() thread running...");
1120 StringVect list = new StringVect();
1121 for (int i = 0; i < codecs.size(); ++i) {
1122 list.add((String) codecs.get(i));
1123 }
1124 configurationManagerJNI.setActiveAudioCodecList(list, accountID);
1125 }
1126 });
1127 }
1128
1129 @Override
alisionfde875f2013-05-28 17:01:54 -04001130 public HashMap<String, SipCall> getCallList() throws RemoteException {
alision85704182013-05-29 15:23:03 -04001131 // class CallList extends SipRunnableWithReturn {
1132 //
1133 // @Override
1134 // protected StringVect doRun() throws SameThreadException {
1135 // Log.i(TAG, "SipService.getCallList() thread running...");
1136 // return callManagerJNI.getCallList();
1137 // }
1138 // }
1139 //
1140 // CallList runInstance = new CallList();
1141 // getExecutor().execute(runInstance);
1142 // while (!runInstance.isDone()) {
1143 // Log.w(TAG, "Waiting for getAudioCodecList");
1144 // }
1145 // StringVect swigmap = (StringVect) runInstance.getVal();
1146 //
1147 // ArrayList<String> nativemap = new ArrayList<String>();
1148 // for (int i = 0; i < swigmap.size(); ++i) {
1149 //
1150 // String t = swigmap.get(i);
1151 // nativemap.add(t);
1152 // }
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001153 // if(callManagerJNI == null)
1154 // return new HashMap<String, SipCall>();
1155 //
1156 //
1157 // HashMap<String, SipCall> results = new HashMap<String, SipCall>();
1158 // StringVect calls = callManagerJNI.getCallList();
1159 // for(int i = 0 ; i < calls.size(); ++i){
1160 // results.put(calls.get(i), new SipCall(calls.get(i), callManagerJNI.getCallDetails(calls.get(i))));
1161 // }
alision9f7a6ec2013-05-24 16:26:26 -04001162
alision2cb99562013-05-30 17:02:20 -04001163 return getCurrent_calls();
alision9f7a6ec2013-05-24 16:26:26 -04001164 }
1165
alision85704182013-05-29 15:23:03 -04001166 @Override
1167 public SipCall getCall(String callID) throws RemoteException {
alision2cb99562013-05-30 17:02:20 -04001168 return getCurrent_calls().get(callID);
1169 }
1170
1171 /***********************
1172 * Notification API
1173 ***********************/
1174 @Override
1175 public void createNotification() throws RemoteException {
1176 makeNotification();
alisioncc7bb422013-06-06 15:31:39 -04001177
alision2cb99562013-05-30 17:02:20 -04001178 }
1179
1180 @Override
1181 public void destroyNotification() throws RemoteException {
1182 removeNotification();
alisioncc7bb422013-06-06 15:31:39 -04001183
alision2cb99562013-05-30 17:02:20 -04001184 }
alisioncc7bb422013-06-06 15:31:39 -04001185
Adrien Béraud9360f242013-09-19 11:07:42 +10001186 private final int NOTIFICATION_ID = new Random().nextInt(1000);
alisioncc7bb422013-06-06 15:31:39 -04001187
alision2cb99562013-05-30 17:02:20 -04001188 private void makeNotification() {
alisioncc7bb422013-06-06 15:31:39 -04001189 if (current_calls.size() == 0) {
alision2cb99562013-05-30 17:02:20 -04001190 return;
1191 }
Alexandre Lision4ab53972013-11-04 16:59:18 -05001192 Intent notificationIntent = new Intent(getApplicationContext(), HomeActivity.class);
alisioncc7bb422013-06-06 15:31:39 -04001193 PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 007, notificationIntent,
1194 PendingIntent.FLAG_UPDATE_CURRENT);
alision2cb99562013-05-30 17:02:20 -04001195
1196 NotificationManager nm = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
Adrien Béraud9360f242013-09-19 11:07:42 +10001197 nm.cancel(NOTIFICATION_ID); // clear previous notifications.
alision2cb99562013-05-30 17:02:20 -04001198
1199 NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
alisioncc7bb422013-06-06 15:31:39 -04001200 //
1201 // builder.setContent(view);
1202 builder.setContentIntent(contentIntent).setOngoing(true).setSmallIcon(R.drawable.ic_launcher)
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001203 .setContentTitle(getCurrent_calls().size() + " ongoing calls").setTicker("Pending calls").setWhen(System.currentTimeMillis())
1204 .setAutoCancel(false);
alision2cb99562013-05-30 17:02:20 -04001205 builder.setPriority(NotificationCompat.PRIORITY_MAX);
1206 Notification n = builder.build();
1207
1208 nm.notify(NOTIFICATION_ID, n);
alision2cb99562013-05-30 17:02:20 -04001209 }
1210
1211 public void removeNotification() {
1212 NotificationManager nm = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
1213 nm.cancel(NOTIFICATION_ID);
alision85704182013-05-29 15:23:03 -04001214 }
1215
alisiondf1dac92013-06-27 17:35:53 -04001216 @Override
1217 public Conference getCurrentCall() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -04001218 for (SipCall i : current_calls.values()) {
Adrien Béraud9360f242013-09-19 11:07:42 +10001219
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001220 // Incoming >> Ongoing
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001221 if (i.isIncoming()) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001222 Conference tmp = new Conference("-1");
1223 tmp.getParticipants().add(i);
1224 return tmp;
1225 }
Adrien Béraud9360f242013-09-19 11:07:42 +10001226
Alexandre Lision67817192013-07-18 12:04:30 -04001227 if (i.isOngoing()) {
alisiondf1dac92013-06-27 17:35:53 -04001228 Conference tmp = new Conference("-1");
1229 tmp.getParticipants().add(i);
1230 return tmp;
1231 }
1232 }
Alexandre Lision67817192013-07-18 12:04:30 -04001233
1234 if (!current_confs.isEmpty()) {
alisiondf1dac92013-06-27 17:35:53 -04001235 return (Conference) current_confs.values().toArray()[0];
1236 }
1237 return null;
1238 }
1239
Alexandre Lision64dc8c02013-09-25 15:32:25 -04001240 @Override
1241 public void playDtmf(final String key) throws RemoteException {
1242 getExecutor().execute(new SipRunnable() {
1243 @Override
1244 protected void doRun() throws SameThreadException, RemoteException {
1245 Log.i(TAG, "SipService.playDtmf() thread running...");
1246 callManagerJNI.playDTMF(key);
1247 }
1248 });
1249 }
1250
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001251 @Override
1252 public List getConcurrentCalls() throws RemoteException {
1253 ArrayList<Conference> toReturn = new ArrayList<Conference>();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001254
1255 for (SipCall sip : current_calls.values()) {
1256 if (!sip.isCurrent()) {
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001257 Conference tmp = new Conference("-1");
1258 tmp.getParticipants().add(sip);
1259 toReturn.add(tmp);
1260 }
1261 }
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001262
1263 Log.i(TAG, "toReturn SIZE " + toReturn.size());
1264
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001265 return toReturn;
1266 }
1267
Alexandre Lision4fb22622013-10-21 16:26:33 -04001268 @Override
1269 public String getCurrentAudioCodecName(String callID) throws RemoteException {
1270 return callManagerJNI.getCurrentAudioCodecName(callID);
1271 }
1272
Alexandre Lision31e30902013-11-08 15:16:59 -05001273 @Override
1274 public void setMuted(final boolean mute) throws RemoteException {
1275 getExecutor().execute(new SipRunnable() {
1276 @Override
1277 protected void doRun() throws SameThreadException, RemoteException {
1278 Log.i(TAG, "SipService.setMuted() thread running...");
1279 configurationManagerJNI.muteCapture(mute);
1280 }
1281 });
1282 }
1283
1284 @Override
1285 public boolean isCaptureMuted() throws RemoteException {
1286 class IsMuted extends SipRunnableWithReturn {
1287
1288 @Override
1289 protected Boolean doRun() throws SameThreadException {
1290 Log.i(TAG, "SipService.isCaptureMuted() thread running...");
1291 return configurationManagerJNI.isCaptureMuted();
1292 }
1293 }
1294
1295 IsMuted runInstance = new IsMuted();
1296 getExecutor().execute(runInstance);
1297 while (!runInstance.isDone()) {
1298 }
1299
1300 return (Boolean) runInstance.getVal();
1301 }
1302
Alexandre Lision3b7148e2013-11-13 17:23:06 -05001303 @Override
1304 public List getCredentials(final String accountID) throws RemoteException {
1305 class Credentials extends SipRunnableWithReturn {
1306
1307 @Override
1308 protected List doRun() throws SameThreadException {
1309 Log.i(TAG, "SipService.getCredentials() thread running...");
1310 VectMap map = configurationManagerJNI.getCredentials(accountID);
1311 ArrayList<HashMap<String, String>> result = AccountDetailsHandler.convertCredentialsToNative(map);
1312 return result;
1313 }
1314 }
1315
1316 Credentials runInstance = new Credentials();
1317 getExecutor().execute(runInstance);
1318 while (!runInstance.isDone()) {
1319 }
1320 return (List) runInstance.getVal();
1321 }
1322
1323 @Override
1324 public void setCredentials(final String accountID, final List creds) throws RemoteException {
1325 getExecutor().execute(new SipRunnable() {
1326 @Override
1327 protected void doRun() throws SameThreadException, RemoteException {
1328 Log.i(TAG, "SipService.setCredentials() thread running...");
1329 configurationManagerJNI.setCredentials(accountID, AccountDetailsHandler.convertCredentialsToSwig(creds));;
1330 }
1331 });
1332 }
1333
Alexandre Lision451f2a82013-11-12 12:55:55 -05001334
1335
Emeric Vigier6119d782012-09-21 18:04:14 -04001336 };
Alexandre Lision63870a72013-10-28 16:33:47 -04001337}