3434import android .media .AudioManager ;
3535import android .net .ConnectivityManager ;
3636import android .net .NetworkInfo ;
37+ import android .net .wifi .WifiManager ;
3738import android .os .Handler ;
3839import 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 {
0 commit comments