1717
1818import android .content .Context ;
1919import android .net .ConnectivityManager ;
20- import android .net .Network ;
21- import android .net .NetworkCapabilities ;
2220import android .net .NetworkInfo ;
2321import android .os .Build ;
2422import androidx .annotation .NonNull ;
23+ import androidx .annotation .Nullable ;
2524import androidx .annotation .RequiresApi ;
26-
2725import com .github .pwittchen .reactivenetwork .library .rx2 .info .NetworkState ;
2826
2927/**
3028 * Connectivity class represents current connectivity status. It wraps NetworkInfo object.
3129 */
32- @ RequiresApi (api = Build .VERSION_CODES .CUPCAKE )
3330public final class Connectivity {
3431 static final int UNKNOWN_TYPE = -1 ;
3532 static final int UNKNOWN_SUB_TYPE = -1 ;
36- private NetworkInfo .State state ; // NOPMD
37- private NetworkInfo .DetailedState detailedState ; // NOPMD
38- private int type ; // NOPMD
39- private int subType ; // NOPMD
40- private boolean available ; // NOPMD
41- private boolean failover ; // NOPMD
42- private boolean roaming ; // NOPMD
43- private String typeName ; // NOPMD
44- private String subTypeName ; // NOPMD
45- private String reason ; // NOPMD
46- private String extraInfo ; // NOPMD
47- @ RequiresApi ( api = Build . VERSION_CODES . LOLLIPOP )
48- private NetworkState networkState ;
33+ @ Nullable private NetworkInfo .State state ; // NOPMD
34+ @ Nullable private NetworkInfo .DetailedState detailedState ; // NOPMD
35+ @ Nullable @ RequiresApi ( api = Build . VERSION_CODES . LOLLIPOP )
36+ private NetworkState networkState ; // NOPMD
37+ private final int type ; // NOPMD
38+ private final int subType ; // NOPMD
39+ private final boolean available ; // NOPMD
40+ private final boolean failover ; // NOPMD
41+ private final boolean roaming ; // NOPMD
42+ private final String typeName ; // NOPMD
43+ private final String subTypeName ; // NOPMD
44+ private final String reason ; // NOPMD
45+ private final String extraInfo ; // NOPMD
4946
5047 public static Connectivity create () {
5148 return builder ().build ();
5249 }
5350
51+ @ SuppressWarnings ("PMD" )
5452 public static Connectivity create (@ NonNull Context context ) {
5553 Preconditions .checkNotNull (context , "context == null" );
5654 return create (context , getConnectivityManager (context ));
5755 }
5856
57+ @ SuppressWarnings ("PMD" )
5958 @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
6059 public static Connectivity create (@ NonNull Context context , NetworkState networkState ) {
6160 Preconditions .checkNotNull (context , "context == null" );
@@ -67,6 +66,7 @@ private static ConnectivityManager getConnectivityManager(Context context) {
6766 return (ConnectivityManager ) context .getSystemService (service );
6867 }
6968
69+ @ SuppressWarnings ("PMD" )
7070 protected static Connectivity create (@ NonNull Context context , ConnectivityManager manager ) {
7171 Preconditions .checkNotNull (context , "context == null" );
7272
@@ -78,8 +78,11 @@ protected static Connectivity create(@NonNull Context context, ConnectivityManag
7878 return (networkInfo == null ) ? create () : create (networkInfo );
7979 }
8080
81+ @ SuppressWarnings ("PMD" )
8182 @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
82- protected static Connectivity create (@ NonNull Context context , ConnectivityManager manager , NetworkState networkState ) {
83+ protected static Connectivity create (
84+ @ NonNull Context context , ConnectivityManager manager , NetworkState networkState
85+ ) {
8386 Preconditions .checkNotNull (context , "context == null" );
8487
8588 if (manager == null ) {
@@ -109,16 +112,22 @@ private static Connectivity create(NetworkInfo networkInfo) {
109112 @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
110113 private static Connectivity create (NetworkState networkState ) {
111114 return new Builder ()
112- .networkState (networkState )
113- .build ();
115+ .networkState (networkState )
116+ .build ();
114117 }
115118
116119 private Connectivity (Builder builder ) {
117- if (Preconditions .isAtLeastAndroidLollipop ()) {
118- networkState = builder .networkState ;
120+ if (Preconditions .isAtLeastAndroidLollipop ()) {
121+ if (builder .networkState != null ) {
122+ networkState = builder .networkState ;
123+ }
119124 } else {
120- state = builder .state ;
121- detailedState = builder .detailedState ;
125+ if (builder .state != null ) {
126+ state = builder .state ;
127+ }
128+ if (builder .detailedState != null ) {
129+ detailedState = builder .detailedState ;
130+ }
122131 }
123132 type = builder .type ;
124133 subType = builder .subType ;
@@ -139,22 +148,28 @@ private static Builder builder() {
139148 return new Connectivity .Builder ();
140149 }
141150
142- public NetworkInfo .State state () {
151+ public @ Nullable NetworkInfo .State state () {
143152 return state ;
144153 }
145154
146155 public static Builder state (NetworkInfo .State state ) {
147156 return builder ().state (state );
148157 }
149158
150- public NetworkInfo .DetailedState detailedState () {
159+ public @ Nullable NetworkInfo .DetailedState detailedState () {
151160 return detailedState ;
152161 }
153162
154163 public static Builder state (NetworkInfo .DetailedState detailedState ) {
155164 return builder ().detailedState (detailedState );
156165 }
157166
167+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
168+ @ Nullable
169+ public NetworkState getNetworkState () {
170+ return networkState ;
171+ }
172+
158173 public int type () {
159174 return type ;
160175 }
@@ -227,11 +242,6 @@ public static Builder extraInfo(String extraInfo) {
227242 return builder ().extraInfo (extraInfo );
228243 }
229244
230- @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
231- public NetworkState getNetworkState () {
232- return networkState ;
233- }
234-
235245 @ Override public boolean equals (Object o ) {
236246 if (this == o ) {
237247 return true ;
@@ -277,7 +287,7 @@ public NetworkState getNetworkState() {
277287 }
278288
279289 @ Override public int hashCode () {
280- int result = state .hashCode ();
290+ int result = state != null ? state .hashCode () : 0 ;
281291 result = 31 * result + (detailedState != null ? detailedState .hashCode () : 0 );
282292 result = 31 * result + type ;
283293 result = 31 * result + subType ;
@@ -338,7 +348,7 @@ public final static class Builder {
338348 private String subTypeName = "NONE" ; // NOPMD
339349 private String reason = "" ; // NOPMD
340350 private String extraInfo = "" ; // NOPMD
341- private NetworkState networkState = new NetworkState ();
351+ private NetworkState networkState = new NetworkState (); // NOPMD
342352
343353 public Builder state (NetworkInfo .State state ) {
344354 this .state = state ;
0 commit comments