44import android .content .Intent ;
55import android .content .IntentFilter ;
66import android .net .ConnectivityManager ;
7+ import android .net .NetworkInfo ;
78
8- import com .couchbase .lite .Context ;
99import com .couchbase .lite .NetworkReachabilityManager ;
1010import com .couchbase .lite .util .Log ;
1111
1212public class AndroidNetworkReachabilityManager extends NetworkReachabilityManager {
1313
14- private Context context ;
1514 private boolean listening ;
1615 private android .content .Context wrappedContext ;
1716 private ConnectivityBroadcastReceiver receiver ;
18- private State state ;
1917
20- public enum State {
21- UNKNOWN ,
22-
23- /** This state is returned if there is connectivity to any network **/
24- CONNECTED ,
25- /**
26- * This state is returned if there is no connectivity to any network. This is set to true
27- * under two circumstances:
28- * <ul>
29- * <li>When connectivity is lost to one network, and there is no other available network to
30- * attempt to switch to.</li>
31- * <li>When connectivity is lost to one network, and the attempt to switch to another
32- * network fails.</li>
33- */
34- NOT_CONNECTED
35- }
36-
37- public AndroidNetworkReachabilityManager (AndroidContext context ) {
38- this .context = context ;
39- this .wrappedContext = context .getWrappedContext ();
18+ public AndroidNetworkReachabilityManager (AndroidContext androidContextcontext ) {
19+ this .listening = false ;
20+ this .wrappedContext = androidContextcontext .getWrappedContext ();
4021 this .receiver = new ConnectivityBroadcastReceiver ();
41- this .state = State .UNKNOWN ;
4222 }
4323
44-
4524 public synchronized void startListening () {
4625 if (!listening ) {
4726 IntentFilter filter = new IntentFilter ();
@@ -60,38 +39,32 @@ public synchronized void stopListening() {
6039 } catch (Exception e ) {
6140 Log .e (Log .TAG_SYNC , "%s: stopListening() exception unregistering %s with context %s" , e , this , receiver , wrappedContext );
6241 }
63- context = null ;
6442 listening = false ;
6543 }
6644 }
6745
6846 private class ConnectivityBroadcastReceiver extends BroadcastReceiver {
69-
7047 @ Override
7148 public void onReceive (android .content .Context context , Intent intent ) {
7249 String action = intent .getAction ();
73-
7450 if (!action .equals (ConnectivityManager .CONNECTIVITY_ACTION ) || listening == false ) {
7551 return ;
7652 }
7753
78- boolean noConnectivity = intent .getBooleanExtra (ConnectivityManager .EXTRA_NO_CONNECTIVITY , false );
54+ boolean bOnline = isOnline (context );
55+ Log .v (Log .TAG_SYNC , "BroadcastReceiver.onReceive() bOnline=" + bOnline );
7956
80- if (noConnectivity ) {
81- state = State . NOT_CONNECTED ;
57+ if (bOnline ) {
58+ notifyListenersNetworkReachable () ;
8259 } else {
83- state = State .CONNECTED ;
84- }
85-
86- if (state == State .NOT_CONNECTED ) {
8760 notifyListenersNetworkUneachable ();
8861 }
89-
90- if (state == State .CONNECTED ) {
91- notifyListenersNetworkReachable ();
92- }
93-
9462 }
95- };
63+ }
9664
65+ private boolean isOnline (android .content .Context context ) {
66+ ConnectivityManager cm = (ConnectivityManager ) context .getSystemService (android .content .Context .CONNECTIVITY_SERVICE );
67+ NetworkInfo activeNetwork = cm .getActiveNetworkInfo ();
68+ return activeNetwork != null && activeNetwork .isConnected ();
69+ }
9770}
0 commit comments