blob: 003b29edb9277b4caf57a51e1123e84dc1f0574b [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;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040039import org.sflphone.account.AudioHandler;
Alexandre Lision064e1e02013-10-01 16:18:42 -040040import org.sflphone.account.CallDetailsHandler;
41import org.sflphone.account.HistoryHandler;
42import org.sflphone.client.SFLPhoneHomeActivity;
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;
46import org.sflphone.receivers.IncomingReceiver;
47
alision2cb99562013-05-30 17:02:20 -040048import android.app.Notification;
49import android.app.NotificationManager;
50import android.app.PendingIntent;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040051import android.app.Service;
alision17052d42013-04-22 10:39:38 -040052import android.content.Context;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040053import android.content.Intent;
alision17052d42013-04-22 10:39:38 -040054import android.content.IntentFilter;
alision7f18fc82013-05-01 09:37:33 -040055import android.os.Bundle;
Emeric Vigier6119d782012-09-21 18:04:14 -040056import android.os.Handler;
57import android.os.HandlerThread;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040058import android.os.IBinder;
Emeric Vigier6119d782012-09-21 18:04:14 -040059import android.os.Looper;
60import android.os.Message;
alision5f899632013-04-22 17:26:56 -040061import android.os.RemoteException;
alision2cb99562013-05-30 17:02:20 -040062import android.support.v4.app.NotificationCompat;
alision17052d42013-04-22 10:39:38 -040063import android.support.v4.content.LocalBroadcastManager;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040064import android.util.Log;
Alexandre Lision933ef0a2013-10-15 17:28:40 -040065import android.widget.Toast;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040066
Emeric Vigiereaf2c492012-09-19 14:38:20 -040067public class SipService extends Service {
68
69 static final String TAG = "SipService";
70 static final int DELAY = 5000; /* 5 sec */
71 private boolean runFlag = false;
72 private SipServiceThread sipServiceThread;
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
alision2cb99562013-05-30 17:02:20 -040082 public static final String NOTIF_CREATION = "notif_creation";
83 public static final String NOTIF_DELETION = "notif_deletion";
alision04a00182013-05-10 17:05:29 -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)");
97 return false;
98
99 }
100
101 /* called once by startService() */
102 @Override
103 public void onCreate() {
104 Log.i(TAG, "onCreated");
105 super.onCreate();
106
alision43a9b362013-05-01 16:30:15 -0400107 sipServiceThread = new SipServiceThread();
108
109 IntentFilter callFilter = new IntentFilter(CallManagerCallBack.CALL_STATE_CHANGED);
110 callFilter.addAction(CallManagerCallBack.INCOMING_CALL);
111 callFilter.addAction(CallManagerCallBack.NEW_CALL_CREATED);
alision4a0eb092013-05-07 13:52:03 -0400112 callFilter.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
113 callFilter.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
alision04a00182013-05-10 17:05:29 -0400114 callFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
alision806e18e2013-06-21 15:30:17 -0400115 callFilter.addAction(CallManagerCallBack.CONF_CREATED);
116 callFilter.addAction(CallManagerCallBack.CONF_REMOVED);
117 callFilter.addAction(CallManagerCallBack.CONF_CHANGED);
alisiondf1dac92013-06-27 17:35:53 -0400118 callFilter.addAction(CallManagerCallBack.RECORD_STATE_CHANGED);
alision806e18e2013-06-21 15:30:17 -0400119 receiver = new IncomingReceiver(this, mBinder);
alision2cb99562013-05-30 17:02:20 -0400120 LocalBroadcastManager.getInstance(this).registerReceiver(receiver, callFilter);
alision2cb99562013-05-30 17:02:20 -0400121
alisioncc7bb422013-06-06 15:31:39 -0400122 getExecutor().execute(new StartRunnable());
alisioncc7bb422013-06-06 15:31:39 -0400123 }
alision43a9b362013-05-01 16:30:15 -0400124
125 /* called for each startService() */
126 @Override
127 public int onStartCommand(Intent intent, int flags, int startId) {
128 Log.i(TAG, "onStarted");
129 super.onStartCommand(intent, flags, startId);
130
alision806e18e2013-06-21 15:30:17 -0400131 receiver = new IncomingReceiver(this, mBinder);
alision43a9b362013-05-01 16:30:15 -0400132 if (!runFlag) {
133 sipServiceThread.start();
134 runFlag = true;
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400135 // sflphoneApp.setServiceRunning(true);
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400136 Log.i(TAG, "Sflphone Service started");
alision43a9b362013-05-01 16:30:15 -0400137 }
138
alision907bde72013-06-20 14:40:37 -0400139 return START_NOT_STICKY; /* started and stopped explicitly */
alision43a9b362013-05-01 16:30:15 -0400140 }
141
142 @Override
143 public void onDestroy() {
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400144 Log.i(TAG, "onDestroyed");
alision43a9b362013-05-01 16:30:15 -0400145 /* called once by stopService() */
146 sipServiceThread.interrupt();
147 sipServiceThread = null;
148 runFlag = false;
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400149
150 LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
alision2cb99562013-05-30 17:02:20 -0400151
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400152 // sflphoneApp.setServiceRunning(false);
153 // Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show();
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 }
163
164 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 }
214
215 private void startPjSipStack() throws SameThreadException {
216 if (isPjSipStackStarted)
217 return;
218
219 try {
220 System.loadLibrary("gnustl_shared");
221 System.loadLibrary("expat");
222 System.loadLibrary("yaml");
223 System.loadLibrary("ccgnu2");
224 System.loadLibrary("crypto");
225 System.loadLibrary("ssl");
Alexandre Lision7c6f4a62013-09-05 13:27:01 -0400226 System.loadLibrary("sndfile");
alision43a9b362013-05-01 16:30:15 -0400227 System.loadLibrary("ccrtp1");
alision43a9b362013-05-01 16:30:15 -0400228 System.loadLibrary("samplerate");
229 System.loadLibrary("codec_ulaw");
230 System.loadLibrary("codec_alaw");
Alexandre Lision0a300ff2013-09-23 14:22:34 -0400231 System.loadLibrary("codec_g722");
Alexandre Lision6deda412013-09-25 13:21:22 -0400232 System.loadLibrary("codec_opus");
Alexandre Lisiona1ad1c32013-10-15 16:35:20 -0400233 System.loadLibrary("codec_gsm");
alision43a9b362013-05-01 16:30:15 -0400234 System.loadLibrary("speexresampler");
235 System.loadLibrary("sflphone");
236 isPjSipStackStarted = true;
Adrien Béraud9360f242013-09-19 11:07:42 +1000237
alision43a9b362013-05-01 16:30:15 -0400238 } catch (UnsatisfiedLinkError e) {
239 Log.e(TAG, "Problem with the current Pj stack...", e);
240 isPjSipStackStarted = false;
241 return;
242 } catch (Exception e) {
243 Log.e(TAG, "Problem with the current Pj stack...", e);
244 }
245
Alexandre Lision67817192013-07-18 12:04:30 -0400246 Log.i(TAG, "PjSIPStack started");
247 managerImpl = SFLPhoneservice.instance();
248
249 /* set static AppPath before calling manager.init */
250
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400251 // managerImpl.setPath(sflphoneApp.getAppPath());
alision43a9b362013-05-01 16:30:15 -0400252
alision3ea8f3c2013-07-16 17:35:35 -0400253 callManagerJNI = new CallManager();
alision43a9b362013-05-01 16:30:15 -0400254 callManagerCallBack = new CallManagerCallBack(this);
255 SFLPhoneservice.setCallbackObject(callManagerCallBack);
256
alision3ea8f3c2013-07-16 17:35:35 -0400257 configurationManagerJNI = new ConfigurationManager();
alision43a9b362013-05-01 16:30:15 -0400258 configurationManagerCallback = new ConfigurationManagerCallback(this);
259 SFLPhoneservice.setConfigurationCallbackObject(configurationManagerCallback);
Adrien Béraud9360f242013-09-19 11:07:42 +1000260
Alexandre Lision67817192013-07-18 12:04:30 -0400261 Log.i(TAG, "before init");
262 managerImpl.init("");
Adrien Béraud9360f242013-09-19 11:07:42 +1000263
Alexandre Lision67817192013-07-18 12:04:30 -0400264 Log.i(TAG, "->startPjSipStack");
alision43a9b362013-05-01 16:30:15 -0400265
alision43a9b362013-05-01 16:30:15 -0400266 }
267
Adrien Béraud9360f242013-09-19 11:07:42 +1000268 public HashMap<String, SipCall> getCurrent_calls() {
alision2cb99562013-05-30 17:02:20 -0400269 return current_calls;
270 }
271
alision43a9b362013-05-01 16:30:15 -0400272 // Enforce same thread contract to ensure we do not call from somewhere else
273 public class SameThreadException extends Exception {
274 private static final long serialVersionUID = -905639124232613768L;
275
276 public SameThreadException() {
277 super("Should be launched from a single worker thread");
278 }
279 }
280
281 public abstract static class SipRunnable implements Runnable {
282 protected abstract void doRun() throws SameThreadException, RemoteException;
283
Adrien Béraud9360f242013-09-19 11:07:42 +1000284 @Override
alision43a9b362013-05-01 16:30:15 -0400285 public void run() {
286 try {
287 doRun();
288 } catch (SameThreadException e) {
289 Log.e(TAG, "Not done from same thread");
290 } catch (RemoteException e) {
291 Log.e(TAG, e.toString());
292 }
293 }
294 }
295
296 public abstract static class SipRunnableWithReturn implements Runnable {
297 Object obj = null;
298 boolean done = false;
299
300 protected abstract Object doRun() throws SameThreadException;
301
302 public Object getVal() {
303 return obj;
304 }
305
306 public boolean isDone() {
307 return done;
308 }
309
Adrien Béraud9360f242013-09-19 11:07:42 +1000310 @Override
alision43a9b362013-05-01 16:30:15 -0400311 public void run() {
312 try {
313 obj = doRun();
314 done = true;
315 } catch (SameThreadException e) {
316 Log.e(TAG, "Not done from same thread");
317 }
318 }
319 }
320
321 class StartRunnable extends SipRunnable {
322 @Override
323 protected void doRun() throws SameThreadException {
324 startPjSipStack();
325 }
326 }
327
328 private class SipServiceThread extends Thread {
329
330 public SipServiceThread() {
331 super("sipServiceThread");
332 }
333
334 @Override
335 public void run() {
336 Log.i(TAG, "SipService thread running...");
337 SipService sipService = SipService.this;
338 while (sipService.runFlag) {
339 try {
340 Thread.sleep(DELAY);
341 } catch (InterruptedException e) {
342 sipService.runFlag = false;
343 Log.w(TAG, "service thread interrupted!");
344 }
345 }
346 }
347 }
348
349 /* ************************************
350 *
351 * Implement public interface for the service
352 *
Alexandre Lision67817192013-07-18 12:04:30 -0400353 * *********************************
354 */
355
Emeric Vigier6119d782012-09-21 18:04:14 -0400356 private final ISipService.Stub mBinder = new ISipService.Stub() {
357
358 @Override
alisionfde875f2013-05-28 17:01:54 -0400359 public void placeCall(final SipCall call) {
Emeric Vigier6119d782012-09-21 18:04:14 -0400360 getExecutor().execute(new SipRunnable() {
361 @Override
362 protected void doRun() throws SameThreadException {
363 Log.i(TAG, "SipService.placeCall() thread running...");
Alexandre Lision3ee516e2013-10-07 17:32:15 -0400364 callManagerJNI.placeCall(call.getAccount().getAccountID(), call.getCallId(), call.getContact().getPhones().get(0).getNumber());
Adrien Béraud9360f242013-09-19 11:07:42 +1000365
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400366 HashMap<String, String> details = CallDetailsHandler.convertSwigToNative(callManagerJNI.getCallDetails(call.getCallId()));
Alexandre Lisionebeb3662013-09-17 16:20:54 -0400367 // watchout timestamp stored by sflphone is in seconds
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400368 call.setTimestamp_start(Long.parseLong(details.get(ServiceConstants.call.TIMESTAMP_START)));
alision2cb99562013-05-30 17:02:20 -0400369 getCurrent_calls().put(call.getCallId(), call);
Adrien Béraud9360f242013-09-19 11:07:42 +1000370
Emeric Vigier6119d782012-09-21 18:04:14 -0400371 }
372 });
373 }
374
375 @Override
376 public void refuse(final String callID) {
377 getExecutor().execute(new SipRunnable() {
378 @Override
379 protected void doRun() throws SameThreadException {
380 Log.i(TAG, "SipService.refuse() thread running...");
381 callManagerJNI.refuse(callID);
382 }
383 });
384 }
385
386 @Override
387 public void accept(final String callID) {
388 getExecutor().execute(new SipRunnable() {
389 @Override
390 protected void doRun() throws SameThreadException {
Tristan Matthews40cf25e2013-07-24 13:45:15 -0400391 Log.i(TAG, "SipService.accept() thread running...");
Emeric Vigier6119d782012-09-21 18:04:14 -0400392 callManagerJNI.accept(callID);
393 }
394 });
395 }
396
397 @Override
398 public void hangUp(final String callID) {
399 getExecutor().execute(new SipRunnable() {
400 @Override
401 protected void doRun() throws SameThreadException {
402 Log.i(TAG, "SipService.hangUp() thread running...");
403 callManagerJNI.hangUp(callID);
404 }
405 });
406 }
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400407
408 @Override
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400409 public void hold(final String callID) {
410 getExecutor().execute(new SipRunnable() {
411 @Override
412 protected void doRun() throws SameThreadException {
413 Log.i(TAG, "SipService.hold() thread running...");
414 callManagerJNI.hold(callID);
415 }
416 });
417 }
418
419 @Override
420 public void unhold(final String callID) {
421 getExecutor().execute(new SipRunnable() {
422 @Override
423 protected void doRun() throws SameThreadException {
424 Log.i(TAG, "SipService.unhold() thread running...");
425 callManagerJNI.unhold(callID);
426 }
427 });
428 }
Adrien Béraud9360f242013-09-19 11:07:42 +1000429
Alexandre Lision6711ab22013-09-16 15:15:38 -0400430 @Override
431 public HashMap<String, String> getCallDetails(String callID) throws RemoteException {
432 class CallDetails extends SipRunnableWithReturn {
433 private String id;
434
435 CallDetails(String callID) {
436 id = callID;
437 }
438
439 @Override
440 protected StringMap doRun() throws SameThreadException {
Alexandre Lision3c6b7102013-09-16 16:56:46 -0400441 Log.i(TAG, "SipService.getCallDetails() thread running...");
Alexandre Lision6711ab22013-09-16 15:15:38 -0400442 return callManagerJNI.getCallDetails(id);
443 }
444 }
445
446 CallDetails runInstance = new CallDetails(callID);
447 getExecutor().execute(runInstance);
448
449 while (!runInstance.isDone()) {
450 }
451 StringMap swigmap = (StringMap) runInstance.getVal();
452
453 HashMap<String, String> nativemap = CallDetailsHandler.convertSwigToNative(swigmap);
454
455 return nativemap;
Adrien Béraud9360f242013-09-19 11:07:42 +1000456
Alexandre Lision6711ab22013-09-16 15:15:38 -0400457 }
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400458
459 @Override
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400460 public void setAudioPlugin(final String audioPlugin) {
461 getExecutor().execute(new SipRunnable() {
alision371b77e2013-04-23 14:51:26 -0400462 @Override
463 protected void doRun() throws SameThreadException {
464 Log.i(TAG, "SipService.setAudioPlugin() thread running...");
465 configurationManagerJNI.setAudioPlugin(audioPlugin);
466 }
Alexandre Savard31d27c62012-10-04 16:05:08 -0400467 });
468 }
469
470 @Override
471 public String getCurrentAudioOutputPlugin() {
472 class CurrentAudioPlugin extends SipRunnableWithReturn {
473 @Override
474 protected String doRun() throws SameThreadException {
475 Log.i(TAG, "SipService.getCurrentAudioOutputPlugin() thread running...");
476 return configurationManagerJNI.getCurrentAudioOutputPlugin();
477 }
alision371b77e2013-04-23 14:51:26 -0400478 }
479 ;
Alexandre Savard31d27c62012-10-04 16:05:08 -0400480
481 CurrentAudioPlugin runInstance = new CurrentAudioPlugin();
482 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400483 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400484 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400485 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400486 return (String) runInstance.getVal();
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400487 }
Alexandre Savard713a34d2012-09-26 15:50:41 -0400488
489 @Override
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400490 public ArrayList<String> getAccountList() {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400491 class AccountList extends SipRunnableWithReturn {
492 @Override
493 protected StringVect doRun() throws SameThreadException {
494 Log.i(TAG, "SipService.getAccountList() thread running...");
495 return configurationManagerJNI.getAccountList();
496 }
alision371b77e2013-04-23 14:51:26 -0400497 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400498 AccountList runInstance = new AccountList();
Alexandre Lisioncdec5952013-07-17 14:18:22 -0400499 Log.i(TAG, "SipService.getAccountList() thread running...");
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400500 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400501 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400502 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400503 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400504 StringVect swigvect = (StringVect) runInstance.getVal();
505
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400506 ArrayList<String> nativelist = new ArrayList<String>();
Alexandre Savard52a72522012-09-27 16:40:13 -0400507
alision371b77e2013-04-23 14:51:26 -0400508 for (int i = 0; i < swigvect.size(); i++)
509 nativelist.add(swigvect.get(i));
Alexandre Savard52a72522012-09-27 16:40:13 -0400510
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400511 return nativelist;
alision371b77e2013-04-23 14:51:26 -0400512 }
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400513
514 @Override
alision371b77e2013-04-23 14:51:26 -0400515 public HashMap<String, String> getAccountDetails(final String accountID) {
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400516 class AccountDetails extends SipRunnableWithReturn {
517 private String id;
alision371b77e2013-04-23 14:51:26 -0400518
519 AccountDetails(String accountId) {
520 id = accountId;
521 }
522
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400523 @Override
524 protected StringMap doRun() throws SameThreadException {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400525 Log.i(TAG, "SipService.getAccountDetails() thread running...");
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400526 return configurationManagerJNI.getAccountDetails(id);
527 }
alision371b77e2013-04-23 14:51:26 -0400528 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400529
530 AccountDetails runInstance = new AccountDetails(accountID);
531 getExecutor().execute(runInstance);
alisionfde875f2013-05-28 17:01:54 -0400532
alision371b77e2013-04-23 14:51:26 -0400533 while (!runInstance.isDone()) {
534 }
535 StringMap swigmap = (StringMap) runInstance.getVal();
Alexandre Savard713a34d2012-09-26 15:50:41 -0400536
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400537 HashMap<String, String> nativemap = AccountDetailsHandler.convertSwigToNative(swigmap);
Alexandre Savard713a34d2012-09-26 15:50:41 -0400538
539 return nativemap;
540 }
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400541
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400542 @SuppressWarnings("unchecked")
543 // Hashmap runtime cast
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400544 @Override
alisioncc7bb422013-06-06 15:31:39 -0400545 public void setAccountDetails(final String accountId, final Map map) {
alision371b77e2013-04-23 14:51:26 -0400546 HashMap<String, String> nativemap = (HashMap<String, String>) map;
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400547
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400548 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig(nativemap);
Alexandre Savard718d49f2012-10-02 15:17:13 -0400549
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400550 getExecutor().execute(new SipRunnable() {
551 @Override
552 protected void doRun() throws SameThreadException {
alisioncc7bb422013-06-06 15:31:39 -0400553
554 configurationManagerJNI.setCredentials(accountId, extractCredentials(map));
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400555 configurationManagerJNI.setAccountDetails(accountId, swigmap);
alisioncc7bb422013-06-06 15:31:39 -0400556
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400557 // convertSwigToNative(configurationManagerJNI.getCredentials(accountId));
alisioncc7bb422013-06-06 15:31:39 -0400558 Log.i(TAG, "SipService.setAccountDetails() thread running...");
559 }
560
561 private VectMap extractCredentials(Map map) {
562 VectMap swigmap = new VectMap();
563 StringMap entry = new StringMap();
alision5cfc35d2013-07-11 15:11:39 -0400564 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME));
565 if ((String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_REALM) != null)
566 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_REALM, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_REALM));
alisioncc7bb422013-06-06 15:31:39 -0400567 else
alision5cfc35d2013-07-11 15:11:39 -0400568 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_REALM, "*");
569 entry.set(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, (String) map.get(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD));
alisioncc7bb422013-06-06 15:31:39 -0400570 swigmap.add(entry);
571 return swigmap;
572
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400573 }
574 });
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400575 }
576
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400577 // public ArrayList<HashMap<String, String>> convertSwigToNative(VectMap swigmap) {
578 //
579 // ArrayList<HashMap<String, String>> nativemap = new ArrayList<HashMap<String, String>>();
580 // Log.i(TAG, "swigmap size " + swigmap.size());
581 // for (int i = 0; i < swigmap.size(); ++i) {
582 // Log.i(TAG, "Entry " + i);
583 // StringMap tmp = swigmap.get(i);
584 // Log.i(TAG, tmp.get(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME));
585 // // Log.i(TAG, tmp.get(ServiceConstants.CONFIG_ACCOUNT_REALM));
586 // Log.i(TAG, tmp.get(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD));
587 // }
588 //
589 // return nativemap;
590 // }
alisioncc7bb422013-06-06 15:31:39 -0400591
Alexandre Lisionb4a9e392013-10-01 14:39:43 -0400592 @SuppressWarnings("unchecked")
593 // Hashmap runtime cast
Alexandre Savard46036572012-10-05 13:56:49 -0400594 @Override
595 public String addAccount(Map map) {
596 class AddAccount extends SipRunnableWithReturn {
597 StringMap map;
alision371b77e2013-04-23 14:51:26 -0400598
599 AddAccount(StringMap m) {
600 map = m;
601 }
602
Alexandre Savard46036572012-10-05 13:56:49 -0400603 @Override
604 protected String doRun() throws SameThreadException {
605 Log.i(TAG, "SipService.getAccountDetails() thread running...");
606 return configurationManagerJNI.addAccount(map);
607 }
alision371b77e2013-04-23 14:51:26 -0400608 }
Alexandre Savard46036572012-10-05 13:56:49 -0400609
alision371b77e2013-04-23 14:51:26 -0400610 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig((HashMap<String, String>) map);
Alexandre Savard46036572012-10-05 13:56:49 -0400611
612 AddAccount runInstance = new AddAccount(swigmap);
613 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400614 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400615 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400616 }
Alexandre Savard46036572012-10-05 13:56:49 -0400617 String accountId = (String) runInstance.getVal();
618
619 return accountId;
620 }
621
622 @Override
623 public void removeAccount(final String accountId) {
624 getExecutor().execute(new SipRunnable() {
625 @Override
626 protected void doRun() throws SameThreadException {
627 Log.i(TAG, "SipService.setAccountDetails() thread running...");
628 configurationManagerJNI.removeAccount(accountId);
629 }
630 });
631 }
alision5f899632013-04-22 17:26:56 -0400632
alisione2a38e12013-04-25 14:20:20 -0400633 @Override
634 public ArrayList<HashMap<String, String>> getHistory() throws RemoteException {
635 class History extends SipRunnableWithReturn {
636
637 @Override
638 protected VectMap doRun() throws SameThreadException {
639 Log.i(TAG, "SipService.getHistory() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400640
alisione2a38e12013-04-25 14:20:20 -0400641 return configurationManagerJNI.getHistory();
642 }
643 }
644
645 History runInstance = new History();
646 getExecutor().execute(runInstance);
647 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400648 // Log.w(TAG, "Waiting for getHistory");
alisione2a38e12013-04-25 14:20:20 -0400649 }
alision1005ba12013-06-19 13:52:44 -0400650 Log.i(TAG, "SipService.getHistory() DONE");
alisione2a38e12013-04-25 14:20:20 -0400651 VectMap swigmap = (VectMap) runInstance.getVal();
652
653 ArrayList<HashMap<String, String>> nativemap = HistoryHandler.convertSwigToNative(swigmap);
654
655 return nativemap;
656 }
alision7f18fc82013-05-01 09:37:33 -0400657
alision43a9b362013-05-01 16:30:15 -0400658 /*************************
659 * Transfer related API
660 *************************/
661
alision7f18fc82013-05-01 09:37:33 -0400662 @Override
663 public void transfer(final String callID, final String to) throws RemoteException {
664 getExecutor().execute(new SipRunnable() {
665 @Override
666 protected void doRun() throws SameThreadException, RemoteException {
667 Log.i(TAG, "SipService.transfer() thread running...");
668 if (callManagerJNI.transfer(callID, to)) {
669 Bundle bundle = new Bundle();
670 bundle.putString("CallID", callID);
671 bundle.putString("State", "HUNGUP");
672 Intent intent = new Intent(CallManagerCallBack.CALL_STATE_CHANGED);
alision7f18fc82013-05-01 09:37:33 -0400673 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
alision84813a12013-05-27 17:40:39 -0400674 sendBroadcast(intent);
alision7f18fc82013-05-01 09:37:33 -0400675 } else
676 Log.i(TAG, "NOT OK");
677 }
678 });
679
680 }
alision43a9b362013-05-01 16:30:15 -0400681
alision7f18fc82013-05-01 09:37:33 -0400682 @Override
683 public void attendedTransfer(final String transferID, final String targetID) throws RemoteException {
684 getExecutor().execute(new SipRunnable() {
685 @Override
686 protected void doRun() throws SameThreadException, RemoteException {
alision43a9b362013-05-01 16:30:15 -0400687 Log.i(TAG, "SipService.attendedTransfer() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400688 if (callManagerJNI.attendedTransfer(transferID, targetID)) {
689 Log.i(TAG, "OK");
alision7f18fc82013-05-01 09:37:33 -0400690 } else
691 Log.i(TAG, "NOT OK");
692 }
693 });
alision43a9b362013-05-01 16:30:15 -0400694
695 }
696
697 /*************************
698 * Conference related API
699 *************************/
700
701 @Override
702 public void removeConference(final String confID) throws RemoteException {
703 getExecutor().execute(new SipRunnable() {
704 @Override
705 protected void doRun() throws SameThreadException, RemoteException {
706 Log.i(TAG, "SipService.createConference() thread running...");
707 callManagerJNI.removeConference(confID);
708 }
709 });
710
alision7f18fc82013-05-01 09:37:33 -0400711 }
712
713 @Override
alision43a9b362013-05-01 16:30:15 -0400714 public void joinParticipant(final String sel_callID, final String drag_callID) throws RemoteException {
715 getExecutor().execute(new SipRunnable() {
716 @Override
717 protected void doRun() throws SameThreadException, RemoteException {
718 Log.i(TAG, "SipService.joinParticipant() thread running...");
719 callManagerJNI.joinParticipant(sel_callID, drag_callID);
alision806e18e2013-06-21 15:30:17 -0400720 // Generate a CONF_CREATED callback
alision43a9b362013-05-01 16:30:15 -0400721 }
722 });
723
alision7f18fc82013-05-01 09:37:33 -0400724 }
725
alision7f18fc82013-05-01 09:37:33 -0400726 @Override
alision806e18e2013-06-21 15:30:17 -0400727 public void addParticipant(final SipCall call, final String confID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400728 getExecutor().execute(new SipRunnable() {
729 @Override
730 protected void doRun() throws SameThreadException, RemoteException {
731 Log.i(TAG, "SipService.addParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400732 callManagerJNI.addParticipant(call.getCallId(), confID);
733 current_confs.get(confID).getParticipants().add(call);
alision43a9b362013-05-01 16:30:15 -0400734 }
735 });
736
alision7f18fc82013-05-01 09:37:33 -0400737 }
738
739 @Override
alision43a9b362013-05-01 16:30:15 -0400740 public void addMainParticipant(final String confID) throws RemoteException {
741 getExecutor().execute(new SipRunnable() {
742 @Override
743 protected void doRun() throws SameThreadException, RemoteException {
744 Log.i(TAG, "SipService.addMainParticipant() thread running...");
745 callManagerJNI.addMainParticipant(confID);
746 }
747 });
748
alision7f18fc82013-05-01 09:37:33 -0400749 }
750
751 @Override
alision43a9b362013-05-01 16:30:15 -0400752 public void detachParticipant(final String callID) throws RemoteException {
753 getExecutor().execute(new SipRunnable() {
754 @Override
755 protected void doRun() throws SameThreadException, RemoteException {
756 Log.i(TAG, "SipService.detachParticipant() thread running...");
alision806e18e2013-06-21 15:30:17 -0400757 Log.i(TAG, "Detaching " + callID);
758 Iterator<Entry<String, Conference>> it = current_confs.entrySet().iterator();
759 Log.i(TAG, "current_confs size " + current_confs.size());
760 while (it.hasNext()) {
761 Conference tmp = it.next().getValue();
762 Log.i(TAG, "conf has " + tmp.getParticipants().size() + " participants");
763 if (tmp.contains(callID)) {
764 current_calls.put(callID, tmp.getCall(callID));
765 Log.i(TAG, "Call found and put in current_calls");
766 }
767 }
alision43a9b362013-05-01 16:30:15 -0400768 callManagerJNI.detachParticipant(callID);
769 }
770 });
771
alision7f18fc82013-05-01 09:37:33 -0400772 }
773
774 @Override
alision43a9b362013-05-01 16:30:15 -0400775 public void joinConference(final String sel_confID, final String drag_confID) throws RemoteException {
776 getExecutor().execute(new SipRunnable() {
777 @Override
778 protected void doRun() throws SameThreadException, RemoteException {
779 Log.i(TAG, "SipService.joinConference() thread running...");
780 callManagerJNI.joinConference(sel_confID, drag_confID);
781 }
782 });
783
alision7f18fc82013-05-01 09:37:33 -0400784 }
785
786 @Override
alision43a9b362013-05-01 16:30:15 -0400787 public void hangUpConference(final String 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.hangUpConference(confID);
793 }
794 });
795
alision7f18fc82013-05-01 09:37:33 -0400796 }
797
798 @Override
alision43a9b362013-05-01 16:30:15 -0400799 public void holdConference(final String confID) throws RemoteException {
800 getExecutor().execute(new SipRunnable() {
801 @Override
802 protected void doRun() throws SameThreadException, RemoteException {
803 Log.i(TAG, "SipService.holdConference() thread running...");
804 callManagerJNI.holdConference(confID);
805 }
806 });
807
alision7f18fc82013-05-01 09:37:33 -0400808 }
809
810 @Override
alision43a9b362013-05-01 16:30:15 -0400811 public void unholdConference(final String confID) throws RemoteException {
812 getExecutor().execute(new SipRunnable() {
813 @Override
814 protected void doRun() throws SameThreadException, RemoteException {
815 Log.i(TAG, "SipService.unholdConference() thread running...");
816 callManagerJNI.unholdConference(confID);
817 }
818 });
819
alision7f18fc82013-05-01 09:37:33 -0400820 }
Alexandre Lision67817192013-07-18 12:04:30 -0400821
alisioncd8fb912013-06-28 14:43:51 -0400822 @Override
823 public boolean isConferenceParticipant(final String callID) throws RemoteException {
824 class IsParticipant extends SipRunnableWithReturn {
825
826 @Override
827 protected Boolean doRun() throws SameThreadException {
828 Log.i(TAG, "SipService.isRecording() thread running...");
829 return callManagerJNI.isConferenceParticipant(callID);
830 }
831 }
832
833 IsParticipant runInstance = new IsParticipant();
834 getExecutor().execute(runInstance);
835 while (!runInstance.isDone()) {
836 }
837
838 return (Boolean) runInstance.getVal();
839 }
alision7f18fc82013-05-01 09:37:33 -0400840
841 @Override
alisiondf1dac92013-06-27 17:35:53 -0400842 public HashMap<String, Conference> getConferenceList() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -0400843 // class ConfList extends SipRunnableWithReturn {
844 // @Override
845 // protected StringVect doRun() throws SameThreadException {
846 // Log.i(TAG, "SipService.getConferenceList() thread running...");
847 // return callManagerJNI.getConferenceList();
848 // }
849 // }
850 // ;
851 // ConfList runInstance = new ConfList();
852 // getExecutor().execute(runInstance);
853 // while (!runInstance.isDone()) {
854 // // Log.w(TAG, "Waiting for getConferenceList");
855 // }
856 // StringVect swigvect = (StringVect) runInstance.getVal();
857 //
858 // ArrayList<String> nativelist = new ArrayList<String>();
859 //
860 // for (int i = 0; i < swigvect.size(); i++)
861 // nativelist.add(swigvect.get(i));
862 //
863 // return nativelist;
alisiondf1dac92013-06-27 17:35:53 -0400864 return current_confs;
alision7f18fc82013-05-01 09:37:33 -0400865 }
866
867 @Override
alision907bde72013-06-20 14:40:37 -0400868 public List getParticipantList(final String confID) throws RemoteException {
869 class PartList extends SipRunnableWithReturn {
870 @Override
871 protected StringVect doRun() throws SameThreadException {
872 Log.i(TAG, "SipService.getAccountList() thread running...");
873 return callManagerJNI.getParticipantList(confID);
874 }
875 }
876 ;
877 PartList runInstance = new PartList();
878 getExecutor().execute(runInstance);
879 while (!runInstance.isDone()) {
880 // Log.w(TAG, "Waiting for getConferenceList");
881 }
882 StringVect swigvect = (StringVect) runInstance.getVal();
883
884 ArrayList<String> nativelist = new ArrayList<String>();
885
886 for (int i = 0; i < swigvect.size(); i++)
887 nativelist.add(swigvect.get(i));
888
889 return nativelist;
alision7f18fc82013-05-01 09:37:33 -0400890 }
alision806e18e2013-06-21 15:30:17 -0400891
alision1005ba12013-06-19 13:52:44 -0400892 @Override
alision7f18fc82013-05-01 09:37:33 -0400893 public String getConferenceId(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400894 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400895 return null;
896 }
897
898 @Override
alision806e18e2013-06-21 15:30:17 -0400899 public String getConferenceDetails(final String callID) throws RemoteException {
900 class ConfDetails extends SipRunnableWithReturn {
901 @Override
902 protected StringMap doRun() throws SameThreadException {
903 Log.i(TAG, "SipService.getAccountList() thread running...");
904 return callManagerJNI.getConferenceDetails(callID);
905 }
906 }
907 ;
908 ConfDetails runInstance = new ConfDetails();
909 getExecutor().execute(runInstance);
910 while (!runInstance.isDone()) {
911 // Log.w(TAG, "Waiting for getConferenceList");
912 }
913 StringMap swigvect = (StringMap) runInstance.getVal();
914
915 return swigvect.get("CONF_STATE");
alision7f18fc82013-05-01 09:37:33 -0400916 }
917
alision04a00182013-05-10 17:05:29 -0400918 @Override
919 public String getRecordPath() throws RemoteException {
920 class RecordPath extends SipRunnableWithReturn {
921
922 @Override
923 protected String doRun() throws SameThreadException {
924 Log.i(TAG, "SipService.getRecordPath() thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +1000925 return configurationManagerJNI.getRecordPath();
alision04a00182013-05-10 17:05:29 -0400926 }
927 }
928
929 RecordPath runInstance = new RecordPath();
930 getExecutor().execute(runInstance);
931 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400932 // Log.w(TAG, "Waiting for getRecordPath");
alision04a00182013-05-10 17:05:29 -0400933 }
934 String path = (String) runInstance.getVal();
935
936 return path;
937 }
938
939 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400940 public boolean toggleRecordingCall(final String id) throws RemoteException {
Adrien Béraud9360f242013-09-19 11:07:42 +1000941
Alexandre Lisiona764c682013-09-09 10:02:07 -0400942 class ToggleRecording extends SipRunnableWithReturn {
943
alision04a00182013-05-10 17:05:29 -0400944 @Override
Alexandre Lisiona764c682013-09-09 10:02:07 -0400945 protected Boolean doRun() throws SameThreadException {
946 Log.i(TAG, "SipService.toggleRecordingCall() thread running...");
947 return callManagerJNI.toggleRecording(id);
alision04a00182013-05-10 17:05:29 -0400948 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400949 }
950
951 ToggleRecording runInstance = new ToggleRecording();
952 getExecutor().execute(runInstance);
953 while (!runInstance.isDone()) {
954 }
955
956 return (Boolean) runInstance.getVal();
alision04a00182013-05-10 17:05:29 -0400957
958 }
Alexandre Lision67817192013-07-18 12:04:30 -0400959
alision50fa0722013-06-25 17:29:44 -0400960 @Override
alisiondf1dac92013-06-27 17:35:53 -0400961 public boolean isRecording(final String id) throws RemoteException {
962 class IsRecording extends SipRunnableWithReturn {
963
964 @Override
965 protected Boolean doRun() throws SameThreadException {
966 Log.i(TAG, "SipService.isRecording() thread running...");
967 return callManagerJNI.getIsRecording(id);
968 }
969 }
970
971 IsRecording runInstance = new IsRecording();
972 getExecutor().execute(runInstance);
973 while (!runInstance.isDone()) {
974 }
975
976 return (Boolean) runInstance.getVal();
Alexandre Lision67817192013-07-18 12:04:30 -0400977
alisiondf1dac92013-06-27 17:35:53 -0400978 }
Alexandre Lision67817192013-07-18 12:04:30 -0400979
alisiondf1dac92013-06-27 17:35:53 -0400980 @Override
alision50fa0722013-06-25 17:29:44 -0400981 public boolean startRecordedFilePlayback(final String filepath) throws RemoteException {
982 getExecutor().execute(new SipRunnable() {
983 @Override
984 protected void doRun() throws SameThreadException, RemoteException {
985 Log.i(TAG, "SipService.setRecordingCall() thread running...");
986 callManagerJNI.startRecordedFilePlayback(filepath);
987 }
988 });
989 return false;
990 }
991
992 @Override
993 public void stopRecordedFilePlayback(final String filepath) throws RemoteException {
994 getExecutor().execute(new SipRunnable() {
995 @Override
996 protected void doRun() throws SameThreadException, RemoteException {
997 Log.i(TAG, "SipService.stopRecordedFilePlayback() thread running...");
998 callManagerJNI.stopRecordedFilePlayback(filepath);
999 }
1000 });
1001 }
alision04a00182013-05-10 17:05:29 -04001002
1003 @Override
1004 public void setRecordPath(final String path) throws RemoteException {
1005 getExecutor().execute(new SipRunnable() {
1006 @Override
1007 protected void doRun() throws SameThreadException, RemoteException {
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001008 Log.i(TAG, "SipService.setRecordPath() " + path + " thread running...");
Adrien Béraud9360f242013-09-19 11:07:42 +10001009 configurationManagerJNI.setRecordPath(path);
alision04a00182013-05-10 17:05:29 -04001010 }
1011 });
1012 }
1013
1014 @Override
1015 public void sendTextMessage(final String callID, final String message, final String from) throws RemoteException {
1016 getExecutor().execute(new SipRunnable() {
1017 @Override
1018 protected void doRun() throws SameThreadException, RemoteException {
1019 Log.i(TAG, "SipService.sendTextMessage() thread running...");
Alexandre Lision10314352013-07-17 15:06:56 -04001020 callManagerJNI.sendTextMessage(callID, message);
alision04a00182013-05-10 17:05:29 -04001021 }
1022 });
1023
1024 }
1025
alisiond295ec22013-05-17 10:12:13 -04001026 @Override
1027 public List getAudioCodecList(String accountID) throws RemoteException {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001028 class AudioCodecList extends SipRunnableWithReturn {
1029
1030 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001031 protected ArrayList<Codec> doRun() throws SameThreadException {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001032 Log.i(TAG, "SipService.getAudioCodecList() thread running...");
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001033 ArrayList<Codec> results = new ArrayList<Codec>();
1034 IntVect payloads = configurationManagerJNI.getAudioCodecList();
1035 for(int i = 0; i < payloads.size() ; ++i ){
1036 results.add(new Codec(payloads.get(i), configurationManagerJNI.getAudioCodecDetails(payloads.get(i))));
1037 }
1038 return results;
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001039 }
1040 }
1041
1042 AudioCodecList runInstance = new AudioCodecList();
1043 getExecutor().execute(runInstance);
1044 while (!runInstance.isDone()) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001045 }
Alexandre Lision933ef0a2013-10-15 17:28:40 -04001046 ArrayList<Codec> codecs = (ArrayList<Codec>) runInstance.getVal();
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001047 return codecs;
alisiond295ec22013-05-17 10:12:13 -04001048 }
1049
alision9f7a6ec2013-05-24 16:26:26 -04001050 @Override
alisionfde875f2013-05-28 17:01:54 -04001051 public HashMap<String, SipCall> getCallList() throws RemoteException {
alision85704182013-05-29 15:23:03 -04001052 // class CallList extends SipRunnableWithReturn {
1053 //
1054 // @Override
1055 // protected StringVect doRun() throws SameThreadException {
1056 // Log.i(TAG, "SipService.getCallList() thread running...");
1057 // return callManagerJNI.getCallList();
1058 // }
1059 // }
1060 //
1061 // CallList runInstance = new CallList();
1062 // getExecutor().execute(runInstance);
1063 // while (!runInstance.isDone()) {
1064 // Log.w(TAG, "Waiting for getAudioCodecList");
1065 // }
1066 // StringVect swigmap = (StringVect) runInstance.getVal();
1067 //
1068 // ArrayList<String> nativemap = new ArrayList<String>();
1069 // for (int i = 0; i < swigmap.size(); ++i) {
1070 //
1071 // String t = swigmap.get(i);
1072 // nativemap.add(t);
1073 // }
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001074 // if(callManagerJNI == null)
1075 // return new HashMap<String, SipCall>();
1076 //
1077 //
1078 // HashMap<String, SipCall> results = new HashMap<String, SipCall>();
1079 // StringVect calls = callManagerJNI.getCallList();
1080 // for(int i = 0 ; i < calls.size(); ++i){
1081 // results.put(calls.get(i), new SipCall(calls.get(i), callManagerJNI.getCallDetails(calls.get(i))));
1082 // }
alision9f7a6ec2013-05-24 16:26:26 -04001083
alision2cb99562013-05-30 17:02:20 -04001084 return getCurrent_calls();
alision9f7a6ec2013-05-24 16:26:26 -04001085 }
1086
alision85704182013-05-29 15:23:03 -04001087 @Override
1088 public SipCall getCall(String callID) throws RemoteException {
alision2cb99562013-05-30 17:02:20 -04001089 return getCurrent_calls().get(callID);
1090 }
1091
1092 /***********************
1093 * Notification API
1094 ***********************/
1095 @Override
1096 public void createNotification() throws RemoteException {
1097 makeNotification();
alisioncc7bb422013-06-06 15:31:39 -04001098
alision2cb99562013-05-30 17:02:20 -04001099 }
1100
1101 @Override
1102 public void destroyNotification() throws RemoteException {
1103 removeNotification();
alisioncc7bb422013-06-06 15:31:39 -04001104
alision2cb99562013-05-30 17:02:20 -04001105 }
alisioncc7bb422013-06-06 15:31:39 -04001106
Adrien Béraud9360f242013-09-19 11:07:42 +10001107 private final int NOTIFICATION_ID = new Random().nextInt(1000);
alisioncc7bb422013-06-06 15:31:39 -04001108
alision2cb99562013-05-30 17:02:20 -04001109 private void makeNotification() {
alisioncc7bb422013-06-06 15:31:39 -04001110 if (current_calls.size() == 0) {
alision2cb99562013-05-30 17:02:20 -04001111 return;
1112 }
1113 Intent notificationIntent = new Intent(getApplicationContext(), SFLPhoneHomeActivity.class);
alisioncc7bb422013-06-06 15:31:39 -04001114 PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 007, notificationIntent,
1115 PendingIntent.FLAG_UPDATE_CURRENT);
alision2cb99562013-05-30 17:02:20 -04001116
1117 NotificationManager nm = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
Adrien Béraud9360f242013-09-19 11:07:42 +10001118 nm.cancel(NOTIFICATION_ID); // clear previous notifications.
alision2cb99562013-05-30 17:02:20 -04001119
1120 NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
alisioncc7bb422013-06-06 15:31:39 -04001121 //
1122 // builder.setContent(view);
1123 builder.setContentIntent(contentIntent).setOngoing(true).setSmallIcon(R.drawable.ic_launcher)
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001124 .setContentTitle(getCurrent_calls().size() + " ongoing calls").setTicker("Pending calls").setWhen(System.currentTimeMillis())
1125 .setAutoCancel(false);
alision2cb99562013-05-30 17:02:20 -04001126 builder.setPriority(NotificationCompat.PRIORITY_MAX);
1127 Notification n = builder.build();
1128
1129 nm.notify(NOTIFICATION_ID, n);
alision2cb99562013-05-30 17:02:20 -04001130 }
1131
1132 public void removeNotification() {
1133 NotificationManager nm = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
1134 nm.cancel(NOTIFICATION_ID);
alision85704182013-05-29 15:23:03 -04001135 }
1136
alisiondf1dac92013-06-27 17:35:53 -04001137 @Override
1138 public Conference getCurrentCall() throws RemoteException {
Alexandre Lision67817192013-07-18 12:04:30 -04001139 for (SipCall i : current_calls.values()) {
Adrien Béraud9360f242013-09-19 11:07:42 +10001140
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001141 // Incoming >> Ongoing
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001142 if (i.isIncoming()) {
Alexandre Lisionc51ccb12013-09-11 16:00:30 -04001143 Conference tmp = new Conference("-1");
1144 tmp.getParticipants().add(i);
1145 return tmp;
1146 }
Adrien Béraud9360f242013-09-19 11:07:42 +10001147
Alexandre Lision67817192013-07-18 12:04:30 -04001148 if (i.isOngoing()) {
alisiondf1dac92013-06-27 17:35:53 -04001149 Conference tmp = new Conference("-1");
1150 tmp.getParticipants().add(i);
1151 return tmp;
1152 }
1153 }
Alexandre Lision67817192013-07-18 12:04:30 -04001154
1155 if (!current_confs.isEmpty()) {
alisiondf1dac92013-06-27 17:35:53 -04001156 return (Conference) current_confs.values().toArray()[0];
1157 }
1158 return null;
1159 }
1160
Alexandre Lision64dc8c02013-09-25 15:32:25 -04001161 @Override
1162 public void playDtmf(final String key) throws RemoteException {
1163 getExecutor().execute(new SipRunnable() {
1164 @Override
1165 protected void doRun() throws SameThreadException, RemoteException {
1166 Log.i(TAG, "SipService.playDtmf() thread running...");
1167 callManagerJNI.playDTMF(key);
1168 }
1169 });
1170 }
1171
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001172 @Override
1173 public List getConcurrentCalls() throws RemoteException {
1174 ArrayList<Conference> toReturn = new ArrayList<Conference>();
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001175
1176 for (SipCall sip : current_calls.values()) {
1177 if (!sip.isCurrent()) {
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001178 Conference tmp = new Conference("-1");
1179 tmp.getParticipants().add(sip);
1180 toReturn.add(tmp);
1181 }
1182 }
Alexandre Lisionb4a9e392013-10-01 14:39:43 -04001183
1184 Log.i(TAG, "toReturn SIZE " + toReturn.size());
1185
Alexandre Lision6ae652d2013-09-26 16:39:20 -04001186 return toReturn;
1187 }
1188
Alexandre Lisionafd40e42013-10-15 13:48:37 -04001189 @Override
1190 public Map getRingtoneList() throws RemoteException {
1191 // TODO Stub de la méthode généré automatiquement
1192 return null;
1193 }
1194
Emeric Vigier6119d782012-09-21 18:04:14 -04001195 };
Emeric Vigiereaf2c492012-09-19 14:38:20 -04001196}