1717
1818import android .content .Context ;
1919import android .net .ConnectivityManager ;
20+ import android .net .Network ;
21+ import android .net .NetworkCapabilities ;
2022import android .net .NetworkInfo ;
2123import android .os .Build ;
2224import androidx .annotation .NonNull ;
2325import androidx .annotation .RequiresApi ;
2426
27+ import com .github .pwittchen .reactivenetwork .library .rx2 .info .NetworkState ;
28+
2529/**
2630 * Connectivity class represents current connectivity status. It wraps NetworkInfo object.
2731 */
@@ -40,6 +44,8 @@ public final class Connectivity {
4044 private String subTypeName ; // NOPMD
4145 private String reason ; // NOPMD
4246 private String extraInfo ; // NOPMD
47+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
48+ private NetworkState networkState ;
4349
4450 public static Connectivity create () {
4551 return builder ().build ();
@@ -50,6 +56,12 @@ public static Connectivity create(@NonNull Context context) {
5056 return create (context , getConnectivityManager (context ));
5157 }
5258
59+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
60+ public static Connectivity create (@ NonNull Context context , NetworkState networkState ) {
61+ Preconditions .checkNotNull (context , "context == null" );
62+ return create (context , getConnectivityManager (context ), networkState );
63+ }
64+
5365 private static ConnectivityManager getConnectivityManager (Context context ) {
5466 final String service = Context .CONNECTIVITY_SERVICE ;
5567 return (ConnectivityManager ) context .getSystemService (service );
@@ -66,6 +78,18 @@ protected static Connectivity create(@NonNull Context context, ConnectivityManag
6678 return (networkInfo == null ) ? create () : create (networkInfo );
6779 }
6880
81+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
82+ protected static Connectivity create (@ NonNull Context context , ConnectivityManager manager , NetworkState networkState ) {
83+ Preconditions .checkNotNull (context , "context == null" );
84+
85+ if (manager == null ) {
86+ return create ();
87+ }
88+ networkState .setNetworkCapabilities (manager .getNetworkCapabilities (networkState .getNetwork ()));
89+ networkState .setLinkProperties (manager .getLinkProperties (networkState .getNetwork ()));
90+ return create (networkState );
91+ }
92+
6993 private static Connectivity create (NetworkInfo networkInfo ) {
7094 return new Builder ()
7195 .state (networkInfo .getState ())
@@ -82,9 +106,20 @@ private static Connectivity create(NetworkInfo networkInfo) {
82106 .build ();
83107 }
84108
109+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
110+ private static Connectivity create (NetworkState networkState ) {
111+ return new Builder ()
112+ .networkState (networkState )
113+ .build ();
114+ }
115+
85116 private Connectivity (Builder builder ) {
86- state = builder .state ;
87- detailedState = builder .detailedState ;
117+ if (Preconditions .isAtLeastAndroidLollipop ()) {
118+ networkState = builder .networkState ;
119+ } else {
120+ state = builder .state ;
121+ detailedState = builder .detailedState ;
122+ }
88123 type = builder .type ;
89124 subType = builder .subType ;
90125 available = builder .available ;
@@ -192,6 +227,11 @@ public static Builder extraInfo(String extraInfo) {
192227 return builder ().extraInfo (extraInfo );
193228 }
194229
230+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
231+ public NetworkState getNetworkState () {
232+ return networkState ;
233+ }
234+
195235 @ Override public boolean equals (Object o ) {
196236 if (this == o ) {
197237 return true ;
@@ -298,14 +338,17 @@ public final static class Builder {
298338 private String subTypeName = "NONE" ; // NOPMD
299339 private String reason = "" ; // NOPMD
300340 private String extraInfo = "" ; // NOPMD
341+ private NetworkState networkState = new NetworkState ();
301342
302343 public Builder state (NetworkInfo .State state ) {
303344 this .state = state ;
345+ this .networkState .setConnected (state == NetworkInfo .State .CONNECTED );
304346 return this ;
305347 }
306348
307349 public Builder detailedState (NetworkInfo .DetailedState detailedState ) {
308350 this .detailedState = detailedState ;
351+ this .networkState .setConnected (detailedState == NetworkInfo .DetailedState .CONNECTED );
309352 return this ;
310353 }
311354
@@ -354,6 +397,11 @@ public Builder extraInfo(String extraInfo) {
354397 return this ;
355398 }
356399
400+ public Builder networkState (NetworkState networkState ) {
401+ this .networkState = networkState ;
402+ return this ;
403+ }
404+
357405 public Connectivity build () {
358406 return new Connectivity (this );
359407 }
0 commit comments