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,29 @@ 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+   @ Nullable 
168+   @ RequiresApi (api  = Build .VERSION_CODES .LOLLIPOP )
169+   @ SuppressWarnings ("PMD" )
170+   public  NetworkState  networkState () {
171+     return  networkState ;
172+   }
173+ 
158174  public  int  type () {
159175    return  type ;
160176  }
@@ -227,11 +243,6 @@ public static Builder extraInfo(String extraInfo) {
227243    return  builder ().extraInfo (extraInfo );
228244  }
229245
230-   @ RequiresApi (api  = Build .VERSION_CODES .LOLLIPOP )
231-   public  NetworkState  getNetworkState () {
232-     return  networkState ;
233-   }
234- 
235246  @ Override  public  boolean  equals (Object  o ) {
236247    if  (this  == o ) {
237248      return  true ;
@@ -277,7 +288,7 @@ public NetworkState getNetworkState() {
277288  }
278289
279290  @ Override  public  int  hashCode () {
280-     int  result  = state .hashCode ();
291+     int  result  = state  !=  null  ?  state .hashCode () :  0 ;
281292    result  = 31  * result  + (detailedState  != null  ? detailedState .hashCode () : 0 );
282293    result  = 31  * result  + type ;
283294    result  = 31  * result  + subType ;
@@ -338,7 +349,7 @@ public final static class Builder {
338349    private  String  subTypeName  = "NONE" ; // NOPMD 
339350    private  String  reason  = "" ; // NOPMD 
340351    private  String  extraInfo  = "" ; // NOPMD 
341-     private  NetworkState  networkState  = new  NetworkState ();
352+     private  NetworkState  networkState  = new  NetworkState ();  // NOPMD 
342353
343354    public  Builder  state (NetworkInfo .State  state ) {
344355      this .state  = state ;
0 commit comments