blob: 2f499752aa8a87ed5812417c45af837613f1324c [file] [log] [blame]
Emeric Vigier6119d782012-09-21 18:04:14 -04001/**
2 * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
3 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
4 *
5 * Author: Regis Montoya <r3gis.3R@gmail.com>
6 * Author: Emeric Vigier <emeric.vigier@savoirfairelinux.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 * If you own a pjsip commercial license you can also redistribute it
13 * and/or modify it under the terms of the GNU Lesser General Public License
14 * as an android library.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
Emeric Vigiereaf2c492012-09-19 14:38:20 -040024package com.savoirfairelinux.sflphone.service;
25
Emeric Vigier6119d782012-09-21 18:04:14 -040026import java.lang.ref.WeakReference;
alision17052d42013-04-22 10:39:38 -040027import java.util.ArrayList;
28import java.util.HashMap;
alision7f18fc82013-05-01 09:37:33 -040029import java.util.List;
alision17052d42013-04-22 10:39:38 -040030import java.util.Map;
Emeric Vigier6119d782012-09-21 18:04:14 -040031
Emeric Vigiereaf2c492012-09-19 14:38:20 -040032import android.app.Service;
alision17052d42013-04-22 10:39:38 -040033import android.content.BroadcastReceiver;
34import android.content.Context;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040035import android.content.Intent;
alision17052d42013-04-22 10:39:38 -040036import android.content.IntentFilter;
alision7f18fc82013-05-01 09:37:33 -040037import android.os.Bundle;
Emeric Vigier6119d782012-09-21 18:04:14 -040038import android.os.Handler;
39import android.os.HandlerThread;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040040import android.os.IBinder;
Emeric Vigier6119d782012-09-21 18:04:14 -040041import android.os.Looper;
42import android.os.Message;
alision5f899632013-04-22 17:26:56 -040043import android.os.RemoteException;
alision43a9b362013-05-01 16:30:15 -040044import android.os.Vibrator;
alision17052d42013-04-22 10:39:38 -040045import android.support.v4.content.LocalBroadcastManager;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040046import android.util.Log;
47import android.widget.Toast;
48
alisionf76de3b2013-04-16 15:35:22 -040049import com.savoirfairelinux.sflphone.account.AccountDetailsHandler;
alisiond295ec22013-05-17 10:12:13 -040050import com.savoirfairelinux.sflphone.account.AudioHandler;
alisione2a38e12013-04-25 14:20:20 -040051import com.savoirfairelinux.sflphone.account.HistoryHandler;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040052import com.savoirfairelinux.sflphone.client.SFLphoneApplication;
Alexandre Savard713a34d2012-09-26 15:50:41 -040053
Emeric Vigiereaf2c492012-09-19 14:38:20 -040054public class SipService extends Service {
55
56 static final String TAG = "SipService";
57 static final int DELAY = 5000; /* 5 sec */
58 private boolean runFlag = false;
59 private SipServiceThread sipServiceThread;
Emeric Vigier84e05da2012-09-20 14:53:05 -040060 private SFLphoneApplication sflphoneApp;
Emeric Vigier6119d782012-09-21 18:04:14 -040061 private SipServiceExecutor mExecutor;
62 private static HandlerThread executorThread;
63 private CallManagerJNI callManagerJNI;
Emeric Vigier0007dee2012-09-24 11:35:58 -040064 private CallManagerCallBack callManagerCallBack;
Alexandre Savardc1b08fe2012-09-25 16:24:47 -040065 private ConfigurationManagerJNI configurationManagerJNI;
Alexandre Savardfccd1dc2012-10-17 17:31:38 -040066 private ConfigurationManagerCallback configurationManagerCallback;
Emeric Vigier0007dee2012-09-24 11:35:58 -040067 private ManagerImpl managerImpl;
Emeric Vigier6119d782012-09-21 18:04:14 -040068 private boolean isPjSipStackStarted = false;
alision5f899632013-04-22 17:26:56 -040069 ISipClient client;
alision04a00182013-05-10 17:05:29 -040070
alision4a0eb092013-05-07 13:52:03 -040071 int clients = 0;
Emeric Vigier6119d782012-09-21 18:04:14 -040072
alision43a9b362013-05-01 16:30:15 -040073 private BroadcastReceiver IncomingReceiver = new BroadcastReceiver() {
74
75 @Override
76 public void onReceive(Context context, Intent intent) {
77 // Get instance of Vibrator from current Context
78
79 if (client != null) {
80 try {
alision4a0eb092013-05-07 13:52:03 -040081 if (intent.getAction().contentEquals(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED)) {
82 Log.i(TAG, "Received" + intent.getAction());
alision04a00182013-05-10 17:05:29 -040083 } else if (intent.getAction().contentEquals(ConfigurationManagerCallback.ACCOUNTS_LOADED)) {
alision4a0eb092013-05-07 13:52:03 -040084 Log.i(TAG, "Received" + intent.getAction());
alision04a00182013-05-10 17:05:29 -040085 } else if (intent.getAction().contentEquals(ConfigurationManagerCallback.ACCOUNTS_CHANGED)) {
alision4a0eb092013-05-07 13:52:03 -040086 Log.i(TAG, "Received" + intent.getAction());
alision04a00182013-05-10 17:05:29 -040087 } else if(intent.getAction().contentEquals(CallManagerCallBack.INCOMING_TEXT)) {
88 Log.i(TAG, "Received" + intent.getAction());
89 client.incomingText(intent);
90 } else if (intent.getAction().contentEquals(CallManagerCallBack.INCOMING_CALL)) {
alision43a9b362013-05-01 16:30:15 -040091 Log.i(TAG, "Received" + intent.getAction());
alision43a9b362013-05-01 16:30:15 -040092 client.incomingCall(intent);
alision43a9b362013-05-01 16:30:15 -040093 } else if (intent.getAction().contentEquals(CallManagerCallBack.CALL_STATE_CHANGED)) {
94 Log.i(TAG, "Received" + intent.getAction());
95 client.callStateChanged(intent);
96 } else if (intent.getAction().contentEquals(CallManagerCallBack.NEW_CALL_CREATED)) {
97 Log.i(TAG, "Received" + intent.getAction());
98 Vibrator mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
99 mVibrator.vibrate(300);
100 }
101 } catch (RemoteException e) {
102 Log.e(TAG, e.toString());
103 }
104 }
105
106 }
107 };
108
109 @Override
110 public boolean onUnbind(Intent i) {
111 super.onUnbind(i);
112 Log.i(TAG, "onUnbind(intent)");
113 return false;
114
115 }
116
117 /* called once by startService() */
118 @Override
119 public void onCreate() {
120 Log.i(TAG, "onCreated");
121 super.onCreate();
122
123 sflphoneApp = (SFLphoneApplication) getApplication();
124 sipServiceThread = new SipServiceThread();
125
126 IntentFilter callFilter = new IntentFilter(CallManagerCallBack.CALL_STATE_CHANGED);
127 callFilter.addAction(CallManagerCallBack.INCOMING_CALL);
128 callFilter.addAction(CallManagerCallBack.NEW_CALL_CREATED);
alision4a0eb092013-05-07 13:52:03 -0400129 callFilter.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
130 callFilter.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
131 callFilter.addAction(ConfigurationManagerCallback.ACCOUNTS_LOADED);
alision04a00182013-05-10 17:05:29 -0400132 callFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
133
alision43a9b362013-05-01 16:30:15 -0400134 LocalBroadcastManager.getInstance(this).registerReceiver(IncomingReceiver, callFilter);
135 getExecutor().execute(new StartRunnable());
136 }
137
138 /* called for each startService() */
139 @Override
140 public int onStartCommand(Intent intent, int flags, int startId) {
141 Log.i(TAG, "onStarted");
142 super.onStartCommand(intent, flags, startId);
143
144 if (!runFlag) {
145 sipServiceThread.start();
146 runFlag = true;
147 sflphoneApp.setServiceRunning(true);
148 Toast.makeText(this, "Sflphone Service started", Toast.LENGTH_SHORT).show();
149 }
150
151 return START_STICKY; /* started and stopped explicitly */
152 }
153
154 @Override
155 public void onDestroy() {
156 /* called once by stopService() */
157 sipServiceThread.interrupt();
158 sipServiceThread = null;
159 runFlag = false;
160 sflphoneApp.setServiceRunning(false);
161 Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show();
162 super.onDestroy();
163
164 Log.i(TAG, "onDestroyed");
165 }
166
167 @Override
168 public IBinder onBind(Intent arg0) {
169 Log.i(TAG, "onBound");
170 return mBinder;
171 }
172
173 private static Looper createLooper() {
174 if (executorThread == null) {
175 Log.d(TAG, "Creating new handler thread");
176 // ADT gives a fake warning due to bad parse rule.
177 executorThread = new HandlerThread("SipService.Executor");
178 executorThread.start();
179 }
180 return executorThread.getLooper();
181 }
182
183 public SipServiceExecutor getExecutor() {
184 // create mExecutor lazily
185 if (mExecutor == null) {
186 mExecutor = new SipServiceExecutor(this);
187 }
188 return mExecutor;
189 }
190
191 // Executes immediate tasks in a single executorThread.
192 public static class SipServiceExecutor extends Handler {
193 WeakReference<SipService> handlerService;
194
195 SipServiceExecutor(SipService s) {
196 super(createLooper());
197 handlerService = new WeakReference<SipService>(s);
198 }
199
200 public void execute(Runnable task) {
201 // TODO: add wakelock
202 Message.obtain(this, 0/* don't care */, task).sendToTarget();
203 }
204
205 @Override
206 public void handleMessage(Message msg) {
207 if (msg.obj instanceof Runnable) {
208 executeInternal((Runnable) msg.obj);
209 } else {
210 Log.w(TAG, "can't handle msg: " + msg);
211 }
212 }
213
214 private void executeInternal(Runnable task) {
215 try {
216 task.run();
217 } catch (Throwable t) {
218 Log.e(TAG, "run task: " + task, t);
219 }
220 }
221 }
222
223 private void startPjSipStack() throws SameThreadException {
224 if (isPjSipStackStarted)
225 return;
226
227 try {
228 System.loadLibrary("gnustl_shared");
229 System.loadLibrary("expat");
230 System.loadLibrary("yaml");
231 System.loadLibrary("ccgnu2");
232 System.loadLibrary("crypto");
233 System.loadLibrary("ssl");
234 System.loadLibrary("ccrtp1");
235 System.loadLibrary("dbus");
236 System.loadLibrary("dbus-c++-1");
237 System.loadLibrary("samplerate");
238 System.loadLibrary("codec_ulaw");
239 System.loadLibrary("codec_alaw");
alisiond8c83882013-05-17 17:00:42 -0400240 System.loadLibrary("codec_g722");
alision43a9b362013-05-01 16:30:15 -0400241 System.loadLibrary("speexresampler");
242 System.loadLibrary("sflphone");
243 isPjSipStackStarted = true;
244 } catch (UnsatisfiedLinkError e) {
245 Log.e(TAG, "Problem with the current Pj stack...", e);
246 isPjSipStackStarted = false;
247 return;
248 } catch (Exception e) {
249 Log.e(TAG, "Problem with the current Pj stack...", e);
250 }
251
252 /* get unique instance of managerImpl */
253 managerImpl = SFLPhoneservice.instance();
254
255 /* set static AppPath before calling manager.init */
256 managerImpl.setPath(sflphoneApp.getAppPath());
257
258 callManagerJNI = new CallManagerJNI();
259 callManagerCallBack = new CallManagerCallBack(this);
260 SFLPhoneservice.setCallbackObject(callManagerCallBack);
261
262 configurationManagerJNI = new ConfigurationManagerJNI();
263 configurationManagerCallback = new ConfigurationManagerCallback(this);
264 SFLPhoneservice.setConfigurationCallbackObject(configurationManagerCallback);
265
266 managerImpl.init("");
267 return;
268 }
269
270 // Enforce same thread contract to ensure we do not call from somewhere else
271 public class SameThreadException extends Exception {
272 private static final long serialVersionUID = -905639124232613768L;
273
274 public SameThreadException() {
275 super("Should be launched from a single worker thread");
276 }
277 }
278
279 public abstract static class SipRunnable implements Runnable {
280 protected abstract void doRun() throws SameThreadException, RemoteException;
281
282 public void run() {
283 try {
284 doRun();
285 } catch (SameThreadException e) {
286 Log.e(TAG, "Not done from same thread");
287 } catch (RemoteException e) {
288 Log.e(TAG, e.toString());
289 }
290 }
291 }
292
293 public abstract static class SipRunnableWithReturn implements Runnable {
294 Object obj = null;
295 boolean done = false;
296
297 protected abstract Object doRun() throws SameThreadException;
298
299 public Object getVal() {
300 return obj;
301 }
302
303 public boolean isDone() {
304 return done;
305 }
306
307 public void run() {
308 try {
309 obj = doRun();
310 done = true;
311 } catch (SameThreadException e) {
312 Log.e(TAG, "Not done from same thread");
313 }
314 }
315 }
316
317 class StartRunnable extends SipRunnable {
318 @Override
319 protected void doRun() throws SameThreadException {
320 startPjSipStack();
321 }
322 }
323
324 private class SipServiceThread extends Thread {
325
326 public SipServiceThread() {
327 super("sipServiceThread");
328 }
329
330 @Override
331 public void run() {
332 Log.i(TAG, "SipService thread running...");
333 SipService sipService = SipService.this;
334 while (sipService.runFlag) {
335 try {
336 Thread.sleep(DELAY);
337 } catch (InterruptedException e) {
338 sipService.runFlag = false;
339 Log.w(TAG, "service thread interrupted!");
340 }
341 }
342 }
343 }
344
345 /* ************************************
346 *
347 * Implement public interface for the service
348 *
349 *
350 * **********************************
351 */
Emeric Vigier6119d782012-09-21 18:04:14 -0400352 private final ISipService.Stub mBinder = new ISipService.Stub() {
353
354 @Override
355 public void placeCall(final String accountID, final String callID, final String to) {
356 getExecutor().execute(new SipRunnable() {
357 @Override
358 protected void doRun() throws SameThreadException {
359 Log.i(TAG, "SipService.placeCall() thread running...");
360 callManagerJNI.placeCall(accountID, callID, to);
361 }
362 });
363 }
364
365 @Override
366 public void refuse(final String callID) {
367 getExecutor().execute(new SipRunnable() {
368 @Override
369 protected void doRun() throws SameThreadException {
370 Log.i(TAG, "SipService.refuse() thread running...");
371 callManagerJNI.refuse(callID);
372 }
373 });
374 }
375
376 @Override
377 public void accept(final String callID) {
378 getExecutor().execute(new SipRunnable() {
379 @Override
380 protected void doRun() throws SameThreadException {
381 Log.i(TAG, "SipService.placeCall() thread running...");
382 callManagerJNI.accept(callID);
383 }
384 });
385 }
386
387 @Override
388 public void hangUp(final String callID) {
389 getExecutor().execute(new SipRunnable() {
390 @Override
391 protected void doRun() throws SameThreadException {
392 Log.i(TAG, "SipService.hangUp() thread running...");
393 callManagerJNI.hangUp(callID);
394 }
395 });
396 }
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400397
398 @Override
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400399 public void hold(final String callID) {
400 getExecutor().execute(new SipRunnable() {
401 @Override
402 protected void doRun() throws SameThreadException {
403 Log.i(TAG, "SipService.hold() thread running...");
404 callManagerJNI.hold(callID);
405 }
406 });
407 }
408
409 @Override
410 public void unhold(final String callID) {
411 getExecutor().execute(new SipRunnable() {
412 @Override
413 protected void doRun() throws SameThreadException {
414 Log.i(TAG, "SipService.unhold() thread running...");
415 callManagerJNI.unhold(callID);
416 }
417 });
418 }
419
420 @Override
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400421 public void setAudioPlugin(final String audioPlugin) {
422 getExecutor().execute(new SipRunnable() {
alision371b77e2013-04-23 14:51:26 -0400423 @Override
424 protected void doRun() throws SameThreadException {
425 Log.i(TAG, "SipService.setAudioPlugin() thread running...");
426 configurationManagerJNI.setAudioPlugin(audioPlugin);
427 }
Alexandre Savard31d27c62012-10-04 16:05:08 -0400428 });
429 }
430
431 @Override
432 public String getCurrentAudioOutputPlugin() {
433 class CurrentAudioPlugin extends SipRunnableWithReturn {
434 @Override
435 protected String doRun() throws SameThreadException {
436 Log.i(TAG, "SipService.getCurrentAudioOutputPlugin() thread running...");
437 return configurationManagerJNI.getCurrentAudioOutputPlugin();
438 }
alision371b77e2013-04-23 14:51:26 -0400439 }
440 ;
Alexandre Savard31d27c62012-10-04 16:05:08 -0400441
442 CurrentAudioPlugin runInstance = new CurrentAudioPlugin();
443 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400444 while (!runInstance.isDone()) {
alisiond295ec22013-05-17 10:12:13 -0400445// Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400446 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400447 return (String) runInstance.getVal();
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400448 }
Alexandre Savard713a34d2012-09-26 15:50:41 -0400449
450 @Override
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400451 public ArrayList<String> getAccountList() {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400452 class AccountList extends SipRunnableWithReturn {
453 @Override
454 protected StringVect doRun() throws SameThreadException {
455 Log.i(TAG, "SipService.getAccountList() thread running...");
456 return configurationManagerJNI.getAccountList();
457 }
alision371b77e2013-04-23 14:51:26 -0400458 }
459 ;
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400460 AccountList runInstance = new AccountList();
461 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400462 while (!runInstance.isDone()) {
alisiond295ec22013-05-17 10:12:13 -0400463// Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400464 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400465 StringVect swigvect = (StringVect) runInstance.getVal();
466
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400467 ArrayList<String> nativelist = new ArrayList<String>();
Alexandre Savard52a72522012-09-27 16:40:13 -0400468
alision371b77e2013-04-23 14:51:26 -0400469 for (int i = 0; i < swigvect.size(); i++)
470 nativelist.add(swigvect.get(i));
Alexandre Savard52a72522012-09-27 16:40:13 -0400471
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400472 return nativelist;
alision371b77e2013-04-23 14:51:26 -0400473 }
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400474
475 @Override
alision371b77e2013-04-23 14:51:26 -0400476 public HashMap<String, String> getAccountDetails(final String accountID) {
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400477 class AccountDetails extends SipRunnableWithReturn {
478 private String id;
alision371b77e2013-04-23 14:51:26 -0400479
480 AccountDetails(String accountId) {
481 id = accountId;
482 }
483
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400484 @Override
485 protected StringMap doRun() throws SameThreadException {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400486 Log.i(TAG, "SipService.getAccountDetails() thread running...");
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400487 return configurationManagerJNI.getAccountDetails(id);
488 }
alision371b77e2013-04-23 14:51:26 -0400489 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400490
491 AccountDetails runInstance = new AccountDetails(accountID);
492 getExecutor().execute(runInstance);
alision4a0eb092013-05-07 13:52:03 -0400493 clients++;
alision371b77e2013-04-23 14:51:26 -0400494 while (!runInstance.isDone()) {
alision04a00182013-05-10 17:05:29 -0400495 Log.w(TAG, "Waiting for Details " + clients);
alision371b77e2013-04-23 14:51:26 -0400496 }
alision4a0eb092013-05-07 13:52:03 -0400497 clients--;
alision371b77e2013-04-23 14:51:26 -0400498 StringMap swigmap = (StringMap) runInstance.getVal();
Alexandre Savard713a34d2012-09-26 15:50:41 -0400499
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400500 HashMap<String, String> nativemap = AccountDetailsHandler.convertSwigToNative(swigmap);
Alexandre Savard713a34d2012-09-26 15:50:41 -0400501
502 return nativemap;
503 }
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400504
505 @Override
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400506 public void setAccountDetails(final String accountId, Map map) {
alision371b77e2013-04-23 14:51:26 -0400507 HashMap<String, String> nativemap = (HashMap<String, String>) map;
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400508
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400509 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig(nativemap);
Alexandre Savard718d49f2012-10-02 15:17:13 -0400510
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400511 getExecutor().execute(new SipRunnable() {
512 @Override
513 protected void doRun() throws SameThreadException {
514 Log.i(TAG, "SipService.setAccountDetails() thread running...");
515 configurationManagerJNI.setAccountDetails(accountId, swigmap);
516 }
517 });
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400518 }
519
Alexandre Savard46036572012-10-05 13:56:49 -0400520 @Override
521 public String addAccount(Map map) {
522 class AddAccount extends SipRunnableWithReturn {
523 StringMap map;
alision371b77e2013-04-23 14:51:26 -0400524
525 AddAccount(StringMap m) {
526 map = m;
527 }
528
Alexandre Savard46036572012-10-05 13:56:49 -0400529 @Override
530 protected String doRun() throws SameThreadException {
531 Log.i(TAG, "SipService.getAccountDetails() thread running...");
532 return configurationManagerJNI.addAccount(map);
533 }
alision371b77e2013-04-23 14:51:26 -0400534 }
Alexandre Savard46036572012-10-05 13:56:49 -0400535
alision371b77e2013-04-23 14:51:26 -0400536 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig((HashMap<String, String>) map);
Alexandre Savard46036572012-10-05 13:56:49 -0400537
538 AddAccount runInstance = new AddAccount(swigmap);
539 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400540 while (!runInstance.isDone()) {
alisiond295ec22013-05-17 10:12:13 -0400541// Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400542 }
Alexandre Savard46036572012-10-05 13:56:49 -0400543 String accountId = (String) runInstance.getVal();
544
545 return accountId;
546 }
547
548 @Override
549 public void removeAccount(final String accountId) {
550 getExecutor().execute(new SipRunnable() {
551 @Override
552 protected void doRun() throws SameThreadException {
553 Log.i(TAG, "SipService.setAccountDetails() thread running...");
554 configurationManagerJNI.removeAccount(accountId);
555 }
556 });
557 }
alision5f899632013-04-22 17:26:56 -0400558
559 @Override
560 public void registerClient(ISipClient callback) throws RemoteException {
alision371b77e2013-04-23 14:51:26 -0400561 client = callback;
alision5f899632013-04-22 17:26:56 -0400562 }
alisione2a38e12013-04-25 14:20:20 -0400563
564 @Override
565 public ArrayList<HashMap<String, String>> getHistory() throws RemoteException {
566 class History extends SipRunnableWithReturn {
567
568 @Override
569 protected VectMap doRun() throws SameThreadException {
570 Log.i(TAG, "SipService.getHistory() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400571
alisione2a38e12013-04-25 14:20:20 -0400572 return configurationManagerJNI.getHistory();
573 }
574 }
575
576 History runInstance = new History();
577 getExecutor().execute(runInstance);
578 while (!runInstance.isDone()) {
alisiond295ec22013-05-17 10:12:13 -0400579// Log.w(TAG, "Waiting for getHistory");
alisione2a38e12013-04-25 14:20:20 -0400580 }
581 VectMap swigmap = (VectMap) runInstance.getVal();
582
583 ArrayList<HashMap<String, String>> nativemap = HistoryHandler.convertSwigToNative(swigmap);
584
585 return nativemap;
586 }
alision7f18fc82013-05-01 09:37:33 -0400587
alision43a9b362013-05-01 16:30:15 -0400588 /*************************
589 * Transfer related API
590 *************************/
591
alision7f18fc82013-05-01 09:37:33 -0400592 @Override
593 public void transfer(final String callID, final String to) throws RemoteException {
594 getExecutor().execute(new SipRunnable() {
595 @Override
596 protected void doRun() throws SameThreadException, RemoteException {
597 Log.i(TAG, "SipService.transfer() thread running...");
598 if (callManagerJNI.transfer(callID, to)) {
599 Bundle bundle = new Bundle();
600 bundle.putString("CallID", callID);
601 bundle.putString("State", "HUNGUP");
602 Intent intent = new Intent(CallManagerCallBack.CALL_STATE_CHANGED);
alision43a9b362013-05-01 16:30:15 -0400603 intent.putExtra(CallManagerCallBack.SIGNAL_NAME, CallManagerCallBack.CALL_STATE_CHANGED);
alision7f18fc82013-05-01 09:37:33 -0400604 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
605 client.callStateChanged(intent);
606 } else
607 Log.i(TAG, "NOT OK");
608 }
609 });
610
611 }
alision43a9b362013-05-01 16:30:15 -0400612
alision7f18fc82013-05-01 09:37:33 -0400613 @Override
614 public void attendedTransfer(final String transferID, final String targetID) throws RemoteException {
615 getExecutor().execute(new SipRunnable() {
616 @Override
617 protected void doRun() throws SameThreadException, RemoteException {
alision43a9b362013-05-01 16:30:15 -0400618 Log.i(TAG, "SipService.attendedTransfer() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400619 if (callManagerJNI.attendedTransfer(transferID, targetID)) {
620 Log.i(TAG, "OK");
alision7f18fc82013-05-01 09:37:33 -0400621 } else
622 Log.i(TAG, "NOT OK");
623 }
624 });
alision43a9b362013-05-01 16:30:15 -0400625
626 }
627
628 /*************************
629 * Conference related API
630 *************************/
631
632 @Override
633 public void removeConference(final String confID) throws RemoteException {
634 getExecutor().execute(new SipRunnable() {
635 @Override
636 protected void doRun() throws SameThreadException, RemoteException {
637 Log.i(TAG, "SipService.createConference() thread running...");
638 callManagerJNI.removeConference(confID);
639 }
640 });
641
alision7f18fc82013-05-01 09:37:33 -0400642 }
643
644 @Override
alision43a9b362013-05-01 16:30:15 -0400645 public void joinParticipant(final String sel_callID, final String drag_callID) throws RemoteException {
646 getExecutor().execute(new SipRunnable() {
647 @Override
648 protected void doRun() throws SameThreadException, RemoteException {
649 Log.i(TAG, "SipService.joinParticipant() thread running...");
650 callManagerJNI.joinParticipant(sel_callID, drag_callID);
651 }
652 });
653
alision7f18fc82013-05-01 09:37:33 -0400654 }
655
656 @Override
alision43a9b362013-05-01 16:30:15 -0400657 public void createConfFromParticipantList(final List participants) throws RemoteException {
658 getExecutor().execute(new SipRunnable() {
659 @Override
660 protected void doRun() throws SameThreadException, RemoteException {
661 Log.i(TAG, "SipService.createConfFromParticipantList() thread running...");
662 // callManagerJNI.createConfFromParticipantList(participants);
663 }
664 });
665
alision7f18fc82013-05-01 09:37:33 -0400666 }
667
668 @Override
alision43a9b362013-05-01 16:30:15 -0400669 public void addParticipant(final String callID, final String confID) throws RemoteException {
670 getExecutor().execute(new SipRunnable() {
671 @Override
672 protected void doRun() throws SameThreadException, RemoteException {
673 Log.i(TAG, "SipService.addParticipant() thread running...");
674 callManagerJNI.addParticipant(callID, confID);
675 }
676 });
677
alision7f18fc82013-05-01 09:37:33 -0400678 }
679
680 @Override
alision43a9b362013-05-01 16:30:15 -0400681 public void addMainParticipant(final String confID) throws RemoteException {
682 getExecutor().execute(new SipRunnable() {
683 @Override
684 protected void doRun() throws SameThreadException, RemoteException {
685 Log.i(TAG, "SipService.addMainParticipant() thread running...");
686 callManagerJNI.addMainParticipant(confID);
687 }
688 });
689
alision7f18fc82013-05-01 09:37:33 -0400690 }
691
692 @Override
alision43a9b362013-05-01 16:30:15 -0400693 public void detachParticipant(final String callID) throws RemoteException {
694 getExecutor().execute(new SipRunnable() {
695 @Override
696 protected void doRun() throws SameThreadException, RemoteException {
697 Log.i(TAG, "SipService.detachParticipant() thread running...");
698 callManagerJNI.detachParticipant(callID);
699 }
700 });
701
alision7f18fc82013-05-01 09:37:33 -0400702 }
703
704 @Override
alision43a9b362013-05-01 16:30:15 -0400705 public void joinConference(final String sel_confID, final String drag_confID) throws RemoteException {
706 getExecutor().execute(new SipRunnable() {
707 @Override
708 protected void doRun() throws SameThreadException, RemoteException {
709 Log.i(TAG, "SipService.joinConference() thread running...");
710 callManagerJNI.joinConference(sel_confID, drag_confID);
711 }
712 });
713
alision7f18fc82013-05-01 09:37:33 -0400714 }
715
716 @Override
alision43a9b362013-05-01 16:30:15 -0400717 public void hangUpConference(final String confID) throws RemoteException {
718 getExecutor().execute(new SipRunnable() {
719 @Override
720 protected void doRun() throws SameThreadException, RemoteException {
721 Log.i(TAG, "SipService.joinConference() thread running...");
722 callManagerJNI.hangUpConference(confID);
723 }
724 });
725
alision7f18fc82013-05-01 09:37:33 -0400726 }
727
728 @Override
alision43a9b362013-05-01 16:30:15 -0400729 public void holdConference(final String confID) throws RemoteException {
730 getExecutor().execute(new SipRunnable() {
731 @Override
732 protected void doRun() throws SameThreadException, RemoteException {
733 Log.i(TAG, "SipService.holdConference() thread running...");
734 callManagerJNI.holdConference(confID);
735 }
736 });
737
alision7f18fc82013-05-01 09:37:33 -0400738 }
739
740 @Override
alision43a9b362013-05-01 16:30:15 -0400741 public void unholdConference(final String confID) throws RemoteException {
742 getExecutor().execute(new SipRunnable() {
743 @Override
744 protected void doRun() throws SameThreadException, RemoteException {
745 Log.i(TAG, "SipService.unholdConference() thread running...");
746 callManagerJNI.unholdConference(confID);
747 }
748 });
749
alision7f18fc82013-05-01 09:37:33 -0400750 }
751
752 @Override
753 public List getConferenceList() throws RemoteException {
alision4a0eb092013-05-07 13:52:03 -0400754 class ConfList extends SipRunnableWithReturn {
755 @Override
756 protected StringVect doRun() throws SameThreadException {
757 Log.i(TAG, "SipService.getAccountList() thread running...");
758 return callManagerJNI.getConferenceList();
759 }
760 }
761 ;
762 ConfList runInstance = new ConfList();
763 getExecutor().execute(runInstance);
764 while (!runInstance.isDone()) {
alisiond295ec22013-05-17 10:12:13 -0400765// Log.w(TAG, "Waiting for getConferenceList");
alision4a0eb092013-05-07 13:52:03 -0400766 }
767 StringVect swigvect = (StringVect) runInstance.getVal();
768
769 ArrayList<String> nativelist = new ArrayList<String>();
770
771 for (int i = 0; i < swigvect.size(); i++)
772 nativelist.add(swigvect.get(i));
773
774 return nativelist;
alision7f18fc82013-05-01 09:37:33 -0400775 }
776
777 @Override
778 public List getParticipantList(String confID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400779 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400780 return null;
781 }
782
783 @Override
784 public String getConferenceId(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400785 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400786 return null;
787 }
788
789 @Override
790 public Map getConferenceDetails(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400791 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400792 return null;
793 }
794
alision04a00182013-05-10 17:05:29 -0400795 @Override
796 public String getRecordPath() throws RemoteException {
797 class RecordPath extends SipRunnableWithReturn {
798
799 @Override
800 protected String doRun() throws SameThreadException {
801 Log.i(TAG, "SipService.getRecordPath() thread running...");
802 return configurationManagerJNI.getRecordPath();
803 }
804 }
805
806 RecordPath runInstance = new RecordPath();
807 getExecutor().execute(runInstance);
808 while (!runInstance.isDone()) {
alisiond295ec22013-05-17 10:12:13 -0400809// Log.w(TAG, "Waiting for getRecordPath");
alision04a00182013-05-10 17:05:29 -0400810 }
811 String path = (String) runInstance.getVal();
812
813 return path;
814 }
815
816 @Override
817 public void setRecordingCall(final String id) throws RemoteException {
818 getExecutor().execute(new SipRunnable() {
819 @Override
820 protected void doRun() throws SameThreadException, RemoteException {
821 Log.i(TAG, "SipService.setRecordingCall() thread running...");
822 callManagerJNI.setRecordingCall(id);
823 }
824 });
825
826 }
827
828 @Override
829 public void setRecordPath(final String path) throws RemoteException {
830 getExecutor().execute(new SipRunnable() {
831 @Override
832 protected void doRun() throws SameThreadException, RemoteException {
833 Log.i(TAG, "SipService.setRecordingCall() thread running...");
834 configurationManagerJNI.setRecordPath(path);
835 }
836 });
837 }
838
839 @Override
840 public void sendTextMessage(final String callID, final String message, final String from) throws RemoteException {
841 getExecutor().execute(new SipRunnable() {
842 @Override
843 protected void doRun() throws SameThreadException, RemoteException {
844 Log.i(TAG, "SipService.sendTextMessage() thread running...");
845 callManagerJNI.sendTextMessage(callID, message, from);
846 }
847 });
848
849 }
850
alisiond295ec22013-05-17 10:12:13 -0400851 @Override
852 public List getAudioCodecList(String accountID) throws RemoteException {
853 class AudioCodecList extends SipRunnableWithReturn {
854
855 @Override
856 protected IntVect doRun() throws SameThreadException {
857 Log.i(TAG, "SipService.getAudioCodecList() thread running...");
858 return configurationManagerJNI.getAudioCodecList();
859 }
860 }
861
862 AudioCodecList runInstance = new AudioCodecList();
863 getExecutor().execute(runInstance);
864 while (!runInstance.isDone()) {
865 Log.w(TAG, "Waiting for getAudioCodecList");
866 }
867 IntVect swigmap = (IntVect) runInstance.getVal();
868
869 ArrayList<Integer> codecs = AudioHandler.convertSwigToNative(swigmap);
870
871 return codecs;
872 }
873
Emeric Vigier6119d782012-09-21 18:04:14 -0400874 };
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400875}