Improved network management

This commit is contained in:
M66B
2020-10-28 11:44:15 +01:00
parent fdcb62fb95
commit 791a0a4fe7
2 changed files with 68 additions and 156 deletions

View File

@@ -88,7 +88,7 @@ public class ConnectionHelper {
private Boolean suitable = null;
private Boolean unmetered = null;
private Boolean roaming = null;
private Network active;
private Network active = null;
boolean isConnected() {
return (connected != null && connected);
@@ -125,10 +125,35 @@ public class ConnectionHelper {
return (Objects.equals(this.connected, other.connected) &&
Objects.equals(this.suitable, other.suitable) &&
Objects.equals(this.unmetered, other.unmetered) &&
Objects.equals(this.roaming, other.roaming));
Objects.equals(this.roaming, other.roaming) &&
Objects.equals(this.active, other.active));
} else
return false;
}
@Override
public String toString() {
return "connected=" + connected +
" suitable=" + suitable +
" unmetered=" + unmetered +
" roaming=" + roaming +
" active=" + active;
}
}
static boolean isConnected(Context context, Network network) {
NetworkInfo ni = getNetworkInfo(context, network);
return (ni != null && ni.isConnected());
}
static NetworkInfo getNetworkInfo(Context context, Network network) {
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return (cm == null ? null : cm.getNetworkInfo(network));
} catch (Throwable ex) {
Log.e(ex);
return null;
}
}
static NetworkState getNetworkState(Context context) {