Check for connectivity changes

pull/184/head
M66B 4 years ago
parent a6a7c43c14
commit b184a2f344

@ -1693,77 +1693,106 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
private Boolean last4 = null; private NetworkCapabilities lastActiveCaps = null;
private Boolean last6 = null; private LinkProperties lastActiveProps = null;
@Override @Override
public void onAvailable(@NonNull Network network) { public void onAvailable(@NonNull Network network) {
try { try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getNetworkInfo(network); NetworkInfo ni = cm.getNetworkInfo(network);
NetworkCapabilities caps = cm.getNetworkCapabilities(network); NetworkInfo ani = cm.getActiveNetworkInfo();
EntityLog.log(ServiceSynchronize.this, "Available network=" + network + " info=" + ni + " active=" + ani);
// Transition from metered to unmetered? } catch (Throwable ex) {
boolean transition = false; Log.w(ex);
if (caps != null &&
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) &&
caps.hasCapability((NetworkCapabilities.NET_CAPABILITY_NOT_METERED))) {
Network[] networks = cm.getAllNetworks();
for (Network other : networks)
if (!network.equals(other)) {
NetworkCapabilities c = cm.getNetworkCapabilities(other);
if (c != null &&
c.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) &&
!c.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) {
transition = true;
break;
}
} }
updateState(network, null);
} }
EntityLog.log(ServiceSynchronize.this, @Override
"Available network=" + network + " info=" + ni + " caps=" + caps + " transition=" + transition); public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities caps) {
updateState(network, caps);
if (transition) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Network active = cm.getActiveNetwork();
if (active != null && active.equals(network)) {
boolean reload = (lastActiveCaps != null &&
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) &&
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) &&
lastActiveCaps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) &&
!lastActiveCaps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED));
if (reload || BuildConfig.DEBUG)
EntityLog.log(ServiceSynchronize.this, "Connectivity changed " + network +
" caps=" + caps + " reload=" + reload);
if (reload)
reload(ServiceSynchronize.this, -1L, false, "unmetered"); reload(ServiceSynchronize.this, -1L, false, "unmetered");
lastActiveCaps = caps;
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.e(ex);
} }
updateState(network, null);
} }
@Override @Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities capabilities) { public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties props) {
updateState(network, capabilities); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Network active = cm.getActiveNetwork();
if (active != null && active.equals(network)) {
boolean ahas4 = false;
boolean ahas6 = false;
boolean lhas4 = false;
boolean lhas6 = false;
if (lastActiveProps != null) {
String aname = props.getInterfaceName();
String lname = lastActiveProps.getInterfaceName();
if (!TextUtils.isEmpty(aname) && !TextUtils.isEmpty(lname)) {
NetworkInterface aintf = NetworkInterface.getByName(aname);
NetworkInterface lintf = NetworkInterface.getByName(lname);
if (aintf != null && lintf != null) {
for (InterfaceAddress iaddr : aintf.getInterfaceAddresses()) {
InetAddress addr = iaddr.getAddress();
if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress())
if (addr instanceof Inet4Address)
ahas4 = true;
else if (addr instanceof Inet6Address)
ahas6 = true;
} }
@Override for (InterfaceAddress iaddr : lintf.getInterfaceAddresses()) {
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties properties) {
try {
// Monitor IP v4/v6 availability
boolean has4 = false;
boolean has6 = false;
String name = properties.getInterfaceName();
NetworkInterface ni = NetworkInterface.getByName(name);
if (ni != null)
for (InterfaceAddress iaddr : ni.getInterfaceAddresses()) {
InetAddress addr = iaddr.getAddress(); InetAddress addr = iaddr.getAddress();
if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress()) if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress())
if (addr instanceof Inet4Address) if (addr instanceof Inet4Address)
has4 = true; lhas4 = true;
else if (addr instanceof Inet6Address) else if (addr instanceof Inet6Address)
has6 = true; lhas6 = true;
}
}
}
} }
EntityLog.log(ServiceSynchronize.this, boolean reload = ((ahas4 && !lhas4) || (ahas6 && !lhas6));
"IP intf=" + name + "/" + ni.getDisplayName() + " v4=" + last4 + "/" + has4 + " v6=" + last6 + "/" + has6);
if ((last4 != null && last4 && !has4) || (last6 != null && last6 && !has6)) if (reload || BuildConfig.DEBUG)
reload(ServiceSynchronize.this, -1L, false, "IP"); EntityLog.log(ServiceSynchronize.this, "Connectivity changed " + network +
" props=" + props + " reload=" + reload);
if (reload)
reload(ServiceSynchronize.this, -1L, false, "connectivity");
lastActiveProps = props;
}
last4 = has4;
last6 = has6;
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.e(ex);
} }
} }
@ -1778,6 +1807,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
updateState(network, null); updateState(network, null);
} }

Loading…
Cancel
Save