Skip to content

Commit eb37526

Browse files
committed
fixes in the logic of MarshmallowNetworkObservingStrategy and tests according to PR #455 related to utilization of the NetworkState class
1 parent 0bcd5fe commit eb37526

File tree

14 files changed

+180
-206
lines changed

14 files changed

+180
-206
lines changed

app-kotlin/src/main/kotlin/com/github/pwittchen/reactivenetwork/kotlinapp/MainActivity.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ class MainActivity : Activity() {
4646
.observeOn(AndroidSchedulers.mainThread())
4747
.subscribe { connectivity ->
4848
Log.d(TAG, connectivity.toString())
49-
val state = connectivity.state()
50-
val name = connectivity.typeName()
51-
connectivity_status.text = String.format("state: %s, typeName: %s", state, name)
49+
val state = connectivity.networkState()
50+
val capabilities = connectivity.networkState()!!.networkCapabilities
51+
val isConnected = state!!.isConnected
52+
connectivity_status.text = String.format("connected: %s, capabilities: %s", isConnected, capabilities)
5253
}
5354

5455
internetDisposable = ReactiveNetwork.observeInternetConnectivity()

app/src/main/java/com/github/pwittchen/reactivenetwork/app/MainActivity.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
package com.github.pwittchen.reactivenetwork.app;
1717

1818
import android.app.Activity;
19-
import android.net.NetworkInfo;
19+
import android.net.NetworkCapabilities;
20+
import android.net.TransportInfo;
21+
import android.os.Build;
2022
import android.os.Bundle;
2123
import android.util.Log;
2224
import android.widget.TextView;
25+
import androidx.annotation.RequiresApi;
2326
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork;
27+
import com.github.pwittchen.reactivenetwork.library.rx2.info.NetworkState;
2428
import io.reactivex.android.schedulers.AndroidSchedulers;
2529
import io.reactivex.disposables.Disposable;
2630
import io.reactivex.schedulers.Schedulers;
@@ -39,17 +43,25 @@ public class MainActivity extends Activity {
3943
tvInternetStatus = (TextView) findViewById(R.id.internet_status);
4044
}
4145

42-
@Override protected void onResume() {
46+
@SuppressWarnings("ConstantConditions")
47+
@RequiresApi(api = Build.VERSION_CODES.Q)
48+
@Override
49+
protected void onResume() {
4350
super.onResume();
4451

4552
networkDisposable = ReactiveNetwork.observeNetworkConnectivity(getApplicationContext())
4653
.subscribeOn(Schedulers.io())
4754
.observeOn(AndroidSchedulers.mainThread())
4855
.subscribe(connectivity -> {
4956
Log.d(TAG, connectivity.toString());
50-
final NetworkInfo.State state = connectivity.state();
51-
final String name = connectivity.typeName();
52-
tvConnectivityStatus.setText(String.format("state: %s, typeName: %s", state, name));
57+
58+
NetworkState state = connectivity.networkState();
59+
NetworkCapabilities capabilities = connectivity.networkState().getNetworkCapabilities();
60+
boolean isConnected = state.isConnected();
61+
62+
tvConnectivityStatus.setText(String.format(
63+
"connected: %s, capabilities: %s", isConnected, capabilities
64+
));
5365
});
5466

5567
internetDisposable = ReactiveNetwork.observeInternetConnectivity()

library/src/main/java/com/github/pwittchen/reactivenetwork/library/rx2/Connectivity.java

+44-33
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,44 @@
1717

1818
import android.content.Context;
1919
import android.net.ConnectivityManager;
20-
import android.net.Network;
21-
import android.net.NetworkCapabilities;
2220
import android.net.NetworkInfo;
2321
import android.os.Build;
2422
import androidx.annotation.NonNull;
23+
import androidx.annotation.Nullable;
2524
import androidx.annotation.RequiresApi;
26-
2725
import 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)
3330
public 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;

library/src/main/java/com/github/pwittchen/reactivenetwork/library/rx2/info/NetworkState.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
import android.net.LinkProperties;
44
import android.net.Network;
55
import android.net.NetworkCapabilities;
6+
import androidx.annotation.Nullable;
7+
import com.jakewharton.nopen.annotation.Open;
68

7-
/**
8-
* NetworkState data object
9-
*/
9+
@Open
1010
public class NetworkState {
11-
private boolean isConnected = false;
12-
private Network network = null;
13-
private NetworkCapabilities networkCapabilities = null;
14-
private LinkProperties linkProperties = null;
11+
@SuppressWarnings("PMD") private boolean isConnected = false;
12+
@Nullable private Network network = null;
13+
@Nullable private NetworkCapabilities networkCapabilities = null;
14+
@Nullable private LinkProperties linkProperties = null;
1515

16+
@SuppressWarnings("PMD")
1617
public boolean isConnected() {
1718
return isConnected;
1819
}
@@ -21,27 +22,27 @@ public void setConnected(boolean connected) {
2122
isConnected = connected;
2223
}
2324

24-
public Network getNetwork() {
25+
@Nullable public Network getNetwork() {
2526
return network;
2627
}
2728

28-
public void setNetwork(Network network) {
29+
public void setNetwork(@Nullable Network network) {
2930
this.network = network;
3031
}
3132

32-
public NetworkCapabilities getNetworkCapabilities() {
33+
@Nullable public NetworkCapabilities getNetworkCapabilities() {
3334
return networkCapabilities;
3435
}
3536

36-
public void setNetworkCapabilities(NetworkCapabilities networkCapabilities) {
37+
public void setNetworkCapabilities(@Nullable NetworkCapabilities networkCapabilities) {
3738
this.networkCapabilities = networkCapabilities;
3839
}
3940

40-
public LinkProperties getLinkProperties() {
41+
@Nullable public LinkProperties getLinkProperties() {
4142
return linkProperties;
4243
}
4344

44-
public void setLinkProperties(LinkProperties linkProperties) {
45+
public void setLinkProperties(@Nullable LinkProperties linkProperties) {
4546
this.linkProperties = linkProperties;
4647
}
4748
}

0 commit comments

Comments
 (0)