blob: 58b1783657d6d00b025565a339ed01c57509083f [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 Lision064e1e02013-10-01 16:18:42 -040037import org.sflphone.account.AccountDetailBasic;
38import org.sflphone.account.AccountDetailsHandler;
39import org.sflphone.account.CallDetailsHandler;
40import org.sflphone.account.HistoryHandler;
41import org.sflphone.client.SFLPhoneHomeActivity;
Alexandre Lision933ef0a2013-10-15 17:28:40 -040042import org.sflphone.model.Codec;
Alexandre Lision064e1e02013-10-01 16:18:42 -040043import org.sflphone.model.Conference;
44import org.sflphone.model.SipCall;
45import org.sflphone.receivers.IncomingReceiver;
46
alision2cb99562013-05-30 17:02:20 -040047import android.app.Notification;
48import android.app.NotificationManager;
49import android.app.PendingIntent;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040050import android.app.Service;
alision17052d42013-04-22 10:39:38 -040051import android.content.Context;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040052import android.content.Intent;
alision17052d42013-04-22 10:39:38 -040053import android.content.IntentFilter;
alision7f18fc82013-05-01 09:37:33 -040054import android.os.Bundle;
Emeric Vigier6119d782012-09-21 18:04:14 -040055import android.os.Handler;
56import android.os.HandlerThread;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040057import android.os.IBinder;
Emeric Vigier6119d782012-09-21 18:04:14 -040058import android.os.Looper;
59import android.os.Message;
alision5f899632013-04-22 17:26:56 -040060import android.os.RemoteException;
alision2cb99562013-05-30 17:02:20 -040061import android.support.v4.app.NotificationCompat;
alision17052d42013-04-22 10:39:38 -040062import android.support.v4.content.LocalBroadcastManager;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040063import android.util.Log;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040064
Emeric Vigiereaf2c492012-09-19 14:38:20 -040065public class SipService extends Service {
66
67 static final String TAG = "SipService";
68 static final int DELAY = 5000; /* 5 sec */
69 private boolean runFlag = false;
70 private SipServiceThread sipServiceThread;
Emeric Vigier6119d782012-09-21 18:04:14 -040071 private SipServiceExecutor mExecutor;
72 private static HandlerThread executorThread;
alision3ea8f3c2013-07-16 17:35:35 -040073 private CallManager callManagerJNI;
Alexandre Lision67817192013-07-18 12:04:30 -040074 private ManagerImpl managerImpl;
Emeric Vigier0007dee2012-09-24 11:35:58 -040075 private CallManagerCallBack callManagerCallBack;
alision3ea8f3c2013-07-16 17:35:35 -040076 private ConfigurationManager configurationManagerJNI;
Alexandre Savardfccd1dc2012-10-17 17:31:38 -040077 private ConfigurationManagerCallback configurationManagerCallback;
Emeric Vigier6119d782012-09-21 18:04:14 -040078 private boolean isPjSipStackStarted = false;
alisioncc7bb422013-06-06 15:31:39 -040079
alision2cb99562013-05-30 17:02:20 -040080 public static final String NOTIF_CREATION = "notif_creation";
81 public static final String NOTIF_DELETION = "notif_deletion";
alision04a00182013-05-10 17:05:29 -040082
alision2cb99562013-05-30 17:02:20 -040083 private HashMap<String, SipCall> current_calls = new HashMap<String, SipCall>();
alision806e18e2013-06-21 15:30:17 -040084 private HashMap<String, Conference> current_confs = new HashMap<String, Conference>();
85 private IncomingReceiver receiver;
Emeric Vigier6119d782012-09-21 18:04:14 -040086
alision806e18e2013-06-21 15:30:17 -040087 public HashMap<String, Conference> getCurrent_confs() {
88 return current_confs;
89 }
alision43a9b362013-05-01 16:30:15 -040090
91 @Override
92 public boolean onUnbind(Intent i) {
93 super.onUnbind(i);
94 Log.i(TAG, "onUnbind(intent)");
95 return false;
96
97 }
98
99 /* called once by startService() */
100 @Override
101 public void onCreate() {
102 Log.i(TAG, "onCreated");
103 super.onCreate();
104
alision43a9b362013-05-01 16:30:15 -0400105 sipServiceThread = new SipServiceThread();
106
107 IntentFilter callFilter = new IntentFilter(CallManagerCallBack.CALL_STATE_CHANGED);
108 callFilter.addAction(CallManagerCallBack.INCOMING_CALL);
109 callFilter.addAction(CallManagerCallBack.NEW_CALL_CREATED);
alision4a0eb092013-05-07 13:52:03 -0400110 callFilter.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
111 callFilter.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
alision04a00182013-05-10 17:05:29 -0400112 callFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
alision806e18e2013-06-21 15:30:17 -0400113 callFilter.addAction(CallManagerCallBack.CONF_CREATED);
114 callFilter.addAction(CallManagerCallBack.CONF_REMOVED);
115 callFilter.addAction(CallManagerCallBack.CONF_CHANGED);
alisiondf1dac92013-06-27 17:35:53 -0400116 callFilter.addAction(CallManagerCallBack.RECORD_STATE_CHANGED);
alision806e18e2013-06-21 15:30:17 -0400117 receiver = new IncomingReceiver(this, mBinder);
alision2cb99562013-05-30 17:02:20 -0400118 LocalBroadcastManager.getInstance(this).registerReceiver(receiver, callFilter);
alision2cb99562013-05-30 17:02:20 -0400119
alisioncc7bb422013-06-06 15:31:39 -0400120 getExecutor().execute(new StartRunnable());
alisioncc7bb422013-06-06 15:31:39 -0400121 }
alision43a9b362013-05-01 16:30:15 -0400122
123 /* called for each startService() */
124 @Override
125 public int onStartCommand(Intent intent, int flags, int startId) {
126 Log.i(TAG, "onStarted");
127 super.onStartCommand(intent, flags, startId);
128
alision806e18e2013-06-21 15:30:17 -0400129 receiver = new IncomingReceiver(this, mBinder);
alision43a9b362013-05-01 16:30:15 -0400130 if (!runFlag) {
131 sipServiceThread.start();
132 runFlag = true;
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400133 // sflphoneApp.setServiceRunning(true);
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400134 Log.i(TAG, "Sflphone Service started");
alision43a9b362013-05-01 16:30:15 -0400135 }
136
alision907bde72013-06-20 14:40:37 -0400137 return START_NOT_STICKY; /* started and stopped explicitly */
alision43a9b362013-05-01 16:30:15 -0400138 }
139
140 @Override
141 public void onDestroy() {
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400142 Log.i(TAG, "onDestroyed");
alision43a9b362013-05-01 16:30:15 -0400143 /* called once by stopService() */
144 sipServiceThread.interrupt();
145 sipServiceThread = null;
146 runFlag = false;
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400147
148 LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
alision2cb99562013-05-30 17:02:20 -0400149
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400150 // sflphoneApp.setServiceRunning(false);
151 // Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show();
alision43a9b362013-05-01 16:30:15 -0400152 super.onDestroy();
153
alision43a9b362013-05-01 16:30:15 -0400154 }
155
156 @Override
157 public IBinder onBind(Intent arg0) {
158 Log.i(TAG, "onBound");
159 return mBinder;
160 }
161
162 private static Looper createLooper() {
163 if (executorThread == null) {
164 Log.d(TAG, "Creating new handler thread");
165 // ADT gives a fake warning due to bad parse rule.
166 executorThread = new HandlerThread("SipService.Executor");
167 executorThread.start();
168 }
169 return executorThread.getLooper();
170 }
171
172 public SipServiceExecutor getExecutor() {
173 // create mExecutor lazily
174 if (mExecutor == null) {
175 mExecutor = new SipServiceExecutor(this);
176 }
177 return mExecutor;
178 }
179
180 // Executes immediate tasks in a single executorThread.
181 public static class SipServiceExecutor extends Handler {
182 WeakReference<SipService> handlerService;
183
184 SipServiceExecutor(SipService s) {
185 super(createLooper());
186 handlerService = new WeakReference<SipService>(s);
187 }
188
189 public void execute(Runnable task) {
190 // TODO: add wakelock
191 Message.obtain(this, 0/* don't care */, task).sendToTarget();
Alexandre Lisioncdec5952013-07-17 14:18:22 -0400192 Log.w(TAG, "SenT!");
alision43a9b362013-05-01 16:30:15 -0400193 }
194
195 @Override
196 public void handleMessage(Message msg) {
197 if (msg.obj instanceof Runnable) {
198 executeInternal((Runnable) msg.obj);
199 } else {
200 Log.w(TAG, "can't handle msg: " + msg);
201 }
202 }
203
204 private void executeInternal(Runnable task) {
205 try {
206 task.run();
207 } catch (Throwable t) {
208 Log.e(TAG, "run task: " + task, t);
209 }
210 }
211 }
212
213 private void startPjSipStack() throws SameThreadException {
214 if (isPjSipStackStarted)
215 return;
216
217 try {
218 System.loadLibrary("gnustl_shared");
219 System.loadLibrary("expat");
220 System.loadLibrary("yaml");
221 System.loadLibrary("ccgnu2");
222 System.loadLibrary("crypto");
223 System.loadLibrary("ssl");
Alexandre Lision7c6f4a62013-09-05 13:27:01 -0400224 System.loadLibrary("sndfile");
alision43a9b362013-05-01 16:30:15 -0400225 System.loadLibrary("ccrtp1");
alision43a9b362013-05-01 16:30:15 -0400226 System.loadLibrary("samplerate");
227 System.loadLibrary("codec_ulaw");
228 System.loadLibrary("codec_alaw");
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400229 System.loadLibrary("codec_g722");
Alexandre Lision6deda412013-09-25 13:21:22 -0400230 System.loadLibrary("codec_opus");
Alexandre Lisiona1ad1c32013-10-15 16:35:20 -0400231 System.loadLibrary("codec_gsm");
Alexandre Lision62138172013-10-17 11:52:45 -0400232 System.loadLibrary("codec_speex_nb");
233 System.loadLibrary("codec_speex_ub");
234 System.loadLibrary("codec_speex_wb");
alision43a9b362013-05-01 16:30:15 -0400235 System.loadLibrary("speexresampler");
236 System.loadLibrary("sflphone");
237 isPjSipStackStarted = true;
Adrien Béraud9360f242013-09-19 11:07:42 +1000238
alision43a9b362013-05-01 16:30:15 -0400239 } catch (UnsatisfiedLinkError e) {
240 Log.e(TAG, "Problem with the current Pj stack...", e);
241 isPjSipStackStarted = false;
242 return;
243 } catch (Exception e) {
244 Log.e(TAG, "Problem with the current Pj stack...", e);
245 }
246
Alexandre Lision67817192013-07-18 12:04:30 -0400247 Log.i(TAG, "PjSIPStack started");
248 managerImpl = SFLPhoneservice.instance();
249
250 /* set static AppPath before calling manager.init */
251
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400252 // managerImpl.setPath(sflphoneApp.getAppPath());
alision43a9b362013-05-01 16:30:15 -0400253
alision3ea8f3c2013-07-16 17:35:35 -0400254 callManagerJNI = new CallManager();
alision43a9b362013-05-01 16:30:15 -0400255 callManagerCallBack = new CallManagerCallBack(this);
256 SFLPhoneservice.setCallbackObject(callManagerCallBack);
257
alision3ea8f3c2013-07-16 17:35:35 -0400258 configurationManagerJNI = new ConfigurationManager();
alision43a9b362013-05-01 16:30:15 -0400259 configurationManagerCallback = new ConfigurationManagerCallback(this);
260 SFLPhoneservice.setConfigurationCallbackObject(configurationManagerCallback);
Adrien Béraud9360f242013-09-19 11:07:42 +1000261
Alexandre Lision67817192013-07-18 12:04:30 -0400262 Log.i(TAG, "before init");
263 managerImpl.init("");
Adrien Béraud9360f242013-09-19 11:07:42 +1000264
Alexandre Lision67817192013-07-18 12:04:30 -0400265 Log.i(TAG, "->startPjSipStack");
alision43a9b362013-05-01 16:30:15 -0400266
alision43a9b362013-05-01 16:30:15 -0400267 }
268
Adrien Béraud9360f242013-09-19 11:07:42 +1000269 public HashMap<String, SipCall> getCurrent_calls() {
alision2cb99562013-05-30 17:02:20 -0400270 return current_calls;
271 }
272
alision43a9b362013-05-01 16:30:15 -0400273 // Enforce same thread contract to ensure we do not call from somewhere else
274 public class SameThreadException extends Exception {
275 private static final long serialVersionUID = -905639124232613768L;
276
277 public SameThreadException() {
278 super("Should be launched from a single worker thread");
279 }
280 }
281
282 public abstract static class SipRunnable implements Runnable {
283 protected abstract void doRun() throws SameThreadException, RemoteException;
284
Adrien Béraud9360f242013-09-19 11:07:42 +1000285 @Override
alision43a9b362013-05-01 16:30:15 -0400286 public void run() {
287 try {
288 doRun();
289 } catch (SameThreadException e) {
290 Log.e(TAG, "Not done from same thread");
291 } catch (RemoteException e) {
292 Log.e(TAG, e.toString());
293 }
294 }
295 }
296
297 public abstract static class SipRunnableWithReturn implements Runnable {
298 Object obj = null;
299 boolean done = false;
300
301 protected abstract Object doRun() throws SameThreadException;
302
303 public Object getVal() {
304 return obj;
305 }
306
307 public boolean isDone() {
308 return done;
309 }
310
Adrien Béraud9360f242013-09-19 11:07:42 +1000311 @Override
alision43a9b362013-05-01 16:30:15 -0400312 public void run() {
313 try {
314 obj = doRun();
315 done = true;
316 } catch (SameThreadException e) {
317 Log.e(TAG, "Not done from same thread");
318 }
319 }
320 }
321
322 class StartRunnable extends SipRunnable {
323 @Override
324 protected void doRun() throws SameThreadException {
325 startPjSipStack();
326 }
327 }
328
329 private class SipServiceThread extends Thread {
330
331 public SipServiceThread() {
332 super("sipServiceThread");
333 }
334
335 @Override
336 public void run() {
337 Log.i(TAG, "SipService thread running...");
338 SipService sipService = SipService.this;
339 while (sipService.runFlag) {
340 try {
341 Thread.sleep(DELAY);
342 } catch (InterruptedException e) {
343 sipService.runFlag = false;
344 Log.w(TAG, "service thread interrupted!");
345 }
346 }
347 }
348 }
349
350 /* ************************************
351 *
352 * Implement public interface for the service
353 *
Alexandre Lision67817192013-07-18 12:04:30 -0400354 * *********************************
355 */
356
Emeric Vigier6119d782012-09-21 18:04:14 -0400357 private final ISipService.Stub mBinder = new ISipService.Stub() {
358
359 @Override
alisionfde875f2013-05-28 17:01:54 -0400360 public void placeCall(final SipCall call) {
Emeric Vigier6119d782012-09-21 18:04:14 -0400361 getExecutor().execute(new SipRunnable() {
362 @Override
363 protected void doRun() throws SameThreadException {
364 Log.i(TAG, "SipService.placeCall() thread running...");
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400365 callManagerJNI.placeCall(call.getAccount().getAccountID(), call.getCallId(), call.getContact().getPhones().get(0).getNumber());
Adrien Béraud9360f242013-09-19 11:07:42 +1000366
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400367 HashMap<String, String> details = CallDetailsHandler.convertSwigToNative(callManagerJNI.getCallDetails(call.getCallId()));
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400368 // watchout timestamp stored by sflphone is in seconds
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400369 call.setTimestamp_start(Long.parseLong(details.get(ServiceConstants.call.TIMESTAMP_START)));
alision2cb99562013-05-30 17:02:20 -0400370 getCurrent_calls().put(call.getCallId(), call);
Adrien Béraud9360f242013-09-19 11:07:42 +1000371
Emeric Vigier6119d782012-09-21 18:04:14 -0400372 }
373 });
374 }
375
376 @Override
377 public void refuse(final String callID) {
378 getExecutor().execute(new SipRunnable() {
379 @Override
380 protected void doRun() throws SameThreadException {
381 Log.i(TAG, "SipService.refuse() thread running...");
382 callManagerJNI.refuse(callID);
383 }
384 });
385 }
386
387 @Override
388 public void accept(final String callID) {
389 getExecutor().execute(new SipRunnable() {
390 @Override
391 protected void doRun() throws SameThreadException {
Tristan Matthews40cf25e2013-07-24 13:45:15 -0400392 Log.i(TAG, "SipService.accept() thread running...");
Emeric Vigier6119d782012-09-21 18:04:14 -0400393 callManagerJNI.accept(callID);
394 }
395 });
396 }
397
398 @Override
399 public void hangUp(final String callID) {
400 getExecutor().execute(new SipRunnable() {
401 @Override
402 protected void doRun() throws SameThreadException {
403 Log.i(TAG, "SipService.hangUp() thread running...");
404 callManagerJNI.hangUp(callID);
405 }
406 });
407 }
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400408
409 @Override
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400410 public void hold(final String callID) {
411 getExecutor().execute(new SipRunnable() {
412 @Override
413 protected void doRun() throws SameThreadException {
414 Log.i(TAG, "SipService.hold() thread running...");
415 callManagerJNI.hold(callID);
416 }
417 });
418 }
419
420 @Override
421 public void unhold(final String callID) {
422 getExecutor().execute(new SipRunnable() {
423 @Override
424 protected void doRun() throws SameThreadException {
425 Log.i(TAG, "SipService.unhold() thread running...");
426 callManagerJNI.unhold(callID);
427 }
428 });
429 }
Adrien Béraud9360f242013-09-19 11:07:42 +1000430
Alexandre Lision6711ab22013-09-16 15:15:38 -0400431 @Override
432 public HashMap<String, String> getCallDetails(String callID) throws RemoteException {
433 class CallDetails extends SipRunnableWithReturn {
434 private String id;
435
436 CallDetails(String callID) {
437 id = callID;
438 }
439
440 @Override
441 protected StringMap doRun() throws SameThreadException {
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400442 Log.i(TAG, "SipService.getCallDetails() thread running...");
Alexandre Lision6711ab22013-09-16 15:15:38 -0400443 return callManagerJNI.getCallDetails(id);
444 }
445 }
446
447 CallDetails runInstance = new CallDetails(callID);
448 getExecutor().execute(runInstance);
449
450 while (!runInstance.isDone()) {
451 }
452 StringMap swigmap = (StringMap) runInstance.getVal();
453
454 HashMap<String, String> nativemap = CallDetailsHandler.convertSwigToNative(swigmap);
455
456 return nativemap;
Adrien Béraud9360f242013-09-19 11:07:42 +1000457
Alexandre Lision6711ab22013-09-16 15:15:38 -0400458 }
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400459
460 @Override
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400461 public void setAudioPlugin(final String audioPlugin) {
462 getExecutor().execute(new SipRunnable() {
alision371b77e2013-04-23 14:51:26 -0400463 @Override
464 protected void doRun() throws SameThreadException {
465 Log.i(TAG, "SipService.setAudioPlugin() thread running...");
466 configurationManagerJNI.setAudioPlugin(audioPlugin);
467 }
Alexandre Savard31d27c62012-10-04 16:05:08 -0400468 });
469 }
470
471 @Override
472 public String getCurrentAudioOutputPlugin() {
473 class CurrentAudioPlugin extends SipRunnableWithReturn {
474 @Override
475 protected String doRun() throws SameThreadException {
476 Log.i(TAG, "SipService.getCurrentAudioOutputPlugin() thread running...");
477 return configurationManagerJNI.getCurrentAudioOutputPlugin();
478 }
alision371b77e2013-04-23 14:51:26 -0400479 }
480 ;
Alexandre Savard31d27c62012-10-04 16:05:08 -0400481
482 CurrentAudioPlugin runInstance = new CurrentAudioPlugin();
483 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400484 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400485 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400486 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400487 return (String) runInstance.getVal();
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400488 }
Alexandre Savard713a34d2012-09-26 15:50:41 -0400489
490 @Override
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400491 public ArrayList<String> getAccountList() {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400492 class AccountList extends SipRunnableWithReturn {
493 @Override
494 protected StringVect doRun() throws SameThreadException {
495 Log.i(TAG, "SipService.getAccountList() thread running...");
496 return configurationManagerJNI.getAccountList();
497 }
alision371b77e2013-04-23 14:51:26 -0400498 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400499 AccountList runInstance = new AccountList();
Alexandre Lisioncdec5952013-07-17 14:18:22 -0400500 Log.i(TAG, "SipService.getAccountList() thread running...");
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400501 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400502 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400503 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400504 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400505 StringVect swigvect = (StringVect) runInstance.getVal();
506
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400507 ArrayList<String> nativelist = new ArrayList<String>();
Alexandre Savard52a72522012-09-27 16:40:13 -0400508
alision371b77e2013-04-23 14:51:26 -0400509 for (int i = 0; i < swigvect.size(); i++)
510 nativelist.add(swigvect.get(i));
Alexandre Savard52a72522012-09-27 16:40:13 -0400511
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400512 return nativelist;
alision371b77e2013-04-23 14:51:26 -0400513 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -0400514
Alexandre Lision4cf78702013-10-16 13:43:23 -0400515 @Override
Alexandre Lision4b4233a2013-10-16 17:24:17 -0400516 public void setAccountOrder(final String order) {
Alexandre Lision4cf78702013-10-16 13:43:23 -0400517 getExecutor().execute(new SipRunnable() {
518 @Override
519 protected void doRun() throws SameThreadException {
520 Log.i(TAG, "SipService.setAccountsOrder() thread running...");
521 configurationManagerJNI.setAccountsOrder(order);
522 }
523 });
524 }
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400525
526 @Override
alision371b77e2013-04-23 14:51:26 -0400527 public HashMap<String, String> getAccountDetails(final String accountID) {
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400528 class AccountDetails extends SipRunnableWithReturn {
529 private String id;
alision371b77e2013-04-23 14:51:26 -0400530
531 AccountDetails(String accountId) {
532 id = accountId;
533 }
534
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400535 @Override
536 protected StringMap doRun() throws SameThreadException {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400537 Log.i(TAG, "SipService.getAccountDetails() thread running...");
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400538 return configurationManagerJNI.getAccountDetails(id);
539 }
alision371b77e2013-04-23 14:51:26 -0400540 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400541
542 AccountDetails runInstance = new AccountDetails(accountID);
543 getExecutor().execute(runInstance);
alisionfde875f2013-05-28 17:01:54 -0400544
alision371b77e2013-04-23 14:51:26 -0400545 while (!runInstance.isDone()) {
546 }
547 StringMap swigmap = (StringMap) runInstance.getVal();
Alexandre Savard713a34d2012-09-26 15:50:41 -0400548
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400549 HashMap<String, String> nativemap = AccountDetailsHandler.convertSwigToNative(swigmap);
Alexandre Savard713a34d2012-09-26 15:50:41 -0400550
551 return nativemap;
552 }
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400553
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400554 @SuppressWarnings("unchecked")
555 // Hashmap runtime cast
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400556 @Override
alisioncc7bb422013-06-06 15:31:39 -0400557 public void setAccountDetails(final String accountId, final Map map) {
alision371b77e2013-04-23 14:51:26 -0400558 HashMap<String, String> nativemap = (HashMap<String, String>) map;
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400559
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400560 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig(nativemap);
Alexandre Savard718d49f2012-10-02 15:17:13 -0400561
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400562 getExecutor().execute(new SipRunnable() {
563 @Override
564 protected void doRun() throws SameThreadException {
alisioncc7bb422013-06-06 15:31:39 -0400565
566 configurationManagerJNI.setCredentials(accountId, extractCredentials(map));
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400567 configurationManagerJNI.setAccountDetails(accountId, swigmap);
alisioncc7bb422013-06-06 15:31:39 -0400568
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400569 // convertSwigToNative(configurationManagerJNI.getCredentials(accountId));
alisioncc7bb422013-06-06 15:31:39 -0400570 Log.i(TAG, "SipService.setAccountDetails() thread running...");
571 }
572
573 private VectMap extractCredentials(Map map) {
574 VectMap swigmap = new VectMap();
575 StringMap entry = new StringMap();
alision5cfc35d2013-07-11 15:11:39 -0400576 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME));
577 if ((String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_REALM) != null)
578 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_REALM, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_REALM));
alisioncc7bb422013-06-06 15:31:39 -0400579 else
alision5cfc35d2013-07-11 15:11:39 -0400580 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_REALM, "*");
581 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD));
alisioncc7bb422013-06-06 15:31:39 -0400582 swigmap.add(entry);
583 return swigmap;
584
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400585 }
586 });
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400587 }
588
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400589 // public ArrayList<HashMap<String, String>> convertSwigToNative(VectMap swigmap) {
590 //
591 // ArrayList<HashMap<String, String>> nativemap = new ArrayList<HashMap<String, String>>();
592 // Log.i(TAG, "swigmap size " + swigmap.size());
593 // for (int i = 0; i < swigmap.size(); ++i) {
594 // Log.i(TAG, "Entry " + i);
595 // StringMap tmp = swigmap.get(i);
596 // Log.i(TAG, tmp.get(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME));
597 // // Log.i(TAG, tmp.get(ServiceConstants.CONFIG_ACCOUNT_REALM));
598 // Log.i(TAG, tmp.get(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD));
599 // }
600 //
601 // return nativemap;
602 // }
alisioncc7bb422013-06-06 15:31:39 -0400603
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400604 @SuppressWarnings("unchecked")
605 // Hashmap runtime cast
Alexandre Savard46036572012-10-05 13:56:49 -0400606 @Override
607 public String addAccount(Map map) {
608 class AddAccount extends SipRunnableWithReturn {
609 StringMap map;
alision371b77e2013-04-23 14:51:26 -0400610
611 AddAccount(StringMap m) {
612 map = m;
613 }
614
Alexandre Savard46036572012-10-05 13:56:49 -0400615 @Override
616 protected String doRun() throws SameThreadException {
617 Log.i(TAG, "SipService.getAccountDetails() thread running...");
618 return configurationManagerJNI.addAccount(map);
619 }
alision371b77e2013-04-23 14:51:26 -0400620 }
Alexandre Savard46036572012-10-05 13:56:49 -0400621
alision371b77e2013-04-23 14:51:26 -0400622 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig((HashMap<String, String>) map);
Alexandre Savard46036572012-10-05 13:56:49 -0400623
624 AddAccount runInstance = new AddAccount(swigmap);
625 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400626 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400627 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400628 }
Alexandre Savard46036572012-10-05 13:56:49 -0400629 String accountId = (String) runInstance.getVal();
630
631 return accountId;
632 }
633
634 @Override
635 public void removeAccount(final String accountId) {
636 getExecutor().execute(new SipRunnable() {
637 @Override
638 protected void doRun() throws SameThreadException {
639 Log.i(TAG, "SipService.setAccountDetails() thread running...");
640 configurationManagerJNI.removeAccount(accountId);
641 }
642 });
643 }
alision5f899632013-04-22 17:26:56 -0400644
alisione2a38e12013-04-25 14:20:20 -0400645 @Override
646 public ArrayList<HashMap<String, String>> getHistory() throws RemoteException {
647 class History extends SipRunnableWithReturn {
648
649 @Override
650 protected VectMap doRun() throws SameThreadException {
651 Log.i(TAG, "SipService.getHistory() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400652
alisione2a38e12013-04-25 14:20:20 -0400653 return configurationManagerJNI.getHistory();
654 }
655 }
656
657 History runInstance = new History();
658 getExecutor().execute(runInstance);
659 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400660 // Log.w(TAG, "Waiting for getHistory");
alisione2a38e12013-04-25 14:20:20 -0400661 }
alision1005ba12013-06-19 13:52:44 -0400662 Log.i(TAG, "SipService.getHistory() DONE");
alisione2a38e12013-04-25 14:20:20 -0400663 VectMap swigmap = (VectMap) runInstance.getVal();
664
665 ArrayList<HashMap<String, String>> nativemap = HistoryHandler.convertSwigToNative(swigmap);
666
667 return nativemap;
668 }
alision7f18fc82013-05-01 09:37:33 -0400669
alision43a9b362013-05-01 16:30:15 -0400670 /*************************
671 * Transfer related API
672 *************************/
673
alision7f18fc82013-05-01 09:37:33 -0400674 @Override
675 public void transfer(final String callID, final String to) throws RemoteException {
676 getExecutor().execute(new SipRunnable() {
677 @Override
678 protected void doRun() throws SameThreadException, RemoteException {
679 Log.i(TAG, "SipService.transfer() thread running...");
680 if (callManagerJNI.transfer(callID, to)) {
681 Bundle bundle = new Bundle();
682 bundle.putString("CallID", callID);
683 bundle.putString("State", "HUNGUP");
684 Intent intent = new Intent(CallManagerCallBack.CALL_STATE_CHANGED);
alision7f18fc82013-05-01 09:37:33 -0400685 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
alision84813a12013-05-27 17:40:39 -0400686 sendBroadcast(intent);
alision7f18fc82013-05-01 09:37:33 -0400687 } else
688 Log.i(TAG, "NOT OK");
689 }
690 });
691
692 }
alision43a9b362013-05-01 16:30:15 -0400693
alision7f18fc82013-05-01 09:37:33 -0400694 @Override
695 public void attendedTransfer(final String transferID, final String targetID) throws RemoteException {
696 getExecutor().execute(new SipRunnable() {
697 @Override
698 protected void doRun() throws SameThreadException, RemoteException {
alision43a9b362013-05-01 16:30:15 -0400699 Log.i(TAG, "SipService.attendedTransfer() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400700 if (callManagerJNI.attendedTransfer(transferID, targetID)) {
701 Log.i(TAG, "OK");
alision7f18fc82013-05-01 09:37:33 -0400702 } else
703 Log.i(TAG, "NOT OK");
704 }
705 });
alision43a9b362013-05-01 16:30:15 -0400706
707 }
708
709 /*************************
710 * Conference related API
711 *************************/
712
713 @Override
714 public void removeConference(final String confID) throws RemoteException {
715 getExecutor().execute(new SipRunnable() {
716 @Override
717 protected void doRun() throws SameThreadException, RemoteException {
718 Log.i(TAG, "SipService.createConference() thread running...");
719 callManagerJNI.removeConference(confID);
720 }
721 });
722
alision7f18fc82013-05-01 09:37:33 -0400723 }
724
725 @Override
alision43a9b362013-05-01 16:30:15 -0400726 public void joinParticipant(final String sel_callID, final String drag_callID) throws RemoteException {
727 getExecutor().execute(new SipRunnable() {
728 @Override
729 protected void doRun() throws SameThreadException, RemoteException {
730 Log.i(TAG, "SipService.joinParticipant() thread running...");
731 callManagerJNI.joinParticipant(sel_callID, drag_callID);
alision806e18e2013-06-21 15:30:17 -0400732 // Generate a CONF_CREATED callback
alision43a9b362013-05-01 16:30:15 -0400733 }
734 });
735
alision7f18fc82013-05-01 09:37:33 -0400736 }
737
alision7f18fc82013-05-01 09:37:33 -0400738 @Override
alision806e18e2013-06-21 15:30:17 -0400739 public void addParticipant(final SipCall call, final String confID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400740 getExecutor().execute(new SipRunnable() {
741 @Override
742 protected void doRun() throws SameThreadException, RemoteException {
743 Log.i(TAG, "SipService.addParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400744 callManagerJNI.addParticipant(call.getCallId(), confID);
745 current_confs.get(confID).getParticipants().add(call);
alision43a9b362013-05-01 16:30:15 -0400746 }
747 });
748
alision7f18fc82013-05-01 09:37:33 -0400749 }
750
751 @Override
alision43a9b362013-05-01 16:30:15 -0400752 public void addMainParticipant(final String confID) throws RemoteException {
753 getExecutor().execute(new SipRunnable() {
754 @Override
755 protected void doRun() throws SameThreadException, RemoteException {
756 Log.i(TAG, "SipService.addMainParticipant() thread running...");
757 callManagerJNI.addMainParticipant(confID);
758 }
759 });
760
alision7f18fc82013-05-01 09:37:33 -0400761 }
762
763 @Override
alision43a9b362013-05-01 16:30:15 -0400764 public void detachParticipant(final String callID) throws RemoteException {
765 getExecutor().execute(new SipRunnable() {
766 @Override
767 protected void doRun() throws SameThreadException, RemoteException {
768 Log.i(TAG, "SipService.detachParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400769 Log.i(TAG, "Detaching " + callID);
770 Iterator<Entry<String, Conference>> it = current_confs.entrySet().iterator();
771 Log.i(TAG, "current_confs size " + current_confs.size());
772 while (it.hasNext()) {
773 Conference tmp = it.next().getValue();
774 Log.i(TAG, "conf has " + tmp.getParticipants().size() + " participants");
775 if (tmp.contains(callID)) {
776 current_calls.put(callID, tmp.getCall(callID));
777 Log.i(TAG, "Call found and put in current_calls");
778 }
779 }
alision43a9b362013-05-01 16:30:15 -0400780 callManagerJNI.detachParticipant(callID);
781 }
782 });
783
alision7f18fc82013-05-01 09:37:33 -0400784 }
785
786 @Override
alision43a9b362013-05-01 16:30:15 -0400787 public void joinConference(final String sel_confID, final String drag_confID) throws RemoteException {
788 getExecutor().execute(new SipRunnable() {
789 @Override
790 protected void doRun() throws SameThreadException, RemoteException {
791 Log.i(TAG, "SipService.joinConference() thread running...");
792 callManagerJNI.joinConference(sel_confID, drag_confID);
793 }
794 });
795
alision7f18fc82013-05-01 09:37:33 -0400796 }
797
798 @Override
alision43a9b362013-05-01 16:30:15 -0400799 public void hangUpConference(final String confID) throws RemoteException {
800 getExecutor().execute(new SipRunnable() {
801 @Override
802 protected void doRun() throws SameThreadException, RemoteException {
803 Log.i(TAG, "SipService.joinConference() thread running...");
804 callManagerJNI.hangUpConference(confID);
805 }
806 });
807
alision7f18fc82013-05-01 09:37:33 -0400808 }
809
810 @Override
alision43a9b362013-05-01 16:30:15 -0400811 public void holdConference(final String confID) throws RemoteException {
812 getExecutor().execute(new SipRunnable() {
813 @Override
814 protected void doRun() throws SameThreadException, RemoteException {
815 Log.i(TAG, "SipService.holdConference() thread running...");
816 callManagerJNI.holdConference(confID);
817 }
818 });
819
alision7f18fc82013-05-01 09:37:33 -0400820 }
821
822 @Override
alision43a9b362013-05-01 16:30:15 -0400823 public void unholdConference(final String confID) throws RemoteException {
824 getExecutor().execute(new SipRunnable() {
825 @Override
826 protected void doRun() throws SameThreadException, RemoteException {
827 Log.i(TAG, "SipService.unholdConference() thread running...");
828 callManagerJNI.unholdConference(confID);
829 }
830 });
831
alision7f18fc82013-05-01 09:37:33 -0400832 }
Alexandre Lision67817192013-07-18 12:04:30 -0400833
alisioncd8fb912013-06-28 14:43:51 -0400834 @Override
835 public boolean isConferenceParticipant(final String callID) throws RemoteException {
836 class IsParticipant extends SipRunnableWithReturn {
837
838 @Override
839 protected Boolean doRun() throws SameThreadException {
840 Log.i(TAG, "SipService.isRecording() thread running...");
841 return callManagerJNI.isConferenceParticipant(callID);
842 }
843 }
844
845 IsParticipant runInstance = new IsParticipant();
846 getExecutor().execute(runInstance);
847 while (!runInstance.isDone()) {
848 }
849
850 return (Boolean) runInstance.getVal();
851 }
alision7f18fc82013-05-01 09:37:33 -0400852
853 @Override
alisiondf1dac92013-06-27 17:35:53 -0400854 public HashMap<String, Conference> getConferenceList() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -0400855 // class ConfList extends SipRunnableWithReturn {
856 // @Override
857 // protected StringVect doRun() throws SameThreadException {
858 // Log.i(TAG, "SipService.getConferenceList() thread running...");
859 // return callManagerJNI.getConferenceList();
860 // }
861 // }
862 // ;
863 // ConfList runInstance = new ConfList();
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;
alisiondf1dac92013-06-27 17:35:53 -0400876 return current_confs;
alision7f18fc82013-05-01 09:37:33 -0400877 }
878
879 @Override
alision907bde72013-06-20 14:40:37 -0400880 public List getParticipantList(final String confID) throws RemoteException {
881 class PartList extends SipRunnableWithReturn {
882 @Override
883 protected StringVect doRun() throws SameThreadException {
884 Log.i(TAG, "SipService.getAccountList() thread running...");
885 return callManagerJNI.getParticipantList(confID);
886 }
887 }
888 ;
889 PartList runInstance = new PartList();
890 getExecutor().execute(runInstance);
891 while (!runInstance.isDone()) {
892 // Log.w(TAG, "Waiting for getConferenceList");
893 }
894 StringVect swigvect = (StringVect) runInstance.getVal();
895
896 ArrayList<String> nativelist = new ArrayList<String>();
897
898 for (int i = 0; i < swigvect.size(); i++)
899 nativelist.add(swigvect.get(i));
900
901 return nativelist;
alision7f18fc82013-05-01 09:37:33 -0400902 }
alision806e18e2013-06-21 15:30:17 -0400903
alision1005ba12013-06-19 13:52:44 -0400904 @Override
alision7f18fc82013-05-01 09:37:33 -0400905 public String getConferenceId(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400906 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400907 return null;
908 }
909
910 @Override
alision806e18e2013-06-21 15:30:17 -0400911 public String getConferenceDetails(final String callID) throws RemoteException {
912 class ConfDetails extends SipRunnableWithReturn {
913 @Override
914 protected StringMap doRun() throws SameThreadException {
915 Log.i(TAG, "SipService.getAccountList() thread running...");
916 return callManagerJNI.getConferenceDetails(callID);
917 }
918 }
919 ;
920 ConfDetails runInstance = new ConfDetails();
921 getExecutor().execute(runInstance);
922 while (!runInstance.isDone()) {
923 // Log.w(TAG, "Waiting for getConferenceList");
924 }
925 StringMap swigvect = (StringMap) runInstance.getVal();
926
927 return swigvect.get("CONF_STATE");
alision7f18fc82013-05-01 09:37:33 -0400928 }
929
alision04a00182013-05-10 17:05:29 -0400930 @Override
931 public String getRecordPath() throws RemoteException {
932 class RecordPath extends SipRunnableWithReturn {
933
934 @Override
935 protected String doRun() throws SameThreadException {
936 Log.i(TAG, "SipService.getRecordPath() thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +1000937 return configurationManagerJNI.getRecordPath();
alision04a00182013-05-10 17:05:29 -0400938 }
939 }
940
941 RecordPath runInstance = new RecordPath();
942 getExecutor().execute(runInstance);
943 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400944 // Log.w(TAG, "Waiting for getRecordPath");
alision04a00182013-05-10 17:05:29 -0400945 }
946 String path = (String) runInstance.getVal();
947
948 return path;
949 }
950
951 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400952 public boolean toggleRecordingCall(final String id) throws RemoteException {
Adrien Béraud9360f242013-09-19 11:07:42 +1000953
Alexandre Lisiona764c682013-09-09 10:02:07 -0400954 class ToggleRecording extends SipRunnableWithReturn {
955
alision04a00182013-05-10 17:05:29 -0400956 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400957 protected Boolean doRun() throws SameThreadException {
958 Log.i(TAG, "SipService.toggleRecordingCall() thread running...");
959 return callManagerJNI.toggleRecording(id);
alision04a00182013-05-10 17:05:29 -0400960 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400961 }
962
963 ToggleRecording runInstance = new ToggleRecording();
964 getExecutor().execute(runInstance);
965 while (!runInstance.isDone()) {
966 }
967
968 return (Boolean) runInstance.getVal();
alision04a00182013-05-10 17:05:29 -0400969
970 }
Alexandre Lision67817192013-07-18 12:04:30 -0400971
alision50fa0722013-06-25 17:29:44 -0400972 @Override
alisiondf1dac92013-06-27 17:35:53 -0400973 public boolean isRecording(final String id) throws RemoteException {
974 class IsRecording extends SipRunnableWithReturn {
975
976 @Override
977 protected Boolean doRun() throws SameThreadException {
978 Log.i(TAG, "SipService.isRecording() thread running...");
979 return callManagerJNI.getIsRecording(id);
980 }
981 }
982
983 IsRecording runInstance = new IsRecording();
984 getExecutor().execute(runInstance);
985 while (!runInstance.isDone()) {
986 }
987
988 return (Boolean) runInstance.getVal();
Alexandre Lision67817192013-07-18 12:04:30 -0400989
alisiondf1dac92013-06-27 17:35:53 -0400990 }
Alexandre Lision67817192013-07-18 12:04:30 -0400991
alisiondf1dac92013-06-27 17:35:53 -0400992 @Override
alision50fa0722013-06-25 17:29:44 -0400993 public boolean startRecordedFilePlayback(final String filepath) throws RemoteException {
994 getExecutor().execute(new SipRunnable() {
995 @Override
996 protected void doRun() throws SameThreadException, RemoteException {
997 Log.i(TAG, "SipService.setRecordingCall() thread running...");
998 callManagerJNI.startRecordedFilePlayback(filepath);
999 }
1000 });
1001 return false;
1002 }
1003
1004 @Override
1005 public void stopRecordedFilePlayback(final String filepath) throws RemoteException {
1006 getExecutor().execute(new SipRunnable() {
1007 @Override
1008 protected void doRun() throws SameThreadException, RemoteException {
1009 Log.i(TAG, "SipService.stopRecordedFilePlayback() thread running...");
1010 callManagerJNI.stopRecordedFilePlayback(filepath);
1011 }
1012 });
1013 }
alision04a00182013-05-10 17:05:29 -04001014
1015 @Override
1016 public void setRecordPath(final String path) throws RemoteException {
1017 getExecutor().execute(new SipRunnable() {
1018 @Override
1019 protected void doRun() throws SameThreadException, RemoteException {
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001020 Log.i(TAG, "SipService.setRecordPath() " + path + " thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +10001021 configurationManagerJNI.setRecordPath(path);
alision04a00182013-05-10 17:05:29 -04001022 }
1023 });
1024 }
1025
1026 @Override
1027 public void sendTextMessage(final String callID, final String message, final String from) throws RemoteException {
1028 getExecutor().execute(new SipRunnable() {
1029 @Override
1030 protected void doRun() throws SameThreadException, RemoteException {
1031 Log.i(TAG, "SipService.sendTextMessage() thread running...");
Alexandre Lision10314352013-07-17 15:06:56 -04001032 callManagerJNI.sendTextMessage(callID, 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));
1051
1052 if (results.get(i).getName().length() == 0)
1053 results.remove(i);
Alexandre Lision4cf78702013-10-16 13:43:23 -04001054 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001055
Alexandre Lision039a3cf2013-10-16 17:44:57 -04001056 // if (results.get(active_payloads.get(i)) != null) {
1057 // results.get(active_payloads.get(i)).setEnabled(true);
1058
1059 IntVect payloads = configurationManagerJNI.getAudioCodecList();
1060
1061 for (int i = 0; i < payloads.size(); ++i) {
1062 boolean isActive = false;
1063 for (Codec co : results) {
1064 if (co.getPayload().toString().contentEquals(String.valueOf(payloads.get(i))))
1065 isActive = true;
1066
1067 }
1068 if (isActive)
1069 continue;
1070 else
1071 results.add(new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i)), false));
1072
1073 }
1074
1075 // if (!results.containsKey(payloads.get(i))) {
1076 // results.put(payloads.get(i), new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i)), false));
1077 // Log.i(TAG, "Other, Adding:" + results.get((payloads.get(i))).getName());
1078 // }
1079
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001080 return results;
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001081 }
1082 }
1083
1084 AudioCodecList runInstance = new AudioCodecList();
1085 getExecutor().execute(runInstance);
1086 while (!runInstance.isDone()) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001087 }
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001088 ArrayList<Codec> codecs = (ArrayList<Codec>) runInstance.getVal();
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001089 return codecs;
alisiond295ec22013-05-17 10:12:13 -04001090 }
1091
alision9f7a6ec2013-05-24 16:26:26 -04001092 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -04001093 public Map getRingtoneList() throws RemoteException {
1094 class RingtoneList extends SipRunnableWithReturn {
1095
1096 @Override
1097 protected StringMap doRun() throws SameThreadException {
1098 Log.i(TAG, "SipService.getRingtoneList() thread running...");
1099 return configurationManagerJNI.getRingtoneList();
1100 }
1101 }
1102
1103 RingtoneList runInstance = new RingtoneList();
1104 getExecutor().execute(runInstance);
1105 while (!runInstance.isDone()) {
1106 }
Alexandre Lision4b4233a2013-10-16 17:24:17 -04001107 StringMap ringtones = (StringMap) runInstance.getVal();
1108
1109 for (int i = 0; i < ringtones.size(); ++i) {
1110 // Log.i(TAG,"ringtones "+i+" "+ ringtones.);
1111 }
1112
Alexandre Lision4cf78702013-10-16 13:43:23 -04001113 return null;
1114 }
1115
1116 @Override
1117 public void setActiveCodecList(final List codecs, final String accountID) throws RemoteException {
1118 getExecutor().execute(new SipRunnable() {
1119 @Override
1120 protected void doRun() throws SameThreadException, RemoteException {
1121 Log.i(TAG, "SipService.setActiveAudioCodecList() thread running...");
1122 StringVect list = new StringVect();
1123 for (int i = 0; i < codecs.size(); ++i) {
1124 list.add((String) codecs.get(i));
1125 }
1126 configurationManagerJNI.setActiveAudioCodecList(list, accountID);
1127 }
1128 });
1129 }
1130
1131 @Override
alisionfde875f2013-05-28 17:01:54 -04001132 public HashMap<String, SipCall> getCallList() throws RemoteException {
alision85704182013-05-29 15:23:03 -04001133 // class CallList extends SipRunnableWithReturn {
1134 //
1135 // @Override
1136 // protected StringVect doRun() throws SameThreadException {
1137 // Log.i(TAG, "SipService.getCallList() thread running...");
1138 // return callManagerJNI.getCallList();
1139 // }
1140 // }
1141 //
1142 // CallList runInstance = new CallList();
1143 // getExecutor().execute(runInstance);
1144 // while (!runInstance.isDone()) {
1145 // Log.w(TAG, "Waiting for getAudioCodecList");
1146 // }
1147 // StringVect swigmap = (StringVect) runInstance.getVal();
1148 //
1149 // ArrayList<String> nativemap = new ArrayList<String>();
1150 // for (int i = 0; i < swigmap.size(); ++i) {
1151 //
1152 // String t = swigmap.get(i);
1153 // nativemap.add(t);
1154 // }
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001155 // if(callManagerJNI == null)
1156 // return new HashMap<String, SipCall>();
1157 //
1158 //
1159 // HashMap<String, SipCall> results = new HashMap<String, SipCall>();
1160 // StringVect calls = callManagerJNI.getCallList();
1161 // for(int i = 0 ; i < calls.size(); ++i){
1162 // results.put(calls.get(i), new SipCall(calls.get(i), callManagerJNI.getCallDetails(calls.get(i))));
1163 // }
alision9f7a6ec2013-05-24 16:26:26 -04001164
alision2cb99562013-05-30 17:02:20 -04001165 return getCurrent_calls();
alision9f7a6ec2013-05-24 16:26:26 -04001166 }
1167
alision85704182013-05-29 15:23:03 -04001168 @Override
1169 public SipCall getCall(String callID) throws RemoteException {
alision2cb99562013-05-30 17:02:20 -04001170 return getCurrent_calls().get(callID);
1171 }
1172
1173 /***********************
1174 * Notification API
1175 ***********************/
1176 @Override
1177 public void createNotification() throws RemoteException {
1178 makeNotification();
alisioncc7bb422013-06-06 15:31:39 -04001179
alision2cb99562013-05-30 17:02:20 -04001180 }
1181
1182 @Override
1183 public void destroyNotification() throws RemoteException {
1184 removeNotification();
alisioncc7bb422013-06-06 15:31:39 -04001185
alision2cb99562013-05-30 17:02:20 -04001186 }
alisioncc7bb422013-06-06 15:31:39 -04001187
Adrien Béraud9360f242013-09-19 11:07:42 +10001188 private final int NOTIFICATION_ID = new Random().nextInt(1000);
alisioncc7bb422013-06-06 15:31:39 -04001189
alision2cb99562013-05-30 17:02:20 -04001190 private void makeNotification() {
alisioncc7bb422013-06-06 15:31:39 -04001191 if (current_calls.size() == 0) {
alision2cb99562013-05-30 17:02:20 -04001192 return;
1193 }
1194 Intent notificationIntent = new Intent(getApplicationContext(), SFLPhoneHomeActivity.class);
alisioncc7bb422013-06-06 15:31:39 -04001195 PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 007, notificationIntent,
1196 PendingIntent.FLAG_UPDATE_CURRENT);
alision2cb99562013-05-30 17:02:20 -04001197
1198 NotificationManager nm = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
Adrien Béraud9360f242013-09-19 11:07:42 +10001199 nm.cancel(NOTIFICATION_ID); // clear previous notifications.
alision2cb99562013-05-30 17:02:20 -04001200
1201 NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
alisioncc7bb422013-06-06 15:31:39 -04001202 //
1203 // builder.setContent(view);
1204 builder.setContentIntent(contentIntent).setOngoing(true).setSmallIcon(R.drawable.ic_launcher)
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001205 .setContentTitle(getCurrent_calls().size() + " ongoing calls").setTicker("Pending calls").setWhen(System.currentTimeMillis())
1206 .setAutoCancel(false);
alision2cb99562013-05-30 17:02:20 -04001207 builder.setPriority(NotificationCompat.PRIORITY_MAX);
1208 Notification n = builder.build();
1209
1210 nm.notify(NOTIFICATION_ID, n);
alision2cb99562013-05-30 17:02:20 -04001211 }
1212
1213 public void removeNotification() {
1214 NotificationManager nm = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
1215 nm.cancel(NOTIFICATION_ID);
alision85704182013-05-29 15:23:03 -04001216 }
1217
alisiondf1dac92013-06-27 17:35:53 -04001218 @Override
1219 public Conference getCurrentCall() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -04001220 for (SipCall i : current_calls.values()) {
Adrien Béraud9360f242013-09-19 11:07:42 +10001221
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001222 // Incoming >> Ongoing
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001223 if (i.isIncoming()) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001224 Conference tmp = new Conference("-1");
1225 tmp.getParticipants().add(i);
1226 return tmp;
1227 }
Adrien Béraud9360f242013-09-19 11:07:42 +10001228
Alexandre Lision67817192013-07-18 12:04:30 -04001229 if (i.isOngoing()) {
alisiondf1dac92013-06-27 17:35:53 -04001230 Conference tmp = new Conference("-1");
1231 tmp.getParticipants().add(i);
1232 return tmp;
1233 }
1234 }
Alexandre Lision67817192013-07-18 12:04:30 -04001235
1236 if (!current_confs.isEmpty()) {
alisiondf1dac92013-06-27 17:35:53 -04001237 return (Conference) current_confs.values().toArray()[0];
1238 }
1239 return null;
1240 }
1241
Alexandre Lision64dc8c02013-09-25 15:32:25 -04001242 @Override
1243 public void playDtmf(final String key) throws RemoteException {
1244 getExecutor().execute(new SipRunnable() {
1245 @Override
1246 protected void doRun() throws SameThreadException, RemoteException {
1247 Log.i(TAG, "SipService.playDtmf() thread running...");
1248 callManagerJNI.playDTMF(key);
1249 }
1250 });
1251 }
1252
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001253 @Override
1254 public List getConcurrentCalls() throws RemoteException {
1255 ArrayList<Conference> toReturn = new ArrayList<Conference>();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001256
1257 for (SipCall sip : current_calls.values()) {
1258 if (!sip.isCurrent()) {
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001259 Conference tmp = new Conference("-1");
1260 tmp.getParticipants().add(sip);
1261 toReturn.add(tmp);
1262 }
1263 }
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001264
1265 Log.i(TAG, "toReturn SIZE " + toReturn.size());
1266
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001267 return toReturn;
1268 }
1269
Emeric Vigier6119d782012-09-21 18:04:14 -04001270 };
Emeric Vigiereaf2c492012-09-19 14:38:20 -04001271}