Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 1 | /** |
| 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 Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 24 | package com.savoirfairelinux.sflphone.service; |
| 25 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 26 | import java.lang.ref.WeakReference; |
| 27 | |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 28 | import android.app.Service; |
| 29 | import android.content.Intent; |
| 30 | import android.os.Binder; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.HandlerThread; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 33 | import android.os.IBinder; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 34 | import android.os.Looper; |
| 35 | import android.os.Message; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.widget.Toast; |
| 38 | |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 39 | import com.savoirfairelinux.sflphone.service.ManagerImpl; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 40 | import com.savoirfairelinux.sflphone.client.SFLphoneApplication; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 41 | import com.savoirfairelinux.sflphone.service.ISipService; |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 42 | import com.savoirfairelinux.sflphone.utils.AccountDetailsHandler; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 43 | |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 44 | import java.util.Map; |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 45 | import java.util.HashMap; |
Alexandre Savard | 6b85e7e | 2012-09-27 15:43:14 -0400 | [diff] [blame] | 46 | import java.util.ArrayList; |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 47 | |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 48 | public class SipService extends Service { |
| 49 | |
| 50 | static final String TAG = "SipService"; |
| 51 | static final int DELAY = 5000; /* 5 sec */ |
| 52 | private boolean runFlag = false; |
| 53 | private SipServiceThread sipServiceThread; |
Emeric Vigier | 84e05da | 2012-09-20 14:53:05 -0400 | [diff] [blame] | 54 | private SFLphoneApplication sflphoneApp; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 55 | private SipServiceExecutor mExecutor; |
| 56 | private static HandlerThread executorThread; |
| 57 | private CallManagerJNI callManagerJNI; |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 58 | private CallManagerCallBack callManagerCallBack; |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 59 | private ConfigurationManagerJNI configurationManagerJNI; |
Alexandre Savard | fccd1dc | 2012-10-17 17:31:38 -0400 | [diff] [blame] | 60 | private ConfigurationManagerCallback configurationManagerCallback; |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 61 | private ManagerImpl managerImpl; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 62 | private boolean isPjSipStackStarted = false; |
Alexandre Savard | 4f11d7a | 2012-10-18 13:15:36 -0400 | [diff] [blame] | 63 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 64 | |
| 65 | /* Implement public interface for the service */ |
| 66 | private final ISipService.Stub mBinder = new ISipService.Stub() { |
| 67 | |
| 68 | @Override |
| 69 | public void placeCall(final String accountID, final String callID, final String to) { |
| 70 | getExecutor().execute(new SipRunnable() { |
| 71 | @Override |
| 72 | protected void doRun() throws SameThreadException { |
| 73 | Log.i(TAG, "SipService.placeCall() thread running..."); |
| 74 | callManagerJNI.placeCall(accountID, callID, to); |
| 75 | } |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void refuse(final String callID) { |
| 81 | getExecutor().execute(new SipRunnable() { |
| 82 | @Override |
| 83 | protected void doRun() throws SameThreadException { |
| 84 | Log.i(TAG, "SipService.refuse() thread running..."); |
| 85 | callManagerJNI.refuse(callID); |
| 86 | } |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void accept(final String callID) { |
| 92 | getExecutor().execute(new SipRunnable() { |
| 93 | @Override |
| 94 | protected void doRun() throws SameThreadException { |
| 95 | Log.i(TAG, "SipService.placeCall() thread running..."); |
| 96 | callManagerJNI.accept(callID); |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void hangUp(final String callID) { |
| 103 | getExecutor().execute(new SipRunnable() { |
| 104 | @Override |
| 105 | protected void doRun() throws SameThreadException { |
| 106 | Log.i(TAG, "SipService.hangUp() thread running..."); |
| 107 | callManagerJNI.hangUp(callID); |
| 108 | } |
| 109 | }); |
| 110 | } |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 111 | |
| 112 | @Override |
Alexandre Savard | e9dc899 | 2012-10-26 12:12:27 -0400 | [diff] [blame] | 113 | public void hold(final String callID) { |
| 114 | getExecutor().execute(new SipRunnable() { |
| 115 | @Override |
| 116 | protected void doRun() throws SameThreadException { |
| 117 | Log.i(TAG, "SipService.hold() thread running..."); |
| 118 | callManagerJNI.hold(callID); |
| 119 | } |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void unhold(final String callID) { |
| 125 | getExecutor().execute(new SipRunnable() { |
| 126 | @Override |
| 127 | protected void doRun() throws SameThreadException { |
| 128 | Log.i(TAG, "SipService.unhold() thread running..."); |
| 129 | callManagerJNI.unhold(callID); |
| 130 | } |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | @Override |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 135 | public void setAudioPlugin(final String audioPlugin) { |
| 136 | getExecutor().execute(new SipRunnable() { |
| 137 | @Override |
| 138 | protected void doRun() throws SameThreadException { |
| 139 | Log.i(TAG, "SipService.setAudioPlugin() thread running..."); |
| 140 | configurationManagerJNI.setAudioPlugin(audioPlugin); |
| 141 | } |
Alexandre Savard | 31d27c6 | 2012-10-04 16:05:08 -0400 | [diff] [blame] | 142 | }); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public String getCurrentAudioOutputPlugin() { |
| 147 | class CurrentAudioPlugin extends SipRunnableWithReturn { |
| 148 | @Override |
| 149 | protected String doRun() throws SameThreadException { |
| 150 | Log.i(TAG, "SipService.getCurrentAudioOutputPlugin() thread running..."); |
| 151 | return configurationManagerJNI.getCurrentAudioOutputPlugin(); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | CurrentAudioPlugin runInstance = new CurrentAudioPlugin(); |
| 156 | getExecutor().execute(runInstance); |
| 157 | while(!runInstance.isDone()) {} |
Alexandre Savard | 7a902bc | 2012-10-04 16:32:35 -0400 | [diff] [blame] | 158 | return (String) runInstance.getVal(); |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 159 | } |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 160 | |
| 161 | @Override |
Alexandre Savard | 6b85e7e | 2012-09-27 15:43:14 -0400 | [diff] [blame] | 162 | public ArrayList<String> getAccountList() { |
Alexandre Savard | 7a2b220 | 2012-10-04 17:07:33 -0400 | [diff] [blame] | 163 | class AccountList extends SipRunnableWithReturn { |
| 164 | @Override |
| 165 | protected StringVect doRun() throws SameThreadException { |
| 166 | Log.i(TAG, "SipService.getAccountList() thread running..."); |
| 167 | return configurationManagerJNI.getAccountList(); |
| 168 | } |
| 169 | }; |
| 170 | AccountList runInstance = new AccountList(); |
| 171 | getExecutor().execute(runInstance); |
| 172 | while(!runInstance.isDone()) {} |
| 173 | StringVect swigvect = (StringVect) runInstance.getVal(); |
| 174 | |
Alexandre Savard | 6b85e7e | 2012-09-27 15:43:14 -0400 | [diff] [blame] | 175 | ArrayList<String> nativelist = new ArrayList<String>(); |
Alexandre Savard | 52a7252 | 2012-09-27 16:40:13 -0400 | [diff] [blame] | 176 | |
| 177 | for(int i = 0; i < swigvect.size(); i++) |
| 178 | nativelist.add(swigvect.get(i)); |
| 179 | |
Alexandre Savard | 6b85e7e | 2012-09-27 15:43:14 -0400 | [diff] [blame] | 180 | return nativelist; |
| 181 | } |
| 182 | |
| 183 | @Override |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 184 | public HashMap<String,String> getAccountDetails(final String accountID) { |
Alexandre Savard | 7a902bc | 2012-10-04 16:32:35 -0400 | [diff] [blame] | 185 | class AccountDetails extends SipRunnableWithReturn { |
| 186 | private String id; |
| 187 | AccountDetails(String accountId) { id = accountId; } |
| 188 | @Override |
| 189 | protected StringMap doRun() throws SameThreadException { |
Alexandre Savard | 7a2b220 | 2012-10-04 17:07:33 -0400 | [diff] [blame] | 190 | Log.i(TAG, "SipService.getAccountDetails() thread running..."); |
Alexandre Savard | 7a902bc | 2012-10-04 16:32:35 -0400 | [diff] [blame] | 191 | return configurationManagerJNI.getAccountDetails(id); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | AccountDetails runInstance = new AccountDetails(accountID); |
| 196 | getExecutor().execute(runInstance); |
| 197 | while(!runInstance.isDone()) {} |
| 198 | StringMap swigmap = (StringMap) runInstance.getVal(); |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 199 | |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 200 | HashMap<String, String> nativemap = AccountDetailsHandler.convertSwigToNative(swigmap); |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 201 | |
| 202 | return nativemap; |
| 203 | } |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 204 | |
| 205 | @Override |
Alexandre Savard | 7a2b220 | 2012-10-04 17:07:33 -0400 | [diff] [blame] | 206 | public void setAccountDetails(final String accountId, Map map) { |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 207 | HashMap<String,String> nativemap = (HashMap<String,String>) map; |
| 208 | |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 209 | final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig(nativemap); |
Alexandre Savard | 718d49f | 2012-10-02 15:17:13 -0400 | [diff] [blame] | 210 | |
Alexandre Savard | 7a2b220 | 2012-10-04 17:07:33 -0400 | [diff] [blame] | 211 | getExecutor().execute(new SipRunnable() { |
| 212 | @Override |
| 213 | protected void doRun() throws SameThreadException { |
| 214 | Log.i(TAG, "SipService.setAccountDetails() thread running..."); |
| 215 | configurationManagerJNI.setAccountDetails(accountId, swigmap); |
| 216 | } |
| 217 | }); |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Alexandre Savard | 4603657 | 2012-10-05 13:56:49 -0400 | [diff] [blame] | 220 | @Override |
| 221 | public String addAccount(Map map) { |
| 222 | class AddAccount extends SipRunnableWithReturn { |
| 223 | StringMap map; |
| 224 | AddAccount(StringMap m) { map = m; } |
| 225 | @Override |
| 226 | protected String doRun() throws SameThreadException { |
| 227 | Log.i(TAG, "SipService.getAccountDetails() thread running..."); |
| 228 | return configurationManagerJNI.addAccount(map); |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig((HashMap<String,String>)map); |
| 233 | |
| 234 | AddAccount runInstance = new AddAccount(swigmap); |
| 235 | getExecutor().execute(runInstance); |
| 236 | while(!runInstance.isDone()) {} |
| 237 | String accountId = (String) runInstance.getVal(); |
| 238 | |
| 239 | return accountId; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void removeAccount(final String accountId) { |
| 244 | getExecutor().execute(new SipRunnable() { |
| 245 | @Override |
| 246 | protected void doRun() throws SameThreadException { |
| 247 | Log.i(TAG, "SipService.setAccountDetails() thread running..."); |
| 248 | configurationManagerJNI.removeAccount(accountId); |
| 249 | } |
| 250 | }); |
| 251 | } |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | /** |
| 255 | * Class used for the client Binder. Because we know this service always |
| 256 | * runs in the same process as its clients, we don't need to deal with IPC. |
| 257 | */ |
| 258 | public class LocalBinder extends Binder { |
| 259 | public SipService getService() { |
| 260 | // Return this instance of LocalService so clients can call public methods |
| 261 | return SipService.this; |
| 262 | } |
| 263 | } |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 264 | |
| 265 | /* called once by startService() */ |
| 266 | @Override |
| 267 | public void onCreate() { |
| 268 | Log.i(TAG, "onCreated"); |
| 269 | super.onCreate(); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 270 | sflphoneApp = (SFLphoneApplication) getApplication(); |
| 271 | sipServiceThread = new SipServiceThread(); |
| 272 | getExecutor().execute(new StartRunnable()); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* called for each startService() */ |
| 276 | @Override |
| 277 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 278 | Log.i(TAG, "onStarted"); |
| 279 | super.onStartCommand(intent, flags, startId); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 280 | |
| 281 | runFlag = true; |
| 282 | sipServiceThread.start(); |
| 283 | sflphoneApp.setServiceRunning(true); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 284 | Toast.makeText(this, "Sflphone Service started", Toast.LENGTH_SHORT).show(); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 285 | |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 286 | Log.i(TAG, "onStarted"); |
| 287 | return START_STICKY; /* started and stopped explicitly */ |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public void onDestroy() { |
| 292 | /* called once by stopService() */ |
| 293 | super.onDestroy(); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 294 | runFlag = false; |
| 295 | sipServiceThread.interrupt(); |
| 296 | sipServiceThread = null; |
| 297 | sflphoneApp.setServiceRunning(false); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 298 | Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show(); |
| 299 | |
| 300 | Log.i(TAG, "onDestroyed"); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public IBinder onBind(Intent arg0) { |
| 305 | Log.i(TAG, "onBound"); |
| 306 | return mBinder; |
| 307 | } |
| 308 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 309 | private static Looper createLooper() { |
| 310 | if(executorThread == null) { |
| 311 | Log.d(TAG, "Creating new handler thread"); |
| 312 | // ADT gives a fake warning due to bad parse rule. |
| 313 | executorThread = new HandlerThread("SipService.Executor"); |
| 314 | executorThread.start(); |
| 315 | } |
| 316 | return executorThread.getLooper(); |
| 317 | } |
| 318 | |
| 319 | public SipServiceExecutor getExecutor() { |
| 320 | // create mExecutor lazily |
| 321 | if (mExecutor == null) { |
| 322 | mExecutor = new SipServiceExecutor(this); |
| 323 | } |
| 324 | return mExecutor; |
| 325 | } |
| 326 | |
| 327 | // Executes immediate tasks in a single executorThread. |
| 328 | public static class SipServiceExecutor extends Handler { |
| 329 | WeakReference<SipService> handlerService; |
| 330 | |
| 331 | SipServiceExecutor(SipService s) { |
| 332 | super(createLooper()); |
| 333 | handlerService = new WeakReference<SipService>(s); |
| 334 | } |
| 335 | |
| 336 | public void execute(Runnable task) { |
| 337 | // TODO: add wakelock |
| 338 | Message.obtain(this, 0/* don't care */, task).sendToTarget(); |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public void handleMessage(Message msg) { |
| 343 | if (msg.obj instanceof Runnable) { |
| 344 | executeInternal((Runnable) msg.obj); |
| 345 | } else { |
| 346 | Log.w(TAG, "can't handle msg: " + msg); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | private void executeInternal(Runnable task) { |
| 351 | try { |
| 352 | task.run(); |
| 353 | } catch (Throwable t) { |
| 354 | Log.e(TAG, "run task: " + task, t); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | private void startPjSipStack() throws SameThreadException { |
| 360 | if (isPjSipStackStarted) |
| 361 | return; |
| 362 | |
| 363 | try { |
| 364 | System.loadLibrary("gnustl_shared"); |
| 365 | System.loadLibrary("expat"); |
| 366 | System.loadLibrary("yaml"); |
| 367 | System.loadLibrary("ccgnu2"); |
| 368 | System.loadLibrary("crypto"); |
| 369 | System.loadLibrary("ssl"); |
| 370 | System.loadLibrary("ccrtp1"); |
| 371 | System.loadLibrary("dbus"); |
| 372 | System.loadLibrary("dbus-c++-1"); |
| 373 | System.loadLibrary("samplerate"); |
| 374 | System.loadLibrary("codec_ulaw"); |
| 375 | System.loadLibrary("codec_alaw"); |
| 376 | System.loadLibrary("speexresampler"); |
| 377 | System.loadLibrary("sflphone"); |
| 378 | isPjSipStackStarted = true; |
| 379 | } catch (UnsatisfiedLinkError e) { |
| 380 | Log.e(TAG, "Problem with the current Pj stack...", e); |
| 381 | isPjSipStackStarted = false; |
| 382 | return; |
| 383 | } catch (Exception e) { |
| 384 | Log.e(TAG, "Problem with the current Pj stack...", e); |
| 385 | } |
| 386 | |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 387 | /* get unique instance of managerImpl */ |
| 388 | managerImpl = SFLPhoneservice.instance(); |
Alexandre Savard | 718d49f | 2012-10-02 15:17:13 -0400 | [diff] [blame] | 389 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 390 | /* set static AppPath before calling manager.init */ |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 391 | managerImpl.setPath(sflphoneApp.getAppPath()); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 392 | |
Alexandre Savard | 718d49f | 2012-10-02 15:17:13 -0400 | [diff] [blame] | 393 | callManagerJNI = new CallManagerJNI(); |
Alexandre Savard | 74c1cad | 2012-10-24 16:39:00 -0400 | [diff] [blame] | 394 | callManagerCallBack = new CallManagerCallBack(this); |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 395 | SFLPhoneservice.setCallbackObject(callManagerCallBack); |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 396 | |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 397 | configurationManagerJNI = new ConfigurationManagerJNI(); |
Alexandre Savard | 4f11d7a | 2012-10-18 13:15:36 -0400 | [diff] [blame] | 398 | configurationManagerCallback = new ConfigurationManagerCallback(this); |
Alexandre Savard | fccd1dc | 2012-10-17 17:31:38 -0400 | [diff] [blame] | 399 | SFLPhoneservice.setConfigurationCallbackObject(configurationManagerCallback); |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 400 | |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 401 | managerImpl.init(""); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | |
| 405 | // Enforce same thread contract to ensure we do not call from somewhere else |
| 406 | public class SameThreadException extends Exception { |
| 407 | private static final long serialVersionUID = -905639124232613768L; |
| 408 | |
| 409 | public SameThreadException() { |
| 410 | super("Should be launched from a single worker thread"); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | public abstract static class SipRunnable implements Runnable { |
| 415 | protected abstract void doRun() throws SameThreadException; |
| 416 | |
| 417 | public void run() { |
| 418 | try { |
| 419 | doRun(); |
| 420 | }catch(SameThreadException e) { |
| 421 | Log.e(TAG, "Not done from same thread"); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
Alexandre Savard | 31d27c6 | 2012-10-04 16:05:08 -0400 | [diff] [blame] | 426 | public abstract static class SipRunnableWithReturn implements Runnable { |
| 427 | Object obj = null; |
| 428 | boolean done = false; |
| 429 | |
| 430 | protected abstract Object doRun() throws SameThreadException; |
| 431 | |
| 432 | public Object getVal() { |
| 433 | return obj; |
| 434 | } |
| 435 | |
| 436 | public boolean isDone() { |
| 437 | return done; |
| 438 | } |
| 439 | |
| 440 | public void run() { |
| 441 | try { |
| 442 | obj = doRun(); |
| 443 | done = true; |
| 444 | }catch(SameThreadException e) { |
| 445 | Log.e(TAG, "Not done from same thread"); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 450 | class StartRunnable extends SipRunnable { |
| 451 | @Override |
| 452 | protected void doRun() throws SameThreadException { |
| 453 | startPjSipStack(); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | private class SipServiceThread extends Thread { |
| 458 | |
| 459 | public SipServiceThread() { |
| 460 | super("sipServiceThread"); |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public void run() { |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 465 | Log.i(TAG, "SipService thread running..."); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 466 | SipService sipService = SipService.this; |
| 467 | while(sipService.runFlag) { |
| 468 | try { |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 469 | Thread.sleep(DELAY); |
| 470 | } catch (InterruptedException e) { |
| 471 | sipService.runFlag = false; |
| 472 | Log.w(TAG, "service thread interrupted!"); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |