blob: 35a3b62b3977ceeeeace6d5542a6a0e886df4822 [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>
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 */
Emeric Vigiereaf2c492012-09-19 14:38:20 -040025package com.savoirfairelinux.sflphone.service;
26
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;
alision7f18fc82013-05-01 09:37:33 -040030import java.util.List;
alision17052d42013-04-22 10:39:38 -040031import java.util.Map;
Emeric Vigier6119d782012-09-21 18:04:14 -040032
Emeric Vigiereaf2c492012-09-19 14:38:20 -040033import android.app.Service;
alision17052d42013-04-22 10:39:38 -040034import android.content.BroadcastReceiver;
35import android.content.Context;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040036import android.content.Intent;
alision17052d42013-04-22 10:39:38 -040037import android.content.IntentFilter;
alision7f18fc82013-05-01 09:37:33 -040038import android.os.Bundle;
Emeric Vigier6119d782012-09-21 18:04:14 -040039import android.os.Handler;
40import android.os.HandlerThread;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040041import android.os.IBinder;
Emeric Vigier6119d782012-09-21 18:04:14 -040042import android.os.Looper;
43import android.os.Message;
alisionfde875f2013-05-28 17:01:54 -040044import android.os.Parcelable;
alision5f899632013-04-22 17:26:56 -040045import android.os.RemoteException;
alision43a9b362013-05-01 16:30:15 -040046import android.os.Vibrator;
alision17052d42013-04-22 10:39:38 -040047import android.support.v4.content.LocalBroadcastManager;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040048import android.util.Log;
49import android.widget.Toast;
50
alisionf76de3b2013-04-16 15:35:22 -040051import com.savoirfairelinux.sflphone.account.AccountDetailsHandler;
alisiond295ec22013-05-17 10:12:13 -040052import com.savoirfairelinux.sflphone.account.AudioHandler;
alisione2a38e12013-04-25 14:20:20 -040053import com.savoirfairelinux.sflphone.account.HistoryHandler;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040054import com.savoirfairelinux.sflphone.client.SFLphoneApplication;
alisionfde875f2013-05-28 17:01:54 -040055import com.savoirfairelinux.sflphone.model.CallContact;
56import com.savoirfairelinux.sflphone.model.SipCall;
Alexandre Savard713a34d2012-09-26 15:50:41 -040057
Emeric Vigiereaf2c492012-09-19 14:38:20 -040058public class SipService extends Service {
59
60 static final String TAG = "SipService";
61 static final int DELAY = 5000; /* 5 sec */
62 private boolean runFlag = false;
63 private SipServiceThread sipServiceThread;
Emeric Vigier84e05da2012-09-20 14:53:05 -040064 private SFLphoneApplication sflphoneApp;
Emeric Vigier6119d782012-09-21 18:04:14 -040065 private SipServiceExecutor mExecutor;
66 private static HandlerThread executorThread;
67 private CallManagerJNI callManagerJNI;
Emeric Vigier0007dee2012-09-24 11:35:58 -040068 private CallManagerCallBack callManagerCallBack;
Alexandre Savardc1b08fe2012-09-25 16:24:47 -040069 private ConfigurationManagerJNI configurationManagerJNI;
Alexandre Savardfccd1dc2012-10-17 17:31:38 -040070 private ConfigurationManagerCallback configurationManagerCallback;
Emeric Vigier0007dee2012-09-24 11:35:58 -040071 private ManagerImpl managerImpl;
Emeric Vigier6119d782012-09-21 18:04:14 -040072 private boolean isPjSipStackStarted = false;
alisionfde875f2013-05-28 17:01:54 -040073
74 HashMap<String, SipCall> current_calls = new HashMap<String, SipCall>();
alision04a00182013-05-10 17:05:29 -040075
Emeric Vigier6119d782012-09-21 18:04:14 -040076
alision43a9b362013-05-01 16:30:15 -040077 private BroadcastReceiver IncomingReceiver = new BroadcastReceiver() {
78
79 @Override
80 public void onReceive(Context context, Intent intent) {
81 // Get instance of Vibrator from current Context
82
alision84813a12013-05-27 17:40:39 -040083 if (intent.getAction().contentEquals(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED)) {
84 Log.i(TAG, "Received" + intent.getAction());
85 } else if (intent.getAction().contentEquals(ConfigurationManagerCallback.ACCOUNTS_LOADED)) {
86 Log.i(TAG, "Received" + intent.getAction());
87 } else if (intent.getAction().contentEquals(ConfigurationManagerCallback.ACCOUNTS_CHANGED)) {
88 Log.i(TAG, "Received" + intent.getAction());
89 } else if (intent.getAction().contentEquals(CallManagerCallBack.INCOMING_TEXT)) {
90 Log.i(TAG, "Received" + intent.getAction());
91 sendBroadcast(intent);
92 } else if (intent.getAction().contentEquals(CallManagerCallBack.INCOMING_CALL)) {
alisionfde875f2013-05-28 17:01:54 -040093 Bundle b = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newcall");
94
95 SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance();
96 callBuilder.startCallCreation(b.getString("CallID")).setAccountID(b.getString("AccountID")).setCallType(SipCall.state.CALL_TYPE_OUTGOING);
97 callBuilder.addContact(CallContact.ContactBuilder.buildUnknownContact(b.getString("From")));
98
alision85992112013-05-29 12:18:08 -040099 Intent toSend = new Intent(CallManagerCallBack.INCOMING_CALL);
alisionfde875f2013-05-28 17:01:54 -0400100 try {
101 SipCall newCall = callBuilder.build();
102 toSend.putExtra("newcall",newCall);
103 current_calls.put(newCall.getCallId(), newCall);
104 sendBroadcast(toSend);
105 } catch (Exception e) {
106 Log.e(TAG, e.toString());
107 }
108
109
alision84813a12013-05-27 17:40:39 -0400110 } else if (intent.getAction().contentEquals(CallManagerCallBack.CALL_STATE_CHANGED)) {
alisionfde875f2013-05-28 17:01:54 -0400111
112 Bundle b = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
113 String newState = b.getString("State");
114 if (newState.equals("INCOMING")) {
115 current_calls.get(b.getString("CallID")).setmCallState(SipCall.state.CALL_STATE_INCOMING);
116 } else if (newState.equals("RINGING")) {
117 current_calls.get(b.getString("CallID")).setmCallState(SipCall.state.CALL_STATE_RINGING);
118 } else if (newState.equals("CURRENT")) {
119 current_calls.get(b.getString("CallID")).setmCallState(SipCall.state.CALL_STATE_CURRENT);
120 } else if (newState.equals("HUNGUP")) {
121 current_calls.remove(b.getString("CallID"));
122 } else if (newState.equals("BUSY")) {
123 current_calls.remove(b.getString("CallID"));
124 } else if (newState.equals("FAILURE")) {
125 current_calls.remove(b.getString("CallID"));
126 } else if (newState.equals("HOLD")) {
127 current_calls.get(b.getString("CallID")).setmCallState(SipCall.state.CALL_STATE_HOLD);
128 } else if (newState.equals("UNHOLD")) {
129 current_calls.get(b.getString("CallID")).setmCallState(SipCall.state.CALL_STATE_CURRENT);
130 } else {
131 current_calls.get(b.getString("CallID")).setmCallState(SipCall.state.CALL_STATE_NONE);
132 }
133
alision84813a12013-05-27 17:40:39 -0400134 sendBroadcast(intent);
135 } else if (intent.getAction().contentEquals(CallManagerCallBack.NEW_CALL_CREATED)) {
136 Log.i(TAG, "Received" + intent.getAction());
137 Vibrator mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
138 mVibrator.vibrate(300);
alision43a9b362013-05-01 16:30:15 -0400139 }
140
141 }
142 };
143
144 @Override
145 public boolean onUnbind(Intent i) {
146 super.onUnbind(i);
147 Log.i(TAG, "onUnbind(intent)");
148 return false;
149
150 }
151
152 /* called once by startService() */
153 @Override
154 public void onCreate() {
155 Log.i(TAG, "onCreated");
156 super.onCreate();
157
158 sflphoneApp = (SFLphoneApplication) getApplication();
159 sipServiceThread = new SipServiceThread();
160
161 IntentFilter callFilter = new IntentFilter(CallManagerCallBack.CALL_STATE_CHANGED);
162 callFilter.addAction(CallManagerCallBack.INCOMING_CALL);
163 callFilter.addAction(CallManagerCallBack.NEW_CALL_CREATED);
alision4a0eb092013-05-07 13:52:03 -0400164 callFilter.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
165 callFilter.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
166 callFilter.addAction(ConfigurationManagerCallback.ACCOUNTS_LOADED);
alision04a00182013-05-10 17:05:29 -0400167 callFilter.addAction(CallManagerCallBack.INCOMING_TEXT);
168
alision43a9b362013-05-01 16:30:15 -0400169 LocalBroadcastManager.getInstance(this).registerReceiver(IncomingReceiver, callFilter);
170 getExecutor().execute(new StartRunnable());
171 }
172
173 /* called for each startService() */
174 @Override
175 public int onStartCommand(Intent intent, int flags, int startId) {
176 Log.i(TAG, "onStarted");
177 super.onStartCommand(intent, flags, startId);
178
179 if (!runFlag) {
180 sipServiceThread.start();
181 runFlag = true;
182 sflphoneApp.setServiceRunning(true);
183 Toast.makeText(this, "Sflphone Service started", Toast.LENGTH_SHORT).show();
184 }
185
186 return START_STICKY; /* started and stopped explicitly */
187 }
188
189 @Override
190 public void onDestroy() {
191 /* called once by stopService() */
192 sipServiceThread.interrupt();
193 sipServiceThread = null;
194 runFlag = false;
195 sflphoneApp.setServiceRunning(false);
196 Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show();
197 super.onDestroy();
198
199 Log.i(TAG, "onDestroyed");
200 }
201
202 @Override
203 public IBinder onBind(Intent arg0) {
204 Log.i(TAG, "onBound");
205 return mBinder;
206 }
207
208 private static Looper createLooper() {
209 if (executorThread == null) {
210 Log.d(TAG, "Creating new handler thread");
211 // ADT gives a fake warning due to bad parse rule.
212 executorThread = new HandlerThread("SipService.Executor");
213 executorThread.start();
214 }
215 return executorThread.getLooper();
216 }
217
218 public SipServiceExecutor getExecutor() {
219 // create mExecutor lazily
220 if (mExecutor == null) {
221 mExecutor = new SipServiceExecutor(this);
222 }
223 return mExecutor;
224 }
225
226 // Executes immediate tasks in a single executorThread.
227 public static class SipServiceExecutor extends Handler {
228 WeakReference<SipService> handlerService;
229
230 SipServiceExecutor(SipService s) {
231 super(createLooper());
232 handlerService = new WeakReference<SipService>(s);
233 }
234
235 public void execute(Runnable task) {
236 // TODO: add wakelock
237 Message.obtain(this, 0/* don't care */, task).sendToTarget();
238 }
239
240 @Override
241 public void handleMessage(Message msg) {
242 if (msg.obj instanceof Runnable) {
243 executeInternal((Runnable) msg.obj);
244 } else {
245 Log.w(TAG, "can't handle msg: " + msg);
246 }
247 }
248
249 private void executeInternal(Runnable task) {
250 try {
251 task.run();
252 } catch (Throwable t) {
253 Log.e(TAG, "run task: " + task, t);
254 }
255 }
256 }
257
258 private void startPjSipStack() throws SameThreadException {
259 if (isPjSipStackStarted)
260 return;
261
262 try {
263 System.loadLibrary("gnustl_shared");
264 System.loadLibrary("expat");
265 System.loadLibrary("yaml");
266 System.loadLibrary("ccgnu2");
267 System.loadLibrary("crypto");
268 System.loadLibrary("ssl");
269 System.loadLibrary("ccrtp1");
270 System.loadLibrary("dbus");
271 System.loadLibrary("dbus-c++-1");
272 System.loadLibrary("samplerate");
273 System.loadLibrary("codec_ulaw");
274 System.loadLibrary("codec_alaw");
alisiond8c83882013-05-17 17:00:42 -0400275 System.loadLibrary("codec_g722");
alision43a9b362013-05-01 16:30:15 -0400276 System.loadLibrary("speexresampler");
277 System.loadLibrary("sflphone");
278 isPjSipStackStarted = true;
279 } catch (UnsatisfiedLinkError e) {
280 Log.e(TAG, "Problem with the current Pj stack...", e);
281 isPjSipStackStarted = false;
282 return;
283 } catch (Exception e) {
284 Log.e(TAG, "Problem with the current Pj stack...", e);
285 }
286
287 /* get unique instance of managerImpl */
288 managerImpl = SFLPhoneservice.instance();
289
290 /* set static AppPath before calling manager.init */
291 managerImpl.setPath(sflphoneApp.getAppPath());
292
293 callManagerJNI = new CallManagerJNI();
294 callManagerCallBack = new CallManagerCallBack(this);
295 SFLPhoneservice.setCallbackObject(callManagerCallBack);
296
297 configurationManagerJNI = new ConfigurationManagerJNI();
298 configurationManagerCallback = new ConfigurationManagerCallback(this);
299 SFLPhoneservice.setConfigurationCallbackObject(configurationManagerCallback);
300
301 managerImpl.init("");
302 return;
303 }
304
305 // Enforce same thread contract to ensure we do not call from somewhere else
306 public class SameThreadException extends Exception {
307 private static final long serialVersionUID = -905639124232613768L;
308
309 public SameThreadException() {
310 super("Should be launched from a single worker thread");
311 }
312 }
313
314 public abstract static class SipRunnable implements Runnable {
315 protected abstract void doRun() throws SameThreadException, RemoteException;
316
317 public void run() {
318 try {
319 doRun();
320 } catch (SameThreadException e) {
321 Log.e(TAG, "Not done from same thread");
322 } catch (RemoteException e) {
323 Log.e(TAG, e.toString());
324 }
325 }
326 }
327
328 public abstract static class SipRunnableWithReturn implements Runnable {
329 Object obj = null;
330 boolean done = false;
331
332 protected abstract Object doRun() throws SameThreadException;
333
334 public Object getVal() {
335 return obj;
336 }
337
338 public boolean isDone() {
339 return done;
340 }
341
342 public void run() {
343 try {
344 obj = doRun();
345 done = true;
346 } catch (SameThreadException e) {
347 Log.e(TAG, "Not done from same thread");
348 }
349 }
350 }
351
352 class StartRunnable extends SipRunnable {
353 @Override
354 protected void doRun() throws SameThreadException {
355 startPjSipStack();
356 }
357 }
358
359 private class SipServiceThread extends Thread {
360
361 public SipServiceThread() {
362 super("sipServiceThread");
363 }
364
365 @Override
366 public void run() {
367 Log.i(TAG, "SipService thread running...");
368 SipService sipService = SipService.this;
369 while (sipService.runFlag) {
370 try {
371 Thread.sleep(DELAY);
372 } catch (InterruptedException e) {
373 sipService.runFlag = false;
374 Log.w(TAG, "service thread interrupted!");
375 }
376 }
377 }
378 }
379
380 /* ************************************
381 *
382 * Implement public interface for the service
383 *
384 *
385 * **********************************
386 */
Emeric Vigier6119d782012-09-21 18:04:14 -0400387 private final ISipService.Stub mBinder = new ISipService.Stub() {
388
389 @Override
alisionfde875f2013-05-28 17:01:54 -0400390 public void placeCall(final SipCall call) {
Emeric Vigier6119d782012-09-21 18:04:14 -0400391 getExecutor().execute(new SipRunnable() {
392 @Override
393 protected void doRun() throws SameThreadException {
394 Log.i(TAG, "SipService.placeCall() thread running...");
alisionfde875f2013-05-28 17:01:54 -0400395 callManagerJNI.placeCall(call.getAccountID(), call.getCallId(), call.getContacts().get(0).getPhones().get(0).getNumber());
396 current_calls.put(call.getCallId(), call);
Emeric Vigier6119d782012-09-21 18:04:14 -0400397 }
398 });
399 }
400
401 @Override
402 public void refuse(final String callID) {
403 getExecutor().execute(new SipRunnable() {
404 @Override
405 protected void doRun() throws SameThreadException {
406 Log.i(TAG, "SipService.refuse() thread running...");
407 callManagerJNI.refuse(callID);
408 }
409 });
410 }
411
412 @Override
413 public void accept(final String callID) {
414 getExecutor().execute(new SipRunnable() {
415 @Override
416 protected void doRun() throws SameThreadException {
417 Log.i(TAG, "SipService.placeCall() thread running...");
418 callManagerJNI.accept(callID);
419 }
420 });
421 }
422
423 @Override
424 public void hangUp(final String callID) {
425 getExecutor().execute(new SipRunnable() {
426 @Override
427 protected void doRun() throws SameThreadException {
428 Log.i(TAG, "SipService.hangUp() thread running...");
429 callManagerJNI.hangUp(callID);
430 }
431 });
432 }
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400433
434 @Override
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400435 public void hold(final String callID) {
436 getExecutor().execute(new SipRunnable() {
437 @Override
438 protected void doRun() throws SameThreadException {
439 Log.i(TAG, "SipService.hold() thread running...");
440 callManagerJNI.hold(callID);
441 }
442 });
443 }
444
445 @Override
446 public void unhold(final String callID) {
447 getExecutor().execute(new SipRunnable() {
448 @Override
449 protected void doRun() throws SameThreadException {
450 Log.i(TAG, "SipService.unhold() thread running...");
451 callManagerJNI.unhold(callID);
452 }
453 });
454 }
455
456 @Override
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400457 public void setAudioPlugin(final String audioPlugin) {
458 getExecutor().execute(new SipRunnable() {
alision371b77e2013-04-23 14:51:26 -0400459 @Override
460 protected void doRun() throws SameThreadException {
461 Log.i(TAG, "SipService.setAudioPlugin() thread running...");
462 configurationManagerJNI.setAudioPlugin(audioPlugin);
463 }
Alexandre Savard31d27c62012-10-04 16:05:08 -0400464 });
465 }
466
467 @Override
468 public String getCurrentAudioOutputPlugin() {
469 class CurrentAudioPlugin extends SipRunnableWithReturn {
470 @Override
471 protected String doRun() throws SameThreadException {
472 Log.i(TAG, "SipService.getCurrentAudioOutputPlugin() thread running...");
473 return configurationManagerJNI.getCurrentAudioOutputPlugin();
474 }
alision371b77e2013-04-23 14:51:26 -0400475 }
476 ;
Alexandre Savard31d27c62012-10-04 16:05:08 -0400477
478 CurrentAudioPlugin runInstance = new CurrentAudioPlugin();
479 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400480 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400481 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400482 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400483 return (String) runInstance.getVal();
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400484 }
Alexandre Savard713a34d2012-09-26 15:50:41 -0400485
486 @Override
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400487 public ArrayList<String> getAccountList() {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400488 class AccountList extends SipRunnableWithReturn {
489 @Override
490 protected StringVect doRun() throws SameThreadException {
491 Log.i(TAG, "SipService.getAccountList() thread running...");
492 return configurationManagerJNI.getAccountList();
493 }
alision371b77e2013-04-23 14:51:26 -0400494 }
495 ;
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400496 AccountList runInstance = new AccountList();
497 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400498 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400499 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400500 }
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400501 StringVect swigvect = (StringVect) runInstance.getVal();
502
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400503 ArrayList<String> nativelist = new ArrayList<String>();
Alexandre Savard52a72522012-09-27 16:40:13 -0400504
alision371b77e2013-04-23 14:51:26 -0400505 for (int i = 0; i < swigvect.size(); i++)
506 nativelist.add(swigvect.get(i));
Alexandre Savard52a72522012-09-27 16:40:13 -0400507
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400508 return nativelist;
alision371b77e2013-04-23 14:51:26 -0400509 }
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400510
511 @Override
alision371b77e2013-04-23 14:51:26 -0400512 public HashMap<String, String> getAccountDetails(final String accountID) {
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400513 class AccountDetails extends SipRunnableWithReturn {
514 private String id;
alision371b77e2013-04-23 14:51:26 -0400515
516 AccountDetails(String accountId) {
517 id = accountId;
518 }
519
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400520 @Override
521 protected StringMap doRun() throws SameThreadException {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400522 Log.i(TAG, "SipService.getAccountDetails() thread running...");
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400523 return configurationManagerJNI.getAccountDetails(id);
524 }
alision371b77e2013-04-23 14:51:26 -0400525 }
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400526
527 AccountDetails runInstance = new AccountDetails(accountID);
528 getExecutor().execute(runInstance);
alisionfde875f2013-05-28 17:01:54 -0400529
alision371b77e2013-04-23 14:51:26 -0400530 while (!runInstance.isDone()) {
531 }
532 StringMap swigmap = (StringMap) runInstance.getVal();
Alexandre Savard713a34d2012-09-26 15:50:41 -0400533
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400534 HashMap<String, String> nativemap = AccountDetailsHandler.convertSwigToNative(swigmap);
Alexandre Savard713a34d2012-09-26 15:50:41 -0400535
536 return nativemap;
537 }
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400538
539 @Override
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400540 public void setAccountDetails(final String accountId, Map map) {
alision371b77e2013-04-23 14:51:26 -0400541 HashMap<String, String> nativemap = (HashMap<String, String>) map;
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400542
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400543 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig(nativemap);
Alexandre Savard718d49f2012-10-02 15:17:13 -0400544
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400545 getExecutor().execute(new SipRunnable() {
546 @Override
547 protected void doRun() throws SameThreadException {
548 Log.i(TAG, "SipService.setAccountDetails() thread running...");
549 configurationManagerJNI.setAccountDetails(accountId, swigmap);
550 }
551 });
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400552 }
553
Alexandre Savard46036572012-10-05 13:56:49 -0400554 @Override
555 public String addAccount(Map map) {
556 class AddAccount extends SipRunnableWithReturn {
557 StringMap map;
alision371b77e2013-04-23 14:51:26 -0400558
559 AddAccount(StringMap m) {
560 map = m;
561 }
562
Alexandre Savard46036572012-10-05 13:56:49 -0400563 @Override
564 protected String doRun() throws SameThreadException {
565 Log.i(TAG, "SipService.getAccountDetails() thread running...");
566 return configurationManagerJNI.addAccount(map);
567 }
alision371b77e2013-04-23 14:51:26 -0400568 }
Alexandre Savard46036572012-10-05 13:56:49 -0400569
alision371b77e2013-04-23 14:51:26 -0400570 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig((HashMap<String, String>) map);
Alexandre Savard46036572012-10-05 13:56:49 -0400571
572 AddAccount runInstance = new AddAccount(swigmap);
573 getExecutor().execute(runInstance);
alision371b77e2013-04-23 14:51:26 -0400574 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400575 // Log.e(TAG, "Waiting for Nofing");
alision371b77e2013-04-23 14:51:26 -0400576 }
Alexandre Savard46036572012-10-05 13:56:49 -0400577 String accountId = (String) runInstance.getVal();
578
579 return accountId;
580 }
581
582 @Override
583 public void removeAccount(final String accountId) {
584 getExecutor().execute(new SipRunnable() {
585 @Override
586 protected void doRun() throws SameThreadException {
587 Log.i(TAG, "SipService.setAccountDetails() thread running...");
588 configurationManagerJNI.removeAccount(accountId);
589 }
590 });
591 }
alision5f899632013-04-22 17:26:56 -0400592
alision84813a12013-05-27 17:40:39 -0400593
alisione2a38e12013-04-25 14:20:20 -0400594 @Override
595 public ArrayList<HashMap<String, String>> getHistory() throws RemoteException {
596 class History extends SipRunnableWithReturn {
597
598 @Override
599 protected VectMap doRun() throws SameThreadException {
600 Log.i(TAG, "SipService.getHistory() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400601
alisione2a38e12013-04-25 14:20:20 -0400602 return configurationManagerJNI.getHistory();
603 }
604 }
605
606 History runInstance = new History();
607 getExecutor().execute(runInstance);
608 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400609 // Log.w(TAG, "Waiting for getHistory");
alisione2a38e12013-04-25 14:20:20 -0400610 }
611 VectMap swigmap = (VectMap) runInstance.getVal();
612
613 ArrayList<HashMap<String, String>> nativemap = HistoryHandler.convertSwigToNative(swigmap);
614
615 return nativemap;
616 }
alision7f18fc82013-05-01 09:37:33 -0400617
alision43a9b362013-05-01 16:30:15 -0400618 /*************************
619 * Transfer related API
620 *************************/
621
alision7f18fc82013-05-01 09:37:33 -0400622 @Override
623 public void transfer(final String callID, final String to) throws RemoteException {
624 getExecutor().execute(new SipRunnable() {
625 @Override
626 protected void doRun() throws SameThreadException, RemoteException {
627 Log.i(TAG, "SipService.transfer() thread running...");
628 if (callManagerJNI.transfer(callID, to)) {
629 Bundle bundle = new Bundle();
630 bundle.putString("CallID", callID);
631 bundle.putString("State", "HUNGUP");
632 Intent intent = new Intent(CallManagerCallBack.CALL_STATE_CHANGED);
alision43a9b362013-05-01 16:30:15 -0400633 intent.putExtra(CallManagerCallBack.SIGNAL_NAME, CallManagerCallBack.CALL_STATE_CHANGED);
alision7f18fc82013-05-01 09:37:33 -0400634 intent.putExtra("com.savoirfairelinux.sflphone.service.newstate", bundle);
alision84813a12013-05-27 17:40:39 -0400635 sendBroadcast(intent);
alision7f18fc82013-05-01 09:37:33 -0400636 } else
637 Log.i(TAG, "NOT OK");
638 }
639 });
640
641 }
alision43a9b362013-05-01 16:30:15 -0400642
alision7f18fc82013-05-01 09:37:33 -0400643 @Override
644 public void attendedTransfer(final String transferID, final String targetID) throws RemoteException {
645 getExecutor().execute(new SipRunnable() {
646 @Override
647 protected void doRun() throws SameThreadException, RemoteException {
alision43a9b362013-05-01 16:30:15 -0400648 Log.i(TAG, "SipService.attendedTransfer() thread running...");
alision7f18fc82013-05-01 09:37:33 -0400649 if (callManagerJNI.attendedTransfer(transferID, targetID)) {
650 Log.i(TAG, "OK");
alision7f18fc82013-05-01 09:37:33 -0400651 } else
652 Log.i(TAG, "NOT OK");
653 }
654 });
alision43a9b362013-05-01 16:30:15 -0400655
656 }
657
658 /*************************
659 * Conference related API
660 *************************/
661
662 @Override
663 public void removeConference(final String confID) throws RemoteException {
664 getExecutor().execute(new SipRunnable() {
665 @Override
666 protected void doRun() throws SameThreadException, RemoteException {
667 Log.i(TAG, "SipService.createConference() thread running...");
668 callManagerJNI.removeConference(confID);
669 }
670 });
671
alision7f18fc82013-05-01 09:37:33 -0400672 }
673
674 @Override
alision43a9b362013-05-01 16:30:15 -0400675 public void joinParticipant(final String sel_callID, final String drag_callID) throws RemoteException {
676 getExecutor().execute(new SipRunnable() {
677 @Override
678 protected void doRun() throws SameThreadException, RemoteException {
679 Log.i(TAG, "SipService.joinParticipant() thread running...");
680 callManagerJNI.joinParticipant(sel_callID, drag_callID);
681 }
682 });
683
alision7f18fc82013-05-01 09:37:33 -0400684 }
685
686 @Override
alision43a9b362013-05-01 16:30:15 -0400687 public void createConfFromParticipantList(final List participants) throws RemoteException {
688 getExecutor().execute(new SipRunnable() {
689 @Override
690 protected void doRun() throws SameThreadException, RemoteException {
691 Log.i(TAG, "SipService.createConfFromParticipantList() thread running...");
692 // callManagerJNI.createConfFromParticipantList(participants);
693 }
694 });
695
alision7f18fc82013-05-01 09:37:33 -0400696 }
697
698 @Override
alision43a9b362013-05-01 16:30:15 -0400699 public void addParticipant(final String callID, final String confID) throws RemoteException {
700 getExecutor().execute(new SipRunnable() {
701 @Override
702 protected void doRun() throws SameThreadException, RemoteException {
703 Log.i(TAG, "SipService.addParticipant() thread running...");
704 callManagerJNI.addParticipant(callID, confID);
705 }
706 });
707
alision7f18fc82013-05-01 09:37:33 -0400708 }
709
710 @Override
alision43a9b362013-05-01 16:30:15 -0400711 public void addMainParticipant(final String confID) throws RemoteException {
712 getExecutor().execute(new SipRunnable() {
713 @Override
714 protected void doRun() throws SameThreadException, RemoteException {
715 Log.i(TAG, "SipService.addMainParticipant() thread running...");
716 callManagerJNI.addMainParticipant(confID);
717 }
718 });
719
alision7f18fc82013-05-01 09:37:33 -0400720 }
721
722 @Override
alision43a9b362013-05-01 16:30:15 -0400723 public void detachParticipant(final String callID) throws RemoteException {
724 getExecutor().execute(new SipRunnable() {
725 @Override
726 protected void doRun() throws SameThreadException, RemoteException {
727 Log.i(TAG, "SipService.detachParticipant() thread running...");
728 callManagerJNI.detachParticipant(callID);
729 }
730 });
731
alision7f18fc82013-05-01 09:37:33 -0400732 }
733
734 @Override
alision43a9b362013-05-01 16:30:15 -0400735 public void joinConference(final String sel_confID, final String drag_confID) throws RemoteException {
736 getExecutor().execute(new SipRunnable() {
737 @Override
738 protected void doRun() throws SameThreadException, RemoteException {
739 Log.i(TAG, "SipService.joinConference() thread running...");
740 callManagerJNI.joinConference(sel_confID, drag_confID);
741 }
742 });
743
alision7f18fc82013-05-01 09:37:33 -0400744 }
745
746 @Override
alision43a9b362013-05-01 16:30:15 -0400747 public void hangUpConference(final String confID) throws RemoteException {
748 getExecutor().execute(new SipRunnable() {
749 @Override
750 protected void doRun() throws SameThreadException, RemoteException {
751 Log.i(TAG, "SipService.joinConference() thread running...");
752 callManagerJNI.hangUpConference(confID);
753 }
754 });
755
alision7f18fc82013-05-01 09:37:33 -0400756 }
757
758 @Override
alision43a9b362013-05-01 16:30:15 -0400759 public void holdConference(final String confID) throws RemoteException {
760 getExecutor().execute(new SipRunnable() {
761 @Override
762 protected void doRun() throws SameThreadException, RemoteException {
763 Log.i(TAG, "SipService.holdConference() thread running...");
764 callManagerJNI.holdConference(confID);
765 }
766 });
767
alision7f18fc82013-05-01 09:37:33 -0400768 }
769
770 @Override
alision43a9b362013-05-01 16:30:15 -0400771 public void unholdConference(final String confID) throws RemoteException {
772 getExecutor().execute(new SipRunnable() {
773 @Override
774 protected void doRun() throws SameThreadException, RemoteException {
775 Log.i(TAG, "SipService.unholdConference() thread running...");
776 callManagerJNI.unholdConference(confID);
777 }
778 });
779
alision7f18fc82013-05-01 09:37:33 -0400780 }
781
782 @Override
783 public List getConferenceList() throws RemoteException {
alision4a0eb092013-05-07 13:52:03 -0400784 class ConfList extends SipRunnableWithReturn {
785 @Override
786 protected StringVect doRun() throws SameThreadException {
787 Log.i(TAG, "SipService.getAccountList() thread running...");
788 return callManagerJNI.getConferenceList();
789 }
790 }
791 ;
792 ConfList runInstance = new ConfList();
793 getExecutor().execute(runInstance);
794 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400795 // Log.w(TAG, "Waiting for getConferenceList");
alision4a0eb092013-05-07 13:52:03 -0400796 }
797 StringVect swigvect = (StringVect) runInstance.getVal();
798
799 ArrayList<String> nativelist = new ArrayList<String>();
800
801 for (int i = 0; i < swigvect.size(); i++)
802 nativelist.add(swigvect.get(i));
803
804 return nativelist;
alision7f18fc82013-05-01 09:37:33 -0400805 }
806
807 @Override
808 public List getParticipantList(String confID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400809 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400810 return null;
811 }
812
813 @Override
814 public String getConferenceId(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400815 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400816 return null;
817 }
818
819 @Override
820 public Map getConferenceDetails(String callID) throws RemoteException {
alision43a9b362013-05-01 16:30:15 -0400821 Log.e(TAG, "getConferenceList not implemented");
alision7f18fc82013-05-01 09:37:33 -0400822 return null;
823 }
824
alision04a00182013-05-10 17:05:29 -0400825 @Override
826 public String getRecordPath() throws RemoteException {
827 class RecordPath extends SipRunnableWithReturn {
828
829 @Override
830 protected String doRun() throws SameThreadException {
831 Log.i(TAG, "SipService.getRecordPath() thread running...");
832 return configurationManagerJNI.getRecordPath();
833 }
834 }
835
836 RecordPath runInstance = new RecordPath();
837 getExecutor().execute(runInstance);
838 while (!runInstance.isDone()) {
alision84813a12013-05-27 17:40:39 -0400839 // Log.w(TAG, "Waiting for getRecordPath");
alision04a00182013-05-10 17:05:29 -0400840 }
841 String path = (String) runInstance.getVal();
842
843 return path;
844 }
845
846 @Override
847 public void setRecordingCall(final String id) throws RemoteException {
848 getExecutor().execute(new SipRunnable() {
849 @Override
850 protected void doRun() throws SameThreadException, RemoteException {
851 Log.i(TAG, "SipService.setRecordingCall() thread running...");
852 callManagerJNI.setRecordingCall(id);
853 }
854 });
855
856 }
857
858 @Override
859 public void setRecordPath(final String path) throws RemoteException {
860 getExecutor().execute(new SipRunnable() {
861 @Override
862 protected void doRun() throws SameThreadException, RemoteException {
863 Log.i(TAG, "SipService.setRecordingCall() thread running...");
864 configurationManagerJNI.setRecordPath(path);
865 }
866 });
867 }
868
869 @Override
870 public void sendTextMessage(final String callID, final String message, final String from) throws RemoteException {
871 getExecutor().execute(new SipRunnable() {
872 @Override
873 protected void doRun() throws SameThreadException, RemoteException {
874 Log.i(TAG, "SipService.sendTextMessage() thread running...");
875 callManagerJNI.sendTextMessage(callID, message, from);
876 }
877 });
878
879 }
880
alisiond295ec22013-05-17 10:12:13 -0400881 @Override
882 public List getAudioCodecList(String accountID) throws RemoteException {
883 class AudioCodecList extends SipRunnableWithReturn {
884
885 @Override
886 protected IntVect doRun() throws SameThreadException {
887 Log.i(TAG, "SipService.getAudioCodecList() thread running...");
888 return configurationManagerJNI.getAudioCodecList();
889 }
890 }
891
892 AudioCodecList runInstance = new AudioCodecList();
893 getExecutor().execute(runInstance);
894 while (!runInstance.isDone()) {
895 Log.w(TAG, "Waiting for getAudioCodecList");
896 }
897 IntVect swigmap = (IntVect) runInstance.getVal();
898
899 ArrayList<Integer> codecs = AudioHandler.convertSwigToNative(swigmap);
900
901 return codecs;
902 }
903
alision9f7a6ec2013-05-24 16:26:26 -0400904 @Override
alisionfde875f2013-05-28 17:01:54 -0400905 public HashMap<String, SipCall> getCallList() throws RemoteException {
906// class CallList extends SipRunnableWithReturn {
907//
908// @Override
909// protected StringVect doRun() throws SameThreadException {
910// Log.i(TAG, "SipService.getCallList() thread running...");
911// return callManagerJNI.getCallList();
912// }
913// }
914//
915// CallList runInstance = new CallList();
916// getExecutor().execute(runInstance);
917// while (!runInstance.isDone()) {
918// Log.w(TAG, "Waiting for getAudioCodecList");
919// }
920// StringVect swigmap = (StringVect) runInstance.getVal();
921//
922// ArrayList<String> nativemap = new ArrayList<String>();
923// for (int i = 0; i < swigmap.size(); ++i) {
924//
925// String t = swigmap.get(i);
926// nativemap.add(t);
927// }
alision9f7a6ec2013-05-24 16:26:26 -0400928
alisionfde875f2013-05-28 17:01:54 -0400929 return current_calls;
alision9f7a6ec2013-05-24 16:26:26 -0400930 }
931
Emeric Vigier6119d782012-09-21 18:04:14 -0400932 };
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400933}