Skip to content

Commit efff7da

Browse files
committed
Fixes #175: Exception when changing from mobile to wifi and trying to make a call before the transition is complete. Fixes #178: When connectivity is lost completely we shouldn't shutdown JAIN SIP stack
1 parent c521f0e commit efff7da

File tree

5 files changed

+182
-98
lines changed

5 files changed

+182
-98
lines changed

Examples/restcomm-messenger/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
minSdkVersion 16
1010
targetSdkVersion 22
1111
versionCode 1
12-
versionName "1.0.0-BETA2#8"
12+
versionName "1.0.0-BETA2#9"
1313
}
1414
buildTypes {
1515
release {

restcomm.android.client.sdk/src/main/java/org/mobicents/restcomm/android/client/sdk/RCConnection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ private boolean haveConnectivity()
492492
return false;
493493
}
494494

495-
RCDevice.ReachabilityState state = device.getReachability();
495+
DeviceImpl.ReachabilityState state = device.getReachability();
496496

497-
if (state == RCDevice.ReachabilityState.REACHABILITY_WIFI ||
498-
state == RCDevice.ReachabilityState.REACHABILITY_MOBILE) {
497+
if (state == DeviceImpl.ReachabilityState.REACHABILITY_WIFI ||
498+
state == DeviceImpl.ReachabilityState.REACHABILITY_MOBILE) {
499499
return true;
500500
}
501501
else {

restcomm.android.client.sdk/src/main/java/org/mobicents/restcomm/android/client/sdk/RCDevice.java

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import android.media.AudioManager;
3535
import android.net.ConnectivityManager;
3636
import android.net.NetworkInfo;
37+
import android.net.wifi.WifiManager;
3738
import android.os.Handler;
3839
import android.util.Log;
3940

@@ -106,12 +107,6 @@ public enum DeviceCapability {
106107
CLIENT_NAME,
107108
}
108109

109-
public enum ReachabilityState {
110-
REACHABILITY_WIFI,
111-
REACHABILITY_MOBILE,
112-
REACHABILITY_NONE,
113-
}
114-
115110
private static final String TAG = "RCDevice";
116111
//private static boolean online = false;
117112
public static String OUTGOING_CALL = "ACTION_OUTGOING_CALL";
@@ -129,7 +124,7 @@ public enum ReachabilityState {
129124
PendingIntent pendingCallIntent;
130125
PendingIntent pendingMessageIntent;
131126
private RCConnection incomingConnection;
132-
private ReachabilityState reachabilityState = ReachabilityState.REACHABILITY_NONE;
127+
private DeviceImpl.ReachabilityState reachabilityState = DeviceImpl.ReachabilityState.REACHABILITY_NONE;
133128
private SipProfile sipProfile = null;
134129
//MediaPlayer messagePlayer;
135130
//AudioManager audioManager;
@@ -156,52 +151,36 @@ protected RCDevice(HashMap<String, Object> parameters, RCDeviceListener deviceLi
156151
// initialize JAIN SIP if we have connectivity
157152
this.parameters = parameters;
158153
//RCClient client = RCClient.getInstance();
159-
reachabilityState = checkReachability();
154+
reachabilityState = DeviceImpl.checkReachability(RCClient.getContext());
160155

161-
if (reachabilityState == ReachabilityState.REACHABILITY_WIFI ||
162-
reachabilityState == ReachabilityState.REACHABILITY_MOBILE) {
163-
initializeSignalling();
156+
boolean connectivity = false;
157+
if (reachabilityState == DeviceImpl.ReachabilityState.REACHABILITY_WIFI ||
158+
reachabilityState == DeviceImpl.ReachabilityState.REACHABILITY_MOBILE) {
159+
connectivity = true;
164160
}
161+
initializeSignalling(connectivity);
165162
}
166163

167164
@Override
168165
public void onReceive(Context context, Intent intent) {
169-
onReachabilityChanged(checkReachability());
166+
onReachabilityChanged(DeviceImpl.checkReachability(RCClient.getContext()));
170167
}
171168

172-
private void initializeSignalling()
169+
private void initializeSignalling(boolean connectivity)
173170
{
174171
sipProfile = new SipProfile();
175172
updateSipProfile(parameters);
176173
DeviceImpl deviceImpl = DeviceImpl.GetInstance();
177-
deviceImpl.Initialize(RCClient.getContext(), sipProfile);
174+
deviceImpl.Initialize(RCClient.getContext(), sipProfile, connectivity);
178175
DeviceImpl.GetInstance().sipuaDeviceListener = this;
179176
// register after initialization
180-
DeviceImpl.GetInstance().Register();
181-
state = DeviceState.READY;
182-
}
183-
184-
private ReachabilityState checkReachability()
185-
{
186-
ConnectivityManager cm = (ConnectivityManager) RCClient.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
187-
188-
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
189-
if (null != activeNetwork) {
190-
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
191-
Log.w(TAG, "Reachability event: WIFI");
192-
return ReachabilityState.REACHABILITY_WIFI;
193-
}
194-
195-
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
196-
Log.w(TAG, "Reachability event: MOBILE");
197-
return ReachabilityState.REACHABILITY_MOBILE;
198-
}
177+
if (connectivity) {
178+
DeviceImpl.GetInstance().Register();
179+
state = DeviceState.READY;
199180
}
200-
Log.w(TAG, "Reachability event: NONE");
201-
return ReachabilityState.REACHABILITY_NONE;
202181
}
203182

204-
private void onReachabilityChanged(final ReachabilityState newState)
183+
private void onReachabilityChanged(final DeviceImpl.ReachabilityState newState)
205184
{
206185
//final RCDevice device = this;
207186
//final ReachabilityState state = newState;
@@ -215,20 +194,20 @@ private void onReachabilityChanged(final ReachabilityState newState)
215194
public void run() {
216195
*/
217196

218-
if (newState == ReachabilityState.REACHABILITY_NONE && state != DeviceState.OFFLINE) {
197+
if (newState == DeviceImpl.ReachabilityState.REACHABILITY_NONE && state != DeviceState.OFFLINE) {
219198
Log.w(TAG, "Reachability changed; no connectivity");
220199
// TODO: here we need to unregister before shutting down, but for that we need to wait for the unREGISTER reply, which complicates things
221-
//DeviceImpl.GetInstance().Unregister();
222-
DeviceImpl.GetInstance().Shutdown();
223-
sipProfile = null;
200+
//DeviceImpl.GetInstance().Shutdown();
201+
DeviceImpl.GetInstance().unbind();
202+
//sipProfile = null;
224203
state = DeviceState.OFFLINE;
225204
reachabilityState = newState;
226205
return;
227206
}
228207

229208
// old state wifi and new state mobile or the reverse; need to shutdown and restart network facilities
230-
if ((reachabilityState == ReachabilityState.REACHABILITY_WIFI && newState == ReachabilityState.REACHABILITY_MOBILE) ||
231-
(reachabilityState == ReachabilityState.REACHABILITY_MOBILE && newState == ReachabilityState.REACHABILITY_WIFI)) {
209+
if ((reachabilityState == DeviceImpl.ReachabilityState.REACHABILITY_WIFI && newState == DeviceImpl.ReachabilityState.REACHABILITY_MOBILE) ||
210+
(reachabilityState == DeviceImpl.ReachabilityState.REACHABILITY_MOBILE && newState == DeviceImpl.ReachabilityState.REACHABILITY_WIFI)) {
232211
if (state != DeviceState.OFFLINE) {
233212
Log.w(TAG, "Reachability action: switch between wifi and mobile. Device state: " + state);
234213
// stop JAIN
@@ -244,11 +223,14 @@ public void run() {
244223
}
245224
}
246225

247-
if ((newState == ReachabilityState.REACHABILITY_WIFI || newState == ReachabilityState.REACHABILITY_MOBILE)
248-
&& state != DeviceState.READY) {
226+
if ((newState == DeviceImpl.ReachabilityState.REACHABILITY_WIFI || newState == DeviceImpl.ReachabilityState.REACHABILITY_MOBILE)
227+
&& state == DeviceState.OFFLINE) {
249228
Log.w(TAG, "Reachability action: wifi/mobile available. Device state: " + state);
250-
initializeSignalling();
229+
DeviceImpl.GetInstance().bind();
230+
DeviceImpl.GetInstance().Register();
231+
//initializeSignalling(true);
251232
reachabilityState = newState;
233+
state = DeviceState.READY;
252234
}
253235

254236
/*
@@ -258,27 +240,34 @@ public void run() {
258240
*/
259241
}
260242

261-
public ReachabilityState getReachability()
243+
public DeviceImpl.ReachabilityState getReachability()
262244
{
263245
return reachabilityState;
264246
}
265247

266-
public void shutdown() {
248+
public void shutdown()
249+
{
267250
this.listener = null;
268251

269252
if (DeviceImpl.isInitialized()) {
270-
DeviceImpl.GetInstance().Unregister();
271-
// allow for the unregister to be serviced before we shut down the stack (delay 2 secs)
272-
// TODO: a better way to do this would be to wait for the response to the unregistration
273-
// before we shutdown
274-
Handler mainHandler = new Handler(RCClient.getContext().getMainLooper());
275-
Runnable myRunnable = new Runnable() {
276-
@Override
277-
public void run() {
278-
DeviceImpl.GetInstance().Shutdown();
279-
}
280-
};
281-
mainHandler.postDelayed(myRunnable, 500);
253+
if (state != DeviceState.OFFLINE) {
254+
DeviceImpl.GetInstance().Unregister();
255+
// allow for the unregister to be serviced before we shut down the stack (delay 2 secs)
256+
// TODO: a better way to do this would be to wait for the response to the unregistration
257+
// before we shutdown
258+
Handler mainHandler = new Handler(RCClient.getContext().getMainLooper());
259+
Runnable myRunnable = new Runnable() {
260+
@Override
261+
public void run() {
262+
DeviceImpl.GetInstance().Shutdown();
263+
}
264+
};
265+
mainHandler.postDelayed(myRunnable, 500);
266+
}
267+
else {
268+
// we are offline, no need for unregister
269+
DeviceImpl.GetInstance().Shutdown();
270+
}
282271
}
283272
state = DeviceState.OFFLINE;
284273
}
@@ -361,6 +350,11 @@ public void updateCapabilityToken(String token) {
361350
*/
362351
public RCConnection connect(Map<String, Object> parameters, RCConnectionListener listener) {
363352
//Activity activity = (Activity) listener;
353+
if (DeviceImpl.checkReachability(RCClient.getContext()) == DeviceImpl.ReachabilityState.REACHABILITY_NONE) {
354+
Log.e(TAG, "connect(): No reachability");
355+
return null;
356+
}
357+
364358
if (state == DeviceState.READY) {
365359
Log.i(TAG, "RCDevice.connect(), with connectivity");
366360

@@ -625,8 +619,8 @@ public void run() {
625619
// Helpers
626620
/*
627621
private boolean haveConnectivity() {
628-
RCClient client = RCClient.getInstance();
629-
WifiManager wifi = (WifiManager) client.context.getSystemService(client.context.WIFI_SERVICE);
622+
Context context = RCClient.getContext();
623+
WifiManager wifi = (WifiManager) RCClient.getContext().getSystemService(Context.WIFI_SERVICE);
630624
if (wifi.isWifiEnabled()) {
631625
return true;
632626
} else {

sipua/src/main/java/org/mobicents/restcomm/android/sipua/impl/DeviceImpl.java

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
//import org.mobicents.restcomm.android.sdk.ui.NotifyMessage;
2222

2323
import android.content.Context;
24+
import android.javax.sip.ObjectInUseException;
25+
import android.net.ConnectivityManager;
26+
import android.net.NetworkInfo;
2427
import android.os.Handler;
2528
import android.util.Log;
2629

@@ -40,6 +43,12 @@ public class DeviceImpl implements IDevice,Serializable {
4043
// use this handler for registration refreshes
4144
Handler registerRefreshHandler = null;
4245

46+
public enum ReachabilityState {
47+
REACHABILITY_WIFI,
48+
REACHABILITY_MOBILE,
49+
REACHABILITY_NONE,
50+
}
51+
4352
private DeviceImpl(){
4453

4554
}
@@ -50,22 +59,36 @@ public static DeviceImpl GetInstance(){
5059
}
5160
return device;
5261
}
53-
public void Initialize(Context context, SipProfile sipProfile, HashMap<String,String> customHeaders){
54-
this.Initialize(context, sipProfile);
62+
public void Initialize(Context context, SipProfile sipProfile, boolean connectivity, HashMap<String,String> customHeaders){
63+
this.Initialize(context, sipProfile, connectivity);
5564
sipManager.setCustomHeaders(customHeaders);
5665
}
57-
public void Initialize(Context context, SipProfile sipProfile) {
66+
public void Initialize(Context context, SipProfile sipProfile, boolean connectivity) {
5867
Log.v(TAG, "Initialize()");
5968

6069
this.context = context;
6170
this.sipProfile = sipProfile;
62-
sipManager = new SipManager(sipProfile);
71+
sipManager = new SipManager(sipProfile, connectivity);
6372
soundManager = new SoundManager(context,sipProfile.getLocalIp());
6473
sipManager.addSipListener(this);
6574
registerRefreshHandler = new Handler(context.getMainLooper());
6675
initialized = true;
6776
}
6877

78+
// release JAIN networking facilities
79+
public void unbind()
80+
{
81+
// disable auto registration refreshes
82+
registerRefreshHandler.removeCallbacksAndMessages(null);
83+
sipManager.unbind();
84+
}
85+
86+
// setup JAIN networking facilities
87+
public void bind()
88+
{
89+
sipManager.bind();
90+
}
91+
6992
public void Shutdown()
7093
{
7194
Log.v(TAG, "Shutdown");
@@ -167,7 +190,12 @@ public void CallWebrtc(String to, String sdp, HashMap<String, String> sipHeaders
167190
sipManager.setCustomHeaders(sipHeaders);
168191
}
169192
*/
170-
this.sipManager.CallWebrtc(to, sdp, sipHeaders);
193+
if (checkReachability(this.context) != ReachabilityState.REACHABILITY_NONE) {
194+
this.sipManager.CallWebrtc(to, sdp, sipHeaders);
195+
}
196+
else {
197+
Log.e(TAG, "No reachability");
198+
}
171199
} catch (NotInitializedException e) {
172200
e.printStackTrace();
173201
}
@@ -303,8 +331,29 @@ public static Object deserialize(byte[] b) {
303331
} catch(IOException ioe) {
304332
return null;
305333
}
306-
}
307-
308-
334+
}
335+
336+
static public ReachabilityState checkReachability(Context context)
337+
{
338+
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
339+
340+
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
341+
if (null != activeNetwork) {
342+
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI && activeNetwork.isConnected()) {
343+
Log.w(TAG, "Reachability event: WIFI");
344+
return ReachabilityState.REACHABILITY_WIFI;
345+
}
346+
347+
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && activeNetwork.isConnected()) {
348+
Log.w(TAG, "Reachability event: MOBILE");
349+
return ReachabilityState.REACHABILITY_MOBILE;
350+
}
351+
}
352+
Log.w(TAG, "Reachability event: NONE");
353+
return ReachabilityState.REACHABILITY_NONE;
354+
}
355+
356+
357+
309358

310359
}

0 commit comments

Comments
 (0)