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