Skip to content

Commit

Permalink
Merge branch 'chaesung-network_info_issue' into RxJava2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittchen committed Dec 4, 2022
2 parents ddfde34 + c3371e1 commit 0bcd5fe
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.github.pwittchen.reactivenetwork.library.rx2.info.NetworkState;

/**
* Connectivity class represents current connectivity status. It wraps NetworkInfo object.
*/
Expand All @@ -40,6 +44,8 @@ public final class Connectivity {
private String subTypeName; // NOPMD
private String reason; // NOPMD
private String extraInfo; // NOPMD
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private NetworkState networkState;

public static Connectivity create() {
return builder().build();
Expand All @@ -50,6 +56,12 @@ public static Connectivity create(@NonNull Context context) {
return create(context, getConnectivityManager(context));
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static Connectivity create(@NonNull Context context, NetworkState networkState) {
Preconditions.checkNotNull(context, "context == null");
return create(context, getConnectivityManager(context), networkState);
}

private static ConnectivityManager getConnectivityManager(Context context) {
final String service = Context.CONNECTIVITY_SERVICE;
return (ConnectivityManager) context.getSystemService(service);
Expand All @@ -66,6 +78,18 @@ protected static Connectivity create(@NonNull Context context, ConnectivityManag
return (networkInfo == null) ? create() : create(networkInfo);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
protected static Connectivity create(@NonNull Context context, ConnectivityManager manager, NetworkState networkState) {
Preconditions.checkNotNull(context, "context == null");

if (manager == null) {
return create();
}
networkState.setNetworkCapabilities(manager.getNetworkCapabilities(networkState.getNetwork()));
networkState.setLinkProperties(manager.getLinkProperties(networkState.getNetwork()));
return create(networkState);
}

private static Connectivity create(NetworkInfo networkInfo) {
return new Builder()
.state(networkInfo.getState())
Expand All @@ -82,9 +106,20 @@ private static Connectivity create(NetworkInfo networkInfo) {
.build();
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static Connectivity create(NetworkState networkState) {
return new Builder()
.networkState(networkState)
.build();
}

private Connectivity(Builder builder) {
state = builder.state;
detailedState = builder.detailedState;
if(Preconditions.isAtLeastAndroidLollipop()) {
networkState = builder.networkState;
} else {
state = builder.state;
detailedState = builder.detailedState;
}
type = builder.type;
subType = builder.subType;
available = builder.available;
Expand Down Expand Up @@ -192,6 +227,11 @@ public static Builder extraInfo(String extraInfo) {
return builder().extraInfo(extraInfo);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public NetworkState getNetworkState() {
return networkState;
}

@Override public boolean equals(Object o) {
if (this == o) {
return true;
Expand Down Expand Up @@ -298,14 +338,17 @@ public final static class Builder {
private String subTypeName = "NONE"; // NOPMD
private String reason = ""; // NOPMD
private String extraInfo = ""; // NOPMD
private NetworkState networkState = new NetworkState();

public Builder state(NetworkInfo.State state) {
this.state = state;
this.networkState.setConnected(state == NetworkInfo.State.CONNECTED);
return this;
}

public Builder detailedState(NetworkInfo.DetailedState detailedState) {
this.detailedState = detailedState;
this.networkState.setConnected(detailedState == NetworkInfo.DetailedState.CONNECTED);
return this;
}

Expand Down Expand Up @@ -354,6 +397,11 @@ public Builder extraInfo(String extraInfo) {
return this;
}

public Builder networkState(NetworkState networkState) {
this.networkState = networkState;
return this;
}

public Connectivity build() {
return new Connectivity(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.pwittchen.reactivenetwork.library.rx2.info;

import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;

/**
* NetworkState data object
*/
public class NetworkState {
private boolean isConnected = false;
private Network network = null;
private NetworkCapabilities networkCapabilities = null;
private LinkProperties linkProperties = null;

public boolean isConnected() {
return isConnected;
}

public void setConnected(boolean connected) {
isConnected = connected;
}

public Network getNetwork() {
return network;
}

public void setNetwork(Network network) {
this.network = network;
}

public NetworkCapabilities getNetworkCapabilities() {
return networkCapabilities;
}

public void setNetworkCapabilities(NetworkCapabilities networkCapabilities) {
this.networkCapabilities = networkCapabilities;
}

public LinkProperties getLinkProperties() {
return linkProperties;
}

public void setLinkProperties(LinkProperties linkProperties) {
this.linkProperties = linkProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.util.Log;

import androidx.annotation.NonNull;

import com.github.pwittchen.reactivenetwork.library.rx2.Connectivity;
import com.github.pwittchen.reactivenetwork.library.rx2.info.NetworkState;
import com.github.pwittchen.reactivenetwork.library.rx2.network.observing.NetworkObservingStrategy;
import com.jakewharton.nopen.annotation.Open;
import io.reactivex.Observable;
Expand All @@ -40,6 +46,7 @@
implements NetworkObservingStrategy {
@SuppressWarnings("NullAway") // it has to be initialized in the Observable due to Context
private NetworkCallback networkCallback;
private NetworkState networkState = new NetworkState();

@Override public Observable<Connectivity> observeNetworkConnectivity(final Context context) {
final String service = Context.CONNECTIVITY_SERVICE;
Expand Down Expand Up @@ -73,12 +80,30 @@ private void tryToUnregisterCallback(final ConnectivityManager manager) {
private NetworkCallback createNetworkCallback(final ObservableEmitter<Connectivity> subscriber,
final Context context) {
return new ConnectivityManager.NetworkCallback() {
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
networkState.setNetwork(network);
networkState.setNetworkCapabilities(networkCapabilities);
subscriber.onNext(Connectivity.create(context, networkState));
}

@Override
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
networkState.setNetwork(network);
networkState.setLinkProperties(linkProperties);
subscriber.onNext(Connectivity.create(context, networkState));
}

@Override public void onAvailable(Network network) {
subscriber.onNext(Connectivity.create(context));
networkState.setNetwork(network);
networkState.setConnected(true);
subscriber.onNext(Connectivity.create(context, networkState));
}

@Override public void onLost(Network network) {
subscriber.onNext(Connectivity.create(context));
networkState.setNetwork(network);
networkState.setConnected(false);
subscriber.onNext(Connectivity.create(context, networkState));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
Expand All @@ -29,6 +30,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
import com.github.pwittchen.reactivenetwork.library.rx2.Connectivity;
import com.github.pwittchen.reactivenetwork.library.rx2.info.NetworkState;
import com.github.pwittchen.reactivenetwork.library.rx2.network.observing.NetworkObservingStrategy;
import com.jakewharton.nopen.annotation.Open;
import io.reactivex.BackpressureStrategy;
Expand Down Expand Up @@ -58,6 +60,7 @@
private final Subject<Connectivity> connectivitySubject;
private final BroadcastReceiver idleReceiver;
private Connectivity lastConnectivity = Connectivity.create();
private NetworkState networkState = new NetworkState();

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

protected ConnectivityManager.NetworkCallback createNetworkCallback(final Context context) {
return new ConnectivityManager.NetworkCallback() {
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
networkState.setNetwork(network);
networkState.setNetworkCapabilities(networkCapabilities);
onNext(Connectivity.create(context, networkState));
}

@Override
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
networkState.setNetwork(network);
networkState.setLinkProperties(linkProperties);
onNext(Connectivity.create(context, networkState));
}

@Override public void onAvailable(Network network) {
onNext(Connectivity.create(context));
networkState.setNetwork(network);
networkState.setConnected(true);
onNext(Connectivity.create(context, networkState));
}

@Override public void onLost(Network network) {
onNext(Connectivity.create(context));
networkState.setNetwork(network);
networkState.setConnected(false);
onNext(Connectivity.create(context, networkState));
}
};
}
Expand Down

0 comments on commit 0bcd5fe

Please sign in to comment.