Skip to content

Commit 0bcd5fe

Browse files
committed
Merge branch 'chaesung-network_info_issue' into RxJava2.x
2 parents ddfde34 + c3371e1 commit 0bcd5fe

File tree

4 files changed

+147
-6
lines changed

4 files changed

+147
-6
lines changed

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

+50-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717

1818
import android.content.Context;
1919
import android.net.ConnectivityManager;
20+
import android.net.Network;
21+
import android.net.NetworkCapabilities;
2022
import android.net.NetworkInfo;
2123
import android.os.Build;
2224
import androidx.annotation.NonNull;
2325
import 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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.pwittchen.reactivenetwork.library.rx2.info;
2+
3+
import android.net.LinkProperties;
4+
import android.net.Network;
5+
import android.net.NetworkCapabilities;
6+
7+
/**
8+
* NetworkState data object
9+
*/
10+
public class NetworkState {
11+
private boolean isConnected = false;
12+
private Network network = null;
13+
private NetworkCapabilities networkCapabilities = null;
14+
private LinkProperties linkProperties = null;
15+
16+
public boolean isConnected() {
17+
return isConnected;
18+
}
19+
20+
public void setConnected(boolean connected) {
21+
isConnected = connected;
22+
}
23+
24+
public Network getNetwork() {
25+
return network;
26+
}
27+
28+
public void setNetwork(Network network) {
29+
this.network = network;
30+
}
31+
32+
public NetworkCapabilities getNetworkCapabilities() {
33+
return networkCapabilities;
34+
}
35+
36+
public void setNetworkCapabilities(NetworkCapabilities networkCapabilities) {
37+
this.networkCapabilities = networkCapabilities;
38+
}
39+
40+
public LinkProperties getLinkProperties() {
41+
return linkProperties;
42+
}
43+
44+
public void setLinkProperties(LinkProperties linkProperties) {
45+
this.linkProperties = linkProperties;
46+
}
47+
}

library/src/main/java/com/github/pwittchen/reactivenetwork/library/rx2/network/observing/strategy/LollipopNetworkObservingStrategy.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
import android.content.Context;
2020
import android.net.ConnectivityManager;
2121
import android.net.ConnectivityManager.NetworkCallback;
22+
import android.net.LinkProperties;
2223
import android.net.Network;
24+
import android.net.NetworkCapabilities;
2325
import android.net.NetworkRequest;
2426
import android.util.Log;
27+
28+
import androidx.annotation.NonNull;
29+
2530
import com.github.pwittchen.reactivenetwork.library.rx2.Connectivity;
31+
import com.github.pwittchen.reactivenetwork.library.rx2.info.NetworkState;
2632
import com.github.pwittchen.reactivenetwork.library.rx2.network.observing.NetworkObservingStrategy;
2733
import com.jakewharton.nopen.annotation.Open;
2834
import io.reactivex.Observable;
@@ -40,6 +46,7 @@
4046
implements NetworkObservingStrategy {
4147
@SuppressWarnings("NullAway") // it has to be initialized in the Observable due to Context
4248
private NetworkCallback networkCallback;
49+
private NetworkState networkState = new NetworkState();
4350

4451
@Override public Observable<Connectivity> observeNetworkConnectivity(final Context context) {
4552
final String service = Context.CONNECTIVITY_SERVICE;
@@ -73,12 +80,30 @@ private void tryToUnregisterCallback(final ConnectivityManager manager) {
7380
private NetworkCallback createNetworkCallback(final ObservableEmitter<Connectivity> subscriber,
7481
final Context context) {
7582
return new ConnectivityManager.NetworkCallback() {
83+
@Override
84+
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
85+
networkState.setNetwork(network);
86+
networkState.setNetworkCapabilities(networkCapabilities);
87+
subscriber.onNext(Connectivity.create(context, networkState));
88+
}
89+
90+
@Override
91+
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
92+
networkState.setNetwork(network);
93+
networkState.setLinkProperties(linkProperties);
94+
subscriber.onNext(Connectivity.create(context, networkState));
95+
}
96+
7697
@Override public void onAvailable(Network network) {
77-
subscriber.onNext(Connectivity.create(context));
98+
networkState.setNetwork(network);
99+
networkState.setConnected(true);
100+
subscriber.onNext(Connectivity.create(context, networkState));
78101
}
79102

80103
@Override public void onLost(Network network) {
81-
subscriber.onNext(Connectivity.create(context));
104+
networkState.setNetwork(network);
105+
networkState.setConnected(false);
106+
subscriber.onNext(Connectivity.create(context, networkState));
82107
}
83108
};
84109
}

library/src/main/java/com/github/pwittchen/reactivenetwork/library/rx2/network/observing/strategy/MarshmallowNetworkObservingStrategy.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.content.Intent;
2222
import android.content.IntentFilter;
2323
import android.net.ConnectivityManager;
24+
import android.net.LinkProperties;
2425
import android.net.Network;
2526
import android.net.NetworkCapabilities;
2627
import android.net.NetworkInfo;
@@ -29,6 +30,7 @@
2930
import android.util.Log;
3031
import androidx.annotation.NonNull;
3132
import com.github.pwittchen.reactivenetwork.library.rx2.Connectivity;
33+
import com.github.pwittchen.reactivenetwork.library.rx2.info.NetworkState;
3234
import com.github.pwittchen.reactivenetwork.library.rx2.network.observing.NetworkObservingStrategy;
3335
import com.jakewharton.nopen.annotation.Open;
3436
import io.reactivex.BackpressureStrategy;
@@ -58,6 +60,7 @@
5860
private final Subject<Connectivity> connectivitySubject;
5961
private final BroadcastReceiver idleReceiver;
6062
private Connectivity lastConnectivity = Connectivity.create();
63+
private NetworkState networkState = new NetworkState();
6164

6265
@SuppressWarnings("NullAway") // networkCallback cannot be initialized here
6366
public MarshmallowNetworkObservingStrategy() {
@@ -157,12 +160,30 @@ protected void tryToUnregisterReceiver(Context context) {
157160

158161
protected ConnectivityManager.NetworkCallback createNetworkCallback(final Context context) {
159162
return new ConnectivityManager.NetworkCallback() {
163+
@Override
164+
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
165+
networkState.setNetwork(network);
166+
networkState.setNetworkCapabilities(networkCapabilities);
167+
onNext(Connectivity.create(context, networkState));
168+
}
169+
170+
@Override
171+
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
172+
networkState.setNetwork(network);
173+
networkState.setLinkProperties(linkProperties);
174+
onNext(Connectivity.create(context, networkState));
175+
}
176+
160177
@Override public void onAvailable(Network network) {
161-
onNext(Connectivity.create(context));
178+
networkState.setNetwork(network);
179+
networkState.setConnected(true);
180+
onNext(Connectivity.create(context, networkState));
162181
}
163182

164183
@Override public void onLost(Network network) {
165-
onNext(Connectivity.create(context));
184+
networkState.setNetwork(network);
185+
networkState.setConnected(false);
186+
onNext(Connectivity.create(context, networkState));
166187
}
167188
};
168189
}

0 commit comments

Comments
 (0)