Skip to content

fixes in the code and tests according to PR #455 related to utilization of the NetworkState class #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: RxJava2.x
Choose a base branch
from
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 @@ -20,41 +20,53 @@
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
@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");
return create(context, getConnectivityManager(context), networkState);
}

private static ConnectivityManager getConnectivityManager(Context context) {
final String service = Context.CONNECTIVITY_SERVICE;
return (ConnectivityManager) context.getSystemService(service);
}

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

Expand All @@ -66,6 +78,21 @@ 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
) {
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 +109,26 @@ 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()) {
if (builder.networkState != null) {
networkState = builder.networkState;
}
} else {
if (builder.state != null) {
state = builder.state;
}
if (builder.detailedState != null) {
detailedState = builder.detailedState;
}
}
type = builder.type;
subType = builder.subType;
available = builder.available;
Expand All @@ -104,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 @@ -237,7 +288,7 @@ public static Builder extraInfo(String extraInfo) {
}

@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 @@ -298,14 +349,17 @@ public final static class Builder {
private String subTypeName = "NONE"; // NOPMD
private String reason = ""; // NOPMD
private String extraInfo = ""; // NOPMD
private NetworkState networkState = new NetworkState(); // NOPMD

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 +408,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,63 @@
/*
* Copyright (C) 2022 Piotr Wittchen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.pwittchen.reactivenetwork.library.rx2.info;

import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import androidx.annotation.Nullable;
import com.jakewharton.nopen.annotation.Open;

@Open
public class NetworkState {
@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;
}

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

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

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

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

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

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

public void setLinkProperties(@Nullable 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
Loading