Skip to content

Commit

Permalink
fixes in the logic of MarshmallowNetworkObservingStrategy and tests a…
Browse files Browse the repository at this point in the history
…ccording to PR #455 related to utilization of the NetworkState class
  • Loading branch information
pwittchen committed Dec 4, 2022
1 parent 0bcd5fe commit eb37526
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ class MainActivity : Activity() {
.observeOn(AndroidSchedulers.mainThread())
.subscribe { connectivity ->
Log.d(TAG, connectivity.toString())
val state = connectivity.state()
val name = connectivity.typeName()
connectivity_status.text = String.format("state: %s, typeName: %s", state, name)
val state = connectivity.networkState()
val capabilities = connectivity.networkState()!!.networkCapabilities
val isConnected = state!!.isConnected
connectivity_status.text = String.format("connected: %s, capabilities: %s", isConnected, capabilities)
}

internetDisposable = ReactiveNetwork.observeInternetConnectivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
package com.github.pwittchen.reactivenetwork.app;

import android.app.Activity;
import android.net.NetworkInfo;
import android.net.NetworkCapabilities;
import android.net.TransportInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork;
import com.github.pwittchen.reactivenetwork.library.rx2.info.NetworkState;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
Expand All @@ -39,17 +43,25 @@ public class MainActivity extends Activity {
tvInternetStatus = (TextView) findViewById(R.id.internet_status);
}

@Override protected void onResume() {
@SuppressWarnings("ConstantConditions")
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onResume() {
super.onResume();

networkDisposable = ReactiveNetwork.observeNetworkConnectivity(getApplicationContext())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(connectivity -> {
Log.d(TAG, connectivity.toString());
final NetworkInfo.State state = connectivity.state();
final String name = connectivity.typeName();
tvConnectivityStatus.setText(String.format("state: %s, typeName: %s", state, name));

NetworkState state = connectivity.networkState();
NetworkCapabilities capabilities = connectivity.networkState().getNetworkCapabilities();
boolean isConnected = state.isConnected();

tvConnectivityStatus.setText(String.format(
"connected: %s, capabilities: %s", isConnected, capabilities
));
});

internetDisposable = ReactiveNetwork.observeInternetConnectivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,44 @@

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.Nullable;
import androidx.annotation.RequiresApi;

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

/**
* Connectivity class represents current connectivity status. It wraps NetworkInfo object.
*/
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
public final class Connectivity {
static final int UNKNOWN_TYPE = -1;
static final int UNKNOWN_SUB_TYPE = -1;
private NetworkInfo.State state; // NOPMD
private NetworkInfo.DetailedState detailedState; // NOPMD
private int type; // NOPMD
private int subType; // NOPMD
private boolean available; // NOPMD
private boolean failover; // NOPMD
private boolean roaming; // NOPMD
private String typeName; // NOPMD
private String subTypeName; // NOPMD
private String reason; // NOPMD
private String extraInfo; // NOPMD
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private NetworkState networkState;
@Nullable private NetworkInfo.State state; // NOPMD
@Nullable private NetworkInfo.DetailedState detailedState; // NOPMD
@Nullable @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private NetworkState networkState; // NOPMD
private final int type; // NOPMD
private final int subType; // NOPMD
private final boolean available; // NOPMD
private final boolean failover; // NOPMD
private final boolean roaming; // NOPMD
private final String typeName; // NOPMD
private final String subTypeName; // NOPMD
private final String reason; // NOPMD
private final String extraInfo; // NOPMD

public static Connectivity create() {
return builder().build();
}

@SuppressWarnings("PMD")
public static Connectivity create(@NonNull Context context) {
Preconditions.checkNotNull(context, "context == null");
return create(context, getConnectivityManager(context));
}

@SuppressWarnings("PMD")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static Connectivity create(@NonNull Context context, NetworkState networkState) {
Preconditions.checkNotNull(context, "context == null");
Expand All @@ -67,6 +66,7 @@ private static ConnectivityManager getConnectivityManager(Context context) {
return (ConnectivityManager) context.getSystemService(service);
}

@SuppressWarnings("PMD")
protected static Connectivity create(@NonNull Context context, ConnectivityManager manager) {
Preconditions.checkNotNull(context, "context == null");

Expand All @@ -78,8 +78,11 @@ protected static Connectivity create(@NonNull Context context, ConnectivityManag
return (networkInfo == null) ? create() : create(networkInfo);
}

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

if (manager == null) {
Expand Down Expand Up @@ -109,16 +112,22 @@ private static Connectivity create(NetworkInfo networkInfo) {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static Connectivity create(NetworkState networkState) {
return new Builder()
.networkState(networkState)
.build();
.networkState(networkState)
.build();
}

private Connectivity(Builder builder) {
if(Preconditions.isAtLeastAndroidLollipop()) {
networkState = builder.networkState;
if (Preconditions.isAtLeastAndroidLollipop()) {
if (builder.networkState != null) {
networkState = builder.networkState;
}
} else {
state = builder.state;
detailedState = builder.detailedState;
if (builder.state != null) {
state = builder.state;
}
if (builder.detailedState != null) {
detailedState = builder.detailedState;
}
}
type = builder.type;
subType = builder.subType;
Expand All @@ -139,22 +148,29 @@ private static Builder builder() {
return new Connectivity.Builder();
}

public NetworkInfo.State state() {
public @Nullable NetworkInfo.State state() {
return state;
}

public static Builder state(NetworkInfo.State state) {
return builder().state(state);
}

public NetworkInfo.DetailedState detailedState() {
public @Nullable NetworkInfo.DetailedState detailedState() {
return detailedState;
}

public static Builder state(NetworkInfo.DetailedState detailedState) {
return builder().detailedState(detailedState);
}

@Nullable
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings("PMD")
public NetworkState networkState() {
return networkState;
}

public int type() {
return type;
}
Expand Down Expand Up @@ -227,11 +243,6 @@ 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 @@ -277,7 +288,7 @@ public NetworkState getNetworkState() {
}

@Override public int hashCode() {
int result = state.hashCode();
int result = state != null ? state.hashCode() : 0;
result = 31 * result + (detailedState != null ? detailedState.hashCode() : 0);
result = 31 * result + type;
result = 31 * result + subType;
Expand Down Expand Up @@ -338,7 +349,7 @@ public final static class Builder {
private String subTypeName = "NONE"; // NOPMD
private String reason = ""; // NOPMD
private String extraInfo = ""; // NOPMD
private NetworkState networkState = new NetworkState();
private NetworkState networkState = new NetworkState(); // NOPMD

public Builder state(NetworkInfo.State state) {
this.state = state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import androidx.annotation.Nullable;
import com.jakewharton.nopen.annotation.Open;

/**
* NetworkState data object
*/
@Open
public class NetworkState {
private boolean isConnected = false;
private Network network = null;
private NetworkCapabilities networkCapabilities = null;
private LinkProperties linkProperties = null;
@SuppressWarnings("PMD") private boolean isConnected = false;
@Nullable private Network network = null;
@Nullable private NetworkCapabilities networkCapabilities = null;
@Nullable private LinkProperties linkProperties = null;

@SuppressWarnings("PMD")
public boolean isConnected() {
return isConnected;
}
Expand All @@ -21,27 +22,27 @@ public void setConnected(boolean connected) {
isConnected = connected;
}

public Network getNetwork() {
@Nullable public Network getNetwork() {
return network;
}

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

public NetworkCapabilities getNetworkCapabilities() {
@Nullable public NetworkCapabilities getNetworkCapabilities() {
return networkCapabilities;
}

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

public LinkProperties getLinkProperties() {
@Nullable public LinkProperties getLinkProperties() {
return linkProperties;
}

public void setLinkProperties(LinkProperties linkProperties) {
public void setLinkProperties(@Nullable LinkProperties linkProperties) {
this.linkProperties = linkProperties;
}
}
Loading

0 comments on commit eb37526

Please sign in to comment.