17
17
18
18
import android .content .Context ;
19
19
import android .net .ConnectivityManager ;
20
- import android .net .Network ;
21
- import android .net .NetworkCapabilities ;
22
20
import android .net .NetworkInfo ;
23
21
import android .os .Build ;
24
22
import androidx .annotation .NonNull ;
23
+ import androidx .annotation .Nullable ;
25
24
import androidx .annotation .RequiresApi ;
26
-
27
25
import com .github .pwittchen .reactivenetwork .library .rx2 .info .NetworkState ;
28
26
29
27
/**
30
28
* Connectivity class represents current connectivity status. It wraps NetworkInfo object.
31
29
*/
32
- @ RequiresApi (api = Build .VERSION_CODES .CUPCAKE )
33
30
public final class Connectivity {
34
31
static final int UNKNOWN_TYPE = -1 ;
35
32
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
49
46
50
47
public static Connectivity create () {
51
48
return builder ().build ();
52
49
}
53
50
51
+ @ SuppressWarnings ("PMD" )
54
52
public static Connectivity create (@ NonNull Context context ) {
55
53
Preconditions .checkNotNull (context , "context == null" );
56
54
return create (context , getConnectivityManager (context ));
57
55
}
58
56
57
+ @ SuppressWarnings ("PMD" )
59
58
@ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
60
59
public static Connectivity create (@ NonNull Context context , NetworkState networkState ) {
61
60
Preconditions .checkNotNull (context , "context == null" );
@@ -67,6 +66,7 @@ private static ConnectivityManager getConnectivityManager(Context context) {
67
66
return (ConnectivityManager ) context .getSystemService (service );
68
67
}
69
68
69
+ @ SuppressWarnings ("PMD" )
70
70
protected static Connectivity create (@ NonNull Context context , ConnectivityManager manager ) {
71
71
Preconditions .checkNotNull (context , "context == null" );
72
72
@@ -78,8 +78,11 @@ protected static Connectivity create(@NonNull Context context, ConnectivityManag
78
78
return (networkInfo == null ) ? create () : create (networkInfo );
79
79
}
80
80
81
+ @ SuppressWarnings ("PMD" )
81
82
@ 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
+ ) {
83
86
Preconditions .checkNotNull (context , "context == null" );
84
87
85
88
if (manager == null ) {
@@ -109,16 +112,22 @@ private static Connectivity create(NetworkInfo networkInfo) {
109
112
@ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
110
113
private static Connectivity create (NetworkState networkState ) {
111
114
return new Builder ()
112
- .networkState (networkState )
113
- .build ();
115
+ .networkState (networkState )
116
+ .build ();
114
117
}
115
118
116
119
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
+ }
119
124
} 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
+ }
122
131
}
123
132
type = builder .type ;
124
133
subType = builder .subType ;
@@ -139,22 +148,29 @@ private static Builder builder() {
139
148
return new Connectivity .Builder ();
140
149
}
141
150
142
- public NetworkInfo .State state () {
151
+ public @ Nullable NetworkInfo .State state () {
143
152
return state ;
144
153
}
145
154
146
155
public static Builder state (NetworkInfo .State state ) {
147
156
return builder ().state (state );
148
157
}
149
158
150
- public NetworkInfo .DetailedState detailedState () {
159
+ public @ Nullable NetworkInfo .DetailedState detailedState () {
151
160
return detailedState ;
152
161
}
153
162
154
163
public static Builder state (NetworkInfo .DetailedState detailedState ) {
155
164
return builder ().detailedState (detailedState );
156
165
}
157
166
167
+ @ Nullable
168
+ @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
169
+ @ SuppressWarnings ("PMD" )
170
+ public NetworkState networkState () {
171
+ return networkState ;
172
+ }
173
+
158
174
public int type () {
159
175
return type ;
160
176
}
@@ -227,11 +243,6 @@ public static Builder extraInfo(String extraInfo) {
227
243
return builder ().extraInfo (extraInfo );
228
244
}
229
245
230
- @ RequiresApi (api = Build .VERSION_CODES .LOLLIPOP )
231
- public NetworkState getNetworkState () {
232
- return networkState ;
233
- }
234
-
235
246
@ Override public boolean equals (Object o ) {
236
247
if (this == o ) {
237
248
return true ;
@@ -277,7 +288,7 @@ public NetworkState getNetworkState() {
277
288
}
278
289
279
290
@ Override public int hashCode () {
280
- int result = state .hashCode ();
291
+ int result = state != null ? state .hashCode () : 0 ;
281
292
result = 31 * result + (detailedState != null ? detailedState .hashCode () : 0 );
282
293
result = 31 * result + type ;
283
294
result = 31 * result + subType ;
@@ -338,7 +349,7 @@ public final static class Builder {
338
349
private String subTypeName = "NONE" ; // NOPMD
339
350
private String reason = "" ; // NOPMD
340
351
private String extraInfo = "" ; // NOPMD
341
- private NetworkState networkState = new NetworkState ();
352
+ private NetworkState networkState = new NetworkState (); // NOPMD
342
353
343
354
public Builder state (NetworkInfo .State state ) {
344
355
this .state = state ;
0 commit comments