17
17
18
18
import android .content .Context ;
19
19
import android .net .ConnectivityManager ;
20
+ import android .net .Network ;
21
+ import android .net .NetworkCapabilities ;
20
22
import android .net .NetworkInfo ;
21
23
import android .os .Build ;
22
24
import androidx .annotation .NonNull ;
23
25
import androidx .annotation .RequiresApi ;
24
26
27
+ import com .github .pwittchen .reactivenetwork .library .rx2 .info .NetworkState ;
28
+
25
29
/**
26
30
* Connectivity class represents current connectivity status. It wraps NetworkInfo object.
27
31
*/
@@ -40,6 +44,8 @@ public final class Connectivity {
40
44
private String subTypeName ; // NOPMD
41
45
private String reason ; // NOPMD
42
46
private String extraInfo ; // NOPMD
47
+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
48
+ private NetworkState networkState ;
43
49
44
50
public static Connectivity create () {
45
51
return builder ().build ();
@@ -50,6 +56,12 @@ public static Connectivity create(@NonNull Context context) {
50
56
return create (context , getConnectivityManager (context ));
51
57
}
52
58
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
+
53
65
private static ConnectivityManager getConnectivityManager (Context context ) {
54
66
final String service = Context .CONNECTIVITY_SERVICE ;
55
67
return (ConnectivityManager ) context .getSystemService (service );
@@ -66,6 +78,18 @@ protected static Connectivity create(@NonNull Context context, ConnectivityManag
66
78
return (networkInfo == null ) ? create () : create (networkInfo );
67
79
}
68
80
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
+
69
93
private static Connectivity create (NetworkInfo networkInfo ) {
70
94
return new Builder ()
71
95
.state (networkInfo .getState ())
@@ -82,9 +106,20 @@ private static Connectivity create(NetworkInfo networkInfo) {
82
106
.build ();
83
107
}
84
108
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
+
85
116
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
+ }
88
123
type = builder .type ;
89
124
subType = builder .subType ;
90
125
available = builder .available ;
@@ -192,6 +227,11 @@ public static Builder extraInfo(String extraInfo) {
192
227
return builder ().extraInfo (extraInfo );
193
228
}
194
229
230
+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
231
+ public NetworkState getNetworkState () {
232
+ return networkState ;
233
+ }
234
+
195
235
@ Override public boolean equals (Object o ) {
196
236
if (this == o ) {
197
237
return true ;
@@ -298,14 +338,17 @@ public final static class Builder {
298
338
private String subTypeName = "NONE" ; // NOPMD
299
339
private String reason = "" ; // NOPMD
300
340
private String extraInfo = "" ; // NOPMD
341
+ private NetworkState networkState = new NetworkState ();
301
342
302
343
public Builder state (NetworkInfo .State state ) {
303
344
this .state = state ;
345
+ this .networkState .setConnected (state == NetworkInfo .State .CONNECTED );
304
346
return this ;
305
347
}
306
348
307
349
public Builder detailedState (NetworkInfo .DetailedState detailedState ) {
308
350
this .detailedState = detailedState ;
351
+ this .networkState .setConnected (detailedState == NetworkInfo .DetailedState .CONNECTED );
309
352
return this ;
310
353
}
311
354
@@ -354,6 +397,11 @@ public Builder extraInfo(String extraInfo) {
354
397
return this ;
355
398
}
356
399
400
+ public Builder networkState (NetworkState networkState ) {
401
+ this .networkState = networkState ;
402
+ return this ;
403
+ }
404
+
357
405
public Connectivity build () {
358
406
return new Connectivity (this );
359
407
}
0 commit comments